2rsa.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /* Copyright (c) 2014 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. /*
  6. * Implementation of RSA signature verification which uses a pre-processed key
  7. * for computation. The code extends Android's RSA verification code to support
  8. * multiple RSA key lengths and hash digest algorithms.
  9. */
  10. #include "2sysincludes.h"
  11. #include "2common.h"
  12. #include "2rsa.h"
  13. #include "2sha.h"
  14. /**
  15. * a[] -= mod
  16. */
  17. static void subM(const struct vb2_public_key *key, uint32_t *a)
  18. {
  19. int64_t A = 0;
  20. uint32_t i;
  21. for (i = 0; i < key->arrsize; ++i) {
  22. A += (uint64_t)a[i] - key->n[i];
  23. a[i] = (uint32_t)A;
  24. A >>= 32;
  25. }
  26. }
  27. /**
  28. * Return a[] >= mod
  29. */
  30. int vb2_mont_ge(const struct vb2_public_key *key, uint32_t *a)
  31. {
  32. uint32_t i;
  33. for (i = key->arrsize; i;) {
  34. --i;
  35. if (a[i] < key->n[i])
  36. return 0;
  37. if (a[i] > key->n[i])
  38. return 1;
  39. }
  40. return 1; /* equal */
  41. }
  42. /**
  43. * Montgomery c[] += a * b[] / R % mod
  44. */
  45. static void montMulAdd(const struct vb2_public_key *key,
  46. uint32_t *c,
  47. const uint32_t a,
  48. const uint32_t *b)
  49. {
  50. uint64_t A = (uint64_t)a * b[0] + c[0];
  51. uint32_t d0 = (uint32_t)A * key->n0inv;
  52. uint64_t B = (uint64_t)d0 * key->n[0] + (uint32_t)A;
  53. uint32_t i;
  54. for (i = 1; i < key->arrsize; ++i) {
  55. A = (A >> 32) + (uint64_t)a * b[i] + c[i];
  56. B = (B >> 32) + (uint64_t)d0 * key->n[i] + (uint32_t)A;
  57. c[i - 1] = (uint32_t)B;
  58. }
  59. A = (A >> 32) + (B >> 32);
  60. c[i - 1] = (uint32_t)A;
  61. if (A >> 32) {
  62. subM(key, c);
  63. }
  64. }
  65. /**
  66. * Montgomery c[] = a[] * b[] / R % mod
  67. */
  68. static void montMul(const struct vb2_public_key *key,
  69. uint32_t *c,
  70. const uint32_t *a,
  71. const uint32_t *b)
  72. {
  73. uint32_t i;
  74. for (i = 0; i < key->arrsize; ++i) {
  75. c[i] = 0;
  76. }
  77. for (i = 0; i < key->arrsize; ++i) {
  78. montMulAdd(key, c, a[i], b);
  79. }
  80. }
  81. /**
  82. * In-place public exponentiation. (65537}
  83. *
  84. * @param key Key to use in signing
  85. * @param inout Input and output big-endian byte array
  86. * @param workbuf32 Work buffer; caller must verify this is
  87. * (3 * key->arrsize) elements long.
  88. */
  89. static void modpowF4(const struct vb2_public_key *key, uint8_t *inout,
  90. uint32_t *workbuf32)
  91. {
  92. uint32_t *a = workbuf32;
  93. uint32_t *aR = a + key->arrsize;
  94. uint32_t *aaR = aR + key->arrsize;
  95. uint32_t *aaa = aaR; /* Re-use location. */
  96. int i;
  97. /* Convert from big endian byte array to little endian word array. */
  98. for (i = 0; i < (int)key->arrsize; ++i) {
  99. uint32_t tmp =
  100. (inout[((key->arrsize - 1 - i) * 4) + 0] << 24) |
  101. (inout[((key->arrsize - 1 - i) * 4) + 1] << 16) |
  102. (inout[((key->arrsize - 1 - i) * 4) + 2] << 8) |
  103. (inout[((key->arrsize - 1 - i) * 4) + 3] << 0);
  104. a[i] = tmp;
  105. }
  106. montMul(key, aR, a, key->rr); /* aR = a * RR / R mod M */
  107. for (i = 0; i < 16; i+=2) {
  108. montMul(key, aaR, aR, aR); /* aaR = aR * aR / R mod M */
  109. montMul(key, aR, aaR, aaR); /* aR = aaR * aaR / R mod M */
  110. }
  111. montMul(key, aaa, aR, a); /* aaa = aR * a / R mod M */
  112. /* Make sure aaa < mod; aaa is at most 1x mod too large. */
  113. if (vb2_mont_ge(key, aaa)) {
  114. subM(key, aaa);
  115. }
  116. /* Convert to bigendian byte array */
  117. for (i = (int)key->arrsize - 1; i >= 0; --i) {
  118. uint32_t tmp = aaa[i];
  119. *inout++ = (uint8_t)(tmp >> 24);
  120. *inout++ = (uint8_t)(tmp >> 16);
  121. *inout++ = (uint8_t)(tmp >> 8);
  122. *inout++ = (uint8_t)(tmp >> 0);
  123. }
  124. }
  125. static const uint8_t crypto_to_sig[] = {
  126. VB2_SIG_RSA1024,
  127. VB2_SIG_RSA1024,
  128. VB2_SIG_RSA1024,
  129. VB2_SIG_RSA2048,
  130. VB2_SIG_RSA2048,
  131. VB2_SIG_RSA2048,
  132. VB2_SIG_RSA4096,
  133. VB2_SIG_RSA4096,
  134. VB2_SIG_RSA4096,
  135. VB2_SIG_RSA8192,
  136. VB2_SIG_RSA8192,
  137. VB2_SIG_RSA8192,
  138. };
  139. /**
  140. * Convert vb2_crypto_algorithm to vb2_signature_algorithm.
  141. *
  142. * @param algorithm Crypto algorithm (vb2_crypto_algorithm)
  143. *
  144. * @return The signature algorithm for that crypto algorithm, or
  145. * VB2_SIG_INVALID if the crypto algorithm or its corresponding signature
  146. * algorithm is invalid or not supported.
  147. */
  148. enum vb2_signature_algorithm vb2_crypto_to_signature(uint32_t algorithm)
  149. {
  150. if (algorithm < ARRAY_SIZE(crypto_to_sig))
  151. return crypto_to_sig[algorithm];
  152. else
  153. return VB2_SIG_INVALID;
  154. }
  155. uint32_t vb2_rsa_sig_size(enum vb2_signature_algorithm sig_alg)
  156. {
  157. switch (sig_alg) {
  158. case VB2_SIG_RSA1024:
  159. return 1024 / 8;
  160. case VB2_SIG_RSA2048:
  161. return 2048 / 8;
  162. case VB2_SIG_RSA4096:
  163. return 4096 / 8;
  164. case VB2_SIG_RSA8192:
  165. return 8192 / 8;
  166. default:
  167. return 0;
  168. }
  169. }
  170. uint32_t vb2_packed_key_size(enum vb2_signature_algorithm sig_alg)
  171. {
  172. uint32_t sig_size = vb2_rsa_sig_size(sig_alg);
  173. if (!sig_size)
  174. return 0;
  175. /*
  176. * Total size needed by a RSAPublicKey buffer is =
  177. * 2 * key_len bytes for the n and rr arrays
  178. * + sizeof len + sizeof n0inv.
  179. */
  180. return 2 * sig_size + 2 * sizeof(uint32_t);
  181. }
  182. /*
  183. * PKCS 1.5 padding (from the RSA PKCS#1 v2.1 standard)
  184. *
  185. * Depending on the RSA key size and hash function, the padding is calculated
  186. * as follows:
  187. *
  188. * 0x00 || 0x01 || PS || 0x00 || T
  189. *
  190. * T: DER Encoded DigestInfo value which depends on the hash function used.
  191. *
  192. * SHA-1: (0x)30 21 30 09 06 05 2b 0e 03 02 1a 05 00 04 14 || H.
  193. * SHA-256: (0x)30 31 30 0d 06 09 60 86 48 01 65 03 04 02 01 05 00 04 20 || H.
  194. * SHA-512: (0x)30 51 30 0d 06 09 60 86 48 01 65 03 04 02 03 05 00 04 40 || H.
  195. *
  196. * Length(T) = 35 octets for SHA-1
  197. * Length(T) = 51 octets for SHA-256
  198. * Length(T) = 83 octets for SHA-512
  199. *
  200. * PS: octet string consisting of {Length(RSA Key) - Length(T) - 3} 0xFF
  201. */
  202. static const uint8_t sha1_tail[] = {
  203. 0x00,0x30,0x21,0x30,0x09,0x06,0x05,0x2b,
  204. 0x0e,0x03,0x02,0x1a,0x05,0x00,0x04,0x14
  205. };
  206. static const uint8_t sha256_tail[] = {
  207. 0x00,0x30,0x31,0x30,0x0d,0x06,0x09,0x60,
  208. 0x86,0x48,0x01,0x65,0x03,0x04,0x02,0x01,
  209. 0x05,0x00,0x04,0x20
  210. };
  211. static const uint8_t sha512_tail[] = {
  212. 0x00,0x30,0x51,0x30,0x0d,0x06,0x09,0x60,
  213. 0x86,0x48,0x01,0x65,0x03,0x04,0x02,0x03,
  214. 0x05,0x00,0x04,0x40
  215. };
  216. int vb2_check_padding(const uint8_t *sig, const struct vb2_public_key *key)
  217. {
  218. /* Determine padding to use depending on the signature type */
  219. uint32_t sig_size = vb2_rsa_sig_size(key->sig_alg);
  220. uint32_t hash_size = vb2_digest_size(key->hash_alg);
  221. uint32_t pad_size = sig_size - hash_size;
  222. const uint8_t *tail;
  223. uint32_t tail_size;
  224. int result = 0;
  225. int i;
  226. if (!sig_size || !hash_size || hash_size > sig_size)
  227. return VB2_ERROR_RSA_PADDING_SIZE;
  228. switch (key->hash_alg) {
  229. case VB2_HASH_SHA1:
  230. tail = sha1_tail;
  231. tail_size = sizeof(sha1_tail);
  232. break;
  233. case VB2_HASH_SHA256:
  234. tail = sha256_tail;
  235. tail_size = sizeof(sha256_tail);
  236. break;
  237. case VB2_HASH_SHA512:
  238. tail = sha512_tail;
  239. tail_size = sizeof(sha512_tail);
  240. break;
  241. default:
  242. return VB2_ERROR_RSA_PADDING_ALGORITHM;
  243. }
  244. /* First 2 bytes are always 0x00 0x01 */
  245. result |= *sig++ ^ 0x00;
  246. result |= *sig++ ^ 0x01;
  247. /* Then 0xff bytes until the tail */
  248. for (i = 0; i < pad_size - tail_size - 2; i++)
  249. result |= *sig++ ^ 0xff;
  250. /*
  251. * Then the tail. Even though there are probably no timing issues
  252. * here, we use vb2_safe_memcmp() just to be on the safe side.
  253. */
  254. result |= vb2_safe_memcmp(sig, tail, tail_size);
  255. return result ? VB2_ERROR_RSA_PADDING : VB2_SUCCESS;
  256. }
  257. int vb2_rsa_verify_digest(const struct vb2_public_key *key,
  258. uint8_t *sig,
  259. const uint8_t *digest,
  260. const struct vb2_workbuf *wb)
  261. {
  262. struct vb2_workbuf wblocal = *wb;
  263. uint32_t *workbuf32;
  264. uint32_t key_bytes;
  265. int sig_size;
  266. int pad_size;
  267. int rv;
  268. if (!key || !sig || !digest)
  269. return VB2_ERROR_RSA_VERIFY_PARAM;
  270. sig_size = vb2_rsa_sig_size(key->sig_alg);
  271. if (!sig_size) {
  272. VB2_DEBUG("Invalid signature type!\n");
  273. return VB2_ERROR_RSA_VERIFY_ALGORITHM;
  274. }
  275. /* Signature length should be same as key length */
  276. key_bytes = key->arrsize * sizeof(uint32_t);
  277. if (key_bytes != sig_size) {
  278. VB2_DEBUG("Signature is of incorrect length!\n");
  279. return VB2_ERROR_RSA_VERIFY_SIG_LEN;
  280. }
  281. workbuf32 = vb2_workbuf_alloc(&wblocal, 3 * key_bytes);
  282. if (!workbuf32) {
  283. VB2_DEBUG("ERROR - vboot2 work buffer too small!\n");
  284. return VB2_ERROR_RSA_VERIFY_WORKBUF;
  285. }
  286. modpowF4(key, sig, workbuf32);
  287. vb2_workbuf_free(&wblocal, 3 * key_bytes);
  288. /*
  289. * Check padding. Only fail immediately if the padding size is bad.
  290. * Otherwise, continue on to check the digest to reduce the risk of
  291. * timing based attacks.
  292. */
  293. rv = vb2_check_padding(sig, key);
  294. if (rv == VB2_ERROR_RSA_PADDING_SIZE)
  295. return rv;
  296. /*
  297. * Check digest. Even though there are probably no timing issues here,
  298. * use vb2_safe_memcmp() just to be on the safe side. (That's also why
  299. * we don't return before this check if the padding check failed.)
  300. */
  301. pad_size = sig_size - vb2_digest_size(key->hash_alg);
  302. if (vb2_safe_memcmp(sig + pad_size, digest, key_bytes - pad_size)) {
  303. VB2_DEBUG("Digest check failed!\n");
  304. if (!rv)
  305. rv = VB2_ERROR_RSA_VERIFY_DIGEST;
  306. }
  307. return rv;
  308. }