vb20_common_tests.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 "vb2_common.h"
  9. #include "vboot_struct.h" /* For old struct sizes */
  10. #include "test_common.h"
  11. /*
  12. * Test struct packing for vboot_struct.h structs which are passed between
  13. * firmware and OS, or passed between different phases of firmware.
  14. */
  15. static void test_struct_packing(void)
  16. {
  17. /* Test vboot2 versions of vboot1 structs */
  18. TEST_EQ(EXPECTED_VB2_PACKED_KEY_SIZE,
  19. sizeof(struct vb2_packed_key),
  20. "sizeof(vb2_packed_key)");
  21. TEST_EQ(EXPECTED_VB2_SIGNATURE_SIZE,
  22. sizeof(struct vb2_signature),
  23. "sizeof(vb2_signature)");
  24. TEST_EQ(EXPECTED_VB2_KEYBLOCK_SIZE,
  25. sizeof(struct vb2_keyblock),
  26. "sizeof(vb2_keyblock)");
  27. TEST_EQ(EXPECTED_VB2_FW_PREAMBLE_SIZE,
  28. sizeof(struct vb2_fw_preamble),
  29. "sizeof(vb2_fw_preamble)");
  30. TEST_EQ(EXPECTED_VB2_GBB_HEADER_SIZE,
  31. sizeof(struct vb2_gbb_header),
  32. "sizeof(vb2_gbb_header)");
  33. /* And make sure they're the same as their vboot1 equivalents */
  34. TEST_EQ(EXPECTED_VB2_PACKED_KEY_SIZE,
  35. EXPECTED_VBPUBLICKEY_SIZE,
  36. "vboot1->2 packed key sizes same");
  37. TEST_EQ(EXPECTED_VB2_SIGNATURE_SIZE,
  38. EXPECTED_VBSIGNATURE_SIZE,
  39. "vboot1->2 signature sizes same");
  40. TEST_EQ(EXPECTED_VB2_KEYBLOCK_SIZE,
  41. EXPECTED_VBKEYBLOCKHEADER_SIZE,
  42. "vboot1->2 keyblock sizes same");
  43. }
  44. /**
  45. * Helper functions not dependent on specific key sizes
  46. */
  47. static void test_helper_functions(void)
  48. {
  49. {
  50. uint8_t *p = (uint8_t *)test_helper_functions;
  51. TEST_EQ((int)vb2_offset_of(p, p), 0, "vb2_offset_of() equal");
  52. TEST_EQ((int)vb2_offset_of(p, p+10), 10,
  53. "vb2_offset_of() positive");
  54. }
  55. {
  56. struct vb2_packed_key k = {.key_offset = sizeof(k)};
  57. TEST_EQ((int)vb2_offset_of(&k, vb2_packed_key_data(&k)),
  58. sizeof(k), "vb2_packed_key_data() adjacent");
  59. }
  60. {
  61. struct vb2_packed_key k = {.key_offset = 123};
  62. TEST_EQ((int)vb2_offset_of(&k, vb2_packed_key_data(&k)), 123,
  63. "vb2_packed_key_data() spaced");
  64. }
  65. {
  66. struct vb2_signature s = {.sig_offset = sizeof(s)};
  67. TEST_EQ((int)vb2_offset_of(&s, vb2_signature_data(&s)),
  68. sizeof(s), "vb2_signature_data() adjacent");
  69. }
  70. {
  71. struct vb2_signature s = {.sig_offset = 123};
  72. TEST_EQ((int)vb2_offset_of(&s, vb2_signature_data(&s)), 123,
  73. "vb2_signature_data() spaced");
  74. }
  75. {
  76. uint8_t *p = (uint8_t *)test_helper_functions;
  77. TEST_SUCC(vb2_verify_member_inside(p, 20, p, 6, 11, 3),
  78. "MemberInside ok 1");
  79. TEST_SUCC(vb2_verify_member_inside(p, 20, p+4, 4, 8, 4),
  80. "MemberInside ok 2");
  81. TEST_EQ(vb2_verify_member_inside(p, 20, p-4, 4, 8, 4),
  82. VB2_ERROR_INSIDE_MEMBER_OUTSIDE,
  83. "MemberInside member before parent");
  84. TEST_EQ(vb2_verify_member_inside(p, 20, p+20, 4, 8, 4),
  85. VB2_ERROR_INSIDE_MEMBER_OUTSIDE,
  86. "MemberInside member after parent");
  87. TEST_EQ(vb2_verify_member_inside(p, 20, p, 21, 0, 0),
  88. VB2_ERROR_INSIDE_MEMBER_OUTSIDE,
  89. "MemberInside member too big");
  90. TEST_EQ(vb2_verify_member_inside(p, 20, p, 4, 21, 0),
  91. VB2_ERROR_INSIDE_DATA_OUTSIDE,
  92. "MemberInside data after parent");
  93. TEST_EQ(vb2_verify_member_inside(p, 20, p, 4, SIZE_MAX, 0),
  94. VB2_ERROR_INSIDE_DATA_OUTSIDE,
  95. "MemberInside data before parent");
  96. TEST_EQ(vb2_verify_member_inside(p, 20, p, 4, 4, 17),
  97. VB2_ERROR_INSIDE_DATA_OUTSIDE,
  98. "MemberInside data too big");
  99. TEST_EQ(vb2_verify_member_inside(p, 20, p, 8, 4, 8),
  100. VB2_ERROR_INSIDE_DATA_OVERLAP,
  101. "MemberInside data overlaps member");
  102. TEST_EQ(vb2_verify_member_inside(p, -8, p, 12, 0, 0),
  103. VB2_ERROR_INSIDE_PARENT_WRAPS,
  104. "MemberInside wraparound 1");
  105. TEST_EQ(vb2_verify_member_inside(p, 20, p, -8, 0, 0),
  106. VB2_ERROR_INSIDE_MEMBER_WRAPS,
  107. "MemberInside wraparound 2");
  108. TEST_EQ(vb2_verify_member_inside(p, 20, p, 4, 4, -12),
  109. VB2_ERROR_INSIDE_DATA_WRAPS,
  110. "MemberInside wraparound 3");
  111. }
  112. {
  113. struct vb2_packed_key k = {.key_offset = sizeof(k),
  114. .key_size = 128};
  115. TEST_SUCC(vb2_verify_packed_key_inside(&k, sizeof(k)+128, &k),
  116. "PublicKeyInside ok 1");
  117. TEST_SUCC(vb2_verify_packed_key_inside(&k - 1,
  118. 2*sizeof(k)+128, &k),
  119. "PublicKeyInside ok 2");
  120. TEST_EQ(vb2_verify_packed_key_inside(&k, 128, &k),
  121. VB2_ERROR_INSIDE_DATA_OUTSIDE,
  122. "PublicKeyInside key too big");
  123. }
  124. {
  125. struct vb2_packed_key k = {.key_offset = 100,
  126. .key_size = 4};
  127. TEST_EQ(vb2_verify_packed_key_inside(&k, 99, &k),
  128. VB2_ERROR_INSIDE_DATA_OUTSIDE,
  129. "PublicKeyInside offset too big");
  130. }
  131. {
  132. struct vb2_signature s = {.sig_offset = sizeof(s),
  133. .sig_size = 128};
  134. TEST_SUCC(vb2_verify_signature_inside(&s, sizeof(s)+128, &s),
  135. "SignatureInside ok 1");
  136. TEST_SUCC(vb2_verify_signature_inside(&s - 1,
  137. 2*sizeof(s)+128, &s),
  138. "SignatureInside ok 2");
  139. TEST_EQ(vb2_verify_signature_inside(&s, 128, &s),
  140. VB2_ERROR_INSIDE_DATA_OUTSIDE,
  141. "SignatureInside sig too big");
  142. }
  143. {
  144. struct vb2_signature s = {.sig_offset = 100,
  145. .sig_size = 4};
  146. TEST_EQ(vb2_verify_signature_inside(&s, 99, &s),
  147. VB2_ERROR_INSIDE_DATA_OUTSIDE,
  148. "SignatureInside offset too big");
  149. }
  150. }
  151. int main(int argc, char* argv[])
  152. {
  153. test_struct_packing();
  154. test_helper_functions();
  155. return gTestSuccess ? 0 : 255;
  156. }