rsa-verify.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /*
  2. * Copyright (c) 2013, Google Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of
  7. * the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  17. * MA 02111-1307 USA
  18. */
  19. #include <common.h>
  20. #include <fdtdec.h>
  21. #include <rsa.h>
  22. #include <sha1.h>
  23. #include <asm/byteorder.h>
  24. #include <asm/errno.h>
  25. #include <asm/unaligned.h>
  26. /**
  27. * struct rsa_public_key - holder for a public key
  28. *
  29. * An RSA public key consists of a modulus (typically called N), the inverse
  30. * and R^2, where R is 2^(# key bits).
  31. */
  32. struct rsa_public_key {
  33. uint len; /* Length of modulus[] in number of uint32_t */
  34. uint32_t n0inv; /* -1 / modulus[0] mod 2^32 */
  35. uint32_t *modulus; /* modulus as little endian array */
  36. uint32_t *rr; /* R^2 as little endian array */
  37. };
  38. #define UINT64_MULT32(v, multby) (((uint64_t)(v)) * ((uint32_t)(multby)))
  39. #define RSA2048_BYTES (2048 / 8)
  40. /* This is the minimum/maximum key size we support, in bits */
  41. #define RSA_MIN_KEY_BITS 2048
  42. #define RSA_MAX_KEY_BITS 2048
  43. /* This is the maximum signature length that we support, in bits */
  44. #define RSA_MAX_SIG_BITS 2048
  45. static const uint8_t padding_sha1_rsa2048[RSA2048_BYTES - SHA1_SUM_LEN] = {
  46. 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  47. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  48. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  49. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  50. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  51. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  52. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  53. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  54. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  55. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  56. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  57. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  58. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  59. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  60. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  61. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  62. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  63. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  64. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  65. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  66. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  67. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  68. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  69. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  70. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  71. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  72. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  73. 0xff, 0xff, 0xff, 0xff, 0x00, 0x30, 0x21, 0x30,
  74. 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a,
  75. 0x05, 0x00, 0x04, 0x14
  76. };
  77. /**
  78. * subtract_modulus() - subtract modulus from the given value
  79. *
  80. * @key: Key containing modulus to subtract
  81. * @num: Number to subtract modulus from, as little endian word array
  82. */
  83. static void subtract_modulus(const struct rsa_public_key *key, uint32_t num[])
  84. {
  85. int64_t acc = 0;
  86. uint i;
  87. for (i = 0; i < key->len; i++) {
  88. acc += (uint64_t)num[i] - key->modulus[i];
  89. num[i] = (uint32_t)acc;
  90. acc >>= 32;
  91. }
  92. }
  93. /**
  94. * greater_equal_modulus() - check if a value is >= modulus
  95. *
  96. * @key: Key containing modulus to check
  97. * @num: Number to check against modulus, as little endian word array
  98. * @return 0 if num < modulus, 1 if num >= modulus
  99. */
  100. static int greater_equal_modulus(const struct rsa_public_key *key,
  101. uint32_t num[])
  102. {
  103. uint32_t i;
  104. for (i = key->len - 1; i >= 0; i--) {
  105. if (num[i] < key->modulus[i])
  106. return 0;
  107. if (num[i] > key->modulus[i])
  108. return 1;
  109. }
  110. return 1; /* equal */
  111. }
  112. /**
  113. * montgomery_mul_add_step() - Perform montgomery multiply-add step
  114. *
  115. * Operation: montgomery result[] += a * b[] / n0inv % modulus
  116. *
  117. * @key: RSA key
  118. * @result: Place to put result, as little endian word array
  119. * @a: Multiplier
  120. * @b: Multiplicand, as little endian word array
  121. */
  122. static void montgomery_mul_add_step(const struct rsa_public_key *key,
  123. uint32_t result[], const uint32_t a, const uint32_t b[])
  124. {
  125. uint64_t acc_a, acc_b;
  126. uint32_t d0;
  127. uint i;
  128. acc_a = (uint64_t)a * b[0] + result[0];
  129. d0 = (uint32_t)acc_a * key->n0inv;
  130. acc_b = (uint64_t)d0 * key->modulus[0] + (uint32_t)acc_a;
  131. for (i = 1; i < key->len; i++) {
  132. acc_a = (acc_a >> 32) + (uint64_t)a * b[i] + result[i];
  133. acc_b = (acc_b >> 32) + (uint64_t)d0 * key->modulus[i] +
  134. (uint32_t)acc_a;
  135. result[i - 1] = (uint32_t)acc_b;
  136. }
  137. acc_a = (acc_a >> 32) + (acc_b >> 32);
  138. result[i - 1] = (uint32_t)acc_a;
  139. if (acc_a >> 32)
  140. subtract_modulus(key, result);
  141. }
  142. /**
  143. * montgomery_mul() - Perform montgomery mutitply
  144. *
  145. * Operation: montgomery result[] = a[] * b[] / n0inv % modulus
  146. *
  147. * @key: RSA key
  148. * @result: Place to put result, as little endian word array
  149. * @a: Multiplier, as little endian word array
  150. * @b: Multiplicand, as little endian word array
  151. */
  152. static void montgomery_mul(const struct rsa_public_key *key,
  153. uint32_t result[], uint32_t a[], const uint32_t b[])
  154. {
  155. uint i;
  156. for (i = 0; i < key->len; ++i)
  157. result[i] = 0;
  158. for (i = 0; i < key->len; ++i)
  159. montgomery_mul_add_step(key, result, a[i], b);
  160. }
  161. /**
  162. * pow_mod() - in-place public exponentiation
  163. *
  164. * @key: RSA key
  165. * @inout: Big-endian word array containing value and result
  166. */
  167. static int pow_mod(const struct rsa_public_key *key, uint32_t *inout)
  168. {
  169. uint32_t *result, *ptr;
  170. uint i;
  171. /* Sanity check for stack size - key->len is in 32-bit words */
  172. if (key->len > RSA_MAX_KEY_BITS / 32) {
  173. debug("RSA key words %u exceeds maximum %d\n", key->len,
  174. RSA_MAX_KEY_BITS / 32);
  175. return -EINVAL;
  176. }
  177. uint32_t val[key->len], acc[key->len], tmp[key->len];
  178. result = tmp; /* Re-use location. */
  179. /* Convert from big endian byte array to little endian word array. */
  180. for (i = 0, ptr = inout + key->len - 1; i < key->len; i++, ptr--)
  181. val[i] = get_unaligned_be32(ptr);
  182. montgomery_mul(key, acc, val, key->rr); /* axx = a * RR / R mod M */
  183. for (i = 0; i < 16; i += 2) {
  184. montgomery_mul(key, tmp, acc, acc); /* tmp = acc^2 / R mod M */
  185. montgomery_mul(key, acc, tmp, tmp); /* acc = tmp^2 / R mod M */
  186. }
  187. montgomery_mul(key, result, acc, val); /* result = XX * a / R mod M */
  188. /* Make sure result < mod; result is at most 1x mod too large. */
  189. if (greater_equal_modulus(key, result))
  190. subtract_modulus(key, result);
  191. /* Convert to bigendian byte array */
  192. for (i = key->len - 1, ptr = inout; (int)i >= 0; i--, ptr++)
  193. put_unaligned_be32(result[i], ptr);
  194. return 0;
  195. }
  196. static int rsa_verify_key(const struct rsa_public_key *key, const uint8_t *sig,
  197. const uint32_t sig_len, const uint8_t *hash)
  198. {
  199. const uint8_t *padding;
  200. int pad_len;
  201. int ret;
  202. if (!key || !sig || !hash)
  203. return -EIO;
  204. if (sig_len != (key->len * sizeof(uint32_t))) {
  205. debug("Signature is of incorrect length %d\n", sig_len);
  206. return -EINVAL;
  207. }
  208. /* Sanity check for stack size */
  209. if (sig_len > RSA_MAX_SIG_BITS / 8) {
  210. debug("Signature length %u exceeds maximum %d\n", sig_len,
  211. RSA_MAX_SIG_BITS / 8);
  212. return -EINVAL;
  213. }
  214. uint32_t buf[sig_len / sizeof(uint32_t)];
  215. memcpy(buf, sig, sig_len);
  216. ret = pow_mod(key, buf);
  217. if (ret)
  218. return ret;
  219. /* Determine padding to use depending on the signature type. */
  220. padding = padding_sha1_rsa2048;
  221. pad_len = RSA2048_BYTES - SHA1_SUM_LEN;
  222. /* Check pkcs1.5 padding bytes. */
  223. if (memcmp(buf, padding, pad_len)) {
  224. debug("In RSAVerify(): Padding check failed!\n");
  225. return -EINVAL;
  226. }
  227. /* Check hash. */
  228. if (memcmp((uint8_t *)buf + pad_len, hash, sig_len - pad_len)) {
  229. debug("In RSAVerify(): Hash check failed!\n");
  230. return -EACCES;
  231. }
  232. return 0;
  233. }
  234. static void rsa_convert_big_endian(uint32_t *dst, const uint32_t *src, int len)
  235. {
  236. int i;
  237. for (i = 0; i < len; i++)
  238. dst[i] = fdt32_to_cpu(src[len - 1 - i]);
  239. }
  240. static int rsa_verify_with_keynode(struct image_sign_info *info,
  241. const void *hash, uint8_t *sig, uint sig_len, int node)
  242. {
  243. const void *blob = info->fdt_blob;
  244. struct rsa_public_key key;
  245. const void *modulus, *rr;
  246. int ret;
  247. if (node < 0) {
  248. debug("%s: Skipping invalid node", __func__);
  249. return -EBADF;
  250. }
  251. if (!fdt_getprop(blob, node, "rsa,n0-inverse", NULL)) {
  252. debug("%s: Missing rsa,n0-inverse", __func__);
  253. return -EFAULT;
  254. }
  255. key.len = fdtdec_get_int(blob, node, "rsa,num-bits", 0);
  256. key.n0inv = fdtdec_get_int(blob, node, "rsa,n0-inverse", 0);
  257. modulus = fdt_getprop(blob, node, "rsa,modulus", NULL);
  258. rr = fdt_getprop(blob, node, "rsa,r-squared", NULL);
  259. if (!key.len || !modulus || !rr) {
  260. debug("%s: Missing RSA key info", __func__);
  261. return -EFAULT;
  262. }
  263. /* Sanity check for stack size */
  264. if (key.len > RSA_MAX_KEY_BITS || key.len < RSA_MIN_KEY_BITS) {
  265. debug("RSA key bits %u outside allowed range %d..%d\n",
  266. key.len, RSA_MIN_KEY_BITS, RSA_MAX_KEY_BITS);
  267. return -EFAULT;
  268. }
  269. key.len /= sizeof(uint32_t) * 8;
  270. uint32_t key1[key.len], key2[key.len];
  271. key.modulus = key1;
  272. key.rr = key2;
  273. rsa_convert_big_endian(key.modulus, modulus, key.len);
  274. rsa_convert_big_endian(key.rr, rr, key.len);
  275. if (!key.modulus || !key.rr) {
  276. debug("%s: Out of memory", __func__);
  277. return -ENOMEM;
  278. }
  279. debug("key length %d\n", key.len);
  280. ret = rsa_verify_key(&key, sig, sig_len, hash);
  281. if (ret) {
  282. printf("%s: RSA failed to verify: %d\n", __func__, ret);
  283. return ret;
  284. }
  285. return 0;
  286. }
  287. int rsa_verify(struct image_sign_info *info,
  288. const struct image_region region[], int region_count,
  289. uint8_t *sig, uint sig_len)
  290. {
  291. const void *blob = info->fdt_blob;
  292. uint8_t hash[SHA1_SUM_LEN];
  293. int ndepth, noffset;
  294. int sig_node, node;
  295. char name[100];
  296. sha1_context ctx;
  297. int ret, i;
  298. sig_node = fdt_subnode_offset(blob, 0, FIT_SIG_NODENAME);
  299. if (sig_node < 0) {
  300. debug("%s: No signature node found\n", __func__);
  301. return -ENOENT;
  302. }
  303. sha1_starts(&ctx);
  304. for (i = 0; i < region_count; i++)
  305. sha1_update(&ctx, region[i].data, region[i].size);
  306. sha1_finish(&ctx, hash);
  307. /* See if we must use a particular key */
  308. if (info->required_keynode != -1) {
  309. ret = rsa_verify_with_keynode(info, hash, sig, sig_len,
  310. info->required_keynode);
  311. if (!ret)
  312. return ret;
  313. }
  314. /* Look for a key that matches our hint */
  315. snprintf(name, sizeof(name), "key-%s", info->keyname);
  316. node = fdt_subnode_offset(blob, sig_node, name);
  317. ret = rsa_verify_with_keynode(info, hash, sig, sig_len, node);
  318. if (!ret)
  319. return ret;
  320. /* No luck, so try each of the keys in turn */
  321. for (ndepth = 0, noffset = fdt_next_node(info->fit, sig_node, &ndepth);
  322. (noffset >= 0) && (ndepth > 0);
  323. noffset = fdt_next_node(info->fit, noffset, &ndepth)) {
  324. if (ndepth == 1 && noffset != node) {
  325. ret = rsa_verify_with_keynode(info, hash, sig, sig_len,
  326. noffset);
  327. if (!ret)
  328. break;
  329. }
  330. }
  331. return ret;
  332. }