rsa-verify.c 8.5 KB

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