vb21_host_keyblock_tests.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 keyblock 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_key2.h"
  15. #include "host_keyblock2.h"
  16. #include "test_common.h"
  17. static void keyblock_tests(const char *keys_dir)
  18. {
  19. struct vb2_public_key *pubk2048, *pubk4096, *pubk8192, pubkhash;
  20. struct vb2_private_key *prik4096, *prik8192;
  21. struct vb21_packed_key *pak, *pakgood;
  22. struct vb21_keyblock *kb;
  23. const struct vb2_private_key *prikhash;
  24. const struct vb2_private_key *prik[2];
  25. char fname[1024];
  26. const char test_desc[] = "Test keyblock";
  27. uint8_t workbuf[VB2_KEY_BLOCK_VERIFY_WORKBUF_BYTES]
  28. __attribute__ ((aligned (VB2_WORKBUF_ALIGN)));
  29. struct vb2_workbuf wb;
  30. vb2_workbuf_init(&wb, workbuf, sizeof(workbuf));
  31. /* Read keys */
  32. sprintf(fname, "%s/key_rsa2048.keyb", keys_dir);
  33. TEST_SUCC(vb2_public_key_read_keyb(&pubk2048, fname),
  34. "Read public key 2");
  35. vb2_public_key_set_desc(pubk2048, "Test RSA2048 public key");
  36. pubk2048->hash_alg = VB2_HASH_SHA256;
  37. sprintf(fname, "%s/key_rsa4096.keyb", keys_dir);
  38. TEST_SUCC(vb2_public_key_read_keyb(&pubk4096, fname),
  39. "Read public key 1");
  40. vb2_public_key_set_desc(pubk4096, "Test RSA4096 public key");
  41. pubk4096->hash_alg = VB2_HASH_SHA256;
  42. sprintf(fname, "%s/key_rsa8192.keyb", keys_dir);
  43. TEST_SUCC(vb2_public_key_read_keyb(&pubk8192, fname),
  44. "Read public key 2");
  45. vb2_public_key_set_desc(pubk8192, "Test RSA8192 public key");
  46. pubk8192->hash_alg = VB2_HASH_SHA512;
  47. sprintf(fname, "%s/key_rsa4096.pem", keys_dir);
  48. TEST_SUCC(vb2_private_key_read_pem(&prik4096, fname),
  49. "Read private key 2");
  50. vb2_private_key_set_desc(prik4096, "Test RSA4096 private key");
  51. prik4096->sig_alg = VB2_SIG_RSA4096;
  52. prik4096->hash_alg = VB2_HASH_SHA256;
  53. sprintf(fname, "%s/key_rsa8192.pem", keys_dir);
  54. TEST_SUCC(vb2_private_key_read_pem(&prik8192, fname),
  55. "Read private key 1");
  56. vb2_private_key_set_desc(prik8192, "Test RSA8192 private key");
  57. prik8192->sig_alg = VB2_SIG_RSA8192;
  58. prik8192->hash_alg = VB2_HASH_SHA512;
  59. TEST_SUCC(vb2_private_key_hash(&prikhash, VB2_HASH_SHA512),
  60. "Create private hash key");
  61. TEST_SUCC(vb2_public_key_hash(&pubkhash, VB2_HASH_SHA512),
  62. "Create public hash key");
  63. TEST_SUCC(vb21_public_key_pack(&pakgood, pubk2048), "Test packed key");
  64. /* Sign a keyblock with one key */
  65. prik[0] = prik4096;
  66. TEST_SUCC(vb21_keyblock_create(&kb, pubk2048, prik, 1, 0x1234, NULL),
  67. "Keyblock single");
  68. TEST_PTR_NEQ(kb, NULL, " kb_ptr");
  69. TEST_SUCC(vb21_verify_keyblock(kb, kb->c.total_size, pubk4096, &wb),
  70. " verify");
  71. TEST_EQ(strcmp(vb21_common_desc(kb), pubk2048->desc), 0, " desc");
  72. TEST_EQ(kb->flags, 0x1234, " flags");
  73. pak = (struct vb21_packed_key *)((uint8_t *)kb + kb->key_offset);
  74. TEST_EQ(0, memcmp(pak, pakgood, pakgood->c.total_size), " data key");
  75. free(kb);
  76. /* Sign a keyblock with two keys */
  77. prik[0] = prik8192;
  78. prik[1] = prikhash;
  79. TEST_SUCC(vb21_keyblock_create(&kb, pubk4096, prik, 2, 0, test_desc),
  80. "Keyblock multiple");
  81. TEST_SUCC(vb21_verify_keyblock(kb, kb->c.total_size, pubk8192, &wb),
  82. " verify 1");
  83. TEST_SUCC(vb21_verify_keyblock(kb, kb->c.total_size, &pubkhash, &wb),
  84. " verify 2");
  85. TEST_EQ(strcmp(vb21_common_desc(kb), test_desc), 0, " desc");
  86. TEST_EQ(kb->flags, 0, " flags");
  87. free(kb);
  88. /* Test errors */
  89. prik[0] = prik8192;
  90. prik8192->hash_alg = VB2_HASH_INVALID;
  91. TEST_EQ(vb21_keyblock_create(&kb, pubk4096, prik, 1, 0, NULL),
  92. VB2_KEYBLOCK_CREATE_SIG_SIZE, "Keyblock bad sig size");
  93. TEST_PTR_EQ(kb, NULL, " kb_ptr");
  94. free(kb);
  95. prik[0] = prik4096;
  96. pubk4096->sig_alg = VB2_SIG_INVALID;
  97. TEST_EQ(vb21_keyblock_create(&kb, pubk4096, prik, 1, 0, NULL),
  98. VB2_KEYBLOCK_CREATE_DATA_KEY, "Keyblock bad data key");
  99. free(kb);
  100. /* Free keys */
  101. free(pakgood);
  102. vb2_public_key_free(pubk2048);
  103. vb2_public_key_free(pubk4096);
  104. vb2_public_key_free(pubk8192);
  105. vb2_private_key_free(prik4096);
  106. vb2_private_key_free(prik8192);
  107. }
  108. int main(int argc, char *argv[]) {
  109. if (argc == 2) {
  110. keyblock_tests(argv[1]);
  111. } else {
  112. fprintf(stderr, "Usage: %s <keys_dir>", argv[0]);
  113. return -1;
  114. }
  115. return gTestSuccess ? 0 : 255;
  116. }