lib/rsa: Make fdt_add_bignum() available outside of RSA code
[platform/kernel/u-boot.git] / lib / rsa / rsa-sign.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2013, Google Inc.
4  */
5
6 #include "mkimage.h"
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <image.h>
11 #include <time.h>
12 #include <u-boot/fdt-libcrypto.h>
13 #include <openssl/bn.h>
14 #include <openssl/rsa.h>
15 #include <openssl/pem.h>
16 #include <openssl/err.h>
17 #include <openssl/ssl.h>
18 #include <openssl/evp.h>
19 #include <openssl/engine.h>
20
21 #if OPENSSL_VERSION_NUMBER >= 0x10000000L
22 #define HAVE_ERR_REMOVE_THREAD_STATE
23 #endif
24
25 #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
26         (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x02070000fL)
27 static void RSA_get0_key(const RSA *r,
28                  const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
29 {
30    if (n != NULL)
31        *n = r->n;
32    if (e != NULL)
33        *e = r->e;
34    if (d != NULL)
35        *d = r->d;
36 }
37 #endif
38
39 static int rsa_err(const char *msg)
40 {
41         unsigned long sslErr = ERR_get_error();
42
43         fprintf(stderr, "%s", msg);
44         fprintf(stderr, ": %s\n",
45                 ERR_error_string(sslErr, 0));
46
47         return -1;
48 }
49
50 /**
51  * rsa_pem_get_pub_key() - read a public key from a .crt file
52  *
53  * @keydir:     Directory containins the key
54  * @name        Name of key file (will have a .crt extension)
55  * @rsap        Returns RSA object, or NULL on failure
56  * @return 0 if ok, -ve on error (in which case *rsap will be set to NULL)
57  */
58 static int rsa_pem_get_pub_key(const char *keydir, const char *name, RSA **rsap)
59 {
60         char path[1024];
61         EVP_PKEY *key;
62         X509 *cert;
63         RSA *rsa;
64         FILE *f;
65         int ret;
66
67         *rsap = NULL;
68         snprintf(path, sizeof(path), "%s/%s.crt", keydir, name);
69         f = fopen(path, "r");
70         if (!f) {
71                 fprintf(stderr, "Couldn't open RSA certificate: '%s': %s\n",
72                         path, strerror(errno));
73                 return -EACCES;
74         }
75
76         /* Read the certificate */
77         cert = NULL;
78         if (!PEM_read_X509(f, &cert, NULL, NULL)) {
79                 rsa_err("Couldn't read certificate");
80                 ret = -EINVAL;
81                 goto err_cert;
82         }
83
84         /* Get the public key from the certificate. */
85         key = X509_get_pubkey(cert);
86         if (!key) {
87                 rsa_err("Couldn't read public key\n");
88                 ret = -EINVAL;
89                 goto err_pubkey;
90         }
91
92         /* Convert to a RSA_style key. */
93         rsa = EVP_PKEY_get1_RSA(key);
94         if (!rsa) {
95                 rsa_err("Couldn't convert to a RSA style key");
96                 ret = -EINVAL;
97                 goto err_rsa;
98         }
99         fclose(f);
100         EVP_PKEY_free(key);
101         X509_free(cert);
102         *rsap = rsa;
103
104         return 0;
105
106 err_rsa:
107         EVP_PKEY_free(key);
108 err_pubkey:
109         X509_free(cert);
110 err_cert:
111         fclose(f);
112         return ret;
113 }
114
115 /**
116  * rsa_engine_get_pub_key() - read a public key from given engine
117  *
118  * @keydir:     Key prefix
119  * @name        Name of key
120  * @engine      Engine to use
121  * @rsap        Returns RSA object, or NULL on failure
122  * @return 0 if ok, -ve on error (in which case *rsap will be set to NULL)
123  */
124 static int rsa_engine_get_pub_key(const char *keydir, const char *name,
125                                   ENGINE *engine, RSA **rsap)
126 {
127         const char *engine_id;
128         char key_id[1024];
129         EVP_PKEY *key;
130         RSA *rsa;
131         int ret;
132
133         *rsap = NULL;
134
135         engine_id = ENGINE_get_id(engine);
136
137         if (engine_id && !strcmp(engine_id, "pkcs11")) {
138                 if (keydir)
139                         if (strstr(keydir, "object="))
140                                 snprintf(key_id, sizeof(key_id),
141                                          "pkcs11:%s;type=public",
142                                          keydir);
143                         else
144                                 snprintf(key_id, sizeof(key_id),
145                                          "pkcs11:%s;object=%s;type=public",
146                                          keydir, name);
147                 else
148                         snprintf(key_id, sizeof(key_id),
149                                  "pkcs11:object=%s;type=public",
150                                  name);
151         } else if (engine_id) {
152                 if (keydir)
153                         snprintf(key_id, sizeof(key_id),
154                                  "%s%s",
155                                  keydir, name);
156                 else
157                         snprintf(key_id, sizeof(key_id),
158                                  "%s",
159                                  name);
160         } else {
161                 fprintf(stderr, "Engine not supported\n");
162                 return -ENOTSUP;
163         }
164
165         key = ENGINE_load_public_key(engine, key_id, NULL, NULL);
166         if (!key)
167                 return rsa_err("Failure loading public key from engine");
168
169         /* Convert to a RSA_style key. */
170         rsa = EVP_PKEY_get1_RSA(key);
171         if (!rsa) {
172                 rsa_err("Couldn't convert to a RSA style key");
173                 ret = -EINVAL;
174                 goto err_rsa;
175         }
176
177         EVP_PKEY_free(key);
178         *rsap = rsa;
179
180         return 0;
181
182 err_rsa:
183         EVP_PKEY_free(key);
184         return ret;
185 }
186
187 /**
188  * rsa_get_pub_key() - read a public key
189  *
190  * @keydir:     Directory containing the key (PEM file) or key prefix (engine)
191  * @name        Name of key file (will have a .crt extension)
192  * @engine      Engine to use
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  */
196 static int rsa_get_pub_key(const char *keydir, const char *name,
197                            ENGINE *engine, RSA **rsap)
198 {
199         if (engine)
200                 return rsa_engine_get_pub_key(keydir, name, engine, rsap);
201         return rsa_pem_get_pub_key(keydir, name, rsap);
202 }
203
204 /**
205  * rsa_pem_get_priv_key() - read a private key from a .key file
206  *
207  * @keydir:     Directory containing the key
208  * @name        Name of key file (will have a .key extension)
209  * @rsap        Returns RSA object, or NULL on failure
210  * @return 0 if ok, -ve on error (in which case *rsap will be set to NULL)
211  */
212 static int rsa_pem_get_priv_key(const char *keydir, const char *name,
213                                 RSA **rsap)
214 {
215         char path[1024];
216         RSA *rsa;
217         FILE *f;
218
219         *rsap = NULL;
220         snprintf(path, sizeof(path), "%s/%s.key", keydir, name);
221         f = fopen(path, "r");
222         if (!f) {
223                 fprintf(stderr, "Couldn't open RSA private key: '%s': %s\n",
224                         path, strerror(errno));
225                 return -ENOENT;
226         }
227
228         rsa = PEM_read_RSAPrivateKey(f, 0, NULL, path);
229         if (!rsa) {
230                 rsa_err("Failure reading private key");
231                 fclose(f);
232                 return -EPROTO;
233         }
234         fclose(f);
235         *rsap = rsa;
236
237         return 0;
238 }
239
240 /**
241  * rsa_engine_get_priv_key() - read a private key from given engine
242  *
243  * @keydir:     Key prefix
244  * @name        Name of key
245  * @engine      Engine to use
246  * @rsap        Returns RSA object, or NULL on failure
247  * @return 0 if ok, -ve on error (in which case *rsap will be set to NULL)
248  */
249 static int rsa_engine_get_priv_key(const char *keydir, const char *name,
250                                    ENGINE *engine, RSA **rsap)
251 {
252         const char *engine_id;
253         char key_id[1024];
254         EVP_PKEY *key;
255         RSA *rsa;
256         int ret;
257
258         *rsap = NULL;
259
260         engine_id = ENGINE_get_id(engine);
261
262         if (engine_id && !strcmp(engine_id, "pkcs11")) {
263                 if (keydir)
264                         if (strstr(keydir, "object="))
265                                 snprintf(key_id, sizeof(key_id),
266                                          "pkcs11:%s;type=private",
267                                          keydir);
268                         else
269                                 snprintf(key_id, sizeof(key_id),
270                                          "pkcs11:%s;object=%s;type=private",
271                                          keydir, name);
272                 else
273                         snprintf(key_id, sizeof(key_id),
274                                  "pkcs11:object=%s;type=private",
275                                  name);
276         } else if (engine_id) {
277                 if (keydir)
278                         snprintf(key_id, sizeof(key_id),
279                                  "%s%s",
280                                  keydir, name);
281                 else
282                         snprintf(key_id, sizeof(key_id),
283                                  "%s",
284                                  name);
285         } else {
286                 fprintf(stderr, "Engine not supported\n");
287                 return -ENOTSUP;
288         }
289
290         key = ENGINE_load_private_key(engine, key_id, NULL, NULL);
291         if (!key)
292                 return rsa_err("Failure loading private key from engine");
293
294         /* Convert to a RSA_style key. */
295         rsa = EVP_PKEY_get1_RSA(key);
296         if (!rsa) {
297                 rsa_err("Couldn't convert to a RSA style key");
298                 ret = -EINVAL;
299                 goto err_rsa;
300         }
301
302         EVP_PKEY_free(key);
303         *rsap = rsa;
304
305         return 0;
306
307 err_rsa:
308         EVP_PKEY_free(key);
309         return ret;
310 }
311
312 /**
313  * rsa_get_priv_key() - read a private key
314  *
315  * @keydir:     Directory containing the key (PEM file) or key prefix (engine)
316  * @name        Name of key
317  * @engine      Engine to use for signing
318  * @rsap        Returns RSA object, or NULL on failure
319  * @return 0 if ok, -ve on error (in which case *rsap will be set to NULL)
320  */
321 static int rsa_get_priv_key(const char *keydir, const char *name,
322                             ENGINE *engine, RSA **rsap)
323 {
324         if (engine)
325                 return rsa_engine_get_priv_key(keydir, name, engine, rsap);
326         return rsa_pem_get_priv_key(keydir, name, rsap);
327 }
328
329 static int rsa_init(void)
330 {
331         int ret;
332
333 #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
334         (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x02070000fL)
335         ret = SSL_library_init();
336 #else
337         ret = OPENSSL_init_ssl(0, NULL);
338 #endif
339         if (!ret) {
340                 fprintf(stderr, "Failure to init SSL library\n");
341                 return -1;
342         }
343 #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
344         (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x02070000fL)
345         SSL_load_error_strings();
346
347         OpenSSL_add_all_algorithms();
348         OpenSSL_add_all_digests();
349         OpenSSL_add_all_ciphers();
350 #endif
351
352         return 0;
353 }
354
355 static int rsa_engine_init(const char *engine_id, ENGINE **pe)
356 {
357         ENGINE *e;
358         int ret;
359
360         ENGINE_load_builtin_engines();
361
362         e = ENGINE_by_id(engine_id);
363         if (!e) {
364                 fprintf(stderr, "Engine isn't available\n");
365                 ret = -1;
366                 goto err_engine_by_id;
367         }
368
369         if (!ENGINE_init(e)) {
370                 fprintf(stderr, "Couldn't initialize engine\n");
371                 ret = -1;
372                 goto err_engine_init;
373         }
374
375         if (!ENGINE_set_default_RSA(e)) {
376                 fprintf(stderr, "Couldn't set engine as default for RSA\n");
377                 ret = -1;
378                 goto err_set_rsa;
379         }
380
381         *pe = e;
382
383         return 0;
384
385 err_set_rsa:
386         ENGINE_finish(e);
387 err_engine_init:
388         ENGINE_free(e);
389 err_engine_by_id:
390 #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
391         (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x02070000fL)
392         ENGINE_cleanup();
393 #endif
394         return ret;
395 }
396
397 static void rsa_remove(void)
398 {
399 #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
400         (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x02070000fL)
401         CRYPTO_cleanup_all_ex_data();
402         ERR_free_strings();
403 #ifdef HAVE_ERR_REMOVE_THREAD_STATE
404         ERR_remove_thread_state(NULL);
405 #else
406         ERR_remove_state(0);
407 #endif
408         EVP_cleanup();
409 #endif
410 }
411
412 static void rsa_engine_remove(ENGINE *e)
413 {
414         if (e) {
415                 ENGINE_finish(e);
416                 ENGINE_free(e);
417         }
418 }
419
420 static int rsa_sign_with_key(RSA *rsa, struct padding_algo *padding_algo,
421                              struct checksum_algo *checksum_algo,
422                 const struct image_region region[], int region_count,
423                 uint8_t **sigp, uint *sig_size)
424 {
425         EVP_PKEY *key;
426         EVP_PKEY_CTX *ckey;
427         EVP_MD_CTX *context;
428         int ret = 0;
429         size_t size;
430         uint8_t *sig;
431         int i;
432
433         key = EVP_PKEY_new();
434         if (!key)
435                 return rsa_err("EVP_PKEY object creation failed");
436
437         if (!EVP_PKEY_set1_RSA(key, rsa)) {
438                 ret = rsa_err("EVP key setup failed");
439                 goto err_set;
440         }
441
442         size = EVP_PKEY_size(key);
443         sig = malloc(size);
444         if (!sig) {
445                 fprintf(stderr, "Out of memory for signature (%zu bytes)\n",
446                         size);
447                 ret = -ENOMEM;
448                 goto err_alloc;
449         }
450
451         context = EVP_MD_CTX_create();
452         if (!context) {
453                 ret = rsa_err("EVP context creation failed");
454                 goto err_create;
455         }
456         EVP_MD_CTX_init(context);
457
458         ckey = EVP_PKEY_CTX_new(key, NULL);
459         if (!ckey) {
460                 ret = rsa_err("EVP key context creation failed");
461                 goto err_create;
462         }
463
464         if (EVP_DigestSignInit(context, &ckey,
465                                checksum_algo->calculate_sign(),
466                                NULL, key) <= 0) {
467                 ret = rsa_err("Signer setup failed");
468                 goto err_sign;
469         }
470
471 #ifdef CONFIG_FIT_ENABLE_RSASSA_PSS_SUPPORT
472         if (padding_algo && !strcmp(padding_algo->name, "pss")) {
473                 if (EVP_PKEY_CTX_set_rsa_padding(ckey,
474                                                  RSA_PKCS1_PSS_PADDING) <= 0) {
475                         ret = rsa_err("Signer padding setup failed");
476                         goto err_sign;
477                 }
478         }
479 #endif /* CONFIG_FIT_ENABLE_RSASSA_PSS_SUPPORT */
480
481         for (i = 0; i < region_count; i++) {
482                 if (!EVP_DigestSignUpdate(context, region[i].data,
483                                           region[i].size)) {
484                         ret = rsa_err("Signing data failed");
485                         goto err_sign;
486                 }
487         }
488
489         if (!EVP_DigestSignFinal(context, sig, &size)) {
490                 ret = rsa_err("Could not obtain signature");
491                 goto err_sign;
492         }
493
494         #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
495                 (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x02070000fL)
496                 EVP_MD_CTX_cleanup(context);
497         #else
498                 EVP_MD_CTX_reset(context);
499         #endif
500         EVP_MD_CTX_destroy(context);
501         EVP_PKEY_free(key);
502
503         debug("Got signature: %d bytes, expected %zu\n", *sig_size, size);
504         *sigp = sig;
505         *sig_size = size;
506
507         return 0;
508
509 err_sign:
510         EVP_MD_CTX_destroy(context);
511 err_create:
512         free(sig);
513 err_alloc:
514 err_set:
515         EVP_PKEY_free(key);
516         return ret;
517 }
518
519 int rsa_sign(struct image_sign_info *info,
520              const struct image_region region[], int region_count,
521              uint8_t **sigp, uint *sig_len)
522 {
523         RSA *rsa;
524         ENGINE *e = NULL;
525         int ret;
526
527         ret = rsa_init();
528         if (ret)
529                 return ret;
530
531         if (info->engine_id) {
532                 ret = rsa_engine_init(info->engine_id, &e);
533                 if (ret)
534                         goto err_engine;
535         }
536
537         ret = rsa_get_priv_key(info->keydir, info->keyname, e, &rsa);
538         if (ret)
539                 goto err_priv;
540         ret = rsa_sign_with_key(rsa, info->padding, info->checksum, region,
541                                 region_count, sigp, sig_len);
542         if (ret)
543                 goto err_sign;
544
545         RSA_free(rsa);
546         if (info->engine_id)
547                 rsa_engine_remove(e);
548         rsa_remove();
549
550         return ret;
551
552 err_sign:
553         RSA_free(rsa);
554 err_priv:
555         if (info->engine_id)
556                 rsa_engine_remove(e);
557 err_engine:
558         rsa_remove();
559         return ret;
560 }
561
562 /*
563  * rsa_get_exponent(): - Get the public exponent from an RSA key
564  */
565 static int rsa_get_exponent(RSA *key, uint64_t *e)
566 {
567         int ret;
568         BIGNUM *bn_te;
569         const BIGNUM *key_e;
570         uint64_t te;
571
572         ret = -EINVAL;
573         bn_te = NULL;
574
575         if (!e)
576                 goto cleanup;
577
578         RSA_get0_key(key, NULL, &key_e, NULL);
579         if (BN_num_bits(key_e) > 64)
580                 goto cleanup;
581
582         *e = BN_get_word(key_e);
583
584         if (BN_num_bits(key_e) < 33) {
585                 ret = 0;
586                 goto cleanup;
587         }
588
589         bn_te = BN_dup(key_e);
590         if (!bn_te)
591                 goto cleanup;
592
593         if (!BN_rshift(bn_te, bn_te, 32))
594                 goto cleanup;
595
596         if (!BN_mask_bits(bn_te, 32))
597                 goto cleanup;
598
599         te = BN_get_word(bn_te);
600         te <<= 32;
601         *e |= te;
602         ret = 0;
603
604 cleanup:
605         if (bn_te)
606                 BN_free(bn_te);
607
608         return ret;
609 }
610
611 /*
612  * rsa_get_params(): - Get the important parameters of an RSA public key
613  */
614 int rsa_get_params(RSA *key, uint64_t *exponent, uint32_t *n0_invp,
615                    BIGNUM **modulusp, BIGNUM **r_squaredp)
616 {
617         BIGNUM *big1, *big2, *big32, *big2_32;
618         BIGNUM *n, *r, *r_squared, *tmp;
619         const BIGNUM *key_n;
620         BN_CTX *bn_ctx = BN_CTX_new();
621         int ret = 0;
622
623         /* Initialize BIGNUMs */
624         big1 = BN_new();
625         big2 = BN_new();
626         big32 = BN_new();
627         r = BN_new();
628         r_squared = BN_new();
629         tmp = BN_new();
630         big2_32 = BN_new();
631         n = BN_new();
632         if (!big1 || !big2 || !big32 || !r || !r_squared || !tmp || !big2_32 ||
633             !n) {
634                 fprintf(stderr, "Out of memory (bignum)\n");
635                 return -ENOMEM;
636         }
637
638         if (0 != rsa_get_exponent(key, exponent))
639                 ret = -1;
640
641         RSA_get0_key(key, &key_n, NULL, NULL);
642         if (!BN_copy(n, key_n) || !BN_set_word(big1, 1L) ||
643             !BN_set_word(big2, 2L) || !BN_set_word(big32, 32L))
644                 ret = -1;
645
646         /* big2_32 = 2^32 */
647         if (!BN_exp(big2_32, big2, big32, bn_ctx))
648                 ret = -1;
649
650         /* Calculate n0_inv = -1 / n[0] mod 2^32 */
651         if (!BN_mod_inverse(tmp, n, big2_32, bn_ctx) ||
652             !BN_sub(tmp, big2_32, tmp))
653                 ret = -1;
654         *n0_invp = BN_get_word(tmp);
655
656         /* Calculate R = 2^(# of key bits) */
657         if (!BN_set_word(tmp, BN_num_bits(n)) ||
658             !BN_exp(r, big2, tmp, bn_ctx))
659                 ret = -1;
660
661         /* Calculate r_squared = R^2 mod n */
662         if (!BN_copy(r_squared, r) ||
663             !BN_mul(tmp, r_squared, r, bn_ctx) ||
664             !BN_mod(r_squared, tmp, n, bn_ctx))
665                 ret = -1;
666
667         *modulusp = n;
668         *r_squaredp = r_squared;
669
670         BN_free(big1);
671         BN_free(big2);
672         BN_free(big32);
673         BN_free(r);
674         BN_free(tmp);
675         BN_free(big2_32);
676         if (ret) {
677                 fprintf(stderr, "Bignum operations failed\n");
678                 return -ENOMEM;
679         }
680
681         return ret;
682 }
683
684 int rsa_add_verify_data(struct image_sign_info *info, void *keydest)
685 {
686         BIGNUM *modulus, *r_squared;
687         uint64_t exponent;
688         uint32_t n0_inv;
689         int parent, node;
690         char name[100];
691         int ret;
692         int bits;
693         RSA *rsa;
694         ENGINE *e = NULL;
695
696         debug("%s: Getting verification data\n", __func__);
697         if (info->engine_id) {
698                 ret = rsa_engine_init(info->engine_id, &e);
699                 if (ret)
700                         return ret;
701         }
702         ret = rsa_get_pub_key(info->keydir, info->keyname, e, &rsa);
703         if (ret)
704                 goto err_get_pub_key;
705         ret = rsa_get_params(rsa, &exponent, &n0_inv, &modulus, &r_squared);
706         if (ret)
707                 goto err_get_params;
708         bits = BN_num_bits(modulus);
709         parent = fdt_subnode_offset(keydest, 0, FIT_SIG_NODENAME);
710         if (parent == -FDT_ERR_NOTFOUND) {
711                 parent = fdt_add_subnode(keydest, 0, FIT_SIG_NODENAME);
712                 if (parent < 0) {
713                         ret = parent;
714                         if (ret != -FDT_ERR_NOSPACE) {
715                                 fprintf(stderr, "Couldn't create signature node: %s\n",
716                                         fdt_strerror(parent));
717                         }
718                 }
719         }
720         if (ret)
721                 goto done;
722
723         /* Either create or overwrite the named key node */
724         snprintf(name, sizeof(name), "key-%s", info->keyname);
725         node = fdt_subnode_offset(keydest, parent, name);
726         if (node == -FDT_ERR_NOTFOUND) {
727                 node = fdt_add_subnode(keydest, parent, name);
728                 if (node < 0) {
729                         ret = node;
730                         if (ret != -FDT_ERR_NOSPACE) {
731                                 fprintf(stderr, "Could not create key subnode: %s\n",
732                                         fdt_strerror(node));
733                         }
734                 }
735         } else if (node < 0) {
736                 fprintf(stderr, "Cannot select keys parent: %s\n",
737                         fdt_strerror(node));
738                 ret = node;
739         }
740
741         if (!ret) {
742                 ret = fdt_setprop_string(keydest, node, FIT_KEY_HINT,
743                                          info->keyname);
744         }
745         if (!ret)
746                 ret = fdt_setprop_u32(keydest, node, "rsa,num-bits", bits);
747         if (!ret)
748                 ret = fdt_setprop_u32(keydest, node, "rsa,n0-inverse", n0_inv);
749         if (!ret) {
750                 ret = fdt_setprop_u64(keydest, node, "rsa,exponent", exponent);
751         }
752         if (!ret) {
753                 ret = fdt_add_bignum(keydest, node, "rsa,modulus", modulus,
754                                      bits);
755         }
756         if (!ret) {
757                 ret = fdt_add_bignum(keydest, node, "rsa,r-squared", r_squared,
758                                      bits);
759         }
760         if (!ret) {
761                 ret = fdt_setprop_string(keydest, node, FIT_ALGO_PROP,
762                                          info->name);
763         }
764         if (!ret && info->require_keys) {
765                 ret = fdt_setprop_string(keydest, node, FIT_KEY_REQUIRED,
766                                          info->require_keys);
767         }
768 done:
769         BN_free(modulus);
770         BN_free(r_squared);
771         if (ret)
772                 ret = ret == -FDT_ERR_NOSPACE ? -ENOSPC : -EIO;
773 err_get_params:
774         RSA_free(rsa);
775 err_get_pub_key:
776         if (info->engine_id)
777                 rsa_engine_remove(e);
778
779         return ret;
780 }