avb_rsa.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. // SPDX-License-Identifier: MIT OR BSD-3-Clause
  2. /*
  3. * Copyright (C) 2016 The Android Open Source Project
  4. */
  5. /* Implementation of RSA signature verification which uses a pre-processed
  6. * key for computation. The code extends libmincrypt RSA verification code to
  7. * support multiple RSA key lengths and hash digest algorithms.
  8. */
  9. #include "avb_rsa.h"
  10. #include "avb_sha.h"
  11. #include "avb_util.h"
  12. #include "avb_vbmeta_image.h"
  13. typedef struct IAvbKey {
  14. unsigned int len; /* Length of n[] in number of uint32_t */
  15. uint32_t n0inv; /* -1 / n[0] mod 2^32 */
  16. uint32_t* n; /* modulus as array (host-byte order) */
  17. uint32_t* rr; /* R^2 as array (host-byte order) */
  18. } IAvbKey;
  19. static IAvbKey* iavb_parse_key_data(const uint8_t* data, size_t length) {
  20. AvbRSAPublicKeyHeader h;
  21. IAvbKey* key = NULL;
  22. size_t expected_length;
  23. unsigned int i;
  24. const uint8_t* n;
  25. const uint8_t* rr;
  26. if (!avb_rsa_public_key_header_validate_and_byteswap(
  27. (const AvbRSAPublicKeyHeader*)data, &h)) {
  28. avb_error("Invalid key.\n");
  29. goto fail;
  30. }
  31. if (!(h.key_num_bits == 2048 || h.key_num_bits == 4096 ||
  32. h.key_num_bits == 8192)) {
  33. avb_error("Unexpected key length.\n");
  34. goto fail;
  35. }
  36. expected_length = sizeof(AvbRSAPublicKeyHeader) + 2 * h.key_num_bits / 8;
  37. if (length != expected_length) {
  38. avb_error("Key does not match expected length.\n");
  39. goto fail;
  40. }
  41. n = data + sizeof(AvbRSAPublicKeyHeader);
  42. rr = data + sizeof(AvbRSAPublicKeyHeader) + h.key_num_bits / 8;
  43. /* Store n and rr following the key header so we only have to do one
  44. * allocation.
  45. */
  46. key = (IAvbKey*)(avb_malloc(sizeof(IAvbKey) + 2 * h.key_num_bits / 8));
  47. if (key == NULL) {
  48. goto fail;
  49. }
  50. key->len = h.key_num_bits / 32;
  51. key->n0inv = h.n0inv;
  52. key->n = (uint32_t*)(key + 1); /* Skip ahead sizeof(IAvbKey) bytes. */
  53. key->rr = key->n + key->len;
  54. /* Crypto-code below (modpowF4() and friends) expects the key in
  55. * little-endian format (rather than the format we're storing the
  56. * key in), so convert it.
  57. */
  58. for (i = 0; i < key->len; i++) {
  59. key->n[i] = avb_be32toh(((uint32_t*)n)[key->len - i - 1]);
  60. key->rr[i] = avb_be32toh(((uint32_t*)rr)[key->len - i - 1]);
  61. }
  62. return key;
  63. fail:
  64. if (key != NULL) {
  65. avb_free(key);
  66. }
  67. return NULL;
  68. }
  69. static void iavb_free_parsed_key(IAvbKey* key) {
  70. avb_free(key);
  71. }
  72. /* a[] -= mod */
  73. static void subM(const IAvbKey* key, uint32_t* a) {
  74. int64_t A = 0;
  75. uint32_t i;
  76. for (i = 0; i < key->len; ++i) {
  77. A += (uint64_t)a[i] - key->n[i];
  78. a[i] = (uint32_t)A;
  79. A >>= 32;
  80. }
  81. }
  82. /* return a[] >= mod */
  83. static int geM(const IAvbKey* key, uint32_t* a) {
  84. uint32_t i;
  85. for (i = key->len; i;) {
  86. --i;
  87. if (a[i] < key->n[i]) {
  88. return 0;
  89. }
  90. if (a[i] > key->n[i]) {
  91. return 1;
  92. }
  93. }
  94. return 1; /* equal */
  95. }
  96. /* montgomery c[] += a * b[] / R % mod */
  97. static void montMulAdd(const IAvbKey* key,
  98. uint32_t* c,
  99. const uint32_t a,
  100. const uint32_t* b) {
  101. uint64_t A = (uint64_t)a * b[0] + c[0];
  102. uint32_t d0 = (uint32_t)A * key->n0inv;
  103. uint64_t B = (uint64_t)d0 * key->n[0] + (uint32_t)A;
  104. uint32_t i;
  105. for (i = 1; i < key->len; ++i) {
  106. A = (A >> 32) + (uint64_t)a * b[i] + c[i];
  107. B = (B >> 32) + (uint64_t)d0 * key->n[i] + (uint32_t)A;
  108. c[i - 1] = (uint32_t)B;
  109. }
  110. A = (A >> 32) + (B >> 32);
  111. c[i - 1] = (uint32_t)A;
  112. if (A >> 32) {
  113. subM(key, c);
  114. }
  115. }
  116. /* montgomery c[] = a[] * b[] / R % mod */
  117. static void montMul(const IAvbKey* key, uint32_t* c, uint32_t* a, uint32_t* b) {
  118. uint32_t i;
  119. for (i = 0; i < key->len; ++i) {
  120. c[i] = 0;
  121. }
  122. for (i = 0; i < key->len; ++i) {
  123. montMulAdd(key, c, a[i], b);
  124. }
  125. }
  126. /* In-place public exponentiation. (65537}
  127. * Input and output big-endian byte array in inout.
  128. */
  129. static void modpowF4(const IAvbKey* key, uint8_t* inout) {
  130. uint32_t* a = (uint32_t*)avb_malloc(key->len * sizeof(uint32_t));
  131. uint32_t* aR = (uint32_t*)avb_malloc(key->len * sizeof(uint32_t));
  132. uint32_t* aaR = (uint32_t*)avb_malloc(key->len * sizeof(uint32_t));
  133. if (a == NULL || aR == NULL || aaR == NULL) {
  134. goto out;
  135. }
  136. uint32_t* aaa = aaR; /* Re-use location. */
  137. int i;
  138. /* Convert from big endian byte array to little endian word array. */
  139. for (i = 0; i < (int)key->len; ++i) {
  140. uint32_t tmp = (inout[((key->len - 1 - i) * 4) + 0] << 24) |
  141. (inout[((key->len - 1 - i) * 4) + 1] << 16) |
  142. (inout[((key->len - 1 - i) * 4) + 2] << 8) |
  143. (inout[((key->len - 1 - i) * 4) + 3] << 0);
  144. a[i] = tmp;
  145. }
  146. montMul(key, aR, a, key->rr); /* aR = a * RR / R mod M */
  147. for (i = 0; i < 16; i += 2) {
  148. montMul(key, aaR, aR, aR); /* aaR = aR * aR / R mod M */
  149. montMul(key, aR, aaR, aaR); /* aR = aaR * aaR / R mod M */
  150. }
  151. montMul(key, aaa, aR, a); /* aaa = aR * a / R mod M */
  152. /* Make sure aaa < mod; aaa is at most 1x mod too large. */
  153. if (geM(key, aaa)) {
  154. subM(key, aaa);
  155. }
  156. /* Convert to bigendian byte array */
  157. for (i = (int)key->len - 1; i >= 0; --i) {
  158. uint32_t tmp = aaa[i];
  159. *inout++ = (uint8_t)(tmp >> 24);
  160. *inout++ = (uint8_t)(tmp >> 16);
  161. *inout++ = (uint8_t)(tmp >> 8);
  162. *inout++ = (uint8_t)(tmp >> 0);
  163. }
  164. out:
  165. if (a != NULL) {
  166. avb_free(a);
  167. }
  168. if (aR != NULL) {
  169. avb_free(aR);
  170. }
  171. if (aaR != NULL) {
  172. avb_free(aaR);
  173. }
  174. }
  175. /* Verify a RSA PKCS1.5 signature against an expected hash.
  176. * Returns false on failure, true on success.
  177. */
  178. bool avb_rsa_verify(const uint8_t* key,
  179. size_t key_num_bytes,
  180. const uint8_t* sig,
  181. size_t sig_num_bytes,
  182. const uint8_t* hash,
  183. size_t hash_num_bytes,
  184. const uint8_t* padding,
  185. size_t padding_num_bytes) {
  186. uint8_t* buf = NULL;
  187. IAvbKey* parsed_key = NULL;
  188. bool success = false;
  189. if (key == NULL || sig == NULL || hash == NULL || padding == NULL) {
  190. avb_error("Invalid input.\n");
  191. goto out;
  192. }
  193. parsed_key = iavb_parse_key_data(key, key_num_bytes);
  194. if (parsed_key == NULL) {
  195. avb_error("Error parsing key.\n");
  196. goto out;
  197. }
  198. if (sig_num_bytes != (parsed_key->len * sizeof(uint32_t))) {
  199. avb_error("Signature length does not match key length.\n");
  200. goto out;
  201. }
  202. if (padding_num_bytes != sig_num_bytes - hash_num_bytes) {
  203. avb_error("Padding length does not match hash and signature lengths.\n");
  204. goto out;
  205. }
  206. buf = (uint8_t*)avb_malloc(sig_num_bytes);
  207. if (buf == NULL) {
  208. avb_error("Error allocating memory.\n");
  209. goto out;
  210. }
  211. avb_memcpy(buf, sig, sig_num_bytes);
  212. modpowF4(parsed_key, buf);
  213. /* Check padding bytes.
  214. *
  215. * Even though there are probably no timing issues here, we use
  216. * avb_safe_memcmp() just to be on the safe side.
  217. */
  218. if (avb_safe_memcmp(buf, padding, padding_num_bytes)) {
  219. avb_error("Padding check failed.\n");
  220. goto out;
  221. }
  222. /* Check hash. */
  223. if (avb_safe_memcmp(buf + padding_num_bytes, hash, hash_num_bytes)) {
  224. avb_error("Hash check failed.\n");
  225. goto out;
  226. }
  227. success = true;
  228. out:
  229. if (parsed_key != NULL) {
  230. iavb_free_parsed_key(parsed_key);
  231. }
  232. if (buf != NULL) {
  233. avb_free(buf);
  234. }
  235. return success;
  236. }