rsa-sign.c 18 KB

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