Skip to content

Commit 8e6b0af

Browse files
dgarskedanielinux
authored andcommitted
Added Renesas RX TSIP encrypted updates support using AES CTR. Requires wolfSSL/wolfssl#8854
1 parent 476d1c8 commit 8e6b0af

File tree

7 files changed

+87
-5
lines changed

7 files changed

+87
-5
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ src/ecc512_pub_key.c
6161
src/rsa2048_pub_key.c
6262
src/rsa4096_pub_key.c
6363
# Renesas key data files
64-
include/key_data.c
65-
include/key_data.h
64+
include/key_data.*
65+
include/enckey_data.*
6666

6767
# keygen binaries
6868
tools/keytools/sign

arch.mk

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,8 @@ ifeq ($(ARCH),RENESAS_RX)
470470

471471
OBJS+=./lib/wolfssl/wolfcrypt/src/cryptocb.o \
472472
./lib/wolfssl/wolfcrypt/src/port/Renesas/renesas_common.o \
473-
./lib/wolfssl/wolfcrypt/src/port/Renesas/renesas_tsip_util.o
473+
./lib/wolfssl/wolfcrypt/src/port/Renesas/renesas_tsip_util.o \
474+
./lib/wolfssl/wolfcrypt/src/port/Renesas/renesas_tsip_aes.o
474475

475476
# RX TSIP uses pre-compiled .a library by default
476477
ifneq ($(RX_TSIP_SRC),1)

docs/Renesas.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Result is `sample.key_enc.key`. Example: `00000001 6CCB9A1C 8AA58883 B1CB02DE 6C
4040

4141
```sh
4242
# Build keytools for Renesas RX (TSIP)
43+
# Use RENESAS_KEY=2 for TSIP
4344
$ make keytools RENESAS_KEY=2
4445
```
4546

@@ -163,6 +164,34 @@ Output image(s) successfully created.
163164

164165
Download files to flash using Renesas flash programmer.
165166

167+
## RX TSIP AES Encryption (optional)
168+
169+
Create a wrapped AES key for encrypting/decrypting the update
170+
171+
Example key: `fwenc.key`: e07227e477450b1ca266078e217a3c89cbae827a7bb117ff851bc25300163575
172+
Note: `.config` must include `ENCRYPT=1` and `ENCRYPT_WITH_AES256=1`
173+
174+
```sh
175+
$ C:\Renesas\SecurityKeyManagementTool\cli\skmt.exe -genkey -ufpk file=./sample.key -wufpk file=./sample.key_enc.key -key file=./fwenc.key -mcu RX-TSIP -keytype AES-256 -output include/enckey_data.c -filetype csource -keyname wrap_enc_key -iv A8B14B0F5F09D73F31D4777FC0103FB4
176+
Output File: C:\CPG_Controls\wolfboot\include\enckey_data.h
177+
Output File: C:\CPG_Controls\wolfboot\include\enckey_data.c
178+
UFPK: B94A2B961C75510174F0C967ECFC20B377C7FB256DB627B1BFFADEE05EE98AC4
179+
W-UFPK: 000000016CCB9A1C8AA58883B1CB02DE6C37DA6054FB94E206EAE7204D9CCF4C6EEB288C
180+
IV: A8B14B0F5F09D73F31D4777FC0103FB4
181+
Encrypted key: 3C39BE75E9CA5CB9D2D0BBDE111CABC894A2B13F857399B05E7B140518F35D05CD97D8DF20817CEEBA2F207CC90BAF2C
182+
183+
$ C:\Renesas\SecurityKeyManagementTool\cli\skmt.exe -genkey -ufpk file=./sample.key -wufpk file=./sample.key_enc.key -key file=./fwenc.key -mcu RX-TSIP -keytype AES-256 -output fwenc.srec -filetype "mot" -address FFFF0100 -iv A8B14B0F5F09D73F31D4777FC0103FB4
184+
Output File: C:\CPG_Controls\wolfboot\fwenc.srec
185+
UFPK: B94A2B961C75510174F0C967ECFC20B377C7FB256DB627B1BFFADEE05EE98AC4
186+
W-UFPK: 000000016CCB9A1C8AA58883B1CB02DE6C37DA6054FB94E206EAE7204D9CCF4C6EEB288C
187+
IV: A8B14B0F5F09D73F31D4777FC0103FB4
188+
Encrypted key: 3C39BE75E9CA5CB9D2D0BBDE111CABC894A2B13F857399B05E7B140518F35D05CD97D8DF20817CEEBA2F207CC90BAF2C
189+
```
190+
191+
The offset for the wrapped AES key is determined by `RENESAS_TSIP_INSTALLEDENCKEY_ADDR` and defaults to `RENESAS_TSIP_INSTALLEDKEY_ADDR` + 0x100
192+
193+
The key needed for the firmware signing tool is the 32 byte AES Key + 16 byte IV.
194+
`echo "e07227e477450b1ca266078e217a3c89cbae827a7bb117ff851bc25300163575A8B14B0F5F09D73F31D4777FC0103FB4" | xxd -r -p - > fwkey.bin`
166195

167196
### RX TSIP Benchmarks
168197

hal/renesas-rx.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,8 @@ int hal_renesas_init(void)
374374
int err;
375375
uint32_t key_type = 0;
376376
int tsip_key_type = -1;
377+
/* This structure is generated using Renesas Security Key Management Tool
378+
* See docs/Renesas.md */
377379
struct enc_pub_key *encrypted_user_key_data;
378380

379381
if (sipInitDone)

include/encrypt.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@
3838
#else
3939
#include <wolfssl/wolfcrypt/aes.h>
4040
#endif
41+
#ifdef WOLF_CRYPTO_CB
42+
#include <wolfssl/wolfcrypt/cryptocb.h>
43+
#endif
4144

4245
#include <wolfssl/wolfcrypt/pwdbased.h>
4346

include/user_settings.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,7 @@ extern int tolower(int c);
502502
#define WOLF_CRYPTO_CB_ONLY_RSA
503503
#define WOLFSSL_NO_SW_MATH
504504
#define MAX_CRYPTO_DEVID_CALLBACKS 2
505+
#define WOLFSSL_AES_SMALL_TABLES
505506

506507
#ifdef WOLFBOOT_RENESAS_TSIP
507508
#define WOLFSSL_RENESAS_TSIP
@@ -510,6 +511,10 @@ extern int tolower(int c);
510511
#define WOLFSSL_RENESAS_TSIP_CRYPTONLY
511512
#define NO_WOLFSSL_RENESAS_TSIP_CRYPT_HASH
512513
#define RENESAS_TSIP_INSTALLEDKEY_ADDR 0xFFFF0000
514+
#ifndef RENESAS_TSIP_INSTALLEDENCKEY_ADDR
515+
#define RENESAS_TSIP_INSTALLEDENCKEY_ADDR \
516+
(RENESAS_TSIP_INSTALLEDKEY_ADDR + 0x100)
517+
#endif
513518
#define ENCRYPTED_KEY_BYTE_SIZE ENC_PUB_KEY_SIZE
514519
#define RENESAS_DEVID 7890
515520
#endif

src/libwolfboot.c

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1553,6 +1553,7 @@ int RAMFUNCTION chacha_init(void)
15531553
#elif defined(ENCRYPT_WITH_AES128) || defined(ENCRYPT_WITH_AES256)
15541554

15551555
Aes aes_dec, aes_enc;
1556+
15561557
/**
15571558
* @brief Initialize AES encryption.
15581559
*
@@ -1564,6 +1565,46 @@ Aes aes_dec, aes_enc;
15641565
*/
15651566
int aes_init(void)
15661567
{
1568+
#if defined(WOLFBOOT_RENESAS_TSIP)
1569+
/* This structure is generated using Renesas Security Key Management Tool
1570+
* See docs/Renesas.md */
1571+
#include "enckey_data.h"
1572+
int ret;
1573+
int devId = RENESAS_DEVID + 1;
1574+
wrap_enc_key_t* enc_key =(wrap_enc_key_t*)RENESAS_TSIP_INSTALLEDENCKEY_ADDR;
1575+
1576+
XMEMSET(&aes_enc, 0, sizeof(aes_enc));
1577+
XMEMSET(&aes_dec, 0, sizeof(aes_dec));
1578+
wc_AesInit(&aes_enc, NULL, devId);
1579+
wc_AesInit(&aes_dec, NULL, devId);
1580+
1581+
/* Unwrap key and get key index */
1582+
#if ENCRYPT_KEY_SIZE == 32
1583+
ret = R_TSIP_GenerateAes256KeyIndex(enc_key->wufpk, enc_key->initial_vector,
1584+
enc_key->encrypted_user_key, &aes_enc.ctx.tsip_keyIdx);
1585+
#else
1586+
ret = R_TSIP_GenerateAes128KeyIndex(enc_key->wufpk, enc_key->initial_vector,
1587+
enc_key->encrypted_user_key, &aes_enc.ctx.tsip_keyIdx);
1588+
#endif
1589+
if (ret == TSIP_SUCCESS) {
1590+
/* copy to decryption key */
1591+
XMEMCPY(&aes_dec.ctx, &aes_enc.ctx, sizeof(aes_enc.ctx));
1592+
1593+
/* register AES crypto callback */
1594+
extern int wc_tsip_AesCipher(int devIdArg, struct wc_CryptoInfo* info, void* ctx);
1595+
wc_CryptoCb_RegisterDevice(devId, wc_tsip_AesCipher, NULL);
1596+
1597+
encrypt_initialized = 1;
1598+
1599+
/* AES_ENCRYPTION is used for both directions in CTR */
1600+
/* unwrapped key never leaves TSIP and is referenced by tsip_keyIdx */
1601+
wc_AesSetKeyDirect(&aes_enc, enc_key->encrypted_user_key,
1602+
ENCRYPT_KEY_SIZE, enc_key->initial_vector, AES_ENCRYPTION);
1603+
wc_AesSetKeyDirect(&aes_dec, enc_key->encrypted_user_key,
1604+
ENCRYPT_KEY_SIZE, enc_key->initial_vector, AES_ENCRYPTION);
1605+
}
1606+
#else
1607+
15671608
#if defined(MMU) || defined(UNIT_TEST)
15681609
uint8_t *key = ENCRYPT_KEY;
15691610
#else
@@ -1582,8 +1623,8 @@ int aes_init(void)
15821623

15831624
XMEMSET(&aes_enc, 0, sizeof(aes_enc));
15841625
XMEMSET(&aes_dec, 0, sizeof(aes_dec));
1585-
wc_AesInit(&aes_enc, NULL, 0);
1586-
wc_AesInit(&aes_dec, NULL, 0);
1626+
wc_AesInit(&aes_enc, NULL, INVALID_DEVID);
1627+
wc_AesInit(&aes_dec, NULL, INVALID_DEVID);
15871628

15881629
/* Check against 'all 0xff' or 'all zero' cases */
15891630
XMEMSET(ff, 0xFF, ENCRYPT_KEY_SIZE);
@@ -1599,6 +1640,7 @@ int aes_init(void)
15991640
wc_AesSetKeyDirect(&aes_enc, key, ENCRYPT_KEY_SIZE, iv_buf, AES_ENCRYPTION);
16001641
wc_AesSetKeyDirect(&aes_dec, key, ENCRYPT_KEY_SIZE, iv_buf, AES_ENCRYPTION);
16011642
encrypt_initialized = 1;
1643+
#endif
16021644
return 0;
16031645
}
16041646

0 commit comments

Comments
 (0)