rsa-verify.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 <asm/errno.h>
  12. #include <asm/types.h>
  13. #include <asm/unaligned.h>
  14. #else
  15. #include "fdt_host.h"
  16. #include "mkimage.h"
  17. #include <fdt_support.h>
  18. #endif
  19. #include <u-boot/rsa.h>
  20. #include <u-boot/sha1.h>
  21. #include <u-boot/sha256.h>
  22. #define UINT64_MULT32(v, multby) (((uint64_t)(v)) * ((uint32_t)(multby)))
  23. #define get_unaligned_be32(a) fdt32_to_cpu(*(uint32_t *)a)
  24. #define put_unaligned_be32(a, b) (*(uint32_t *)(b) = cpu_to_fdt32(a))
  25. /**
  26. * subtract_modulus() - subtract modulus from the given value
  27. *
  28. * @key: Key containing modulus to subtract
  29. * @num: Number to subtract modulus from, as little endian word array
  30. */
  31. static void subtract_modulus(const struct rsa_public_key *key, uint32_t num[])
  32. {
  33. int64_t acc = 0;
  34. uint i;
  35. for (i = 0; i < key->len; i++) {
  36. acc += (uint64_t)num[i] - key->modulus[i];
  37. num[i] = (uint32_t)acc;
  38. acc >>= 32;
  39. }
  40. }
  41. /**
  42. * greater_equal_modulus() - check if a value is >= modulus
  43. *
  44. * @key: Key containing modulus to check
  45. * @num: Number to check against modulus, as little endian word array
  46. * @return 0 if num < modulus, 1 if num >= modulus
  47. */
  48. static int greater_equal_modulus(const struct rsa_public_key *key,
  49. uint32_t num[])
  50. {
  51. uint32_t i;
  52. for (i = key->len - 1; i >= 0; i--) {
  53. if (num[i] < key->modulus[i])
  54. return 0;
  55. if (num[i] > key->modulus[i])
  56. return 1;
  57. }
  58. return 1; /* equal */
  59. }
  60. /**
  61. * montgomery_mul_add_step() - Perform montgomery multiply-add step
  62. *
  63. * Operation: montgomery result[] += a * b[] / n0inv % modulus
  64. *
  65. * @key: RSA key
  66. * @result: Place to put result, as little endian word array
  67. * @a: Multiplier
  68. * @b: Multiplicand, as little endian word array
  69. */
  70. static void montgomery_mul_add_step(const struct rsa_public_key *key,
  71. uint32_t result[], const uint32_t a, const uint32_t b[])
  72. {
  73. uint64_t acc_a, acc_b;
  74. uint32_t d0;
  75. uint i;
  76. acc_a = (uint64_t)a * b[0] + result[0];
  77. d0 = (uint32_t)acc_a * key->n0inv;
  78. acc_b = (uint64_t)d0 * key->modulus[0] + (uint32_t)acc_a;
  79. for (i = 1; i < key->len; i++) {
  80. acc_a = (acc_a >> 32) + (uint64_t)a * b[i] + result[i];
  81. acc_b = (acc_b >> 32) + (uint64_t)d0 * key->modulus[i] +
  82. (uint32_t)acc_a;
  83. result[i - 1] = (uint32_t)acc_b;
  84. }
  85. acc_a = (acc_a >> 32) + (acc_b >> 32);
  86. result[i - 1] = (uint32_t)acc_a;
  87. if (acc_a >> 32)
  88. subtract_modulus(key, result);
  89. }
  90. /**
  91. * montgomery_mul() - Perform montgomery mutitply
  92. *
  93. * Operation: montgomery result[] = a[] * b[] / n0inv % modulus
  94. *
  95. * @key: RSA key
  96. * @result: Place to put result, as little endian word array
  97. * @a: Multiplier, as little endian word array
  98. * @b: Multiplicand, as little endian word array
  99. */
  100. static void montgomery_mul(const struct rsa_public_key *key,
  101. uint32_t result[], uint32_t a[], const uint32_t b[])
  102. {
  103. uint i;
  104. for (i = 0; i < key->len; ++i)
  105. result[i] = 0;
  106. for (i = 0; i < key->len; ++i)
  107. montgomery_mul_add_step(key, result, a[i], b);
  108. }
  109. /**
  110. * pow_mod() - in-place public exponentiation
  111. *
  112. * @key: RSA key
  113. * @inout: Big-endian word array containing value and result
  114. */
  115. static int pow_mod(const struct rsa_public_key *key, uint32_t *inout)
  116. {
  117. uint32_t *result, *ptr;
  118. uint i;
  119. /* Sanity check for stack size - key->len is in 32-bit words */
  120. if (key->len > RSA_MAX_KEY_BITS / 32) {
  121. debug("RSA key words %u exceeds maximum %d\n", key->len,
  122. RSA_MAX_KEY_BITS / 32);
  123. return -EINVAL;
  124. }
  125. uint32_t val[key->len], acc[key->len], tmp[key->len];
  126. result = tmp; /* Re-use location. */
  127. /* Convert from big endian byte array to little endian word array. */
  128. for (i = 0, ptr = inout + key->len - 1; i < key->len; i++, ptr--)
  129. val[i] = get_unaligned_be32(ptr);
  130. montgomery_mul(key, acc, val, key->rr); /* axx = a * RR / R mod M */
  131. for (i = 0; i < 16; i += 2) {
  132. montgomery_mul(key, tmp, acc, acc); /* tmp = acc^2 / R mod M */
  133. montgomery_mul(key, acc, tmp, tmp); /* acc = tmp^2 / R mod M */
  134. }
  135. montgomery_mul(key, result, acc, val); /* result = XX * a / R mod M */
  136. /* Make sure result < mod; result is at most 1x mod too large. */
  137. if (greater_equal_modulus(key, result))
  138. subtract_modulus(key, result);
  139. /* Convert to bigendian byte array */
  140. for (i = key->len - 1, ptr = inout; (int)i >= 0; i--, ptr++)
  141. put_unaligned_be32(result[i], ptr);
  142. return 0;
  143. }
  144. static int rsa_verify_key(const struct rsa_public_key *key, const uint8_t *sig,
  145. const uint32_t sig_len, const uint8_t *hash,
  146. struct checksum_algo *algo)
  147. {
  148. const uint8_t *padding;
  149. int pad_len;
  150. int ret;
  151. if (!key || !sig || !hash || !algo)
  152. return -EIO;
  153. if (sig_len != (key->len * sizeof(uint32_t))) {
  154. debug("Signature is of incorrect length %d\n", sig_len);
  155. return -EINVAL;
  156. }
  157. debug("Checksum algorithm: %s", algo->name);
  158. /* Sanity check for stack size */
  159. if (sig_len > RSA_MAX_SIG_BITS / 8) {
  160. debug("Signature length %u exceeds maximum %d\n", sig_len,
  161. RSA_MAX_SIG_BITS / 8);
  162. return -EINVAL;
  163. }
  164. uint32_t buf[sig_len / sizeof(uint32_t)];
  165. memcpy(buf, sig, sig_len);
  166. ret = pow_mod(key, buf);
  167. if (ret)
  168. return ret;
  169. padding = algo->rsa_padding;
  170. pad_len = algo->pad_len - algo->checksum_len;
  171. /* Check pkcs1.5 padding bytes. */
  172. if (memcmp(buf, padding, pad_len)) {
  173. debug("In RSAVerify(): Padding check failed!\n");
  174. return -EINVAL;
  175. }
  176. /* Check hash. */
  177. if (memcmp((uint8_t *)buf + pad_len, hash, sig_len - pad_len)) {
  178. debug("In RSAVerify(): Hash check failed!\n");
  179. return -EACCES;
  180. }
  181. return 0;
  182. }
  183. static void rsa_convert_big_endian(uint32_t *dst, const uint32_t *src, int len)
  184. {
  185. int i;
  186. for (i = 0; i < len; i++)
  187. dst[i] = fdt32_to_cpu(src[len - 1 - i]);
  188. }
  189. static int rsa_verify_with_keynode(struct image_sign_info *info,
  190. const void *hash, uint8_t *sig, uint sig_len, int node)
  191. {
  192. const void *blob = info->fdt_blob;
  193. struct rsa_public_key key;
  194. const void *modulus, *rr;
  195. int ret;
  196. if (node < 0) {
  197. debug("%s: Skipping invalid node", __func__);
  198. return -EBADF;
  199. }
  200. if (!fdt_getprop(blob, node, "rsa,n0-inverse", NULL)) {
  201. debug("%s: Missing rsa,n0-inverse", __func__);
  202. return -EFAULT;
  203. }
  204. key.len = fdtdec_get_int(blob, node, "rsa,num-bits", 0);
  205. key.n0inv = fdtdec_get_int(blob, node, "rsa,n0-inverse", 0);
  206. modulus = fdt_getprop(blob, node, "rsa,modulus", NULL);
  207. rr = fdt_getprop(blob, node, "rsa,r-squared", NULL);
  208. if (!key.len || !modulus || !rr) {
  209. debug("%s: Missing RSA key info", __func__);
  210. return -EFAULT;
  211. }
  212. /* Sanity check for stack size */
  213. if (key.len > RSA_MAX_KEY_BITS || key.len < RSA_MIN_KEY_BITS) {
  214. debug("RSA key bits %u outside allowed range %d..%d\n",
  215. key.len, RSA_MIN_KEY_BITS, RSA_MAX_KEY_BITS);
  216. return -EFAULT;
  217. }
  218. key.len /= sizeof(uint32_t) * 8;
  219. uint32_t key1[key.len], key2[key.len];
  220. key.modulus = key1;
  221. key.rr = key2;
  222. rsa_convert_big_endian(key.modulus, modulus, key.len);
  223. rsa_convert_big_endian(key.rr, rr, key.len);
  224. if (!key.modulus || !key.rr) {
  225. debug("%s: Out of memory", __func__);
  226. return -ENOMEM;
  227. }
  228. debug("key length %d\n", key.len);
  229. ret = rsa_verify_key(&key, sig, sig_len, hash, info->algo->checksum);
  230. if (ret) {
  231. printf("%s: RSA failed to verify: %d\n", __func__, ret);
  232. return ret;
  233. }
  234. return 0;
  235. }
  236. int rsa_verify(struct image_sign_info *info,
  237. const struct image_region region[], int region_count,
  238. uint8_t *sig, uint sig_len)
  239. {
  240. const void *blob = info->fdt_blob;
  241. /* Reserve memory for maximum checksum-length */
  242. uint8_t hash[info->algo->checksum->pad_len];
  243. int ndepth, noffset;
  244. int sig_node, node;
  245. char name[100];
  246. int ret;
  247. /*
  248. * Verify that the checksum-length does not exceed the
  249. * rsa-signature-length
  250. */
  251. if (info->algo->checksum->checksum_len >
  252. info->algo->checksum->pad_len) {
  253. debug("%s: invlaid checksum-algorithm %s for %s\n",
  254. __func__, info->algo->checksum->name, info->algo->name);
  255. return -EINVAL;
  256. }
  257. sig_node = fdt_subnode_offset(blob, 0, FIT_SIG_NODENAME);
  258. if (sig_node < 0) {
  259. debug("%s: No signature node found\n", __func__);
  260. return -ENOENT;
  261. }
  262. /* Calculate checksum with checksum-algorithm */
  263. info->algo->checksum->calculate(region, region_count, hash);
  264. /* See if we must use a particular key */
  265. if (info->required_keynode != -1) {
  266. ret = rsa_verify_with_keynode(info, hash, sig, sig_len,
  267. info->required_keynode);
  268. if (!ret)
  269. return ret;
  270. }
  271. /* Look for a key that matches our hint */
  272. snprintf(name, sizeof(name), "key-%s", info->keyname);
  273. node = fdt_subnode_offset(blob, sig_node, name);
  274. ret = rsa_verify_with_keynode(info, hash, sig, sig_len, node);
  275. if (!ret)
  276. return ret;
  277. /* No luck, so try each of the keys in turn */
  278. for (ndepth = 0, noffset = fdt_next_node(info->fit, sig_node, &ndepth);
  279. (noffset >= 0) && (ndepth > 0);
  280. noffset = fdt_next_node(info->fit, noffset, &ndepth)) {
  281. if (ndepth == 1 && noffset != node) {
  282. ret = rsa_verify_with_keynode(info, hash, sig, sig_len,
  283. noffset);
  284. if (!ret)
  285. break;
  286. }
  287. }
  288. return ret;
  289. }