rsa-verify.c 15 KB

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