Skip to content

Commit 9e1d883

Browse files
de-nordicrlubos
authored andcommitted
[nrf noup] PSA implementation of x25519 and ed25519 verification
The commit provides implementation of image verification with ed25519 and encryption/decryption support where random key is encrypted using x25519. Signed-off-by: Dominik Ermel <[email protected]> (cherry picked from commit e874cf8)
1 parent 3cdcdb3 commit 9e1d883

File tree

5 files changed

+632
-60
lines changed

5 files changed

+632
-60
lines changed

boot/bootutil/include/bootutil/crypto/aes_ctr.h

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
#include "mcuboot_config/mcuboot_config.h"
1616

1717
#if (defined(MCUBOOT_USE_MBED_TLS) + \
18-
defined(MCUBOOT_USE_TINYCRYPT)) != 1
19-
#error "One crypto backend must be defined: either MBED_TLS or TINYCRYPT"
18+
defined(MCUBOOT_USE_TINYCRYPT) + defined(MCUBOOT_USE_PSA_CRYPTO)) != 1
19+
#error "One crypto backend must be defined: either MBED_TLS or TINYCRYPT or PSA"
2020
#endif
2121

2222
#if defined(MCUBOOT_USE_MBED_TLS)
@@ -38,12 +38,46 @@
3838
#define BOOTUTIL_CRYPTO_AES_CTR_BLOCK_SIZE TC_AES_BLOCK_SIZE
3939
#endif /* MCUBOOT_USE_TINYCRYPT */
4040

41+
42+
#if defined(MCUBOOT_USE_PSA_CRYPTO)
43+
#include <psa/crypto.h>
44+
#include "bootutil/enc_key_public.h"
45+
#define BOOTUTIL_CRYPTO_AES_CTR_KEY_SIZE BOOT_ENC_KEY_SIZE
46+
#define BOOTUTIL_CRYPTO_AES_CTR_BLOCK_SIZE (16)
47+
#endif
48+
4149
#include <stdint.h>
4250

4351
#ifdef __cplusplus
4452
extern "C" {
4553
#endif
4654

55+
#if defined(MCUBOOT_USE_PSA_CRYPTO)
56+
typedef struct {
57+
/* Fixme: This should not be, here, psa_key_id should be passed */
58+
uint8_t key[BOOT_ENC_KEY_SIZE];
59+
} bootutil_aes_ctr_context;
60+
61+
void bootutil_aes_ctr_init(bootutil_aes_ctr_context *ctx);
62+
63+
static inline void bootutil_aes_ctr_drop(bootutil_aes_ctr_context *ctx)
64+
{
65+
memset(ctx, 0, sizeof(ctx));
66+
}
67+
68+
static inline int bootutil_aes_ctr_set_key(bootutil_aes_ctr_context *ctx, const uint8_t *k)
69+
{
70+
memcpy(ctx->key, k, sizeof(ctx->key));
71+
72+
return 0;
73+
}
74+
75+
int bootutil_aes_ctr_encrypt(bootutil_aes_ctr_context *ctx, uint8_t *counter,
76+
const uint8_t *m, uint32_t mlen, size_t blk_off, uint8_t *c);
77+
int bootutil_aes_ctr_decrypt(bootutil_aes_ctr_context *ctx, uint8_t *counter,
78+
const uint8_t *c, uint32_t clen, size_t blk_off, uint8_t *m);
79+
#endif
80+
4781
#if defined(MCUBOOT_USE_MBED_TLS)
4882
typedef mbedtls_aes_context bootutil_aes_ctr_context;
4983
static inline void bootutil_aes_ctr_init(bootutil_aes_ctr_context *ctx)

boot/bootutil/src/ed25519_psa.c

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright (c) 2020 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
*/
6+
#include <assert.h>
7+
#include <string.h>
8+
#include <stdint.h>
9+
10+
#include <mcuboot_config/mcuboot_config.h>
11+
#include "bootutil/bootutil_log.h"
12+
13+
#include <psa/crypto.h>
14+
#include <psa/crypto_types.h>
15+
16+
BOOT_LOG_MODULE_DECLARE(ed25519_psa);
17+
18+
#define SHA512_DIGEST_LENGTH 64
19+
#define EDDSA_KEY_LENGTH 32
20+
#define EDDSA_SIGNAGURE_LENGTH 64
21+
22+
int ED25519_verify(const uint8_t *message, size_t message_len,
23+
const uint8_t signature[EDDSA_SIGNAGURE_LENGTH],
24+
const uint8_t public_key[EDDSA_KEY_LENGTH])
25+
{
26+
/* Set to any error */
27+
psa_status_t status = PSA_ERROR_BAD_STATE;
28+
psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
29+
psa_key_id_t kid;
30+
int ret = 0; /* Fail by default */
31+
32+
/* Initialize PSA Crypto */
33+
status = psa_crypto_init();
34+
if (status != PSA_SUCCESS) {
35+
BOOT_LOG_ERR("PSA crypto init failed %d\n", status);
36+
return 0;
37+
}
38+
39+
status = PSA_ERROR_BAD_STATE;
40+
41+
psa_set_key_type(&key_attr,
42+
PSA_KEY_TYPE_ECC_PUBLIC_KEY(PSA_ECC_FAMILY_TWISTED_EDWARDS));
43+
psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_VERIFY_MESSAGE);
44+
psa_set_key_algorithm(&key_attr, PSA_ALG_PURE_EDDSA);
45+
46+
status = psa_import_key(&key_attr, public_key, EDDSA_KEY_LENGTH, &kid);
47+
if (status != PSA_SUCCESS) {
48+
BOOT_LOG_ERR("ED25519 key import failed %d", status);
49+
return 0;
50+
}
51+
52+
status = psa_verify_message(kid, PSA_ALG_PURE_EDDSA, message, message_len,
53+
signature, EDDSA_SIGNAGURE_LENGTH);
54+
if (status != PSA_SUCCESS) {
55+
BOOT_LOG_ERR("ED25519 signature verification failed %d", status);
56+
ret = 0;
57+
/* Pass through to destroy key */
58+
} else {
59+
ret = 1;
60+
/* Pass through to destroy key */
61+
}
62+
63+
status = psa_destroy_key(kid);
64+
65+
if (status != PSA_SUCCESS) {
66+
/* Just for logging */
67+
BOOT_LOG_WRN("Failed to destroy key %d", status);
68+
}
69+
70+
return ret;
71+
}

boot/bootutil/src/encrypted.c

Lines changed: 60 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "bootutil/crypto/ecdh_p256.h"
2626
#endif
2727

28+
#if !defined(MCUBOOT_USE_PSA_CRYPTO)
2829
#if defined(MCUBOOT_ENCRYPT_X25519)
2930
#include "bootutil/crypto/ecdh_x25519.h"
3031
#endif
@@ -35,6 +36,7 @@
3536
#include "mbedtls/oid.h"
3637
#include "mbedtls/asn1.h"
3738
#endif
39+
#endif
3840

3941
#include "bootutil/image.h"
4042
#include "bootutil/enc_key.h"
@@ -43,6 +45,30 @@
4345

4446
#include "bootutil_priv.h"
4547

48+
#define EXPECTED_ENC_LEN BOOT_ENC_TLV_SIZE
49+
50+
#if defined(MCUBOOT_ENCRYPT_RSA)
51+
# define EXPECTED_ENC_TLV IMAGE_TLV_ENC_RSA2048
52+
#elif defined(MCUBOOT_ENCRYPT_KW)
53+
# define EXPECTED_ENC_TLV IMAGE_TLV_ENC_KW
54+
#elif defined(MCUBOOT_ENCRYPT_EC256)
55+
# define EXPECTED_ENC_TLV IMAGE_TLV_ENC_EC256
56+
# define EC_PUBK_INDEX (0)
57+
# define EC_TAG_INDEX (65)
58+
# define EC_CIPHERKEY_INDEX (65 + 32)
59+
_Static_assert(EC_CIPHERKEY_INDEX + BOOT_ENC_KEY_SIZE == EXPECTED_ENC_LEN,
60+
"Please fix ECIES-P256 component indexes");
61+
#elif defined(MCUBOOT_ENCRYPT_X25519)
62+
# define EXPECTED_ENC_TLV IMAGE_TLV_ENC_X25519
63+
# define EC_PUBK_INDEX (0)
64+
# define EC_TAG_INDEX (32)
65+
# define EC_CIPHERKEY_INDEX (32 + 32)
66+
_Static_assert(EC_CIPHERKEY_INDEX + BOOT_ENC_KEY_SIZE == EXPECTED_ENC_LEN,
67+
"Please fix ECIES-X25519 component indexes");
68+
#endif
69+
70+
/* NOUP Fixme: */
71+
#if !defined(CONFIG_BOOT_ED25519_PSA)
4672
#if defined(MCUBOOT_ENCRYPT_EC256) || defined(MCUBOOT_ENCRYPT_X25519)
4773
#if defined(_compare)
4874
static inline int bootutil_constant_time_compare(const uint8_t *a, const uint8_t *b, size_t size)
@@ -351,60 +377,6 @@ int boot_enc_retrieve_private_key(struct bootutil_key **private_key)
351377
}
352378
#endif /* !MCUBOOT_ENC_BUILTIN_KEY */
353379

354-
int
355-
boot_enc_init(struct enc_key_data *enc_state, uint8_t slot)
356-
{
357-
bootutil_aes_ctr_init(&enc_state[slot].aes_ctr);
358-
return 0;
359-
}
360-
361-
int
362-
boot_enc_drop(struct enc_key_data *enc_state, uint8_t slot)
363-
{
364-
bootutil_aes_ctr_drop(&enc_state[slot].aes_ctr);
365-
enc_state[slot].valid = 0;
366-
return 0;
367-
}
368-
369-
int
370-
boot_enc_set_key(struct enc_key_data *enc_state, uint8_t slot,
371-
const struct boot_status *bs)
372-
{
373-
int rc;
374-
375-
rc = bootutil_aes_ctr_set_key(&enc_state[slot].aes_ctr, bs->enckey[slot]);
376-
if (rc != 0) {
377-
boot_enc_drop(enc_state, slot);
378-
return -1;
379-
}
380-
381-
enc_state[slot].valid = 1;
382-
383-
return 0;
384-
}
385-
386-
#define EXPECTED_ENC_LEN BOOT_ENC_TLV_SIZE
387-
388-
#if defined(MCUBOOT_ENCRYPT_RSA)
389-
# define EXPECTED_ENC_TLV IMAGE_TLV_ENC_RSA2048
390-
#elif defined(MCUBOOT_ENCRYPT_KW)
391-
# define EXPECTED_ENC_TLV IMAGE_TLV_ENC_KW
392-
#elif defined(MCUBOOT_ENCRYPT_EC256)
393-
# define EXPECTED_ENC_TLV IMAGE_TLV_ENC_EC256
394-
# define EC_PUBK_INDEX (0)
395-
# define EC_TAG_INDEX (65)
396-
# define EC_CIPHERKEY_INDEX (65 + 32)
397-
_Static_assert(EC_CIPHERKEY_INDEX + BOOT_ENC_KEY_SIZE == EXPECTED_ENC_LEN,
398-
"Please fix ECIES-P256 component indexes");
399-
#elif defined(MCUBOOT_ENCRYPT_X25519)
400-
# define EXPECTED_ENC_TLV IMAGE_TLV_ENC_X25519
401-
# define EC_PUBK_INDEX (0)
402-
# define EC_TAG_INDEX (32)
403-
# define EC_CIPHERKEY_INDEX (32 + 32)
404-
_Static_assert(EC_CIPHERKEY_INDEX + BOOT_ENC_KEY_SIZE == EXPECTED_ENC_LEN,
405-
"Please fix ECIES-X25519 component indexes");
406-
#endif
407-
408380
#if ( (defined(MCUBOOT_ENCRYPT_RSA) && defined(MCUBOOT_USE_MBED_TLS) && !defined(MCUBOOT_USE_PSA_CRYPTO)) || \
409381
(defined(MCUBOOT_ENCRYPT_EC256) && defined(MCUBOOT_USE_MBED_TLS)) )
410382
#if MBEDTLS_VERSION_NUMBER >= 0x03000000
@@ -627,6 +599,7 @@ boot_decrypt_key(const uint8_t *buf, uint8_t *enckey)
627599

628600
return rc;
629601
}
602+
#endif /* CONFIG_BOOT_ED25519_PSA */
630603

631604
/*
632605
* Load encryption key.
@@ -681,6 +654,39 @@ boot_enc_load(struct enc_key_data *enc_state, int slot,
681654
return boot_decrypt_key(buf, bs->enckey[slot]);
682655
}
683656

657+
int
658+
boot_enc_init(struct enc_key_data *enc_state, uint8_t slot)
659+
{
660+
bootutil_aes_ctr_init(&enc_state[slot].aes_ctr);
661+
return 0;
662+
}
663+
664+
int
665+
boot_enc_drop(struct enc_key_data *enc_state, uint8_t slot)
666+
{
667+
bootutil_aes_ctr_drop(&enc_state[slot].aes_ctr);
668+
enc_state[slot].valid = 0;
669+
return 0;
670+
}
671+
672+
int
673+
boot_enc_set_key(struct enc_key_data *enc_state, uint8_t slot,
674+
const struct boot_status *bs)
675+
{
676+
int rc;
677+
678+
rc = bootutil_aes_ctr_set_key(&enc_state[slot].aes_ctr, bs->enckey[slot]);
679+
if (rc != 0) {
680+
boot_enc_drop(enc_state, slot);
681+
return -1;
682+
}
683+
684+
enc_state[slot].valid = 1;
685+
686+
return 0;
687+
}
688+
689+
684690
bool
685691
boot_enc_valid(struct enc_key_data *enc_state, int slot)
686692
{

0 commit comments

Comments
 (0)