rsa-sign.c 17 KB

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