rsa-sign.c 15 KB

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