vb20_common3_tests.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  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 <stdio.h>
  8. #include "2sysincludes.h"
  9. #include "2rsa.h"
  10. #include "file_keys.h"
  11. #include "host_common.h"
  12. #include "host_key.h"
  13. #include "host_keyblock.h"
  14. #include "host_signature.h"
  15. #include "vb2_common.h"
  16. #include "vboot_common.h"
  17. #include "test_common.h"
  18. static void resign_keyblock(struct vb2_keyblock *h,
  19. const struct vb2_private_key *key)
  20. {
  21. struct vb2_signature *sig =
  22. vb2_calculate_signature((const uint8_t *)h,
  23. h->keyblock_signature.data_size, key);
  24. vb2_copy_signature(&h->keyblock_signature, sig);
  25. free(sig);
  26. }
  27. static void test_check_keyblock(const struct vb2_public_key *public_key,
  28. const struct vb2_private_key *private_key,
  29. const struct vb2_packed_key *data_key)
  30. {
  31. struct vb2_keyblock *hdr;
  32. struct vb2_keyblock *h;
  33. struct vb2_signature *sig;
  34. uint32_t hsize;
  35. hdr = vb2_create_keyblock(data_key, private_key, 0x1234);
  36. TEST_NEQ((size_t)hdr, 0, "vb2_verify_keyblock() prerequisites");
  37. if (!hdr)
  38. return;
  39. hsize = hdr->keyblock_size;
  40. h = (struct vb2_keyblock *)malloc(hsize + 2048);
  41. sig = &h->keyblock_signature;
  42. memcpy(h, hdr, hsize);
  43. TEST_SUCC(vb2_check_keyblock(h, hsize, sig),
  44. "vb2_check_keyblock() ok");
  45. memcpy(h, hdr, hsize);
  46. TEST_EQ(vb2_check_keyblock(h, hsize - 1, sig),
  47. VB2_ERROR_KEYBLOCK_SIZE, "vb2_check_keyblock() size--");
  48. /* Buffer is allowed to be bigger than keyblock */
  49. memcpy(h, hdr, hsize);
  50. TEST_SUCC(vb2_check_keyblock(h, hsize + 1, sig),
  51. "vb2_check_keyblock() size++");
  52. memcpy(h, hdr, hsize);
  53. h->magic[0] &= 0x12;
  54. TEST_EQ(vb2_check_keyblock(h, hsize, sig),
  55. VB2_ERROR_KEYBLOCK_MAGIC, "vb2_check_keyblock() magic");
  56. /* Care about major version but not minor */
  57. memcpy(h, hdr, hsize);
  58. h->header_version_major++;
  59. resign_keyblock(h, private_key);
  60. TEST_EQ(vb2_check_keyblock(h, hsize, sig),
  61. VB2_ERROR_KEYBLOCK_HEADER_VERSION,
  62. "vb2_check_keyblock() major++");
  63. memcpy(h, hdr, hsize);
  64. h->header_version_major--;
  65. resign_keyblock(h, private_key);
  66. TEST_EQ(vb2_check_keyblock(h, hsize, sig),
  67. VB2_ERROR_KEYBLOCK_HEADER_VERSION,
  68. "vb2_check_keyblock() major--");
  69. memcpy(h, hdr, hsize);
  70. h->header_version_minor++;
  71. resign_keyblock(h, private_key);
  72. TEST_SUCC(vb2_check_keyblock(h, hsize, sig),
  73. "vb2_check_keyblock() minor++");
  74. memcpy(h, hdr, hsize);
  75. h->header_version_minor--;
  76. resign_keyblock(h, private_key);
  77. TEST_SUCC(vb2_check_keyblock(h, hsize, sig),
  78. "vb2_check_keyblock() minor--");
  79. /* Check signature */
  80. memcpy(h, hdr, hsize);
  81. h->keyblock_signature.sig_offset = hsize;
  82. resign_keyblock(h, private_key);
  83. TEST_EQ(vb2_check_keyblock(h, hsize, sig),
  84. VB2_ERROR_KEYBLOCK_SIG_OUTSIDE,
  85. "vb2_check_keyblock() sig off end");
  86. memcpy(h, hdr, hsize);
  87. h->keyblock_signature.data_size = h->keyblock_size + 1;
  88. TEST_EQ(vb2_check_keyblock(h, hsize, sig),
  89. VB2_ERROR_KEYBLOCK_SIGNED_TOO_MUCH,
  90. "vb2_check_keyblock() sig data past end of block");
  91. /* Check that we signed header and data key */
  92. memcpy(h, hdr, hsize);
  93. h->keyblock_signature.data_size = 4;
  94. h->data_key.key_offset = 0;
  95. h->data_key.key_size = 0;
  96. resign_keyblock(h, private_key);
  97. TEST_EQ(vb2_check_keyblock(h, hsize, sig),
  98. VB2_ERROR_KEYBLOCK_SIGNED_TOO_LITTLE,
  99. "vb2_check_keyblock() didn't sign header");
  100. memcpy(h, hdr, hsize);
  101. h->data_key.key_offset = hsize;
  102. resign_keyblock(h, private_key);
  103. TEST_EQ(vb2_check_keyblock(h, hsize, sig),
  104. VB2_ERROR_KEYBLOCK_DATA_KEY_OUTSIDE,
  105. "vb2_check_keyblock() data key off end");
  106. /* Corner cases for error checking */
  107. TEST_EQ(vb2_check_keyblock(NULL, 4, sig),
  108. VB2_ERROR_KEYBLOCK_TOO_SMALL_FOR_HEADER,
  109. "vb2_check_keyblock size too small");
  110. /*
  111. * TODO: verify parser can support a bigger header (i.e., one where
  112. * data_key.key_offset is bigger than expected).
  113. */
  114. free(h);
  115. free(hdr);
  116. }
  117. static void test_verify_keyblock(const struct vb2_public_key *public_key,
  118. const struct vb2_private_key *private_key,
  119. const struct vb2_packed_key *data_key)
  120. {
  121. uint8_t workbuf[VB2_KEY_BLOCK_VERIFY_WORKBUF_BYTES]
  122. __attribute__ ((aligned (VB2_WORKBUF_ALIGN)));
  123. struct vb2_workbuf wb;
  124. struct vb2_keyblock *hdr;
  125. struct vb2_keyblock *h;
  126. uint32_t hsize;
  127. vb2_workbuf_init(&wb, workbuf, sizeof(workbuf));
  128. hdr = vb2_create_keyblock(data_key, private_key, 0x1234);
  129. TEST_NEQ((size_t)hdr, 0, "vb2_verify_keyblock() prerequisites");
  130. if (!hdr)
  131. return;
  132. hsize = hdr->keyblock_size;
  133. h = (struct vb2_keyblock *)malloc(hsize + 2048);
  134. memcpy(h, hdr, hsize);
  135. TEST_SUCC(vb2_verify_keyblock(h, hsize, public_key, &wb),
  136. "vb2_verify_keyblock() ok using key");
  137. /* Failures in keyblock check also cause verify to fail */
  138. memcpy(h, hdr, hsize);
  139. TEST_EQ(vb2_verify_keyblock(h, hsize - 1, public_key, &wb),
  140. VB2_ERROR_KEYBLOCK_SIZE, "vb2_verify_keyblock() check");
  141. /* Check signature */
  142. memcpy(h, hdr, hsize);
  143. h->keyblock_signature.sig_size--;
  144. resign_keyblock(h, private_key);
  145. TEST_EQ(vb2_verify_keyblock(h, hsize, public_key, &wb),
  146. VB2_ERROR_KEYBLOCK_SIG_INVALID,
  147. "vb2_verify_keyblock() sig too small");
  148. memcpy(h, hdr, hsize);
  149. ((uint8_t *)vb2_packed_key_data(&h->data_key))[0] ^= 0x34;
  150. TEST_EQ(vb2_verify_keyblock(h, hsize, public_key, &wb),
  151. VB2_ERROR_KEYBLOCK_SIG_INVALID,
  152. "vb2_verify_keyblock() sig mismatch");
  153. /*
  154. * TODO: verify parser can support a bigger header (i.e., one where
  155. * data_key.key_offset is bigger than expected).
  156. */
  157. free(h);
  158. free(hdr);
  159. }
  160. static void resign_fw_preamble(struct vb2_fw_preamble *h,
  161. struct vb2_private_key *key)
  162. {
  163. struct vb2_signature *sig = vb2_calculate_signature(
  164. (const uint8_t *)h, h->preamble_signature.data_size, key);
  165. vb2_copy_signature(&h->preamble_signature, sig);
  166. free(sig);
  167. }
  168. static void test_verify_fw_preamble(struct vb2_packed_key *public_key,
  169. struct vb2_private_key *private_key,
  170. struct vb2_packed_key *kernel_subkey)
  171. {
  172. struct vb2_fw_preamble *hdr;
  173. struct vb2_fw_preamble *h;
  174. struct vb2_public_key rsa;
  175. uint8_t workbuf[VB2_VERIFY_FIRMWARE_PREAMBLE_WORKBUF_BYTES]
  176. __attribute__ ((aligned (VB2_WORKBUF_ALIGN)));
  177. struct vb2_workbuf wb;
  178. uint32_t hsize;
  179. vb2_workbuf_init(&wb, workbuf, sizeof(workbuf));
  180. /* Create a dummy signature */
  181. struct vb2_signature *body_sig = vb2_alloc_signature(56, 78);
  182. TEST_SUCC(vb2_unpack_key(&rsa, public_key),
  183. "vb2_verify_fw_preamble() prereq key");
  184. hdr = vb2_create_fw_preamble(0x1234, kernel_subkey, body_sig,
  185. private_key, 0x5678);
  186. TEST_PTR_NEQ(hdr, NULL,
  187. "vb2_verify_fw_preamble() prereq test preamble");
  188. if (!hdr) {
  189. free(body_sig);
  190. return;
  191. }
  192. hsize = (uint32_t) hdr->preamble_size;
  193. h = (struct vb2_fw_preamble *)malloc(hsize + 16384);
  194. memcpy(h, hdr, hsize);
  195. TEST_SUCC(vb2_verify_fw_preamble(h, hsize, &rsa, &wb),
  196. "vb2_verify_fw_preamble() ok using key");
  197. memcpy(h, hdr, hsize);
  198. TEST_EQ(vb2_verify_fw_preamble(h, 4, &rsa, &wb),
  199. VB2_ERROR_PREAMBLE_TOO_SMALL_FOR_HEADER,
  200. "vb2_verify_fw_preamble() size tiny");
  201. memcpy(h, hdr, hsize);
  202. TEST_EQ(vb2_verify_fw_preamble(h, hsize - 1, &rsa, &wb),
  203. VB2_ERROR_PREAMBLE_SIZE,
  204. "vb2_verify_fw_preamble() size--");
  205. /* Buffer is allowed to be bigger than preamble */
  206. memcpy(h, hdr, hsize);
  207. TEST_SUCC(vb2_verify_fw_preamble(h, hsize + 1, &rsa, &wb),
  208. "vb2_verify_fw_preamble() size++");
  209. /* Care about major version but not minor */
  210. memcpy(h, hdr, hsize);
  211. h->header_version_major++;
  212. resign_fw_preamble(h, private_key);
  213. TEST_EQ(vb2_verify_fw_preamble(h, hsize, &rsa, &wb),
  214. VB2_ERROR_PREAMBLE_HEADER_VERSION
  215. , "vb2_verify_fw_preamble() major++");
  216. memcpy(h, hdr, hsize);
  217. h->header_version_major--;
  218. resign_fw_preamble(h, private_key);
  219. TEST_EQ(vb2_verify_fw_preamble(h, hsize, &rsa, &wb),
  220. VB2_ERROR_PREAMBLE_HEADER_VERSION,
  221. "vb2_verify_fw_preamble() major--");
  222. memcpy(h, hdr, hsize);
  223. h->header_version_minor++;
  224. resign_fw_preamble(h, private_key);
  225. TEST_SUCC(vb2_verify_fw_preamble(h, hsize, &rsa, &wb),
  226. "vb2_verify_fw_preamble() minor++");
  227. memcpy(h, hdr, hsize);
  228. h->header_version_minor--;
  229. resign_fw_preamble(h, private_key);
  230. TEST_EQ(vb2_verify_fw_preamble(h, hsize, &rsa, &wb),
  231. VB2_ERROR_PREAMBLE_HEADER_OLD,
  232. "vb2_verify_fw_preamble() 2.0 not supported");
  233. /* Check signature */
  234. memcpy(h, hdr, hsize);
  235. h->preamble_signature.sig_offset = hsize;
  236. resign_fw_preamble(h, private_key);
  237. TEST_EQ(vb2_verify_fw_preamble(h, hsize, &rsa, &wb),
  238. VB2_ERROR_PREAMBLE_SIG_OUTSIDE,
  239. "vb2_verify_fw_preamble() sig off end");
  240. memcpy(h, hdr, hsize);
  241. h->preamble_signature.sig_size--;
  242. resign_fw_preamble(h, private_key);
  243. TEST_EQ(vb2_verify_fw_preamble(h, hsize, &rsa, &wb),
  244. VB2_ERROR_PREAMBLE_SIG_INVALID,
  245. "vb2_verify_fw_preamble() sig too small");
  246. memcpy(h, hdr, hsize);
  247. ((uint8_t *)vb2_packed_key_data(&h->kernel_subkey))[0] ^= 0x34;
  248. TEST_EQ(vb2_verify_fw_preamble(h, hsize, &rsa, &wb),
  249. VB2_ERROR_PREAMBLE_SIG_INVALID,
  250. "vb2_verify_fw_preamble() sig mismatch");
  251. /* Check that we signed header, kernel subkey, and body sig */
  252. memcpy(h, hdr, hsize);
  253. h->preamble_signature.data_size = 4;
  254. h->kernel_subkey.key_offset = 0;
  255. h->kernel_subkey.key_size = 0;
  256. h->body_signature.sig_offset = 0;
  257. h->body_signature.sig_size = 0;
  258. resign_fw_preamble(h, private_key);
  259. TEST_EQ(vb2_verify_fw_preamble(h, hsize, &rsa, &wb),
  260. VB2_ERROR_PREAMBLE_SIGNED_TOO_LITTLE,
  261. "vb2_verify_fw_preamble() didn't sign header");
  262. memcpy(h, hdr, hsize);
  263. h->kernel_subkey.key_offset = hsize;
  264. resign_fw_preamble(h, private_key);
  265. TEST_EQ(vb2_verify_fw_preamble(h, hsize, &rsa, &wb),
  266. VB2_ERROR_PREAMBLE_KERNEL_SUBKEY_OUTSIDE,
  267. "vb2_verify_fw_preamble() kernel subkey off end");
  268. memcpy(h, hdr, hsize);
  269. h->body_signature.sig_offset = hsize;
  270. resign_fw_preamble(h, private_key);
  271. TEST_EQ(vb2_verify_fw_preamble(h, hsize, &rsa, &wb),
  272. VB2_ERROR_PREAMBLE_BODY_SIG_OUTSIDE,
  273. "vb2_verify_fw_preamble() body sig off end");
  274. /* TODO: verify with extra padding at end of header. */
  275. free(h);
  276. free(hdr);
  277. free(body_sig);
  278. }
  279. static void resign_kernel_preamble(struct vb2_kernel_preamble *h,
  280. const struct vb2_private_key *key)
  281. {
  282. struct vb2_signature *sig = vb2_calculate_signature(
  283. (const uint8_t *)h, h->preamble_signature.data_size, key);
  284. vb2_copy_signature(&h->preamble_signature, sig);
  285. free(sig);
  286. }
  287. static void test_verify_kernel_preamble(
  288. const struct vb2_packed_key *public_key,
  289. const struct vb2_private_key *private_key)
  290. {
  291. struct vb2_public_key rsa;
  292. // TODO: how many workbuf bytes?
  293. uint8_t workbuf[VB2_VERIFY_FIRMWARE_PREAMBLE_WORKBUF_BYTES]
  294. __attribute__ ((aligned (VB2_WORKBUF_ALIGN)));
  295. struct vb2_workbuf wb;
  296. uint32_t hsize;
  297. vb2_workbuf_init(&wb, workbuf, sizeof(workbuf));
  298. /* Create a dummy signature */
  299. struct vb2_signature *body_sig = vb2_alloc_signature(56, 0x214000);
  300. TEST_SUCC(vb2_unpack_key(&rsa, public_key),
  301. "vb2_verify_kernel_preamble() prereq key");
  302. struct vb2_kernel_preamble *hdr =
  303. vb2_create_kernel_preamble(0x1234, 0x100000, 0x300000, 0x4000,
  304. body_sig, 0x304000, 0x10000, 0, 0,
  305. private_key);
  306. TEST_PTR_NEQ(hdr, NULL,
  307. "vb2_verify_kernel_preamble() prereq test preamble");
  308. if (!hdr) {
  309. free(body_sig);
  310. return;
  311. }
  312. hsize = (uint32_t) hdr->preamble_size;
  313. struct vb2_kernel_preamble *h =
  314. (struct vb2_kernel_preamble *)malloc(hsize + 16384);
  315. memcpy(h, hdr, hsize);
  316. TEST_SUCC(vb2_verify_kernel_preamble(h, hsize, &rsa, &wb),
  317. "vb2_verify_kernel_preamble() ok using key");
  318. memcpy(h, hdr, hsize);
  319. TEST_EQ(vb2_verify_kernel_preamble(h, 4, &rsa, &wb),
  320. VB2_ERROR_PREAMBLE_TOO_SMALL_FOR_HEADER,
  321. "vb2_verify_kernel_preamble() size tiny");
  322. memcpy(h, hdr, hsize);
  323. TEST_EQ(vb2_verify_kernel_preamble(h, hsize - 1, &rsa, &wb),
  324. VB2_ERROR_PREAMBLE_SIZE,
  325. "vb2_verify_kernel_preamble() size--");
  326. /* Buffer is allowed to be bigger than preamble */
  327. memcpy(h, hdr, hsize);
  328. TEST_SUCC(vb2_verify_kernel_preamble(h, hsize + 1, &rsa, &wb),
  329. "vb2_verify_kernel_preamble() size++");
  330. /* Care about major version but not minor */
  331. memcpy(h, hdr, hsize);
  332. h->header_version_major++;
  333. resign_kernel_preamble(h, private_key);
  334. TEST_EQ(vb2_verify_kernel_preamble(h, hsize, &rsa, &wb),
  335. VB2_ERROR_PREAMBLE_HEADER_VERSION
  336. , "vb2_verify_kernel_preamble() major++");
  337. memcpy(h, hdr, hsize);
  338. h->header_version_major--;
  339. resign_kernel_preamble(h, private_key);
  340. TEST_EQ(vb2_verify_kernel_preamble(h, hsize, &rsa, &wb),
  341. VB2_ERROR_PREAMBLE_HEADER_VERSION,
  342. "vb2_verify_kernel_preamble() major--");
  343. memcpy(h, hdr, hsize);
  344. h->header_version_minor++;
  345. resign_kernel_preamble(h, private_key);
  346. TEST_SUCC(vb2_verify_kernel_preamble(h, hsize, &rsa, &wb),
  347. "vb2_verify_kernel_preamble() minor++");
  348. /* Check signature */
  349. memcpy(h, hdr, hsize);
  350. h->preamble_signature.sig_offset = hsize;
  351. resign_kernel_preamble(h, private_key);
  352. TEST_EQ(vb2_verify_kernel_preamble(h, hsize, &rsa, &wb),
  353. VB2_ERROR_PREAMBLE_SIG_OUTSIDE,
  354. "vb2_verify_kernel_preamble() sig off end");
  355. memcpy(h, hdr, hsize);
  356. h->preamble_signature.sig_size--;
  357. resign_kernel_preamble(h, private_key);
  358. TEST_EQ(vb2_verify_kernel_preamble(h, hsize, &rsa, &wb),
  359. VB2_ERROR_PREAMBLE_SIG_INVALID,
  360. "vb2_verify_kernel_preamble() sig too small");
  361. memcpy(h, hdr, hsize);
  362. h->flags++;
  363. TEST_EQ(vb2_verify_kernel_preamble(h, hsize, &rsa, &wb),
  364. VB2_ERROR_PREAMBLE_SIG_INVALID,
  365. "vb2_verify_kernel_preamble() sig mismatch");
  366. /* Check that we signed header and body sig */
  367. memcpy(h, hdr, hsize);
  368. h->preamble_signature.data_size = 4;
  369. h->body_signature.sig_offset = 0;
  370. h->body_signature.sig_size = 0;
  371. resign_kernel_preamble(h, private_key);
  372. TEST_EQ(vb2_verify_kernel_preamble(h, hsize, &rsa, &wb),
  373. VB2_ERROR_PREAMBLE_SIGNED_TOO_LITTLE,
  374. "vb2_verify_kernel_preamble() didn't sign header");
  375. memcpy(h, hdr, hsize);
  376. h->body_signature.sig_offset = hsize;
  377. resign_kernel_preamble(h, private_key);
  378. TEST_EQ(vb2_verify_kernel_preamble(h, hsize, &rsa, &wb),
  379. VB2_ERROR_PREAMBLE_BODY_SIG_OUTSIDE,
  380. "vb2_verify_kernel_preamble() body sig off end");
  381. /* Check bootloader inside signed body */
  382. memcpy(h, hdr, hsize);
  383. h->bootloader_address = h->body_load_address - 1;
  384. resign_kernel_preamble(h, private_key);
  385. TEST_EQ(vb2_verify_kernel_preamble(h, hsize, &rsa, &wb),
  386. VB2_ERROR_PREAMBLE_BOOTLOADER_OUTSIDE,
  387. "vb2_verify_kernel_preamble() bootloader before body");
  388. memcpy(h, hdr, hsize);
  389. h->bootloader_address = h->body_load_address +
  390. h->body_signature.data_size + 1;
  391. resign_kernel_preamble(h, private_key);
  392. TEST_EQ(vb2_verify_kernel_preamble(h, hsize, &rsa, &wb),
  393. VB2_ERROR_PREAMBLE_BOOTLOADER_OUTSIDE,
  394. "vb2_verify_kernel_preamble() bootloader off end of body");
  395. memcpy(h, hdr, hsize);
  396. h->bootloader_address = h->body_load_address +
  397. h->body_signature.data_size + 1;
  398. h->bootloader_size = 0;
  399. resign_kernel_preamble(h, private_key);
  400. TEST_SUCC(vb2_verify_kernel_preamble(h, hsize, &rsa, &wb),
  401. "vb2_verify_kernel_preamble() no bootloader");
  402. /* Check vmlinuz inside signed body */
  403. memcpy(h, hdr, hsize);
  404. h->vmlinuz_header_address = h->body_load_address - 1;
  405. resign_kernel_preamble(h, private_key);
  406. TEST_EQ(vb2_verify_kernel_preamble(h, hsize, &rsa, &wb),
  407. VB2_ERROR_PREAMBLE_VMLINUZ_HEADER_OUTSIDE,
  408. "vb2_verify_kernel_preamble() vmlinuz_header before body");
  409. memcpy(h, hdr, hsize);
  410. h->vmlinuz_header_address = h->body_load_address +
  411. h->body_signature.data_size + 1;
  412. resign_kernel_preamble(h, private_key);
  413. TEST_EQ(vb2_verify_kernel_preamble(h, hsize, &rsa, &wb),
  414. VB2_ERROR_PREAMBLE_VMLINUZ_HEADER_OUTSIDE,
  415. "vb2_verify_kernel_preamble() vmlinuz_header off end of body");
  416. memcpy(h, hdr, hsize);
  417. h->vmlinuz_header_address = h->body_load_address +
  418. h->body_signature.data_size + 1;
  419. h->vmlinuz_header_size = 0;
  420. resign_kernel_preamble(h, private_key);
  421. TEST_SUCC(vb2_verify_kernel_preamble(h, hsize, &rsa, &wb),
  422. "vb2_verify_kernel_preamble() no vmlinuz_header");
  423. /* TODO: verify with extra padding at end of header. */
  424. free(h);
  425. free(hdr);
  426. free(body_sig);
  427. }
  428. int test_permutation(int signing_key_algorithm, int data_key_algorithm,
  429. const char *keys_dir)
  430. {
  431. char filename[1024];
  432. int signing_rsa_len = 8 * vb2_rsa_sig_size(
  433. vb2_crypto_to_signature(signing_key_algorithm));
  434. int data_rsa_len = 8 * vb2_rsa_sig_size(
  435. vb2_crypto_to_signature(data_key_algorithm));
  436. int retval = 1;
  437. struct vb2_private_key *signing_private_key = NULL;
  438. struct vb2_packed_key *signing_public_key = NULL;
  439. struct vb2_packed_key *data_public_key = NULL;
  440. printf("***Testing signing algorithm: %s\n",
  441. vb2_get_crypto_algorithm_name(signing_key_algorithm));
  442. printf("***With data key algorithm: %s\n",
  443. vb2_get_crypto_algorithm_name(data_key_algorithm));
  444. snprintf(filename, sizeof(filename),
  445. "%s/key_rsa%d.pem", keys_dir, signing_rsa_len);
  446. signing_private_key =
  447. vb2_read_private_key_pem(filename, signing_key_algorithm);
  448. if (!signing_private_key) {
  449. fprintf(stderr, "Error reading signing_private_key: %s\n",
  450. filename);
  451. goto cleanup_permutation;
  452. }
  453. snprintf(filename, sizeof(filename),
  454. "%s/key_rsa%d.keyb", keys_dir, signing_rsa_len);
  455. signing_public_key =
  456. vb2_read_packed_keyb(filename, signing_key_algorithm, 1);
  457. if (!signing_public_key) {
  458. fprintf(stderr, "Error reading signing_public_key: %s\n",
  459. filename);
  460. goto cleanup_permutation;
  461. }
  462. snprintf(filename, sizeof(filename),
  463. "%s/key_rsa%d.keyb", keys_dir, data_rsa_len);
  464. data_public_key =
  465. vb2_read_packed_keyb(filename, data_key_algorithm, 1);
  466. if (!data_public_key) {
  467. fprintf(stderr, "Error reading data_public_key: %s\n",
  468. filename);
  469. goto cleanup_permutation;
  470. }
  471. /* Unpack public key */
  472. struct vb2_public_key signing_public_key2;
  473. if (VB2_SUCCESS !=
  474. vb2_unpack_key_buffer(&signing_public_key2,
  475. (uint8_t *)signing_public_key,
  476. signing_public_key->key_offset +
  477. signing_public_key->key_size)) {
  478. fprintf(stderr, "Error unpacking signing_public_key: %s\n",
  479. filename);
  480. goto cleanup_permutation;
  481. }
  482. test_check_keyblock(&signing_public_key2, signing_private_key,
  483. data_public_key);
  484. test_verify_keyblock(&signing_public_key2, signing_private_key,
  485. data_public_key);
  486. test_verify_fw_preamble(signing_public_key, signing_private_key,
  487. data_public_key);
  488. test_verify_kernel_preamble(signing_public_key, signing_private_key);
  489. retval = 0;
  490. cleanup_permutation:
  491. if (signing_public_key)
  492. free(signing_public_key);
  493. if (signing_private_key)
  494. free(signing_private_key);
  495. if (data_public_key)
  496. free(data_public_key);
  497. return retval;
  498. }
  499. struct test_perm
  500. {
  501. int signing_algorithm;
  502. int data_key_algorithm;
  503. };
  504. /* Permutations of signing and data key algorithms in active use */
  505. const struct test_perm test_perms[] = {
  506. {VB2_ALG_RSA4096_SHA256, VB2_ALG_RSA2048_SHA256},
  507. {VB2_ALG_RSA8192_SHA512, VB2_ALG_RSA2048_SHA256},
  508. {VB2_ALG_RSA8192_SHA512, VB2_ALG_RSA4096_SHA256},
  509. };
  510. int main(int argc, char *argv[])
  511. {
  512. if (argc == 2) {
  513. /* Test only the algorithms we use */
  514. int i;
  515. for (i = 0; i < ARRAY_SIZE(test_perms); i++) {
  516. if (test_permutation(test_perms[i].signing_algorithm,
  517. test_perms[i].data_key_algorithm,
  518. argv[1]))
  519. return 1;
  520. }
  521. } else if (argc == 3 && !strcasecmp(argv[2], "--all")) {
  522. /* Test all the algorithms */
  523. int sign_alg, data_alg;
  524. for (sign_alg = 0; sign_alg < VB2_ALG_COUNT; sign_alg++) {
  525. for (data_alg = 0; data_alg < VB2_ALG_COUNT;
  526. data_alg++) {
  527. if (test_permutation(sign_alg, data_alg,
  528. argv[1]))
  529. return 1;
  530. }
  531. }
  532. } else {
  533. fprintf(stderr, "Usage: %s <keys_dir> [--all]", argv[0]);
  534. return -1;
  535. }
  536. return gTestSuccess ? 0 : 255;
  537. }