rsa-verify.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. /**
  55. * rsa_verify_key() - Verify a signature against some data using RSA Key
  56. *
  57. * Verify a RSA PKCS1.5 signature against an expected hash using
  58. * the RSA Key properties in prop structure.
  59. *
  60. * @prop: Specifies key
  61. * @sig: Signature
  62. * @sig_len: Number of bytes in signature
  63. * @hash: Pointer to the expected hash
  64. * @key_len: Number of bytes in rsa key
  65. * @algo: Checksum algo structure having information on DER encoding etc.
  66. * @return 0 if verified, -ve on error
  67. */
  68. static int rsa_verify_key(struct key_prop *prop, const uint8_t *sig,
  69. const uint32_t sig_len, const uint8_t *hash,
  70. const uint32_t key_len, struct checksum_algo *algo)
  71. {
  72. int pad_len;
  73. int ret;
  74. #if !defined(USE_HOSTCC)
  75. struct udevice *mod_exp_dev;
  76. #endif
  77. if (!prop || !sig || !hash || !algo)
  78. return -EIO;
  79. if (sig_len != (prop->num_bits / 8)) {
  80. debug("Signature is of incorrect length %d\n", sig_len);
  81. return -EINVAL;
  82. }
  83. debug("Checksum algorithm: %s", algo->name);
  84. /* Sanity check for stack size */
  85. if (sig_len > RSA_MAX_SIG_BITS / 8) {
  86. debug("Signature length %u exceeds maximum %d\n", sig_len,
  87. RSA_MAX_SIG_BITS / 8);
  88. return -EINVAL;
  89. }
  90. uint8_t buf[sig_len];
  91. #if !defined(USE_HOSTCC)
  92. ret = uclass_get_device(UCLASS_MOD_EXP, 0, &mod_exp_dev);
  93. if (ret) {
  94. printf("RSA: Can't find Modular Exp implementation\n");
  95. return -EINVAL;
  96. }
  97. ret = rsa_mod_exp(mod_exp_dev, sig, sig_len, prop, buf);
  98. #else
  99. ret = rsa_mod_exp_sw(sig, sig_len, prop, buf);
  100. #endif
  101. if (ret) {
  102. debug("Error in Modular exponentation\n");
  103. return ret;
  104. }
  105. pad_len = key_len - algo->checksum_len;
  106. /* Check pkcs1.5 padding bytes. */
  107. ret = rsa_verify_padding(buf, pad_len, algo);
  108. if (ret) {
  109. debug("In RSAVerify(): Padding check failed!\n");
  110. return -EINVAL;
  111. }
  112. /* Check hash. */
  113. if (memcmp((uint8_t *)buf + pad_len, hash, sig_len - pad_len)) {
  114. debug("In RSAVerify(): Hash check failed!\n");
  115. return -EACCES;
  116. }
  117. return 0;
  118. }
  119. /**
  120. * rsa_verify_with_keynode() - Verify a signature against some data using
  121. * information in node with prperties of RSA Key like modulus, exponent etc.
  122. *
  123. * Parse sign-node and fill a key_prop structure with properties of the
  124. * key. Verify a RSA PKCS1.5 signature against an expected hash using
  125. * the properties parsed
  126. *
  127. * @info: Specifies key and FIT information
  128. * @hash: Pointer to the expected hash
  129. * @sig: Signature
  130. * @sig_len: Number of bytes in signature
  131. * @node: Node having the RSA Key properties
  132. * @return 0 if verified, -ve on error
  133. */
  134. static int rsa_verify_with_keynode(struct image_sign_info *info,
  135. const void *hash, uint8_t *sig,
  136. uint sig_len, int node)
  137. {
  138. const void *blob = info->fdt_blob;
  139. struct key_prop prop;
  140. int length;
  141. int ret = 0;
  142. if (node < 0) {
  143. debug("%s: Skipping invalid node", __func__);
  144. return -EBADF;
  145. }
  146. prop.num_bits = fdtdec_get_int(blob, node, "rsa,num-bits", 0);
  147. prop.n0inv = fdtdec_get_int(blob, node, "rsa,n0-inverse", 0);
  148. prop.public_exponent = fdt_getprop(blob, node, "rsa,exponent", &length);
  149. if (!prop.public_exponent || length < sizeof(uint64_t))
  150. prop.public_exponent = NULL;
  151. prop.exp_len = sizeof(uint64_t);
  152. prop.modulus = fdt_getprop(blob, node, "rsa,modulus", NULL);
  153. prop.rr = fdt_getprop(blob, node, "rsa,r-squared", NULL);
  154. if (!prop.num_bits || !prop.modulus) {
  155. debug("%s: Missing RSA key info", __func__);
  156. return -EFAULT;
  157. }
  158. ret = rsa_verify_key(&prop, sig, sig_len, hash,
  159. info->crypto->key_len, info->checksum);
  160. return ret;
  161. }
  162. int rsa_verify(struct image_sign_info *info,
  163. const struct image_region region[], int region_count,
  164. uint8_t *sig, uint sig_len)
  165. {
  166. const void *blob = info->fdt_blob;
  167. /* Reserve memory for maximum checksum-length */
  168. uint8_t hash[info->crypto->key_len];
  169. int ndepth, noffset;
  170. int sig_node, node;
  171. char name[100];
  172. int ret;
  173. /*
  174. * Verify that the checksum-length does not exceed the
  175. * rsa-signature-length
  176. */
  177. if (info->checksum->checksum_len >
  178. info->crypto->key_len) {
  179. debug("%s: invlaid checksum-algorithm %s for %s\n",
  180. __func__, info->checksum->name, info->crypto->name);
  181. return -EINVAL;
  182. }
  183. sig_node = fdt_subnode_offset(blob, 0, FIT_SIG_NODENAME);
  184. if (sig_node < 0) {
  185. debug("%s: No signature node found\n", __func__);
  186. return -ENOENT;
  187. }
  188. /* Calculate checksum with checksum-algorithm */
  189. ret = info->checksum->calculate(info->checksum->name,
  190. region, region_count, hash);
  191. if (ret < 0) {
  192. debug("%s: Error in checksum calculation\n", __func__);
  193. return -EINVAL;
  194. }
  195. /* See if we must use a particular key */
  196. if (info->required_keynode != -1) {
  197. ret = rsa_verify_with_keynode(info, hash, sig, sig_len,
  198. info->required_keynode);
  199. if (!ret)
  200. return ret;
  201. }
  202. /* Look for a key that matches our hint */
  203. snprintf(name, sizeof(name), "key-%s", info->keyname);
  204. node = fdt_subnode_offset(blob, sig_node, name);
  205. ret = rsa_verify_with_keynode(info, hash, sig, sig_len, node);
  206. if (!ret)
  207. return ret;
  208. /* No luck, so try each of the keys in turn */
  209. for (ndepth = 0, noffset = fdt_next_node(info->fit, sig_node, &ndepth);
  210. (noffset >= 0) && (ndepth > 0);
  211. noffset = fdt_next_node(info->fit, noffset, &ndepth)) {
  212. if (ndepth == 1 && noffset != node) {
  213. ret = rsa_verify_with_keynode(info, hash, sig, sig_len,
  214. noffset);
  215. if (!ret)
  216. break;
  217. }
  218. }
  219. return ret;
  220. }