blob: 05ac67b82291c4fb0b8b0f80547b18bea6202814 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass19c402a2013-06-13 15:10:02 -07002/*
3 * Copyright (c) 2013, Google Inc.
Simon Glass19c402a2013-06-13 15:10:02 -07004 */
5
6#include "mkimage.h"
7#include <stdio.h>
8#include <string.h>
Simon Glass19c402a2013-06-13 15:10:02 -07009#include <image.h>
10#include <time.h>
Jelle van der Waac3b43282017-05-08 21:31:19 +020011#include <openssl/bn.h>
Simon Glass19c402a2013-06-13 15:10:02 -070012#include <openssl/rsa.h>
13#include <openssl/pem.h>
14#include <openssl/err.h>
15#include <openssl/ssl.h>
16#include <openssl/evp.h>
George McCollisterf1ca1fd2017-01-06 13:14:17 -060017#include <openssl/engine.h>
Simon Glass19c402a2013-06-13 15:10:02 -070018
19#if OPENSSL_VERSION_NUMBER >= 0x10000000L
20#define HAVE_ERR_REMOVE_THREAD_STATE
21#endif
22
Caliph Nomble7ac1a432018-07-25 22:13:03 -040023#if OPENSSL_VERSION_NUMBER < 0x10100000L || \
24 (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x02070000fL)
Jelle van der Waac3b43282017-05-08 21:31:19 +020025static void RSA_get0_key(const RSA *r,
26 const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
27{
28 if (n != NULL)
29 *n = r->n;
30 if (e != NULL)
31 *e = r->e;
32 if (d != NULL)
33 *d = r->d;
34}
35#endif
36
Simon Glass19c402a2013-06-13 15:10:02 -070037static int rsa_err(const char *msg)
38{
39 unsigned long sslErr = ERR_get_error();
40
41 fprintf(stderr, "%s", msg);
42 fprintf(stderr, ": %s\n",
43 ERR_error_string(sslErr, 0));
44
45 return -1;
46}
47
48/**
George McCollisterf1ca1fd2017-01-06 13:14:17 -060049 * rsa_pem_get_pub_key() - read a public key from a .crt file
Simon Glass19c402a2013-06-13 15:10:02 -070050 *
51 * @keydir: Directory containins the key
52 * @name Name of key file (will have a .crt extension)
53 * @rsap Returns RSA object, or NULL on failure
54 * @return 0 if ok, -ve on error (in which case *rsap will be set to NULL)
55 */
George McCollisterf1ca1fd2017-01-06 13:14:17 -060056static int rsa_pem_get_pub_key(const char *keydir, const char *name, RSA **rsap)
Simon Glass19c402a2013-06-13 15:10:02 -070057{
58 char path[1024];
59 EVP_PKEY *key;
60 X509 *cert;
61 RSA *rsa;
62 FILE *f;
63 int ret;
64
65 *rsap = NULL;
66 snprintf(path, sizeof(path), "%s/%s.crt", keydir, name);
67 f = fopen(path, "r");
68 if (!f) {
69 fprintf(stderr, "Couldn't open RSA certificate: '%s': %s\n",
70 path, strerror(errno));
71 return -EACCES;
72 }
73
74 /* Read the certificate */
75 cert = NULL;
76 if (!PEM_read_X509(f, &cert, NULL, NULL)) {
77 rsa_err("Couldn't read certificate");
78 ret = -EINVAL;
79 goto err_cert;
80 }
81
82 /* Get the public key from the certificate. */
83 key = X509_get_pubkey(cert);
84 if (!key) {
85 rsa_err("Couldn't read public key\n");
86 ret = -EINVAL;
87 goto err_pubkey;
88 }
89
90 /* Convert to a RSA_style key. */
91 rsa = EVP_PKEY_get1_RSA(key);
92 if (!rsa) {
93 rsa_err("Couldn't convert to a RSA style key");
Simon Glass54267162014-07-30 10:00:17 -060094 ret = -EINVAL;
Simon Glass19c402a2013-06-13 15:10:02 -070095 goto err_rsa;
96 }
97 fclose(f);
98 EVP_PKEY_free(key);
99 X509_free(cert);
100 *rsap = rsa;
101
102 return 0;
103
104err_rsa:
105 EVP_PKEY_free(key);
106err_pubkey:
107 X509_free(cert);
108err_cert:
109 fclose(f);
110 return ret;
111}
112
113/**
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600114 * rsa_engine_get_pub_key() - read a public key from given engine
Simon Glass19c402a2013-06-13 15:10:02 -0700115 *
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600116 * @keydir: Key prefix
117 * @name Name of key
118 * @engine Engine to use
119 * @rsap Returns RSA object, or NULL on failure
120 * @return 0 if ok, -ve on error (in which case *rsap will be set to NULL)
121 */
122static int rsa_engine_get_pub_key(const char *keydir, const char *name,
123 ENGINE *engine, RSA **rsap)
124{
125 const char *engine_id;
126 char key_id[1024];
127 EVP_PKEY *key;
128 RSA *rsa;
129 int ret;
130
131 *rsap = NULL;
132
133 engine_id = ENGINE_get_id(engine);
134
135 if (engine_id && !strcmp(engine_id, "pkcs11")) {
136 if (keydir)
137 snprintf(key_id, sizeof(key_id),
138 "pkcs11:%s;object=%s;type=public",
139 keydir, name);
140 else
141 snprintf(key_id, sizeof(key_id),
142 "pkcs11:object=%s;type=public",
143 name);
144 } else {
145 fprintf(stderr, "Engine not supported\n");
146 return -ENOTSUP;
147 }
148
149 key = ENGINE_load_public_key(engine, key_id, NULL, NULL);
150 if (!key)
151 return rsa_err("Failure loading public key from engine");
152
153 /* Convert to a RSA_style key. */
154 rsa = EVP_PKEY_get1_RSA(key);
155 if (!rsa) {
156 rsa_err("Couldn't convert to a RSA style key");
157 ret = -EINVAL;
158 goto err_rsa;
159 }
160
161 EVP_PKEY_free(key);
162 *rsap = rsa;
163
164 return 0;
165
166err_rsa:
167 EVP_PKEY_free(key);
168 return ret;
169}
170
171/**
172 * rsa_get_pub_key() - read a public key
173 *
174 * @keydir: Directory containing the key (PEM file) or key prefix (engine)
175 * @name Name of key file (will have a .crt extension)
176 * @engine Engine to use
177 * @rsap Returns RSA object, or NULL on failure
178 * @return 0 if ok, -ve on error (in which case *rsap will be set to NULL)
179 */
180static int rsa_get_pub_key(const char *keydir, const char *name,
181 ENGINE *engine, RSA **rsap)
182{
183 if (engine)
184 return rsa_engine_get_pub_key(keydir, name, engine, rsap);
185 return rsa_pem_get_pub_key(keydir, name, rsap);
186}
187
188/**
189 * rsa_pem_get_priv_key() - read a private key from a .key file
190 *
191 * @keydir: Directory containing the key
Simon Glass19c402a2013-06-13 15:10:02 -0700192 * @name Name of key file (will have a .key extension)
193 * @rsap Returns RSA object, or NULL on failure
194 * @return 0 if ok, -ve on error (in which case *rsap will be set to NULL)
195 */
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600196static int rsa_pem_get_priv_key(const char *keydir, const char *name,
197 RSA **rsap)
Simon Glass19c402a2013-06-13 15:10:02 -0700198{
199 char path[1024];
200 RSA *rsa;
201 FILE *f;
202
203 *rsap = NULL;
204 snprintf(path, sizeof(path), "%s/%s.key", keydir, name);
205 f = fopen(path, "r");
206 if (!f) {
207 fprintf(stderr, "Couldn't open RSA private key: '%s': %s\n",
208 path, strerror(errno));
209 return -ENOENT;
210 }
211
212 rsa = PEM_read_RSAPrivateKey(f, 0, NULL, path);
213 if (!rsa) {
214 rsa_err("Failure reading private key");
215 fclose(f);
216 return -EPROTO;
217 }
218 fclose(f);
219 *rsap = rsa;
220
221 return 0;
222}
223
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600224/**
225 * rsa_engine_get_priv_key() - read a private key from given engine
226 *
227 * @keydir: Key prefix
228 * @name Name of key
229 * @engine Engine to use
230 * @rsap Returns RSA object, or NULL on failure
231 * @return 0 if ok, -ve on error (in which case *rsap will be set to NULL)
232 */
233static int rsa_engine_get_priv_key(const char *keydir, const char *name,
234 ENGINE *engine, RSA **rsap)
235{
236 const char *engine_id;
237 char key_id[1024];
238 EVP_PKEY *key;
239 RSA *rsa;
240 int ret;
241
242 *rsap = NULL;
243
244 engine_id = ENGINE_get_id(engine);
245
246 if (engine_id && !strcmp(engine_id, "pkcs11")) {
247 if (keydir)
248 snprintf(key_id, sizeof(key_id),
249 "pkcs11:%s;object=%s;type=private",
250 keydir, name);
251 else
252 snprintf(key_id, sizeof(key_id),
253 "pkcs11:object=%s;type=private",
254 name);
255 } else {
256 fprintf(stderr, "Engine not supported\n");
257 return -ENOTSUP;
258 }
259
260 key = ENGINE_load_private_key(engine, key_id, NULL, NULL);
261 if (!key)
262 return rsa_err("Failure loading private key from engine");
263
264 /* Convert to a RSA_style key. */
265 rsa = EVP_PKEY_get1_RSA(key);
266 if (!rsa) {
267 rsa_err("Couldn't convert to a RSA style key");
268 ret = -EINVAL;
269 goto err_rsa;
270 }
271
272 EVP_PKEY_free(key);
273 *rsap = rsa;
274
275 return 0;
276
277err_rsa:
278 EVP_PKEY_free(key);
279 return ret;
280}
281
282/**
283 * rsa_get_priv_key() - read a private key
284 *
285 * @keydir: Directory containing the key (PEM file) or key prefix (engine)
286 * @name Name of key
287 * @engine Engine to use for signing
288 * @rsap Returns RSA object, or NULL on failure
289 * @return 0 if ok, -ve on error (in which case *rsap will be set to NULL)
290 */
291static int rsa_get_priv_key(const char *keydir, const char *name,
292 ENGINE *engine, RSA **rsap)
293{
294 if (engine)
295 return rsa_engine_get_priv_key(keydir, name, engine, rsap);
296 return rsa_pem_get_priv_key(keydir, name, rsap);
297}
298
Simon Glass19c402a2013-06-13 15:10:02 -0700299static int rsa_init(void)
300{
301 int ret;
302
Caliph Nomble7ac1a432018-07-25 22:13:03 -0400303#if OPENSSL_VERSION_NUMBER < 0x10100000L || \
304 (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x02070000fL)
Simon Glass19c402a2013-06-13 15:10:02 -0700305 ret = SSL_library_init();
Jelle van der Waac3b43282017-05-08 21:31:19 +0200306#else
307 ret = OPENSSL_init_ssl(0, NULL);
308#endif
Simon Glass19c402a2013-06-13 15:10:02 -0700309 if (!ret) {
310 fprintf(stderr, "Failure to init SSL library\n");
311 return -1;
312 }
Caliph Nomble7ac1a432018-07-25 22:13:03 -0400313#if OPENSSL_VERSION_NUMBER < 0x10100000L || \
314 (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x02070000fL)
Simon Glass19c402a2013-06-13 15:10:02 -0700315 SSL_load_error_strings();
316
317 OpenSSL_add_all_algorithms();
318 OpenSSL_add_all_digests();
319 OpenSSL_add_all_ciphers();
Jelle van der Waac3b43282017-05-08 21:31:19 +0200320#endif
Simon Glass19c402a2013-06-13 15:10:02 -0700321
322 return 0;
323}
324
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600325static int rsa_engine_init(const char *engine_id, ENGINE **pe)
326{
327 ENGINE *e;
328 int ret;
329
330 ENGINE_load_builtin_engines();
331
332 e = ENGINE_by_id(engine_id);
333 if (!e) {
334 fprintf(stderr, "Engine isn't available\n");
335 ret = -1;
336 goto err_engine_by_id;
337 }
338
339 if (!ENGINE_init(e)) {
340 fprintf(stderr, "Couldn't initialize engine\n");
341 ret = -1;
342 goto err_engine_init;
343 }
344
345 if (!ENGINE_set_default_RSA(e)) {
346 fprintf(stderr, "Couldn't set engine as default for RSA\n");
347 ret = -1;
348 goto err_set_rsa;
349 }
350
351 *pe = e;
352
353 return 0;
354
355err_set_rsa:
356 ENGINE_finish(e);
357err_engine_init:
358 ENGINE_free(e);
359err_engine_by_id:
Caliph Nomble7ac1a432018-07-25 22:13:03 -0400360#if OPENSSL_VERSION_NUMBER < 0x10100000L || \
361 (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x02070000fL)
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600362 ENGINE_cleanup();
Jelle van der Waac3b43282017-05-08 21:31:19 +0200363#endif
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600364 return ret;
365}
366
Simon Glass19c402a2013-06-13 15:10:02 -0700367static void rsa_remove(void)
368{
Caliph Nomble7ac1a432018-07-25 22:13:03 -0400369#if OPENSSL_VERSION_NUMBER < 0x10100000L || \
370 (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x02070000fL)
Simon Glass19c402a2013-06-13 15:10:02 -0700371 CRYPTO_cleanup_all_ex_data();
372 ERR_free_strings();
373#ifdef HAVE_ERR_REMOVE_THREAD_STATE
374 ERR_remove_thread_state(NULL);
375#else
376 ERR_remove_state(0);
377#endif
378 EVP_cleanup();
Jelle van der Waac3b43282017-05-08 21:31:19 +0200379#endif
Simon Glass19c402a2013-06-13 15:10:02 -0700380}
381
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600382static void rsa_engine_remove(ENGINE *e)
383{
384 if (e) {
385 ENGINE_finish(e);
386 ENGINE_free(e);
387 }
388}
389
Heiko Schocher646257d2014-03-03 12:19:26 +0100390static int rsa_sign_with_key(RSA *rsa, struct checksum_algo *checksum_algo,
391 const struct image_region region[], int region_count,
392 uint8_t **sigp, uint *sig_size)
Simon Glass19c402a2013-06-13 15:10:02 -0700393{
394 EVP_PKEY *key;
395 EVP_MD_CTX *context;
396 int size, ret = 0;
397 uint8_t *sig;
398 int i;
399
400 key = EVP_PKEY_new();
401 if (!key)
402 return rsa_err("EVP_PKEY object creation failed");
403
404 if (!EVP_PKEY_set1_RSA(key, rsa)) {
405 ret = rsa_err("EVP key setup failed");
406 goto err_set;
407 }
408
409 size = EVP_PKEY_size(key);
410 sig = malloc(size);
411 if (!sig) {
412 fprintf(stderr, "Out of memory for signature (%d bytes)\n",
413 size);
414 ret = -ENOMEM;
415 goto err_alloc;
416 }
417
418 context = EVP_MD_CTX_create();
419 if (!context) {
420 ret = rsa_err("EVP context creation failed");
421 goto err_create;
422 }
423 EVP_MD_CTX_init(context);
Heiko Schocher29a23f92014-03-03 12:19:30 +0100424 if (!EVP_SignInit(context, checksum_algo->calculate_sign())) {
Simon Glass19c402a2013-06-13 15:10:02 -0700425 ret = rsa_err("Signer setup failed");
426 goto err_sign;
427 }
428
429 for (i = 0; i < region_count; i++) {
430 if (!EVP_SignUpdate(context, region[i].data, region[i].size)) {
431 ret = rsa_err("Signing data failed");
432 goto err_sign;
433 }
434 }
435
436 if (!EVP_SignFinal(context, sig, sig_size, key)) {
437 ret = rsa_err("Could not obtain signature");
438 goto err_sign;
439 }
Caliph Nomble7ac1a432018-07-25 22:13:03 -0400440 #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
441 (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x02070000fL)
Jelle van der Waac3b43282017-05-08 21:31:19 +0200442 EVP_MD_CTX_cleanup(context);
443 #else
444 EVP_MD_CTX_reset(context);
445 #endif
Simon Glass19c402a2013-06-13 15:10:02 -0700446 EVP_MD_CTX_destroy(context);
447 EVP_PKEY_free(key);
448
449 debug("Got signature: %d bytes, expected %d\n", *sig_size, size);
450 *sigp = sig;
451 *sig_size = size;
452
453 return 0;
454
455err_sign:
456 EVP_MD_CTX_destroy(context);
457err_create:
458 free(sig);
459err_alloc:
460err_set:
461 EVP_PKEY_free(key);
462 return ret;
463}
464
465int rsa_sign(struct image_sign_info *info,
466 const struct image_region region[], int region_count,
467 uint8_t **sigp, uint *sig_len)
468{
469 RSA *rsa;
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600470 ENGINE *e = NULL;
Simon Glass19c402a2013-06-13 15:10:02 -0700471 int ret;
472
473 ret = rsa_init();
474 if (ret)
475 return ret;
476
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600477 if (info->engine_id) {
478 ret = rsa_engine_init(info->engine_id, &e);
479 if (ret)
480 goto err_engine;
481 }
482
483 ret = rsa_get_priv_key(info->keydir, info->keyname, e, &rsa);
Simon Glass19c402a2013-06-13 15:10:02 -0700484 if (ret)
485 goto err_priv;
Andrew Duda83dd98e2016-11-08 18:53:41 +0000486 ret = rsa_sign_with_key(rsa, info->checksum, region,
Heiko Schocher646257d2014-03-03 12:19:26 +0100487 region_count, sigp, sig_len);
Simon Glass19c402a2013-06-13 15:10:02 -0700488 if (ret)
489 goto err_sign;
490
491 RSA_free(rsa);
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600492 if (info->engine_id)
493 rsa_engine_remove(e);
Simon Glass19c402a2013-06-13 15:10:02 -0700494 rsa_remove();
495
496 return ret;
497
498err_sign:
499 RSA_free(rsa);
500err_priv:
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600501 if (info->engine_id)
502 rsa_engine_remove(e);
503err_engine:
Simon Glass19c402a2013-06-13 15:10:02 -0700504 rsa_remove();
505 return ret;
506}
507
508/*
Michael van der Westhuizene0f2f152014-07-02 10:17:26 +0200509 * rsa_get_exponent(): - Get the public exponent from an RSA key
510 */
511static int rsa_get_exponent(RSA *key, uint64_t *e)
512{
513 int ret;
514 BIGNUM *bn_te;
Jelle van der Waac3b43282017-05-08 21:31:19 +0200515 const BIGNUM *key_e;
Michael van der Westhuizene0f2f152014-07-02 10:17:26 +0200516 uint64_t te;
517
518 ret = -EINVAL;
519 bn_te = NULL;
520
521 if (!e)
522 goto cleanup;
523
Jelle van der Waac3b43282017-05-08 21:31:19 +0200524 RSA_get0_key(key, NULL, &key_e, NULL);
525 if (BN_num_bits(key_e) > 64)
Michael van der Westhuizene0f2f152014-07-02 10:17:26 +0200526 goto cleanup;
527
Jelle van der Waac3b43282017-05-08 21:31:19 +0200528 *e = BN_get_word(key_e);
Michael van der Westhuizene0f2f152014-07-02 10:17:26 +0200529
Jelle van der Waac3b43282017-05-08 21:31:19 +0200530 if (BN_num_bits(key_e) < 33) {
Michael van der Westhuizene0f2f152014-07-02 10:17:26 +0200531 ret = 0;
532 goto cleanup;
533 }
534
Jelle van der Waac3b43282017-05-08 21:31:19 +0200535 bn_te = BN_dup(key_e);
Michael van der Westhuizene0f2f152014-07-02 10:17:26 +0200536 if (!bn_te)
537 goto cleanup;
538
539 if (!BN_rshift(bn_te, bn_te, 32))
540 goto cleanup;
541
542 if (!BN_mask_bits(bn_te, 32))
543 goto cleanup;
544
545 te = BN_get_word(bn_te);
546 te <<= 32;
547 *e |= te;
548 ret = 0;
549
550cleanup:
551 if (bn_te)
552 BN_free(bn_te);
553
554 return ret;
555}
556
557/*
Simon Glass19c402a2013-06-13 15:10:02 -0700558 * rsa_get_params(): - Get the important parameters of an RSA public key
559 */
Michael van der Westhuizene0f2f152014-07-02 10:17:26 +0200560int rsa_get_params(RSA *key, uint64_t *exponent, uint32_t *n0_invp,
561 BIGNUM **modulusp, BIGNUM **r_squaredp)
Simon Glass19c402a2013-06-13 15:10:02 -0700562{
563 BIGNUM *big1, *big2, *big32, *big2_32;
564 BIGNUM *n, *r, *r_squared, *tmp;
Jelle van der Waac3b43282017-05-08 21:31:19 +0200565 const BIGNUM *key_n;
Simon Glass19c402a2013-06-13 15:10:02 -0700566 BN_CTX *bn_ctx = BN_CTX_new();
567 int ret = 0;
568
569 /* Initialize BIGNUMs */
570 big1 = BN_new();
571 big2 = BN_new();
572 big32 = BN_new();
573 r = BN_new();
574 r_squared = BN_new();
575 tmp = BN_new();
576 big2_32 = BN_new();
577 n = BN_new();
578 if (!big1 || !big2 || !big32 || !r || !r_squared || !tmp || !big2_32 ||
579 !n) {
580 fprintf(stderr, "Out of memory (bignum)\n");
581 return -ENOMEM;
582 }
583
Michael van der Westhuizene0f2f152014-07-02 10:17:26 +0200584 if (0 != rsa_get_exponent(key, exponent))
585 ret = -1;
586
Jelle van der Waac3b43282017-05-08 21:31:19 +0200587 RSA_get0_key(key, &key_n, NULL, NULL);
588 if (!BN_copy(n, key_n) || !BN_set_word(big1, 1L) ||
Simon Glass19c402a2013-06-13 15:10:02 -0700589 !BN_set_word(big2, 2L) || !BN_set_word(big32, 32L))
590 ret = -1;
591
592 /* big2_32 = 2^32 */
593 if (!BN_exp(big2_32, big2, big32, bn_ctx))
594 ret = -1;
595
596 /* Calculate n0_inv = -1 / n[0] mod 2^32 */
597 if (!BN_mod_inverse(tmp, n, big2_32, bn_ctx) ||
598 !BN_sub(tmp, big2_32, tmp))
599 ret = -1;
600 *n0_invp = BN_get_word(tmp);
601
602 /* Calculate R = 2^(# of key bits) */
603 if (!BN_set_word(tmp, BN_num_bits(n)) ||
604 !BN_exp(r, big2, tmp, bn_ctx))
605 ret = -1;
606
607 /* Calculate r_squared = R^2 mod n */
608 if (!BN_copy(r_squared, r) ||
609 !BN_mul(tmp, r_squared, r, bn_ctx) ||
610 !BN_mod(r_squared, tmp, n, bn_ctx))
611 ret = -1;
612
613 *modulusp = n;
614 *r_squaredp = r_squared;
615
616 BN_free(big1);
617 BN_free(big2);
618 BN_free(big32);
619 BN_free(r);
620 BN_free(tmp);
621 BN_free(big2_32);
622 if (ret) {
623 fprintf(stderr, "Bignum operations failed\n");
624 return -ENOMEM;
625 }
626
627 return ret;
628}
629
630static int fdt_add_bignum(void *blob, int noffset, const char *prop_name,
631 BIGNUM *num, int num_bits)
632{
633 int nwords = num_bits / 32;
634 int size;
635 uint32_t *buf, *ptr;
636 BIGNUM *tmp, *big2, *big32, *big2_32;
637 BN_CTX *ctx;
638 int ret;
639
640 tmp = BN_new();
641 big2 = BN_new();
642 big32 = BN_new();
643 big2_32 = BN_new();
Simon Glass8a682e02018-06-12 00:05:00 -0600644
645 /*
646 * Note: This code assumes that all of the above succeed, or all fail.
647 * In practice memory allocations generally do not fail (unless the
648 * process is killed), so it does not seem worth handling each of these
649 * as a separate case. Technicaly this could leak memory on failure,
650 * but a) it won't happen in practice, and b) it doesn't matter as we
651 * will immediately exit with a failure code.
652 */
Simon Glass19c402a2013-06-13 15:10:02 -0700653 if (!tmp || !big2 || !big32 || !big2_32) {
654 fprintf(stderr, "Out of memory (bignum)\n");
655 return -ENOMEM;
656 }
657 ctx = BN_CTX_new();
658 if (!tmp) {
659 fprintf(stderr, "Out of memory (bignum context)\n");
660 return -ENOMEM;
661 }
662 BN_set_word(big2, 2L);
663 BN_set_word(big32, 32L);
664 BN_exp(big2_32, big2, big32, ctx); /* B = 2^32 */
665
666 size = nwords * sizeof(uint32_t);
667 buf = malloc(size);
668 if (!buf) {
669 fprintf(stderr, "Out of memory (%d bytes)\n", size);
670 return -ENOMEM;
671 }
672
673 /* Write out modulus as big endian array of integers */
674 for (ptr = buf + nwords - 1; ptr >= buf; ptr--) {
675 BN_mod(tmp, num, big2_32, ctx); /* n = N mod B */
676 *ptr = cpu_to_fdt32(BN_get_word(tmp));
677 BN_rshift(num, num, 32); /* N = N/B */
678 }
679
mario.six@gdsys.cc713fb2d2016-07-22 08:58:40 +0200680 /*
681 * We try signing with successively increasing size values, so this
682 * might fail several times
683 */
Simon Glass19c402a2013-06-13 15:10:02 -0700684 ret = fdt_setprop(blob, noffset, prop_name, buf, size);
Simon Glass19c402a2013-06-13 15:10:02 -0700685 free(buf);
686 BN_free(tmp);
687 BN_free(big2);
688 BN_free(big32);
689 BN_free(big2_32);
690
Simon Glass8a682e02018-06-12 00:05:00 -0600691 return ret ? -FDT_ERR_NOSPACE : 0;
Simon Glass19c402a2013-06-13 15:10:02 -0700692}
693
694int rsa_add_verify_data(struct image_sign_info *info, void *keydest)
695{
696 BIGNUM *modulus, *r_squared;
Michael van der Westhuizene0f2f152014-07-02 10:17:26 +0200697 uint64_t exponent;
Simon Glass19c402a2013-06-13 15:10:02 -0700698 uint32_t n0_inv;
699 int parent, node;
700 char name[100];
701 int ret;
702 int bits;
703 RSA *rsa;
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600704 ENGINE *e = NULL;
Simon Glass19c402a2013-06-13 15:10:02 -0700705
706 debug("%s: Getting verification data\n", __func__);
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600707 if (info->engine_id) {
708 ret = rsa_engine_init(info->engine_id, &e);
709 if (ret)
710 return ret;
711 }
712 ret = rsa_get_pub_key(info->keydir, info->keyname, e, &rsa);
Simon Glass19c402a2013-06-13 15:10:02 -0700713 if (ret)
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600714 goto err_get_pub_key;
Michael van der Westhuizene0f2f152014-07-02 10:17:26 +0200715 ret = rsa_get_params(rsa, &exponent, &n0_inv, &modulus, &r_squared);
Simon Glass19c402a2013-06-13 15:10:02 -0700716 if (ret)
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600717 goto err_get_params;
Simon Glass19c402a2013-06-13 15:10:02 -0700718 bits = BN_num_bits(modulus);
719 parent = fdt_subnode_offset(keydest, 0, FIT_SIG_NODENAME);
720 if (parent == -FDT_ERR_NOTFOUND) {
721 parent = fdt_add_subnode(keydest, 0, FIT_SIG_NODENAME);
722 if (parent < 0) {
Simon Glass597a8b22014-06-12 07:24:42 -0600723 ret = parent;
724 if (ret != -FDT_ERR_NOSPACE) {
725 fprintf(stderr, "Couldn't create signature node: %s\n",
726 fdt_strerror(parent));
727 }
Simon Glass19c402a2013-06-13 15:10:02 -0700728 }
729 }
Simon Glass597a8b22014-06-12 07:24:42 -0600730 if (ret)
731 goto done;
Simon Glass19c402a2013-06-13 15:10:02 -0700732
733 /* Either create or overwrite the named key node */
734 snprintf(name, sizeof(name), "key-%s", info->keyname);
735 node = fdt_subnode_offset(keydest, parent, name);
736 if (node == -FDT_ERR_NOTFOUND) {
737 node = fdt_add_subnode(keydest, parent, name);
738 if (node < 0) {
Simon Glass597a8b22014-06-12 07:24:42 -0600739 ret = node;
740 if (ret != -FDT_ERR_NOSPACE) {
741 fprintf(stderr, "Could not create key subnode: %s\n",
742 fdt_strerror(node));
743 }
Simon Glass19c402a2013-06-13 15:10:02 -0700744 }
745 } else if (node < 0) {
746 fprintf(stderr, "Cannot select keys parent: %s\n",
747 fdt_strerror(node));
Simon Glass597a8b22014-06-12 07:24:42 -0600748 ret = node;
Simon Glass19c402a2013-06-13 15:10:02 -0700749 }
750
Simon Glass597a8b22014-06-12 07:24:42 -0600751 if (!ret) {
752 ret = fdt_setprop_string(keydest, node, "key-name-hint",
Simon Glass19c402a2013-06-13 15:10:02 -0700753 info->keyname);
Simon Glass597a8b22014-06-12 07:24:42 -0600754 }
Simon Glass4f427a42014-06-02 22:04:51 -0600755 if (!ret)
756 ret = fdt_setprop_u32(keydest, node, "rsa,num-bits", bits);
757 if (!ret)
758 ret = fdt_setprop_u32(keydest, node, "rsa,n0-inverse", n0_inv);
759 if (!ret) {
Michael van der Westhuizene0f2f152014-07-02 10:17:26 +0200760 ret = fdt_setprop_u64(keydest, node, "rsa,exponent", exponent);
761 }
762 if (!ret) {
Simon Glass4f427a42014-06-02 22:04:51 -0600763 ret = fdt_add_bignum(keydest, node, "rsa,modulus", modulus,
764 bits);
765 }
766 if (!ret) {
767 ret = fdt_add_bignum(keydest, node, "rsa,r-squared", r_squared,
768 bits);
769 }
770 if (!ret) {
771 ret = fdt_setprop_string(keydest, node, FIT_ALGO_PROP,
Andrew Duda83dd98e2016-11-08 18:53:41 +0000772 info->name);
Simon Glass4f427a42014-06-02 22:04:51 -0600773 }
mario.six@gdsys.cc2b9ec762016-07-19 11:07:07 +0200774 if (!ret && info->require_keys) {
Simon Glass4f427a42014-06-02 22:04:51 -0600775 ret = fdt_setprop_string(keydest, node, "required",
776 info->require_keys);
Simon Glass19c402a2013-06-13 15:10:02 -0700777 }
Simon Glass597a8b22014-06-12 07:24:42 -0600778done:
Simon Glass19c402a2013-06-13 15:10:02 -0700779 BN_free(modulus);
780 BN_free(r_squared);
781 if (ret)
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600782 ret = ret == -FDT_ERR_NOSPACE ? -ENOSPC : -EIO;
783err_get_params:
784 RSA_free(rsa);
785err_get_pub_key:
786 if (info->engine_id)
787 rsa_engine_remove(e);
Simon Glass19c402a2013-06-13 15:10:02 -0700788
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600789 return ret;
Simon Glass19c402a2013-06-13 15:10:02 -0700790}