vboot_common_tests.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /* Copyright (c) 2013 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 vboot_common.c
  6. */
  7. #include <stdint.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include "host_common.h"
  11. #include "test_common.h"
  12. #include "utility.h"
  13. #include "vboot_common.h"
  14. /*
  15. * Test struct packing for vboot_struct.h structs which are passed between
  16. * firmware and OS, or passed between different phases of firmware.
  17. */
  18. static void StructPackingTest(void)
  19. {
  20. TEST_EQ(EXPECTED_VBPUBLICKEY_SIZE, sizeof(VbPublicKey),
  21. "sizeof(VbPublicKey)");
  22. TEST_EQ(EXPECTED_VBSIGNATURE_SIZE, sizeof(VbSignature),
  23. "sizeof(VbSignature)");
  24. TEST_EQ(EXPECTED_VBKEYBLOCKHEADER_SIZE, sizeof(VbKeyBlockHeader),
  25. "sizeof(VbKeyBlockHeader)");
  26. TEST_EQ(EXPECTED_VBKERNELPREAMBLEHEADER2_2_SIZE,
  27. sizeof(VbKernelPreambleHeader),
  28. "sizeof(VbKernelPreambleHeader)");
  29. TEST_EQ(VB_SHARED_DATA_HEADER_SIZE_V1,
  30. (long)&((VbSharedDataHeader*)NULL)->recovery_reason,
  31. "sizeof(VbSharedDataHeader) V1");
  32. TEST_EQ(VB_SHARED_DATA_HEADER_SIZE_V2,
  33. sizeof(VbSharedDataHeader),
  34. "sizeof(VbSharedDataHeader) V2");
  35. }
  36. /* Test array size macro */
  37. static void ArraySizeTest(void)
  38. {
  39. uint8_t arr1[12];
  40. uint32_t arr2[7];
  41. uint64_t arr3[9];
  42. TEST_EQ(ARRAY_SIZE(arr1), 12, "ARRAYSIZE(uint8_t)");
  43. TEST_EQ(ARRAY_SIZE(arr2), 7, "ARRAYSIZE(uint32_t)");
  44. TEST_EQ(ARRAY_SIZE(arr3), 9, "ARRAYSIZE(uint64_t)");
  45. }
  46. /* Helper functions not dependent on specific key sizes */
  47. static void VerifyHelperFunctions(void)
  48. {
  49. {
  50. uint8_t *p = (uint8_t *)VerifyHelperFunctions;
  51. TEST_EQ((int)OffsetOf(p, p), 0, "OffsetOf() equal");
  52. TEST_EQ((int)OffsetOf(p, p+10), 10, "OffsetOf() positive");
  53. TEST_EQ((int)OffsetOf(p, p+0x12345678), 0x12345678,
  54. "OffsetOf() large");
  55. }
  56. {
  57. VbPublicKey k = {sizeof(k), 2, 3, 4};
  58. TEST_EQ((int)OffsetOf(&k, GetPublicKeyData(&k)), sizeof(k),
  59. "GetPublicKeyData() adjacent");
  60. TEST_EQ((int)OffsetOf(&k, GetPublicKeyDataC(&k)), sizeof(k),
  61. "GetPublicKeyDataC() adjacent");
  62. }
  63. {
  64. VbPublicKey k = {123, 2, 3, 4};
  65. TEST_EQ((int)OffsetOf(&k, GetPublicKeyData(&k)), 123,
  66. "GetPublicKeyData() spaced");
  67. TEST_EQ((int)OffsetOf(&k, GetPublicKeyDataC(&k)), 123,
  68. "GetPublicKeyDataC() spaced");
  69. }
  70. {
  71. uint8_t *p = (uint8_t *)VerifyHelperFunctions;
  72. TEST_EQ(VerifyMemberInside(p, 20, p, 6, 11, 3), 0,
  73. "MemberInside ok 1");
  74. TEST_EQ(VerifyMemberInside(p, 20, p+4, 4, 8, 4), 0,
  75. "MemberInside ok 2");
  76. TEST_EQ(VerifyMemberInside(p, 20, p-4, 4, 8, 4), 1,
  77. "MemberInside member before parent");
  78. TEST_EQ(VerifyMemberInside(p, 20, p+20, 4, 8, 4), 1,
  79. "MemberInside member after parent");
  80. TEST_EQ(VerifyMemberInside(p, 20, p, 21, 0, 0), 1,
  81. "MemberInside member too big");
  82. TEST_EQ(VerifyMemberInside(p, 20, p, 4, 21, 0), 1,
  83. "MemberInside data after parent");
  84. TEST_EQ(VerifyMemberInside(p, 20, p, 4, (uint64_t)-1, 0), 1,
  85. "MemberInside data before parent");
  86. TEST_EQ(VerifyMemberInside(p, 20, p, 4, 4, 17), 1,
  87. "MemberInside data too big");
  88. TEST_EQ(VerifyMemberInside(p, (uint64_t)-1,
  89. p+(uint64_t)-10, 12, 5, 0), 1,
  90. "MemberInside wraparound 1");
  91. TEST_EQ(VerifyMemberInside(p, (uint64_t)-1,
  92. p+(uint64_t)-10, 5, 12, 0), 1,
  93. "MemberInside wraparound 2");
  94. TEST_EQ(VerifyMemberInside(p, (uint64_t)-1,
  95. p+(uint64_t)-10, 5, 0, 12), 1,
  96. "MemberInside wraparound 3");
  97. }
  98. {
  99. VbPublicKey k = {sizeof(k), 128, 0, 0};
  100. TEST_EQ(VerifyPublicKeyInside(&k, sizeof(k)+128, &k), 0,
  101. "PublicKeyInside ok 1");
  102. TEST_EQ(VerifyPublicKeyInside(&k - 1, 2*sizeof(k)+128, &k), 0,
  103. "PublicKeyInside ok 2");
  104. TEST_EQ(VerifyPublicKeyInside(&k, 128, &k), 1,
  105. "PublicKeyInside key too big");
  106. }
  107. {
  108. VbPublicKey k = {100, 4, 0, 0};
  109. TEST_EQ(VerifyPublicKeyInside(&k, 99, &k), 1,
  110. "PublicKeyInside offset too big");
  111. }
  112. {
  113. VbSignature s = {sizeof(s), 128, 2000};
  114. TEST_EQ(VerifySignatureInside(&s, sizeof(s)+128, &s), 0,
  115. "SignatureInside ok 1");
  116. TEST_EQ(VerifySignatureInside(&s - 1, 2*sizeof(s)+128, &s), 0,
  117. "SignatureInside ok 2");
  118. TEST_EQ(VerifySignatureInside(&s, 128, &s), 1,
  119. "SignatureInside sig too big");
  120. }
  121. {
  122. VbSignature s = {100, 4, 0};
  123. TEST_EQ(VerifySignatureInside(&s, 99, &s), 1,
  124. "SignatureInside offset too big");
  125. }
  126. }
  127. /* Public key utility functions */
  128. static void PublicKeyTest(void)
  129. {
  130. VbPublicKey k[3];
  131. VbPublicKey j[5];
  132. /* Fill some bits of the public key data */
  133. memset(j, 0, sizeof(j));
  134. memset(k, 0x42, sizeof(k));
  135. k[1].key_size = 12345;
  136. k[2].key_version = 67;
  137. PublicKeyInit(k, (uint8_t*)(k + 1), 2 * sizeof(VbPublicKey));
  138. TEST_EQ(k->key_offset, sizeof(VbPublicKey), "PublicKeyInit key_offset");
  139. TEST_EQ(k->key_size, 2 * sizeof(VbPublicKey), "PublicKeyInit key_size");
  140. TEST_EQ(k->algorithm, VB2_ALG_COUNT, "PublicKeyInit algorithm");
  141. TEST_EQ(k->key_version, 0, "PublicKeyInit key_version");
  142. /* Set algorithm and version, so we can tell if they get copied */
  143. k->algorithm = 3;
  144. k->key_version = 21;
  145. /* Copying to a smaller destination should fail */
  146. PublicKeyInit(j, (uint8_t*)(j + 1), 2 * sizeof(VbPublicKey) - 1);
  147. TEST_NEQ(0, PublicKeyCopy(j, k), "PublicKeyCopy too small");
  148. /* Copying to same or larger size should succeed */
  149. PublicKeyInit(j, (uint8_t*)(j + 2), 2 * sizeof(VbPublicKey) + 1);
  150. TEST_EQ(0, PublicKeyCopy(j, k), "PublicKeyCopy same");
  151. /* Offset in destination shouldn't have been modified */
  152. TEST_EQ(j->key_offset, 2 * sizeof(VbPublicKey),
  153. "PublicKeyCopy key_offset");
  154. /* Size should have been reduced to match the source */
  155. TEST_EQ(k->key_size, 2 * sizeof(VbPublicKey), "PublicKeyCopy key_size");
  156. /* Other fields should have been copied */
  157. TEST_EQ(k->algorithm, j->algorithm, "PublicKeyCopy algorithm");
  158. TEST_EQ(k->key_version, j->key_version, "PublicKeyCopy key_version");
  159. /* Data should have been copied */
  160. TEST_EQ(0,
  161. memcmp(GetPublicKeyData(k), GetPublicKeyData(j), k->key_size),
  162. "PublicKeyCopy data");
  163. }
  164. /* VbSharedData utility tests */
  165. static void VbSharedDataTest(void)
  166. {
  167. uint8_t buf[VB_SHARED_DATA_MIN_SIZE + 1];
  168. VbSharedDataHeader* d = (VbSharedDataHeader*)buf;
  169. TEST_NEQ(VBOOT_SUCCESS,
  170. VbSharedDataInit(d, sizeof(VbSharedDataHeader) - 1),
  171. "VbSharedDataInit too small");
  172. TEST_NEQ(VBOOT_SUCCESS,
  173. VbSharedDataInit(d, VB_SHARED_DATA_MIN_SIZE - 1),
  174. "VbSharedDataInit too small 2");
  175. TEST_NEQ(VBOOT_SUCCESS,
  176. VbSharedDataInit(NULL, VB_SHARED_DATA_MIN_SIZE),
  177. "VbSharedDataInit null");
  178. memset(buf, 0x68, sizeof(buf));
  179. TEST_EQ(VBOOT_SUCCESS, VbSharedDataInit(d, VB_SHARED_DATA_MIN_SIZE),
  180. "VbSharedDataInit");
  181. /* Check fields that should have been initialized */
  182. TEST_EQ(d->magic, VB_SHARED_DATA_MAGIC, "VbSharedDataInit magic");
  183. TEST_EQ(d->struct_version, VB_SHARED_DATA_VERSION,
  184. "VbSharedDataInit version");
  185. TEST_EQ(d->struct_size, sizeof(VbSharedDataHeader),
  186. "VbSharedDataInit struct_size");
  187. TEST_EQ(d->data_size, VB_SHARED_DATA_MIN_SIZE,
  188. "VbSharedDataInit data_size");
  189. TEST_EQ(d->data_used, d->struct_size, "VbSharedDataInit data_used");
  190. TEST_EQ(d->firmware_index, 0xFF, "VbSharedDataInit firmware index");
  191. /* Sample some other fields to make sure they were zeroed */
  192. TEST_EQ(d->flags, 0, "VbSharedDataInit firmware flags");
  193. TEST_EQ(d->lk_call_count, 0, "VbSharedDataInit lk_call_count");
  194. TEST_EQ(d->kernel_version_lowest, 0,
  195. "VbSharedDataInit kernel_version_lowest");
  196. TEST_NEQ(VBOOT_SUCCESS, VbSharedDataSetKernelKey(NULL, NULL),
  197. "VbSharedDataSetKernelKey null");
  198. }
  199. int main(int argc, char* argv[])
  200. {
  201. StructPackingTest();
  202. ArraySizeTest();
  203. VerifyHelperFunctions();
  204. PublicKeyTest();
  205. VbSharedDataTest();
  206. return gTestSuccess ? 0 : 255;
  207. }