rsa-verify.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * Copyright (c) 2013, Google Inc.
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #ifndef USE_HOSTCC
  7. #include <common.h>
  8. #include <fdtdec.h>
  9. #include <asm/types.h>
  10. #include <asm/byteorder.h>
  11. #include <linux/errno.h>
  12. #include <asm/types.h>
  13. #include <asm/unaligned.h>
  14. #include <dm.h>
  15. #else
  16. #include "fdt_host.h"
  17. #include "mkimage.h"
  18. #include <fdt_support.h>
  19. #endif
  20. #include <u-boot/rsa-mod-exp.h>
  21. #include <u-boot/rsa.h>
  22. /* Default public exponent for backward compatibility */
  23. #define RSA_DEFAULT_PUBEXP 65537
  24. /**
  25. * rsa_verify_padding() - Verify RSA message padding is valid
  26. *
  27. * Verify a RSA message's padding is consistent with PKCS1.5
  28. * padding as described in the RSA PKCS#1 v2.1 standard.
  29. *
  30. * @msg: Padded message
  31. * @pad_len: Number of expected padding bytes
  32. * @algo: Checksum algo structure having information on DER encoding etc.
  33. * @return 0 on success, != 0 on failure
  34. */
  35. static int rsa_verify_padding(const uint8_t *msg, const int pad_len,
  36. struct checksum_algo *algo)
  37. {
  38. int ff_len;
  39. int ret;
  40. /* first byte must be 0x00 */
  41. ret = *msg++;
  42. /* second byte must be 0x01 */
  43. ret |= *msg++ ^ 0x01;
  44. /* next ff_len bytes must be 0xff */
  45. ff_len = pad_len - algo->der_len - 3;
  46. ret |= *msg ^ 0xff;
  47. ret |= memcmp(msg, msg+1, ff_len-1);
  48. msg += ff_len;
  49. /* next byte must be 0x00 */
  50. ret |= *msg++;
  51. /* next der_len bytes must match der_prefix */
  52. ret |= memcmp(msg, algo->der_prefix, algo->der_len);
  53. return ret;
  54. }
  55. /**
  56. * rsa_verify_key() - Verify a signature against some data using RSA Key
  57. *
  58. * Verify a RSA PKCS1.5 signature against an expected hash using
  59. * the RSA Key properties in prop structure.
  60. *
  61. * @prop: Specifies key
  62. * @sig: Signature
  63. * @sig_len: Number of bytes in signature
  64. * @hash: Pointer to the expected hash
  65. * @key_len: Number of bytes in rsa key
  66. * @algo: Checksum algo structure having information on DER encoding etc.
  67. * @return 0 if verified, -ve on error
  68. */
  69. static int rsa_verify_key(struct key_prop *prop, const uint8_t *sig,
  70. const uint32_t sig_len, const uint8_t *hash,
  71. const uint32_t key_len, struct checksum_algo *algo)
  72. {
  73. int pad_len;
  74. int ret;
  75. #if !defined(USE_HOSTCC)
  76. struct udevice *mod_exp_dev;
  77. #endif
  78. if (!prop || !sig || !hash || !algo)
  79. return -EIO;
  80. if (sig_len != (prop->num_bits / 8)) {
  81. debug("Signature is of incorrect length %d\n", sig_len);
  82. return -EINVAL;
  83. }
  84. debug("Checksum algorithm: %s", algo->name);
  85. /* Sanity check for stack size */
  86. if (sig_len > RSA_MAX_SIG_BITS / 8) {
  87. debug("Signature length %u exceeds maximum %d\n", sig_len,
  88. RSA_MAX_SIG_BITS / 8);
  89. return -EINVAL;
  90. }
  91. uint8_t buf[sig_len];
  92. #if !defined(USE_HOSTCC)
  93. ret = uclass_get_device(UCLASS_MOD_EXP, 0, &mod_exp_dev);
  94. if (ret) {
  95. printf("RSA: Can't find Modular Exp implementation\n");
  96. return -EINVAL;
  97. }
  98. ret = rsa_mod_exp(mod_exp_dev, sig, sig_len, prop, buf);
  99. #else
  100. ret = rsa_mod_exp_sw(sig, sig_len, prop, buf);
  101. #endif
  102. if (ret) {
  103. debug("Error in Modular exponentation\n");
  104. return ret;
  105. }
  106. pad_len = key_len - algo->checksum_len;
  107. /* Check pkcs1.5 padding bytes. */
  108. ret = rsa_verify_padding(buf, pad_len, algo);
  109. if (ret) {
  110. debug("In RSAVerify(): Padding check failed!\n");
  111. return -EINVAL;
  112. }
  113. /* Check hash. */
  114. if (memcmp((uint8_t *)buf + pad_len, hash, sig_len - pad_len)) {
  115. debug("In RSAVerify(): Hash check failed!\n");
  116. return -EACCES;
  117. }
  118. return 0;
  119. }
  120. /**
  121. * rsa_verify_with_keynode() - Verify a signature against some data using
  122. * information in node with prperties of RSA Key like modulus, exponent etc.
  123. *
  124. * Parse sign-node and fill a key_prop structure with properties of the
  125. * key. Verify a RSA PKCS1.5 signature against an expected hash using
  126. * the properties parsed
  127. *
  128. * @info: Specifies key and FIT information
  129. * @hash: Pointer to the expected hash
  130. * @sig: Signature
  131. * @sig_len: Number of bytes in signature
  132. * @node: Node having the RSA Key properties
  133. * @return 0 if verified, -ve on error
  134. */
  135. static int rsa_verify_with_keynode(struct image_sign_info *info,
  136. const void *hash, uint8_t *sig,
  137. uint sig_len, int node)
  138. {
  139. const void *blob = info->fdt_blob;
  140. struct key_prop prop;
  141. int length;
  142. int ret = 0;
  143. if (node < 0) {
  144. debug("%s: Skipping invalid node", __func__);
  145. return -EBADF;
  146. }
  147. prop.num_bits = fdtdec_get_int(blob, node, "rsa,num-bits", 0);
  148. prop.n0inv = fdtdec_get_int(blob, node, "rsa,n0-inverse", 0);
  149. prop.public_exponent = fdt_getprop(blob, node, "rsa,exponent", &length);
  150. if (!prop.public_exponent || length < sizeof(uint64_t))
  151. prop.public_exponent = NULL;
  152. prop.exp_len = sizeof(uint64_t);
  153. prop.modulus = fdt_getprop(blob, node, "rsa,modulus", NULL);
  154. prop.rr = fdt_getprop(blob, node, "rsa,r-squared", NULL);
  155. if (!prop.num_bits || !prop.modulus) {
  156. debug("%s: Missing RSA key info", __func__);
  157. return -EFAULT;
  158. }
  159. ret = rsa_verify_key(&prop, sig, sig_len, hash,
  160. info->crypto->key_len, info->checksum);
  161. return ret;
  162. }
  163. int rsa_verify(struct image_sign_info *info,
  164. const struct image_region region[], int region_count,
  165. uint8_t *sig, uint sig_len)
  166. {
  167. const void *blob = info->fdt_blob;
  168. /* Reserve memory for maximum checksum-length */
  169. uint8_t hash[info->crypto->key_len];
  170. int ndepth, noffset;
  171. int sig_node, node;
  172. char name[100];
  173. int ret;
  174. /*
  175. * Verify that the checksum-length does not exceed the
  176. * rsa-signature-length
  177. */
  178. if (info->checksum->checksum_len >
  179. info->crypto->key_len) {
  180. debug("%s: invlaid checksum-algorithm %s for %s\n",
  181. __func__, info->checksum->name, info->crypto->name);
  182. return -EINVAL;
  183. }
  184. sig_node = fdt_subnode_offset(blob, 0, FIT_SIG_NODENAME);
  185. if (sig_node < 0) {
  186. debug("%s: No signature node found\n", __func__);
  187. return -ENOENT;
  188. }
  189. /* Calculate checksum with checksum-algorithm */
  190. ret = info->checksum->calculate(info->checksum->name,
  191. region, region_count, hash);
  192. if (ret < 0) {
  193. debug("%s: Error in checksum calculation\n", __func__);
  194. return -EINVAL;
  195. }
  196. /* See if we must use a particular key */
  197. if (info->required_keynode != -1) {
  198. ret = rsa_verify_with_keynode(info, hash, sig, sig_len,
  199. info->required_keynode);
  200. if (!ret)
  201. return ret;
  202. }
  203. /* Look for a key that matches our hint */
  204. snprintf(name, sizeof(name), "key-%s", info->keyname);
  205. node = fdt_subnode_offset(blob, sig_node, name);
  206. ret = rsa_verify_with_keynode(info, hash, sig, sig_len, node);
  207. if (!ret)
  208. return ret;
  209. /* No luck, so try each of the keys in turn */
  210. for (ndepth = 0, noffset = fdt_next_node(info->fit, sig_node, &ndepth);
  211. (noffset >= 0) && (ndepth > 0);
  212. noffset = fdt_next_node(info->fit, noffset, &ndepth)) {
  213. if (ndepth == 1 && noffset != node) {
  214. ret = rsa_verify_with_keynode(info, hash, sig, sig_len,
  215. noffset);
  216. if (!ret)
  217. break;
  218. }
  219. }
  220. return ret;
  221. }