@@ -397,17 +397,20 @@ static const uint8_t Secp384r1[] = {0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x22};
397
397
398
398
static inline void bootutil_ecdsa_init (bootutil_ecdsa_context * ctx )
399
399
{
400
- #if !defined(MCUBOOT_BUILTIN_KEY )
400
+ #if !defined(MCUBOOT_BUILTIN_KEY ) && !defined( CONFIG_BOOT_SIGNATURE_USING_ITS )
401
401
ctx -> key_id = PSA_KEY_ID_NULL ;
402
402
ctx -> curve_byte_count = 0 ;
403
403
ctx -> required_algorithm = 0 ;
404
404
405
- #else /* !MCUBOOT_BUILTIN_KEY */
405
+ #else /* !MCUBOOT_BUILTIN_KEY && !CONFIG_BOOT_SIGNATURE_USING_ITS */
406
406
/* The incoming key ID is equal to the image index. The key ID value must be
407
407
* shifted (by one in this case) because zero is reserved (PSA_KEY_ID_NULL)
408
408
* and considered invalid.
409
409
*/
410
+ #if defined(MCUBOOT_BUILTIN_KEY )
410
411
ctx -> key_id ++ ; /* Make sure it is not equal to 0. */
412
+ #endif
413
+
411
414
#if defined(MCUBOOT_SIGN_EC256 )
412
415
ctx -> curve_byte_count = 32 ;
413
416
ctx -> required_algorithm = PSA_ALG_SHA_256 ;
@@ -426,7 +429,7 @@ static inline void bootutil_ecdsa_drop(bootutil_ecdsa_context *ctx)
426
429
}
427
430
}
428
431
429
- #if !defined(MCUBOOT_BUILTIN_KEY )
432
+ #if !defined(MCUBOOT_BUILTIN_KEY ) && !defined( CONFIG_BOOT_SIGNATURE_USING_ITS )
430
433
/*
431
434
* Parse a ECDSA public key with format specified in RFC5280 et al.
432
435
*
@@ -471,8 +474,9 @@ static int bootutil_ecdsa_parse_public_key(bootutil_ecdsa_context *ctx,
471
474
472
475
return (int )psa_import_key (& key_attributes , * cp , key_size , & ctx -> key_id );
473
476
}
474
- #endif /* !MCUBOOT_BUILTIN_KEY */
477
+ #endif /* !MCUBOOT_BUILTIN_KEY && !CONFIG_BOOT_SIGNATURE_USING_ITS */
475
478
479
+ #if !defined(CONFIG_BOOT_SIGNATURE_USING_ITS )
476
480
/* Verify the signature against the provided hash. The signature gets parsed from
477
481
* the encoding first, then PSA Crypto has a dedicated API for ECDSA verification
478
482
*/
@@ -491,6 +495,60 @@ static inline int bootutil_ecdsa_verify(bootutil_ecdsa_context *ctx,
491
495
return (int ) psa_verify_hash (ctx -> key_id , PSA_ALG_ECDSA (ctx -> required_algorithm ),
492
496
hash , hlen , reformatted_signature , 2 * ctx -> curve_byte_count );
493
497
}
498
+ #else /* !CONFIG_BOOT_SIGNATURE_USING_ITS */
499
+
500
+ struct boot_ecdsa_key_info {
501
+ psa_key_id_t key_id ;
502
+ psa_algorithm_t algorithm ;
503
+ };
504
+
505
+ static psa_key_id_t builtin_key_ids [] = {
506
+ 0x40022100 ,
507
+ 0x40022101 ,
508
+ 0x40022102 ,
509
+ 0x40022103
510
+ };
511
+
512
+ #define BOOT_SIGNATURE_BUILTIN_KEY_SLOTS ARRAY_SIZE(builtin_key_ids)
513
+
514
+ static inline int bootutil_ecdsa_verify (bootutil_ecdsa_context * ctx ,
515
+ uint8_t * pk , size_t pk_len ,
516
+ uint8_t * hash , size_t hlen ,
517
+ uint8_t * sig , size_t slen )
518
+ {
519
+ (void )pk ;
520
+ (void )pk_len ;
521
+ (void )slen ;
522
+ psa_status_t status = PSA_ERROR_BAD_STATE ;
523
+
524
+ /* Initialize PSA Crypto */
525
+ status = psa_crypto_init ();
526
+ if (status != PSA_SUCCESS ) {
527
+ BOOT_LOG_ERR ("PSA crypto init failed %d" , status );
528
+ return 1 ;
529
+ }
530
+
531
+ uint8_t reformatted_signature [96 ] = {0 }; /* Enough for P-384 signature sizes */
532
+ parse_signature_from_rfc5480_encoding (sig , ctx -> curve_byte_count , reformatted_signature );
533
+
534
+ status = PSA_ERROR_BAD_STATE ;
535
+
536
+ for (int i = 0 ; i < BOOT_SIGNATURE_BUILTIN_KEY_SLOTS ; ++ i ) {
537
+ psa_key_id_t kid = builtin_key_ids [i ];
538
+
539
+ status = psa_verify_hash (kid , PSA_ALG_ECDSA (ctx -> required_algorithm ),
540
+ hash , hlen , reformatted_signature , 2 * ctx -> curve_byte_count );
541
+ if (status == PSA_SUCCESS ) {
542
+ break ;
543
+ }
544
+ BOOT_LOG_ERR ("ECDSA signature verification failed %d" , status );
545
+ }
546
+
547
+ return (int ) status ;
548
+ }
549
+
550
+ #endif /* !CONFIG_BOOT_SIGNATURE_USING_ITS */
551
+
494
552
#elif defined(MCUBOOT_USE_MBED_TLS )
495
553
496
554
typedef mbedtls_ecdsa_context bootutil_ecdsa_context ;
0 commit comments