From ddef797dc692bca6b63260cb45a0522b0fc36ce5 Mon Sep 17 00:00:00 2001 From: Aidan Garske Date: Tue, 2 Jun 2026 16:41:42 -0700 Subject: [PATCH 1/3] Add WOLFCOSE_LEAN_VERIFY profile for minimal verify-only builds --- .github/workflows/lean-build.yml | 127 +++++++++++++++++++++++++++++++ Makefile | 12 ++- examples/sign1_verify_lean.c | 111 +++++++++++++++++++++++++++ include/wolfcose/wolfcose.h | 59 +++++++++++++- src/wolfcose.c | 5 ++ 5 files changed, 312 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/lean-build.yml create mode 100644 examples/sign1_verify_lean.c diff --git a/.github/workflows/lean-build.yml b/.github/workflows/lean-build.yml new file mode 100644 index 0000000..e7d318f --- /dev/null +++ b/.github/workflows/lean-build.yml @@ -0,0 +1,127 @@ +name: Lean Build + +on: + push: + branches: [ 'main', 'release/**' ] + pull_request: + branches: [ '*' ] + workflow_dispatch: + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + lean-verify: + name: Lean verify-only (WOLFCOSE_LEAN_VERIFY) + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + + - name: Install dependencies + run: | + sudo apt-get update + sudo apt-get install -y autoconf automake libtool + + - 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 (ECC verify backend) + id: cache-wolfssl + uses: actions/cache@v4 + with: + path: ~/wolfssl-lean + key: wolfssl-lean-ecc-v1-${{ steps.wolfssl-rev.outputs.sha }} + + # Minimal backend for ES256 verification: ECC + SHA-256 only. + # No keygen and no RNG are needed to verify a COSE_Sign1. + - name: Build minimal wolfSSL + if: steps.cache-wolfssl.outputs.cache-hit != 'true' + run: | + cd ~ + git clone --depth 1 https://github.com/wolfSSL/wolfssl.git wolfssl-lean-src + cd wolfssl-lean-src + ./autogen.sh + ./configure --enable-cryptonly --enable-ecc \ + --prefix=$HOME/wolfssl-lean + make -j$(nproc) + make install + + - name: Build and run the lean verify-only example + run: | + export WOLFSSL_DIR=$HOME/wolfssl-lean + export LD_LIBRARY_PATH=$WOLFSSL_DIR/lib + make lean-verify \ + CFLAGS="-std=c11 -DHAVE_ANONYMOUS_INLINE_AGGREGATES=1 -Os -Wall -Wextra -Wpedantic -Wshadow -Wconversion -ffunction-sections -fdata-sections -I./include -isystem $WOLFSSL_DIR/include" \ + LDFLAGS="-L$WOLFSSL_DIR/lib -lwolfssl -Wl,--gc-sections" + + - name: Assert the signing API is absent from a verify-only build + run: | + BIN=examples/sign1_verify_lean + if nm "$BIN" | grep -E " [tT] wc_CoseSign1_Sign\b"; then + echo "FAIL: signing API present in a verify-only build"; exit 1 + fi + if ! nm "$BIN" | grep -qE " [tT] wc_CoseSign1_Verify\b"; then + echo "FAIL: verify API missing from a verify build"; exit 1 + fi + echo "OK: verify present, signing absent" + + - name: Report lean example size + run: size examples/sign1_verify_lean || true + + lean-compile-matrix: + name: Lean configs compile clean (strict) + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + + - name: Install dependencies + run: | + sudo apt-get update + sudo apt-get install -y autoconf automake libtool + + - 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 + id: cache-wolfssl + uses: actions/cache@v4 + with: + path: ~/wolfssl-install + key: wolfssl-ubuntu-latest-v3-${{ steps.wolfssl-rev.outputs.sha }} + + - name: Build wolfSSL (full features) + if: steps.cache-wolfssl.outputs.cache-hit != 'true' + run: | + cd ~ + git clone --depth 1 https://github.com/wolfSSL/wolfssl.git + cd wolfssl + ./autogen.sh + ./configure --enable-ecc --enable-ed25519 --enable-ed448 \ + --enable-curve25519 --enable-aesgcm --enable-aesccm \ + --enable-sha384 --enable-sha512 --enable-keygen \ + --enable-rsapss --enable-chacha --enable-poly1305 \ + --enable-dilithium \ + --prefix=$HOME/wolfssl-install + make -j$(nproc) + make install + + # Each lean/minimal config must compile with zero warnings under the strict + # flag set that backs the MISRA C:2023 job. + - name: Compile wolfcose.c in lean configurations (no warnings) + run: | + export WOLFSSL_DIR=$HOME/wolfssl-install + SF="-std=c11 -Os -Wall -Wextra -Wpedantic -Wshadow -Wconversion -Werror -DHAVE_ANONYMOUS_INLINE_AGGREGATES=1 -I./include -isystem $WOLFSSL_DIR/include" + echo "== default (full) ==" + gcc $SF -c src/wolfcose.c -o /tmp/full.o + echo "== WOLFCOSE_LEAN_VERIFY ==" + gcc $SF -DWOLFCOSE_LEAN_VERIFY -c src/wolfcose.c -o /tmp/lean.o + echo "== sign-only ==" + gcc $SF -DWOLFCOSE_NO_SIGN1_VERIFY -DWOLFCOSE_NO_ENCRYPT0 -DWOLFCOSE_NO_MAC0 \ + -DWOLFCOSE_NO_SIGN -DWOLFCOSE_NO_ENCRYPT -DWOLFCOSE_NO_MAC \ + -DWOLFCOSE_NO_RECIPIENTS -c src/wolfcose.c -o /tmp/sign.o + echo "All lean configurations compiled clean." diff --git a/Makefile b/Makefile index b3ee04f..daa25d2 100644 --- a/Makefile +++ b/Makefile @@ -45,6 +45,7 @@ DEMO_BIN = examples/lifecycle_demo ENC_DEMO = examples/encrypt0_demo MAC_DEMO = examples/mac0_demo SIGN1_DEMO = examples/sign1_demo +LEANV_DEMO = examples/sign1_verify_lean # Comprehensive tests (CI) COMP_SIGN = examples/comprehensive/sign_all @@ -59,7 +60,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 coverage tool tool-test cmdline-test demo demos comprehensive scenarios c99-check clean +.PHONY: all shared test coverage tool tool-test cmdline-test demo demos lean-verify comprehensive scenarios c99-check clean # --- Core library --- all: $(LIB_A) @@ -134,6 +135,15 @@ demos: $(LIB_A) ./$(MAC_DEMO) ./$(SIGN1_DEMO) +# --- Lean verify-only example (WOLFCOSE_LEAN_VERIFY) --- +# Compiles the wolfCOSE sources directly with the lean macro instead of the full +# prebuilt library, so the example exercises the minimal verify-only profile. +lean-verify: + $(CC) $(CFLAGS) -DWOLFCOSE_LEAN_VERIFY -o $(LEANV_DEMO) \ + $(LEANV_DEMO).c src/wolfcose.c src/wolfcose_cbor.c $(LDFLAGS) + @echo "=== Running lean verify-only example ===" + ./$(LEANV_DEMO) + # --- Comprehensive algorithm tests (CI) --- comprehensive: $(LIB_A) @mkdir -p examples/comprehensive diff --git a/examples/sign1_verify_lean.c b/examples/sign1_verify_lean.c new file mode 100644 index 0000000..5940195 --- /dev/null +++ b/examples/sign1_verify_lean.c @@ -0,0 +1,111 @@ +/* sign1_verify_lean.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 . + */ + +/* Verify-only lean build example. + * + * Build this with -DWOLFCOSE_LEAN_VERIFY. That profile compiles in only the + * COSE_Sign1 verify path. It is the common on-device case: a device verifies a + * signed firmware image or attestation, and signing happens off-device on a + * server or HSM. Signing and the RNG it needs are not compiled in at all, while + * full RFC 9052 verification (header decode, crit enforcement, duplicate-label + * detection, Sig_structure rebuild) stays intact. + * + * The device holds only a public key. Here both the P-256 public key and a + * COSE_Sign1 message produced off-device are embedded as fixed test vectors. + */ + +#include +#include +#include +#include + +/* P-256 public key (X and Y, 32 bytes each). The matching private key lives + * off-device and is never present in a lean verify build. */ +static const unsigned char PUB_X[32] = { + 0x2c,0x3c,0x9f,0xd7,0xfc,0x15,0x48,0x7b,0x37,0x18,0x0e,0x37,0x95,0x56,0xb4,0xfd, + 0xbb,0x11,0x3c,0x78,0xe0,0xa5,0x3a,0x0b,0x25,0x71,0xf5,0xff,0xb0,0xdf,0x93,0x28}; +static const unsigned char PUB_Y[32] = { + 0x10,0xc3,0x85,0xc2,0xb6,0x8f,0x79,0xd7,0xe9,0x5e,0x43,0x62,0xf5,0xf4,0x06,0x21, + 0xdc,0x2c,0xf6,0x55,0x87,0xeb,0x94,0x61,0x13,0xe5,0xe2,0x8c,0xeb,0x2e,0xd2,0xce}; + +/* A COSE_Sign1 (ES256) over the payload below, signed off-device. */ +static const unsigned char COSE_SIGN1[] = { + 210,132,67,161,1,38,160,88,31,119,111,108,102,67,79,83,69,32,115,105,122,101, + 32,98,101,110,99,104,109,97,114,107,32,112,97,121,108,111,97,100,88,64,59,0, + 231,221,224,83,68,247,200,191,96,153,241,21,82,224,140,57,84,22,93,156,13,27, + 158,52,92,1,3,133,149,6,107,5,177,236,51,215,88,17,151,62,250,187,32,253,203, + 136,234,87,178,237,194,236,125,41,120,249,26,131,6,201,71,139}; + +static const char EXPECTED_PAYLOAD[] = "wolfCOSE size benchmark payload"; + +int main(void) +{ + ecc_key eccKey; + WOLFCOSE_KEY key; + WOLFCOSE_HDR hdr; + unsigned char scratch[256]; + const uint8_t* payload = NULL; + size_t payloadLen = 0; + int ret; + int rc = 1; + + if (wc_ecc_init(&eccKey) != 0) { + (void)printf("wc_ecc_init failed\n"); + return 1; + } + + /* Import the public key only. A NULL private scalar keeps this public. */ + ret = wc_ecc_import_unsigned(&eccKey, (byte*)PUB_X, (byte*)PUB_Y, NULL, + ECC_SECP256R1); + if (ret == 0) { + ret = wc_CoseKey_Init(&key); + } + if (ret == 0) { + ret = wc_CoseKey_SetEcc(&key, WOLFCOSE_CRV_P256, &eccKey); + } + + if (ret == 0) { + (void)memset(&hdr, 0, sizeof(hdr)); + ret = wc_CoseSign1_Verify(&key, COSE_SIGN1, sizeof(COSE_SIGN1), + NULL, 0, NULL, 0, + scratch, sizeof(scratch), &hdr, + &payload, &payloadLen); + } + + if (ret == WOLFCOSE_SUCCESS) { + if ((payloadLen == (sizeof(EXPECTED_PAYLOAD) - 1)) && + (payload != NULL) && + (memcmp(payload, EXPECTED_PAYLOAD, payloadLen) == 0)) { + (void)printf("lean verify-only: COSE_Sign1 ES256 verified, " + "payload = \"%.*s\"\n", (int)payloadLen, payload); + rc = 0; + } + else { + (void)printf("lean verify-only: payload mismatch\n"); + } + } + else { + (void)printf("lean verify-only: verify failed (%d)\n", ret); + } + + wc_CoseKey_Free(&key); + wc_ecc_free(&eccKey); + return rc; +} diff --git a/include/wolfcose/wolfcose.h b/include/wolfcose/wolfcose.h index bd524c5..a01430b 100644 --- a/include/wolfcose/wolfcose.h +++ b/include/wolfcose/wolfcose.h @@ -61,11 +61,62 @@ extern "C" { * * Users exclude features via WOLFCOSE_NO_* defines: * -DWOLFCOSE_NO_ENCRYPT0 -DWOLFCOSE_NO_MAC0 → Sign-only build - * -DWOLFCOSE_NO_SIGN1_SIGN -DWOLFCOSE_NO_CBOR_ENCODE → Verify-only build + * -DWOLFCOSE_NO_SIGN1_SIGN → Sign1 verify, no signing + * -DWOLFCOSE_LEAN_VERIFY → smallest secure verify-only * * Parent gates imply children unless child is explicitly excluded. + * + * A verify build keeps WOLFCOSE_CBOR_ENCODE enabled. Verification rebuilds the + * Sig_structure with the CBOR encoder, so the encoder cannot be excluded from a + * build that verifies. * ----- */ +/* ----- + * WOLFCOSE_LEAN_VERIFY: convenience profile for the smallest secure on-device + * build. It only verifies COSE_Sign1. This is the common embedded case where a + * device verifies signed firmware or attestation and signing happens off-device + * on a server or HSM. It removes signing (and, transitively, the RNG), the other + * message types, key distribution, and COSE_Key (de)serialization. It keeps full + * RFC 9052 verification: header decode, crit enforcement, duplicate-label + * detection, and the Sig_structure rebuild. It sets existing opt-out macros, so + * a user may still override any individual one before this point. + * ----- */ +#ifdef WOLFCOSE_LEAN_VERIFY + #ifndef WOLFCOSE_NO_SIGN1_SIGN + #define WOLFCOSE_NO_SIGN1_SIGN + #endif + #ifndef WOLFCOSE_NO_ENCRYPT0 + #define WOLFCOSE_NO_ENCRYPT0 + #endif + #ifndef WOLFCOSE_NO_MAC0 + #define WOLFCOSE_NO_MAC0 + #endif + #ifndef WOLFCOSE_NO_SIGN + #define WOLFCOSE_NO_SIGN + #endif + #ifndef WOLFCOSE_NO_ENCRYPT + #define WOLFCOSE_NO_ENCRYPT + #endif + #ifndef WOLFCOSE_NO_MAC + #define WOLFCOSE_NO_MAC + #endif + #ifndef WOLFCOSE_NO_RECIPIENTS + #define WOLFCOSE_NO_RECIPIENTS + #endif + #ifndef WOLFCOSE_NO_KEY_WRAP + #define WOLFCOSE_NO_KEY_WRAP + #endif + #ifndef WOLFCOSE_NO_ECDH + #define WOLFCOSE_NO_ECDH + #endif + #ifndef WOLFCOSE_NO_KEY_ENCODE + #define WOLFCOSE_NO_KEY_ENCODE + #endif + #ifndef WOLFCOSE_NO_KEY_DECODE + #define WOLFCOSE_NO_KEY_DECODE + #endif +#endif /* WOLFCOSE_LEAN_VERIFY */ + /* === Message Type Gates === */ /* SIGN1 */ @@ -208,6 +259,12 @@ extern "C" { #endif #endif +/* A lean verify build must actually verify. Fail loudly if it was reduced to a + * library that can do nothing. */ +#if defined(WOLFCOSE_LEAN_VERIFY) && !defined(WOLFCOSE_SIGN1_VERIFY) + #error "WOLFCOSE_LEAN_VERIFY requires Sign1 verify; do not also disable it" +#endif + /* ----- Error codes (-9000 to -9099) ----- */ #define WOLFCOSE_SUCCESS 0 #define WOLFCOSE_E_INVALID_ARG (-9000) diff --git a/src/wolfcose.c b/src/wolfcose.c index 2f18d5c..f762538 100644 --- a/src/wolfcose.c +++ b/src/wolfcose.c @@ -78,6 +78,10 @@ WOLFCOSE_LOCAL void wolfCose_ForceZero(void* mem, size_t len) /* ----- Constant-time comparison (side-channel safe) ----- */ +/* Only the MAC verify paths compare secret tags, so this helper is compiled + * only when one of them is enabled. This keeps sign-only and verify-only + * builds free of an unused-function warning. */ +#if defined(WOLFCOSE_MAC0_VERIFY) || defined(WOLFCOSE_MAC_VERIFY) /** * Constant-time memory comparison (matches wolfSSL ConstantCompare pattern). * Returns 0 if equal, non-zero otherwise. @@ -96,6 +100,7 @@ static int wolfCose_ConstantCompare(const byte* a, const byte* b, } return (int)result; } +#endif /* WOLFCOSE_MAC0_VERIFY || WOLFCOSE_MAC_VERIFY */ /* ----- RFC 9052 context strings ----- */ WOLFCOSE_LOCAL const uint8_t WOLFCOSE_CTX_SIGNATURE1[10] = { From a086bd1492aa589a76e9727a6a7282c881a7de7d Mon Sep 17 00:00:00 2001 From: aidan garske Date: Wed, 3 Jun 2026 11:32:24 -0700 Subject: [PATCH 2/3] Correct ES256 footprint/speed numbers, add GPLv3 LICENSE, and document lean verify build --- LICENSE | 674 ++++++++++++++++++++++++++++++++++++++++++ README.md | 10 +- docs/Home.md | 4 +- docs/Macros.md | 14 + docs/Message-Types.md | 2 +- tests/force_failure.c | 15 + tests/force_failure.h | 15 + 7 files changed, 727 insertions(+), 7 deletions(-) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..94a9ed0 --- /dev/null +++ b/LICENSE @@ -0,0 +1,674 @@ + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program 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. + + This program 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 . + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + Copyright (C) + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +. diff --git a/README.md b/README.md index 4bc167c..210d0ea 100644 --- a/README.md +++ b/README.md @@ -7,9 +7,11 @@ wolfCOSE is a lightweight C library implementing [CBOR (RFC 8949)](https://www.r - **Complete RFC 9052 message set**: all six COSE message types, including multi-signer `COSE_Sign` and multi-recipient `COSE_Encrypt` / `COSE_Mac` - **Post-quantum signing**: ML-DSA (FIPS 204) at all three security levels - **40 algorithms** across signing, encryption, MAC, and key distribution -- **Zero dynamic allocation**: all operations use caller-provided buffers -- **Tiny footprint**: 7.5 KB `.text` minimal build (Sign1+ECC), 25.6 KB full (40 algorithms), zero `.data`/`.bss` -- **Full COSE lifecycle in ~<1KB RAM** (excluding wolfCrypt internals) +- **Zero dynamic allocation**: heap-allocation-free and non-recursive. Every operation runs on caller-provided buffers within a bounded, target-customizable stack ceiling — nothing on the heap, zero `.data`/`.bss` +- **Tiny footprint** (ES256 `COSE_Sign1`, dead-code-eliminated, GCC 14.2 x86_64), verify-only → sign + verify: + - **3.5–5.1 KB** COSE engine, **5.1–6.8 KB** with the built-in CBOR engine (independent of the crypto backend) + - **26.2 KB** verify-only (`WOLFCOSE_LEAN_VERIFY`) → **34.6 KB** sign + verify — total flash with wolfCrypt +- **Fast** (ES256 `COSE_Sign1`, x86_64, wolfCrypt `sp_256` asm): **66,538** sign/s, **26,437** verify/s - **Path to FIPS 140-3** via wolfCrypt FIPS Certificate #4718 (sole crypto dependency) ## Supported Algorithms @@ -174,7 +176,7 @@ Full documentation is available in the [Wiki](https://github.com/wolfSSL/wolfCOS ## License -wolfCOSE is free software licensed under the [GPLv3](https://www.gnu.org/licenses/gpl-3.0.html). +wolfCOSE is free software licensed under [GPLv3](https://www.gnu.org/licenses/gpl-3.0.html); see [LICENSE](LICENSE) for the full text. Copyright (C) 2026 wolfSSL Inc. diff --git a/docs/Home.md b/docs/Home.md index 1cc3168..849d1d3 100644 --- a/docs/Home.md +++ b/docs/Home.md @@ -17,8 +17,8 @@ It uses [wolfSSL](https://www.wolfssl.com/) as the cryptographic backend and is | Complete RFC 9052 | All six COSE message types — Sign1, Sign, Encrypt0, Encrypt, Mac0, Mac | | Multi-signer / multi-recipient | Full `COSE_Sign`, `COSE_Encrypt`, and `COSE_Mac` support | | Post-quantum signing | ML-DSA (FIPS 204) at all three security levels — first COSE library to ship native PQC | -| Zero allocation | All operations use caller-provided buffers, no malloc | -| Tiny footprint | 7.5 KB minimal `.text` (Sign1+ECC), 25.6 KB full (40 algorithms), zero `.data`/`.bss` | +| Zero dynamic allocation | Heap-allocation-free, non-recursive; caller-provided buffers within a bounded, target-customizable stack ceiling — zero `.data`/`.bss` | +| Tiny footprint | 3.5–5.1 KB COSE engine, 5.1–6.8 KB with the built-in CBOR engine (verify-only → sign + verify); 26.2 KB → 34.6 KB total with wolfCrypt — ES256 `COSE_Sign1`, dead-code-eliminated | | 40 algorithms | Signing, encryption, MAC, and key distribution — classical and post-quantum | | FIPS 140-3 path | Sole crypto dependency is wolfCrypt FIPS Certificate #4718 | | CNSA 2.0 ready | ML-DSA-44/65/87 for quantum-resistant signatures | diff --git a/docs/Macros.md b/docs/Macros.md index b9f60b8..7fef640 100644 --- a/docs/Macros.md +++ b/docs/Macros.md @@ -140,6 +140,20 @@ wolfCOSE uses an opt-out design: all features are enabled by default, and you di --- +## Build Profiles + +Convenience macros that select a curated set of feature gates for a common deployment, so you do not list each `WOLFCOSE_NO_*` by hand. + +### `WOLFCOSE_LEAN_VERIFY` — minimal verify-only + +Defining `WOLFCOSE_LEAN_VERIFY` builds the smallest secure on-device profile: COSE_Sign1 verification only. It implies `WOLFCOSE_NO_SIGN1_SIGN`, `WOLFCOSE_NO_ENCRYPT0`, `WOLFCOSE_NO_MAC0`, `WOLFCOSE_NO_SIGN`, `WOLFCOSE_NO_ENCRYPT`, `WOLFCOSE_NO_MAC`, `WOLFCOSE_NO_RECIPIENTS`, `WOLFCOSE_NO_KEY_WRAP`, `WOLFCOSE_NO_ECDH`, `WOLFCOSE_NO_KEY_ENCODE`, and `WOLFCOSE_NO_KEY_DECODE`. Sign1 verify must stay enabled; the build errors out if it is also disabled. + +```bash +make lean-verify # builds + runs examples/sign1_verify_lean.c with the profile +# or directly: +cc -DWOLFCOSE_LEAN_VERIFY ... src/wolfcose.c src/wolfcose_cbor.c +``` + ## Example Build Configurations ### Sign-Only Build (Minimal) diff --git a/docs/Message-Types.md b/docs/Message-Types.md index a629dc1..007252e 100644 --- a/docs/Message-Types.md +++ b/docs/Message-Types.md @@ -162,7 +162,7 @@ You only pay for the message types you use. Define the appropriate `WOLFCOSE_NO_ -DWOLFCOSE_NO_SIGN1_SIGN -DWOLFCOSE_NO_CBOR_ENCODE /* verify-only build */ ``` -A minimal Sign1-verify-only build is around **7.5 KB of `.text`**, including the built-in CBOR engine. +A minimal Sign1-verify-only build (`WOLFCOSE_LEAN_VERIFY`) is about **5.1 KB** of wolfCOSE library code (3.5 KB COSE engine + the built-in CBOR engine), or **26.2 KB** total flash with a minimal wolfCrypt ES256 backend — rising to **6.8 KB** / **34.6 KB** for sign + verify. ## See also diff --git a/tests/force_failure.c b/tests/force_failure.c index e1b1235..cd07f98 100644 --- a/tests/force_failure.c +++ b/tests/force_failure.c @@ -2,6 +2,21 @@ * * 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 . + * * Implementation of forced failure injection for coverage testing. */ diff --git a/tests/force_failure.h b/tests/force_failure.h index 9012889..804ef87 100644 --- a/tests/force_failure.h +++ b/tests/force_failure.h @@ -2,6 +2,21 @@ * * 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 . + * * Forced failure injection layer for wolfCrypt functions. * This allows testing of error handling paths without rebuilding wolfSSL * with WOLFSSL_FORCE_MALLOC_FAIL_TEST. From 96d853248e6531763d7d371de7fecdfb764e4cca Mon Sep 17 00:00:00 2001 From: Aidan Garske Date: Wed, 3 Jun 2026 14:47:54 -0700 Subject: [PATCH 3/3] Add WOLFCOSE_LEAN_MLDSA (sign+verify) and WOLFCOSE_LEAN_VERIFY_MLDSA (verify-only) profiles, ML-DSA examples, docs, and CI --- .github/workflows/lean-build.yml | 73 ++++++++++++++++++++ Makefile | 19 +++++- docs/Macros.md | 22 ++++++ docs/Testing.md | 11 +++ examples/sign1_mldsa.c | 113 +++++++++++++++++++++++++++++++ examples/sign1_verify_mldsa.c | 92 +++++++++++++++++++++++++ include/wolfcose/wolfcose.h | 45 ++++++++++++ 7 files changed, 374 insertions(+), 1 deletion(-) create mode 100644 examples/sign1_mldsa.c create mode 100644 examples/sign1_verify_mldsa.c diff --git a/.github/workflows/lean-build.yml b/.github/workflows/lean-build.yml index e7d318f..1b406f8 100644 --- a/.github/workflows/lean-build.yml +++ b/.github/workflows/lean-build.yml @@ -124,4 +124,77 @@ jobs: gcc $SF -DWOLFCOSE_NO_SIGN1_VERIFY -DWOLFCOSE_NO_ENCRYPT0 -DWOLFCOSE_NO_MAC0 \ -DWOLFCOSE_NO_SIGN -DWOLFCOSE_NO_ENCRYPT -DWOLFCOSE_NO_MAC \ -DWOLFCOSE_NO_RECIPIENTS -c src/wolfcose.c -o /tmp/sign.o + echo "== WOLFCOSE_LEAN_MLDSA (sign+verify) ==" + gcc $SF -DWOLFCOSE_LEAN_MLDSA -c src/wolfcose.c -o /tmp/mldsa.o + echo "== WOLFCOSE_LEAN_VERIFY_MLDSA (verify-only) ==" + gcc $SF -DWOLFCOSE_LEAN_VERIFY_MLDSA -c src/wolfcose.c -o /tmp/mldsav.o echo "All lean configurations compiled clean." + + mldsa: + name: Post-quantum ML-DSA lean sign+verify and verify-only + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + + - name: Install dependencies + run: | + sudo apt-get update + sudo apt-get install -y autoconf automake libtool + + - 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 (ML-DSA backend) + id: cache-wolfssl + uses: actions/cache@v4 + with: + path: ~/wolfssl-mldsa + key: wolfssl-mldsa-v1-${{ steps.wolfssl-rev.outputs.sha }} + + # ML-DSA (FIPS 204) backend. Dilithium pulls in SHAKE/SHA-3; RNG stays for + # key generation and signing in the sign+verify demo. + - name: Build wolfSSL with ML-DSA + if: steps.cache-wolfssl.outputs.cache-hit != 'true' + run: | + cd ~ + git clone --depth 1 https://github.com/wolfSSL/wolfssl.git wolfssl-mldsa-src + cd wolfssl-mldsa-src + ./autogen.sh + ./configure --enable-cryptonly --enable-dilithium \ + --prefix=$HOME/wolfssl-mldsa + make -j$(nproc) + make install + + - name: Build and run the ML-DSA sign + verify example (WOLFCOSE_LEAN_MLDSA) + run: | + export WOLFSSL_DIR=$HOME/wolfssl-mldsa + export LD_LIBRARY_PATH=$WOLFSSL_DIR/lib + make mldsa-demo \ + CFLAGS="-std=c11 -DHAVE_ANONYMOUS_INLINE_AGGREGATES=1 -Os -Wall -Wextra -Wpedantic -Wshadow -Wconversion -ffunction-sections -fdata-sections -I./include -isystem $WOLFSSL_DIR/include" \ + LDFLAGS="-L$WOLFSSL_DIR/lib -lwolfssl -Wl,--gc-sections" + + - name: Build and run the lean ML-DSA verify-only example (WOLFCOSE_LEAN_VERIFY_MLDSA) + run: | + export WOLFSSL_DIR=$HOME/wolfssl-mldsa + export LD_LIBRARY_PATH=$WOLFSSL_DIR/lib + make mldsa-verify \ + CFLAGS="-std=c11 -DHAVE_ANONYMOUS_INLINE_AGGREGATES=1 -Os -Wall -Wextra -Wpedantic -Wshadow -Wconversion -ffunction-sections -fdata-sections -I./include -isystem $WOLFSSL_DIR/include" \ + LDFLAGS="-L$WOLFSSL_DIR/lib -lwolfssl -Wl,--gc-sections" + + - name: Assert signing API is absent from the ML-DSA verify-only build + run: | + BIN=examples/sign1_verify_mldsa + if nm "$BIN" | grep -E " [tT] wc_CoseSign1_Sign\b"; then + echo "FAIL: signing API present in a verify-only build"; exit 1 + fi + if ! nm "$BIN" | grep -qE " [tT] wc_CoseSign1_Verify\b"; then + echo "FAIL: verify API missing from a verify build"; exit 1 + fi + echo "OK: ML-DSA verify present, signing absent" + + - name: Report ML-DSA example sizes + run: | + size examples/sign1_mldsa || true + size examples/sign1_verify_mldsa || true diff --git a/Makefile b/Makefile index daa25d2..e52f7b4 100644 --- a/Makefile +++ b/Makefile @@ -46,6 +46,8 @@ ENC_DEMO = examples/encrypt0_demo MAC_DEMO = examples/mac0_demo SIGN1_DEMO = examples/sign1_demo LEANV_DEMO = examples/sign1_verify_lean +MLDSA_DEMO = examples/sign1_mldsa +MLDSAV_DEMO = examples/sign1_verify_mldsa # Comprehensive tests (CI) COMP_SIGN = examples/comprehensive/sign_all @@ -60,7 +62,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 coverage tool tool-test cmdline-test demo demos lean-verify comprehensive scenarios c99-check clean +.PHONY: all shared test coverage tool tool-test cmdline-test demo demos lean-verify mldsa-demo mldsa-verify comprehensive scenarios c99-check clean # --- Core library --- all: $(LIB_A) @@ -144,6 +146,21 @@ lean-verify: @echo "=== Running lean verify-only example ===" ./$(LEANV_DEMO) +# --- Post-quantum ML-DSA lean sign + verify (WOLFCOSE_LEAN_MLDSA) --- +# Requires wolfSSL built with ML-DSA (./configure --enable-dilithium). +mldsa-demo: + $(CC) $(CFLAGS) -DWOLFCOSE_LEAN_MLDSA -o $(MLDSA_DEMO) \ + $(MLDSA_DEMO).c src/wolfcose.c src/wolfcose_cbor.c $(LDFLAGS) + @echo "=== Running ML-DSA sign + verify example ===" + ./$(MLDSA_DEMO) + +# --- Smallest post-quantum verify-only (WOLFCOSE_LEAN_VERIFY_MLDSA) --- +mldsa-verify: + $(CC) $(CFLAGS) -DWOLFCOSE_LEAN_VERIFY_MLDSA -o $(MLDSAV_DEMO) \ + $(MLDSAV_DEMO).c src/wolfcose.c src/wolfcose_cbor.c $(LDFLAGS) + @echo "=== Running lean ML-DSA verify-only example ===" + ./$(MLDSAV_DEMO) + # --- Comprehensive algorithm tests (CI) --- comprehensive: $(LIB_A) @mkdir -p examples/comprehensive diff --git a/docs/Macros.md b/docs/Macros.md index 7fef640..bfd6899 100644 --- a/docs/Macros.md +++ b/docs/Macros.md @@ -154,6 +154,28 @@ make lean-verify # builds + runs examples/sign1_verify_lean.c with the profi cc -DWOLFCOSE_LEAN_VERIFY ... src/wolfcose.c src/wolfcose_cbor.c ``` +### `WOLFCOSE_LEAN_MLDSA` — lean post-quantum sign + verify + +Defining `WOLFCOSE_LEAN_MLDSA` builds a lean COSE_Sign1 **sign and verify** profile for post-quantum ML-DSA (FIPS 204). It drops the other message types and key distribution — implying `WOLFCOSE_NO_ENCRYPT0`, `WOLFCOSE_NO_MAC0`, `WOLFCOSE_NO_SIGN`, `WOLFCOSE_NO_ENCRYPT`, `WOLFCOSE_NO_MAC`, `WOLFCOSE_NO_RECIPIENTS`, `WOLFCOSE_NO_KEY_WRAP`, `WOLFCOSE_NO_ECDH`, `WOLFCOSE_NO_KEY_ENCODE`, and `WOLFCOSE_NO_KEY_DECODE` — but keeps **both** Sign1 sign and verify. It does **not** imply `WOLFCOSE_NO_SIGN1_SIGN`. + +Pair it with a wolfCrypt backend that enables only ML-DSA (`--enable-dilithium`) with ECC/RSA/AES disabled; the ECDSA/EdDSA/RSA paths then compile out via their wolfCrypt feature guards (`HAVE_ECC`, etc.). + +```bash +make mldsa-demo # builds + runs examples/sign1_mldsa.c (sign + verify) +# or directly: +cc -DWOLFCOSE_LEAN_MLDSA ... src/wolfcose.c src/wolfcose_cbor.c +``` + +### `WOLFCOSE_LEAN_VERIFY_MLDSA` — minimal post-quantum verify-only + +Defining `WOLFCOSE_LEAN_VERIFY_MLDSA` is the smallest secure on-device PQ build: ML-DSA COSE_Sign1 **verify only**. It implies `WOLFCOSE_LEAN_MLDSA` plus `WOLFCOSE_NO_SIGN1_SIGN`, so signing and the RNG it needs are not compiled in, while full RFC 9052 verification stays intact. Pair with a wolfCrypt build that enables ML-DSA **verify** only (e.g. `WOLFSSL_DILITHIUM_VERIFY_ONLY`). + +```bash +make mldsa-verify # builds + runs examples/sign1_verify_mldsa.c with the profile +# or directly: +cc -DWOLFCOSE_LEAN_VERIFY_MLDSA ... src/wolfcose.c src/wolfcose_cbor.c +``` + ## Example Build Configurations ### Sign-Only Build (Minimal) diff --git a/docs/Testing.md b/docs/Testing.md index fe6f8a6..41a7797 100644 --- a/docs/Testing.md +++ b/docs/Testing.md @@ -120,6 +120,17 @@ wolfCOSE runs the following CI checks on every push and pull request: 4. **Scenario Examples**: Real-world workflow tests 5. **Tool Tests**: CLI round-trip tests (17 algorithms) +### Lean and Post-Quantum Builds + +The Lean Build workflow (`.github/workflows/lean-build.yml`) exercises the minimal +on-device build profiles: + +| Job | What it checks | +|-----|----------------| +| Lean verify-only | Builds + runs `examples/sign1_verify_lean.c` with `WOLFCOSE_LEAN_VERIFY` against a minimal ECC-only wolfSSL; asserts the signing API is not linked. | +| Lean configs compile clean | Strict `-Werror` compile of the full, `WOLFCOSE_LEAN_VERIFY`, sign-only, `WOLFCOSE_LEAN_MLDSA`, and `WOLFCOSE_LEAN_VERIFY_MLDSA` configurations. | +| Post-quantum ML-DSA | Builds wolfSSL with ML-DSA and runs `make mldsa-demo` (sign+verify) and `make mldsa-verify` (verify-only); asserts the verify-only build links no signing API. | + ### Static Analysis | Tool | Purpose | diff --git a/examples/sign1_mldsa.c b/examples/sign1_mldsa.c new file mode 100644 index 0000000..13c1367 --- /dev/null +++ b/examples/sign1_mldsa.c @@ -0,0 +1,113 @@ +/* sign1_mldsa.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 . + */ + +/* Post-quantum COSE_Sign1 sign + verify with ML-DSA (FIPS 204). + * + * Full round trip: generate an ML-DSA-44 key, sign a payload into a + * COSE_Sign1, then verify it. Requires wolfSSL built with ML-DSA + * (./configure --enable-dilithium). For the verify-only on-device case see + * sign1_verify_mldsa.c and the WOLFCOSE_LEAN_MLDSA build profile. + */ + +#include +#include +#include +#include +#include + +static const char PAYLOAD[] = "wolfCOSE ML-DSA payload"; + +/* ML-DSA messages and scratch are large; keep them off the stack. */ +static unsigned char gScratch[20000]; +static unsigned char gMsg[8192]; + +int main(void) +{ + wc_MlDsaKey dlKey; + WOLFCOSE_KEY key; + WOLFCOSE_HDR hdr; + WC_RNG rng; + const uint8_t* payload = NULL; + size_t payloadLen = 0; + size_t msgLen = 0; + int ret; + int rc = 1; + + if (wc_InitRng(&rng) != 0) { + (void)printf("wc_InitRng failed\n"); + return 1; + } + + ret = wc_MlDsaKey_Init(&dlKey, NULL, INVALID_DEVID); + if (ret == 0) { + ret = wc_MlDsaKey_SetParams(&dlKey, WC_ML_DSA_44); + } + if (ret == 0) { + ret = wc_MlDsaKey_MakeKey(&dlKey, &rng); + } + if (ret == 0) { + ret = wc_CoseKey_Init(&key); + } + if (ret == 0) { + ret = wc_CoseKey_SetMlDsa(&key, WOLFCOSE_ALG_ML_DSA_44, &dlKey); + } + + /* Sign */ + if (ret == 0) { + ret = wc_CoseSign1_Sign(&key, WOLFCOSE_ALG_ML_DSA_44, NULL, 0, + (const uint8_t*)PAYLOAD, sizeof(PAYLOAD) - 1, + NULL, 0, NULL, 0, + gScratch, sizeof(gScratch), + gMsg, sizeof(gMsg), &msgLen, &rng); + } + if (ret == WOLFCOSE_SUCCESS) { + (void)printf("ML-DSA-44 sign: COSE_Sign1 produced, %u bytes\n", + (unsigned int)msgLen); + } + else { + (void)printf("ML-DSA-44 sign failed (%d)\n", ret); + } + + /* Verify the message we just signed */ + if (ret == WOLFCOSE_SUCCESS) { + (void)memset(&hdr, 0, sizeof(hdr)); + ret = wc_CoseSign1_Verify(&key, gMsg, msgLen, NULL, 0, NULL, 0, + gScratch, sizeof(gScratch), &hdr, + &payload, &payloadLen); + } + if (ret == WOLFCOSE_SUCCESS && + payloadLen == (sizeof(PAYLOAD) - 1) && payload != NULL && + memcmp(payload, PAYLOAD, payloadLen) == 0) { + (void)printf("ML-DSA-44 verify: OK, payload = \"%.*s\"\n", + (int)payloadLen, payload); + rc = 0; + } + else if (ret == WOLFCOSE_SUCCESS) { + (void)printf("ML-DSA-44 verify: payload mismatch\n"); + } + else { + (void)printf("ML-DSA-44 verify failed (%d)\n", ret); + } + + wc_CoseKey_Free(&key); + wc_MlDsaKey_Free(&dlKey); + wc_FreeRng(&rng); + return rc; +} diff --git a/examples/sign1_verify_mldsa.c b/examples/sign1_verify_mldsa.c new file mode 100644 index 0000000..5a2bfbf --- /dev/null +++ b/examples/sign1_verify_mldsa.c @@ -0,0 +1,92 @@ +/* sign1_verify_mldsa.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 . + */ + +/* Post-quantum verify-only lean example (ML-DSA / FIPS 204). + * + * Build with -DWOLFCOSE_LEAN_VERIFY_MLDSA: only the COSE_Sign1 verify path, no + * signing or RNG. The device holds only the ML-DSA public key; the public key + * and a COSE_Sign1 produced off-device are embedded below as fixed test + * vectors. Requires wolfSSL built with ML-DSA verify (e.g. --enable-dilithium). + */ + +#include +#include +#include +#include + +static const unsigned char MLDSA44_PUB[1312]={81,23,10,39,96,186,64,19,212,69,119,21,76,9,214,64,219,188,78,188,158,14,164,77,150,121,186,137,116,182,170,125,162,143,1,20,46,90,111,90,255,63,32,14,99,3,227,62,58,222,55,83,29,82,44,78,100,15,128,204,239,137,41,236,243,233,97,192,211,148,67,110,209,221,60,248,93,151,2,104,57,155,19,164,190,52,199,56,179,89,144,111,17,139,42,35,4,120,137,84,240,96,246,53,137,172,59,114,209,192,221,163,37,78,208,58,6,182,228,81,157,248,57,143,160,112,4,57,184,164,250,86,213,242,128,75,23,142,232,145,79,5,6,189,77,247,73,94,20,98,158,246,95,142,123,3,54,116,107,172,88,202,243,128,235,191,59,215,102,91,239,24,247,120,135,31,225,109,144,158,185,235,117,89,85,34,38,32,53,226,34,131,73,198,232,201,239,48,218,2,28,234,105,76,226,9,230,190,159,95,86,21,229,63,48,243,125,52,152,184,198,248,164,151,253,202,10,64,160,113,210,9,3,74,141,222,250,245,119,100,219,229,90,62,15,147,47,68,70,97,225,103,96,166,230,106,147,149,30,199,28,116,247,62,9,253,225,153,240,214,156,101,141,41,93,32,139,145,54,86,81,62,246,205,224,134,111,55,72,110,228,216,186,0,147,161,12,56,221,81,177,235,27,255,192,35,83,64,254,1,125,114,173,71,231,191,127,73,35,167,249,242,236,200,45,22,8,175,94,8,202,91,201,86,22,207,125,161,196,242,102,161,55,45,230,174,237,125,101,181,56,42,120,40,167,102,96,145,20,12,128,140,127,3,213,178,62,234,236,205,129,115,60,237,133,109,233,223,205,132,173,33,132,164,123,143,196,219,7,103,190,247,74,39,45,101,160,93,199,66,225,84,73,157,35,133,157,167,222,223,132,15,214,168,64,37,40,44,29,13,58,148,187,231,78,170,166,251,107,29,195,239,51,124,1,142,255,237,255,116,193,175,21,63,201,215,10,185,141,73,196,227,150,229,169,131,76,253,204,219,151,38,207,236,98,166,130,230,226,218,9,38,196,139,231,180,81,47,231,146,5,125,136,20,240,103,202,206,52,31,189,176,97,110,72,139,158,77,91,98,224,170,216,25,133,73,166,43,38,120,107,103,25,193,141,184,117,63,221,126,2,63,39,18,161,83,113,193,50,132,56,93,102,58,2,137,84,163,116,56,157,114,159,74,148,152,140,229,249,78,202,232,165,175,57,198,38,205,128,252,107,203,56,52,237,206,20,32,108,81,12,42,107,224,157,232,230,220,55,71,127,117,33,53,222,106,230,137,47,61,238,245,99,184,208,195,123,112,211,144,169,6,135,76,251,78,110,32,229,41,101,33,254,163,9,44,204,24,248,196,63,238,147,74,102,55,139,40,145,73,196,26,103,248,167,226,128,33,222,241,214,10,125,51,198,74,53,248,42,64,51,69,116,211,13,64,189,114,101,15,193,144,76,12,129,201,15,57,148,135,27,245,199,122,4,213,29,98,34,53,243,208,190,202,200,110,161,15,30,103,102,168,54,49,42,17,70,35,213,212,121,151,158,172,5,110,89,190,55,44,49,204,251,169,157,120,104,88,161,208,102,174,54,235,107,14,168,144,218,106,124,49,187,252,138,2,157,205,84,198,114,44,55,18,69,253,24,251,33,243,243,126,233,125,36,249,210,2,157,50,214,99,51,66,35,173,241,74,225,176,249,74,213,206,215,149,197,175,124,212,248,193,108,57,62,124,185,208,231,69,56,124,78,183,34,57,224,140,243,131,71,31,145,37,48,252,188,63,210,0,149,127,237,47,167,205,201,64,123,27,41,207,70,248,109,87,198,159,82,87,43,26,242,39,194,203,15,141,235,87,100,255,149,128,36,71,190,252,38,157,187,17,206,74,228,242,50,250,150,220,216,206,176,79,174,221,68,172,136,91,137,234,155,239,145,31,187,199,98,248,24,171,2,38,189,67,230,35,124,162,199,186,79,148,65,39,76,162,1,53,246,46,198,17,202,87,64,242,78,198,43,235,80,50,123,15,130,123,34,17,159,56,8,248,133,160,230,42,251,80,82,134,13,153,158,198,88,26,147,130,1,152,133,171,159,49,213,97,53,18,135,177,242,191,49,80,245,218,21,156,22,220,82,73,28,148,129,162,176,182,174,3,144,235,41,64,103,191,249,150,164,122,45,47,77,69,193,15,26,121,154,30,63,121,164,230,86,220,116,104,29,62,246,214,189,133,167,120,102,78,35,162,237,166,237,19,88,177,147,145,234,91,104,171,113,13,250,167,66,30,127,73,53,104,136,155,40,240,248,28,0,208,109,7,218,89,156,18,24,70,25,56,248,113,168,164,197,249,82,162,119,55,118,135,152,236,186,11,8,107,4,110,180,23,129,61,209,155,23,182,190,85,208,245,192,106,252,164,173,23,105,76,252,175,33,30,198,51,203,167,11,217,239,217,4,245,22,217,78,233,191,139,118,17,253,198,61,204,33,250,203,249,204,135,80,204,82,58,7,95,48,172,239,234,171,192,243,200,220,214,39,158,252,5,126,142,230,194,211,208,231,122,59,207,226,11,173,53,210,168,112,30,25,63,62,69,203,255,138,9,199,243,146,243,248,130,213,253,254,248,59,150,139,253,78,17,178,202,45,49,177,4,214,240,54,209,55,75,125,203,126,51,189,212,170,172,185,190,197,251,118,120,164,203,167,240,41,249,121,118,135,190,11,123,109,208,42,202,167,206,81,227,18,119,36,128,235,90,91,68,72,205,135,54,133,62,65,123,112,78,15,22,106,177,58,189,56,142,41,174,16,52,199,149,133,158,236,94,148,84,164,112,26,180,193,26,145,155,29,38,25,52,80,27,141,207,89,7,115,146,119,246,126,200,36,117,105,104,244,194,79,39,169,160,116,141,158,96,139,50,107,43,}; +static const unsigned char MLDSA44_COSE[2466]={210,132,68,161,1,56,47,160,88,33,119,111,108,102,67,79,83,69,32,77,76,45,68,83,65,32,98,101,110,99,104,109,97,114,107,32,112,97,121,108,111,97,100,89,9,116,81,253,252,115,238,218,166,203,103,70,245,7,213,174,169,167,96,85,133,60,243,119,233,122,138,142,71,159,125,155,88,186,243,181,43,181,207,44,39,237,185,60,231,23,150,41,140,161,114,36,158,222,254,207,216,190,136,148,138,218,255,122,30,53,183,136,1,55,141,181,93,90,71,100,101,246,141,133,56,200,242,174,179,251,166,162,138,203,253,120,200,32,46,123,193,218,209,181,7,35,109,155,118,29,82,0,121,10,245,176,97,64,31,48,253,5,144,240,139,192,155,203,58,81,159,191,152,110,104,84,40,39,191,116,235,150,123,31,104,248,168,150,210,207,13,168,147,83,239,172,204,242,228,153,67,238,43,130,192,101,146,203,176,129,101,191,78,204,43,64,175,230,207,226,222,91,205,255,109,30,43,98,214,54,206,203,78,211,91,208,55,222,40,152,249,151,208,212,88,178,172,8,93,165,207,94,250,170,240,19,87,204,228,16,196,206,45,140,149,145,117,238,187,115,200,84,136,68,20,202,203,229,238,11,143,37,182,177,189,183,156,122,11,105,208,244,197,156,105,50,155,118,216,174,122,115,70,95,70,167,1,67,227,107,228,182,252,5,67,110,38,178,36,45,158,146,146,126,184,250,75,19,3,179,226,19,25,11,167,60,161,26,143,177,99,166,32,28,176,198,122,133,197,47,114,153,142,190,105,108,112,170,17,58,79,186,198,170,177,232,71,153,119,19,146,241,42,144,20,167,138,240,90,224,157,16,99,89,253,225,185,60,193,143,57,25,183,255,216,130,254,133,229,161,35,173,83,200,228,90,97,254,128,169,154,40,58,255,101,105,111,151,250,213,20,181,190,14,192,84,231,138,250,203,112,149,242,60,237,114,159,67,237,119,9,159,186,20,149,166,45,134,249,75,196,125,246,132,13,31,61,150,19,103,226,188,68,202,108,95,51,209,75,246,51,9,9,207,231,9,180,150,108,229,6,28,241,244,91,20,107,28,119,15,74,245,65,209,99,219,77,234,123,49,27,246,91,14,190,44,228,242,42,111,164,102,131,188,219,69,49,68,6,168,157,47,187,111,112,242,5,84,150,49,242,169,25,55,140,249,199,213,123,82,231,33,205,23,235,120,194,20,122,53,127,103,159,90,107,80,97,27,237,173,57,168,33,56,180,142,227,67,138,186,164,118,58,83,31,32,211,13,60,78,138,15,199,96,6,71,115,160,208,111,32,245,229,141,3,68,39,176,203,125,199,25,158,249,205,71,255,100,253,40,245,211,155,25,156,159,9,194,253,238,14,181,197,144,231,218,75,177,116,16,181,175,67,219,149,58,119,10,45,55,207,175,102,2,59,64,90,107,18,16,147,37,115,79,189,199,195,122,26,186,229,29,160,119,65,100,11,96,220,95,31,135,140,139,30,62,71,216,45,83,201,46,2,3,96,183,6,214,234,138,142,98,187,192,146,221,232,112,48,164,58,88,58,243,101,5,95,140,247,107,170,130,131,35,202,243,191,115,220,23,188,150,17,23,133,133,143,247,61,19,186,19,213,152,134,219,86,170,230,232,2,251,18,231,52,142,45,120,191,54,41,129,221,181,128,63,108,7,167,25,119,161,164,144,94,95,250,159,209,241,88,93,228,162,35,69,226,89,187,225,185,249,168,157,236,64,58,66,226,161,227,4,131,226,2,234,24,19,203,107,245,88,99,74,253,87,78,136,117,165,82,224,129,146,1,183,84,104,43,189,96,206,32,253,17,75,205,79,128,36,138,101,231,155,75,215,4,229,3,96,59,83,59,36,22,236,107,191,134,180,34,104,157,121,121,12,190,89,96,126,208,30,165,32,224,127,116,37,173,84,234,219,173,17,105,151,96,21,199,77,131,101,61,104,129,53,208,53,4,108,87,143,119,117,181,81,162,140,106,37,237,236,67,104,88,26,240,95,149,227,15,22,16,72,83,138,240,78,139,220,224,157,247,186,173,88,90,58,253,214,111,243,28,15,133,48,23,171,251,241,222,196,226,27,224,41,149,33,13,1,249,40,202,246,70,32,225,130,45,210,118,143,240,63,83,182,90,86,224,158,103,216,9,101,194,181,145,206,155,212,206,238,80,191,87,48,217,140,65,214,146,222,240,117,216,142,107,252,247,221,77,144,41,8,80,147,197,246,252,79,160,247,102,214,133,38,235,29,63,198,229,162,90,178,133,58,253,134,175,74,93,232,86,237,240,50,148,17,210,190,9,242,31,6,226,181,101,242,86,145,4,227,120,181,203,107,91,34,144,117,0,48,125,6,246,144,233,198,65,55,116,222,115,27,185,89,75,110,25,66,82,57,213,34,186,156,58,210,253,202,183,149,105,97,192,84,224,186,116,232,116,236,193,185,202,104,227,194,238,85,160,139,209,28,4,197,199,238,34,61,218,131,223,182,248,122,183,137,128,132,228,223,235,3,145,252,63,132,53,2,64,225,20,197,218,155,120,111,80,245,197,165,115,72,158,174,112,206,59,254,34,190,218,135,44,171,171,211,197,39,232,245,218,59,8,250,64,8,167,98,172,220,176,148,99,102,7,196,218,135,55,64,217,110,41,252,227,219,164,151,104,24,42,6,136,199,56,111,122,211,13,46,192,135,57,124,153,47,148,117,48,242,69,229,218,50,176,156,87,165,49,50,221,35,128,15,154,190,99,246,32,46,187,65,106,139,89,160,77,152,10,84,42,95,30,111,107,47,98,231,159,106,150,146,209,184,163,208,43,183,212,131,237,157,177,114,55,193,227,210,177,233,20,77,13,91,172,13,125,135,242,209,60,29,165,206,69,226,177,141,128,186,171,129,179,17,0,214,63,77,186,108,185,125,6,225,0,249,237,68,43,248,39,18,160,97,29,201,242,187,135,2,156,254,124,192,121,52,254,114,14,237,106,102,122,64,57,42,118,173,189,11,65,255,191,27,47,98,28,27,80,138,91,245,3,39,204,243,159,237,72,3,250,88,138,96,60,156,12,239,44,232,249,158,72,90,44,37,114,128,54,35,108,227,153,73,123,176,242,204,246,98,253,108,141,64,120,10,100,64,214,48,126,62,128,225,83,58,201,227,3,97,182,157,7,144,32,144,254,160,250,103,82,242,113,49,240,249,244,213,95,216,33,206,124,31,250,226,151,122,1,243,170,127,139,67,75,217,136,249,194,202,130,6,249,238,96,152,207,40,37,47,8,28,45,68,58,11,187,24,132,56,17,122,219,81,200,185,4,43,229,131,252,250,35,144,123,165,240,66,193,29,54,90,94,86,107,172,6,84,34,58,53,239,247,123,179,57,51,147,144,200,59,198,136,144,203,45,231,132,2,178,88,18,58,197,254,198,74,184,237,169,123,185,160,105,70,186,153,165,239,90,2,51,223,100,54,117,186,200,191,217,112,87,43,106,86,113,124,8,148,162,121,162,78,194,160,19,160,65,11,199,246,197,245,196,46,186,233,104,213,37,211,114,116,249,139,116,202,140,26,253,24,215,21,18,65,42,40,98,146,15,4,217,59,248,106,18,6,201,107,118,249,66,85,109,140,86,109,82,106,185,21,69,66,209,218,103,98,34,158,61,84,9,114,67,118,12,192,145,153,253,161,8,88,57,77,124,151,33,107,248,165,144,23,99,120,189,71,64,204,136,56,169,195,132,12,191,175,92,11,38,214,104,82,203,238,246,104,187,172,241,61,223,76,149,148,22,195,192,255,128,176,35,194,56,217,14,25,33,89,50,250,74,156,4,139,190,94,125,250,150,27,142,103,60,206,142,152,108,105,72,223,221,140,218,26,36,112,156,163,182,124,53,218,93,39,159,239,149,123,248,130,230,177,251,128,93,127,219,33,78,109,23,210,119,134,58,190,103,131,201,107,7,211,83,40,125,229,74,136,188,153,124,67,31,81,126,8,165,117,191,186,191,246,224,18,173,164,101,239,63,218,159,173,68,79,63,246,255,8,234,190,253,152,16,254,188,245,192,129,69,175,159,119,249,50,123,22,227,176,32,77,235,58,240,168,164,100,189,27,122,49,104,176,192,139,164,204,195,157,58,167,192,221,255,179,230,212,132,15,119,222,99,72,90,70,18,227,26,242,131,70,50,54,197,25,41,45,46,136,34,194,135,185,51,93,117,149,181,229,194,225,212,145,203,237,120,199,180,187,230,69,159,84,114,34,116,181,194,185,12,66,57,178,60,40,196,196,233,0,159,235,15,190,139,35,41,229,116,211,116,213,32,203,199,66,32,121,78,165,92,120,51,248,19,0,236,98,242,179,0,24,202,194,61,145,39,82,174,215,99,172,181,4,164,135,26,200,123,97,254,154,232,218,239,5,49,35,141,241,169,88,156,8,223,111,175,189,113,221,144,149,178,29,165,200,1,249,212,97,108,208,133,73,255,191,188,39,8,131,181,26,131,155,58,171,31,222,117,31,106,216,68,232,67,236,73,114,70,72,226,1,216,123,248,94,61,193,249,224,125,162,241,9,27,50,19,165,152,240,10,89,73,186,113,71,76,167,209,80,105,219,107,105,32,227,186,38,238,158,110,35,82,242,63,248,100,210,89,28,38,220,212,8,180,251,67,35,156,149,58,104,168,154,238,128,96,156,50,113,237,126,11,102,149,26,20,115,226,130,216,16,251,82,175,234,21,200,75,28,247,235,133,180,207,165,89,34,225,36,81,143,163,22,28,192,249,223,139,208,232,15,164,94,46,196,16,250,125,164,198,202,130,9,104,109,54,68,114,26,222,76,230,37,34,77,242,7,82,180,131,12,133,18,193,167,167,232,110,138,33,55,18,233,213,255,57,63,39,191,29,3,40,185,36,52,3,131,224,103,80,160,191,2,140,88,173,168,153,61,235,39,119,130,124,231,199,133,153,172,180,5,171,115,18,145,220,10,132,240,83,147,132,41,146,0,98,42,208,45,252,1,100,162,126,224,179,231,51,15,95,192,152,247,7,134,118,40,100,8,117,42,247,92,103,229,95,44,132,136,1,57,125,151,160,250,109,245,173,48,185,65,84,119,55,127,117,175,237,139,55,73,17,131,32,83,117,51,140,244,24,124,251,77,42,237,74,158,121,224,101,239,117,190,187,223,30,120,210,209,137,196,213,37,80,2,141,254,134,54,75,230,157,1,81,176,122,250,182,36,241,7,111,154,228,179,129,245,47,90,78,101,87,254,143,46,134,90,30,24,178,19,105,30,95,123,209,207,53,70,39,39,234,192,72,16,139,52,48,219,21,4,12,23,55,59,84,91,94,97,150,180,183,194,207,237,7,25,71,99,121,134,146,156,161,163,176,182,193,212,241,2,7,8,19,37,43,45,70,79,87,118,151,179,194,200,229,230,249,253,254,27,32,78,96,117,125,150,156,162,192,247,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,30,50,61,}; + +static const char EXPECTED_PAYLOAD[] = "wolfCOSE ML-DSA benchmark payload"; + +int main(void) +{ + wc_MlDsaKey dlKey; + WOLFCOSE_KEY key; + WOLFCOSE_HDR hdr; + unsigned char scratch[4096]; + const uint8_t* payload = NULL; + size_t payloadLen = 0; + int ret; + int rc = 1; + + if (wc_MlDsaKey_Init(&dlKey, NULL, INVALID_DEVID) != 0) { + (void)printf("wc_MlDsaKey_Init failed\n"); + return 1; + } + + ret = wc_MlDsaKey_SetParams(&dlKey, WC_ML_DSA_44); + if (ret == 0) { + /* import the public key only (raw) */ + ret = wc_MlDsaKey_ImportPubRaw(&dlKey, MLDSA44_PUB, + (word32)sizeof(MLDSA44_PUB)); + } + if (ret == 0) { + ret = wc_CoseKey_Init(&key); + } + if (ret == 0) { + ret = wc_CoseKey_SetMlDsa(&key, WOLFCOSE_ALG_ML_DSA_44, &dlKey); + } + if (ret == 0) { + (void)memset(&hdr, 0, sizeof(hdr)); + ret = wc_CoseSign1_Verify(&key, MLDSA44_COSE, sizeof(MLDSA44_COSE), + NULL, 0, NULL, 0, + scratch, sizeof(scratch), &hdr, + &payload, &payloadLen); + } + + if (ret == WOLFCOSE_SUCCESS && + payloadLen == (sizeof(EXPECTED_PAYLOAD) - 1) && payload != NULL && + memcmp(payload, EXPECTED_PAYLOAD, payloadLen) == 0) { + (void)printf("lean ML-DSA verify-only: COSE_Sign1 ML-DSA-44 verified, " + "payload = \"%.*s\"\n", (int)payloadLen, payload); + rc = 0; + } + else if (ret == WOLFCOSE_SUCCESS) { + (void)printf("lean ML-DSA verify-only: payload mismatch\n"); + } + else { + (void)printf("lean ML-DSA verify-only: verify failed (%d)\n", ret); + } + + wc_CoseKey_Free(&key); + wc_MlDsaKey_Free(&dlKey); + return rc; +} diff --git a/include/wolfcose/wolfcose.h b/include/wolfcose/wolfcose.h index bb59ee9..b9fb47e 100644 --- a/include/wolfcose/wolfcose.h +++ b/include/wolfcose/wolfcose.h @@ -81,6 +81,51 @@ extern "C" { * detection, and the Sig_structure rebuild. It sets existing opt-out macros, so * a user may still override any individual one before this point. * ----- */ +/* ML-DSA (FIPS 204) lean profiles (see docs build-options page for details): + * WOLFCOSE_LEAN_MLDSA - lean Sign1 sign+verify with ML-DSA + * WOLFCOSE_LEAN_VERIFY_MLDSA - verify-only (LEAN_MLDSA + WOLFCOSE_NO_SIGN1_SIGN) */ +#ifdef WOLFCOSE_LEAN_VERIFY_MLDSA + #ifndef WOLFCOSE_LEAN_MLDSA + #define WOLFCOSE_LEAN_MLDSA + #endif + #ifndef WOLFCOSE_NO_SIGN1_SIGN + #define WOLFCOSE_NO_SIGN1_SIGN + #endif +#endif /* WOLFCOSE_LEAN_VERIFY_MLDSA */ + +#ifdef WOLFCOSE_LEAN_MLDSA + #ifndef WOLFCOSE_NO_ENCRYPT0 + #define WOLFCOSE_NO_ENCRYPT0 + #endif + #ifndef WOLFCOSE_NO_MAC0 + #define WOLFCOSE_NO_MAC0 + #endif + #ifndef WOLFCOSE_NO_SIGN + #define WOLFCOSE_NO_SIGN + #endif + #ifndef WOLFCOSE_NO_ENCRYPT + #define WOLFCOSE_NO_ENCRYPT + #endif + #ifndef WOLFCOSE_NO_MAC + #define WOLFCOSE_NO_MAC + #endif + #ifndef WOLFCOSE_NO_RECIPIENTS + #define WOLFCOSE_NO_RECIPIENTS + #endif + #ifndef WOLFCOSE_NO_KEY_WRAP + #define WOLFCOSE_NO_KEY_WRAP + #endif + #ifndef WOLFCOSE_NO_ECDH + #define WOLFCOSE_NO_ECDH + #endif + #ifndef WOLFCOSE_NO_KEY_ENCODE + #define WOLFCOSE_NO_KEY_ENCODE + #endif + #ifndef WOLFCOSE_NO_KEY_DECODE + #define WOLFCOSE_NO_KEY_DECODE + #endif +#endif /* WOLFCOSE_LEAN_MLDSA */ + #ifdef WOLFCOSE_LEAN_VERIFY #ifndef WOLFCOSE_NO_SIGN1_SIGN #define WOLFCOSE_NO_SIGN1_SIGN