diff --git a/rtl8735b/README.md b/rtl8735b/README.md index 6a04053d8..ad82feffb 100644 --- a/rtl8735b/README.md +++ b/rtl8735b/README.md @@ -11,7 +11,13 @@ It is built inside the RealTek AmebaPro2 FreeRTOS SDK (which provides the startu - The key is device-bound and deterministic: the same seed yields the same key (so GMAC verifies and AES round-trips), and a different seed yields a different key (GCM decrypt returns `AES_GCM_AUTH_E`). - The port tolerates unaligned caller buffers (it bounces them to satisfy the HAL's 32-byte alignment); the example includes an explicitly-unaligned GCM check. - HMAC-SHA256 under the HUK (`wc_HmacInit` with `devId = WC_HUK_DEVID`, then `wc_HmacSetKey`/`wc_HmacUpdate`/`wc_HmacFinal`): the MAC is deterministic for a given seed and differs for a wrong seed. -- HUK-bound ECDSA P-256 sign: a key is generated in software, its private scalar is wrapped under the HUK (ECB-encrypted with the HUK device), and signing then goes through the HUK device (unwrap + sign); the signature verifies against the software public key. +- HUK-bound ECDSA P-256 sign: a key is generated in software, its private scalar is wrapped under the HUK (AES-GCM-encrypted with the HUK device), and signing then goes through the HUK device (unwrap + sign); the signature verifies against the software public key. +- HUK-wrapped key storage: `huk_blob_create()` and `huk_blob_load()` in `main.c` show how to wrap a P-256 key, keep the seed, wrapped scalar and public key (plus the IV and tag for GCM) as a stored blob, and sign with a key set up from that blob alone. The blob only unwraps on the device that wrapped it. The example runs both wrap formats: + - AES-GCM (recommended): a damaged tag or wrapped scalar fails with `AES_GCM_AUTH_E`. + - AES-ECB: the same blob layout as the STM32 DHUK `wc_ecc_import_wrapped_private()`, for wrap code shared between the two parts. The port selects it when the key's `iv`, `ivSz`, `tag` and `tagSz` are all NULL/0. ECB has no integrity check: a damaged blob signs with a wrong scalar and the signature does not verify. Signing with an ECB blob needs wolfSSL PR #12056 or later. + + Neither format covers the stored public key, so an application that relies on it needs its own integrity check; the example's verify of a fresh signature against the stored public key is one. +- HUK-wrapped AES key in a key-storage slot: an application AES key, stored wrapped under the HUK, is loaded with `wc_KeyStore_ImportWrapped()` into a hardware key-storage slot and then used through an `Aes` made with `wc_AesInit_Id()`, so GCM/ECB run from the slot and the key is in RAM only during the import. Both wrap formats are shown: RFC 3394 (`WC_KEYWRAP_FORMAT_AESKW`, a damaged blob fails with `BAD_KEYWRAP_IV_E`) and raw ECB (`WC_KEYWRAP_FORMAT_VENDOR`, the STM32 DHUK style, no integrity check). Needs wolfSSL PR #12056 or later; with an older wolfSSL the test is not built. - HW ECDSA P-256 verify offload: the same signature is then verified through the HW engine (the public point is imported into a `WC_HUK_DEVID` key so `wc_ecc_verify_hash` dispatches to `hal_ecdsa`), and a tampered digest is shown to be rejected. No HUK context is needed to verify -- any P-256 public key. - **Plaintext-key AES (second device):** with `WOLFSSL_RTL8735B_AES` also enabled (see `user_settings.h`), the example registers a second crypto-callback device, `wc_Rtl8735b_AesRegister(WC_RTL8735B_AES_DEVID)`, that runs a caller-supplied AES key directly on the HW engine with no HUK binding. It reproduces a published AES-GCM test vector (proving the key is used verbatim), then encrypts the same 32 key bytes through both devices to show the plaintext-key ciphertext differs from the HUK device-bound ciphertext -- the two devices coexist and are selected per `Aes` by `devId`. @@ -78,6 +84,43 @@ Remove the J27 jumper and reset to boot. The console (UART, 115200) prints: [PASS] verify with software public key [PASS] HW ECDSA verify (good sig) [PASS] HW ECDSA verify (tampered -> reject) +== HUK-wrapped ECDSA key: wrap, store, reload, sign == +[PASS] InitRng +-- GCM-wrapped blob -- +[PASS] create wrapped key blob +[PASS] sign with reloaded blob +[PASS] verify with stored public key +[PASS] tampered blob tag -> AES_GCM_AUTH_E +-- ECB-wrapped blob -- +[PASS] create wrapped key blob +[PASS] sign with reloaded blob +[PASS] verify with stored public key +[PASS] tampered ECB blob signs, signature does not verify +== HUK-wrapped AES key in a key-storage slot == +[PASS] make key reference +[PASS] software AES reference +-- ECB-wrapped AES-256 key -- +[PASS] wrap key under the HUK +[PASS] wc_KeyStore_ImportWrapped into slot +[PASS] wc_KeyStore_GetInfo: AES-256 +[PASS] import into an occupied slot -> BAD_STATE_E +[PASS] AES-ECB and AES-GCM from the slot match the software key +[PASS] wc_KeyStore_Delete +[PASS] AES from a deleted slot fails +[PASS] damaged ECB blob imports +[PASS] ... but the key is wrong +-- RFC 3394-wrapped AES-256 key -- +[PASS] wrap key under the HUK +[PASS] wc_KeyStore_ImportWrapped into slot +[PASS] wc_KeyStore_GetInfo: AES-256 +[PASS] import into an occupied slot -> BAD_STATE_E +[PASS] AES-ECB and AES-GCM from the slot match the software key +[PASS] wc_KeyStore_Delete +[PASS] AES from a deleted slot fails +[PASS] damaged RFC 3394 blob -> BAD_KEYWRAP_IV_E +-- re-import a different key, and AES-128 -- +[PASS] slot reused for a second key +[PASS] AES-128 key from a slot matches the software key == AES-GCM with a plaintext key (devId=WC_RTL8735B_AES_DEVID) == [PASS] AesInit(devId=WC_RTL8735B_AES_DEVID) [PASS] AesGcmSetKey(plaintext 128-bit key) @@ -94,7 +137,7 @@ Remove the J27 jumper and reset to boot. The console (UART, 115200) prints: [PASS] ciphertext matches FIPS-197 vector [PASS] AesEcb round-trip == AES-GCM plaintext key, UNALIGNED buffers (port bounces) == ... round-trip PASS -== AES-GCM plaintext key, non-12-byte IV must hard-fail == ... 16-byte IV rejected PASS +== AES-GCM plaintext key, non-12-byte IV uses software GCM == ... matches software GCM PASS === done === ``` diff --git a/rtl8735b/main.c b/rtl8735b/main.c index 395cdd19e..4c53a20ae 100644 --- a/rtl8735b/main.c +++ b/rtl8735b/main.c @@ -11,7 +11,7 @@ * device (WC_RTL8735B_AES_DEVID) that runs a caller-supplied plaintext AES key * directly on the HW engine (no HUK binding), selected per Aes by devId, and * exercises it with AES-GCM (published KAT + coexistence), an AES-ECB FIPS-197 - * KAT, the unaligned-buffer bounce path, and non-12-byte-IV rejection. + * KAT, the unaligned-buffer bounce path, and software GCM for a non-12-byte IV. * * Build: configure with -DEXAMPLE=wolfcrypt_huk (see wolfcrypt_huk.cmake). */ @@ -32,6 +32,9 @@ #include #include #include +#ifdef WC_RTL8735B_HAVE_KEYSTORE + #include +#endif /* ForceZero (used to scrub key material below). Inline the misc helpers the same * way wolfCrypt's own test.c/benchmark.c do; misc.c provides ForceZero only as an @@ -419,7 +422,7 @@ static void huk_hmac_test(void) } /* HUK-bound ECDSA P-256 sign. A key is generated in software, its private - * scalar wrapped under the HUK (ECB-encrypted with the HUK device using the + * scalar wrapped under the HUK (AES-GCM-encrypted with the HUK device using the * seed), then signing goes through the HUK device (which unwraps under the HUK * and signs). The signature is checked against the software public key. To * exercise the HW ECDSA engine instead of the software-after-unwrap path, set @@ -570,6 +573,440 @@ static void huk_ecdsa_test(void) ForceZero(seed, sizeof(seed)); } +/* A HUK-wrapped P-256 key in the form an application stores it, for example in + * flash. The seed is not secret, and only the device that wrapped the scalar can + * unwrap it. The scalar is wrapped with AES-GCM (authenticated, recommended) or + * AES-ECB (the same blob layout as the STM32 DHUK, for code shared between the + * two parts). An ECB blob has no IV or tag. */ +#define HUK_WRAP_GCM 0 +#define HUK_WRAP_ECB 1 + +typedef struct huk_key_blob { + byte format; /* HUK_WRAP_GCM or HUK_WRAP_ECB */ + byte seed[32]; + byte iv[12]; + byte tag[16]; + byte wrapped[32]; + byte qx[32]; + byte qy[32]; +} huk_key_blob; + +/* Wrap a 32-byte scalar under the HUK with the blob's seed (and IV for GCM). */ +static int huk_blob_wrap(huk_key_blob* blob, const byte* d) +{ + Aes aes; + int ret; + + if (blob->format != HUK_WRAP_GCM && blob->format != HUK_WRAP_ECB) { + return BAD_FUNC_ARG; + } + ret = wc_AesInit(&aes, NULL, WC_HUK_DEVID); + if (ret != 0) { + return ret; + } + if (blob->format == HUK_WRAP_GCM) { + ret = wc_AesGcmSetKey(&aes, blob->seed, sizeof(blob->seed)); + if (ret == 0) { + ret = wc_AesGcmEncrypt(&aes, blob->wrapped, d, + sizeof(blob->wrapped), blob->iv, + sizeof(blob->iv), blob->tag, + sizeof(blob->tag), NULL, 0); + } + } + else { + ret = wc_AesSetKey(&aes, blob->seed, sizeof(blob->seed), NULL, + AES_ENCRYPTION); + if (ret == 0) { + ret = wc_AesEcbEncrypt(&aes, blob->wrapped, d, + sizeof(blob->wrapped)); + } + } + wc_AesFree(&aes); + return ret; +} + +/* Generate a P-256 key and wrap its scalar under the HUK with a fresh seed (and + * a fresh IV for GCM). */ +static int huk_blob_create(WC_RNG* rng, byte format, huk_key_blob* blob) +{ + ecc_key key; + byte d[32] __attribute__((aligned(32))); + word32 dLen = sizeof(d); + word32 qxLen = sizeof(blob->qx); + word32 qyLen = sizeof(blob->qy); + int ret; + + memset(blob, 0, sizeof(*blob)); + blob->format = format; + ret = wc_ecc_init(&key); + if (ret != 0) { + return ret; + } + ret = wc_ecc_make_key(rng, sizeof(d), &key); + if (ret == 0) { + ret = wc_ecc_export_private_only(&key, d, &dLen); + } + if (ret == 0 && dLen != sizeof(d)) { + ret = BUFFER_E; + } + if (ret == 0) { + ret = wc_ecc_export_public_raw(&key, blob->qx, &qxLen, blob->qy, &qyLen); + } + if (ret == 0) { + ret = wc_RNG_GenerateBlock(rng, blob->seed, sizeof(blob->seed)); + } + if (ret == 0 && format == HUK_WRAP_GCM) { + ret = wc_RNG_GenerateBlock(rng, blob->iv, sizeof(blob->iv)); + } + if (ret == 0) { + ret = huk_blob_wrap(blob, d); + } + wc_ecc_free(&key); + ForceZero(d, sizeof(d)); + return ret; +} + +/* Set up a signing key from a stored blob. The key borrows hk and the blob, so + * both must stay valid until wc_ecc_free. For an ECB blob the iv and tag fields + * stay NULL/0, which is how the port tells the two formats apart, so an unknown + * format byte is rejected rather than falling back to the unauthenticated ECB + * unwrap. */ +static int huk_blob_load(const huk_key_blob* blob, wc_Rtl8735b_EccKey* hk, + ecc_key* key) +{ + int ret; + + if (blob->format != HUK_WRAP_GCM && blob->format != HUK_WRAP_ECB) { + return BAD_FUNC_ARG; + } + memset(hk, 0, sizeof(*hk)); + hk->seed = blob->seed; hk->seedSz = sizeof(blob->seed); + hk->wrapped = blob->wrapped; hk->wrappedLen = sizeof(blob->wrapped); + hk->plainLen = sizeof(blob->wrapped); + if (blob->format == HUK_WRAP_GCM) { + hk->iv = blob->iv; hk->ivSz = sizeof(blob->iv); + hk->tag = blob->tag; hk->tagSz = sizeof(blob->tag); + } + + ret = wc_ecc_init_ex(key, NULL, WC_HUK_DEVID); + if (ret == 0) { + ret = wc_ecc_set_curve(key, sizeof(blob->wrapped), ECC_SECP256R1); + } + if (ret == 0) { + key->devCtx = hk; + } + return ret; +} + +/* Sign with a blob, using a key object set up only from what was stored. */ +static int huk_blob_sign(const huk_key_blob* blob, WC_RNG* rng, const byte* hash, + word32 hashSz, byte* sig, word32* sigLen) +{ + wc_Rtl8735b_EccKey hk; + ecc_key key; + int ret; + + memset(&key, 0, sizeof(key)); + ret = huk_blob_load(blob, &hk, &key); + if (ret == 0) { + ret = wc_ecc_sign_hash(hash, hashSz, sig, sigLen, rng, &key); + } + wc_ecc_free(&key); + return ret; +} + +/* Verify a signature with the blob's stored public key. Returns 1 if valid. */ +static int huk_blob_verify(const huk_key_blob* blob, const byte* hash, + word32 hashSz, const byte* sig, word32 sigLen) +{ + ecc_key pub; + int verified = 0; + int ret; + + ret = wc_ecc_init(&pub); + if (ret == 0) { + ret = wc_ecc_import_unsigned(&pub, blob->qx, blob->qy, NULL, + ECC_SECP256R1); + if (ret == 0) { + ret = wc_ecc_verify_hash(sig, sigLen, hash, hashSz, &verified, + &pub); + } + wc_ecc_free(&pub); + } + return (ret == 0 && verified == 1); +} + +/* Wrap a key, reload it from the stored blob, and sign with it, for both wrap + * formats. A real application writes the blob to flash and reloads it after a + * reset. */ +static void huk_ecdsa_reload_test(void) +{ + static huk_key_blob blob; + WC_RNG rng; + byte hash[32]; + byte sig[80]; + word32 sigLen; + int format; + int ret; + + dbg_printf("\r\n== HUK-wrapped ECDSA key: wrap, store, reload, sign ==\r\n"); + + memset(hash, 0x42, sizeof(hash)); + ret = wc_InitRng(&rng); + CHECK("InitRng", ret == 0); + if (ret != 0) { + return; + } + for (format = HUK_WRAP_GCM; format <= HUK_WRAP_ECB; format++) { + const char* name = (format == HUK_WRAP_GCM) ? "GCM" : "ECB"; + + dbg_printf("-- %s-wrapped blob --\r\n", name); + ret = huk_blob_create(&rng, (byte)format, &blob); + CHECK("create wrapped key blob", ret == 0); + if (ret != 0) { + continue; + } + sigLen = sizeof(sig); + ret = huk_blob_sign(&blob, &rng, hash, sizeof(hash), sig, &sigLen); + CHECK("sign with reloaded blob", ret == 0); + if (ret == 0) { + CHECK("verify with stored public key", + huk_blob_verify(&blob, hash, sizeof(hash), sig, sigLen)); + } + + /* A damaged GCM blob cannot produce a key: the tag does not match. A + * damaged ECB blob unwraps to a wrong scalar, so it signs, but the + * signature does not verify. Neither covers qx/qy. */ + sigLen = sizeof(sig); + if (format == HUK_WRAP_GCM) { + blob.tag[0] ^= 0x01; + ret = huk_blob_sign(&blob, &rng, hash, sizeof(hash), sig, &sigLen); + CHECK("tampered blob tag -> AES_GCM_AUTH_E", ret == AES_GCM_AUTH_E); + blob.tag[0] ^= 0x01; + blob.format ^= 0x02; + sigLen = sizeof(sig); + ret = huk_blob_sign(&blob, &rng, hash, sizeof(hash), sig, &sigLen); + CHECK("corrupted format byte -> BAD_FUNC_ARG", ret == BAD_FUNC_ARG); + } + else { + blob.wrapped[0] ^= 0x01; + ret = huk_blob_sign(&blob, &rng, hash, sizeof(hash), sig, &sigLen); + CHECK("tampered ECB blob signs, signature does not verify", + ret == 0 && + !huk_blob_verify(&blob, hash, sizeof(hash), sig, sigLen)); + } + } + wc_FreeRng(&rng); +} + +#ifdef WC_RTL8735B_HAVE_KEYSTORE +/* An application AES key, wrapped under the HUK and stored, loaded into a + * key-storage slot with wc_KeyStore_ImportWrapped() and then used from that slot + * through an Aes made with wc_AesInit_Id(). The key is in RAM only while it is + * imported. */ +#define HUK_KS_SLOT WC_RTL8735B_KS_SLOT_FIRST + +/* Wrap key under the HUK-derived key for seed: AES-ECB (the STM32 DHUK style) + * or RFC 3394 key wrap (authenticated). Returns the blob size, or < 0. */ +static int huk_key_wrap(int format, const byte* seed, word32 seedSz, + const byte* key, word32 keySz, byte* blob, + word32 blobSz) +{ + Aes aes; + int ret; + + if (seedSz != 32) { + return BAD_FUNC_ARG; + } + if (format != WC_KEYWRAP_FORMAT_AESKW && keySz > blobSz) { + return BUFFER_E; + } + ret = wc_AesInit(&aes, NULL, WC_HUK_DEVID); + if (ret != 0) { + return ret; + } + ret = wc_AesSetKey(&aes, seed, seedSz, NULL, AES_ENCRYPTION); + if (ret == 0) { + if (format == WC_KEYWRAP_FORMAT_AESKW) { + ret = wc_AesKeyWrap_ex(&aes, key, keySz, blob, blobSz, NULL); + } + else { + ret = wc_AesEcbEncrypt(&aes, blob, key, keySz); + if (ret == 0) { + ret = (int)keySz; + } + } + } + wc_AesFree(&aes); + return ret; +} + +/* AES-ECB of one block and AES-GCM of pt, with either the key in a slot + * (ref != NULL) or a plain software key. */ +static int huk_slot_aes(const byte* ref, word32 refSz, const byte* key, + word32 keySz, const byte* pt, byte* ecbOut, + byte* gcmOut, byte* tag) +{ + /* Fixed so the slot and software runs produce comparable output. An + * application must use a unique IV for every message under a key. */ + static const byte iv[12] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; + Aes aes; + int ret; + + if (ref != NULL) { + ret = wc_AesInit_Id(&aes, (unsigned char*)ref, (int)refSz, NULL, + WC_HUK_DEVID); + } + else { + ret = wc_AesInit(&aes, NULL, INVALID_DEVID); + if (ret == 0) { + ret = wc_AesSetKey(&aes, key, keySz, NULL, AES_ENCRYPTION); + } + if (ret == 0) { + ret = wc_AesGcmSetKey(&aes, key, keySz); + } + } + if (ret == 0) { + ret = wc_AesEcbEncrypt(&aes, ecbOut, pt, 16); + } + if (ret == 0) { + ret = wc_AesGcmEncrypt(&aes, gcmOut, pt, 32, iv, sizeof(iv), tag, 16, + NULL, 0); + } + wc_AesFree(&aes); + return ret; +} + +static int huk_slot_import(const byte* ref, word32 refSz, const byte* seed, + word32 seedSz, int format, const byte* blob, + word32 blobSz) +{ + return wc_KeyStore_ImportWrapped(WC_HUK_DEVID, ref, refSz, + WC_KEYSTORE_KEY_AES, seed, seedSz, + (word32)format, blob, blobSz, 0, NULL); +} + +static void huk_slot_key_test(void) +{ + static const int formats[2] = { WC_KEYWRAP_FORMAT_VENDOR, + WC_KEYWRAP_FORMAT_AESKW }; + byte key[32], key2[32], seed[32], pt[32]; + byte blob[40], ref[WC_RTL8735B_KEYREF_SZ]; + byte ecb0[16], ecb1[16], gcm0[32], gcm1[32], tag0[16], tag1[16]; + word32 refSz = sizeof(ref); + word32 keyType = 0, keyBits = 0; + int i, f, ret, blobSz; + + dbg_printf("\r\n== HUK-wrapped AES key in a key-storage slot ==\r\n"); + for (i = 0; i < 32; i++) { + key[i] = (byte)(0x10 + i); + key2[i] = (byte)(0x90 + i); + seed[i] = (byte)(0x5A ^ i); + pt[i] = (byte)(0xC0 + i); + } + ret = wc_Rtl8735b_MakeKeyRef(HUK_KS_SLOT, ref, &refSz); + CHECK("make key reference", ret == 0); + if (ret != 0) { + return; + } + ret = huk_slot_aes(NULL, 0, key, 32, pt, ecb0, gcm0, tag0); + CHECK("software AES reference", ret == 0); + if (ret != 0) { + return; + } + + for (f = 0; f < 2; f++) { + const char* name = (formats[f] == WC_KEYWRAP_FORMAT_AESKW) ? + "RFC 3394" : "ECB"; + + dbg_printf("-- %s-wrapped AES-256 key --\r\n", name); + blobSz = huk_key_wrap(formats[f], seed, sizeof(seed), key, 32, blob, + sizeof(blob)); + CHECK("wrap key under the HUK", blobSz > 0); + if (blobSz <= 0) { + continue; + } + ret = huk_slot_import(ref, refSz, seed, sizeof(seed), formats[f], + blob, (word32)blobSz); + CHECK("wc_KeyStore_ImportWrapped into slot", ret == 0); + if (ret != 0) { + continue; + } + ret = wc_KeyStore_GetInfo(WC_HUK_DEVID, ref, refSz, &keyType, &keyBits, + NULL, NULL); + CHECK("wc_KeyStore_GetInfo: AES-256", + ret == 0 && keyType == WC_KEYSTORE_KEY_AES && keyBits == 256); + ret = huk_slot_import(ref, refSz, seed, sizeof(seed), formats[f], + blob, (word32)blobSz); + CHECK("import into an occupied slot -> BAD_STATE_E", + ret == BAD_STATE_E); + memset(ecb1, 0, sizeof(ecb1)); + ret = huk_slot_aes(ref, refSz, NULL, 0, pt, ecb1, gcm1, tag1); + CHECK("AES-ECB and AES-GCM from the slot match the software key", + ret == 0 && memcmp(ecb0, ecb1, 16) == 0 && + memcmp(gcm0, gcm1, 32) == 0 && memcmp(tag0, tag1, 16) == 0); + ret = wc_KeyStore_Delete(WC_HUK_DEVID, ref, refSz, NULL); + CHECK("wc_KeyStore_Delete", ret == 0); + ret = huk_slot_aes(ref, refSz, NULL, 0, pt, ecb1, gcm1, tag1); + CHECK("AES from a deleted slot fails", ret != 0); + + /* RFC 3394 rejects a damaged blob; ECB imports a wrong key. */ + blob[blobSz - 1] ^= 0x01; + ret = huk_slot_import(ref, refSz, seed, sizeof(seed), formats[f], + blob, (word32)blobSz); + if (formats[f] == WC_KEYWRAP_FORMAT_AESKW) { + CHECK("damaged RFC 3394 blob -> BAD_KEYWRAP_IV_E", + ret == BAD_KEYWRAP_IV_E); + } + else { + CHECK("damaged ECB blob imports", ret == 0); + ret = huk_slot_aes(ref, refSz, NULL, 0, pt, ecb1, gcm1, tag1); + CHECK("... but the key is wrong", ret == 0 && + memcmp(ecb0, ecb1, 16) != 0); + (void)wc_KeyStore_Delete(WC_HUK_DEVID, ref, refSz, NULL); + } + } + + dbg_printf("-- re-import a different key, and AES-128 --\r\n"); + blobSz = huk_key_wrap(WC_KEYWRAP_FORMAT_AESKW, seed, sizeof(seed), key2, + 32, blob, sizeof(blob)); + ret = (blobSz > 0) ? 0 : blobSz; + if (ret == 0) { + ret = huk_slot_import(ref, refSz, seed, sizeof(seed), + WC_KEYWRAP_FORMAT_AESKW, blob, (word32)blobSz); + } + if (ret == 0) { + ret = huk_slot_aes(NULL, 0, key2, 32, pt, ecb0, gcm0, tag0); + } + if (ret == 0) { + ret = huk_slot_aes(ref, refSz, NULL, 0, pt, ecb1, gcm1, tag1); + } + CHECK("slot reused for a second key", ret == 0 && + memcmp(ecb0, ecb1, 16) == 0 && memcmp(tag0, tag1, 16) == 0); + (void)wc_KeyStore_Delete(WC_HUK_DEVID, ref, refSz, NULL); + + blobSz = huk_key_wrap(WC_KEYWRAP_FORMAT_AESKW, seed, sizeof(seed), key, + 16, blob, sizeof(blob)); + ret = (blobSz > 0) ? 0 : blobSz; + if (ret == 0) { + ret = huk_slot_import(ref, refSz, seed, sizeof(seed), + WC_KEYWRAP_FORMAT_AESKW, blob, (word32)blobSz); + } + if (ret == 0) { + ret = huk_slot_aes(NULL, 0, key, 16, pt, ecb0, gcm0, tag0); + } + if (ret == 0) { + ret = huk_slot_aes(ref, refSz, NULL, 0, pt, ecb1, gcm1, tag1); + } + CHECK("AES-128 key from a slot matches the software key", ret == 0 && + memcmp(ecb0, ecb1, 16) == 0 && memcmp(tag0, tag1, 16) == 0); + (void)wc_KeyStore_Delete(WC_HUK_DEVID, ref, refSz, NULL); + + ForceZero(key, sizeof(key)); + ForceZero(key2, sizeof(key2)); +} +#endif /* WC_RTL8735B_HAVE_KEYSTORE */ + #ifdef WOLFSSL_RTL8735B_AES /* AES-GCM with a plaintext key through the second device (WC_RTL8735B_AES_DEVID). * Unlike the HUK device, the key bytes are the actual AES key, so the output @@ -785,9 +1222,9 @@ static void plain_gcm_unaligned_test(void) ForceZero(key, 16); /* scrub key material before return */ } -/* A non-12-byte GCM IV must hard-fail on the plaintext device too, NOT silently - * fall back to software GCM (regression for the IV hard-error contract). */ -static void plain_gcm_badiv_test(void) +/* The HW path only takes a 12-byte IV, so the plaintext device declines any other + * length and software GCM serves it with the same literal key. */ +static void plain_gcm_long_iv_test(void) { Aes aes; byte key[16] __attribute__((aligned(32))); @@ -795,12 +1232,14 @@ static void plain_gcm_badiv_test(void) byte pt[16] __attribute__((aligned(32))); byte ct[16] __attribute__((aligned(32))); byte tag[16] __attribute__((aligned(32))); + byte swCt[16]; + byte swTag[16]; int ret; memset(key, 0x5A, sizeof(key)); memset(iv, 0x11, sizeof(iv)); memset(pt, 0x33, sizeof(pt)); - dbg_printf("\r\n== AES-GCM plaintext key, non-12-byte IV must hard-fail " + dbg_printf("\r\n== AES-GCM plaintext key, non-12-byte IV uses software GCM " "==\r\n"); ret = wc_AesInit(&aes, NULL, WC_RTL8735B_AES_DEVID); CHECK("AesInit(devId=WC_RTL8735B_AES_DEVID)", ret == 0); @@ -815,8 +1254,22 @@ static void plain_gcm_badiv_test(void) } ret = wc_AesGcmEncrypt(&aes, ct, pt, sizeof(pt), iv, sizeof(iv), tag, sizeof(tag), NULL, 0); - CHECK("16-byte IV rejected (no silent SW fallback)", ret != 0); + CHECK("AesGcmEncrypt (16-byte IV)", ret == 0); wc_AesFree(&aes); + if (ret == 0) { + ret = wc_AesInit(&aes, NULL, INVALID_DEVID); + if (ret == 0) { + ret = wc_AesGcmSetKey(&aes, key, sizeof(key)); + if (ret == 0) { + ret = wc_AesGcmEncrypt(&aes, swCt, pt, sizeof(pt), iv, sizeof(iv), + swTag, sizeof(swTag), NULL, 0); + } + wc_AesFree(&aes); + } + CHECK("16-byte IV matches software GCM with the same key", + ret == 0 && memcmp(ct, swCt, sizeof(ct)) == 0 && + memcmp(tag, swTag, sizeof(tag)) == 0); + } ForceZero(key, sizeof(key)); /* scrub key material before return */ } #endif /* WOLFSSL_RTL8735B_AES */ @@ -858,12 +1311,16 @@ static void wolf_huk_thread(void* param) huk_gcm_badiv_test(); huk_hmac_test(); huk_ecdsa_test(); + huk_ecdsa_reload_test(); +#ifdef WC_RTL8735B_HAVE_KEYSTORE + huk_slot_key_test(); +#endif #ifdef WOLFSSL_RTL8735B_AES if (aesRet == 0) { plain_gcm_test(); plain_ecb_test(); plain_gcm_unaligned_test(); - plain_gcm_badiv_test(); + plain_gcm_long_iv_test(); wc_Rtl8735b_AesUnRegister(WC_RTL8735B_AES_DEVID); } #endif diff --git a/rtl8735b/tls/README.md b/rtl8735b/tls/README.md index 410bb9133..0e9910096 100644 --- a/rtl8735b/tls/README.md +++ b/rtl8735b/tls/README.md @@ -6,16 +6,38 @@ the RTL8735B HW ECDSA engine via the wolfCrypt HUK crypto-callback port. The ECDHE key agreement and record-layer AES-GCM run in software. Proves HUK/HW-accelerated server authentication in a real TLS handshake. +A second handshake runs mutual TLS where the client's private key is stored only as a HUK-wrapped blob: the client's CertificateVerify signature is made from the blob, and the plaintext key is never loaded into wolfSSL. + Validated on RTL8735B silicon: ``` == TLS 1.2 ECDHE-ECDSA (server auth on HUK) == -[PASS] TLS handshake completed +[PASS] CTX_new (client+server) +[PASS] server cert + key +[PASS] client load_verify (CA) +cipher: ECDHE-ECDSA-AES128-GCM-SHA256 +[PASS] handshake + record (server signs on HW) + +== TLS 1.2 mutual auth, client key HUK-wrapped == +[PASS] CTX_new (client+server) +[PASS] server cert + key, client cert required +[PASS] client cert + public key only +-- GCM-wrapped client key -- +[PASS] wrap the client private key under the HUK cipher: ECDHE-ECDSA-AES128-GCM-SHA256 -[PASS] client write -[PASS] server read == client msg +[PASS] mutual handshake + record (client signs from the HUK blob) +client err -180 +[PASS] tampered HUK blob fails the handshake +-- ECB-wrapped client key -- +[PASS] wrap the client private key under the HUK +cipher: ECDHE-ECDSA-AES128-GCM-SHA256 +[PASS] mutual handshake + record (client signs from the HUK blob) +server err -229 +[PASS] tampered HUK blob fails the handshake ``` +The ECB-wrapped client key needs the ECB support in wolfSSL PR #12056; against older wolfSSL the ECB handshake fails with `BAD_FUNC_ARG` (-170). + ## Key design point: use a PK sign callback, NOT a whole-CTX devId Do NOT `wolfSSL_CTX_SetDevId(ctx, WC_HUK_DEVID)` for a TLS CTX. The HUK device @@ -31,6 +53,16 @@ sign callback (`wolfSSL_CTX_SetEccSignCb`). Inside it, sign with a temporary HW engine while the PRF/HMAC, record AES, and ECDHE stay in software. See `huk_ecc_sign_cb` in `main.c`. Requires `HAVE_PK_CALLBACKS`. +## Mutual TLS with a HUK-wrapped client key + +`huk_mtls_test` in `main.c` shows a device-bound client key: + +1. Provisioning (`huk_wrap_client_key`): the client's P-256 scalar is wrapped under the HUK with a fresh seed, using AES-GCM (recommended, with a fresh IV) or AES-ECB (the STM32 DHUK blob layout, for code shared between the two parts), and the plaintext is scrubbed. The stored blob is the format, seed and wrapped scalar, plus the IV and tag for GCM; only this device can unwrap it. In the example the scalar comes from `certs_test.h` as a stand-in for a key whose certificate was issued at manufacture. +2. Client setup: `wolfSSL_CTX_SetEccSignCb(cctx, huk_wrapped_sign_cb)` first, then `wolfSSL_CTX_use_certificate_buffer` with the client certificate and `wolfSSL_CTX_use_PrivateKey_buffer` with the client's **public** key. With a sign callback set, wolfSSL accepts a public key in place of the private key (`WOLF_PRIVATE_KEY_ID`, enabled automatically by `HAVE_PK_CALLBACKS`) and sends every signature to the callback. +3. Per connection: `wolfSSL_SetEccSignCtx(ssl, &blob)`. The callback builds a `wc_Rtl8735b_EccKey` from the blob, attaches it to a `WC_HUK_DEVID` `ecc_key` through `devCtx`, and calls `wc_ecc_sign_hash`, which unwraps the scalar under the HUK and signs. + +The example runs the handshake with both formats, and with a tampered blob of each. A tampered or wrong-device GCM blob fails its unwrap on the client, so nothing is signed. ECB has no integrity check: a tampered blob unwraps to a wrong scalar and signs, and the server rejects the CertificateVerify, so the handshake still fails but only at the peer. An ECB blob leaves the `iv` and `tag` fields of `wc_Rtl8735b_EccKey` NULL/0, which is how the port selects the ECB unwrap, so the callback rejects an unknown format byte rather than falling back to ECB. The unwrapped scalar is in RAM only for the duration of each sign. + ## Build Install this directory at `/component/example/wolfcrypt_huk_tls/` and select @@ -52,6 +84,4 @@ example. skipped) -- do not use that in production without a real time source. - `WOLFSSL_USER_IO` + `WOLFSSL_NO_SOCK`: the transport is two in-memory buffers via custom `SetIORecv`/`SetIOSend` callbacks; no sockets are used. -- Model shown is the general HW-offload key (a plain P-256 server key signed on - the engine). A device-bound HUK-wrapped server key is a follow-on (the sign - callback would use a `wc_Rtl8735b_EccKey` via the key's `devCtx`). +- The mutual-auth handshake also uses `cliecc_cert_der_256`, `ecc_clikey_der_256` (wrapped at provisioning only) and `ecc_clikeypub_der_256`. The server trusts the self-signed client certificate directly. diff --git a/rtl8735b/tls/main.c b/rtl8735b/tls/main.c index 31b19493d..b37ab0f6f 100644 --- a/rtl8735b/tls/main.c +++ b/rtl8735b/tls/main.c @@ -7,10 +7,13 @@ * scalar-mult path; see the port README). Proves HUK-accelerated server auth in * a real TLS handshake. * - * Two server-key models (RTL_TLS_KEY_MODEL): - * 1 = general HW offload: a plain P-256 server key loaded from certs_test.h; - * with devId set, its handshake ECDSA sign dispatches to the HW engine. - * 2 = device-bound HUK-wrapped key (follow-on; see README). + * Two handshakes run: + * 1. Server auth: a plain P-256 server key from certs_test.h; its handshake + * ECDSA sign dispatches to the HW engine. + * 2. Mutual auth with a device-bound client key: the client's P-256 scalar is + * wrapped under the HUK (AES-GCM) and only the wrapped blob is kept. The + * client loads its certificate and public key, and its CertificateVerify + * signature comes from the blob. A tampered blob must fail the handshake. * * Build: configure with -DEXAMPLE=wolfcrypt_huk_tls (see wolfcrypt_huk_tls.cmake). */ @@ -26,10 +29,16 @@ #include #include #include +#include +#include #include -#ifndef RTL_TLS_KEY_MODEL - #define RTL_TLS_KEY_MODEL 1 +/* ForceZero, inlined the same way wolfCrypt's test.c does. */ +#ifdef NO_INLINE + #include +#else + #define WOLFSSL_MISC_INCLUDED + #include #endif #define STACKSIZE (16 * 1024) @@ -156,65 +165,147 @@ static int huk_ecc_sign_cb(WOLFSSL* ssl, const byte* in, word32 inSz, return ret; } -static void huk_tls_test(void) +/* A HUK-wrapped P-256 private key as an application stores it (for example in + * flash). The seed is not secret; only the device that wrapped the scalar can + * unwrap it. The scalar is wrapped with AES-GCM (authenticated, recommended) or + * AES-ECB (the STM32 DHUK blob layout); an ECB blob has no IV or tag. */ +#define HUK_WRAP_GCM 0 +#define HUK_WRAP_ECB 1 + +typedef struct huk_key_blob { + byte format; /* HUK_WRAP_GCM or HUK_WRAP_ECB */ + byte seed[32]; + byte iv[12]; + byte tag[16]; + byte wrapped[32]; +} huk_key_blob; + +/* Provisioning step: wrap the client's private scalar under the HUK with a + * fresh seed (and IV for GCM). The plaintext key from certs_test.h stands in + * for a key whose certificate was issued at manufacture; after this only the + * blob is kept. */ +static int huk_wrap_client_key(WC_RNG* rng, byte format, huk_key_blob* blob) { - WOLFSSL_CTX* cctx = NULL; - WOLFSSL_CTX* sctx = NULL; - WOLFSSL* cssl = NULL; - WOLFSSL* sssl = NULL; - const char msg[] = "hello from HUK TLS client"; - char rx[64]; - int cdone = 0, sdone = 0; - int i, ret; - - dbg_printf("\r\n== TLS 1.2 ECDHE-ECDSA (server auth on HUK) ==\r\n"); + ecc_key key; + Aes aes; + byte d[32] __attribute__((aligned(32))); + word32 dLen = sizeof(d); + word32 idx = 0; + int ret; - sctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()); - cctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()); - CHECK("CTX_new (client+server)", sctx != NULL && cctx != NULL); - if (sctx == NULL || cctx == NULL) { - goto cleanup; + if (format != HUK_WRAP_GCM && format != HUK_WRAP_ECB) { + return BAD_FUNC_ARG; } + memset(blob, 0, sizeof(*blob)); + blob->format = format; + ret = wc_ecc_init(&key); + if (ret != 0) { + return ret; + } + ret = wc_EccPrivateKeyDecode(ecc_clikey_der_256, &idx, &key, + sizeof_ecc_clikey_der_256); + if (ret == 0) { + ret = wc_ecc_export_private_only(&key, d, &dLen); + } + if (ret == 0 && dLen != sizeof(d)) { + ret = BUFFER_E; + } + wc_ecc_free(&key); + if (ret == 0) { + ret = wc_RNG_GenerateBlock(rng, blob->seed, sizeof(blob->seed)); + } + if (ret == 0 && format == HUK_WRAP_GCM) { + ret = wc_RNG_GenerateBlock(rng, blob->iv, sizeof(blob->iv)); + } + if (ret == 0) { + ret = wc_AesInit(&aes, NULL, WC_HUK_DEVID); + if (ret == 0) { + if (format == HUK_WRAP_GCM) { + ret = wc_AesGcmSetKey(&aes, blob->seed, sizeof(blob->seed)); + if (ret == 0) { + ret = wc_AesGcmEncrypt(&aes, blob->wrapped, d, sizeof(d), + blob->iv, sizeof(blob->iv), + blob->tag, sizeof(blob->tag), + NULL, 0); + } + } + else { + ret = wc_AesSetKey(&aes, blob->seed, sizeof(blob->seed), NULL, + AES_ENCRYPTION); + if (ret == 0) { + ret = wc_AesEcbEncrypt(&aes, blob->wrapped, d, sizeof(d)); + } + } + wc_AesFree(&aes); + } + } + ForceZero(d, sizeof(d)); + return ret; +} - /* RNG for the sign callback's nonce. */ - ret = wc_InitRng(&g_signRng); - CHECK("sign-callback RNG init", ret == 0); - - /* Route ONLY the server's cert ECDSA sign to the HUK HW engine (PK callback); - * TLS's own HMAC/PRF, record AES and ECDHE stay in software. */ - wolfSSL_CTX_SetEccSignCb(sctx, huk_ecc_sign_cb); - - /* Restrict to ECDHE-ECDSA + AES-GCM so server auth uses ECDSA (HUK). */ - wolfSSL_CTX_set_cipher_list(sctx, "ECDHE-ECDSA-AES128-GCM-SHA256"); - wolfSSL_CTX_set_cipher_list(cctx, "ECDHE-ECDSA-AES128-GCM-SHA256"); - - /* Server: P-256 cert + key (model 1: general HW offload). */ - ret = wolfSSL_CTX_use_certificate_buffer(sctx, serv_ecc_der_256, - sizeof_serv_ecc_der_256, WOLFSSL_FILETYPE_ASN1); - CHECK("server use_certificate", ret == WOLFSSL_SUCCESS); - ret = wolfSSL_CTX_use_PrivateKey_buffer(sctx, ecc_key_der_256, - sizeof_ecc_key_der_256, WOLFSSL_FILETYPE_ASN1); - CHECK("server use_PrivateKey", ret == WOLFSSL_SUCCESS); - - /* Client: trust the CA that signed the server cert. */ - ret = wolfSSL_CTX_load_verify_buffer(cctx, ca_ecc_cert_der_256, - sizeof_ca_ecc_cert_der_256, WOLFSSL_FILETYPE_ASN1); - if (ret != WOLFSSL_SUCCESS) { - dbg_printf("load_verify ret=%d\r\n", ret); +/* Client ECC sign PK callback: sign the CertificateVerify hash with the + * HUK-wrapped key passed as the callback context (wolfSSL_SetEccSignCtx). + * keyDer is the public key the client loaded and is not needed here. For an + * ECB blob the iv and tag fields stay NULL/0, which is how the port tells the + * two formats apart, so an unknown format byte is rejected rather than + * falling back to the unauthenticated ECB unwrap. */ +static int huk_wrapped_sign_cb(WOLFSSL* ssl, const byte* in, word32 inSz, + byte* out, word32* outSz, const byte* keyDer, word32 keySz, void* ctx) +{ + const huk_key_blob* blob = (const huk_key_blob*)ctx; + wc_Rtl8735b_EccKey hk; + ecc_key key; + int ret; + (void)ssl; (void)keyDer; (void)keySz; + + if (blob == NULL || + (blob->format != HUK_WRAP_GCM && blob->format != HUK_WRAP_ECB)) { + return BAD_FUNC_ARG; + } + memset(&hk, 0, sizeof(hk)); + hk.seed = blob->seed; hk.seedSz = sizeof(blob->seed); + hk.wrapped = blob->wrapped; hk.wrappedLen = sizeof(blob->wrapped); + hk.plainLen = sizeof(blob->wrapped); + if (blob->format == HUK_WRAP_GCM) { + hk.iv = blob->iv; hk.ivSz = sizeof(blob->iv); + hk.tag = blob->tag; hk.tagSz = sizeof(blob->tag); } - CHECK("client load_verify (CA)", ret == WOLFSSL_SUCCESS); - wolfSSL_SetIORecv(sctx, recv_server); - wolfSSL_SetIOSend(sctx, send_server); - wolfSSL_SetIORecv(cctx, recv_client); - wolfSSL_SetIOSend(cctx, send_client); + ret = wc_ecc_init_ex(&key, NULL, WC_HUK_DEVID); + if (ret == 0) { + ret = wc_ecc_set_curve(&key, sizeof(blob->wrapped), ECC_SECP256R1); + if (ret == 0) { + key.devCtx = &hk; + ret = wc_ecc_sign_hash(in, inSz, out, outSz, &g_signRng, &key); + } + wc_ecc_free(&key); + } + return ret; +} +/* Run one in-memory handshake and send one record client -> server. signCtx, + * when set, is the client's sign-callback context. Returns 0 when the + * handshake completed and the record arrived intact. */ +static int huk_tls_session(WOLFSSL_CTX* cctx, WOLFSSL_CTX* sctx, void* signCtx) +{ + WOLFSSL* cssl; + WOLFSSL* sssl; + const char msg[] = "hello from HUK TLS client"; + char rx[64]; + int cdone = 0, sdone = 0; + int i, ret = -1; + + client_buffer_sz = 0; + server_buffer_sz = 0; sssl = wolfSSL_new(sctx); cssl = wolfSSL_new(cctx); - CHECK("wolfSSL_new (client+server)", sssl != NULL && cssl != NULL); if (sssl == NULL || cssl == NULL) { + dbg_printf("wolfSSL_new failed\r\n"); goto cleanup; } + if (signCtx != NULL) { + wolfSSL_SetEccSignCtx(cssl, signCtx); + } /* Drive the handshake: interleave connect/accept until both complete. */ for (i = 0; i < 20 && (cdone == 0 || sdone == 0); i++) { @@ -241,25 +332,156 @@ static void huk_tls_test(void) } } } - CHECK("TLS handshake completed", cdone == 1 && sdone == 1); - + ret = -1; if (cdone == 1 && sdone == 1) { dbg_printf("cipher: %s\r\n", wolfSSL_get_cipher(sssl)); - /* Exchange one application record client -> server. */ - ret = wolfSSL_write(cssl, msg, (int)sizeof(msg)); - CHECK("client write", ret == (int)sizeof(msg)); memset(rx, 0, sizeof(rx)); - ret = wolfSSL_read(sssl, rx, sizeof(rx) - 1); - CHECK("server read == client msg", - ret == (int)sizeof(msg) && memcmp(rx, msg, sizeof(msg)) == 0); + if (wolfSSL_write(cssl, msg, (int)sizeof(msg)) == (int)sizeof(msg) && + wolfSSL_read(sssl, rx, sizeof(rx) - 1) == (int)sizeof(msg) && + memcmp(rx, msg, sizeof(msg)) == 0) { + ret = 0; + } } cleanup: if (cssl != NULL) wolfSSL_free(cssl); if (sssl != NULL) wolfSSL_free(sssl); + return ret; +} + +/* Server side shared by both handshakes: P-256 cert + key, ECDSA signed on the + * HW engine, ECDHE-ECDSA + AES-GCM only so server auth uses ECDSA. */ +static int huk_tls_server_ctx(WOLFSSL_CTX* sctx) +{ + wolfSSL_CTX_SetEccSignCb(sctx, huk_ecc_sign_cb); + wolfSSL_CTX_set_cipher_list(sctx, "ECDHE-ECDSA-AES128-GCM-SHA256"); + wolfSSL_SetIORecv(sctx, recv_server); + wolfSSL_SetIOSend(sctx, send_server); + if (wolfSSL_CTX_use_certificate_buffer(sctx, serv_ecc_der_256, + sizeof_serv_ecc_der_256, WOLFSSL_FILETYPE_ASN1) + != WOLFSSL_SUCCESS) { + return -1; + } + if (wolfSSL_CTX_use_PrivateKey_buffer(sctx, ecc_key_der_256, + sizeof_ecc_key_der_256, WOLFSSL_FILETYPE_ASN1) != WOLFSSL_SUCCESS) { + return -1; + } + return 0; +} + +/* Client side shared by both handshakes: trust the CA that signed the server + * cert. */ +static int huk_tls_client_ctx(WOLFSSL_CTX* cctx) +{ + wolfSSL_CTX_set_cipher_list(cctx, "ECDHE-ECDSA-AES128-GCM-SHA256"); + wolfSSL_SetIORecv(cctx, recv_client); + wolfSSL_SetIOSend(cctx, send_client); + if (wolfSSL_CTX_load_verify_buffer(cctx, ca_ecc_cert_der_256, + sizeof_ca_ecc_cert_der_256, WOLFSSL_FILETYPE_ASN1) + != WOLFSSL_SUCCESS) { + return -1; + } + return 0; +} + +static void huk_tls_test(void) +{ + WOLFSSL_CTX* cctx; + WOLFSSL_CTX* sctx; + + dbg_printf("\r\n== TLS 1.2 ECDHE-ECDSA (server auth on HUK) ==\r\n"); + + sctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()); + cctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()); + CHECK("CTX_new (client+server)", sctx != NULL && cctx != NULL); + if (sctx != NULL && cctx != NULL) { + CHECK("server cert + key", huk_tls_server_ctx(sctx) == 0); + CHECK("client load_verify (CA)", huk_tls_client_ctx(cctx) == 0); + CHECK("handshake + record (server signs on HW)", + huk_tls_session(cctx, sctx, NULL) == 0); + } + if (cctx != NULL) wolfSSL_CTX_free(cctx); + if (sctx != NULL) wolfSSL_CTX_free(sctx); +} + +static void huk_mtls_test(void) +{ + WOLFSSL_CTX* cctx; + WOLFSSL_CTX* sctx; + huk_key_blob blob; + huk_key_blob bad; + int format; + int ret; + + dbg_printf("\r\n== TLS 1.2 mutual auth, client key HUK-wrapped ==\r\n"); + + sctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()); + cctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()); + CHECK("CTX_new (client+server)", sctx != NULL && cctx != NULL); + if (sctx == NULL || cctx == NULL) { + goto cleanup; + } + + /* Server requires a client certificate and trusts the self-signed client + * cert. */ + ret = huk_tls_server_ctx(sctx); + if (ret == 0) { + wolfSSL_CTX_set_verify(sctx, WOLFSSL_VERIFY_PEER | + WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, NULL); + if (wolfSSL_CTX_load_verify_buffer(sctx, cliecc_cert_der_256, + sizeof_cliecc_cert_der_256, WOLFSSL_FILETYPE_ASN1) + != WOLFSSL_SUCCESS) { + ret = -1; + } + } + CHECK("server cert + key, client cert required", ret == 0); + if (ret != 0) { + goto cleanup; + } + + /* Client: the sign callback is set before the key is loaded, so wolfSSL + * accepts the public key in place of the private key and sends every + * signature to the callback. */ + ret = huk_tls_client_ctx(cctx); + if (ret == 0) { + wolfSSL_CTX_SetEccSignCb(cctx, huk_wrapped_sign_cb); + if (wolfSSL_CTX_use_certificate_buffer(cctx, cliecc_cert_der_256, + sizeof_cliecc_cert_der_256, WOLFSSL_FILETYPE_ASN1) + != WOLFSSL_SUCCESS || + wolfSSL_CTX_use_PrivateKey_buffer(cctx, ecc_clikeypub_der_256, + sizeof_ecc_clikeypub_der_256, WOLFSSL_FILETYPE_ASN1) + != WOLFSSL_SUCCESS) { + ret = -1; + } + } + CHECK("client cert + public key only", ret == 0); + if (ret != 0) { + goto cleanup; + } + + /* A tampered GCM blob fails its unwrap on the client. A tampered ECB blob + * unwraps to a wrong scalar and signs, and the server rejects the + * CertificateVerify. Either way the handshake fails. */ + for (format = HUK_WRAP_GCM; format <= HUK_WRAP_ECB; format++) { + dbg_printf("-- %s-wrapped client key --\r\n", + (format == HUK_WRAP_GCM) ? "GCM" : "ECB"); + ret = huk_wrap_client_key(&g_signRng, (byte)format, &blob); + CHECK("wrap the client private key under the HUK", ret == 0); + if (ret != 0) { + continue; + } + CHECK("mutual handshake + record (client signs from the HUK blob)", + huk_tls_session(cctx, sctx, &blob) == 0); + + memcpy(&bad, &blob, sizeof(bad)); + bad.wrapped[0] ^= 0x01; + CHECK("tampered HUK blob fails the handshake", + huk_tls_session(cctx, sctx, &bad) != 0); + } + +cleanup: if (cctx != NULL) wolfSSL_CTX_free(cctx); if (sctx != NULL) wolfSSL_CTX_free(sctx); - wc_FreeRng(&g_signRng); } static void wolf_tls_thread(void* param) @@ -277,7 +499,13 @@ static void wolf_tls_thread(void* param) CHECK("wc_Rtl8735b_HukRegister", ret == 0); } if (ret == 0) { - huk_tls_test(); + ret = wc_InitRng(&g_signRng); + CHECK("sign-callback RNG init", ret == 0); + if (ret == 0) { + huk_tls_test(); + huk_mtls_test(); + wc_FreeRng(&g_signRng); + } wc_Rtl8735b_HukUnRegister(WC_HUK_DEVID); } wolfSSL_Cleanup(); diff --git a/rtl8735b/tls/user_settings.h b/rtl8735b/tls/user_settings.h index 4970d4964..ab436a9b7 100644 --- a/rtl8735b/tls/user_settings.h +++ b/rtl8735b/tls/user_settings.h @@ -39,12 +39,12 @@ extern "C" { #define WOLFSSL_NO_TLS12_RENEGOTIATION #define NO_SESSION_CACHE /* save RAM (no resumption cache) */ #define WOLFSSL_AEAD_ONLY /* AES-GCM suites only (no CBC-HMAC) */ -#define HAVE_PK_CALLBACKS /* route ONLY the server ECDSA sign to the HUK +#define HAVE_PK_CALLBACKS /* route ONLY the ECDSA signs to the HUK * (whole-CTX devId would send TLS's own 32-byte * HMAC/PRF + record AES to the HUK too, whose * key==HUK-seed semantics break TLS) */ -/* ---- test certificate buffers (serv_ecc/ecc_key/ca_ecc, P-256) ---- */ +/* ---- test certificate buffers (server, CA and client, P-256) ---- */ #define USE_CERT_BUFFERS_256 #define NO_ASN_TIME /* no RTC on the board -> skip cert date checks */ diff --git a/rtl8735b/user_settings.h b/rtl8735b/user_settings.h index 54359efb4..6eabec041 100644 --- a/rtl8735b/user_settings.h +++ b/rtl8735b/user_settings.h @@ -17,6 +17,7 @@ extern "C" { #define WOLF_CRYPTO_CB #define WOLF_CRYPTO_CB_COPY /* HUK HMAC buffer copy/free ops */ #define WOLF_CRYPTO_CB_FREE +#define WOLF_CRYPTO_CB_KEYSTORE /* HUK-wrapped AES keys in key-storage slots */ /* ---- platform / RTOS ---- */ #define WOLFSSL_GENERAL_ALIGNMENT 4 @@ -34,6 +35,7 @@ extern "C" { #define HAVE_AESGCM #define WOLFSSL_AES_DIRECT #define HAVE_AES_ECB +#define HAVE_AES_KEYWRAP /* RFC 3394 wrap for key-store imports */ #define HAVE_AES_CBC #define WOLFSSL_AES_COUNTER #define WOLFSSL_AES_256 diff --git a/rtl8735b/wolfcrypt_huk.cmake b/rtl8735b/wolfcrypt_huk.cmake index 02f470326..82a89938e 100644 --- a/rtl8735b/wolfcrypt_huk.cmake +++ b/rtl8735b/wolfcrypt_huk.cmake @@ -55,3 +55,7 @@ list(APPEND app_example_sources ${WOLFSSL_ROOT}/wolfcrypt/src/sp_int.c ${WOLFSSL_ROOT}/wolfcrypt/src/wolfmath.c ) +# Key-store API (wc_KeyStore_*), present in wolfSSL builds that have it. +if(EXISTS "${WOLFSSL_ROOT}/wolfcrypt/src/wc_keystore.c") + list(APPEND app_example_sources ${WOLFSSL_ROOT}/wolfcrypt/src/wc_keystore.c) +endif()