Skip to content

Commit 4f35253

Browse files
authored
Add STM32Cube pack support docs and on device test for wolfCOSE (#61)
2 parents ff28105 + f48bda2 commit 4f35253

8 files changed

Lines changed: 356 additions & 0 deletions

File tree

IDE/STM32Cube/README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# wolfCOSE for STM32Cube
2+
3+
wolfCOSE is a zero-allocation C implementation of CBOR (RFC 8949) and COSE
4+
(RFC 9052/9053) built on top of wolfCrypt. It ships as an STM32Cube pack,
5+
`I-CUBE-wolfCOSE`, which provides wolfCOSE as an STM32 middleware and depends
6+
on the wolfSSL pack (`I-CUBE-wolfSSL`) for wolfCrypt.
7+
8+
This directory holds the files the pack builds from:
9+
10+
- `wolfcose_test.c` / `wolfcose_test.h`: a self test that runs a `COSE_Sign1`
11+
ES256 sign and verify and prints the result. It is built by the pack `Test`
12+
component. Call `wolfCOSETest()` from your application once the clocks and
13+
console are initialized; it returns `0` on success.
14+
- `default_conf.ftl`: the STM32CubeMX configuration template for the pack.
15+
16+
## Dependency
17+
18+
Install the wolfSSL pack (`I-CUBE-wolfSSL`, version 5.9.2 or later) first and
19+
enable `wolfCrypt: Core`. wolfCOSE uses wolfCrypt for hashing, signing, and AEAD.
20+
See the
21+
[wolfSSL STM32Cube README](https://github.com/wolfSSL/wolfssl/blob/master/IDE/STM32Cube/README.md)
22+
for the wolfSSL pack documentation and install instructions.
23+
24+
## Enabling in STM32CubeMX
25+
26+
1. `Help`, `Manage embedded software packages`, `From Local...` and install
27+
the wolfSSL pack, then this wolfCOSE pack.
28+
2. Enable the RNG peripheral under `Pinout & Configuration`, `Security`, `RNG`.
29+
Signing needs entropy, and `wc_GenerateSeed()` fails without it.
30+
3. In the project `.ioc`, open `Software Packs`, `Select Components`, expand
31+
`wolfCOSE` and check `Core` (and `wolfSSL`, `wolfCrypt: Core`). To run the
32+
on device self test, also check `wolfCOSE` `Test`.
33+
4. In the `Software Packs` configuration category, enable the wolfCOSE pack.
34+
5. Generate code and build.
35+
36+
## Configuration
37+
38+
wolfCOSE is configured through the wolfSSL `user_settings.h` (included before
39+
`wolfcose/settings.h`). Algorithm support such as `WOLFCOSE_ENABLE_ES384`,
40+
`WOLFCOSE_ENABLE_ES512`, and `WOLFCOSE_ENABLE_MLDSA` requires the matching
41+
wolfCrypt features (`HAVE_ECC`, `WOLFSSL_SHA384/512`, `WOLFSSL_HAVE_MLDSA`).
42+
43+
See the wolfCOSE `examples/` for sign1, mac0, and encrypt0 usage, and the
44+
[STM32Cube wiki page](https://github.com/wolfSSL/wolfCOSE/wiki/STM32Cube) for
45+
the full walkthrough including a ready to run NUCLEO-H563ZI project.

IDE/STM32Cube/default_conf.ftl

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
[#ftl]
2+
/**
3+
******************************************************************************
4+
* File Name : ${name}
5+
* Description : This file provides code for the configuration
6+
* of the ${name} instances.
7+
******************************************************************************
8+
[@common.optinclude name=mxTmpFolder+"/license.tmp"/][#--include License text --]
9+
******************************************************************************
10+
*/
11+
[#assign s = name]
12+
[#assign toto = s?replace(".","_")]
13+
[#assign toto = toto?replace("/","")]
14+
[#assign toto = toto?replace("-","_")]
15+
[#assign inclusion_protection = toto?upper_case]
16+
/* Define to prevent recursive inclusion -------------------------------------*/
17+
#ifndef __${inclusion_protection}__
18+
#define __${inclusion_protection}__
19+
20+
#ifdef __cplusplus
21+
extern "C" {
22+
#endif
23+
24+
25+
/* Includes ------------------------------------------------------------------*/
26+
[#if includes??]
27+
[#list includes as include]
28+
#include "${include}"
29+
[/#list]
30+
[/#if]
31+
32+
[#-- SWIPdatas is a list of SWIPconfigModel --]
33+
[#list SWIPdatas as SWIP]
34+
[#-- Global variables --]
35+
[#if SWIP.variables??]
36+
[#list SWIP.variables as variable]
37+
extern ${variable.value} ${variable.name};
38+
[/#list]
39+
[/#if]
40+
41+
[#-- Global variables --]
42+
43+
[#assign instName = SWIP.ipName]
44+
[#assign fileName = SWIP.fileName]
45+
[#assign version = SWIP.version]
46+
47+
/**
48+
MiddleWare name : ${instName}
49+
MiddleWare fileName : ${fileName}
50+
MiddleWare version : ${version}
51+
*/
52+
[#if SWIP.defines??]
53+
[#list SWIP.defines as definition]
54+
/*---------- [#if definition.comments??]${definition.comments}[/#if] -----------*/
55+
#define ${definition.name} #t#t ${definition.value}
56+
[#if definition.description??]${definition.description} [/#if]
57+
[/#list]
58+
[/#if]
59+
60+
61+
62+
[/#list]
63+
64+
#define WOLFCOSE_STM32_CUBEMX
65+
66+
/* wolfCOSE takes its crypto configuration from the wolfSSL user_settings.h. */
67+
68+
#if defined(WOLFCOSE_CONF_DEBUG) && WOLFCOSE_CONF_DEBUG == 1
69+
#define DEBUG_WOLFCOSE
70+
#endif
71+
72+
#ifdef __cplusplus
73+
}
74+
#endif
75+
#endif /* ${inclusion_protection}_H */
76+
77+
/**
78+
* @}
79+
*/
80+
81+
/*****END OF FILE****/

IDE/STM32Cube/wolfcose_test.c

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/* wolfcose_test.c
2+
*
3+
* Copyright (C) 2026 wolfSSL Inc.
4+
*
5+
* This file is part of wolfCOSE.
6+
*
7+
* wolfCOSE is free software; you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation; either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* wolfCOSE is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, see <https://www.gnu.org/licenses/>.
19+
*/
20+
21+
#include "wolfcose_test.h"
22+
23+
#include <stdio.h>
24+
#include <string.h>
25+
26+
int wolfCOSETest(void)
27+
{
28+
#if defined(WOLFCOSE_HAVE_ES256) && defined(WOLFCOSE_SIGN1_SIGN) && \
29+
defined(WOLFCOSE_SIGN1_VERIFY)
30+
WOLFCOSE_KEY key;
31+
ecc_key eccKey;
32+
WC_RNG rng;
33+
WOLFCOSE_HDR hdr;
34+
const uint8_t payload[] = "wolfCOSE STM32 self test";
35+
uint8_t scratch[WOLFCOSE_MAX_SCRATCH_SZ];
36+
uint8_t out[512];
37+
const uint8_t* decPayload = NULL;
38+
size_t payloadLen = sizeof(payload) - 1u;
39+
size_t outLen = 0;
40+
size_t decPayloadLen = 0;
41+
int rngInited = 0;
42+
int eccInited = 0;
43+
int keyInited = 0;
44+
int ret;
45+
46+
printf("Running wolfCOSE test (COSE_Sign1 ES256)...\n");
47+
48+
ret = wc_InitRng(&rng);
49+
if (ret == 0) {
50+
rngInited = 1;
51+
ret = wc_ecc_init(&eccKey);
52+
}
53+
if (ret == 0) {
54+
eccInited = 1;
55+
ret = wc_ecc_make_key(&rng, 32, &eccKey);
56+
}
57+
if (ret == 0) {
58+
ret = wc_CoseKey_Init(&key);
59+
}
60+
if (ret == 0) {
61+
keyInited = 1;
62+
ret = wc_CoseKey_SetEcc(&key, WOLFCOSE_CRV_P256, &eccKey);
63+
}
64+
if (ret == 0) {
65+
ret = wc_CoseSign1_Sign(&key, WOLFCOSE_ALG_ES256,
66+
NULL, 0, payload, payloadLen, NULL, 0, NULL, 0,
67+
scratch, sizeof(scratch), out, sizeof(out), &outLen, &rng);
68+
}
69+
if (ret == 0) {
70+
ret = wc_CoseSign1_Verify(&key, out, outLen, NULL, 0, NULL, 0,
71+
scratch, sizeof(scratch), &hdr, &decPayload, &decPayloadLen);
72+
}
73+
if (ret == 0) {
74+
if ((decPayload == NULL) || (decPayloadLen != payloadLen) ||
75+
(memcmp(decPayload, payload, decPayloadLen) != 0) ||
76+
(hdr.alg != WOLFCOSE_ALG_ES256)) {
77+
ret = -1;
78+
}
79+
}
80+
81+
if (keyInited != 0) {
82+
wc_CoseKey_Free(&key);
83+
}
84+
if (eccInited != 0) {
85+
(void)wc_ecc_free(&eccKey);
86+
}
87+
if (rngInited != 0) {
88+
(void)wc_FreeRng(&rng);
89+
}
90+
91+
if (ret == 0) {
92+
printf("wolfCOSE test: PASS (COSE_Sign1 %lu bytes)\n", (unsigned long)outLen);
93+
}
94+
else {
95+
printf("wolfCOSE test: FAIL, ret %d\n", ret);
96+
}
97+
return ret;
98+
#else
99+
/* ES256 COSE_Sign1 not compiled in; report not run so it is not read as pass */
100+
printf("wolfCOSE test: needs ES256 with COSE_Sign1 sign and verify\n");
101+
return -1;
102+
#endif
103+
}

IDE/STM32Cube/wolfcose_test.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/* wolfcose_test.h
2+
*
3+
* Copyright (C) 2026 wolfSSL Inc.
4+
*
5+
* This file is part of wolfCOSE.
6+
*
7+
* wolfCOSE is free software; you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation; either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* wolfCOSE is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, see <https://www.gnu.org/licenses/>.
19+
*/
20+
21+
#ifndef WOLFCOSE_TEST_H_
22+
#define WOLFCOSE_TEST_H_
23+
24+
#include <wolfcose/wolfcose.h>
25+
#include <wolfssl/wolfcrypt/ecc.h>
26+
#include <wolfssl/wolfcrypt/random.h>
27+
28+
#ifdef __cplusplus
29+
extern "C" {
30+
#endif
31+
32+
int wolfCOSETest(void);
33+
34+
#ifdef __cplusplus
35+
}
36+
#endif
37+
38+
#endif /* WOLFCOSE_TEST_H_ */

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ wolfCOSE is a lightweight C library implementing [CBOR (RFC 8949)](https://www.r
1717
(`WOLFCOSE_LEAN_VERIFY_MLDSA`) and **~35.8 KB** sign + verify, within about 1 KB of classical ES256. The wolfCOSE portion
1818
alone is **4.6 KB** and **~6.6 KB** respectively. See [Footprint](https://github.com/wolfSSL/wolfCOSE/wiki/Footprint)
1919
- **Path to FIPS 140-3**: via wolfCrypt **FIPS Certificate #4718** (sole crypto dependency)
20+
- **STM32Cube ready**: available as a drop-in STM32Cube pack (`I-CUBE-wolfCOSE`) for STM32CubeMX and STM32CubeIDE, so STM32 devices get COSE and CBOR out of the box (see [STM32Cube](https://github.com/wolfSSL/wolfCOSE/wiki/STM32Cube))
2021

2122
## Supported Algorithms
2223

@@ -193,6 +194,7 @@ Full documentation is available in the [Wiki](https://github.com/wolfSSL/wolfCOS
193194
- [Testing](https://github.com/wolfSSL/wolfCOSE/wiki/Testing): Test infrastructure, coverage, and failure injection
194195
- [MISRA Compliance](https://github.com/wolfSSL/wolfCOSE/wiki/MISRA-Compliance): MISRA C:2012 and C:2023 compliance status and deviation rationale
195196
- [Project Structure](https://github.com/wolfSSL/wolfCOSE/wiki/Project-Structure): Source file layout
197+
- [STM32Cube](https://github.com/wolfSSL/wolfCOSE/wiki/STM32Cube): Install and run wolfCOSE as an STM32Cube pack on device
196198

197199
## Release Notes
198200

docs/Home.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ It uses [wolfSSL](https://www.wolfssl.com/) as the cryptographic backend and is
3636
| [[Footprint]] | Size and speed numbers, desktop and on-device |
3737
| [[Testing]] | Unit tests, coverage, and failure injection |
3838
| [[Project Structure]] | Source code layout and file descriptions |
39+
| [[STM32Cube]] | Install and run wolfCOSE as an STM32Cube pack on device |
3940
| [[Release Notes]] | Per-version changelog and release highlights |
4041

4142
## Supported Message Types

docs/STM32Cube.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# STM32Cube
2+
3+
wolfCOSE ships as an STM32Cube pack, `I-CUBE-wolfCOSE`, so it drops into any
4+
STM32CubeMX or STM32CubeIDE project without manual source integration. The pack
5+
provides wolfCOSE as an STM32 middleware and uses the wolfSSL pack
6+
(`I-CUBE-wolfSSL`) for all cryptography.
7+
8+
Supported cores: Cortex-M0, M0+, M3, M4, M7, M23, M33, M55, and STM32MP1.
9+
10+
## Prerequisites
11+
12+
- STM32CubeMX, plus STM32CubeIDE or another toolchain to build.
13+
- The wolfSSL pack, `I-CUBE-wolfSSL` 5.9.2 or later. wolfCOSE depends on
14+
wolfCrypt for hashing, signing, and AEAD.
15+
- A board with a hardware RNG and a UART for console output.
16+
17+
## Install the packs
18+
19+
1. Download the wolfSSL pack
20+
([I-CUBE-wolfSSL.pack](https://www.wolfssl.com/files/ide/I-CUBE-wolfSSL.pack))
21+
and the wolfCOSE pack
22+
([I-CUBE-wolfCOSE.pack](https://www.wolfssl.com/files/ide/I-CUBE-wolfCOSE.pack)).
23+
2. In STM32CubeMX, open `Help`, `Manage embedded software packages`,
24+
`From Local...`, and install the wolfSSL pack first, then the wolfCOSE pack.
25+
26+
## Add wolfCOSE to a project
27+
28+
1. Open or create a project `.ioc` for your board.
29+
2. Enable the RNG peripheral under `Pinout & Configuration`, `Security`, `RNG`.
30+
Signing needs entropy, and `wc_GenerateSeed()` fails without it.
31+
3. Open `Software Packs`, `Select Components`.
32+
4. Enable `wolfSSL` `wolfCrypt` `Core` and `wolfCOSE` `Core`. To run the
33+
on device self test, also enable `wolfCOSE` `Test`.
34+
5. In the `Software Packs` configuration category, enable each pack.
35+
6. Generate code and build with your toolchain.
36+
37+
## Configure algorithms
38+
39+
wolfCOSE reads its configuration from the wolfSSL `user_settings.h`, included
40+
before `wolfcose/settings.h`. Enable the wolfCrypt features that match the COSE
41+
algorithms you use:
42+
43+
- ES256: `HAVE_ECC`, `WOLFSSL_SHA256`
44+
- ES384 or ES512: add `WOLFSSL_SHA384` or `WOLFSSL_SHA512`
45+
- ML-DSA (RFC 9964): `WOLFSSL_HAVE_MLDSA`
46+
- Encrypt0 AEAD: `HAVE_AESGCM`, or `HAVE_CHACHA` with `HAVE_POLY1305`
47+
- MAC0: HMAC, which is on by default with SHA support
48+
49+
If a required wolfCrypt feature is missing, `wolfcose/settings.h` raises a
50+
compile error naming it.
51+
52+
## Run on a device
53+
54+
The `Test` component builds `wolfcose_test.c`, a self test that runs a
55+
`COSE_Sign1` sign and verify and reports the result over your configured
56+
console. Call `wolfCOSETest()` from your application once `main` has initialized
57+
the clocks and console.
58+
59+
A ready to run example for the NUCLEO-H563ZI board lives in
60+
[wolfssl-examples-stm32](https://github.com/wolfSSL/wolfssl-examples-stm32),
61+
with a pre-configured `.ioc`: install the packs, open the `.ioc`, generate,
62+
add the glue and software-crypto config from that example's README, build,
63+
flash, and watch the console.
64+
65+
Verified on NUCLEO-H563ZI hardware, the console prints:
66+
67+
```
68+
== wolfCOSE NUCLEO-H563ZI ==
69+
Running wolfCOSE test (COSE_Sign1 ES256)...
70+
wolfCOSE test: PASS (COSE_Sign1 99 bytes)
71+
```
72+
73+
## Notes
74+
75+
- Only pack source is kept in the repository. The built pack is posted at
76+
[wolfssl.com/files/ide/I-CUBE-wolfCOSE.pack](https://www.wolfssl.com/files/ide/I-CUBE-wolfCOSE.pack).
77+
- On some STM32 families the wolfSSL pack enables hardware hash and RNG by
78+
default. If a build reports a missing HAL module or hash symbol, enable the
79+
matching peripheral in the `.ioc`, or select software crypto with
80+
`NO_STM32_HASH` and `NO_STM32_RNG` in the generated
81+
`wolfSSL.I-CUBE-wolfSSL_conf.h`. On the STM32H5 the hardware hash block
82+
references a HAL enum that does not exist, so software crypto is required
83+
there; the
84+
[NUCLEO-H563ZI example](https://github.com/wolfSSL/wolfssl-examples-stm32/tree/master/wolfCOSE-STM32-Example)
85+
shows the exact edit.

docs/_Sidebar.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@
1010
- [[Testing]]
1111
- [[MISRA Compliance]]
1212
- [[Project Structure]]
13+
- [[STM32Cube]]
1314
- [[Release Notes]]

0 commit comments

Comments
 (0)