rsa-verify.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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 <asm/types.h>
  9. #include <asm/byteorder.h>
  10. #include <linux/errno.h>
  11. #include <asm/types.h>
  12. #include <asm/unaligned.h>
  13. #include <dm.h>
  14. #else
  15. #include "fdt_host.h"
  16. #include "mkimage.h"
  17. #include <fdt_support.h>
  18. #endif
  19. #include <u-boot/rsa-mod-exp.h>
  20. #include <u-boot/rsa.h>
  21. /* Default public exponent for backward compatibility */
  22. #define RSA_DEFAULT_PUBEXP 65537
  23. /**
  24. * rsa_verify_padding() - Verify RSA message padding is valid
  25. *
  26. * Verify a RSA message's padding is consistent with PKCS1.5
  27. * padding as described in the RSA PKCS#1 v2.1 standard.
  28. *
  29. * @msg: Padded message
  30. * @pad_len: Number of expected padding bytes
  31. * @algo: Checksum algo structure having information on DER encoding etc.
  32. * @return 0 on success, != 0 on failure
  33. */
  34. static int rsa_verify_padding(const uint8_t *msg, const int pad_len,
  35. struct checksum_algo *algo)
  36. {
  37. int ff_len;
  38. int ret;
  39. /* first byte must be 0x00 */
  40. ret = *msg++;
  41. /* second byte must be 0x01 */
  42. ret |= *msg++ ^ 0x01;
  43. /* next ff_len bytes must be 0xff */
  44. ff_len = pad_len - algo->der_len - 3;
  45. ret |= *msg ^ 0xff;
  46. ret |= memcmp(msg, msg+1, ff_len-1);
  47. msg += ff_len;
  48. /* next byte must be 0x00 */
  49. ret |= *msg++;
  50. /* next der_len bytes must match der_prefix */
  51. ret |= memcmp(msg, algo->der_prefix, algo->der_len);
  52. return ret;
  53. }
  54. int padding_pkcs_15_verify(struct image_sign_info *info,
  55. uint8_t *msg, int msg_len,
  56. const uint8_t *hash, int hash_len)
  57. {
  58. struct checksum_algo *checksum = info->checksum;
  59. int ret, pad_len = msg_len - checksum->checksum_len;
  60. /* Check pkcs1.5 padding bytes. */
  61. ret = rsa_verify_padding(msg, pad_len, checksum);
  62. if (ret) {
  63. debug("In RSAVerify(): Padding check failed!\n");
  64. return -EINVAL;
  65. }
  66. /* Check hash. */
  67. if (memcmp((uint8_t *)msg + pad_len, hash, msg_len - pad_len)) {
  68. debug("In RSAVerify(): Hash check failed!\n");
  69. return -EACCES;
  70. }
  71. return 0;
  72. }
  73. #ifdef CONFIG_FIT_ENABLE_RSASSA_PSS_SUPPORT
  74. static void u32_i2osp(uint32_t val, uint8_t *buf)
  75. {
  76. buf[0] = (uint8_t)((val >> 24) & 0xff);
  77. buf[1] = (uint8_t)((val >> 16) & 0xff);
  78. buf[2] = (uint8_t)((val >> 8) & 0xff);
  79. buf[3] = (uint8_t)((val >> 0) & 0xff);
  80. }
  81. /**
  82. * mask_generation_function1() - generate an octet string
  83. *
  84. * Generate an octet string used to check rsa signature.
  85. * It use an input octet string and a hash function.
  86. *
  87. * @checksum: A Hash function
  88. * @seed: Specifies an input variable octet string
  89. * @seed_len: Size of the input octet string
  90. * @output: Specifies the output octet string
  91. * @output_len: Size of the output octet string
  92. * @return 0 if the octet string was correctly generated, others on error
  93. */
  94. static int mask_generation_function1(struct checksum_algo *checksum,
  95. uint8_t *seed, int seed_len,
  96. uint8_t *output, int output_len)
  97. {
  98. struct image_region region[2];
  99. int ret = 0, i, i_output = 0, region_count = 2;
  100. uint32_t counter = 0;
  101. uint8_t buf_counter[4], *tmp;
  102. int hash_len = checksum->checksum_len;
  103. memset(output, 0, output_len);
  104. region[0].data = seed;
  105. region[0].size = seed_len;
  106. region[1].data = &buf_counter[0];
  107. region[1].size = 4;
  108. tmp = malloc(hash_len);
  109. if (!tmp) {
  110. debug("%s: can't allocate array tmp\n", __func__);
  111. ret = -ENOMEM;
  112. goto out;
  113. }
  114. while (i_output < output_len) {
  115. u32_i2osp(counter, &buf_counter[0]);
  116. ret = checksum->calculate(checksum->name,
  117. region, region_count,
  118. tmp);
  119. if (ret < 0) {
  120. debug("%s: Error in checksum calculation\n", __func__);
  121. goto out;
  122. }
  123. i = 0;
  124. while ((i_output < output_len) && (i < hash_len)) {
  125. output[i_output] = tmp[i];
  126. i_output++;
  127. i++;
  128. }
  129. counter++;
  130. }
  131. out:
  132. free(tmp);
  133. return ret;
  134. }
  135. static int compute_hash_prime(struct checksum_algo *checksum,
  136. uint8_t *pad, int pad_len,
  137. uint8_t *hash, int hash_len,
  138. uint8_t *salt, int salt_len,
  139. uint8_t *hprime)
  140. {
  141. struct image_region region[3];
  142. int ret, region_count = 3;
  143. region[0].data = pad;
  144. region[0].size = pad_len;
  145. region[1].data = hash;
  146. region[1].size = hash_len;
  147. region[2].data = salt;
  148. region[2].size = salt_len;
  149. ret = checksum->calculate(checksum->name, region, region_count, hprime);
  150. if (ret < 0) {
  151. debug("%s: Error in checksum calculation\n", __func__);
  152. goto out;
  153. }
  154. out:
  155. return ret;
  156. }
  157. int padding_pss_verify(struct image_sign_info *info,
  158. uint8_t *msg, int msg_len,
  159. const uint8_t *hash, int hash_len)
  160. {
  161. uint8_t *masked_db = NULL;
  162. int masked_db_len = msg_len - hash_len - 1;
  163. uint8_t *h = NULL, *hprime = NULL;
  164. int h_len = hash_len;
  165. uint8_t *db_mask = NULL;
  166. int db_mask_len = masked_db_len;
  167. uint8_t *db = NULL, *salt = NULL;
  168. int db_len = masked_db_len, salt_len = msg_len - hash_len - 2;
  169. uint8_t pad_zero[8] = { 0 };
  170. int ret, i, leftmost_bits = 1;
  171. uint8_t leftmost_mask;
  172. struct checksum_algo *checksum = info->checksum;
  173. /* first, allocate everything */
  174. masked_db = malloc(masked_db_len);
  175. h = malloc(h_len);
  176. db_mask = malloc(db_mask_len);
  177. db = malloc(db_len);
  178. salt = malloc(salt_len);
  179. hprime = malloc(hash_len);
  180. if (!masked_db || !h || !db_mask || !db || !salt || !hprime) {
  181. printf("%s: can't allocate some buffer\n", __func__);
  182. ret = -ENOMEM;
  183. goto out;
  184. }
  185. /* step 4: check if the last byte is 0xbc */
  186. if (msg[msg_len - 1] != 0xbc) {
  187. printf("%s: invalid pss padding (0xbc is missing)\n", __func__);
  188. ret = -EINVAL;
  189. goto out;
  190. }
  191. /* step 5 */
  192. memcpy(masked_db, msg, masked_db_len);
  193. memcpy(h, msg + masked_db_len, h_len);
  194. /* step 6 */
  195. leftmost_mask = (0xff >> (8 - leftmost_bits)) << (8 - leftmost_bits);
  196. if (masked_db[0] & leftmost_mask) {
  197. printf("%s: invalid pss padding ", __func__);
  198. printf("(leftmost bit of maskedDB not zero)\n");
  199. ret = -EINVAL;
  200. goto out;
  201. }
  202. /* step 7 */
  203. mask_generation_function1(checksum, h, h_len, db_mask, db_mask_len);
  204. /* step 8 */
  205. for (i = 0; i < db_len; i++)
  206. db[i] = masked_db[i] ^ db_mask[i];
  207. /* step 9 */
  208. db[0] &= 0xff >> leftmost_bits;
  209. /* step 10 */
  210. if (db[0] != 0x01) {
  211. printf("%s: invalid pss padding ", __func__);
  212. printf("(leftmost byte of db isn't 0x01)\n");
  213. ret = EINVAL;
  214. goto out;
  215. }
  216. /* step 11 */
  217. memcpy(salt, &db[1], salt_len);
  218. /* step 12 & 13 */
  219. compute_hash_prime(checksum, pad_zero, 8,
  220. (uint8_t *)hash, hash_len,
  221. salt, salt_len, hprime);
  222. /* step 14 */
  223. ret = memcmp(h, hprime, hash_len);
  224. out:
  225. free(hprime);
  226. free(salt);
  227. free(db);
  228. free(db_mask);
  229. free(h);
  230. free(masked_db);
  231. return ret;
  232. }
  233. #endif
  234. /**
  235. * rsa_verify_key() - Verify a signature against some data using RSA Key
  236. *
  237. * Verify a RSA PKCS1.5 signature against an expected hash using
  238. * the RSA Key properties in prop structure.
  239. *
  240. * @info: Specifies key and FIT information
  241. * @prop: Specifies key
  242. * @sig: Signature
  243. * @sig_len: Number of bytes in signature
  244. * @hash: Pointer to the expected hash
  245. * @key_len: Number of bytes in rsa key
  246. * @return 0 if verified, -ve on error
  247. */
  248. static int rsa_verify_key(struct image_sign_info *info,
  249. struct key_prop *prop, const uint8_t *sig,
  250. const uint32_t sig_len, const uint8_t *hash,
  251. const uint32_t key_len)
  252. {
  253. int ret;
  254. #if !defined(USE_HOSTCC)
  255. struct udevice *mod_exp_dev;
  256. #endif
  257. struct checksum_algo *checksum = info->checksum;
  258. struct padding_algo *padding = info->padding;
  259. int hash_len;
  260. if (!prop || !sig || !hash || !checksum)
  261. return -EIO;
  262. if (sig_len != (prop->num_bits / 8)) {
  263. debug("Signature is of incorrect length %d\n", sig_len);
  264. return -EINVAL;
  265. }
  266. debug("Checksum algorithm: %s", checksum->name);
  267. /* Sanity check for stack size */
  268. if (sig_len > RSA_MAX_SIG_BITS / 8) {
  269. debug("Signature length %u exceeds maximum %d\n", sig_len,
  270. RSA_MAX_SIG_BITS / 8);
  271. return -EINVAL;
  272. }
  273. uint8_t buf[sig_len];
  274. hash_len = checksum->checksum_len;
  275. #if !defined(USE_HOSTCC)
  276. ret = uclass_get_device(UCLASS_MOD_EXP, 0, &mod_exp_dev);
  277. if (ret) {
  278. printf("RSA: Can't find Modular Exp implementation\n");
  279. return -EINVAL;
  280. }
  281. ret = rsa_mod_exp(mod_exp_dev, sig, sig_len, prop, buf);
  282. #else
  283. ret = rsa_mod_exp_sw(sig, sig_len, prop, buf);
  284. #endif
  285. if (ret) {
  286. debug("Error in Modular exponentation\n");
  287. return ret;
  288. }
  289. ret = padding->verify(info, buf, key_len, hash, hash_len);
  290. if (ret) {
  291. debug("In RSAVerify(): padding check failed!\n");
  292. return ret;
  293. }
  294. return 0;
  295. }
  296. /**
  297. * rsa_verify_with_keynode() - Verify a signature against some data using
  298. * information in node with prperties of RSA Key like modulus, exponent etc.
  299. *
  300. * Parse sign-node and fill a key_prop structure with properties of the
  301. * key. Verify a RSA PKCS1.5 signature against an expected hash using
  302. * the properties parsed
  303. *
  304. * @info: Specifies key and FIT information
  305. * @hash: Pointer to the expected hash
  306. * @sig: Signature
  307. * @sig_len: Number of bytes in signature
  308. * @node: Node having the RSA Key properties
  309. * @return 0 if verified, -ve on error
  310. */
  311. static int rsa_verify_with_keynode(struct image_sign_info *info,
  312. const void *hash, uint8_t *sig,
  313. uint sig_len, int node)
  314. {
  315. const void *blob = info->fdt_blob;
  316. struct key_prop prop;
  317. int length;
  318. int ret = 0;
  319. if (node < 0) {
  320. debug("%s: Skipping invalid node", __func__);
  321. return -EBADF;
  322. }
  323. prop.num_bits = fdtdec_get_int(blob, node, "rsa,num-bits", 0);
  324. prop.n0inv = fdtdec_get_int(blob, node, "rsa,n0-inverse", 0);
  325. prop.public_exponent = fdt_getprop(blob, node, "rsa,exponent", &length);
  326. if (!prop.public_exponent || length < sizeof(uint64_t))
  327. prop.public_exponent = NULL;
  328. prop.exp_len = sizeof(uint64_t);
  329. prop.modulus = fdt_getprop(blob, node, "rsa,modulus", NULL);
  330. prop.rr = fdt_getprop(blob, node, "rsa,r-squared", NULL);
  331. if (!prop.num_bits || !prop.modulus) {
  332. debug("%s: Missing RSA key info", __func__);
  333. return -EFAULT;
  334. }
  335. ret = rsa_verify_key(info, &prop, sig, sig_len, hash,
  336. info->crypto->key_len);
  337. return ret;
  338. }
  339. int rsa_verify(struct image_sign_info *info,
  340. const struct image_region region[], int region_count,
  341. uint8_t *sig, uint sig_len)
  342. {
  343. const void *blob = info->fdt_blob;
  344. /* Reserve memory for maximum checksum-length */
  345. uint8_t hash[info->crypto->key_len];
  346. int ndepth, noffset;
  347. int sig_node, node;
  348. char name[100];
  349. int ret;
  350. /*
  351. * Verify that the checksum-length does not exceed the
  352. * rsa-signature-length
  353. */
  354. if (info->checksum->checksum_len >
  355. info->crypto->key_len) {
  356. debug("%s: invlaid checksum-algorithm %s for %s\n",
  357. __func__, info->checksum->name, info->crypto->name);
  358. return -EINVAL;
  359. }
  360. sig_node = fdt_subnode_offset(blob, 0, FIT_SIG_NODENAME);
  361. if (sig_node < 0) {
  362. debug("%s: No signature node found\n", __func__);
  363. return -ENOENT;
  364. }
  365. /* Calculate checksum with checksum-algorithm */
  366. ret = info->checksum->calculate(info->checksum->name,
  367. region, region_count, hash);
  368. if (ret < 0) {
  369. debug("%s: Error in checksum calculation\n", __func__);
  370. return -EINVAL;
  371. }
  372. /* See if we must use a particular key */
  373. if (info->required_keynode != -1) {
  374. ret = rsa_verify_with_keynode(info, hash, sig, sig_len,
  375. info->required_keynode);
  376. return ret;
  377. }
  378. /* Look for a key that matches our hint */
  379. snprintf(name, sizeof(name), "key-%s", info->keyname);
  380. node = fdt_subnode_offset(blob, sig_node, name);
  381. ret = rsa_verify_with_keynode(info, hash, sig, sig_len, node);
  382. if (!ret)
  383. return ret;
  384. /* No luck, so try each of the keys in turn */
  385. for (ndepth = 0, noffset = fdt_next_node(info->fit, sig_node, &ndepth);
  386. (noffset >= 0) && (ndepth > 0);
  387. noffset = fdt_next_node(info->fit, noffset, &ndepth)) {
  388. if (ndepth == 1 && noffset != node) {
  389. ret = rsa_verify_with_keynode(info, hash, sig, sig_len,
  390. noffset);
  391. if (!ret)
  392. break;
  393. }
  394. }
  395. return ret;
  396. }