Skip to content

Commit 9143522

Browse files
jgriffithsDaniel Newton
andcommitted
attestation: misc cleanups
- fix SENSITIVE_POP in attestation_initialise() - fix attestation_can_be_initialised() with CONFIG_DEBUG_MODE - avoid uneeded reloads of attestation data - ensure keys are always initialized before being freed Co-authored-by: Daniel Newton <dnewton@blockstream.com>
1 parent a8e2554 commit 9143522

1 file changed

Lines changed: 31 additions & 44 deletions

File tree

main/attestation/attestation.c

Lines changed: 31 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ static bool import_rsa_key(mbedtls_pk_context* pk, const char* pem, const size_t
178178
JADE_ASSERT(pem_len);
179179
JADE_ASSERT(pem[pem_len] == '\0'); // bytes passed to parser must include nul-terminator
180180

181+
mbedtls_pk_init(pk);
181182
int rc;
182183
if (is_private_key) {
183184
JADE_LOGI("Importing RSA private key from pem of length %u", pem_len);
@@ -257,17 +258,17 @@ static int rsa_rsassa_pkcs1_v15_encode(
257258
if (md_alg != MBEDTLS_MD_NONE) {
258259
const mbedtls_md_info_t* md_info = mbedtls_md_info_from_type(md_alg);
259260
if (md_info == NULL)
260-
return (MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
261+
return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
261262

262263
if (mbedtls_oid_get_oid_by_md(md_alg, &oid, &oid_size) != 0)
263-
return (MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
264+
return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
264265

265266
hashlen = mbedtls_md_get_size(md_info);
266267

267268
/* Double-check that 8 + hashlen + oid_size can be used as a
268269
* 1-byte ASN.1 length encoding and that there's no overflow. */
269270
if (8 + hashlen + oid_size >= 0x80 || 10 + hashlen < hashlen || 10 + hashlen + oid_size < 10 + hashlen)
270-
return (MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
271+
return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
271272

272273
/*
273274
* Static bounds check:
@@ -278,19 +279,19 @@ static int rsa_rsassa_pkcs1_v15_encode(
278279
* - Need oid_size bytes for hash alg OID.
279280
*/
280281
if (nb_pad < 10 + hashlen + oid_size)
281-
return (MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
282+
return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
282283
nb_pad -= 10 + hashlen + oid_size;
283284
} else {
284285
if (nb_pad < hashlen)
285-
return (MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
286+
return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
286287

287288
nb_pad -= hashlen;
288289
}
289290

290291
/* Need space for signature header and padding delimiter (3 bytes),
291292
* and 8 bytes for the minimal padding */
292293
if (nb_pad < 3 + 8)
293-
return (MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
294+
return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
294295
nb_pad -= 3;
295296

296297
/* Now nb_pad is the amount of memory to be filled
@@ -306,7 +307,7 @@ static int rsa_rsassa_pkcs1_v15_encode(
306307
/* Are we signing raw data? */
307308
if (md_alg == MBEDTLS_MD_NONE) {
308309
memcpy(p, hash, hashlen);
309-
return (0);
310+
return 0;
310311
}
311312

312313
/* Signing hashed data, add corresponding ASN.1 structure
@@ -341,10 +342,10 @@ static int rsa_rsassa_pkcs1_v15_encode(
341342
* after the initial bounds check. */
342343
if (p != dst + dst_len) {
343344
mbedtls_platform_zeroize(dst, dst_len);
344-
return (MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
345+
return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
345346
}
346347

347-
return (0);
348+
return 0;
348349
}
349350

350351
static void calc_rinv_mprime(const mbedtls_mpi* N, mbedtls_mpi* rinv, uint32_t* mprime)
@@ -418,8 +419,8 @@ static void rsa_ctx_to_ds_params(mbedtls_rsa_context* rsa, esp_ds_p_data_t* para
418419

419420
bool attestation_can_be_initialised(void)
420421
{
422+
#if !defined(CONFIG_SECURE_BOOT) && !defined(CONFIG_DEBUG_MODE)
421423
// Only 'secure-boot' units can be set-up with attestation
422-
#ifndef CONFIG_SECURE_BOOT
423424
return false;
424425
#else
425426
// Check efuse is currently unused (ie. 'user', [or already set if in dev mode])
@@ -432,26 +433,21 @@ bool attestation_can_be_initialised(void)
432433
if (purpose != ESP_EFUSE_KEY_PURPOSE_USER) {
433434
return false;
434435
}
435-
#endif
436-
437-
#ifndef ALLOW_REINITIALISE
438436
// Check efuse is both readable and writable
439437
if (esp_efuse_get_key_dis_read(JADE_ATTEST_EFUSE) || esp_efuse_get_key_dis_write(JADE_ATTEST_EFUSE)) {
440438
return false;
441439
}
442440
#endif
443441

444-
// Check efuses can still be made read-only
445-
if (esp_efuse_read_field_bit(ESP_EFUSE_WR_DIS_RD_DIS)) {
446-
return false;
447-
}
448-
449-
return true;
442+
// Check efuse can still be made read-only (i.e. is not set)
443+
return !esp_efuse_read_field_bit(ESP_EFUSE_WR_DIS_RD_DIS);
450444
#endif // CONFIG_SECURE_BOOT
451445
}
452446

453-
bool attestation_initialised(void)
447+
static bool attestation_initialised_impl(attestation_data_t* attestation_data)
454448
{
449+
JADE_ASSERT(attestation_data);
450+
455451
// Check efuse is set to expected purpose
456452
const esp_efuse_purpose_t purpose = esp_efuse_get_key_purpose(JADE_ATTEST_EFUSE);
457453
if (purpose != ESP_EFUSE_KEY_PURPOSE_HMAC_DOWN_DIGITAL_SIGNATURE) {
@@ -466,13 +462,13 @@ bool attestation_initialised(void)
466462
#endif
467463

468464
// Check saved attestation data
469-
attestation_data_t attestation_data = {};
470-
if (!load_attestation_data(&attestation_data)
471-
|| attestation_data.encrypted_ds_params.rsa_length != ESP_DS_RSA_4096) {
472-
return false;
473-
}
465+
return load_attestation_data(attestation_data);
466+
}
474467

475-
return true;
468+
bool attestation_initialised(void)
469+
{
470+
attestation_data_t attestation_data = {};
471+
return attestation_initialised_impl(&attestation_data);
476472
}
477473

478474
bool attestation_initialise(const char* privkey_pem, const size_t privkey_pem_len, const char* ext_pubkey_pem,
@@ -503,19 +499,18 @@ bool attestation_initialise(const char* privkey_pem, const size_t privkey_pem_le
503499

504500
uint8_t hmac_key[32];
505501
SENSITIVE_PUSH(hmac_key, sizeof(hmac_key));
506-
get_random(hmac_key, sizeof(hmac_key));
507502

508503
#ifdef ALLOW_REINITIALISE
509504
// Use fixed key data for dev period
510-
const uint8_t key_data[32] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
511-
0x17, 0x18, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38 };
512-
for (size_t i = 0; i < sizeof(hmac_key); ++i) {
513-
hmac_key[i] = key_data[i];
514-
}
505+
const uint8_t key_data[sizeof(hmac_key)]
506+
= { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x21, 0x22,
507+
0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38 };
508+
memcpy(hmac_key, key_data, sizeof(hmac_key));
509+
#else
510+
get_random(hmac_key, sizeof(hmac_key));
515511
#endif
516512

517513
mbedtls_pk_context pk;
518-
mbedtls_pk_init(&pk);
519514
SENSITIVE_PUSH(&pk, sizeof(pk));
520515

521516
const bool is_private_key = true;
@@ -610,9 +605,9 @@ bool attestation_initialise(const char* privkey_pem, const size_t privkey_pem_le
610605
retval = true;
611606

612607
cleanup:
613-
SENSITIVE_POP(hmac_key);
614608
mbedtls_pk_free(&pk);
615609
SENSITIVE_POP(&pk);
610+
SENSITIVE_POP(hmac_key);
616611

617612
return retval;
618613
}
@@ -633,16 +628,9 @@ bool attestation_sign_challenge(const uint8_t* challenge, const size_t challenge
633628
JADE_INIT_OUT_SIZE(ext_sig_written);
634629

635630
// Check to see if attestation data initialised
636-
if (!attestation_initialised()) {
637-
JADE_LOGE("Attestation data not properly initialised");
638-
return false;
639-
}
640-
641-
// Load attestation data
642631
attestation_data_t attestation_data = {};
643-
if (!load_attestation_data(&attestation_data)
644-
|| attestation_data.encrypted_ds_params.rsa_length != ESP_DS_RSA_4096) {
645-
JADE_LOGE("Failed to load attestation data");
632+
if (!attestation_initialised_impl(&attestation_data)) {
633+
JADE_LOGE("Attestation data not properly initialised");
646634
return false;
647635
}
648636

@@ -706,7 +694,6 @@ bool attestation_verify(const uint8_t* challenge, const size_t challenge_len, co
706694
// Import pubkeys
707695
const bool is_private_key = false;
708696
mbedtls_pk_context pk;
709-
mbedtls_pk_init(&pk);
710697

711698
// Import RSA public key for signer and check signature over challenge data
712699
if (!import_rsa_key(&pk, pubkey_pem, pubkey_pem_len, is_private_key)) {

0 commit comments

Comments
 (0)