vb21_common2_tests.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. * Tests for firmware image library.
  6. */
  7. #include <stdint.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include "2sysincludes.h"
  11. #include "2common.h"
  12. #include "2rsa.h"
  13. #include "vb21_common.h"
  14. #include "host_common.h"
  15. #include "host_key2.h"
  16. #include "host_signature2.h"
  17. #include "test_common.h"
  18. static const uint8_t test_data[] = "This is some test data to sign.";
  19. static const uint32_t test_size = sizeof(test_data);
  20. static void test_unpack_key(const struct vb21_packed_key *key)
  21. {
  22. struct vb2_public_key pubk;
  23. struct vb21_packed_key *key2;
  24. uint32_t size = key->c.total_size;
  25. /* Make a copy of the key for testing */
  26. key2 = (struct vb21_packed_key *)malloc(size);
  27. memcpy(key2, key, size);
  28. TEST_SUCC(vb21_unpack_key(&pubk, (uint8_t *)key2, size),
  29. "vb21_unpack_key() ok");
  30. memcpy(key2, key, size);
  31. key2->key_offset += 4;
  32. TEST_EQ(vb21_unpack_key(&pubk, (uint8_t *)key2, size),
  33. VB2_ERROR_COMMON_MEMBER_SIZE,
  34. "vb21_unpack_key() buffer too small");
  35. memcpy(key2, key, size);
  36. key2->c.fixed_size += size;
  37. TEST_EQ(vb21_unpack_key(&pubk, (uint8_t *)key2, size),
  38. VB2_ERROR_COMMON_FIXED_SIZE,
  39. "vb21_unpack_key() buffer too small for desc");
  40. memcpy(key2, key, size);
  41. key2->c.desc_size = 0;
  42. TEST_SUCC(vb21_unpack_key(&pubk, (uint8_t *)key2, size),
  43. "vb21_unpack_key() no desc");
  44. TEST_EQ(strcmp(pubk.desc, ""), 0, " empty desc string");
  45. memcpy(key2, key, size);
  46. key2->c.magic++;
  47. TEST_EQ(vb21_unpack_key(&pubk, (uint8_t *)key2, size),
  48. VB2_ERROR_UNPACK_KEY_MAGIC,
  49. "vb21_unpack_key() bad magic");
  50. memcpy(key2, key, size);
  51. key2->c.struct_version_major++;
  52. TEST_EQ(vb21_unpack_key(&pubk, (uint8_t *)key2, size),
  53. VB2_ERROR_UNPACK_KEY_STRUCT_VERSION,
  54. "vb21_unpack_key() bad major version");
  55. /*
  56. * Minor version changes are ok. Note that this test assumes that the
  57. * source key struct version is the highest actually known to the
  58. * reader. If the reader does know about minor version + 1 and that
  59. * adds fields, this test will likely fail. But at that point, we
  60. * should have already added a test for minor version compatibility to
  61. * handle both old and new struct versions, so someone will have
  62. * noticed this comment.
  63. */
  64. memcpy(key2, key, size);
  65. key2->c.struct_version_minor++;
  66. TEST_SUCC(vb21_unpack_key(&pubk, (uint8_t *)key2, size),
  67. "vb21_unpack_key() minor version change ok");
  68. memcpy(key2, key, size);
  69. key2->sig_alg = VB2_SIG_INVALID;
  70. TEST_EQ(vb21_unpack_key(&pubk, (uint8_t *)key2, size),
  71. VB2_ERROR_UNPACK_KEY_SIG_ALGORITHM,
  72. "vb21_unpack_key() bad sig algorithm");
  73. memcpy(key2, key, size);
  74. key2->hash_alg = VB2_HASH_INVALID;
  75. TEST_EQ(vb21_unpack_key(&pubk, (uint8_t *)key2, size),
  76. VB2_ERROR_UNPACK_KEY_HASH_ALGORITHM,
  77. "vb21_unpack_key() bad hash algorithm");
  78. memcpy(key2, key, size);
  79. key2->key_size -= 4;
  80. TEST_EQ(vb21_unpack_key(&pubk, (uint8_t *)key2, size),
  81. VB2_ERROR_UNPACK_KEY_SIZE,
  82. "vb21_unpack_key() invalid size");
  83. memcpy(key2, key, size);
  84. key2->key_offset--;
  85. TEST_EQ(vb21_unpack_key(&pubk, (uint8_t *)key2, size),
  86. VB2_ERROR_COMMON_MEMBER_UNALIGNED,
  87. "vb21_unpack_key() unaligned data");
  88. memcpy(key2, key, size);
  89. *(uint32_t *)((uint8_t *)key2 + key2->key_offset) /= 2;
  90. TEST_EQ(vb21_unpack_key(&pubk, (uint8_t *)key2, size),
  91. VB2_ERROR_UNPACK_KEY_ARRAY_SIZE,
  92. "vb21_unpack_key() invalid key array size");
  93. free(key2);
  94. }
  95. static void test_verify_signature(const struct vb21_signature *sig)
  96. {
  97. struct vb21_signature *sig2;
  98. uint8_t *buf2;
  99. uint32_t size;
  100. /* Make a copy of the signature */
  101. size = sig->c.total_size;
  102. buf2 = malloc(size);
  103. sig2 = (struct vb21_signature *)buf2;
  104. memcpy(buf2, sig, size);
  105. TEST_SUCC(vb21_verify_signature(sig2, size), "verify_sig ok");
  106. sig2->c.magic = VB21_MAGIC_PACKED_KEY;
  107. TEST_EQ(vb21_verify_signature(sig2, size), VB2_ERROR_SIG_MAGIC,
  108. "verify_sig magic");
  109. memcpy(buf2, sig, size);
  110. sig2->c.total_size += 4;
  111. TEST_EQ(vb21_verify_signature(sig2, size), VB2_ERROR_COMMON_TOTAL_SIZE,
  112. "verify_sig common header");
  113. memcpy(buf2, sig, size);
  114. sig2->c.struct_version_minor++;
  115. TEST_SUCC(vb21_verify_signature(sig2, size), "verify_sig minor ver");
  116. sig2->c.struct_version_major++;
  117. TEST_EQ(vb21_verify_signature(sig2, size), VB2_ERROR_SIG_VERSION,
  118. "verify_sig major ver");
  119. memcpy(buf2, sig, size);
  120. sig2->c.fixed_size -= 4;
  121. sig2->c.desc_size += 4;
  122. TEST_EQ(vb21_verify_signature(sig2, size), VB2_ERROR_SIG_HEADER_SIZE,
  123. "verify_sig header size");
  124. memcpy(buf2, sig, size);
  125. sig2->sig_size += 4;
  126. TEST_EQ(vb21_verify_signature(sig2, size), VB2_ERROR_COMMON_MEMBER_SIZE,
  127. "verify_sig sig size");
  128. memcpy(buf2, sig, size);
  129. sig2->sig_alg = VB2_SIG_INVALID;
  130. TEST_EQ(vb21_verify_signature(sig2, size), VB2_ERROR_SIG_ALGORITHM,
  131. "verify_sig sig alg");
  132. memcpy(buf2, sig, size);
  133. sig2->sig_alg = (sig2->sig_alg == VB2_SIG_NONE ?
  134. VB2_SIG_RSA1024 : VB2_SIG_NONE);
  135. TEST_EQ(vb21_verify_signature(sig2, size), VB2_ERROR_SIG_SIZE,
  136. "verify_sig sig size");
  137. free(buf2);
  138. }
  139. static void test_verify_data(const struct vb2_public_key *pubk_orig,
  140. const struct vb21_signature *sig)
  141. {
  142. uint8_t workbuf[VB2_VERIFY_DATA_WORKBUF_BYTES]
  143. __attribute__ ((aligned (VB2_WORKBUF_ALIGN)));
  144. struct vb2_workbuf wb;
  145. struct vb2_public_key pubk;
  146. struct vb21_signature *sig2;
  147. uint8_t *buf2;
  148. uint32_t size;
  149. vb2_workbuf_init(&wb, workbuf, sizeof(workbuf));
  150. pubk = *pubk_orig;
  151. /* Allocate signature copy for tests */
  152. size = sig->c.total_size;
  153. buf2 = malloc(size);
  154. sig2 = (struct vb21_signature *)buf2;
  155. memcpy(buf2, sig, size);
  156. pubk.sig_alg = VB2_SIG_INVALID;
  157. TEST_EQ(vb21_verify_data(test_data, test_size, sig2, &pubk, &wb),
  158. VB2_ERROR_VDATA_ALGORITHM, "vb21_verify_data() bad sig alg");
  159. pubk = *pubk_orig;
  160. memcpy(buf2, sig, size);
  161. pubk.hash_alg = VB2_HASH_INVALID;
  162. TEST_EQ(vb21_verify_data(test_data, test_size, sig2, &pubk, &wb),
  163. VB2_ERROR_VDATA_DIGEST_SIZE,
  164. "vb21_verify_data() bad hash alg");
  165. pubk = *pubk_orig;
  166. vb2_workbuf_init(&wb, workbuf, 4);
  167. memcpy(buf2, sig, size);
  168. TEST_EQ(vb21_verify_data(test_data, test_size, sig2, &pubk, &wb),
  169. VB2_ERROR_VDATA_WORKBUF_DIGEST,
  170. "vb21_verify_data() workbuf too small");
  171. vb2_workbuf_init(&wb, workbuf, sizeof(workbuf));
  172. memcpy(buf2, sig, size);
  173. TEST_EQ(vb21_verify_data(test_data, test_size, sig2, &pubk, &wb),
  174. 0, "vb21_verify_data() ok");
  175. memcpy(buf2, sig, size);
  176. sig2->sig_size -= 16;
  177. TEST_EQ(vb21_verify_data(test_data, test_size, sig2, &pubk, &wb),
  178. VB2_ERROR_VDATA_SIG_SIZE, "vb21_verify_data() wrong sig size");
  179. memcpy(buf2, sig, size);
  180. TEST_EQ(vb21_verify_data(test_data, test_size - 1, sig2, &pubk, &wb),
  181. VB2_ERROR_VDATA_SIZE, "vb21_verify_data() wrong data size");
  182. memcpy(buf2, sig, size);
  183. sig2->hash_alg = (sig2->hash_alg == VB2_HASH_SHA1 ?
  184. VB2_HASH_SHA256 : VB2_HASH_SHA1);
  185. TEST_EQ(vb21_verify_data(test_data, test_size, sig2, &pubk, &wb),
  186. VB2_ERROR_VDATA_ALGORITHM_MISMATCH,
  187. "vb21_verify_data() alg mismatch");
  188. memcpy(buf2, sig, size);
  189. buf2[sig2->sig_offset] ^= 0x5A;
  190. TEST_EQ(vb21_verify_data(test_data, test_size, sig2, &pubk, &wb),
  191. VB2_ERROR_RSA_PADDING, "vb21_verify_data() wrong sig");
  192. free(buf2);
  193. }
  194. int test_algorithm(int key_algorithm, const char *keys_dir)
  195. {
  196. char filename[1024];
  197. int rsa_bits = 8 * vb2_rsa_sig_size(
  198. vb2_crypto_to_signature(key_algorithm));
  199. enum vb2_signature_algorithm sig_alg =
  200. vb2_crypto_to_signature(key_algorithm);
  201. enum vb2_hash_algorithm hash_alg = vb2_crypto_to_hash(key_algorithm);
  202. struct vb2_private_key *prik = NULL;
  203. struct vb21_signature *sig2 = NULL;
  204. struct vb2_public_key *pubk = NULL;
  205. struct vb21_packed_key *key2 = NULL;
  206. printf("***Testing algorithm: %s\n",
  207. vb2_get_crypto_algorithm_name(key_algorithm));
  208. snprintf(filename, sizeof(filename),
  209. "%s/key_rsa%d.pem", keys_dir, rsa_bits);
  210. TEST_SUCC(vb2_private_key_read_pem(&prik, filename),
  211. "Read private key");
  212. prik->hash_alg = hash_alg;
  213. prik->sig_alg = sig_alg;
  214. vb2_private_key_set_desc(prik, "private key");
  215. snprintf(filename, sizeof(filename),
  216. "%s/key_rsa%d.keyb", keys_dir, rsa_bits);
  217. TEST_SUCC(vb2_public_key_read_keyb(&pubk, filename),
  218. "Read public key");
  219. pubk->hash_alg = hash_alg;
  220. vb2_public_key_set_desc(pubk, "public key");
  221. TEST_SUCC(vb21_public_key_pack(&key2, pubk), "Pack public key");
  222. /* Calculate good signatures */
  223. TEST_SUCC(vb21_sign_data(&sig2, test_data, test_size, prik, ""),
  224. "Make test signature");
  225. test_unpack_key(key2);
  226. test_verify_data(pubk, sig2);
  227. test_verify_signature(sig2);
  228. free(key2);
  229. free(sig2);
  230. vb2_private_key_free(prik);
  231. vb2_public_key_free(pubk);
  232. return 0;
  233. }
  234. /* Test only the algorithms we use */
  235. const int key_algs[] = {
  236. VB2_ALG_RSA2048_SHA256,
  237. VB2_ALG_RSA4096_SHA256,
  238. VB2_ALG_RSA8192_SHA512,
  239. };
  240. int main(int argc, char *argv[]) {
  241. if (argc == 2) {
  242. int i;
  243. for (i = 0; i < ARRAY_SIZE(key_algs); i++) {
  244. if (test_algorithm(key_algs[i], argv[1]))
  245. return 1;
  246. }
  247. } else if (argc == 3 && !strcasecmp(argv[2], "--all")) {
  248. /* Test all the algorithms */
  249. int alg;
  250. for (alg = 0; alg < VB2_ALG_COUNT; alg++) {
  251. if (test_algorithm(alg, argv[1]))
  252. return 1;
  253. }
  254. } else {
  255. fprintf(stderr, "Usage: %s <keys_dir> [--all]", argv[0]);
  256. return -1;
  257. }
  258. return gTestSuccess ? 0 : 255;
  259. }