rsa-sign.c 18 KB

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