rsa-verify.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2013, Google Inc.
  4. */
  5. #ifndef USE_HOSTCC
  6. #include <common.h>
  7. #include <fdtdec.h>
  8. #include <log.h>
  9. #include <malloc.h>
  10. #include <asm/types.h>
  11. #include <asm/byteorder.h>
  12. #include <linux/errno.h>
  13. #include <asm/types.h>
  14. #include <asm/unaligned.h>
  15. #include <dm.h>
  16. #else
  17. #include "fdt_host.h"
  18. #include "mkimage.h"
  19. #include <fdt_support.h>
  20. #endif
  21. #include <linux/kconfig.h>
  22. #include <u-boot/rsa-mod-exp.h>
  23. #include <u-boot/rsa.h>
  24. #ifndef __UBOOT__
  25. /*
  26. * NOTE:
  27. * Since host tools, like mkimage, make use of openssl library for
  28. * RSA encryption, rsa_verify_with_pkey()/rsa_gen_key_prop() are
  29. * of no use and should not be compiled in.
  30. * So just turn off CONFIG_RSA_VERIFY_WITH_PKEY.
  31. */
  32. #undef CONFIG_RSA_VERIFY_WITH_PKEY
  33. #endif
  34. /* Default public exponent for backward compatibility */
  35. #define RSA_DEFAULT_PUBEXP 65537
  36. /**
  37. * rsa_verify_padding() - Verify RSA message padding is valid
  38. *
  39. * Verify a RSA message's padding is consistent with PKCS1.5
  40. * padding as described in the RSA PKCS#1 v2.1 standard.
  41. *
  42. * @msg: Padded message
  43. * @pad_len: Number of expected padding bytes
  44. * @algo: Checksum algo structure having information on DER encoding etc.
  45. * @return 0 on success, != 0 on failure
  46. */
  47. static int rsa_verify_padding(const uint8_t *msg, const int pad_len,
  48. struct checksum_algo *algo)
  49. {
  50. int ff_len;
  51. int ret;
  52. /* first byte must be 0x00 */
  53. ret = *msg++;
  54. /* second byte must be 0x01 */
  55. ret |= *msg++ ^ 0x01;
  56. /* next ff_len bytes must be 0xff */
  57. ff_len = pad_len - algo->der_len - 3;
  58. ret |= *msg ^ 0xff;
  59. ret |= memcmp(msg, msg+1, ff_len-1);
  60. msg += ff_len;
  61. /* next byte must be 0x00 */
  62. ret |= *msg++;
  63. /* next der_len bytes must match der_prefix */
  64. ret |= memcmp(msg, algo->der_prefix, algo->der_len);
  65. return ret;
  66. }
  67. int padding_pkcs_15_verify(struct image_sign_info *info,
  68. uint8_t *msg, int msg_len,
  69. const uint8_t *hash, int hash_len)
  70. {
  71. struct checksum_algo *checksum = info->checksum;
  72. int ret, pad_len = msg_len - checksum->checksum_len;
  73. /* Check pkcs1.5 padding bytes. */
  74. ret = rsa_verify_padding(msg, pad_len, checksum);
  75. if (ret) {
  76. debug("In RSAVerify(): Padding check failed!\n");
  77. return -EINVAL;
  78. }
  79. /* Check hash. */
  80. if (memcmp((uint8_t *)msg + pad_len, hash, msg_len - pad_len)) {
  81. debug("In RSAVerify(): Hash check failed!\n");
  82. return -EACCES;
  83. }
  84. return 0;
  85. }
  86. #ifndef USE_HOSTCC
  87. U_BOOT_PADDING_ALGO(pkcs_15) = {
  88. .name = "pkcs-1.5",
  89. .verify = padding_pkcs_15_verify,
  90. };
  91. #endif
  92. #ifdef CONFIG_FIT_RSASSA_PSS
  93. static void u32_i2osp(uint32_t val, uint8_t *buf)
  94. {
  95. buf[0] = (uint8_t)((val >> 24) & 0xff);
  96. buf[1] = (uint8_t)((val >> 16) & 0xff);
  97. buf[2] = (uint8_t)((val >> 8) & 0xff);
  98. buf[3] = (uint8_t)((val >> 0) & 0xff);
  99. }
  100. /**
  101. * mask_generation_function1() - generate an octet string
  102. *
  103. * Generate an octet string used to check rsa signature.
  104. * It use an input octet string and a hash function.
  105. *
  106. * @checksum: A Hash function
  107. * @seed: Specifies an input variable octet string
  108. * @seed_len: Size of the input octet string
  109. * @output: Specifies the output octet string
  110. * @output_len: Size of the output octet string
  111. * @return 0 if the octet string was correctly generated, others on error
  112. */
  113. static int mask_generation_function1(struct checksum_algo *checksum,
  114. uint8_t *seed, int seed_len,
  115. uint8_t *output, int output_len)
  116. {
  117. struct image_region region[2];
  118. int ret = 0, i, i_output = 0, region_count = 2;
  119. uint32_t counter = 0;
  120. uint8_t buf_counter[4], *tmp;
  121. int hash_len = checksum->checksum_len;
  122. memset(output, 0, output_len);
  123. region[0].data = seed;
  124. region[0].size = seed_len;
  125. region[1].data = &buf_counter[0];
  126. region[1].size = 4;
  127. tmp = malloc(hash_len);
  128. if (!tmp) {
  129. debug("%s: can't allocate array tmp\n", __func__);
  130. ret = -ENOMEM;
  131. goto out;
  132. }
  133. while (i_output < output_len) {
  134. u32_i2osp(counter, &buf_counter[0]);
  135. ret = checksum->calculate(checksum->name,
  136. region, region_count,
  137. tmp);
  138. if (ret < 0) {
  139. debug("%s: Error in checksum calculation\n", __func__);
  140. goto out;
  141. }
  142. i = 0;
  143. while ((i_output < output_len) && (i < hash_len)) {
  144. output[i_output] = tmp[i];
  145. i_output++;
  146. i++;
  147. }
  148. counter++;
  149. }
  150. out:
  151. free(tmp);
  152. return ret;
  153. }
  154. static int compute_hash_prime(struct checksum_algo *checksum,
  155. uint8_t *pad, int pad_len,
  156. uint8_t *hash, int hash_len,
  157. uint8_t *salt, int salt_len,
  158. uint8_t *hprime)
  159. {
  160. struct image_region region[3];
  161. int ret, region_count = 3;
  162. region[0].data = pad;
  163. region[0].size = pad_len;
  164. region[1].data = hash;
  165. region[1].size = hash_len;
  166. region[2].data = salt;
  167. region[2].size = salt_len;
  168. ret = checksum->calculate(checksum->name, region, region_count, hprime);
  169. if (ret < 0) {
  170. debug("%s: Error in checksum calculation\n", __func__);
  171. goto out;
  172. }
  173. out:
  174. return ret;
  175. }
  176. /*
  177. * padding_pss_verify() - verify the pss padding of a signature
  178. *
  179. * Only works with a rsa_pss_saltlen:-2 (default value) right now
  180. * saltlen:-1 "set the salt length to the digest length" is currently
  181. * not supported.
  182. *
  183. * @info: Specifies key and FIT information
  184. * @msg: byte array of message, len equal to msg_len
  185. * @msg_len: Message length
  186. * @hash: Pointer to the expected hash
  187. * @hash_len: Length of the hash
  188. */
  189. int padding_pss_verify(struct image_sign_info *info,
  190. uint8_t *msg, int msg_len,
  191. const uint8_t *hash, int hash_len)
  192. {
  193. uint8_t *masked_db = NULL;
  194. int masked_db_len = msg_len - hash_len - 1;
  195. uint8_t *h = NULL, *hprime = NULL;
  196. int h_len = hash_len;
  197. uint8_t *db_mask = NULL;
  198. int db_mask_len = masked_db_len;
  199. uint8_t *db = NULL, *salt = NULL;
  200. int db_len = masked_db_len, salt_len = msg_len - hash_len - 2;
  201. uint8_t pad_zero[8] = { 0 };
  202. int ret, i, leftmost_bits = 1;
  203. uint8_t leftmost_mask;
  204. struct checksum_algo *checksum = info->checksum;
  205. /* first, allocate everything */
  206. masked_db = malloc(masked_db_len);
  207. h = malloc(h_len);
  208. db_mask = malloc(db_mask_len);
  209. db = malloc(db_len);
  210. salt = malloc(salt_len);
  211. hprime = malloc(hash_len);
  212. if (!masked_db || !h || !db_mask || !db || !salt || !hprime) {
  213. printf("%s: can't allocate some buffer\n", __func__);
  214. ret = -ENOMEM;
  215. goto out;
  216. }
  217. /* step 4: check if the last byte is 0xbc */
  218. if (msg[msg_len - 1] != 0xbc) {
  219. printf("%s: invalid pss padding (0xbc is missing)\n", __func__);
  220. ret = -EINVAL;
  221. goto out;
  222. }
  223. /* step 5 */
  224. memcpy(masked_db, msg, masked_db_len);
  225. memcpy(h, msg + masked_db_len, h_len);
  226. /* step 6 */
  227. leftmost_mask = (0xff >> (8 - leftmost_bits)) << (8 - leftmost_bits);
  228. if (masked_db[0] & leftmost_mask) {
  229. printf("%s: invalid pss padding ", __func__);
  230. printf("(leftmost bit of maskedDB not zero)\n");
  231. ret = -EINVAL;
  232. goto out;
  233. }
  234. /* step 7 */
  235. mask_generation_function1(checksum, h, h_len, db_mask, db_mask_len);
  236. /* step 8 */
  237. for (i = 0; i < db_len; i++)
  238. db[i] = masked_db[i] ^ db_mask[i];
  239. /* step 9 */
  240. db[0] &= 0xff >> leftmost_bits;
  241. /* step 10 */
  242. if (db[0] != 0x01) {
  243. printf("%s: invalid pss padding ", __func__);
  244. printf("(leftmost byte of db isn't 0x01)\n");
  245. ret = EINVAL;
  246. goto out;
  247. }
  248. /* step 11 */
  249. memcpy(salt, &db[1], salt_len);
  250. /* step 12 & 13 */
  251. compute_hash_prime(checksum, pad_zero, 8,
  252. (uint8_t *)hash, hash_len,
  253. salt, salt_len, hprime);
  254. /* step 14 */
  255. ret = memcmp(h, hprime, hash_len);
  256. out:
  257. free(hprime);
  258. free(salt);
  259. free(db);
  260. free(db_mask);
  261. free(h);
  262. free(masked_db);
  263. return ret;
  264. }
  265. #ifndef USE_HOSTCC
  266. U_BOOT_PADDING_ALGO(pss) = {
  267. .name = "pss",
  268. .verify = padding_pss_verify,
  269. };
  270. #endif
  271. #endif
  272. #if CONFIG_IS_ENABLED(FIT_SIGNATURE) || CONFIG_IS_ENABLED(RSA_VERIFY_WITH_PKEY)
  273. /**
  274. * rsa_verify_key() - Verify a signature against some data using RSA Key
  275. *
  276. * Verify a RSA PKCS1.5 signature against an expected hash using
  277. * the RSA Key properties in prop structure.
  278. *
  279. * @info: Specifies key and FIT information
  280. * @prop: Specifies key
  281. * @sig: Signature
  282. * @sig_len: Number of bytes in signature
  283. * @hash: Pointer to the expected hash
  284. * @key_len: Number of bytes in rsa key
  285. * @return 0 if verified, -ve on error
  286. */
  287. static int rsa_verify_key(struct image_sign_info *info,
  288. struct key_prop *prop, const uint8_t *sig,
  289. const uint32_t sig_len, const uint8_t *hash,
  290. const uint32_t key_len)
  291. {
  292. int ret;
  293. #if !defined(USE_HOSTCC)
  294. struct udevice *mod_exp_dev;
  295. #endif
  296. struct checksum_algo *checksum = info->checksum;
  297. struct padding_algo *padding = info->padding;
  298. int hash_len;
  299. if (!prop || !sig || !hash || !checksum)
  300. return -EIO;
  301. if (sig_len != (prop->num_bits / 8)) {
  302. debug("Signature is of incorrect length %d\n", sig_len);
  303. return -EINVAL;
  304. }
  305. debug("Checksum algorithm: %s", checksum->name);
  306. /* Sanity check for stack size */
  307. if (sig_len > RSA_MAX_SIG_BITS / 8) {
  308. debug("Signature length %u exceeds maximum %d\n", sig_len,
  309. RSA_MAX_SIG_BITS / 8);
  310. return -EINVAL;
  311. }
  312. uint8_t buf[sig_len];
  313. hash_len = checksum->checksum_len;
  314. #if !defined(USE_HOSTCC)
  315. ret = uclass_get_device(UCLASS_MOD_EXP, 0, &mod_exp_dev);
  316. if (ret) {
  317. printf("RSA: Can't find Modular Exp implementation\n");
  318. return -EINVAL;
  319. }
  320. ret = rsa_mod_exp(mod_exp_dev, sig, sig_len, prop, buf);
  321. #else
  322. ret = rsa_mod_exp_sw(sig, sig_len, prop, buf);
  323. #endif
  324. if (ret) {
  325. debug("Error in Modular exponentation\n");
  326. return ret;
  327. }
  328. ret = padding->verify(info, buf, key_len, hash, hash_len);
  329. if (ret) {
  330. debug("In RSAVerify(): padding check failed!\n");
  331. return ret;
  332. }
  333. return 0;
  334. }
  335. #endif
  336. #if CONFIG_IS_ENABLED(RSA_VERIFY_WITH_PKEY)
  337. /**
  338. * rsa_verify_with_pkey() - Verify a signature against some data using
  339. * only modulus and exponent as RSA key properties.
  340. * @info: Specifies key information
  341. * @hash: Pointer to the expected hash
  342. * @sig: Signature
  343. * @sig_len: Number of bytes in signature
  344. *
  345. * Parse a RSA public key blob in DER format pointed to in @info and fill
  346. * a key_prop structure with properties of the key. Then verify a RSA PKCS1.5
  347. * signature against an expected hash using the calculated properties.
  348. *
  349. * Return 0 if verified, -ve on error
  350. */
  351. int rsa_verify_with_pkey(struct image_sign_info *info,
  352. const void *hash, uint8_t *sig, uint sig_len)
  353. {
  354. struct key_prop *prop;
  355. int ret;
  356. /* Public key is self-described to fill key_prop */
  357. ret = rsa_gen_key_prop(info->key, info->keylen, &prop);
  358. if (ret) {
  359. debug("Generating necessary parameter for decoding failed\n");
  360. return ret;
  361. }
  362. ret = rsa_verify_key(info, prop, sig, sig_len, hash,
  363. info->crypto->key_len);
  364. rsa_free_key_prop(prop);
  365. return ret;
  366. }
  367. #else
  368. int rsa_verify_with_pkey(struct image_sign_info *info,
  369. const void *hash, uint8_t *sig, uint sig_len)
  370. {
  371. return -EACCES;
  372. }
  373. #endif
  374. #if CONFIG_IS_ENABLED(FIT_SIGNATURE)
  375. /**
  376. * rsa_verify_with_keynode() - Verify a signature against some data using
  377. * information in node with prperties of RSA Key like modulus, exponent etc.
  378. *
  379. * Parse sign-node and fill a key_prop structure with properties of the
  380. * key. Verify a RSA PKCS1.5 signature against an expected hash using
  381. * the properties parsed
  382. *
  383. * @info: Specifies key and FIT information
  384. * @hash: Pointer to the expected hash
  385. * @sig: Signature
  386. * @sig_len: Number of bytes in signature
  387. * @node: Node having the RSA Key properties
  388. * @return 0 if verified, -ve on error
  389. */
  390. static int rsa_verify_with_keynode(struct image_sign_info *info,
  391. const void *hash, uint8_t *sig,
  392. uint sig_len, int node)
  393. {
  394. const void *blob = info->fdt_blob;
  395. struct key_prop prop;
  396. int length;
  397. int ret = 0;
  398. const char *algo;
  399. if (node < 0) {
  400. debug("%s: Skipping invalid node", __func__);
  401. return -EBADF;
  402. }
  403. algo = fdt_getprop(blob, node, "algo", NULL);
  404. if (strcmp(info->name, algo)) {
  405. debug("%s: Wrong algo: have %s, expected %s", __func__,
  406. info->name, algo);
  407. return -EFAULT;
  408. }
  409. prop.num_bits = fdtdec_get_int(blob, node, "rsa,num-bits", 0);
  410. prop.n0inv = fdtdec_get_int(blob, node, "rsa,n0-inverse", 0);
  411. prop.public_exponent = fdt_getprop(blob, node, "rsa,exponent", &length);
  412. if (!prop.public_exponent || length < sizeof(uint64_t))
  413. prop.public_exponent = NULL;
  414. prop.exp_len = sizeof(uint64_t);
  415. prop.modulus = fdt_getprop(blob, node, "rsa,modulus", NULL);
  416. prop.rr = fdt_getprop(blob, node, "rsa,r-squared", NULL);
  417. if (!prop.num_bits || !prop.modulus || !prop.rr) {
  418. debug("%s: Missing RSA key info", __func__);
  419. return -EFAULT;
  420. }
  421. ret = rsa_verify_key(info, &prop, sig, sig_len, hash,
  422. info->crypto->key_len);
  423. return ret;
  424. }
  425. #else
  426. static int rsa_verify_with_keynode(struct image_sign_info *info,
  427. const void *hash, uint8_t *sig,
  428. uint sig_len, int node)
  429. {
  430. return -EACCES;
  431. }
  432. #endif
  433. int rsa_verify_hash(struct image_sign_info *info,
  434. const uint8_t *hash, uint8_t *sig, uint sig_len)
  435. {
  436. int ret = -EACCES;
  437. if (CONFIG_IS_ENABLED(RSA_VERIFY_WITH_PKEY) && !info->fdt_blob) {
  438. /* don't rely on fdt properties */
  439. ret = rsa_verify_with_pkey(info, hash, sig, sig_len);
  440. return ret;
  441. }
  442. if (CONFIG_IS_ENABLED(FIT_SIGNATURE)) {
  443. const void *blob = info->fdt_blob;
  444. int ndepth, noffset;
  445. int sig_node, node;
  446. char name[100];
  447. sig_node = fdt_subnode_offset(blob, 0, FIT_SIG_NODENAME);
  448. if (sig_node < 0) {
  449. debug("%s: No signature node found\n", __func__);
  450. return -ENOENT;
  451. }
  452. /* See if we must use a particular key */
  453. if (info->required_keynode != -1) {
  454. ret = rsa_verify_with_keynode(info, hash, sig, sig_len,
  455. info->required_keynode);
  456. return ret;
  457. }
  458. /* Look for a key that matches our hint */
  459. snprintf(name, sizeof(name), "key-%s", info->keyname);
  460. node = fdt_subnode_offset(blob, sig_node, name);
  461. ret = rsa_verify_with_keynode(info, hash, sig, sig_len, node);
  462. if (!ret)
  463. return ret;
  464. /* No luck, so try each of the keys in turn */
  465. for (ndepth = 0, noffset = fdt_next_node(blob, sig_node,
  466. &ndepth);
  467. (noffset >= 0) && (ndepth > 0);
  468. noffset = fdt_next_node(blob, noffset, &ndepth)) {
  469. if (ndepth == 1 && noffset != node) {
  470. ret = rsa_verify_with_keynode(info, hash,
  471. sig, sig_len,
  472. noffset);
  473. if (!ret)
  474. break;
  475. }
  476. }
  477. }
  478. return ret;
  479. }
  480. int rsa_verify(struct image_sign_info *info,
  481. const struct image_region region[], int region_count,
  482. uint8_t *sig, uint sig_len)
  483. {
  484. /* Reserve memory for maximum checksum-length */
  485. uint8_t hash[info->crypto->key_len];
  486. int ret;
  487. /*
  488. * Verify that the checksum-length does not exceed the
  489. * rsa-signature-length
  490. */
  491. if (info->checksum->checksum_len >
  492. info->crypto->key_len) {
  493. debug("%s: invalid checksum-algorithm %s for %s\n",
  494. __func__, info->checksum->name, info->crypto->name);
  495. return -EINVAL;
  496. }
  497. /* Calculate checksum with checksum-algorithm */
  498. ret = info->checksum->calculate(info->checksum->name,
  499. region, region_count, hash);
  500. if (ret < 0) {
  501. debug("%s: Error in checksum calculation\n", __func__);
  502. return -EINVAL;
  503. }
  504. return rsa_verify_hash(info, hash, sig, sig_len);
  505. }
  506. #ifndef USE_HOSTCC
  507. U_BOOT_CRYPTO_ALGO(rsa2048) = {
  508. .name = "rsa2048",
  509. .key_len = RSA2048_BYTES,
  510. .verify = rsa_verify,
  511. };
  512. U_BOOT_CRYPTO_ALGO(rsa4096) = {
  513. .name = "rsa4096",
  514. .key_len = RSA4096_BYTES,
  515. .verify = rsa_verify,
  516. };
  517. #endif