vb21_host_fw_preamble_tests.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 host library vboot2 preamble functions
  6. */
  7. #include <stdio.h>
  8. #include <unistd.h>
  9. #include "2sysincludes.h"
  10. #include "2common.h"
  11. #include "2rsa.h"
  12. #include "vb21_common.h"
  13. #include "host_common.h"
  14. #include "host_fw_preamble2.h"
  15. #include "host_key2.h"
  16. #include "host_signature2.h"
  17. #include "test_common.h"
  18. const uint8_t test_data1[] = "Some test data";
  19. const uint8_t test_data2[] = "Some more test data";
  20. const uint8_t test_data3[] = "Even more test data";
  21. static void preamble_tests(const char *keys_dir)
  22. {
  23. struct vb2_private_key *prik4096;
  24. struct vb2_public_key *pubk4096;
  25. struct vb21_fw_preamble *fp;
  26. const struct vb2_private_key *prikhash;
  27. struct vb21_signature *hashes[3];
  28. char fname[1024];
  29. const char test_desc[] = "Test fw preamble";
  30. const uint32_t test_version = 2061;
  31. const uint32_t test_flags = 0x11223344;
  32. uint32_t hash_next;
  33. int i;
  34. uint8_t workbuf[VB2_VERIFY_FIRMWARE_PREAMBLE_WORKBUF_BYTES]
  35. __attribute__ ((aligned (VB2_WORKBUF_ALIGN)));
  36. struct vb2_workbuf wb;
  37. vb2_workbuf_init(&wb, workbuf, sizeof(workbuf));
  38. /* Read keys */
  39. snprintf(fname, sizeof(fname), "%s/key_rsa4096.keyb", keys_dir);
  40. TEST_SUCC(vb2_public_key_read_keyb(&pubk4096, fname),
  41. "Read public key 1");
  42. vb2_public_key_set_desc(pubk4096, "Test RSA4096 public key");
  43. pubk4096->hash_alg = VB2_HASH_SHA256;
  44. snprintf(fname, sizeof(fname), "%s/key_rsa4096.pem", keys_dir);
  45. TEST_SUCC(vb2_private_key_read_pem(&prik4096, fname),
  46. "Read private key 2");
  47. vb2_private_key_set_desc(prik4096, "Test RSA4096 private key");
  48. prik4096->sig_alg = VB2_SIG_RSA4096;
  49. prik4096->hash_alg = VB2_HASH_SHA256;
  50. TEST_SUCC(vb2_private_key_hash(&prikhash, VB2_HASH_SHA256),
  51. "Create private hash key");
  52. /* Create some signatures */
  53. TEST_SUCC(vb21_sign_data(hashes + 0, test_data1, sizeof(test_data1),
  54. prikhash, "Hash 1"),
  55. "Hash 1");
  56. TEST_SUCC(vb21_sign_data(hashes + 1, test_data2, sizeof(test_data2),
  57. prikhash, "Hash 2"),
  58. "Hash 2");
  59. TEST_SUCC(vb21_sign_data(hashes + 2, test_data3, sizeof(test_data3),
  60. prikhash, "Hash 3"),
  61. "Hash 3");
  62. /* Test good preamble */
  63. TEST_SUCC(vb21_fw_preamble_create(
  64. &fp, prik4096, (const struct vb21_signature **)hashes,
  65. 3, test_version, test_flags, test_desc),
  66. "Create preamble good");
  67. TEST_PTR_NEQ(fp, NULL, " fp_ptr");
  68. TEST_SUCC(vb21_verify_fw_preamble(fp, fp->c.total_size, pubk4096, &wb),
  69. "Verify preamble good");
  70. TEST_EQ(strcmp(vb21_common_desc(fp), test_desc), 0, " desc");
  71. TEST_EQ(fp->fw_version, test_version, " fw_version");
  72. TEST_EQ(fp->flags, test_flags, " flags");
  73. TEST_EQ(fp->hash_count, 3, " hash_count");
  74. hash_next = fp->hash_offset;
  75. for (i = 0; i < 3; i++) {
  76. TEST_EQ(0, memcmp((uint8_t *)fp + hash_next, hashes[i],
  77. hashes[i]->c.total_size), " hash[i]");
  78. hash_next += hashes[i]->c.total_size;
  79. }
  80. free(fp);
  81. /* Test errors */
  82. prik4096->hash_alg = VB2_HASH_INVALID;
  83. TEST_EQ(vb21_fw_preamble_create(&fp, prik4096,
  84. (const struct vb21_signature **)hashes,
  85. 3, test_version, test_flags,
  86. test_desc),
  87. VB2_FW_PREAMBLE_CREATE_SIG_SIZE,
  88. "Create preamble bad sig");
  89. TEST_PTR_EQ(fp, NULL, " fp_ptr");
  90. /* Free keys */
  91. vb2_public_key_free(pubk4096);
  92. vb2_private_key_free(prik4096);
  93. }
  94. int main(int argc, char *argv[]) {
  95. if (argc == 2) {
  96. preamble_tests(argv[1]);
  97. } else {
  98. fprintf(stderr, "Usage: %s <keys_dir>", argv[0]);
  99. return -1;
  100. }
  101. return gTestSuccess ? 0 : 255;
  102. }