Skip to content

Commit 5074554

Browse files
committed
Add Nuvoton NuMicro M2354 crypto callback port
1 parent b6fd669 commit 5074554

20 files changed

Lines changed: 5148 additions & 11 deletions
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
name: "Nuvoton M2354 port"
2+
3+
# Keeps the Nuvoton NuMicro M2354 crypto callback port compiling against the
4+
# real vendor BSP. The port has no autotools option and is not built by any
5+
# other job: an application compiles wolfcrypt/src/port/nuvoton/*.c into its own
6+
# project, the same way wolfcrypt/src/port/st/stm32.c is carried. Without this
7+
# guard a header rename in the BSP, or a refactor in aes.c, ecc.c or cryptocb.h,
8+
# would break the port silently until someone next built for the board.
9+
#
10+
# Nothing is faked. arm-none-eabi-gcc is a plain apt package and the BSP is a
11+
# public GitHub repository, so both legs compile against the genuine Nuvoton
12+
# StdDriver headers at a pinned commit.
13+
#
14+
# secure WOLFSSL_NUVOTON_SECURE, the whole port including nuvoton_hw.c
15+
# nonsecure WOLFSSL_NUVOTON_NSC, where nuvoton_hw.c compiles to nothing
16+
# and the cmse_nonsecure_entry veneers supply the symbols
17+
#
18+
# The runnable example, including those veneers, lives in wolfssl-examples
19+
# under embedded/nuvoton_m2354. Functional correctness is validated on a
20+
# NuMaker-M2354, not here; see wolfcrypt/src/port/nuvoton/README.md.
21+
22+
# START OF COMMON SECTION
23+
on:
24+
push:
25+
branches: [ 'master', 'main', 'release/**' ]
26+
paths:
27+
- 'wolfcrypt/src/port/nuvoton/**'
28+
- 'wolfssl/wolfcrypt/port/nuvoton/**'
29+
- '.github/workflows/nuvoton-m2354-compile.yml'
30+
pull_request:
31+
types: [opened, synchronize, reopened, ready_for_review]
32+
branches: [ '**' ]
33+
paths:
34+
- 'wolfcrypt/src/**'
35+
- 'wolfssl/wolfcrypt/**'
36+
- '.github/workflows/nuvoton-m2354-compile.yml'
37+
workflow_dispatch:
38+
39+
concurrency:
40+
group: ${{ github.workflow }}-${{ github.ref }}
41+
cancel-in-progress: true
42+
43+
permissions:
44+
contents: read
45+
# END OF COMMON SECTION
46+
47+
env:
48+
# Pinned so a BSP change cannot break a PR that did not touch the port. Bump
49+
# it deliberately.
50+
BSP_REF: 3d423be763edabe1d8b3f27dea363b69e787f8f9
51+
52+
jobs:
53+
compile:
54+
name: ${{ matrix.leg }} (Cortex-M23)
55+
if: ${{ github.event_name != 'pull_request' || github.event.pull_request.draft == false }}
56+
runs-on: ubuntu-24.04
57+
timeout-minutes: 20
58+
strategy:
59+
fail-fast: false
60+
matrix:
61+
include:
62+
- leg: secure
63+
world: WOLFSSL_NUVOTON_SECURE
64+
- leg: nonsecure
65+
world: WOLFSSL_NUVOTON_NSC
66+
steps:
67+
- uses: actions/checkout@v4
68+
name: Checkout wolfSSL
69+
70+
- name: Install the toolchain
71+
run: |
72+
sudo apt-get update
73+
sudo apt-get install -y gcc-arm-none-eabi
74+
75+
- name: Checkout the Nuvoton M2354 BSP
76+
run: |
77+
set -e
78+
# The full repository is around 256 MB and almost none of it is
79+
# needed, so take one commit and only the driver tree.
80+
git clone --filter=blob:none --no-checkout --sparse \
81+
https://github.com/OpenNuvoton/M2354BSP "$GITHUB_WORKSPACE/M2354BSP"
82+
cd "$GITHUB_WORKSPACE/M2354BSP"
83+
git sparse-checkout set \
84+
Library/StdDriver/inc Library/Device Library/CMSIS
85+
git fetch --depth 1 origin "$BSP_REF"
86+
git checkout "$BSP_REF"
87+
88+
- name: Compile the port
89+
run: |
90+
set -e
91+
BSP="$GITHUB_WORKSPACE/M2354BSP"
92+
mkdir -p "$GITHUB_WORKSPACE/cfg"
93+
94+
# Minimal configuration: enough to reach every engine in the port.
95+
# The example in wolfssl-examples carries a realistic one.
96+
cat > "$GITHUB_WORKSPACE/cfg/user_settings.h" <<'EOF'
97+
#ifndef CI_USER_SETTINGS_H
98+
#define CI_USER_SETTINGS_H
99+
#define WOLFSSL_NUVOTON_M2354
100+
#define SINGLE_THREADED
101+
#define NO_FILESYSTEM
102+
#define WOLFSSL_SMALL_STACK
103+
#define WOLFSSL_SHA224
104+
#define WOLFSSL_SHA384
105+
#define WOLFSSL_SHA512
106+
#define HAVE_HASHDRBG
107+
#define HAVE_AES_CBC
108+
#define HAVE_AES_ECB
109+
#define WOLFSSL_AES_DIRECT
110+
#define WOLFSSL_AES_COUNTER
111+
#define HAVE_AESGCM
112+
#define HAVE_ECC
113+
#define HAVE_ECC_DHE
114+
#define HAVE_ECC_SIGN
115+
#define HAVE_ECC_VERIFY
116+
#define HAVE_ECC384
117+
#define WOLFSSL_KEY_GEN
118+
#define TFM_TIMING_RESISTANT
119+
#define ECC_TIMING_RESISTANT
120+
#define WC_RSA_BLINDING
121+
#endif
122+
EOF
123+
124+
CFLAGS="-mcpu=cortex-m23 -mthumb -Os -Wall -Wextra -Werror -c"
125+
CFLAGS="$CFLAGS -I. -I$GITHUB_WORKSPACE/cfg -DWOLFSSL_USER_SETTINGS"
126+
CFLAGS="$CFLAGS -D${{ matrix.world }}"
127+
CFLAGS="$CFLAGS -I$BSP/Library/StdDriver/inc"
128+
CFLAGS="$CFLAGS -I$BSP/Library/Device/Nuvoton/M2354/Include"
129+
CFLAGS="$CFLAGS -I$BSP/Library/CMSIS/Include"
130+
131+
for f in wolfcrypt/src/port/nuvoton/*.c wolfcrypt/src/random.c; do
132+
echo " $f"
133+
# shellcheck disable=SC2086
134+
arm-none-eabi-gcc $CFLAGS -o /dev/null "$f"
135+
done

.wolfssl_known_macro_extras

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,6 +1038,7 @@ WOLFSSL_NO_OCSP_OPTIONAL_CERTS
10381038
WOLFSSL_NO_RSA_KEY_CHECK
10391039
WOLFSSL_NO_SERVER_GROUPS_EXT
10401040
WOLFSSL_NO_SESSION_STATS
1041+
WOLFSSL_NO_SHA512_HASHTYPE
10411042
WOLFSSL_NO_SIGALG
10421043
WOLFSSL_NO_SOCKADDR_UN
10431044
WOLFSSL_NO_STRICT_CIPHER_SUITE
@@ -1046,6 +1047,13 @@ WOLFSSL_NO_TRUSTED_CERTS_VERIFY
10461047
WOLFSSL_NO_WORD64_OPS
10471048
WOLFSSL_NO_XOR_OPS
10481049
WOLFSSL_NRF51_AES
1050+
WOLFSSL_NUVOTON_NO_AESCCM
1051+
WOLFSSL_NUVOTON_NO_AESGCM
1052+
WOLFSSL_NUVOTON_NO_HW_MUTEX
1053+
WOLFSSL_NUVOTON_NO_SP_DEFAULT
1054+
WOLFSSL_NUVOTON_NSC
1055+
WOLFSSL_NUVOTON_NSC_IMPL
1056+
WOLFSSL_NUVOTON_RNG_OFFLOAD
10491057
WOLFSSL_NXP_CASPER_ECC_MUL2ADD
10501058
WOLFSSL_NXP_CASPER_ECC_MULMOD
10511059
WOLFSSL_NXP_LPC55S6X

wolfcrypt/src/asn.c

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33997,17 +33997,22 @@ int DecodeECC_DSA_Sig_Ex(const byte* sig, word32 sigLen, mp_int* r, mp_int* s,
3399733997
#endif
3399833998

3399933999

34000-
#ifdef WOLFSSL_ASN_TEMPLATE
34001-
#if defined(HAVE_ECC) && defined(WOLFSSL_CUSTOM_CURVES)
34000+
#ifdef WOLFSSL_ASN_HEX_STRING
3400234001
/* Convert data to hex string.
3400334002
*
3400434003
* Big-endian byte array is converted to big-endian hexadecimal string.
3400534004
*
34005+
* Written for the custom ECC curve parameters, which SEC 1 carries as byte
34006+
* arrays and ecc_set_type holds as strings. Hardware ports whose driver takes
34007+
* key material the same way reuse it rather than growing their own copy; see
34008+
* WOLFSSL_ASN_HEX_STRING in asn.h. Base16_Decode() goes the other way and
34009+
* accepts either case, so the two pair up.
34010+
*
3400634011
* @param [in] input Buffer containing data.
3400734012
* @param [in] inSz Size of data in buffer.
34008-
* @param [out] out Buffer to hold hex string.
34013+
* @param [out] out Buffer to hold hex string. Needs inSz * 2 + 1 bytes.
3400934014
*/
34010-
static void DataToHexString(const byte* input, word32 inSz, char* out)
34015+
void wc_DataToHexString(const byte* input, word32 inSz, char* out)
3401134016
{
3401234017
static const char hexChar[] = { '0', '1', '2', '3', '4', '5', '6', '7',
3401334018
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
@@ -34021,7 +34026,10 @@ static void DataToHexString(const byte* input, word32 inSz, char* out)
3402134026
/* NUL terminate string. */
3402234027
out[i * 2] = '\0';
3402334028
}
34029+
#endif /* WOLFSSL_ASN_HEX_STRING */
3402434030

34031+
#ifdef WOLFSSL_ASN_TEMPLATE
34032+
#if defined(HAVE_ECC) && defined(WOLFSSL_CUSTOM_CURVES)
3402534033
#ifndef WOLFSSL_ECC_CURVE_STATIC
3402634034
/* Convert data to hex string and place in allocated buffer.
3402734035
*
@@ -34048,7 +34056,7 @@ static int DataToHexStringAlloc(const byte* input, word32 inSz, char** out,
3404834056
}
3404934057
else {
3405034058
/* Convert to hex string. */
34051-
DataToHexString(input, inSz, str);
34059+
wc_DataToHexString(input, inSz, str);
3405234060
*out = str;
3405334061
}
3405434062

@@ -34251,23 +34259,23 @@ static int EccSpecifiedECDomainDecode(const byte* input, word32 inSz,
3425134259
#else
3425234260
if (ret == 0) {
3425334261
/* Base X-ordinate */
34254-
DataToHexString(base + 1, (word32)curve->size, (char *)curve->Gx);
34262+
wc_DataToHexString(base + 1, (word32)curve->size, (char *)curve->Gx);
3425534263
/* Base Y-ordinate */
34256-
DataToHexString(base + 1 + curve->size, (word32)curve->size, (char *)curve->Gy);
34264+
wc_DataToHexString(base + 1 + curve->size, (word32)curve->size, (char *)curve->Gy);
3425734265
/* Prime */
34258-
DataToHexString(dataASN[ECCSPECIFIEDASN_IDX_PRIME_P].data.ref.data,
34266+
wc_DataToHexString(dataASN[ECCSPECIFIEDASN_IDX_PRIME_P].data.ref.data,
3425934267
dataASN[ECCSPECIFIEDASN_IDX_PRIME_P].data.ref.length,
3426034268
(char *)curve->prime);
3426134269
/* Parameter A */
34262-
DataToHexString(dataASN[ECCSPECIFIEDASN_IDX_PARAM_A].data.ref.data,
34270+
wc_DataToHexString(dataASN[ECCSPECIFIEDASN_IDX_PARAM_A].data.ref.data,
3426334271
dataASN[ECCSPECIFIEDASN_IDX_PARAM_A].data.ref.length,
3426434272
(char *)curve->Af);
3426534273
/* Parameter B */
34266-
DataToHexString(dataASN[ECCSPECIFIEDASN_IDX_PARAM_B].data.ref.data,
34274+
wc_DataToHexString(dataASN[ECCSPECIFIEDASN_IDX_PARAM_B].data.ref.data,
3426734275
dataASN[ECCSPECIFIEDASN_IDX_PARAM_B].data.ref.length,
3426834276
(char *)curve->Bf);
3426934277
/* Order of curve */
34270-
DataToHexString(dataASN[ECCSPECIFIEDASN_IDX_ORDER].data.ref.data,
34278+
wc_DataToHexString(dataASN[ECCSPECIFIEDASN_IDX_ORDER].data.ref.data,
3427134279
dataASN[ECCSPECIFIEDASN_IDX_ORDER].data.ref.length,
3427234280
(char *)curve->order);
3427334281
}

wolfcrypt/src/include.am

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,15 @@ EXTRA_DIST += wolfcrypt/src/port/ti/ti-aes.c \
133133
wolfcrypt/src/port/silabs/silabs_hash.c \
134134
wolfcrypt/src/port/silabs/silabs_random.c \
135135
wolfcrypt/src/port/silabs/README.md \
136+
wolfcrypt/src/port/nuvoton/nuvoton_hw.h \
137+
wolfcrypt/src/port/nuvoton/nuvoton_hw.c \
138+
wolfcrypt/src/port/nuvoton/nuvoton_cryptocb.c \
139+
wolfcrypt/src/port/nuvoton/nuvoton_cb_rng.c \
140+
wolfcrypt/src/port/nuvoton/nuvoton_cb_hash.c \
141+
wolfcrypt/src/port/nuvoton/nuvoton_cb_cipher.c \
142+
wolfcrypt/src/port/nuvoton/nuvoton_cb_pk.c \
143+
wolfcrypt/src/port/nuvoton/nuvoton_key.c \
144+
wolfcrypt/src/port/nuvoton/README.md \
136145
wolfcrypt/src/port/st/stm32.c \
137146
wolfcrypt/src/port/st/README.md \
138147
wolfcrypt/src/port/st/STM32MP13.md \

0 commit comments

Comments
 (0)