rsa.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /* Copyright (c) 2015 The Chromium OS Authors. All rights reserved.
  2. * Use of this source code is governed by a BSD-style license that can be
  3. * found in the LICENSE file.
  4. *
  5. * Boot descriptor block firmware RSA
  6. */
  7. #include <string.h>
  8. #include "bdb.h"
  9. /* Public key structure in RAM */
  10. struct public_key {
  11. uint32_t arrsize; /* Size of n[] and rr[] arrays in elements */
  12. uint32_t n0inv; /* -1 / n[0] mod 2^32 */
  13. const uint32_t *n; /* Modulus as little endian array */
  14. const uint32_t *rr; /* R^2 as little endian array */
  15. };
  16. /**
  17. * a[] -= mod
  18. */
  19. static void subM(const struct public_key *key, uint32_t *a)
  20. {
  21. int64_t A = 0;
  22. uint32_t i;
  23. for (i = 0; i < key->arrsize; ++i) {
  24. A += (uint64_t)a[i] - key->n[i];
  25. a[i] = (uint32_t)A;
  26. A >>= 32;
  27. }
  28. }
  29. /**
  30. * Return a[] >= mod
  31. */
  32. int vb2_mont_ge(const struct public_key *key, uint32_t *a)
  33. {
  34. uint32_t i;
  35. for (i = key->arrsize; i;) {
  36. --i;
  37. if (a[i] < key->n[i])
  38. return 0;
  39. if (a[i] > key->n[i])
  40. return 1;
  41. }
  42. return 1; /* equal */
  43. }
  44. /**
  45. * Montgomery c[] += a * b[] / R % mod
  46. */
  47. static void montMulAdd(const struct public_key *key,
  48. uint32_t *c,
  49. const uint32_t a,
  50. const uint32_t *b)
  51. {
  52. uint64_t A = (uint64_t)a * b[0] + c[0];
  53. uint32_t d0 = (uint32_t)A * key->n0inv;
  54. uint64_t B = (uint64_t)d0 * key->n[0] + (uint32_t)A;
  55. uint32_t i;
  56. for (i = 1; i < key->arrsize; ++i) {
  57. A = (A >> 32) + (uint64_t)a * b[i] + c[i];
  58. B = (B >> 32) + (uint64_t)d0 * key->n[i] + (uint32_t)A;
  59. c[i - 1] = (uint32_t)B;
  60. }
  61. A = (A >> 32) + (B >> 32);
  62. c[i - 1] = (uint32_t)A;
  63. if (A >> 32) {
  64. subM(key, c);
  65. }
  66. }
  67. /**
  68. * Montgomery c[] = a[] * b[] / R % mod
  69. */
  70. static void montMul(const struct public_key *key,
  71. uint32_t *c,
  72. const uint32_t *a,
  73. const uint32_t *b)
  74. {
  75. uint32_t i;
  76. for (i = 0; i < key->arrsize; ++i) {
  77. c[i] = 0;
  78. }
  79. for (i = 0; i < key->arrsize; ++i) {
  80. montMulAdd(key, c, a[i], b);
  81. }
  82. }
  83. int vb2_safe_memcmp(const void *s1, const void *s2, size_t size)
  84. {
  85. const unsigned char *us1 = s1;
  86. const unsigned char *us2 = s2;
  87. int result = 0;
  88. if (0 == size)
  89. return 0;
  90. /*
  91. * Code snippet without data-dependent branch due to Nate Lawson
  92. * (nate@root.org) of Root Labs.
  93. */
  94. while (size--)
  95. result |= *us1++ ^ *us2++;
  96. return result != 0;
  97. }
  98. /*
  99. * PKCS 1.5 padding (from the RSA PKCS#1 v2.1 standard)
  100. *
  101. * Depending on the RSA key size and hash function, the padding is calculated
  102. * as follows:
  103. *
  104. * 0x00 || 0x01 || PS || 0x00 || T
  105. *
  106. * T: DER Encoded DigestInfo value which depends on the hash function used.
  107. *
  108. * SHA-256: (0x)30 31 30 0d 06 09 60 86 48 01 65 03 04 02 01 05 00 04 20 || H.
  109. *
  110. * Length(T) = 51 octets for SHA-256
  111. *
  112. * PS: octet string consisting of {Length(RSA Key) - Length(T) - 3} 0xFF
  113. */
  114. static const uint8_t sha256_tail[] = {
  115. 0x00,0x30,0x31,0x30,0x0d,0x06,0x09,0x60,
  116. 0x86,0x48,0x01,0x65,0x03,0x04,0x02,0x01,
  117. 0x05,0x00,0x04,0x20
  118. };
  119. int vb2_check_padding(const uint8_t *sig, const struct public_key *key,
  120. uint32_t pad_size)
  121. {
  122. /* Determine padding to use depending on the signature type */
  123. const uint32_t tail_size = sizeof(sha256_tail);
  124. int result = 0;
  125. int i;
  126. /* First 2 bytes are always 0x00 0x01 */
  127. result |= *sig++ ^ 0x00;
  128. result |= *sig++ ^ 0x01;
  129. /* Then 0xff bytes until the tail */
  130. for (i = 0; i < pad_size - tail_size - 2; i++)
  131. result |= *sig++ ^ 0xff;
  132. /*
  133. * Then the tail. Even though there are probably no timing issues
  134. * here, we use vb2_safe_memcmp() just to be on the safe side.
  135. */
  136. result |= vb2_safe_memcmp(sig, sha256_tail, tail_size);
  137. return result ? BDB_ERROR_DIGEST : BDB_SUCCESS;
  138. }
  139. /* Array size for RSA4096 */
  140. #define ARRSIZE4096 (4096 / 32)
  141. /**
  142. * In-place public exponentiation. (exponent 65537, key size 4096 bits)
  143. *
  144. * @param key Key to use in signing
  145. * @param inout Input and output big-endian byte array
  146. */
  147. static void modpowF4(const struct public_key *key, uint8_t *inout)
  148. {
  149. uint32_t a[ARRSIZE4096];
  150. uint32_t aR[ARRSIZE4096];
  151. uint32_t aaR[ARRSIZE4096];
  152. uint32_t *aaa = aaR; /* Re-use location. */
  153. int i;
  154. /* Convert from big endian byte array to little endian word array. */
  155. for (i = 0; i < ARRSIZE4096; ++i) {
  156. uint32_t tmp =
  157. (inout[((ARRSIZE4096 - 1 - i) * 4) + 0] << 24) |
  158. (inout[((ARRSIZE4096 - 1 - i) * 4) + 1] << 16) |
  159. (inout[((ARRSIZE4096 - 1 - i) * 4) + 2] << 8) |
  160. (inout[((ARRSIZE4096 - 1 - i) * 4) + 3] << 0);
  161. a[i] = tmp;
  162. }
  163. montMul(key, aR, a, key->rr); /* aR = a * RR / R mod M */
  164. for (i = 0; i < 16; i+=2) {
  165. montMul(key, aaR, aR, aR); /* aaR = aR * aR / R mod M */
  166. montMul(key, aR, aaR, aaR); /* aR = aaR * aaR / R mod M */
  167. }
  168. montMul(key, aaa, aR, a); /* aaa = aR * a / R mod M */
  169. /* Make sure aaa < mod; aaa is at most 1x mod too large. */
  170. if (vb2_mont_ge(key, aaa)) {
  171. subM(key, aaa);
  172. }
  173. /* Convert to bigendian byte array */
  174. for (i = ARRSIZE4096 - 1; i >= 0; --i) {
  175. uint32_t tmp = aaa[i];
  176. *inout++ = (uint8_t)(tmp >> 24);
  177. *inout++ = (uint8_t)(tmp >> 16);
  178. *inout++ = (uint8_t)(tmp >> 8);
  179. *inout++ = (uint8_t)(tmp >> 0);
  180. }
  181. }
  182. int bdb_rsa4096_verify(const uint8_t *key_data,
  183. const uint8_t *sig,
  184. const uint8_t *digest)
  185. {
  186. const uint32_t *kdata32 = (const uint32_t *)key_data;
  187. struct public_key key;
  188. uint8_t sig_work[BDB_RSA4096_SIG_SIZE];
  189. uint32_t pad_size;
  190. int rv;
  191. /* Unpack key */
  192. if (kdata32[0] != ARRSIZE4096)
  193. return BDB_ERROR_DIGEST; /* Wrong key size */
  194. key.arrsize = kdata32[0];
  195. key.n0inv = kdata32[1];
  196. key.n = kdata32 + 2;
  197. key.rr = kdata32 + 2 + key.arrsize;
  198. /* Copy signature to work buffer */
  199. memcpy(sig_work, sig, sizeof(sig_work));
  200. modpowF4(&key, sig_work);
  201. /*
  202. * Check padding. Continue on to check the digest even if error to
  203. * reduce the risk of timing based attacks.
  204. */
  205. pad_size = key.arrsize * sizeof(uint32_t) - BDB_SHA256_DIGEST_SIZE;
  206. rv = vb2_check_padding(sig_work, &key, pad_size);
  207. /*
  208. * Check digest. Even though there are probably no timing issues here,
  209. * use vb2_safe_memcmp() just to be on the safe side. (That's also why
  210. * we don't return before this check if the padding check failed.)
  211. */
  212. if (vb2_safe_memcmp(sig_work + pad_size, digest,
  213. BDB_SHA256_DIGEST_SIZE))
  214. rv = BDB_ERROR_DIGEST;
  215. return rv;
  216. }
  217. /* Array size for RSA3072B */
  218. #define ARRSIZE3072B (3072 / 32)
  219. /**
  220. * In-place public exponentiation. (exponent 3, key size 3072 bits)
  221. *
  222. * @param key Key to use in signing
  223. * @param inout Input and output big-endian byte array
  224. */
  225. static void modpow3(const struct public_key *key, uint8_t *inout)
  226. {
  227. uint32_t a[ARRSIZE3072B];
  228. uint32_t aR[ARRSIZE3072B];
  229. uint32_t aaR[ARRSIZE3072B];
  230. uint32_t *aaa = aR; /* Re-use location */
  231. int i;
  232. /* Convert from big endian byte array to little endian word array. */
  233. for (i = 0; i < ARRSIZE3072B; ++i) {
  234. uint32_t tmp =
  235. (inout[((ARRSIZE3072B - 1 - i) * 4) + 0] << 24) |
  236. (inout[((ARRSIZE3072B - 1 - i) * 4) + 1] << 16) |
  237. (inout[((ARRSIZE3072B - 1 - i) * 4) + 2] << 8) |
  238. (inout[((ARRSIZE3072B - 1 - i) * 4) + 3] << 0);
  239. a[i] = tmp;
  240. }
  241. montMul(key, aR, a, key->rr); /* aR = a * RR / R mod M */
  242. montMul(key, aaR, aR, aR); /* aaR = aR * aR / R mod M */
  243. montMul(key, aaa, aaR, a); /* aaa = aaR * a / R mod M */
  244. /* Make sure aaa < mod; aaa is at most 1x mod too large. */
  245. if (vb2_mont_ge(key, aaa)) {
  246. subM(key, aaa);
  247. }
  248. /* Convert to bigendian byte array */
  249. for (i = ARRSIZE3072B - 1; i >= 0; --i) {
  250. uint32_t tmp = aaa[i];
  251. *inout++ = (uint8_t)(tmp >> 24);
  252. *inout++ = (uint8_t)(tmp >> 16);
  253. *inout++ = (uint8_t)(tmp >> 8);
  254. *inout++ = (uint8_t)(tmp >> 0);
  255. }
  256. }
  257. int bdb_rsa3072b_verify(const uint8_t *key_data,
  258. const uint8_t *sig,
  259. const uint8_t *digest)
  260. {
  261. const uint32_t *kdata32 = (const uint32_t *)key_data;
  262. struct public_key key;
  263. uint8_t sig_work[BDB_RSA3072B_SIG_SIZE];
  264. uint32_t pad_size;
  265. int rv;
  266. /* Unpack key */
  267. if (kdata32[0] != ARRSIZE3072B)
  268. return BDB_ERROR_DIGEST; /* Wrong key size */
  269. key.arrsize = kdata32[0];
  270. key.n0inv = kdata32[1];
  271. key.n = kdata32 + 2;
  272. key.rr = kdata32 + 2 + key.arrsize;
  273. /* Copy signature to work buffer */
  274. memcpy(sig_work, sig, sizeof(sig_work));
  275. modpow3(&key, sig_work);
  276. /*
  277. * Check padding. Continue on to check the digest even if error to
  278. * reduce the risk of timing based attacks.
  279. */
  280. pad_size = key.arrsize * sizeof(uint32_t) - BDB_SHA256_DIGEST_SIZE;
  281. rv = vb2_check_padding(sig_work, &key, pad_size);
  282. /*
  283. * Check digest. Even though there are probably no timing issues here,
  284. * use vb2_safe_memcmp() just to be on the safe side. (That's also why
  285. * we don't return before this check if the padding check failed.)
  286. */
  287. if (vb2_safe_memcmp(sig_work + pad_size, digest,
  288. BDB_SHA256_DIGEST_SIZE))
  289. rv = BDB_ERROR_DIGEST;
  290. return rv;
  291. }