rsa-mod-exp.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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 <log.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. #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/rsa-mod-exp.h>
  21. #define UINT64_MULT32(v, multby) (((uint64_t)(v)) * ((uint32_t)(multby)))
  22. #define get_unaligned_be32(a) fdt32_to_cpu(*(uint32_t *)a)
  23. #define put_unaligned_be32(a, b) (*(uint32_t *)(b) = cpu_to_fdt32(a))
  24. static inline uint64_t fdt64_to_cpup(const void *p)
  25. {
  26. fdt64_t w;
  27. memcpy(&w, p, sizeof(w));
  28. return fdt64_to_cpu(w);
  29. }
  30. /* Default public exponent for backward compatibility */
  31. #define RSA_DEFAULT_PUBEXP 65537
  32. /**
  33. * subtract_modulus() - subtract modulus from the given value
  34. *
  35. * @key: Key containing modulus to subtract
  36. * @num: Number to subtract modulus from, as little endian word array
  37. */
  38. static void subtract_modulus(const struct rsa_public_key *key, uint32_t num[])
  39. {
  40. int64_t acc = 0;
  41. uint i;
  42. for (i = 0; i < key->len; i++) {
  43. acc += (uint64_t)num[i] - key->modulus[i];
  44. num[i] = (uint32_t)acc;
  45. acc >>= 32;
  46. }
  47. }
  48. /**
  49. * greater_equal_modulus() - check if a value is >= modulus
  50. *
  51. * @key: Key containing modulus to check
  52. * @num: Number to check against modulus, as little endian word array
  53. * Return: 0 if num < modulus, 1 if num >= modulus
  54. */
  55. static int greater_equal_modulus(const struct rsa_public_key *key,
  56. uint32_t num[])
  57. {
  58. int i;
  59. for (i = (int)key->len - 1; i >= 0; i--) {
  60. if (num[i] < key->modulus[i])
  61. return 0;
  62. if (num[i] > key->modulus[i])
  63. return 1;
  64. }
  65. return 1; /* equal */
  66. }
  67. /**
  68. * montgomery_mul_add_step() - Perform montgomery multiply-add step
  69. *
  70. * Operation: montgomery result[] += a * b[] / n0inv % modulus
  71. *
  72. * @key: RSA key
  73. * @result: Place to put result, as little endian word array
  74. * @a: Multiplier
  75. * @b: Multiplicand, as little endian word array
  76. */
  77. static void montgomery_mul_add_step(const struct rsa_public_key *key,
  78. uint32_t result[], const uint32_t a, const uint32_t b[])
  79. {
  80. uint64_t acc_a, acc_b;
  81. uint32_t d0;
  82. uint i;
  83. acc_a = (uint64_t)a * b[0] + result[0];
  84. d0 = (uint32_t)acc_a * key->n0inv;
  85. acc_b = (uint64_t)d0 * key->modulus[0] + (uint32_t)acc_a;
  86. for (i = 1; i < key->len; i++) {
  87. acc_a = (acc_a >> 32) + (uint64_t)a * b[i] + result[i];
  88. acc_b = (acc_b >> 32) + (uint64_t)d0 * key->modulus[i] +
  89. (uint32_t)acc_a;
  90. result[i - 1] = (uint32_t)acc_b;
  91. }
  92. acc_a = (acc_a >> 32) + (acc_b >> 32);
  93. result[i - 1] = (uint32_t)acc_a;
  94. if (acc_a >> 32)
  95. subtract_modulus(key, result);
  96. }
  97. /**
  98. * montgomery_mul() - Perform montgomery mutitply
  99. *
  100. * Operation: montgomery result[] = a[] * b[] / n0inv % modulus
  101. *
  102. * @key: RSA key
  103. * @result: Place to put result, as little endian word array
  104. * @a: Multiplier, as little endian word array
  105. * @b: Multiplicand, as little endian word array
  106. */
  107. static void montgomery_mul(const struct rsa_public_key *key,
  108. uint32_t result[], uint32_t a[], const uint32_t b[])
  109. {
  110. uint i;
  111. for (i = 0; i < key->len; ++i)
  112. result[i] = 0;
  113. for (i = 0; i < key->len; ++i)
  114. montgomery_mul_add_step(key, result, a[i], b);
  115. }
  116. /**
  117. * num_pub_exponent_bits() - Number of bits in the public exponent
  118. *
  119. * @key: RSA key
  120. * @num_bits: Storage for the number of public exponent bits
  121. */
  122. static int num_public_exponent_bits(const struct rsa_public_key *key,
  123. int *num_bits)
  124. {
  125. uint64_t exponent;
  126. int exponent_bits;
  127. const uint max_bits = (sizeof(exponent) * 8);
  128. exponent = key->exponent;
  129. exponent_bits = 0;
  130. if (!exponent) {
  131. *num_bits = exponent_bits;
  132. return 0;
  133. }
  134. for (exponent_bits = 1; exponent_bits < max_bits + 1; ++exponent_bits)
  135. if (!(exponent >>= 1)) {
  136. *num_bits = exponent_bits;
  137. return 0;
  138. }
  139. return -EINVAL;
  140. }
  141. /**
  142. * is_public_exponent_bit_set() - Check if a bit in the public exponent is set
  143. *
  144. * @key: RSA key
  145. * @pos: The bit position to check
  146. */
  147. static int is_public_exponent_bit_set(const struct rsa_public_key *key,
  148. int pos)
  149. {
  150. return key->exponent & (1ULL << pos);
  151. }
  152. /**
  153. * pow_mod() - in-place public exponentiation
  154. *
  155. * @key: RSA key
  156. * @inout: Big-endian word array containing value and result
  157. */
  158. static int pow_mod(const struct rsa_public_key *key, uint32_t *inout)
  159. {
  160. uint32_t *result, *ptr;
  161. uint i;
  162. int j, k;
  163. /* Sanity check for stack size - key->len is in 32-bit words */
  164. if (key->len > RSA_MAX_KEY_BITS / 32) {
  165. debug("RSA key words %u exceeds maximum %d\n", key->len,
  166. RSA_MAX_KEY_BITS / 32);
  167. return -EINVAL;
  168. }
  169. uint32_t val[key->len], acc[key->len], tmp[key->len];
  170. uint32_t a_scaled[key->len];
  171. result = tmp; /* Re-use location. */
  172. /* Convert from big endian byte array to little endian word array. */
  173. for (i = 0, ptr = inout + key->len - 1; i < key->len; i++, ptr--)
  174. val[i] = get_unaligned_be32(ptr);
  175. if (0 != num_public_exponent_bits(key, &k))
  176. return -EINVAL;
  177. if (k < 2) {
  178. debug("Public exponent is too short (%d bits, minimum 2)\n",
  179. k);
  180. return -EINVAL;
  181. }
  182. if (!is_public_exponent_bit_set(key, 0)) {
  183. debug("LSB of RSA public exponent must be set.\n");
  184. return -EINVAL;
  185. }
  186. /* the bit at e[k-1] is 1 by definition, so start with: C := M */
  187. montgomery_mul(key, acc, val, key->rr); /* acc = a * RR / R mod n */
  188. /* retain scaled version for intermediate use */
  189. memcpy(a_scaled, acc, key->len * sizeof(a_scaled[0]));
  190. for (j = k - 2; j > 0; --j) {
  191. montgomery_mul(key, tmp, acc, acc); /* tmp = acc^2 / R mod n */
  192. if (is_public_exponent_bit_set(key, j)) {
  193. /* acc = tmp * val / R mod n */
  194. montgomery_mul(key, acc, tmp, a_scaled);
  195. } else {
  196. /* e[j] == 0, copy tmp back to acc for next operation */
  197. memcpy(acc, tmp, key->len * sizeof(acc[0]));
  198. }
  199. }
  200. /* the bit at e[0] is always 1 */
  201. montgomery_mul(key, tmp, acc, acc); /* tmp = acc^2 / R mod n */
  202. montgomery_mul(key, acc, tmp, val); /* acc = tmp * a / R mod M */
  203. memcpy(result, acc, key->len * sizeof(result[0]));
  204. /* Make sure result < mod; result is at most 1x mod too large. */
  205. if (greater_equal_modulus(key, result))
  206. subtract_modulus(key, result);
  207. /* Convert to bigendian byte array */
  208. for (i = key->len - 1, ptr = inout; (int)i >= 0; i--, ptr++)
  209. put_unaligned_be32(result[i], ptr);
  210. return 0;
  211. }
  212. static void rsa_convert_big_endian(uint32_t *dst, const uint32_t *src, int len)
  213. {
  214. int i;
  215. for (i = 0; i < len; i++)
  216. dst[i] = fdt32_to_cpu(src[len - 1 - i]);
  217. }
  218. int rsa_mod_exp_sw(const uint8_t *sig, uint32_t sig_len,
  219. struct key_prop *prop, uint8_t *out)
  220. {
  221. struct rsa_public_key key;
  222. int ret;
  223. if (!prop) {
  224. debug("%s: Skipping invalid prop", __func__);
  225. return -EBADF;
  226. }
  227. key.n0inv = prop->n0inv;
  228. key.len = prop->num_bits;
  229. if (!prop->public_exponent)
  230. key.exponent = RSA_DEFAULT_PUBEXP;
  231. else
  232. key.exponent = fdt64_to_cpup(prop->public_exponent);
  233. if (!key.len || !prop->modulus || !prop->rr) {
  234. debug("%s: Missing RSA key info", __func__);
  235. return -EFAULT;
  236. }
  237. /* Sanity check for stack size */
  238. if (key.len > RSA_MAX_KEY_BITS || key.len < RSA_MIN_KEY_BITS) {
  239. debug("RSA key bits %u outside allowed range %d..%d\n",
  240. key.len, RSA_MIN_KEY_BITS, RSA_MAX_KEY_BITS);
  241. return -EFAULT;
  242. }
  243. key.len /= sizeof(uint32_t) * 8;
  244. uint32_t key1[key.len], key2[key.len];
  245. key.modulus = key1;
  246. key.rr = key2;
  247. rsa_convert_big_endian(key.modulus, (uint32_t *)prop->modulus, key.len);
  248. rsa_convert_big_endian(key.rr, (uint32_t *)prop->rr, key.len);
  249. if (!key.modulus || !key.rr) {
  250. debug("%s: Out of memory", __func__);
  251. return -ENOMEM;
  252. }
  253. uint32_t buf[sig_len / sizeof(uint32_t)];
  254. memcpy(buf, sig, sig_len);
  255. ret = pow_mod(&key, buf);
  256. if (ret)
  257. return ret;
  258. memcpy(out, buf, sig_len);
  259. return 0;
  260. }
  261. #if defined(CONFIG_CMD_ZYNQ_RSA)
  262. /**
  263. * zynq_pow_mod - in-place public exponentiation
  264. *
  265. * @keyptr: RSA key
  266. * @inout: Big-endian word array containing value and result
  267. * Return: 0 on successful calculation, otherwise failure error code
  268. *
  269. * FIXME: Use pow_mod() instead of zynq_pow_mod()
  270. * pow_mod calculation required for zynq is bit different from
  271. * pw_mod above here, hence defined zynq specific routine.
  272. */
  273. int zynq_pow_mod(uint32_t *keyptr, uint32_t *inout)
  274. {
  275. u32 *result, *ptr;
  276. uint i;
  277. struct rsa_public_key *key;
  278. u32 val[RSA2048_BYTES], acc[RSA2048_BYTES], tmp[RSA2048_BYTES];
  279. key = (struct rsa_public_key *)keyptr;
  280. /* Sanity check for stack size - key->len is in 32-bit words */
  281. if (key->len > RSA_MAX_KEY_BITS / 32) {
  282. debug("RSA key words %u exceeds maximum %d\n", key->len,
  283. RSA_MAX_KEY_BITS / 32);
  284. return -EINVAL;
  285. }
  286. result = tmp; /* Re-use location. */
  287. for (i = 0, ptr = inout; i < key->len; i++, ptr++)
  288. val[i] = *(ptr);
  289. montgomery_mul(key, acc, val, key->rr); /* axx = a * RR / R mod M */
  290. for (i = 0; i < 16; i += 2) {
  291. montgomery_mul(key, tmp, acc, acc); /* tmp = acc^2 / R mod M */
  292. montgomery_mul(key, acc, tmp, tmp); /* acc = tmp^2 / R mod M */
  293. }
  294. montgomery_mul(key, result, acc, val); /* result = XX * a / R mod M */
  295. /* Make sure result < mod; result is at most 1x mod too large. */
  296. if (greater_equal_modulus(key, result))
  297. subtract_modulus(key, result);
  298. for (i = 0, ptr = inout; i < key->len; i++, ptr++)
  299. *ptr = result[i];
  300. return 0;
  301. }
  302. #endif