vb21_common_tests.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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 2common.c
  6. */
  7. #include "2sysincludes.h"
  8. #include "2common.h"
  9. #include "2rsa.h"
  10. #include "vb21_common.h"
  11. #include "host_fw_preamble2.h"
  12. #include "host_key2.h"
  13. #include "host_keyblock2.h"
  14. #include "host_signature2.h"
  15. #include "test_common.h"
  16. static const uint8_t test_data[] = "This is some test data to sign.";
  17. static const uint8_t test_data2[] = "Some more test data";
  18. static const uint8_t test_data3[] = "Even more test data";
  19. /*
  20. * Test struct packing for vboot_struct.h structs which are passed between
  21. * firmware and OS, or passed between different phases of firmware.
  22. */
  23. static void test_struct_packing(void)
  24. {
  25. /* Test new struct sizes */
  26. TEST_EQ(EXPECTED_ID_SIZE,
  27. sizeof(struct vb2_id),
  28. "sizeof(vb2_id)");
  29. TEST_EQ(EXPECTED_VB21_STRUCT_COMMON_SIZE,
  30. sizeof(struct vb21_struct_common),
  31. "sizeof(vb21_struct_common)");
  32. TEST_EQ(EXPECTED_VB21_PACKED_KEY_SIZE,
  33. sizeof(struct vb21_packed_key),
  34. "sizeof(vb21_packed_key)");
  35. TEST_EQ(EXPECTED_VB21_SIGNATURE_SIZE,
  36. sizeof(struct vb21_signature),
  37. "sizeof(vb21_signature)");
  38. TEST_EQ(EXPECTED_VB21_KEYBLOCK_SIZE,
  39. sizeof(struct vb21_keyblock),
  40. "sizeof(vb21_keyblock)");
  41. TEST_EQ(EXPECTED_VB21_FW_PREAMBLE_SIZE,
  42. sizeof(struct vb21_fw_preamble),
  43. "sizeof(vb21_fw_preamble)");
  44. }
  45. /**
  46. * Common header functions
  47. */
  48. static void test_common_header_functions(void)
  49. {
  50. uint8_t cbuf[sizeof(struct vb21_struct_common) + 128];
  51. uint8_t cbufgood[sizeof(cbuf)];
  52. struct vb21_struct_common *c = (struct vb21_struct_common *)cbuf;
  53. struct vb21_struct_common *c2;
  54. const char test_desc[32] = "test desc";
  55. uint32_t desc_end, m;
  56. c->total_size = sizeof(cbuf);
  57. c->fixed_size = sizeof(*c);
  58. c->desc_size = sizeof(test_desc);
  59. memcpy(cbuf + c->fixed_size, test_desc, sizeof(test_desc));
  60. desc_end = c->fixed_size + c->desc_size;
  61. c2 = (struct vb21_struct_common *)(cbuf + desc_end);
  62. c2->total_size = c->total_size - desc_end;
  63. c2->fixed_size = sizeof(*c2);
  64. c2->desc_size = 0;
  65. /* Description helper */
  66. TEST_EQ(0, strcmp(vb21_common_desc(c), test_desc), "vb21_common_desc()");
  67. TEST_EQ(0, strcmp(vb21_common_desc(c2), ""), "vb21_common_desc() empty");
  68. TEST_SUCC(vb21_verify_common_header(cbuf, sizeof(cbuf)),
  69. "vb21_verify_common_header() good");
  70. memcpy(cbufgood, cbuf, sizeof(cbufgood));
  71. memcpy(cbuf, cbufgood, sizeof(cbuf));
  72. c->total_size += 4;
  73. TEST_EQ(vb21_verify_common_header(cbuf, sizeof(cbuf)),
  74. VB2_ERROR_COMMON_TOTAL_SIZE,
  75. "vb21_verify_common_header() total size");
  76. memcpy(cbuf, cbufgood, sizeof(cbuf));
  77. c->fixed_size = c->total_size + 4;
  78. TEST_EQ(vb21_verify_common_header(cbuf, sizeof(cbuf)),
  79. VB2_ERROR_COMMON_FIXED_SIZE,
  80. "vb21_verify_common_header() fixed size");
  81. memcpy(cbuf, cbufgood, sizeof(cbuf));
  82. c->desc_size = c->total_size - c->fixed_size + 4;
  83. TEST_EQ(vb21_verify_common_header(cbuf, sizeof(cbuf)),
  84. VB2_ERROR_COMMON_DESC_SIZE,
  85. "vb21_verify_common_header() desc size");
  86. memcpy(cbuf, cbufgood, sizeof(cbuf));
  87. c->total_size--;
  88. TEST_EQ(vb21_verify_common_header(cbuf, sizeof(cbuf)),
  89. VB2_ERROR_COMMON_TOTAL_UNALIGNED,
  90. "vb21_verify_common_header() total unaligned");
  91. memcpy(cbuf, cbufgood, sizeof(cbuf));
  92. c->fixed_size++;
  93. TEST_EQ(vb21_verify_common_header(cbuf, sizeof(cbuf)),
  94. VB2_ERROR_COMMON_FIXED_UNALIGNED,
  95. "vb21_verify_common_header() fixed unaligned");
  96. memcpy(cbuf, cbufgood, sizeof(cbuf));
  97. c->desc_size--;
  98. TEST_EQ(vb21_verify_common_header(cbuf, sizeof(cbuf)),
  99. VB2_ERROR_COMMON_DESC_UNALIGNED,
  100. "vb21_verify_common_header() desc unaligned");
  101. memcpy(cbuf, cbufgood, sizeof(cbuf));
  102. c->desc_size = -4;
  103. TEST_EQ(vb21_verify_common_header(cbuf, sizeof(cbuf)),
  104. VB2_ERROR_COMMON_DESC_WRAPS,
  105. "vb21_verify_common_header() desc wraps");
  106. memcpy(cbuf, cbufgood, sizeof(cbuf));
  107. cbuf[desc_end - 1] = 1;
  108. TEST_EQ(vb21_verify_common_header(cbuf, sizeof(cbuf)),
  109. VB2_ERROR_COMMON_DESC_TERMINATOR,
  110. "vb21_verify_common_header() desc not terminated");
  111. /* Member checking function */
  112. memcpy(cbuf, cbufgood, sizeof(cbuf));
  113. m = 0;
  114. TEST_SUCC(vb21_verify_common_member(cbuf, &m, c->total_size - 8, 4),
  115. "vb21_verify_common_member()");
  116. TEST_EQ(m, c->total_size - 4, " new minimum");
  117. m = desc_end;
  118. TEST_SUCC(vb21_verify_common_member(cbuf, &m, desc_end, 4),
  119. "vb21_verify_common_member() good offset");
  120. TEST_EQ(m, desc_end + 4, " new minimum");
  121. m = 0;
  122. TEST_EQ(vb21_verify_common_member(cbuf, &m, c->total_size - 8, -4),
  123. VB2_ERROR_COMMON_MEMBER_WRAPS,
  124. "vb21_verify_common_member() wraps");
  125. m = 0;
  126. TEST_EQ(vb21_verify_common_member(cbuf, &m, c->total_size - 7, 4),
  127. VB2_ERROR_COMMON_MEMBER_UNALIGNED,
  128. "vb21_verify_common_member() offset unaligned");
  129. m = 0;
  130. TEST_EQ(vb21_verify_common_member(cbuf, &m, c->total_size - 8, 5),
  131. VB2_ERROR_COMMON_MEMBER_UNALIGNED,
  132. "vb21_verify_common_member() size unaligned");
  133. m = 0;
  134. TEST_EQ(vb21_verify_common_member(cbuf, &m, desc_end - 4, 4),
  135. VB2_ERROR_COMMON_MEMBER_OVERLAP,
  136. "vb21_verify_common_member() overlap");
  137. m = desc_end + 4;
  138. TEST_EQ(vb21_verify_common_member(cbuf, &m, desc_end, 4),
  139. VB2_ERROR_COMMON_MEMBER_OVERLAP,
  140. "vb21_verify_common_member() overlap 2");
  141. m = 0;
  142. TEST_EQ(vb21_verify_common_member(cbuf, &m, c->total_size - 4, 8),
  143. VB2_ERROR_COMMON_MEMBER_SIZE,
  144. "vb21_verify_common_member() size");
  145. /* Subobject checking */
  146. m = 0;
  147. TEST_SUCC(vb21_verify_common_subobject(cbuf, &m, desc_end),
  148. "vb21_verify_common_subobject() good offset");
  149. TEST_EQ(m, sizeof(cbuf), " new minimum");
  150. m = desc_end + 4;
  151. TEST_EQ(vb21_verify_common_subobject(cbuf, &m, desc_end),
  152. VB2_ERROR_COMMON_MEMBER_OVERLAP,
  153. "vb21_verify_common_subobject() overlap");
  154. m = 0;
  155. c2->total_size += 4;
  156. TEST_EQ(vb21_verify_common_subobject(cbuf, &m, desc_end),
  157. VB2_ERROR_COMMON_TOTAL_SIZE,
  158. "vb21_verify_common_subobject() size");
  159. }
  160. /**
  161. * Signature size
  162. */
  163. static void test_sig_size(void)
  164. {
  165. TEST_EQ(vb2_sig_size(VB2_SIG_INVALID, VB2_HASH_SHA256), 0,
  166. "vb2_sig_size() sig invalid");
  167. TEST_EQ(vb2_sig_size(VB2_SIG_RSA2048, VB2_HASH_INVALID), 0,
  168. "vb2_sig_size() hash invalid");
  169. TEST_EQ(vb2_sig_size(VB2_SIG_RSA2048, VB2_HASH_SHA256), 2048 / 8,
  170. "vb2_sig_size() RSA2048");
  171. TEST_EQ(vb2_sig_size(VB2_SIG_RSA4096, VB2_HASH_SHA256), 4096 / 8,
  172. "vb2_sig_size() RSA4096");
  173. TEST_EQ(vb2_sig_size(VB2_SIG_RSA8192, VB2_HASH_SHA512), 8192 / 8,
  174. "vb2_sig_size() RSA8192");
  175. TEST_EQ(vb2_sig_size(VB2_SIG_NONE, VB2_HASH_SHA1),
  176. VB2_SHA1_DIGEST_SIZE, "vb2_sig_size() SHA1");
  177. TEST_EQ(vb2_sig_size(VB2_SIG_NONE, VB2_HASH_SHA256),
  178. VB2_SHA256_DIGEST_SIZE, "vb2_sig_size() SHA256");
  179. TEST_EQ(vb2_sig_size(VB2_SIG_NONE, VB2_HASH_SHA512),
  180. VB2_SHA512_DIGEST_SIZE, "vb2_sig_size() SHA512");
  181. }
  182. /**
  183. * Verify data on bare hash
  184. */
  185. static void test_verify_hash(void)
  186. {
  187. struct vb21_signature *sig;
  188. const struct vb2_private_key *prik;
  189. struct vb2_public_key pubk;
  190. uint8_t workbuf[VB2_VERIFY_DATA_WORKBUF_BYTES]
  191. __attribute__ ((aligned (VB2_WORKBUF_ALIGN)));
  192. struct vb2_workbuf wb;
  193. vb2_workbuf_init(&wb, workbuf, sizeof(workbuf));
  194. TEST_SUCC(vb2_private_key_hash(&prik, VB2_HASH_SHA256),
  195. "create private hash key");
  196. TEST_SUCC(vb2_public_key_hash(&pubk, VB2_HASH_SHA256),
  197. "create hash key");
  198. /* Create the signature */
  199. TEST_SUCC(vb21_sign_data(&sig, test_data, sizeof(test_data),
  200. prik, NULL),
  201. "create hash sig");
  202. TEST_SUCC(vb21_verify_data(test_data, sizeof(test_data),
  203. sig, &pubk, &wb),
  204. "vb21_verify_data() hash ok");
  205. *((uint8_t *)sig + sig->sig_offset) ^= 0xab;
  206. TEST_EQ(vb21_verify_data(test_data, sizeof(test_data), sig, &pubk, &wb),
  207. VB2_ERROR_VDATA_VERIFY_DIGEST, "vb21_verify_data() hash bad");
  208. free(sig);
  209. }
  210. /**
  211. * Verify keyblock
  212. */
  213. static void test_verify_keyblock(void)
  214. {
  215. const char desc[16] = "test keyblock";
  216. const struct vb2_private_key *prik[2];
  217. struct vb2_public_key pubk, pubk2, pubk3;
  218. struct vb21_signature *sig;
  219. struct vb21_keyblock *kbuf;
  220. uint32_t buf_size;
  221. uint8_t *buf, *buf2;
  222. uint8_t workbuf[VB2_KEY_BLOCK_VERIFY_WORKBUF_BYTES]
  223. __attribute__ ((aligned (VB2_WORKBUF_ALIGN)));
  224. struct vb2_workbuf wb;
  225. TEST_SUCC(vb2_public_key_hash(&pubk, VB2_HASH_SHA256),
  226. "create hash key 1");
  227. TEST_SUCC(vb2_public_key_hash(&pubk2, VB2_HASH_SHA512),
  228. "create hash key 2");
  229. TEST_SUCC(vb2_public_key_hash(&pubk3, VB2_HASH_SHA1),
  230. "create hash key 3");
  231. TEST_SUCC(vb2_private_key_hash(prik + 0, VB2_HASH_SHA256),
  232. "create private key 1");
  233. TEST_SUCC(vb2_private_key_hash(prik + 1, VB2_HASH_SHA512),
  234. "create private key 2");
  235. /* Create the test keyblock */
  236. TEST_SUCC(vb21_keyblock_create(&kbuf, &pubk3, prik, 2, 0x4321, desc),
  237. "create keyblock");
  238. buf = (uint8_t *)kbuf;
  239. buf_size = kbuf->c.total_size;
  240. /* Make a copy of the buffer, so we can mangle it for tests */
  241. buf2 = malloc(buf_size);
  242. memcpy(buf2, buf, buf_size);
  243. vb2_workbuf_init(&wb, workbuf, sizeof(workbuf));
  244. kbuf = (struct vb21_keyblock *)buf;
  245. TEST_SUCC(vb21_verify_keyblock(kbuf, buf_size, &pubk, &wb),
  246. "vb21_verify_keyblock()");
  247. memcpy(buf, buf2, buf_size);
  248. TEST_SUCC(vb21_verify_keyblock(kbuf, buf_size, &pubk2, &wb),
  249. "vb21_verify_keyblock() key 2");
  250. memcpy(buf, buf2, buf_size);
  251. TEST_EQ(vb21_verify_keyblock(kbuf, buf_size, &pubk3, &wb),
  252. VB2_ERROR_KEYBLOCK_SIG_ID,
  253. "vb21_verify_keyblock() key not present");
  254. memcpy(buf, buf2, buf_size);
  255. kbuf->c.magic = VB21_MAGIC_PACKED_KEY;
  256. TEST_EQ(vb21_verify_keyblock(kbuf, buf_size, &pubk, &wb),
  257. VB2_ERROR_KEYBLOCK_MAGIC,
  258. "vb21_verify_keyblock() magic");
  259. memcpy(buf, buf2, buf_size);
  260. kbuf->c.fixed_size++;
  261. TEST_EQ(vb21_verify_keyblock(kbuf, buf_size, &pubk, &wb),
  262. VB2_ERROR_COMMON_FIXED_UNALIGNED,
  263. "vb21_verify_keyblock() header");
  264. memcpy(buf, buf2, buf_size);
  265. kbuf->c.struct_version_major++;
  266. TEST_EQ(vb21_verify_keyblock(kbuf, buf_size, &pubk, &wb),
  267. VB2_ERROR_KEYBLOCK_HEADER_VERSION,
  268. "vb21_verify_keyblock() major version");
  269. memcpy(buf, buf2, buf_size);
  270. kbuf->c.struct_version_minor++;
  271. /* That changes the signature, so resign the keyblock */
  272. vb21_sign_data(&sig, buf, kbuf->sig_offset, prik[0], NULL);
  273. memcpy(buf + kbuf->sig_offset, sig, sig->c.total_size);
  274. free(sig);
  275. TEST_SUCC(vb21_verify_keyblock(kbuf, buf_size, &pubk, &wb),
  276. "vb21_verify_keyblock() minor version");
  277. memcpy(buf, buf2, buf_size);
  278. kbuf->c.fixed_size -= 4;
  279. kbuf->c.desc_size += 4;
  280. TEST_EQ(vb21_verify_keyblock(kbuf, buf_size, &pubk, &wb),
  281. VB2_ERROR_KEYBLOCK_SIZE,
  282. "vb21_verify_keyblock() header size");
  283. memcpy(buf, buf2, buf_size);
  284. kbuf->key_offset = kbuf->c.total_size - 4;
  285. TEST_EQ(vb21_verify_keyblock(kbuf, buf_size, &pubk, &wb),
  286. VB2_ERROR_COMMON_MEMBER_SIZE,
  287. "vb21_verify_keyblock() data key outside");
  288. memcpy(buf, buf2, buf_size);
  289. sig = (struct vb21_signature *)(buf + kbuf->sig_offset);
  290. sig->data_size--;
  291. TEST_EQ(vb21_verify_keyblock(kbuf, buf_size, &pubk, &wb),
  292. VB2_ERROR_KEYBLOCK_SIGNED_SIZE,
  293. "vb21_verify_keyblock() signed wrong size");
  294. memcpy(buf, buf2, buf_size);
  295. sig = (struct vb21_signature *)(buf + kbuf->sig_offset);
  296. sig->c.total_size = kbuf->c.total_size - 4;
  297. TEST_EQ(vb21_verify_keyblock(kbuf, buf_size, &pubk, &wb),
  298. VB2_ERROR_COMMON_TOTAL_SIZE,
  299. "vb21_verify_keyblock() key outside keyblock");
  300. memcpy(buf, buf2, buf_size);
  301. sig = (struct vb21_signature *)(buf + kbuf->sig_offset);
  302. sig->c.struct_version_major++;
  303. TEST_EQ(vb21_verify_keyblock(kbuf, buf_size, &pubk, &wb),
  304. VB2_ERROR_SIG_VERSION,
  305. "vb21_verify_keyblock() corrupt key");
  306. memcpy(buf, buf2, buf_size);
  307. kbuf->c.struct_version_minor++;
  308. TEST_EQ(vb21_verify_keyblock(kbuf, buf_size, &pubk, &wb),
  309. VB2_ERROR_VDATA_VERIFY_DIGEST,
  310. "vb21_verify_keyblock() corrupt");
  311. free(buf);
  312. free(buf2);
  313. }
  314. /**
  315. * Verify firmware preamble
  316. */
  317. static void test_verify_fw_preamble(void)
  318. {
  319. const char desc[16] = "test preamble";
  320. const struct vb2_private_key *prikhash;
  321. struct vb21_signature *hashes[3];
  322. struct vb2_public_key pubk;
  323. struct vb21_signature *sig;
  324. struct vb21_fw_preamble *pre;
  325. uint32_t buf_size;
  326. uint8_t *buf, *buf2;
  327. uint8_t workbuf[VB2_VERIFY_FIRMWARE_PREAMBLE_WORKBUF_BYTES]
  328. __attribute__ ((aligned (VB2_WORKBUF_ALIGN)));
  329. struct vb2_workbuf wb;
  330. /*
  331. * Preambles will usually be signed with a real key not a bare hash,
  332. * but the call to vb21_verify_data() inside the preamble check is the
  333. * same (and its functionality is verified separately), and using a
  334. * bare hash here saves us from needing to have a private key to do
  335. * this test.
  336. */
  337. TEST_SUCC(vb2_public_key_hash(&pubk, VB2_HASH_SHA256),
  338. "create hash key");
  339. TEST_SUCC(vb2_private_key_hash(&prikhash, VB2_HASH_SHA256),
  340. "Create private hash key");
  341. /* Create some signatures */
  342. TEST_SUCC(vb21_sign_data(hashes + 0, test_data, sizeof(test_data),
  343. prikhash, "Hash 1"),
  344. "Hash 1");
  345. TEST_SUCC(vb21_sign_data(hashes + 1, test_data2, sizeof(test_data2),
  346. prikhash, "Hash 2"),
  347. "Hash 2");
  348. TEST_SUCC(vb21_sign_data(hashes + 2, test_data3, sizeof(test_data3),
  349. prikhash, "Hash 3"),
  350. "Hash 3");
  351. /* Test good preamble */
  352. TEST_SUCC(vb21_fw_preamble_create(
  353. &pre, prikhash,
  354. (const struct vb21_signature **)hashes,
  355. 3, 0x1234, 0x5678, desc),
  356. "Create preamble good");
  357. buf = (uint8_t *)pre;
  358. buf_size = pre->c.total_size;
  359. /* Make a copy of the buffer, so we can mangle it for tests */
  360. buf2 = malloc(buf_size);
  361. memcpy(buf2, buf, buf_size);
  362. vb2_workbuf_init(&wb, workbuf, sizeof(workbuf));
  363. pre = (struct vb21_fw_preamble *)buf;
  364. TEST_SUCC(vb21_verify_fw_preamble(pre, buf_size, &pubk, &wb),
  365. "vb21_verify_fw_preamble()");
  366. memcpy(buf, buf2, buf_size);
  367. pre->c.magic = VB21_MAGIC_PACKED_KEY;
  368. TEST_EQ(vb21_verify_fw_preamble(pre, buf_size, &pubk, &wb),
  369. VB2_ERROR_PREAMBLE_MAGIC,
  370. "vb21_verify_fw_preamble() magic");
  371. memcpy(buf, buf2, buf_size);
  372. pre->c.fixed_size++;
  373. TEST_EQ(vb21_verify_fw_preamble(pre, buf_size, &pubk, &wb),
  374. VB2_ERROR_COMMON_FIXED_UNALIGNED,
  375. "vb21_verify_fw_preamble() header");
  376. memcpy(buf, buf2, buf_size);
  377. pre->c.struct_version_major++;
  378. TEST_EQ(vb21_verify_fw_preamble(pre, buf_size, &pubk, &wb),
  379. VB2_ERROR_PREAMBLE_HEADER_VERSION,
  380. "vb21_verify_fw_preamble() major version");
  381. memcpy(buf, buf2, buf_size);
  382. pre->c.struct_version_minor++;
  383. /* That changes the signature, so resign the fw_preamble */
  384. vb21_sign_data(&sig, buf, pre->sig_offset, prikhash, NULL);
  385. memcpy(buf + pre->sig_offset, sig, sig->c.total_size);
  386. free(sig);
  387. TEST_SUCC(vb21_verify_fw_preamble(pre, buf_size, &pubk, &wb),
  388. "vb21_verify_fw_preamble() minor version");
  389. memcpy(buf, buf2, buf_size);
  390. pre->c.fixed_size -= 4;
  391. pre->c.desc_size += 4;
  392. TEST_EQ(vb21_verify_fw_preamble(pre, buf_size, &pubk, &wb),
  393. VB2_ERROR_PREAMBLE_SIZE,
  394. "vb21_verify_fw_preamble() header size");
  395. memcpy(buf, buf2, buf_size);
  396. sig = (struct vb21_signature *)(buf + pre->hash_offset);
  397. sig->c.total_size += pre->c.total_size;
  398. TEST_EQ(vb21_verify_fw_preamble(pre, buf_size, &pubk, &wb),
  399. VB2_ERROR_COMMON_TOTAL_SIZE,
  400. "vb21_verify_fw_preamble() hash size");
  401. memcpy(buf, buf2, buf_size);
  402. sig = (struct vb21_signature *)(buf + pre->hash_offset);
  403. sig->sig_size /= 2;
  404. TEST_EQ(vb21_verify_fw_preamble(pre, buf_size, &pubk, &wb),
  405. VB2_ERROR_SIG_SIZE,
  406. "vb21_verify_fw_preamble() hash integrity");
  407. memcpy(buf, buf2, buf_size);
  408. pre->hash_count++;
  409. TEST_EQ(vb21_verify_fw_preamble(pre, buf_size, &pubk, &wb),
  410. VB2_ERROR_COMMON_MEMBER_OVERLAP,
  411. "vb21_verify_fw_preamble() hash count");
  412. memcpy(buf, buf2, buf_size);
  413. sig = (struct vb21_signature *)(buf + pre->sig_offset);
  414. sig->c.total_size += 4;
  415. TEST_EQ(vb21_verify_fw_preamble(pre, buf_size, &pubk, &wb),
  416. VB2_ERROR_COMMON_TOTAL_SIZE,
  417. "vb21_verify_fw_preamble() sig inside");
  418. memcpy(buf, buf2, buf_size);
  419. sig = (struct vb21_signature *)(buf + pre->sig_offset);
  420. buf[pre->sig_offset + sig->sig_offset]++;
  421. TEST_EQ(vb21_verify_fw_preamble(pre, buf_size, &pubk, &wb),
  422. VB2_ERROR_VDATA_VERIFY_DIGEST,
  423. "vb21_verify_fw_preamble() sig corrupt");
  424. memcpy(buf, buf2, buf_size);
  425. pre->flags++;
  426. TEST_EQ(vb21_verify_fw_preamble(pre, buf_size, &pubk, &wb),
  427. VB2_ERROR_VDATA_VERIFY_DIGEST,
  428. "vb21_verify_fw_preamble() preamble corrupt");
  429. free(buf);
  430. free(buf2);
  431. }
  432. int main(int argc, char* argv[])
  433. {
  434. test_struct_packing();
  435. test_common_header_functions();
  436. test_sig_size();
  437. test_verify_hash();
  438. test_verify_keyblock();
  439. test_verify_fw_preamble();
  440. return gTestSuccess ? 0 : 255;
  441. }