]>
git.ipfire.org Git - people/ms/u-boot.git/blob - lib/rsa/rsa-sign.c
2 * Copyright (c) 2013, Google Inc.
4 * SPDX-License-Identifier: GPL-2.0+
12 #include <openssl/rsa.h>
13 #include <openssl/pem.h>
14 #include <openssl/err.h>
15 #include <openssl/ssl.h>
16 #include <openssl/evp.h>
18 #if OPENSSL_VERSION_NUMBER >= 0x10000000L
19 #define HAVE_ERR_REMOVE_THREAD_STATE
22 static int rsa_err(const char *msg
)
24 unsigned long sslErr
= ERR_get_error();
26 fprintf(stderr
, "%s", msg
);
27 fprintf(stderr
, ": %s\n",
28 ERR_error_string(sslErr
, 0));
34 * rsa_get_pub_key() - read a public key from a .crt file
36 * @keydir: Directory containins the key
37 * @name Name of key file (will have a .crt extension)
38 * @rsap Returns RSA object, or NULL on failure
39 * @return 0 if ok, -ve on error (in which case *rsap will be set to NULL)
41 static int rsa_get_pub_key(const char *keydir
, const char *name
, RSA
**rsap
)
51 snprintf(path
, sizeof(path
), "%s/%s.crt", keydir
, name
);
54 fprintf(stderr
, "Couldn't open RSA certificate: '%s': %s\n",
55 path
, strerror(errno
));
59 /* Read the certificate */
61 if (!PEM_read_X509(f
, &cert
, NULL
, NULL
)) {
62 rsa_err("Couldn't read certificate");
67 /* Get the public key from the certificate. */
68 key
= X509_get_pubkey(cert
);
70 rsa_err("Couldn't read public key\n");
75 /* Convert to a RSA_style key. */
76 rsa
= EVP_PKEY_get1_RSA(key
);
78 rsa_err("Couldn't convert to a RSA style key");
99 * rsa_get_priv_key() - read a private key from a .key file
101 * @keydir: Directory containins the key
102 * @name Name of key file (will have a .key extension)
103 * @rsap Returns RSA object, or NULL on failure
104 * @return 0 if ok, -ve on error (in which case *rsap will be set to NULL)
106 static int rsa_get_priv_key(const char *keydir
, const char *name
, RSA
**rsap
)
113 snprintf(path
, sizeof(path
), "%s/%s.key", keydir
, name
);
114 f
= fopen(path
, "r");
116 fprintf(stderr
, "Couldn't open RSA private key: '%s': %s\n",
117 path
, strerror(errno
));
121 rsa
= PEM_read_RSAPrivateKey(f
, 0, NULL
, path
);
123 rsa_err("Failure reading private key");
133 static int rsa_init(void)
137 ret
= SSL_library_init();
139 fprintf(stderr
, "Failure to init SSL library\n");
142 SSL_load_error_strings();
144 OpenSSL_add_all_algorithms();
145 OpenSSL_add_all_digests();
146 OpenSSL_add_all_ciphers();
151 static void rsa_remove(void)
153 CRYPTO_cleanup_all_ex_data();
155 #ifdef HAVE_ERR_REMOVE_THREAD_STATE
156 ERR_remove_thread_state(NULL
);
163 static int rsa_sign_with_key(RSA
*rsa
, struct checksum_algo
*checksum_algo
,
164 const struct image_region region
[], int region_count
,
165 uint8_t **sigp
, uint
*sig_size
)
173 key
= EVP_PKEY_new();
175 return rsa_err("EVP_PKEY object creation failed");
177 if (!EVP_PKEY_set1_RSA(key
, rsa
)) {
178 ret
= rsa_err("EVP key setup failed");
182 size
= EVP_PKEY_size(key
);
185 fprintf(stderr
, "Out of memory for signature (%d bytes)\n",
191 context
= EVP_MD_CTX_create();
193 ret
= rsa_err("EVP context creation failed");
196 EVP_MD_CTX_init(context
);
197 if (!EVP_SignInit(context
, checksum_algo
->calculate_sign())) {
198 ret
= rsa_err("Signer setup failed");
202 for (i
= 0; i
< region_count
; i
++) {
203 if (!EVP_SignUpdate(context
, region
[i
].data
, region
[i
].size
)) {
204 ret
= rsa_err("Signing data failed");
209 if (!EVP_SignFinal(context
, sig
, sig_size
, key
)) {
210 ret
= rsa_err("Could not obtain signature");
213 EVP_MD_CTX_cleanup(context
);
214 EVP_MD_CTX_destroy(context
);
217 debug("Got signature: %d bytes, expected %d\n", *sig_size
, size
);
224 EVP_MD_CTX_destroy(context
);
233 int rsa_sign(struct image_sign_info
*info
,
234 const struct image_region region
[], int region_count
,
235 uint8_t **sigp
, uint
*sig_len
)
244 ret
= rsa_get_priv_key(info
->keydir
, info
->keyname
, &rsa
);
247 ret
= rsa_sign_with_key(rsa
, info
->checksum
, region
,
248 region_count
, sigp
, sig_len
);
265 * rsa_get_exponent(): - Get the public exponent from an RSA key
267 static int rsa_get_exponent(RSA
*key
, uint64_t *e
)
279 if (BN_num_bits(key
->e
) > 64)
282 *e
= BN_get_word(key
->e
);
284 if (BN_num_bits(key
->e
) < 33) {
289 bn_te
= BN_dup(key
->e
);
293 if (!BN_rshift(bn_te
, bn_te
, 32))
296 if (!BN_mask_bits(bn_te
, 32))
299 te
= BN_get_word(bn_te
);
312 * rsa_get_params(): - Get the important parameters of an RSA public key
314 int rsa_get_params(RSA
*key
, uint64_t *exponent
, uint32_t *n0_invp
,
315 BIGNUM
**modulusp
, BIGNUM
**r_squaredp
)
317 BIGNUM
*big1
, *big2
, *big32
, *big2_32
;
318 BIGNUM
*n
, *r
, *r_squared
, *tmp
;
319 BN_CTX
*bn_ctx
= BN_CTX_new();
322 /* Initialize BIGNUMs */
327 r_squared
= BN_new();
331 if (!big1
|| !big2
|| !big32
|| !r
|| !r_squared
|| !tmp
|| !big2_32
||
333 fprintf(stderr
, "Out of memory (bignum)\n");
337 if (0 != rsa_get_exponent(key
, exponent
))
340 if (!BN_copy(n
, key
->n
) || !BN_set_word(big1
, 1L) ||
341 !BN_set_word(big2
, 2L) || !BN_set_word(big32
, 32L))
345 if (!BN_exp(big2_32
, big2
, big32
, bn_ctx
))
348 /* Calculate n0_inv = -1 / n[0] mod 2^32 */
349 if (!BN_mod_inverse(tmp
, n
, big2_32
, bn_ctx
) ||
350 !BN_sub(tmp
, big2_32
, tmp
))
352 *n0_invp
= BN_get_word(tmp
);
354 /* Calculate R = 2^(# of key bits) */
355 if (!BN_set_word(tmp
, BN_num_bits(n
)) ||
356 !BN_exp(r
, big2
, tmp
, bn_ctx
))
359 /* Calculate r_squared = R^2 mod n */
360 if (!BN_copy(r_squared
, r
) ||
361 !BN_mul(tmp
, r_squared
, r
, bn_ctx
) ||
362 !BN_mod(r_squared
, tmp
, n
, bn_ctx
))
366 *r_squaredp
= r_squared
;
375 fprintf(stderr
, "Bignum operations failed\n");
382 static int fdt_add_bignum(void *blob
, int noffset
, const char *prop_name
,
383 BIGNUM
*num
, int num_bits
)
385 int nwords
= num_bits
/ 32;
388 BIGNUM
*tmp
, *big2
, *big32
, *big2_32
;
396 if (!tmp
|| !big2
|| !big32
|| !big2_32
) {
397 fprintf(stderr
, "Out of memory (bignum)\n");
402 fprintf(stderr
, "Out of memory (bignum context)\n");
405 BN_set_word(big2
, 2L);
406 BN_set_word(big32
, 32L);
407 BN_exp(big2_32
, big2
, big32
, ctx
); /* B = 2^32 */
409 size
= nwords
* sizeof(uint32_t);
412 fprintf(stderr
, "Out of memory (%d bytes)\n", size
);
416 /* Write out modulus as big endian array of integers */
417 for (ptr
= buf
+ nwords
- 1; ptr
>= buf
; ptr
--) {
418 BN_mod(tmp
, num
, big2_32
, ctx
); /* n = N mod B */
419 *ptr
= cpu_to_fdt32(BN_get_word(tmp
));
420 BN_rshift(num
, num
, 32); /* N = N/B */
424 * We try signing with successively increasing size values, so this
425 * might fail several times
427 ret
= fdt_setprop(blob
, noffset
, prop_name
, buf
, size
);
429 return -FDT_ERR_NOSPACE
;
439 int rsa_add_verify_data(struct image_sign_info
*info
, void *keydest
)
441 BIGNUM
*modulus
, *r_squared
;
450 debug("%s: Getting verification data\n", __func__
);
451 ret
= rsa_get_pub_key(info
->keydir
, info
->keyname
, &rsa
);
454 ret
= rsa_get_params(rsa
, &exponent
, &n0_inv
, &modulus
, &r_squared
);
457 bits
= BN_num_bits(modulus
);
458 parent
= fdt_subnode_offset(keydest
, 0, FIT_SIG_NODENAME
);
459 if (parent
== -FDT_ERR_NOTFOUND
) {
460 parent
= fdt_add_subnode(keydest
, 0, FIT_SIG_NODENAME
);
463 if (ret
!= -FDT_ERR_NOSPACE
) {
464 fprintf(stderr
, "Couldn't create signature node: %s\n",
465 fdt_strerror(parent
));
472 /* Either create or overwrite the named key node */
473 snprintf(name
, sizeof(name
), "key-%s", info
->keyname
);
474 node
= fdt_subnode_offset(keydest
, parent
, name
);
475 if (node
== -FDT_ERR_NOTFOUND
) {
476 node
= fdt_add_subnode(keydest
, parent
, name
);
479 if (ret
!= -FDT_ERR_NOSPACE
) {
480 fprintf(stderr
, "Could not create key subnode: %s\n",
484 } else if (node
< 0) {
485 fprintf(stderr
, "Cannot select keys parent: %s\n",
491 ret
= fdt_setprop_string(keydest
, node
, "key-name-hint",
495 ret
= fdt_setprop_u32(keydest
, node
, "rsa,num-bits", bits
);
497 ret
= fdt_setprop_u32(keydest
, node
, "rsa,n0-inverse", n0_inv
);
499 ret
= fdt_setprop_u64(keydest
, node
, "rsa,exponent", exponent
);
502 ret
= fdt_add_bignum(keydest
, node
, "rsa,modulus", modulus
,
506 ret
= fdt_add_bignum(keydest
, node
, "rsa,r-squared", r_squared
,
510 ret
= fdt_setprop_string(keydest
, node
, FIT_ALGO_PROP
,
513 if (!ret
&& info
->require_keys
) {
514 ret
= fdt_setprop_string(keydest
, node
, "required",
521 return ret
== -FDT_ERR_NOSPACE
? -ENOSPC
: -EIO
;