avb_rsa.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * Copyright (C) 2016 The Android Open Source Project
  3. *
  4. * Permission is hereby granted, free of charge, to any person
  5. * obtaining a copy of this software and associated documentation
  6. * files (the "Software"), to deal in the Software without
  7. * restriction, including without limitation the rights to use, copy,
  8. * modify, merge, publish, distribute, sublicense, and/or sell copies
  9. * of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  19. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  20. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. */
  24. /* Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
  25. * Use of this source code is governed by a BSD-style license that can be
  26. * found in the LICENSE file.
  27. */
  28. /* Implementation of RSA signature verification which uses a pre-processed
  29. * key for computation. The code extends libmincrypt RSA verification code to
  30. * support multiple RSA key lengths and hash digest algorithms.
  31. */
  32. #include "avb_rsa.h"
  33. #include "avb_sha.h"
  34. #include "avb_util.h"
  35. #include "avb_vbmeta_image.h"
  36. typedef struct IAvbKey {
  37. unsigned int len; /* Length of n[] in number of uint32_t */
  38. uint32_t n0inv; /* -1 / n[0] mod 2^32 */
  39. uint32_t* n; /* modulus as array (host-byte order) */
  40. uint32_t* rr; /* R^2 as array (host-byte order) */
  41. } IAvbKey;
  42. static IAvbKey* iavb_parse_key_data(const uint8_t* data, size_t length) {
  43. AvbRSAPublicKeyHeader h;
  44. IAvbKey* key = NULL;
  45. size_t expected_length;
  46. unsigned int i;
  47. const uint8_t* n;
  48. const uint8_t* rr;
  49. if (!avb_rsa_public_key_header_validate_and_byteswap(
  50. (const AvbRSAPublicKeyHeader*)data, &h)) {
  51. avb_error("Invalid key.\n");
  52. goto fail;
  53. }
  54. if (!(h.key_num_bits == 2048 || h.key_num_bits == 4096 ||
  55. h.key_num_bits == 8192)) {
  56. avb_error("Unexpected key length.\n");
  57. goto fail;
  58. }
  59. expected_length = sizeof(AvbRSAPublicKeyHeader) + 2 * h.key_num_bits / 8;
  60. if (length != expected_length) {
  61. avb_error("Key does not match expected length.\n");
  62. goto fail;
  63. }
  64. n = data + sizeof(AvbRSAPublicKeyHeader);
  65. rr = data + sizeof(AvbRSAPublicKeyHeader) + h.key_num_bits / 8;
  66. /* Store n and rr following the key header so we only have to do one
  67. * allocation.
  68. */
  69. key = (IAvbKey*)(avb_malloc(sizeof(IAvbKey) + 2 * h.key_num_bits / 8));
  70. if (key == NULL) {
  71. goto fail;
  72. }
  73. key->len = h.key_num_bits / 32;
  74. key->n0inv = h.n0inv;
  75. key->n = (uint32_t*)(key + 1); /* Skip ahead sizeof(IAvbKey) bytes. */
  76. key->rr = key->n + key->len;
  77. /* Crypto-code below (modpowF4() and friends) expects the key in
  78. * little-endian format (rather than the format we're storing the
  79. * key in), so convert it.
  80. */
  81. for (i = 0; i < key->len; i++) {
  82. key->n[i] = avb_be32toh(((uint32_t*)n)[key->len - i - 1]);
  83. key->rr[i] = avb_be32toh(((uint32_t*)rr)[key->len - i - 1]);
  84. }
  85. return key;
  86. fail:
  87. if (key != NULL) {
  88. avb_free(key);
  89. }
  90. return NULL;
  91. }
  92. static void iavb_free_parsed_key(IAvbKey* key) {
  93. avb_free(key);
  94. }
  95. /* a[] -= mod */
  96. static void subM(const IAvbKey* key, uint32_t* a) {
  97. int64_t A = 0;
  98. uint32_t i;
  99. for (i = 0; i < key->len; ++i) {
  100. A += (uint64_t)a[i] - key->n[i];
  101. a[i] = (uint32_t)A;
  102. A >>= 32;
  103. }
  104. }
  105. /* return a[] >= mod */
  106. static int geM(const IAvbKey* key, uint32_t* a) {
  107. uint32_t i;
  108. for (i = key->len; i;) {
  109. --i;
  110. if (a[i] < key->n[i]) {
  111. return 0;
  112. }
  113. if (a[i] > key->n[i]) {
  114. return 1;
  115. }
  116. }
  117. return 1; /* equal */
  118. }
  119. /* montgomery c[] += a * b[] / R % mod */
  120. static void montMulAdd(const IAvbKey* key,
  121. uint32_t* c,
  122. const uint32_t a,
  123. const uint32_t* b) {
  124. uint64_t A = (uint64_t)a * b[0] + c[0];
  125. uint32_t d0 = (uint32_t)A * key->n0inv;
  126. uint64_t B = (uint64_t)d0 * key->n[0] + (uint32_t)A;
  127. uint32_t i;
  128. for (i = 1; i < key->len; ++i) {
  129. A = (A >> 32) + (uint64_t)a * b[i] + c[i];
  130. B = (B >> 32) + (uint64_t)d0 * key->n[i] + (uint32_t)A;
  131. c[i - 1] = (uint32_t)B;
  132. }
  133. A = (A >> 32) + (B >> 32);
  134. c[i - 1] = (uint32_t)A;
  135. if (A >> 32) {
  136. subM(key, c);
  137. }
  138. }
  139. /* montgomery c[] = a[] * b[] / R % mod */
  140. static void montMul(const IAvbKey* key, uint32_t* c, uint32_t* a, uint32_t* b) {
  141. uint32_t i;
  142. for (i = 0; i < key->len; ++i) {
  143. c[i] = 0;
  144. }
  145. for (i = 0; i < key->len; ++i) {
  146. montMulAdd(key, c, a[i], b);
  147. }
  148. }
  149. /* In-place public exponentiation. (65537}
  150. * Input and output big-endian byte array in inout.
  151. */
  152. static void modpowF4(const IAvbKey* key, uint8_t* inout) {
  153. uint32_t* a = (uint32_t*)avb_malloc(key->len * sizeof(uint32_t));
  154. uint32_t* aR = (uint32_t*)avb_malloc(key->len * sizeof(uint32_t));
  155. uint32_t* aaR = (uint32_t*)avb_malloc(key->len * sizeof(uint32_t));
  156. if (a == NULL || aR == NULL || aaR == NULL) {
  157. goto out;
  158. }
  159. uint32_t* aaa = aaR; /* Re-use location. */
  160. int i;
  161. /* Convert from big endian byte array to little endian word array. */
  162. for (i = 0; i < (int)key->len; ++i) {
  163. uint32_t tmp = (inout[((key->len - 1 - i) * 4) + 0] << 24) |
  164. (inout[((key->len - 1 - i) * 4) + 1] << 16) |
  165. (inout[((key->len - 1 - i) * 4) + 2] << 8) |
  166. (inout[((key->len - 1 - i) * 4) + 3] << 0);
  167. a[i] = tmp;
  168. }
  169. montMul(key, aR, a, key->rr); /* aR = a * RR / R mod M */
  170. for (i = 0; i < 16; i += 2) {
  171. montMul(key, aaR, aR, aR); /* aaR = aR * aR / R mod M */
  172. montMul(key, aR, aaR, aaR); /* aR = aaR * aaR / R mod M */
  173. }
  174. montMul(key, aaa, aR, a); /* aaa = aR * a / R mod M */
  175. /* Make sure aaa < mod; aaa is at most 1x mod too large. */
  176. if (geM(key, aaa)) {
  177. subM(key, aaa);
  178. }
  179. /* Convert to bigendian byte array */
  180. for (i = (int)key->len - 1; i >= 0; --i) {
  181. uint32_t tmp = aaa[i];
  182. *inout++ = (uint8_t)(tmp >> 24);
  183. *inout++ = (uint8_t)(tmp >> 16);
  184. *inout++ = (uint8_t)(tmp >> 8);
  185. *inout++ = (uint8_t)(tmp >> 0);
  186. }
  187. out:
  188. if (a != NULL) {
  189. avb_free(a);
  190. }
  191. if (aR != NULL) {
  192. avb_free(aR);
  193. }
  194. if (aaR != NULL) {
  195. avb_free(aaR);
  196. }
  197. }
  198. /* Verify a RSA PKCS1.5 signature against an expected hash.
  199. * Returns false on failure, true on success.
  200. */
  201. bool avb_rsa_verify(const uint8_t* key,
  202. size_t key_num_bytes,
  203. const uint8_t* sig,
  204. size_t sig_num_bytes,
  205. const uint8_t* hash,
  206. size_t hash_num_bytes,
  207. const uint8_t* padding,
  208. size_t padding_num_bytes) {
  209. uint8_t* buf = NULL;
  210. IAvbKey* parsed_key = NULL;
  211. bool success = false;
  212. if (key == NULL || sig == NULL || hash == NULL || padding == NULL) {
  213. avb_error("Invalid input.\n");
  214. goto out;
  215. }
  216. parsed_key = iavb_parse_key_data(key, key_num_bytes);
  217. if (parsed_key == NULL) {
  218. avb_error("Error parsing key.\n");
  219. goto out;
  220. }
  221. if (sig_num_bytes != (parsed_key->len * sizeof(uint32_t))) {
  222. avb_error("Signature length does not match key length.\n");
  223. goto out;
  224. }
  225. if (padding_num_bytes != sig_num_bytes - hash_num_bytes) {
  226. avb_error("Padding length does not match hash and signature lengths.\n");
  227. goto out;
  228. }
  229. buf = (uint8_t*)avb_malloc(sig_num_bytes);
  230. if (buf == NULL) {
  231. avb_error("Error allocating memory.\n");
  232. goto out;
  233. }
  234. avb_memcpy(buf, sig, sig_num_bytes);
  235. modpowF4(parsed_key, buf);
  236. /* Check padding bytes.
  237. *
  238. * Even though there are probably no timing issues here, we use
  239. * avb_safe_memcmp() just to be on the safe side.
  240. */
  241. if (avb_safe_memcmp(buf, padding, padding_num_bytes)) {
  242. avb_error("Padding check failed.\n");
  243. goto out;
  244. }
  245. /* Check hash. */
  246. if (avb_safe_memcmp(buf + padding_num_bytes, hash, hash_num_bytes)) {
  247. avb_error("Hash check failed.\n");
  248. goto out;
  249. }
  250. success = true;
  251. out:
  252. if (parsed_key != NULL) {
  253. iavb_free_parsed_key(parsed_key);
  254. }
  255. if (buf != NULL) {
  256. avb_free(buf);
  257. }
  258. return success;
  259. }