From 4706c809a06704b859f077604006cb302a5ffedc Mon Sep 17 00:00:00 2001 From: Aidan Garske Date: Mon, 16 Nov 2026 11:02:49 -0800 Subject: [PATCH] Sign ECDSA without ASN and add WOLFCOSE_NO_LOCAL_SIGN --- .github/workflows/lean-build.yml | 74 +++++++ .gitignore | 1 + Makefile | 33 ++- docs/Macros.md | 11 + include/wolfcose/settings.h | 18 +- src/wolfcose_countersign.c | 6 + src/wolfcose_ecc.c | 24 ++- src/wolfcose_internal.h | 9 +- src/wolfcose_key.c | 2 + src/wolfcose_sign.c | 6 + src/wolfcose_sign1.c | 6 + tests/test_cose.c | 125 ++++++++++- tests/test_cose_examples.c | 6 +- tests/test_eat_psa.c | 4 + tests/test_no_local_sign.c | 350 +++++++++++++++++++++++++++++++ 15 files changed, 649 insertions(+), 26 deletions(-) create mode 100644 tests/test_no_local_sign.c diff --git a/.github/workflows/lean-build.yml b/.github/workflows/lean-build.yml index 1976915..f8dacb0 100644 --- a/.github/workflows/lean-build.yml +++ b/.github/workflows/lean-build.yml @@ -189,8 +189,18 @@ jobs: -DWOLFCOSE_ENABLE_DEPRECATED_ALGS build_cfg "deprecated RFC 9053 alg IDs, Ed448 only" \ -DWOLFCOSE_ENABLE_DEPRECATED_ALGS -DWOLFCOSE_NO_EDDSA + build_cfg "WOLFCOSE_NO_LOCAL_SIGN (external signer only)" \ + -DWOLFCOSE_ENABLE_EXT_SIGN -DWOLFCOSE_NO_LOCAL_SIGN + build_cfg "WOLFCOSE_LEAN + WOLFCOSE_NO_LOCAL_SIGN" -DWOLFCOSE_LEAN \ + -DWOLFCOSE_ENABLE_EXT_SIGN -DWOLFCOSE_NO_LOCAL_SIGN echo "All lean configurations compiled clean." + - name: External signer only (WOLFCOSE_NO_LOCAL_SIGN) + run: | + export WOLFSSL_DIR=$HOME/wolfssl-install + export LD_LIBRARY_PATH=$WOLFSSL_DIR/lib + make no-local-sign-test WOLFSSL_PREFIX=$WOLFSSL_DIR PKG_CONFIG=false + - name: Check PSA/EAT opt-in feature matrix run: | export WOLFSSL_DIR=$HOME/wolfssl-install @@ -268,3 +278,67 @@ jobs: run: | size examples/sign1_mldsa || true size examples/sign1_verify_mldsa || true + + no-asn: + name: ECDSA sign + verify against NO_ASN wolfSSL + runs-on: ubuntu-latest + container: + image: ghcr.io/wolfssl/wolfcose-ci@sha256:c073c7afb4cd3f296b8c1a305db2f718530b7a8aca899482629aee3a5cf8bfbc + + steps: + - uses: actions/checkout@v4 + + - name: Trust workspace + if: runner.os == 'Linux' + run: git config --global --add safe.directory "$GITHUB_WORKSPACE" + + - name: Resolve wolfSSL master commit + id: wolfssl-rev + run: echo "sha=$(git ls-remote https://github.com/wolfSSL/wolfssl.git HEAD | cut -f1)" >> "$GITHUB_OUTPUT" + + - name: Cache wolfSSL (NO_ASN) + id: cache-wolfssl + uses: actions/cache@v4 + with: + path: ~/wolfssl-noasn + key: wolfcose-ci-v1-wolfssl-noasn-v1-${{ steps.wolfssl-rev.outputs.sha }} + + # Deterministic k lets the suite reproduce the ES256 known-answer vector, + # proving NO_ASN signing is byte-identical to the DER path. + - name: Build NO_ASN wolfSSL + if: steps.cache-wolfssl.outputs.cache-hit != 'true' + run: | + cd ~ + git clone --depth 1 https://github.com/wolfSSL/wolfssl.git wolfssl-noasn-src + cd wolfssl-noasn-src + ./autogen.sh + ./configure --enable-cryptonly --enable-ecc --enable-sha384 --enable-sha512 \ + --enable-keygen --enable-asn=no --disable-rsa --disable-dh \ + CPPFLAGS="-DWOLFSSL_ECDSA_DETERMINISTIC_K -DWOLFSSL_NO_MALLOC" \ + --prefix=$HOME/wolfssl-noasn + make -j$(nproc) + make install + + - name: Test suite (local ECDSA sign + verify, known-answer vector) + run: | + export LD_LIBRARY_PATH=$HOME/wolfssl-noasn/lib + make test WOLFSSL_PREFIX=$HOME/wolfssl-noasn PKG_CONFIG=false \ + EXTRA_CFLAGS="-DWOLFCOSE_ENABLE_DETERMINISTIC_ECDSA" + + - name: Assert no DER signature conversion is referenced + run: | + if nm -u src/*.o | grep -E "wc_ecc_(sig_to_rs|rs_raw_to_sig)$"; then + echo "FAIL: DER conversion referenced in a NO_ASN build"; exit 1 + fi + echo "OK: raw r||s used directly" + + - name: Delegated signing suite + run: | + export LD_LIBRARY_PATH=$HOME/wolfssl-noasn/lib + make clean + make ext-sign-test WOLFSSL_PREFIX=$HOME/wolfssl-noasn PKG_CONFIG=false + + - name: External signer only (WOLFCOSE_NO_LOCAL_SIGN) + run: | + export LD_LIBRARY_PATH=$HOME/wolfssl-noasn/lib + make no-local-sign-test WOLFSSL_PREFIX=$HOME/wolfssl-noasn PKG_CONFIG=false diff --git a/.gitignore b/.gitignore index 7ca05ba..d746124 100644 --- a/.gitignore +++ b/.gitignore @@ -15,6 +15,7 @@ experimental-check.err tests/test_wolfcose tests/test_wolfcose_eat_psa tests/test_wolfcose_eat_psa_limits +tests/test_no_local_sign tools/wolfcose_tool examples/lifecycle_demo tests/interop/t_cose/interop_tcose diff --git a/Makefile b/Makefile index c156918..8e64557 100644 --- a/Makefile +++ b/Makefile @@ -166,7 +166,7 @@ SCEN_IOTFLEET = examples/scenarios/iot_fleet_config SCEN_SENSOR = examples/scenarios/sensor_attestation SCEN_BROADCAST = examples/scenarios/group_broadcast_mac -.PHONY: all shared test pkg-config-test ecdsa-policy-test rsapss-policy-test countersign-config-test zero-alloc-check force-zero-check zeroize-test deprecated-algs-test ecc-import-policy-test ext-sign-test ext-sign-demo ext-sign-force-failure coverage eat-psa-test eat-psa-float-test eat-psa-min-buffers-test eat-psa-claim-limits-test eat-psa-profile-test eat-psa-config-check eat-psa-ext-sign-test eat-psa-ext-sign-force-failure eat-psa-coverage eat-psa-coverage-force-failure hpke-coverage-force-failure generic-reduced-alg-test tool tool-test cmdline-test demo demos hpke-demo lean-verify psa-eat-lean-verify psa-eat-demo mldsa-demo mldsa-verify lms-demo lms-verify comprehensive scenarios release-scenarios release-coverage cxx-check valgrind-check release-validate release-artifacts interop-tcose tcose-upstream interop-go-cose interop-python-cwt interop-rust-coset c99-check c99-check-lms c99-hpke-check experimental-check clean FORCE +.PHONY: all shared test pkg-config-test ecdsa-policy-test rsapss-policy-test countersign-config-test zero-alloc-check force-zero-check zeroize-test deprecated-algs-test ecc-import-policy-test ext-sign-test no-local-sign-test ext-sign-demo ext-sign-force-failure coverage eat-psa-test eat-psa-float-test eat-psa-min-buffers-test eat-psa-claim-limits-test eat-psa-profile-test eat-psa-config-check eat-psa-ext-sign-test eat-psa-ext-sign-force-failure eat-psa-coverage eat-psa-coverage-force-failure hpke-coverage-force-failure generic-reduced-alg-test tool tool-test cmdline-test demo demos hpke-demo lean-verify psa-eat-lean-verify psa-eat-demo mldsa-demo mldsa-verify lms-demo lms-verify comprehensive scenarios release-scenarios release-coverage cxx-check valgrind-check release-validate release-artifacts interop-tcose tcose-upstream interop-go-cose interop-python-cwt interop-rust-coset c99-check c99-check-lms c99-hpke-check experimental-check clean FORCE # --- Core library --- all: $(LIB_A) @@ -222,7 +222,8 @@ ECDSA_POLICY_NO_SUPPORT_FLAGS = $(CFLAGS) $(WOLFCOSE_POLICY_DISABLE_HPKE_FLAGS) -UWOLFCOSE_LEAN_VERIFY_MLDSA \ -UWOLFCOSE_LEAN_MLDSA \ -UWOLFCOSE_LEAN_VERIFY_LMS \ - -UWOLFCOSE_LEAN_LMS + -UWOLFCOSE_LEAN_LMS \ + -UWOLFCOSE_NO_LOCAL_SIGN ECDSA_POLICY_FLAGS = $(ECDSA_POLICY_BASE_FLAGS) \ -DWOLFSSL_ECDSA_DETERMINISTIC_K \ -DWOLFCOSE_ENABLE_DETERMINISTIC_ECDSA @@ -251,6 +252,14 @@ ecdsa-policy-test: echo "FAIL: deterministic ECDSA probe failed unexpectedly"; \ exit 1; \ fi; \ + if ! $(CC) $(ECDSA_POLICY_NO_SUPPORT_FLAGS) \ + -DWOLFCOSE_ENABLE_DETERMINISTIC_ECDSA \ + -DWOLFCOSE_ENABLE_EXT_SIGN -DWOLFCOSE_NO_LOCAL_SIGN \ + $(ECDSA_POLICY_HEADER) >"$$log_file" 2>&1; then \ + cat "$$log_file"; \ + echo "FAIL: external-signer-only build rejected for deterministic ECDSA"; \ + exit 1; \ + fi; \ if ! $(CC) $(ECDSA_POLICY_FLAGS) $(ECDSA_POLICY_HEADER) \ >"$$log_file" 2>&1; then \ cat "$$log_file"; \ @@ -763,6 +772,22 @@ ext-sign-test: -o $(TEST_BIN) $(SRC) $(TEST_SRC) $(LDFLAGS) $(LDLIBS) ./$(TEST_BIN) +# --- External signer only: local signing compiled out --- +# The nm check proves the library references no wolfCrypt signing primitive, +# which a --gc-sections link would otherwise hide. +NO_LOCAL_SIGN_FLAGS = -DWOLFCOSE_ENABLE_EXT_SIGN -DWOLFCOSE_NO_LOCAL_SIGN +NO_LOCAL_SIGN_TEST_BIN = tests/test_no_local_sign +no-local-sign-test: + $(MAKE) clean + $(MAKE) $(LIB_A) EXTRA_CFLAGS='$(EXTRA_CFLAGS) $(NO_LOCAL_SIGN_FLAGS)' + $(CC) $(CFLAGS) $(NO_LOCAL_SIGN_FLAGS) -o $(NO_LOCAL_SIGN_TEST_BIN) \ + tests/test_no_local_sign.c $(LIB_A) $(LDFLAGS) $(LDLIBS) + ./$(NO_LOCAL_SIGN_TEST_BIN) + @if nm -u $(OBJ) | grep -E 'wc_(ecc_sign_hash|ed25519_sign_msg|ed448_sign_msg|RsaPSS_Sign_ex|MlDsaKey_SignCtx|LmsKey_Sign)$$'; then \ + echo "FAIL: local signing primitive referenced"; exit 1; \ + fi + @echo "PASS: no local signing primitive referenced" + eat-psa-ext-sign-test: $(CC) $(CFLAGS) $(EAT_PSA_FULL_FLAGS) -DWOLFCOSE_ENABLE_EXT_SIGN \ -o $(TEST_BIN) $(SRC) $(TEST_SRC) $(LDFLAGS) $(LDLIBS) @@ -1238,6 +1263,8 @@ c99-check: -DWOLFCOSE_EAT_PSA_MAX_CLAIMS=10u \ -DWOLFCOSE_EAT_PSA_MAX_COMPONENT_CLAIMS=5u -fsyntax-only \ tests/test_eat_psa_limits.c + $(CC) $(C99_FLAGS) $(NO_LOCAL_SIGN_FLAGS) -fsyntax-only $(SRC) \ + tests/test_no_local_sign.c @echo "PASS: all sources conform to ISO C99 (-pedantic-errors)" # Strict C99 gate for the LMS-gated branches and profiles. Needs an @@ -1333,7 +1360,7 @@ c99-hpke-check: # --- Cleanup --- clean: rm -f $(OBJ) $(TEST_BIN) $(TOOL_BIN) $(DEMO_BIN) $(ENC_DEMO) $(HPKE_DEMO) $(MAC_DEMO) \ - $(EAT_PSA_TEST_BIN) $(EAT_PSA_LIMITS_TEST_BIN) \ + $(EAT_PSA_TEST_BIN) $(EAT_PSA_LIMITS_TEST_BIN) $(NO_LOCAL_SIGN_TEST_BIN) \ $(EXTSIGN_DEMO) $(SIGN1_DEMO) $(LEANV_DEMO) $(EAT_LEANV_DEMO) \ $(EAT_DEMO) $(MLDSA_DEMO) $(MLDSAV_DEMO) $(LMS_DEMO) $(LMSV_DEMO) $(CXX_SMOKE) \ $(COMP_SIGN) $(COMP_ENCRYPT) $(COMP_MAC) $(COMP_ERRORS) \ diff --git a/docs/Macros.md b/docs/Macros.md index ea19c06..8456188 100644 --- a/docs/Macros.md +++ b/docs/Macros.md @@ -415,6 +415,7 @@ encryption. | Define | Description | Default | |--------|-------------|---------| | `WOLFCOSE_ENABLE_EXT_SIGN` | Opt in `wc_CoseKey_SetExtSigner()`, which delegates signing to a caller-supplied callback | off | +| `WOLFCOSE_NO_LOCAL_SIGN` | Compile out every local wolfCrypt signing call (`wc_ecc_sign_hash`, `wc_ed25519_sign_msg`, RSA-PSS, ML-DSA, HSS-LMS) so only the callback signs. Requires `WOLFCOSE_ENABLE_EXT_SIGN` | off | Off in every build unless explicitly enabled, lean or not. Resolved internally as the read-only `WOLFCOSE_EXT_SIGN` gate. @@ -432,6 +433,14 @@ It returns the raw COSE signature; wolfCOSE checks the returned length against t A delegated key needs no local *private* key, but it must declare enough for wolfCOSE to know the expected signature length: ES* and ML-DSA need nothing beyond `alg`; EdDSA needs `kty`/`crv`; PS* needs `kty` plus a local `RsaKey` attached via `wc_CoseKey_SetRsa()` for its modulus size; HSS-LMS needs an initialized `LmsKey` attached via `wc_CoseKey_SetLms()`, because its length follows the key's parameter set. +With `WOLFCOSE_NO_LOCAL_SIGN`, a key without a signer is rejected with `WOLFCOSE_E_COSE_KEY_TYPE` even if it holds private material, and the signing primitives are not linked. Use it when every key lives behind the callback: + +```bash +make EXTRA_CFLAGS="-DWOLFCOSE_ENABLE_EXT_SIGN -DWOLFCOSE_NO_LOCAL_SIGN" +``` + +`WOLFCOSE_ENABLE_DETERMINISTIC_ECDSA` does not apply to such a build, so the wolfSSL backend need not support deterministic signing. `make no-local-sign-test` covers the option: ES256 Sign1, Sign and countersignatures and Ed25519 Sign1 through the callback, the refusal of local private keys, and a check that no signing primitive is linked. The main test suites sign with local keys, so they stop with an error under it. + Pass a NULL callback to detach. Attaching local key material with `wc_CoseKey_SetEcc()` and friends detaches implicitly, so always call `wc_CoseKey_SetExtSigner()` last. `wc_CoseKey_Decode()` is rejected on a key that has a signer attached, rather than silently importing private material and signing locally with it. For a key that has no local wolfCrypt object at all, set `kty` (and `crv` for EdDSA) on the `WOLFCOSE_KEY` directly before attaching the signer — there is no setter for declaring a key type without attaching one. @@ -638,6 +647,8 @@ wolfCOSE requires these wolfSSL features for full functionality: | `HAVE_HKDF` | ECDH-ES key derivation | | `HAVE_HPKE` + `HAVE_ECC` P-256 + SHA-256 + `HAVE_AESGCM` | Experimental COSE-HPKE P0 | +wolfSSL built with `NO_ASN` (`--enable-asn=no`) is supported for ECDSA signing and verification: `wc_ecc_sign_hash` and `wc_ecc_verify_hash` then use the fixed-width `r || s` form COSE needs, so wolfCOSE skips the DER conversion and does not link `wc_ecc_sig_to_rs` or `wc_ecc_rs_raw_to_sig`. The COSE signature encoding is the same as in an ASN build; signatures are byte-for-byte identical only with deterministic ECDSA, since a random nonce differs per run. + --- ## Test and Example Gates diff --git a/include/wolfcose/settings.h b/include/wolfcose/settings.h index 514dced..3931225 100644 --- a/include/wolfcose/settings.h +++ b/include/wolfcose/settings.h @@ -527,17 +527,10 @@ extern "C" { #error "wolfCOSE RSA-PSS key validation requires WOLFSSL_EXPORT_INT" #endif -/* The local ECDSA signing path converts a wolfCrypt DER signature to the - * fixed-width COSE representation. NO_ASN is supported for verification - * builds only. */ -#if defined(NO_ASN) && defined(WOLFCOSE_HAVE_ECDSA) && \ - (defined(WOLFCOSE_SIGN1_SIGN) || defined(WOLFCOSE_SIGN_SIGN)) - #error "wolfCOSE ECDSA signing requires ASN support" -#endif - -/* Optional RFC 6979 deterministic ECDSA signing. */ +/* Optional RFC 6979 deterministic ECDSA signing; an external-signer-only + * build never signs locally, so the backend need not support it. */ #if defined(WOLFCOSE_ENABLE_DETERMINISTIC_ECDSA) && \ - defined(WOLFCOSE_HAVE_ECDSA) && \ + defined(WOLFCOSE_HAVE_ECDSA) && !defined(WOLFCOSE_NO_LOCAL_SIGN) && \ (defined(WOLFCOSE_SIGN1_SIGN) || defined(WOLFCOSE_SIGN_SIGN) || \ defined(WOLFCOSE_COUNTERSIGN_SIGN)) && \ !defined(WOLFCOSE_HAVE_DETERMINISTIC_ECDSA) @@ -1031,6 +1024,11 @@ extern "C" { #error "WOLFCOSE_ENABLE_EXT_SIGN needs a signing op, which needs at least one local signature algorithm; the LEAN_VERIFY profiles are incompatible" #endif +/* WOLFCOSE_NO_LOCAL_SIGN: every signature comes from the external signer. */ +#if defined(WOLFCOSE_NO_LOCAL_SIGN) && !defined(WOLFCOSE_EXT_SIGN) + #error "WOLFCOSE_NO_LOCAL_SIGN requires WOLFCOSE_ENABLE_EXT_SIGN" +#endif + #ifdef __cplusplus } #endif diff --git a/src/wolfcose_countersign.c b/src/wolfcose_countersign.c index 83d7bbd..d3364d6 100644 --- a/src/wolfcose_countersign.c +++ b/src/wolfcose_countersign.c @@ -676,9 +676,13 @@ static int wolfCose_CounterSignTbs(WOLFCOSE_KEY* key, int32_t alg, *sigLen = 0u; } #if defined(WOLFCOSE_EXT_SIGN) +#if !defined(WOLFCOSE_NO_LOCAL_SIGN) if ((ret == WOLFCOSE_SUCCESS) && (key->signCb == NULL) && (rng == NULL)) { ret = WOLFCOSE_E_INVALID_ARG; } +#else + (void)rng; +#endif #else if ((ret == WOLFCOSE_SUCCESS) && (rng == NULL)) { ret = WOLFCOSE_E_INVALID_ARG; @@ -707,6 +711,7 @@ static int wolfCose_CounterSignTbs(WOLFCOSE_KEY* key, int32_t alg, } else #endif +#ifndef WOLFCOSE_NO_LOCAL_SIGN #ifdef WOLFCOSE_HAVE_ECDSA if ((ret == WOLFCOSE_SUCCESS) && (wolfCose_AlgIsEcdsa(alg) != 0)) { enum wc_HashType hashType = WC_HASH_TYPE_NONE; @@ -878,6 +883,7 @@ static int wolfCose_CounterSignTbs(WOLFCOSE_KEY* key, int32_t alg, } else #endif +#endif /* !WOLFCOSE_NO_LOCAL_SIGN */ if (ret == WOLFCOSE_SUCCESS) { ret = WOLFCOSE_E_COSE_BAD_ALG; } diff --git a/src/wolfcose_ecc.c b/src/wolfcose_ecc.c index 4c029bc..a6171ac 100644 --- a/src/wolfcose_ecc.c +++ b/src/wolfcose_ecc.c @@ -49,8 +49,8 @@ /* ----- Internal: ECC DER <-> raw r||s conversion ----- */ #ifdef WOLFCOSE_HAVE_ECDSA -#if defined(WOLFCOSE_SIGN1_SIGN) || defined(WOLFCOSE_SIGN_SIGN) || \ - defined(WOLFCOSE_COUNTERSIGN_SIGN) +#if (defined(WOLFCOSE_SIGN1_SIGN) || defined(WOLFCOSE_SIGN_SIGN) || \ + defined(WOLFCOSE_COUNTERSIGN_SIGN)) && !defined(WOLFCOSE_NO_LOCAL_SIGN) int wolfCose_EccSignRaw(const uint8_t* hash, size_t hashLen, uint8_t* sigBuf, size_t* sigLen, size_t coordSz, enum wc_HashType hashType, @@ -59,8 +59,10 @@ int wolfCose_EccSignRaw(const uint8_t* hash, size_t hashLen, int ret; uint8_t derSig[ECC_MAX_SIG_SIZE]; word32 derSigLen = (word32)sizeof(derSig); +#ifndef NO_ASN word32 rLen; word32 sLen; +#endif #ifdef WOLFCOSE_HAVE_DETERMINISTIC_ECDSA byte savedDeterministic = 0u; enum wc_HashType savedHashType = WC_HASH_TYPE_NONE; @@ -85,7 +87,7 @@ int wolfCose_EccSignRaw(const uint8_t* hash, size_t hashLen, (void)hashType; ret = WOLFCOSE_SUCCESS; #endif - /* Sign producing DER-encoded signature */ + /* DER-encoded signature, or fixed-width r||s under NO_ASN */ if (ret == WOLFCOSE_SUCCESS) { INJECT_FAILURE(WOLF_FAIL_ECC_SIGN, -1, ret = wc_ecc_sign_hash(hash, (word32)hashLen, derSig, @@ -94,6 +96,19 @@ int wolfCose_EccSignRaw(const uint8_t* hash, size_t hashLen, ret = WOLFCOSE_E_CRYPTO; } } +#ifdef NO_ASN + /* Older wolfCrypt releases write 2 * curve size without checking + * outlen, so sign into the local buffer and copy out an exact fit. */ + if (ret == WOLFCOSE_SUCCESS) { + if ((size_t)derSigLen != (coordSz * 2u)) { + ret = WOLFCOSE_E_CRYPTO; + } + else { + (void)XMEMCPY(sigBuf, derSig, coordSz * 2u); + *sigLen = coordSz * 2u; + } + } +#else if (ret == WOLFCOSE_SUCCESS) { /* Extract raw r and s from DER */ rLen = (word32)coordSz; @@ -127,6 +142,7 @@ int wolfCose_EccSignRaw(const uint8_t* hash, size_t hashLen, *sigLen = coordSz * 2u; } } +#endif /* NO_ASN */ (void)wolfCose_ForceZero(derSig, sizeof(derSig)); #ifdef WOLFCOSE_HAVE_DETERMINISTIC_ECDSA (void)wc_ecc_set_deterministic_ex(eccKey, savedDeterministic, @@ -135,7 +151,7 @@ int wolfCose_EccSignRaw(const uint8_t* hash, size_t hashLen, } return ret; } -#endif /* signature creation */ +#endif /* signature creation && !WOLFCOSE_NO_LOCAL_SIGN */ int wolfCose_EccVerifyRaw(const uint8_t* sigBuf, size_t sigLen, const uint8_t* hash, size_t hashLen, diff --git a/src/wolfcose_internal.h b/src/wolfcose_internal.h index e27548a..8115693 100644 --- a/src/wolfcose_internal.h +++ b/src/wolfcose_internal.h @@ -451,7 +451,8 @@ WOLFCOSE_LOCAL int wolfCose_HmacType(int32_t alg, #ifdef WOLFCOSE_HAVE_ECDSA /** * \brief Sign a hash with ECC, producing raw r||s output. - * Wraps wolfCrypt DER signature -> fixed-width r||s conversion. + * Converts the wolfCrypt DER signature to fixed-width r||s; a + * NO_ASN wolfCrypt already returns r||s. * \param hash Hash to sign. * \param hashLen Hash length. * \param sigBuf Output: raw r||s signature. @@ -462,14 +463,14 @@ WOLFCOSE_LOCAL int wolfCose_HmacType(int32_t alg, * \param eccKey Caller-owned ECC key with private key. * \return WOLFCOSE_SUCCESS or negative error code. */ -#if defined(WOLFCOSE_SIGN1_SIGN) || defined(WOLFCOSE_SIGN_SIGN) || \ - defined(WOLFCOSE_COUNTERSIGN_SIGN) +#if (defined(WOLFCOSE_SIGN1_SIGN) || defined(WOLFCOSE_SIGN_SIGN) || \ + defined(WOLFCOSE_COUNTERSIGN_SIGN)) && !defined(WOLFCOSE_NO_LOCAL_SIGN) WOLFCOSE_LOCAL int wolfCose_EccSignRaw(const uint8_t* hash, size_t hashLen, uint8_t* sigBuf, size_t* sigLen, size_t coordSz, enum wc_HashType hashType, WC_RNG* rng, ecc_key* eccKey); -#endif /* WOLFCOSE_SIGN1_SIGN || WOLFCOSE_SIGN_SIGN || COUNTERSIGN_SIGN */ +#endif /* signature creation && !WOLFCOSE_NO_LOCAL_SIGN */ /** diff --git a/src/wolfcose_key.c b/src/wolfcose_key.c index 5864ddd..f07b987 100644 --- a/src/wolfcose_key.c +++ b/src/wolfcose_key.c @@ -305,9 +305,11 @@ int wolfCose_KeyCanSign(const WOLFCOSE_KEY* key) { int can = 0; +#ifndef WOLFCOSE_NO_LOCAL_SIGN if (key->hasPrivate == 1u) { can = 1; } +#endif #if defined(WOLFCOSE_EXT_SIGN) if (key->signCb != NULL) { can = 1; diff --git a/src/wolfcose_sign.c b/src/wolfcose_sign.c index 0d7de49..a0999f1 100644 --- a/src/wolfcose_sign.c +++ b/src/wolfcose_sign.c @@ -209,6 +209,7 @@ int wc_CoseSign_Sign(const WOLFCOSE_SIGNATURE* signers, size_t signerCount, /* rng is required only when a signer signs locally; a delegated signer owns * its randomness. */ +#if !defined(WOLFCOSE_NO_LOCAL_SIGN) if ((ret == WOLFCOSE_SUCCESS) && (rng == NULL)) { #if defined(WOLFCOSE_EXT_SIGN) size_t s; @@ -225,6 +226,9 @@ int wc_CoseSign_Sign(const WOLFCOSE_SIGNATURE* signers, size_t signerCount, ret = WOLFCOSE_E_INVALID_ARG; #endif } +#else + (void)rng; +#endif /* !WOLFCOSE_NO_LOCAL_SIGN */ #ifdef WOLFCOSE_CHECK_WORD32_LEN if ((ret == WOLFCOSE_SUCCESS) && ((wolfCose_LenFitsWord32(payloadLen) == 0) || @@ -577,6 +581,7 @@ int wc_CoseSign_Sign(const WOLFCOSE_SIGNATURE* signers, size_t signerCount, } else #endif +#ifndef WOLFCOSE_NO_LOCAL_SIGN #ifdef WOLFCOSE_HAVE_ECDSA if ((ret == WOLFCOSE_SUCCESS) && (wolfCose_AlgIsEcdsa(signer->algId) != 0)) { @@ -762,6 +767,7 @@ int wc_CoseSign_Sign(const WOLFCOSE_SIGNATURE* signers, size_t signerCount, } else #endif /* WOLFCOSE_HAVE_LMS && !WOLFSSL_LMS_VERIFY_ONLY */ +#endif /* !WOLFCOSE_NO_LOCAL_SIGN */ if (ret == WOLFCOSE_SUCCESS) { ret = WOLFCOSE_E_COSE_BAD_ALG; } diff --git a/src/wolfcose_sign1.c b/src/wolfcose_sign1.c index 8688a09..86e95ae 100644 --- a/src/wolfcose_sign1.c +++ b/src/wolfcose_sign1.c @@ -601,9 +601,13 @@ int wc_CoseSign1_Sign_ex(WOLFCOSE_KEY* key, int32_t alg, } /* An external signer owns its own randomness, so rng is required only when * wolfCrypt does the signing. */ +#if !defined(WOLFCOSE_NO_LOCAL_SIGN) if ((ret == WOLFCOSE_SUCCESS) && (key->signCb == NULL) && (rng == NULL)) { ret = WOLFCOSE_E_INVALID_ARG; } +#else + (void)rng; +#endif #else if ((key == NULL) || (sigPayload == NULL) || (scratch == NULL) || (out == NULL) || (outLen == NULL) || (rng == NULL)) { @@ -706,6 +710,7 @@ int wc_CoseSign1_Sign_ex(WOLFCOSE_KEY* key, int32_t alg, } else #endif +#ifndef WOLFCOSE_NO_LOCAL_SIGN #if defined(WOLFCOSE_HAVE_EDDSA) || defined(WOLFCOSE_HAVE_ED448) if ((ret == WOLFCOSE_SUCCESS) && (wolfCose_AlgIsEddsa(alg) != 0)) { word32 edSigLen = (word32)sizeof(sigBuf); @@ -985,6 +990,7 @@ int wc_CoseSign1_Sign_ex(WOLFCOSE_KEY* key, int32_t alg, } else #endif /* WOLFCOSE_HAVE_LMS && !WOLFSSL_LMS_VERIFY_ONLY */ +#endif /* !WOLFCOSE_NO_LOCAL_SIGN */ if (ret == WOLFCOSE_SUCCESS) { ret = WOLFCOSE_E_COSE_BAD_ALG; } diff --git a/tests/test_cose.c b/tests/test_cose.c index d4ffa7b..89daac6 100644 --- a/tests/test_cose.c +++ b/tests/test_cose.c @@ -40,6 +40,10 @@ #include #include "../src/wolfcose_internal.h" /* For testing internal helpers */ + +#ifdef WOLFCOSE_NO_LOCAL_SIGN + #error "this suite signs with local keys; test WOLFCOSE_NO_LOCAL_SIGN with make no-local-sign-test" +#endif #include "test_suite.h" #include #if defined(WOLFCOSE_HAVE_ES256) || defined(WOLFCOSE_HAVE_HPKE_0) @@ -1140,6 +1144,116 @@ static void test_cose_sign1_ecc(const char* label, int32_t alg, int32_t crv, } #endif /* WOLFCOSE_HAVE_ES256 */ +#if defined(WOLFCOSE_HAVE_ES256) && defined(WOLFCOSE_SIGN1_VERIFY) +/* RFC 6979 A.2.5 P-256 key. The expected message was produced by an + * independent RFC 6979 implementation, so ASN and NO_ASN wolfCrypt builds must + * both verify it and, with deterministic signing, reproduce it byte for byte. */ +static const uint8_t kKatEs256Priv[32] = { + 0xC9u, 0xAFu, 0xA9u, 0xD8u, 0x45u, 0xBAu, 0x75u, 0x16u, + 0x6Bu, 0x5Cu, 0x21u, 0x57u, 0x67u, 0xB1u, 0xD6u, 0x93u, + 0x4Eu, 0x50u, 0xC3u, 0xDBu, 0x36u, 0xE8u, 0x9Bu, 0x12u, + 0x7Bu, 0x8Au, 0x62u, 0x2Bu, 0x12u, 0x0Fu, 0x67u, 0x21u +}; +static const uint8_t kKatEs256Pub[65] = { + 0x04u, + 0x60u, 0xFEu, 0xD4u, 0xBAu, 0x25u, 0x5Au, 0x9Du, 0x31u, + 0xC9u, 0x61u, 0xEBu, 0x74u, 0xC6u, 0x35u, 0x6Du, 0x68u, + 0xC0u, 0x49u, 0xB8u, 0x92u, 0x3Bu, 0x61u, 0xFAu, 0x6Cu, + 0xE6u, 0x69u, 0x62u, 0x2Eu, 0x60u, 0xF2u, 0x9Fu, 0xB6u, + 0x79u, 0x03u, 0xFEu, 0x10u, 0x08u, 0xB8u, 0xBCu, 0x99u, + 0xA4u, 0x1Au, 0xE9u, 0xE9u, 0x56u, 0x28u, 0xBCu, 0x64u, + 0xF2u, 0xF1u, 0xB2u, 0x0Cu, 0x2Du, 0x7Eu, 0x9Fu, 0x51u, + 0x77u, 0xA3u, 0xC2u, 0x94u, 0xD4u, 0x46u, 0x22u, 0x99u +}; +static const uint8_t kKatEs256Msg[] = { + 0xD2u, 0x84u, 0x43u, 0xA1u, 0x01u, 0x28u, 0xA0u, 0x54u, + 0x54u, 0x68u, 0x69u, 0x73u, 0x20u, 0x69u, 0x73u, 0x20u, + 0x74u, 0x68u, 0x65u, 0x20u, 0x63u, 0x6Fu, 0x6Eu, 0x74u, + 0x65u, 0x6Eu, 0x74u, 0x2Eu, 0x58u, 0x40u, 0x06u, 0x24u, + 0xFEu, 0x60u, 0x11u, 0x44u, 0x8Au, 0xE3u, 0x91u, 0xA0u, + 0x35u, 0x5Eu, 0x76u, 0x56u, 0xF0u, 0xB7u, 0xE6u, 0x02u, + 0xA5u, 0x9Bu, 0x0Cu, 0xAAu, 0x3Au, 0xFAu, 0x3Au, 0x42u, + 0x2Bu, 0xE9u, 0x54u, 0x25u, 0x90u, 0x4Eu, 0x25u, 0xE9u, + 0x10u, 0xE6u, 0x55u, 0x2Au, 0xCFu, 0x59u, 0xC6u, 0xE2u, + 0x59u, 0x8Du, 0x6Au, 0xF3u, 0x35u, 0x88u, 0xC3u, 0xECu, + 0xD8u, 0x2Bu, 0x80u, 0x8Au, 0x56u, 0x34u, 0xC3u, 0x47u, + 0x06u, 0xD1u, 0x4Fu, 0x3Du, 0x02u, 0xC2u +}; + +#if defined(WOLFCOSE_SIGN1_SIGN) && \ + defined(WOLFCOSE_HAVE_DETERMINISTIC_ECDSA) && \ + defined(WOLFSSL_ECDSA_DETERMINISTIC_K) && \ + !defined(WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT) + #define WOLFCOSE_TEST_ES256_KAT_SIGN +#endif + +static void test_cose_sign1_es256_kat(void) +{ + static const uint8_t payload[] = "This is the content."; + WOLFCOSE_KEY coseKey; + ecc_key eccKey; + WOLFCOSE_HDR hdr; + uint8_t scratch[WOLFCOSE_MAX_SCRATCH_SZ]; + const uint8_t* decPayload = NULL; + size_t decPayloadLen = 0; + int eccInited = 0; + int ret; +#ifdef WOLFCOSE_TEST_ES256_KAT_SIGN + WC_RNG rng; + uint8_t out[sizeof(kKatEs256Msg)]; + size_t outLen = 0; +#endif + + TEST_LOG(" [Sign1 ESP256 known answer]\n"); + + ret = wc_ecc_init(&eccKey); + TEST_ASSERT(ret == 0, "kat ecc init"); + if (ret == 0) { + eccInited = 1; + /* import_unsigned, as a NO_ASN wolfCrypt cannot import a private + * key together with its x963 public point. */ + ret = wc_ecc_import_unsigned(&eccKey, &kKatEs256Pub[1], + &kKatEs256Pub[33], kKatEs256Priv, ECC_SECP256R1); + TEST_ASSERT(ret == 0, "kat ecc import"); + } + if (ret == 0) { + (void)wc_CoseKey_Init(&coseKey); + ret = wc_CoseKey_SetEcc(&coseKey, WOLFCOSE_CRV_P256, &eccKey); + TEST_ASSERT(ret == 0, "kat cose key set"); + } + if (ret == 0) { + ret = wc_CoseSign1_Verify(&coseKey, kKatEs256Msg, + sizeof(kKatEs256Msg), NULL, 0, NULL, 0, + scratch, sizeof(scratch), &hdr, &decPayload, &decPayloadLen); + TEST_ASSERT(ret == 0, "kat verify"); + TEST_ASSERT((ret != 0) || + ((decPayloadLen == (sizeof(payload) - 1u)) && + (memcmp(decPayload, payload, decPayloadLen) == 0)), + "kat payload"); + } +#ifdef WOLFCOSE_TEST_ES256_KAT_SIGN + if (ret == 0) { + ret = wc_InitRng(&rng); + TEST_ASSERT(ret == 0, "kat rng init"); + if (ret == 0) { + ret = wc_CoseSign1_Sign(&coseKey, WOLFCOSE_ALG_ESP256, NULL, 0, + payload, sizeof(payload) - 1u, NULL, 0, NULL, 0, + scratch, sizeof(scratch), out, sizeof(out), &outLen, &rng); + TEST_ASSERT(ret == 0, "kat deterministic sign"); + TEST_ASSERT((ret != 0) || + ((outLen == sizeof(kKatEs256Msg)) && + (memcmp(out, kKatEs256Msg, outLen) == 0)), + "kat deterministic output matches vector"); + (void)wc_FreeRng(&rng); + } + } +#endif + if (eccInited != 0) { + (void)wc_ecc_free(&eccKey); + } +} +#endif /* WOLFCOSE_HAVE_ES256 && WOLFCOSE_SIGN1_VERIFY */ + #if defined(WOLFCOSE_EXT_SIGN) && defined(WOLFCOSE_HAVE_ES256) typedef struct { WC_RNG* rng; @@ -24601,7 +24715,8 @@ static void test_force_failure_crypto(void) coseMsg, sizeof(coseMsg), &coseMsgLen, &rng); TEST_ASSERT(ret == WOLFCOSE_E_CRYPTO, "ECC sign forced failure"); - /* Test ECC sig_to_rs failure */ +#ifndef NO_ASN + /* Test ECC sig_to_rs failure (DER path only) */ coseMsgLen = sizeof(coseMsg); wolfForceFailure_Set(WOLF_FAIL_ECC_SIG_TO_RS); ret = wc_CoseSign1_Sign(&key, WOLFCOSE_ALG_ESP256, @@ -24612,6 +24727,7 @@ static void test_force_failure_crypto(void) scratch, sizeof(scratch), coseMsg, sizeof(coseMsg), &coseMsgLen, &rng); TEST_ASSERT(ret == WOLFCOSE_E_CRYPTO, "ECC sig_to_rs forced failure"); +#endif /* Create a valid signature for verify tests */ coseMsgLen = sizeof(coseMsg); @@ -24627,7 +24743,8 @@ static void test_force_failure_crypto(void) size_t decodedPayloadLen; WOLFCOSE_HDR hdr; - /* Test ECC rs_to_sig failure */ +#ifndef NO_ASN + /* Test ECC rs_to_sig failure (DER path only) */ wolfForceFailure_Set(WOLF_FAIL_ECC_RS_TO_SIG); ret = wc_CoseSign1_Verify(&key, coseMsg, coseMsgLen, NULL, 0, /* detached */ @@ -24635,6 +24752,7 @@ static void test_force_failure_crypto(void) scratch, sizeof(scratch), &hdr, &decodedPayload, &decodedPayloadLen); TEST_ASSERT(ret == WOLFCOSE_E_CRYPTO, "ECC rs_to_sig forced failure"); +#endif /* Test ECC verify failure */ wolfForceFailure_Set(WOLF_FAIL_ECC_VERIFY); @@ -30199,6 +30317,9 @@ int test_cose(void) /* Sign1 basic tests */ #ifdef WOLFCOSE_HAVE_ES256 test_cose_sign1_ecc("ESP256", WOLFCOSE_ALG_ESP256, WOLFCOSE_CRV_P256, 32); +#ifdef WOLFCOSE_SIGN1_VERIFY + test_cose_sign1_es256_kat(); +#endif test_cose_sign1_with_aad(); test_cose_sign1_detached(); #if defined(WOLFCOSE_EXT_SIGN) diff --git a/tests/test_cose_examples.c b/tests/test_cose_examples.c index 92be64e..5faa898 100644 --- a/tests/test_cose_examples.c +++ b/tests/test_cose_examples.c @@ -673,9 +673,9 @@ static void test_example_encrypt_ecdh(void) TEST_ASSERT(ret == 0, "Examples ECDH-ES ECC init"); if (ret == 0) { ecc_key_inited = 1; - ret = wc_ecc_import_private_key_ex(example_ecdh_private, - (word32)sizeof(example_ecdh_private), example_ecdh_public, - (word32)sizeof(example_ecdh_public), &ecc_key, ECC_SECP256R1); + /* NO_ASN wolfCrypt rejects a private import with an x963 point. */ + ret = wc_ecc_import_unsigned(&ecc_key, &example_ecdh_public[1], + &example_ecdh_public[33], example_ecdh_private, ECC_SECP256R1); TEST_ASSERT(ret == 0, "Examples ECDH-ES private key import"); } if (ret == 0) { diff --git a/tests/test_eat_psa.c b/tests/test_eat_psa.c index 2c514ec..a10785d 100644 --- a/tests/test_eat_psa.c +++ b/tests/test_eat_psa.c @@ -8,6 +8,10 @@ #include #include + +#ifdef WOLFCOSE_NO_LOCAL_SIGN + #error "this suite signs with local keys; test WOLFCOSE_NO_LOCAL_SIGN with make no-local-sign-test" +#endif #include #include #ifdef HAVE_ECC diff --git a/tests/test_no_local_sign.c b/tests/test_no_local_sign.c new file mode 100644 index 0000000..22940cf --- /dev/null +++ b/tests/test_no_local_sign.c @@ -0,0 +1,350 @@ +/* test_no_local_sign.c + * + * Copyright (C) 2026 wolfSSL Inc. + * + * This file is part of wolfCOSE. + * + * wolfCOSE is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * wolfCOSE is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + */ + +/* WOLFCOSE_NO_LOCAL_SIGN: a key with local private material is refused and + * the external signer still signs messages that verify. */ + +#include +#include + +#include +#include +#include +#ifdef WOLFCOSE_HAVE_EDDSA + #include +#endif + +#if !defined(WOLFCOSE_NO_LOCAL_SIGN) || !defined(WOLFCOSE_HAVE_ES256) || \ + !defined(WOLFCOSE_SIGN1_SIGN) || !defined(WOLFCOSE_SIGN1_VERIFY) + #error "test_no_local_sign needs WOLFCOSE_NO_LOCAL_SIGN with ES256 Sign1" +#endif + +static int g_failures = 0; + +#define NLS_ASSERT(cond, name) do { \ + if (!(cond)) { \ + (void)printf(" FAIL: %s (line %d)\n", (name), __LINE__); \ + g_failures++; \ + } \ + else { \ + (void)printf(" PASS: %s\n", (name)); \ + } \ +} while (0) + +typedef struct { + WC_RNG* rng; + ecc_key* key; +} nls_signer; + +/* Stands in for a secure element: digest in, fixed-width r||s out. */ +static int nls_sign_cb(void* cbCtx, int32_t alg, const uint8_t* tbs, + size_t tbsLen, uint8_t* sig, size_t sigSz, + size_t* sigLen) +{ + nls_signer* se = (nls_signer*)cbCtx; + uint8_t der[ECC_MAX_SIG_SIZE]; + word32 derLen = (word32)sizeof(der); + int ret = 0; +#ifndef NO_ASN + word32 rLen = 32u; + word32 sLen = 32u; +#endif + + if ((alg != WOLFCOSE_ALG_ESP256) || (sigSz < 64u)) { + ret = -1; + } + if (ret == 0) { + ret = wc_ecc_sign_hash(tbs, (word32)tbsLen, der, &derLen, se->rng, + se->key); + } +#ifdef NO_ASN + if ((ret == 0) && (derLen != 64u)) { + ret = -1; + } + if (ret == 0) { + (void)memcpy(sig, der, 64u); + } +#else + if (ret == 0) { + (void)memset(sig, 0, 64u); + ret = wc_ecc_sig_to_rs(der, derLen, sig, &rLen, &sig[32], &sLen); + } + if ((ret == 0) && ((rLen > 32u) || (sLen > 32u))) { + ret = -1; + } + if (ret == 0) { + (void)memmove(&sig[32u - rLen], sig, rLen); + (void)memset(sig, 0, 32u - rLen); + (void)memmove(&sig[32u + (32u - sLen)], &sig[32], sLen); + (void)memset(&sig[32], 0, 32u - sLen); + } +#endif + if (ret == 0) { + *sigLen = 64u; + } + return (ret == 0) ? 0 : -1; +} + +#ifdef WOLFCOSE_HAVE_EDDSA +static int nls_ed25519_cb(void* cbCtx, int32_t alg, const uint8_t* tbs, + size_t tbsLen, uint8_t* sig, size_t sigSz, + size_t* sigLen) +{ + word32 len = (word32)sigSz; + int ret = 0; + + if ((alg != WOLFCOSE_ALG_ED25519) || (sigSz < ED25519_SIG_SIZE)) { + ret = -1; + } + if (ret == 0) { + ret = wc_ed25519_sign_msg(tbs, (word32)tbsLen, sig, &len, + (ed25519_key*)cbCtx); + } + if (ret == 0) { + *sigLen = (size_t)len; + } + return (ret == 0) ? 0 : -1; +} + +/* Drives the EdDSA branch at runtime: refusal of a local key, then a + * delegated signature that verifies. */ +static void nls_test_ed25519(WC_RNG* rng, uint8_t* scratch, size_t scratchSz, + uint8_t* out, size_t outSz) +{ + static const uint8_t payload[] = "external ed25519 signer"; + ed25519_key edKey; + WOLFCOSE_KEY localKey; + WOLFCOSE_KEY extKey; + WOLFCOSE_HDR hdr; + size_t outLen = 0; + const uint8_t* decPayload = NULL; + size_t decPayloadLen = 0; + int edInited = 0; + int ret; + + ret = wc_ed25519_init(&edKey); + NLS_ASSERT(ret == 0, "ed25519 init"); + if (ret == 0) { + edInited = 1; + ret = wc_ed25519_make_key(rng, ED25519_KEY_SIZE, &edKey); + NLS_ASSERT(ret == 0, "ed25519 keygen"); + } + if (ret == 0) { + (void)wc_CoseKey_Init(&localKey); + ret = wc_CoseKey_SetEd25519(&localKey, &edKey); + NLS_ASSERT(ret == 0, "ed25519 local key set"); + } + if (ret == 0) { + ret = wc_CoseSign1_Sign(&localKey, WOLFCOSE_ALG_ED25519, NULL, 0, + payload, sizeof(payload) - 1u, NULL, 0, NULL, 0, + scratch, scratchSz, out, outSz, &outLen, NULL); + NLS_ASSERT(ret == WOLFCOSE_E_COSE_KEY_TYPE, + "Sign1 refuses a local Ed25519 private key"); + ret = 0; + } + if (ret == 0) { + (void)wc_CoseKey_Init(&extKey); + extKey.kty = WOLFCOSE_KTY_OKP; + extKey.crv = WOLFCOSE_CRV_ED25519; + ret = wc_CoseKey_SetExtSigner(&extKey, nls_ed25519_cb, &edKey); + NLS_ASSERT(ret == 0, "ed25519 external signer attached"); + } + if (ret == 0) { + ret = wc_CoseSign1_Sign(&extKey, WOLFCOSE_ALG_ED25519, NULL, 0, + payload, sizeof(payload) - 1u, NULL, 0, NULL, 0, + scratch, scratchSz, out, outSz, &outLen, NULL); + NLS_ASSERT(ret == 0, "Sign1 through the Ed25519 external signer"); + } + if (ret == 0) { + ret = wc_CoseSign1_Verify(&localKey, out, outLen, NULL, 0, NULL, 0, + scratch, scratchSz, &hdr, &decPayload, &decPayloadLen); + NLS_ASSERT(ret == 0, "Ed25519 Sign1 verifies"); + NLS_ASSERT((ret != 0) || + ((decPayloadLen == (sizeof(payload) - 1u)) && + (memcmp(decPayload, payload, decPayloadLen) == 0)), + "Ed25519 Sign1 payload round-trips"); + } + if (edInited != 0) { + (void)wc_ed25519_free(&edKey); + } +} +#endif + +int main(void) +{ + static const uint8_t payload[] = "external signer only"; + WC_RNG rng; + ecc_key eccKey; + nls_signer se; + WOLFCOSE_KEY localKey; + WOLFCOSE_KEY extKey; + WOLFCOSE_HDR hdr; + uint8_t scratch[WOLFCOSE_MAX_SCRATCH_SZ]; + uint8_t out[256]; + size_t outLen = 0; + const uint8_t* decPayload = NULL; + size_t decPayloadLen = 0; + int rngInited = 0; + int eccInited = 0; + int ret; +#if defined(WOLFCOSE_SIGN_SIGN) + WOLFCOSE_SIGNATURE signer; +#endif +#if defined(WOLFCOSE_COUNTERSIGN_SIGN) && defined(WOLFCOSE_COUNTERSIGN_VERIFY) + WOLFCOSE_COUNTERSIGNATURE counterSigner; + uint8_t csOut[512]; + size_t csOutLen = 0; +#endif + + (void)printf("=== wolfCOSE WOLFCOSE_NO_LOCAL_SIGN Tests ===\n"); + + ret = wc_InitRng(&rng); + NLS_ASSERT(ret == 0, "rng init"); + if (ret == 0) { + rngInited = 1; + ret = wc_ecc_init(&eccKey); + NLS_ASSERT(ret == 0, "ecc init"); + } + if (ret == 0) { + eccInited = 1; + ret = wc_ecc_make_key(&rng, 32, &eccKey); + NLS_ASSERT(ret == 0, "ecc keygen"); + } + if (ret == 0) { + (void)wc_CoseKey_Init(&localKey); + ret = wc_CoseKey_SetEcc(&localKey, WOLFCOSE_CRV_P256, &eccKey); + NLS_ASSERT(ret == 0, "local key set"); + } + if (ret == 0) { + ret = wc_CoseSign1_Sign(&localKey, WOLFCOSE_ALG_ESP256, NULL, 0, + payload, sizeof(payload) - 1u, NULL, 0, NULL, 0, + scratch, sizeof(scratch), out, sizeof(out), &outLen, &rng); + NLS_ASSERT(ret == WOLFCOSE_E_COSE_KEY_TYPE, + "Sign1 refuses a local private key"); + ret = wc_CoseSign1_Sign(&localKey, WOLFCOSE_ALG_ESP256, NULL, 0, + payload, sizeof(payload) - 1u, NULL, 0, NULL, 0, + scratch, sizeof(scratch), out, sizeof(out), &outLen, NULL); + NLS_ASSERT(ret == WOLFCOSE_E_COSE_KEY_TYPE, + "Sign1 refuses a local private key without an RNG"); + ret = 0; + } +#if defined(WOLFCOSE_SIGN_SIGN) + if (ret == 0) { + (void)memset(&signer, 0, sizeof(signer)); + signer.algId = WOLFCOSE_ALG_ESP256; + signer.key = &localKey; + ret = wc_CoseSign_Sign(&signer, 1u, payload, sizeof(payload) - 1u, + NULL, 0, NULL, 0, scratch, sizeof(scratch), + out, sizeof(out), &outLen, &rng); + NLS_ASSERT(ret == WOLFCOSE_E_COSE_KEY_TYPE, + "Sign refuses a local private key"); + ret = wc_CoseSign_Sign(&signer, 1u, payload, sizeof(payload) - 1u, + NULL, 0, NULL, 0, scratch, sizeof(scratch), + out, sizeof(out), &outLen, NULL); + NLS_ASSERT(ret == WOLFCOSE_E_COSE_KEY_TYPE, + "Sign refuses a local private key without an RNG"); + ret = 0; + } +#endif + if (ret == 0) { + se.rng = &rng; + se.key = &eccKey; + (void)wc_CoseKey_Init(&extKey); + ret = wc_CoseKey_SetEcc(&extKey, WOLFCOSE_CRV_P256, &eccKey); + if (ret == 0) { + ret = wc_CoseKey_SetExtSigner(&extKey, nls_sign_cb, &se); + } + NLS_ASSERT(ret == 0, "external signer attached"); + } + if (ret == 0) { + ret = wc_CoseSign1_Sign(&extKey, WOLFCOSE_ALG_ESP256, NULL, 0, + payload, sizeof(payload) - 1u, NULL, 0, NULL, 0, + scratch, sizeof(scratch), out, sizeof(out), &outLen, NULL); + NLS_ASSERT(ret == 0, "Sign1 through the external signer"); + } + if (ret == 0) { + ret = wc_CoseSign1_Verify(&localKey, out, outLen, NULL, 0, NULL, 0, + scratch, sizeof(scratch), &hdr, &decPayload, &decPayloadLen); + NLS_ASSERT(ret == 0, "Sign1 verifies"); + NLS_ASSERT((ret != 0) || + ((decPayloadLen == (sizeof(payload) - 1u)) && + (memcmp(decPayload, payload, decPayloadLen) == 0)), + "Sign1 payload round-trips"); + } +#if defined(WOLFCOSE_COUNTERSIGN_SIGN) && defined(WOLFCOSE_COUNTERSIGN_VERIFY) + if (ret == 0) { + (void)memset(&counterSigner, 0, sizeof(counterSigner)); + counterSigner.algId = WOLFCOSE_ALG_ESP256; + counterSigner.key = &localKey; + ret = wc_Cose_AddCounterSignature(&counterSigner, out, outLen, + NULL, 0u, NULL, 0u, scratch, sizeof(scratch), + csOut, sizeof(csOut), &csOutLen, &rng); + NLS_ASSERT(ret == WOLFCOSE_E_COSE_KEY_TYPE, + "countersign refuses a local private key"); + ret = wc_Cose_AddCounterSignature(&counterSigner, out, outLen, + NULL, 0u, NULL, 0u, scratch, sizeof(scratch), + csOut, sizeof(csOut), &csOutLen, NULL); + NLS_ASSERT(ret == WOLFCOSE_E_COSE_KEY_TYPE, + "countersign refuses a local private key without an RNG"); + ret = 0; + } + if (ret == 0) { + counterSigner.key = &extKey; + ret = wc_Cose_AddCounterSignature(&counterSigner, out, outLen, + NULL, 0u, NULL, 0u, scratch, sizeof(scratch), + csOut, sizeof(csOut), &csOutLen, NULL); + NLS_ASSERT(ret == 0, "countersignature through the external signer"); + } + if (ret == 0) { + ret = wc_Cose_VerifyCounterSignature(&localKey, 0u, csOut, csOutLen, + NULL, 0u, NULL, 0u, scratch, sizeof(scratch), &hdr); + NLS_ASSERT(ret == 0, "countersignature verifies"); + } +#endif +#if defined(WOLFCOSE_SIGN_SIGN) && defined(WOLFCOSE_SIGN_VERIFY) + if (ret == 0) { + signer.key = &extKey; + ret = wc_CoseSign_Sign(&signer, 1u, payload, sizeof(payload) - 1u, + NULL, 0, NULL, 0, scratch, sizeof(scratch), + out, sizeof(out), &outLen, NULL); + NLS_ASSERT(ret == 0, "Sign through the external signer"); + } + if (ret == 0) { + ret = wc_CoseSign_Verify(&localKey, 0u, out, outLen, NULL, 0, NULL, 0, + scratch, sizeof(scratch), &hdr, &decPayload, &decPayloadLen); + NLS_ASSERT(ret == 0, "Sign verifies"); + } +#endif +#ifdef WOLFCOSE_HAVE_EDDSA + if (rngInited != 0) { + nls_test_ed25519(&rng, scratch, sizeof(scratch), out, sizeof(out)); + } +#endif + + if (eccInited != 0) { + (void)wc_ecc_free(&eccKey); + } + if (rngInited != 0) { + (void)wc_FreeRng(&rng); + } + (void)printf("=== Results: %s ===\n", + (g_failures == 0) ? "ALL PASSED" : "FAILURES"); + return (g_failures == 0) ? 0 : 1; +}