Skip to content

Commit d957e03

Browse files
Jaska Uimonenlgirdwood
authored andcommitted
rimage: make ace15 signing to support openssl3
Ace signing is currently missing openssl3 support, so let's add it. Signed-off-by: Jaska Uimonen <jaska.uimonen@intel.com>
1 parent a1b6e6d commit d957e03

2 files changed

Lines changed: 85 additions & 73 deletions

File tree

src/include/rimage/rimage.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,9 @@ int pkcs_v1_5_sign_man_v2_5(struct image *image,
190190
void *ptr1, unsigned int size1, void *ptr2,
191191
unsigned int size2);
192192
int pkcs_v1_5_sign_man_ace_v1_5(struct image *image,
193-
struct fw_image_manifest_ace_v1_5 *man,
194-
void *ptr1, unsigned int size1, void *ptr2,
195-
unsigned int size2);
193+
struct fw_image_manifest_ace_v1_5 *man,
194+
void *ptr1, unsigned int size1, void *ptr2,
195+
unsigned int size2);
196196

197197
int verify_image(struct image *image);
198198
int ri_manifest_verify_v1_5(struct image *image);
@@ -209,6 +209,10 @@ int pkcs_v1_5_verify_man_v2_5(struct image *image,
209209
struct fw_image_manifest_v2_5 *man,
210210
void *ptr1, unsigned int size1, void *ptr2,
211211
unsigned int size2);
212+
int pkcs_v1_5_verify_man_ace_v1_5(struct image *image,
213+
struct fw_image_manifest_ace_v1_5 *man,
214+
void *ptr1, unsigned int size1, void *ptr2,
215+
unsigned int size2);
212216

213217
int resign_image(struct image *image);
214218
int get_key_size(struct image *image);

src/pkcs1_5.c

Lines changed: 78 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
enum manver {
3030
V15 = 0,
3131
V18 = 1,
32-
V25 = 2
32+
V25 = 2,
33+
VACE15 = 3
3334
};
3435

3536
static void bytes_swap(uint8_t *ptr, uint32_t size)
@@ -183,6 +184,8 @@ static int rimage_sign(EVP_PKEY *privkey, struct image *image, enum manver ver,
183184
signature, &siglen, priv_rsa);
184185
break;
185186
case V25:
187+
/* fallthrough */
188+
case VACE15:
186189
ret = RSA_padding_add_PKCS1_PSS(priv_rsa, sig, digest, image->md,
187190
/* salt length */ 32);
188191
if (ret > 0)
@@ -212,7 +215,7 @@ static int rimage_sign(EVP_PKEY *privkey, struct image *image, enum manver ver,
212215
if (ret <= 0)
213216
goto out;
214217

215-
if (ver == V25) {
218+
if (ver == V25 || ver == VACE15) {
216219
ret = EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PSS_PADDING);
217220
if (ret <= 0) {
218221
fprintf(stderr, "error: failed to set rsa padding\n");
@@ -281,6 +284,8 @@ static int rimage_verify(EVP_PKEY *privkey, struct image *image, enum manver ver
281284
}
282285
break;
283286
case V25:
287+
/* fallthrough */
288+
case VACE15:
284289
/* decrypt signature */
285290
ret = RSA_public_decrypt(RSA_size(priv_rsa), signature, sig, priv_rsa,
286291
RSA_NO_PADDING);
@@ -339,6 +344,8 @@ static int rimage_verify(EVP_PKEY *privkey, struct image *image, enum manver ver
339344

340345
break;
341346
case V25:
347+
/* fallthrough */
348+
case VACE15:
342349
ret = EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PSS_PADDING);
343350
if (ret <= 0)
344351
goto out;
@@ -523,60 +530,27 @@ int pkcs_v1_5_sign_man_v1_8(struct image *image,
523530
return ret;
524531
}
525532

526-
/*
527-
* RSA signature of manifest. The signature is an RSA PSS
528-
* of the entire manifest structure, including all
529-
* extensions, and excluding the last 3 fields of the
530-
* manifest header (Public Key, Exponent and Signature).
531-
*/
532-
#if OPENSSL_VERSION_NUMBER < 0x30000000L
533-
int pkcs_v1_5_sign_man_ace_v1_5(struct image *image,
534-
struct fw_image_manifest_ace_v1_5 *man,
533+
int pkcs_v1_5_sign_man_v2_5(struct image *image,
534+
struct fw_image_manifest_v2_5 *man,
535535
void *ptr1, unsigned int size1, void *ptr2,
536536
unsigned int size2)
537537
{
538-
RSA *priv_rsa = NULL;
539538
EVP_PKEY *privkey;
540-
FILE *fp;
541-
const BIGNUM *n, *e, *d;
542539
unsigned char digest[SHA384_DIGEST_LENGTH];
543540
unsigned char mod[MAN_RSA_KEY_MODULUS_LEN_2_5];
544-
unsigned char sig[MAN_RSA_SIGNATURE_LEN_2_5];
545-
char path[256];
546541
int ret = -EINVAL, i;
547542

548543
#if DEBUG_PKCS
549544
fprintf(stdout, "offsets 0x%lx size 0x%x offset 0x%lx size 0x%x\n",
550545
ptr1 - (void *)man, size1, ptr2 - (void *)man, size2);
551546
#endif
552547

553-
/* require private key */
554-
if (!image->key_name) {
555-
return -EINVAL;
556-
}
557-
558-
/* create new PSS key */
559-
privkey = EVP_PKEY_new();
560-
if (!privkey)
561-
return -ENOMEM;
562-
563-
/* load in RSA private key from PEM file */
564-
memset(path, 0, sizeof(path));
565-
strncpy(path, image->key_name, sizeof(path) - 1);
566-
567-
fprintf(stdout, " pkcs: PSS signing with key %s\n", path);
568-
fp = fopen(path, "rb");
569-
if (!fp) {
570-
fprintf(stderr, "error: can't open file %s %d\n",
571-
path, -errno);
572-
return -errno;
573-
}
574-
PEM_read_PrivateKey(fp, &privkey, NULL, NULL);
575-
fclose(fp);
548+
ret = rimage_read_key(&privkey, image);
549+
if (ret < 0)
550+
return ret;
576551

577552
/* validate RSA private key */
578-
priv_rsa = EVP_PKEY_get1_RSA(privkey);
579-
if (RSA_check_key(priv_rsa)) {
553+
if (rimage_check_key(privkey) > 0) {
580554
fprintf(stdout, " pkcs: RSA private key is valid.\n");
581555
} else {
582556
fprintf(stderr, "error: validating RSA private key.\n");
@@ -595,25 +569,15 @@ int pkcs_v1_5_sign_man_ace_v1_5(struct image *image,
595569
fprintf(stdout, "\n");
596570

597571
/* sign the manifest */
598-
ret = RSA_padding_add_PKCS1_PSS(priv_rsa, sig,
599-
digest, image->md, /* salt length */ 32);
600-
if (ret <= 0) {
601-
ERR_error_string(ERR_get_error(), path);
602-
fprintf(stderr, "error: failed to sign manifest %s\n", path);
603-
}
604-
605-
/* encrypt the signature using the private key */
606-
ret = RSA_private_encrypt(RSA_size(priv_rsa), sig,
607-
(unsigned char *)man->css.signature, priv_rsa, RSA_NO_PADDING);
572+
ret = rimage_sign(privkey, image, V25, digest,
573+
(unsigned char *)man->css.signature);
608574
if (ret <= 0) {
609-
ERR_error_string(ERR_get_error(), path);
610-
fprintf(stderr, "error: failed to encrypt signature %s\n", path);
575+
fprintf(stderr, "error: failed to sign manifest\n");
576+
return ret;
611577
}
612578

613579
/* copy public key modulus and exponent to manifest */
614-
RSA_get0_key(priv_rsa, &n, &e, &d);
615-
BN_bn2bin(n, mod);
616-
BN_bn2bin(e, (unsigned char *)man->css.exponent);
580+
rimage_set_modexp(privkey, mod, (unsigned char *)man->css.exponent);
617581

618582
/* modulus is reversed */
619583
for (i = 0; i < MAN_RSA_KEY_MODULUS_LEN_2_5; i++)
@@ -625,20 +589,11 @@ int pkcs_v1_5_sign_man_ace_v1_5(struct image *image,
625589
EVP_PKEY_free(privkey);
626590
return ret;
627591
}
628-
#else
629-
int pkcs_v1_5_sign_man_ace_v1_5(struct image *image,
630-
struct fw_image_manifest_ace_v1_5 *man,
631-
void *ptr1, unsigned int size1, void *ptr2,
632-
unsigned int size2)
633-
{
634-
return -EINVAL;
635-
}
636-
#endif
637592

638-
int pkcs_v1_5_sign_man_v2_5(struct image *image,
639-
struct fw_image_manifest_v2_5 *man,
640-
void *ptr1, unsigned int size1, void *ptr2,
641-
unsigned int size2)
593+
int pkcs_v1_5_sign_man_ace_v1_5(struct image *image,
594+
struct fw_image_manifest_ace_v1_5 *man,
595+
void *ptr1, unsigned int size1, void *ptr2,
596+
unsigned int size2)
642597
{
643598
EVP_PKEY *privkey;
644599
unsigned char digest[SHA384_DIGEST_LENGTH];
@@ -674,7 +629,7 @@ int pkcs_v1_5_sign_man_v2_5(struct image *image,
674629
fprintf(stdout, "\n");
675630

676631
/* sign the manifest */
677-
ret = rimage_sign(privkey, image, V25, digest,
632+
ret = rimage_sign(privkey, image, VACE15, digest,
678633
(unsigned char *)man->css.signature);
679634
if (ret <= 0) {
680635
fprintf(stderr, "error: failed to sign manifest\n");
@@ -935,6 +890,59 @@ int pkcs_v1_5_verify_man_v2_5(struct image *image,
935890
return ret;
936891
}
937892

893+
int pkcs_v1_5_verify_man_ace_v1_5(struct image *image,
894+
struct fw_image_manifest_ace_v1_5 *man,
895+
void *ptr1, unsigned int size1, void *ptr2,
896+
unsigned int size2)
897+
{
898+
EVP_PKEY *privkey;
899+
unsigned char digest[SHA384_DIGEST_LENGTH];
900+
int ret = -EINVAL, i;
901+
902+
#if DEBUG_PKCS
903+
fprintf(stdout, "offsets 0x%lx size 0x%x offset 0x%lx size 0x%x\n",
904+
ptr1 - (void *)man, size1, ptr2 - (void *)man, size2);
905+
#endif
906+
907+
ret = rimage_read_key(&privkey, image);
908+
if (ret < 0)
909+
return ret;
910+
911+
/* validate RSA private key */
912+
if (rimage_check_key(privkey) > 0) {
913+
fprintf(stdout, " pkcs: RSA private key is valid.\n");
914+
} else {
915+
fprintf(stderr, "error: validating RSA private key.\n");
916+
return -EINVAL;
917+
}
918+
919+
/* calculate the digest - SHA384 on CAVS2_5+ */
920+
module_sha384_create(image);
921+
module_sha_update(image, ptr1, size1);
922+
module_sha_update(image, ptr2, size2);
923+
module_sha_complete(image, digest);
924+
925+
fprintf(stdout, " pkcs: digest for manifest is ");
926+
for (i = 0; i < SHA384_DIGEST_LENGTH; i++)
927+
fprintf(stdout, "%02x", digest[i]);
928+
fprintf(stdout, "\n");
929+
930+
/* signature is reversed, swap it */
931+
bytes_swap(man->css.signature, sizeof(man->css.signature));
932+
933+
/* verify */
934+
ret = rimage_verify(privkey, image, VACE15, digest,
935+
(unsigned char *)man->css.signature);
936+
937+
if (ret <= 0)
938+
fprintf(stderr, "error: failed to verify manifest\n");
939+
else
940+
fprintf(stdout, "pkcs: signature is valid !\n");
941+
942+
EVP_PKEY_free(privkey);
943+
return ret;
944+
}
945+
938946
int ri_manifest_verify_v1_5(struct image *image)
939947
{
940948
struct fw_image_manifest_v1_5 *man = image->fw_image;

0 commit comments

Comments
 (0)