Skip to content

Commit f12d2f0

Browse files
mattia-moffadanielinux
authored andcommitted
Add PKCS11 backend for encrypted partitions
1 parent aa39be2 commit f12d2f0

File tree

7 files changed

+326
-18
lines changed

7 files changed

+326
-18
lines changed

docs/encrypted_partitions.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,36 @@ To compile wolfBoot with encryption support, use the option `ENCRYPT=1`.
7373
By default, this also selects `ENCRYPT_WITH_CHACHA=1`. To use AES encryption instead,
7474
select `ENCRYPT_WITH_AES128=1` or `ENCRYPT_WITH_AES256=1`.
7575

76+
### PKCS#11 backend
77+
78+
On ARM TrustZone configurations with `WOLFCRYPT_TZ_PKCS11` enabled, it is
79+
possible to use the keyvault provided by wolfBoot to store the encryption key.
80+
To enable this, use the configuration option `ENCRYPT_PKCS11=1` alongside
81+
`WOLFCRYPT_TZ=1`, `WOLFCRYPT_TZ_PKCS11=1` and `ENCRYPT=1`, and set
82+
`ENCRYPT_PKCS11_PIN` to the user PIN for the PKCS#11 vault. This should be set
83+
in the form of a C string literal, for example
84+
`ENCRYPT_PKCS11_PIN="\x01\x02\x03\x04"`, and the ending null byte will be
85+
ignored.
86+
87+
When this is enabled, instead of providing the key to wolfBoot via
88+
`wolfBoot_set_encrypt_key`, the application should use the PKCS#11 API to store
89+
it in the keyvault with an appropriate ID (via the `CKA_ID` attribute). This ID
90+
should then be used as the `key` parameter (in place of the key) when calling
91+
`wolfBoot_set_encrypt_key`.
92+
93+
The following configuration options are also available:
94+
95+
- `ENCRYPT_PKCS11_KEY_ID_SIZE` (default `4`): this is the size, in bytes, of
96+
the key ID.
97+
- `ENCRYPT_PKCS11_MECHANISM` (default `0x00001086UL`, i.e. AES-CTR): this is
98+
the numeric ID of the PKCS#11 mechanism to be used for encryption. Currently
99+
only AES-CTR is supported. The list of IDs can be found in the [appendix of
100+
the PKCS#11 mechanism specification](https://docs.oasis-open.org/pkcs11/pkcs11-curr/v2.40/cs01/pkcs11-curr-v2.40-cs01.html#_Toc228894920),
101+
while the list of IDs supported by wolfPKCS11 is set in the [pkcs11.h](https://github.com/wolfSSL/wolfPKCS11/blob/master/wolfpkcs11/pkcs11.h)
102+
header file.
103+
- `ENCRYPT_PKCS11_BLOCK_SIZE` (default `16`, for AES-CTR) and
104+
`ENCRYPT_PKCS11_NONCE_SIZE` (default `16`, for AES-CTR) should be set in case
105+
you set `ENCRYPT_PKCS11_MECHANISM` to something other than the default.
76106

77107
### Signing and encrypting the update bundle with ChaCha20-256
78108

include/encrypt.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,21 @@ extern Aes aes_dec, aes_enc;
6363

6464
int aes_init(void);
6565
void aes_set_iv(uint8_t *nonce, uint32_t address);
66+
67+
#elif defined(ENCRYPT_PKCS11)
68+
#include "wolfboot/wcs_pkcs11.h"
69+
70+
int pkcs11_crypto_init(void);
71+
void pkcs11_crypto_set_iv(uint8_t *nonce, uint32_t iv_ctr);
72+
int pkcs11_crypto_encrypt(uint8_t *out, uint8_t *in, size_t size);
73+
int pkcs11_crypto_decrypt(uint8_t *out, uint8_t *in, size_t size);
74+
void pkcs11_crypto_deinit(void);
75+
76+
#define crypto_init() pkcs11_crypto_init()
77+
#define crypto_encrypt(eb,b,sz) pkcs11_crypto_encrypt(eb, b, sz)
78+
#define crypto_decrypt(db,b,sz) pkcs11_crypto_decrypt(db, b, sz)
79+
#define crypto_set_iv(n,a) pkcs11_crypto_set_iv(n, a)
80+
6681
#endif /* ENCRYPT_WITH_CHACHA */
6782

6883
/* external flash encryption read/write functions */

include/user_settings.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,11 +353,15 @@ extern int tolower(int c);
353353
# define WOLFBOOT_SECURE_PKCS11
354354
# define WOLFPKCS11_USER_SETTINGS
355355
# define WOLFPKCS11_NO_TIME
356+
#ifndef WOLFSSL_AES_COUNTER
356357
# define WOLFSSL_AES_COUNTER
358+
#endif
359+
# define HAVE_AESCTR
360+
#ifndef WOLFSSL_AES_DIRECT
357361
# define WOLFSSL_AES_DIRECT
362+
#endif
358363
# define WOLFSSL_AES_GCM
359364
# define GCM_TABLE_4BIT
360-
# define ENCRYPT_WITH_AES128
361365
# define WOLFSSL_AES_128
362366
# define HAVE_SCRYPT
363367
# define HAVE_AESGCM

include/wolfboot/wolfboot.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,11 @@ int wolfBoot_get_partition_state(uint8_t part, uint8_t *st);
514514
#define ENCRYPT_BLOCK_SIZE 16
515515
#define ENCRYPT_KEY_SIZE 32 /* AES256 */
516516
#define ENCRYPT_NONCE_SIZE 16 /* AES IV size */
517+
#elif defined(ENCRYPT_PKCS11)
518+
#define ENCRYPT_BLOCK_SIZE ENCRYPT_PKCS11_BLOCK_SIZE
519+
/* In this case, the key ID is stored in flash rather than the key itself */
520+
#define ENCRYPT_KEY_SIZE ENCRYPT_PKCS11_KEY_ID_SIZE
521+
#define ENCRYPT_NONCE_SIZE ENCRYPT_PKCS11_NONCE_SIZE
517522
#else
518523
# error "Encryption ON, but no encryption algorithm selected."
519524
#endif

options.mk

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -530,19 +530,37 @@ endif
530530

531531
ifeq ($(ENCRYPT),1)
532532
CFLAGS+=-D"EXT_ENCRYPTED=1"
533-
ifeq ($(ENCRYPT_WITH_AES128),1)
534-
CFLAGS+=-DWOLFSSL_AES_COUNTER -DWOLFSSL_AES_DIRECT
535-
CFLAGS+=-DENCRYPT_WITH_AES128 -DWOLFSSL_AES_128
536-
WOLFCRYPT_OBJS+=$(WOLFBOOT_LIB_WOLFSSL)/wolfcrypt/src/aes.o
533+
ifeq ($(ENCRYPT_PKCS11),1)
534+
CFLAGS+=-DENCRYPT_PKCS11 -D'ENCRYPT_PKCS11_PIN=$(ENCRYPT_PKCS11_PIN)'
535+
ifeq ($(ENCRYPT_PKCS11_KEY_ID_SIZE),)
536+
ENCRYPT_PKCS11_KEY_ID_SIZE=4
537+
endif
538+
CFLAGS+=-DENCRYPT_PKCS11_KEY_ID_SIZE=$(ENCRYPT_PKCS11_KEY_ID_SIZE)
539+
ifeq ($(ENCRYPT_PKCS11_MECHANISM),)
540+
# No mechanism defined; assume AES-CTR
541+
CFLAGS+=-DENCRYPT_PKCS11_MECHANISM=0x00001086UL
542+
CFLAGS+=-DENCRYPT_PKCS11_BLOCK_SIZE=16
543+
CFLAGS+=-DENCRYPT_PKCS11_NONCE_SIZE=16
544+
else
545+
CFLAGS+=-DENCRYPT_PKCS11_MECHANISM=$(ENCRYPT_PKCS11_MECHANISM)
546+
CFLAGS+=-DENCRYPT_PKCS11_BLOCK_SIZE=$(ENCRYPT_PKCS11_BLOCK_SIZE)
547+
CFLAGS+=-DENCRYPT_PKCS11_NONCE_SIZE=$(ENCRYPT_PKCS11_NONCE_SIZE)
548+
endif
537549
else
538-
ifeq ($(ENCRYPT_WITH_AES256),1)
550+
ifeq ($(ENCRYPT_WITH_AES128),1)
539551
CFLAGS+=-DWOLFSSL_AES_COUNTER -DWOLFSSL_AES_DIRECT
540-
CFLAGS+=-DENCRYPT_WITH_AES256 -DWOLFSSL_AES_256
552+
CFLAGS+=-DENCRYPT_WITH_AES128 -DWOLFSSL_AES_128
541553
WOLFCRYPT_OBJS+=$(WOLFBOOT_LIB_WOLFSSL)/wolfcrypt/src/aes.o
542554
else
543-
ENCRYPT_WITH_CHACHA=1
544-
WOLFCRYPT_OBJS+=$(WOLFBOOT_LIB_WOLFSSL)/wolfcrypt/src/chacha.o
545-
CFLAGS+=-DENCRYPT_WITH_CHACHA -DHAVE_CHACHA
555+
ifeq ($(ENCRYPT_WITH_AES256),1)
556+
CFLAGS+=-DWOLFSSL_AES_COUNTER -DWOLFSSL_AES_DIRECT
557+
CFLAGS+=-DENCRYPT_WITH_AES256 -DWOLFSSL_AES_256
558+
WOLFCRYPT_OBJS+=$(WOLFBOOT_LIB_WOLFSSL)/wolfcrypt/src/aes.o
559+
else
560+
ENCRYPT_WITH_CHACHA=1
561+
WOLFCRYPT_OBJS+=$(WOLFBOOT_LIB_WOLFSSL)/wolfcrypt/src/chacha.o
562+
CFLAGS+=-DENCRYPT_WITH_CHACHA -DHAVE_CHACHA
563+
endif
546564
endif
547565
endif
548566
endif
@@ -665,7 +683,7 @@ ifeq ($(WOLFCRYPT_TZ_PKCS11),1)
665683
$(WOLFBOOT_LIB_WOLFPKCS11)/src/slot.o \
666684
$(WOLFBOOT_LIB_WOLFPKCS11)/src/wolfpkcs11.o
667685
STACK_USAGE=16688
668-
ifneq ($(ENCRYPT),1)
686+
ifeq ($(ENCRYPT_WITH_AES128)$(ENCRYPT_WITH_AES256),)
669687
WOLFCRYPT_OBJS+=$(WOLFBOOT_LIB_WOLFSSL)/wolfcrypt/src/aes.o
670688
endif
671689
ifeq ($(findstring RSA,$(SIGN)),)

0 commit comments

Comments
 (0)