vb2_secdata_tests.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 secure storage library.
  6. */
  7. #include <stdint.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include "2sysincludes.h"
  12. #include "test_common.h"
  13. #include "vboot_common.h"
  14. #include "2common.h"
  15. #include "2api.h"
  16. #include "2misc.h"
  17. #include "2secdata.h"
  18. static void test_changed(struct vb2_context *ctx, int changed, const char *why)
  19. {
  20. if (changed)
  21. TEST_NEQ(ctx->flags & VB2_CONTEXT_SECDATA_CHANGED, 0, why);
  22. else
  23. TEST_EQ(ctx->flags & VB2_CONTEXT_SECDATA_CHANGED, 0, why);
  24. ctx->flags &= ~VB2_CONTEXT_SECDATA_CHANGED;
  25. };
  26. static void secdata_test(void)
  27. {
  28. uint8_t workbuf[VB2_WORKBUF_RECOMMENDED_SIZE]
  29. __attribute__ ((aligned (VB2_WORKBUF_ALIGN)));
  30. struct vb2_context c = {
  31. .flags = 0,
  32. .workbuf = workbuf,
  33. .workbuf_size = sizeof(workbuf),
  34. };
  35. uint32_t v = 1;
  36. /* Blank data is invalid */
  37. memset(c.secdata, 0xa6, sizeof(c.secdata));
  38. TEST_EQ(vb2_secdata_check_crc(&c),
  39. VB2_ERROR_SECDATA_CRC, "Check blank CRC");
  40. TEST_EQ(vb2_secdata_init(&c),
  41. VB2_ERROR_SECDATA_CRC, "Init blank CRC");
  42. /* Ensure zeroed buffers are invalid (coreboot relies on this) */
  43. memset(c.secdata, 0, sizeof(c.secdata));
  44. TEST_EQ(vb2_secdata_init(&c), VB2_ERROR_SECDATA_ZERO, "Zeroed buffer");
  45. /* Create good data */
  46. TEST_SUCC(vb2_secdata_create(&c), "Create");
  47. TEST_SUCC(vb2_secdata_check_crc(&c), "Check created CRC");
  48. TEST_SUCC(vb2_secdata_init(&c), "Init created CRC");
  49. test_changed(&c, 1, "Create changes data");
  50. /* Now corrupt it */
  51. c.secdata[2]++;
  52. TEST_EQ(vb2_secdata_check_crc(&c),
  53. VB2_ERROR_SECDATA_CRC, "Check invalid CRC");
  54. TEST_EQ(vb2_secdata_init(&c),
  55. VB2_ERROR_SECDATA_CRC, "Init invalid CRC");
  56. vb2_secdata_create(&c);
  57. c.flags = 0;
  58. /* Read/write flags */
  59. TEST_SUCC(vb2_secdata_get(&c, VB2_SECDATA_FLAGS, &v), "Get flags");
  60. TEST_EQ(v, 0, "Flags created 0");
  61. test_changed(&c, 0, "Get doesn't change data");
  62. TEST_SUCC(vb2_secdata_set(&c, VB2_SECDATA_FLAGS, 0x12), "Set flags");
  63. test_changed(&c, 1, "Set changes data");
  64. TEST_SUCC(vb2_secdata_set(&c, VB2_SECDATA_FLAGS, 0x12), "Set flags 2");
  65. test_changed(&c, 0, "Set again doesn't change data");
  66. TEST_SUCC(vb2_secdata_get(&c, VB2_SECDATA_FLAGS, &v), "Get flags 2");
  67. TEST_EQ(v, 0x12, "Flags changed");
  68. TEST_EQ(vb2_secdata_set(&c, VB2_SECDATA_FLAGS, 0x100),
  69. VB2_ERROR_SECDATA_SET_FLAGS, "Bad flags");
  70. /* Read/write versions */
  71. TEST_SUCC(vb2_secdata_get(&c, VB2_SECDATA_VERSIONS, &v),
  72. "Get versions");
  73. TEST_EQ(v, 0, "Versions created 0");
  74. test_changed(&c, 0, "Get doesn't change data");
  75. TEST_SUCC(vb2_secdata_set(&c, VB2_SECDATA_VERSIONS, 0x123456ff),
  76. "Set versions");
  77. test_changed(&c, 1, "Set changes data");
  78. TEST_SUCC(vb2_secdata_set(&c, VB2_SECDATA_VERSIONS, 0x123456ff),
  79. "Set versions 2");
  80. test_changed(&c, 0, "Set again doesn't change data");
  81. TEST_SUCC(vb2_secdata_get(&c, VB2_SECDATA_VERSIONS, &v),
  82. "Get versions 2");
  83. TEST_EQ(v, 0x123456ff, "Versions changed");
  84. /* Invalid field fails */
  85. TEST_EQ(vb2_secdata_get(&c, -1, &v),
  86. VB2_ERROR_SECDATA_GET_PARAM, "Get invalid");
  87. TEST_EQ(vb2_secdata_set(&c, -1, 456),
  88. VB2_ERROR_SECDATA_SET_PARAM, "Set invalid");
  89. test_changed(&c, 0, "Set invalid field doesn't change data");
  90. /* Read/write uninitialized data fails */
  91. vb2_get_sd(&c)->status &= ~VB2_SD_STATUS_SECDATA_INIT;
  92. TEST_EQ(vb2_secdata_get(&c, VB2_SECDATA_VERSIONS, &v),
  93. VB2_ERROR_SECDATA_GET_UNINITIALIZED, "Get uninitialized");
  94. test_changed(&c, 0, "Get uninitialized doesn't change data");
  95. TEST_EQ(vb2_secdata_set(&c, VB2_SECDATA_VERSIONS, 0x123456ff),
  96. VB2_ERROR_SECDATA_SET_UNINITIALIZED, "Set uninitialized");
  97. test_changed(&c, 0, "Set uninitialized doesn't change data");
  98. }
  99. int main(int argc, char* argv[])
  100. {
  101. secdata_test();
  102. return gTestSuccess ? 0 : 255;
  103. }