vb21_misc_tests.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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 misc library, new-style structs
  6. */
  7. #include <stdio.h>
  8. #include "2sysincludes.h"
  9. #include "2api.h"
  10. #include "2common.h"
  11. #include "2misc.h"
  12. #include "2nvstorage.h"
  13. #include "2secdata.h"
  14. #include "vb21_common.h"
  15. #include "test_common.h"
  16. /* Common context for tests */
  17. static uint8_t workbuf[VB2_WORKBUF_RECOMMENDED_SIZE]
  18. __attribute__ ((aligned (VB2_WORKBUF_ALIGN)));
  19. static struct vb2_context ctx;
  20. static struct vb2_shared_data *sd;
  21. /* Mocked function data */
  22. static struct {
  23. struct vb2_gbb_header h;
  24. struct vb21_packed_key rootkey;
  25. char rootkey_data[32];
  26. } mock_gbb;
  27. static struct {
  28. /* Keyblock */
  29. struct {
  30. struct vb21_keyblock kb;
  31. struct vb21_packed_key data_key;
  32. char data_key_data[16];
  33. uint8_t kbdata[128];
  34. } k;
  35. /* Preamble follows keyblock */
  36. struct {
  37. struct vb21_fw_preamble pre;
  38. uint8_t predata[128];
  39. } p;
  40. } mock_vblock;
  41. static int mock_read_res_fail_on_call;
  42. static int mock_unpack_key_retval;
  43. static int mock_verify_keyblock_retval;
  44. static int mock_verify_preamble_retval;
  45. /* Type of test to reset for */
  46. enum reset_type {
  47. FOR_KEYBLOCK,
  48. FOR_PREAMBLE
  49. };
  50. static void reset_common_data(enum reset_type t)
  51. {
  52. struct vb21_keyblock *kb = &mock_vblock.k.kb;
  53. struct vb21_packed_key *dk = &mock_vblock.k.data_key;
  54. struct vb21_fw_preamble *pre = &mock_vblock.p.pre;
  55. memset(workbuf, 0xaa, sizeof(workbuf));
  56. memset(&ctx, 0, sizeof(ctx));
  57. ctx.workbuf = workbuf;
  58. ctx.workbuf_size = sizeof(workbuf);
  59. vb2_init_context(&ctx);
  60. sd = vb2_get_sd(&ctx);
  61. vb2_nv_init(&ctx);
  62. vb2_secdata_create(&ctx);
  63. vb2_secdata_init(&ctx);
  64. mock_read_res_fail_on_call = 0;
  65. mock_unpack_key_retval = VB2_SUCCESS;
  66. mock_verify_keyblock_retval = VB2_SUCCESS;
  67. mock_verify_preamble_retval = VB2_SUCCESS;
  68. /* Set up mock data for verifying keyblock */
  69. sd->fw_version_secdata = 0x20002;
  70. vb2_secdata_set(&ctx, VB2_SECDATA_VERSIONS, sd->fw_version_secdata);
  71. sd->gbb_rootkey_offset = vb2_offset_of(&mock_gbb, &mock_gbb.rootkey);
  72. sd->gbb_rootkey_size = sizeof(mock_gbb.rootkey_data);
  73. sd->last_fw_result = VB2_FW_RESULT_SUCCESS;
  74. mock_gbb.rootkey.sig_alg = VB2_SIG_RSA8192;
  75. mock_gbb.rootkey.key_offset =
  76. vb2_offset_of(&mock_gbb.rootkey,
  77. &mock_gbb.rootkey_data);
  78. mock_gbb.rootkey.key_size = sizeof(mock_gbb.rootkey_data);
  79. kb->c.total_size = sizeof(mock_vblock.k);
  80. kb->key_offset = vb2_offset_of(&mock_vblock.k.kb,
  81. &mock_vblock.k.data_key);
  82. dk->c.fixed_size = sizeof(mock_vblock.k.data_key);
  83. dk->sig_alg = VB2_SIG_RSA4096;
  84. dk->key_version = 2;
  85. dk->key_offset = dk->c.fixed_size;
  86. dk->key_size = sizeof(mock_vblock.k.data_key_data);
  87. dk->c.total_size = dk->key_offset + dk->key_size;
  88. strcpy(mock_vblock.k.data_key_data, "data key data!!");
  89. pre->c.total_size = sizeof(mock_vblock.p);
  90. pre->fw_version = 2;
  91. /* If verifying preamble, verify keyblock first to set up data key */
  92. if (t == FOR_PREAMBLE)
  93. vb21_load_fw_keyblock(&ctx);
  94. };
  95. /* Mocked functions */
  96. int vb2ex_read_resource(struct vb2_context *ctx,
  97. enum vb2_resource_index index,
  98. uint32_t offset,
  99. void *buf,
  100. uint32_t size)
  101. {
  102. uint8_t *rptr;
  103. uint32_t rsize;
  104. if (--mock_read_res_fail_on_call == 0)
  105. return VB2_ERROR_EX_READ_RESOURCE_INDEX;
  106. switch(index) {
  107. case VB2_RES_GBB:
  108. rptr = (uint8_t *)&mock_gbb;
  109. rsize = sizeof(mock_gbb);
  110. break;
  111. case VB2_RES_FW_VBLOCK:
  112. rptr = (uint8_t *)&mock_vblock;
  113. rsize = sizeof(mock_vblock);
  114. break;
  115. default:
  116. return VB2_ERROR_EX_READ_RESOURCE_INDEX;
  117. }
  118. if (offset > rsize || offset + size > rsize)
  119. return VB2_ERROR_EX_READ_RESOURCE_SIZE;
  120. memcpy(buf, rptr + offset, size);
  121. return VB2_SUCCESS;
  122. }
  123. int vb21_unpack_key(struct vb2_public_key *key,
  124. const uint8_t *buf,
  125. uint32_t size)
  126. {
  127. return mock_unpack_key_retval;
  128. }
  129. int vb21_verify_keyblock(struct vb21_keyblock *block,
  130. uint32_t size,
  131. const struct vb2_public_key *key,
  132. const struct vb2_workbuf *wb)
  133. {
  134. return mock_verify_keyblock_retval;
  135. }
  136. int vb21_verify_fw_preamble(struct vb21_fw_preamble *preamble,
  137. uint32_t size,
  138. const struct vb2_public_key *key,
  139. const struct vb2_workbuf *wb)
  140. {
  141. return mock_verify_preamble_retval;
  142. }
  143. /* Tests */
  144. static void load_keyblock_tests(void)
  145. {
  146. struct vb21_keyblock *kb = &mock_vblock.k.kb;
  147. struct vb21_packed_key *dk = &mock_vblock.k.data_key;
  148. struct vb21_packed_key *k;
  149. int wb_used_before;
  150. /* Test successful call */
  151. reset_common_data(FOR_KEYBLOCK);
  152. wb_used_before = ctx.workbuf_used;
  153. TEST_SUCC(vb21_load_fw_keyblock(&ctx), "keyblock verify");
  154. TEST_EQ(sd->fw_version, 0x20000, "keyblock version");
  155. TEST_EQ(sd->vblock_preamble_offset, sizeof(mock_vblock.k),
  156. "preamble offset");
  157. TEST_EQ(sd->workbuf_data_key_offset,
  158. (wb_used_before + (VB2_WORKBUF_ALIGN - 1)) &
  159. ~(VB2_WORKBUF_ALIGN - 1),
  160. "keyblock data key offset");
  161. TEST_EQ(ctx.workbuf_used,
  162. sd->workbuf_data_key_offset + sd->workbuf_data_key_size,
  163. "workbuf used");
  164. /* Make sure data key was properly saved */
  165. k = (struct vb21_packed_key *)(ctx.workbuf +
  166. sd->workbuf_data_key_offset);
  167. TEST_EQ(k->sig_alg, VB2_SIG_RSA4096, "data key algorithm");
  168. TEST_EQ(k->key_version, 2, "data key version");
  169. TEST_EQ(k->key_size, sizeof(mock_vblock.k.data_key_data),
  170. "data key size");
  171. TEST_EQ(memcmp(ctx.workbuf + sd->workbuf_data_key_offset +
  172. k->key_offset, mock_vblock.k.data_key_data,
  173. sizeof(mock_vblock.k.data_key_data)),
  174. 0, "data key data");
  175. TEST_EQ(ctx.workbuf_used,
  176. sd->workbuf_data_key_offset + sd->workbuf_data_key_size,
  177. "workbuf used after");
  178. /* Test failures */
  179. reset_common_data(FOR_KEYBLOCK);
  180. ctx.workbuf_used = ctx.workbuf_size - sd->gbb_rootkey_size + 8;
  181. TEST_EQ(vb21_load_fw_keyblock(&ctx),
  182. VB2_ERROR_FW_KEYBLOCK_WORKBUF_ROOT_KEY,
  183. "keyblock not enough workbuf for root key");
  184. reset_common_data(FOR_KEYBLOCK);
  185. sd->gbb_rootkey_size = sizeof(mock_gbb);
  186. TEST_EQ(vb21_load_fw_keyblock(&ctx),
  187. VB2_ERROR_EX_READ_RESOURCE_SIZE,
  188. "keyblock read root key");
  189. reset_common_data(FOR_KEYBLOCK);
  190. mock_unpack_key_retval = VB2_ERROR_UNPACK_KEY_SIG_ALGORITHM;
  191. TEST_EQ(vb21_load_fw_keyblock(&ctx),
  192. VB2_ERROR_UNPACK_KEY_SIG_ALGORITHM,
  193. "keyblock unpack root key");
  194. reset_common_data(FOR_KEYBLOCK);
  195. ctx.workbuf_used = ctx.workbuf_size - sd->gbb_rootkey_size - 8;
  196. TEST_EQ(vb21_load_fw_keyblock(&ctx),
  197. VB2_ERROR_READ_RESOURCE_OBJECT_BUF,
  198. "keyblock not enough workbuf for header");
  199. reset_common_data(FOR_KEYBLOCK);
  200. mock_read_res_fail_on_call = 2;
  201. TEST_EQ(vb21_load_fw_keyblock(&ctx),
  202. VB2_ERROR_EX_READ_RESOURCE_INDEX,
  203. "keyblock read keyblock header");
  204. reset_common_data(FOR_KEYBLOCK);
  205. ctx.workbuf_used = ctx.workbuf_size - sd->gbb_rootkey_size
  206. - sizeof(struct vb21_keyblock);
  207. TEST_EQ(vb21_load_fw_keyblock(&ctx),
  208. VB2_ERROR_READ_RESOURCE_OBJECT_BUF,
  209. "keyblock not enough workbuf for entire keyblock");
  210. reset_common_data(FOR_KEYBLOCK);
  211. kb->c.total_size = sizeof(mock_vblock) + 1;
  212. TEST_EQ(vb21_load_fw_keyblock(&ctx),
  213. VB2_ERROR_EX_READ_RESOURCE_SIZE,
  214. "keyblock read keyblock");
  215. reset_common_data(FOR_KEYBLOCK);
  216. mock_verify_keyblock_retval = VB2_ERROR_KEYBLOCK_MAGIC;
  217. TEST_EQ(vb21_load_fw_keyblock(&ctx),
  218. VB2_ERROR_KEYBLOCK_MAGIC,
  219. "keyblock verify keyblock");
  220. reset_common_data(FOR_KEYBLOCK);
  221. dk->key_version = 0x10000;
  222. TEST_EQ(vb21_load_fw_keyblock(&ctx),
  223. VB2_ERROR_FW_KEYBLOCK_VERSION_RANGE,
  224. "keyblock version range");
  225. reset_common_data(FOR_KEYBLOCK);
  226. dk->key_version = 1;
  227. TEST_EQ(vb21_load_fw_keyblock(&ctx),
  228. VB2_ERROR_FW_KEYBLOCK_VERSION_ROLLBACK,
  229. "keyblock rollback");
  230. reset_common_data(FOR_KEYBLOCK);
  231. dk->key_version = 1;
  232. sd->gbb_flags |= VB2_GBB_FLAG_DISABLE_FW_ROLLBACK_CHECK;
  233. TEST_SUCC(vb21_load_fw_keyblock(&ctx), "keyblock rollback + GBB flag");
  234. }
  235. static void load_preamble_tests(void)
  236. {
  237. struct vb21_fw_preamble *pre = &mock_vblock.p.pre;
  238. int data_key_offset_before;
  239. uint32_t v;
  240. /* Test successful call */
  241. reset_common_data(FOR_PREAMBLE);
  242. data_key_offset_before = sd->workbuf_data_key_offset;
  243. TEST_SUCC(vb21_load_fw_preamble(&ctx), "preamble good");
  244. TEST_EQ(sd->fw_version, 0x20002, "combined version");
  245. TEST_EQ(sd->workbuf_preamble_offset, data_key_offset_before,
  246. "preamble offset");
  247. TEST_EQ(sd->workbuf_preamble_size, pre->c.total_size, "preamble size");
  248. TEST_EQ(ctx.workbuf_used,
  249. sd->workbuf_preamble_offset + sd->workbuf_preamble_size,
  250. "workbuf used");
  251. TEST_EQ(sd->workbuf_data_key_offset, 0, "data key offset gone");
  252. TEST_EQ(sd->workbuf_data_key_size, 0, "data key size gone");
  253. /* Expected failures */
  254. reset_common_data(FOR_PREAMBLE);
  255. sd->workbuf_data_key_size = 0;
  256. TEST_EQ(vb21_load_fw_preamble(&ctx),
  257. VB2_ERROR_FW_PREAMBLE2_DATA_KEY,
  258. "preamble no data key");
  259. reset_common_data(FOR_PREAMBLE);
  260. mock_unpack_key_retval = VB2_ERROR_UNPACK_KEY_HASH_ALGORITHM;
  261. TEST_EQ(vb21_load_fw_preamble(&ctx),
  262. VB2_ERROR_UNPACK_KEY_HASH_ALGORITHM,
  263. "preamble unpack data key");
  264. reset_common_data(FOR_PREAMBLE);
  265. ctx.workbuf_used = ctx.workbuf_size
  266. - sizeof(struct vb21_fw_preamble) + 8;
  267. TEST_EQ(vb21_load_fw_preamble(&ctx),
  268. VB2_ERROR_READ_RESOURCE_OBJECT_BUF,
  269. "preamble not enough workbuf for header");
  270. reset_common_data(FOR_PREAMBLE);
  271. sd->vblock_preamble_offset = sizeof(mock_vblock);
  272. TEST_EQ(vb21_load_fw_preamble(&ctx),
  273. VB2_ERROR_EX_READ_RESOURCE_SIZE,
  274. "preamble read header");
  275. reset_common_data(FOR_PREAMBLE);
  276. ctx.workbuf_used = ctx.workbuf_size - sizeof(mock_vblock.p) + 8;
  277. TEST_EQ(vb21_load_fw_preamble(&ctx),
  278. VB2_ERROR_READ_RESOURCE_OBJECT_BUF,
  279. "preamble not enough workbuf");
  280. reset_common_data(FOR_PREAMBLE);
  281. pre->c.total_size = sizeof(mock_vblock);
  282. TEST_EQ(vb21_load_fw_preamble(&ctx),
  283. VB2_ERROR_EX_READ_RESOURCE_SIZE,
  284. "preamble read full");
  285. reset_common_data(FOR_PREAMBLE);
  286. mock_verify_preamble_retval = VB2_ERROR_PREAMBLE_SIG_INVALID;
  287. TEST_EQ(vb21_load_fw_preamble(&ctx),
  288. VB2_ERROR_PREAMBLE_SIG_INVALID,
  289. "preamble verify");
  290. reset_common_data(FOR_PREAMBLE);
  291. pre->fw_version = 0x10000;
  292. TEST_EQ(vb21_load_fw_preamble(&ctx),
  293. VB2_ERROR_FW_PREAMBLE_VERSION_RANGE,
  294. "preamble version range");
  295. reset_common_data(FOR_PREAMBLE);
  296. pre->fw_version = 1;
  297. TEST_EQ(vb21_load_fw_preamble(&ctx),
  298. VB2_ERROR_FW_PREAMBLE_VERSION_ROLLBACK,
  299. "preamble version rollback");
  300. reset_common_data(FOR_PREAMBLE);
  301. pre->fw_version = 1;
  302. sd->gbb_flags |= VB2_GBB_FLAG_DISABLE_FW_ROLLBACK_CHECK;
  303. TEST_SUCC(vb21_load_fw_preamble(&ctx), "version rollback with GBB flag");
  304. reset_common_data(FOR_PREAMBLE);
  305. pre->fw_version = 3;
  306. TEST_SUCC(vb21_load_fw_preamble(&ctx),
  307. "preamble version roll forward");
  308. vb2_secdata_get(&ctx, VB2_SECDATA_VERSIONS, &v);
  309. TEST_EQ(v, 0x20003, "roll forward");
  310. /* Newer version without result success doesn't roll forward */
  311. reset_common_data(FOR_PREAMBLE);
  312. pre->fw_version = 3;
  313. sd->last_fw_result = VB2_FW_RESULT_UNKNOWN;
  314. TEST_SUCC(vb21_load_fw_preamble(&ctx),
  315. "preamble version no roll forward 1");
  316. vb2_secdata_get(&ctx, VB2_SECDATA_VERSIONS, &v);
  317. TEST_EQ(v, 0x20002, "no roll forward");
  318. /* Newer version with success but for other slot doesn't roll forward */
  319. reset_common_data(FOR_PREAMBLE);
  320. pre->fw_version = 3;
  321. sd->last_fw_slot = 1;
  322. TEST_SUCC(vb21_load_fw_preamble(&ctx),
  323. "preamble version no roll forward 2");
  324. vb2_secdata_get(&ctx, VB2_SECDATA_VERSIONS, &v);
  325. TEST_EQ(v, 0x20002, "no roll forward");
  326. }
  327. int main(int argc, char* argv[])
  328. {
  329. load_keyblock_tests();
  330. load_preamble_tests();
  331. return gTestSuccess ? 0 : 255;
  332. }