vb21_host_sig_tests.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 key 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_signature2.h"
  16. #include "test_common.h"
  17. /* Test only the algorithms we use */
  18. struct alg_combo {
  19. const char *name;
  20. enum vb2_signature_algorithm sig_alg;
  21. enum vb2_hash_algorithm hash_alg;
  22. };
  23. static const struct alg_combo test_algs[] = {
  24. {"RSA2048/SHA-256", VB2_SIG_RSA2048, VB2_HASH_SHA256},
  25. {"RSA4096/SHA-256", VB2_SIG_RSA4096, VB2_HASH_SHA256},
  26. {"RSA8192/SHA-512", VB2_SIG_RSA8192, VB2_HASH_SHA512},
  27. };
  28. const struct vb2_id test_id = {.raw = {0xaa}};
  29. const char *test_desc = "The test key";
  30. const char *test_sig_desc = "The test signature";
  31. const uint8_t test_data[] = "Some test data";
  32. const uint32_t test_size = sizeof(test_data);
  33. static void sig_tests(const struct alg_combo *combo,
  34. const char *pemfile,
  35. const char *keybfile)
  36. {
  37. struct vb2_private_key *prik, prik2;
  38. const struct vb2_private_key *prihash, *priks[2];
  39. struct vb2_public_key *pubk, pubhash;
  40. struct vb21_signature *sig, *sig2;
  41. uint32_t size;
  42. uint8_t workbuf[VB2_VERIFY_DATA_WORKBUF_BYTES]
  43. __attribute__ ((aligned (VB2_WORKBUF_ALIGN)));
  44. struct vb2_workbuf wb;
  45. uint8_t *buf;
  46. uint32_t bufsize;
  47. struct vb21_struct_common *c;
  48. uint32_t c_sig_offs;
  49. vb2_workbuf_init(&wb, workbuf, sizeof(workbuf));
  50. /* Create test keys */
  51. /* TODO: should read these from .vbprik2, .vbpubk2 files */
  52. TEST_SUCC(vb2_private_key_read_pem(&prik, pemfile), "Read private key");
  53. prik->id = test_id;
  54. prik->hash_alg = combo->hash_alg;
  55. prik->sig_alg = combo->sig_alg;
  56. vb2_private_key_set_desc(prik, test_desc);
  57. TEST_SUCC(vb2_public_key_read_keyb(&pubk, keybfile), "Read pub key");
  58. pubk->id = &test_id;
  59. pubk->hash_alg = combo->hash_alg;
  60. vb2_public_key_set_desc(pubk, test_desc);
  61. TEST_SUCC(vb2_private_key_hash(&prihash, combo->hash_alg),
  62. "Private hash key");
  63. TEST_SUCC(vb2_public_key_hash(&pubhash, combo->hash_alg),
  64. "Public hash key");
  65. priks[0] = prik;
  66. priks[1] = prihash;
  67. /* Sign test data */
  68. TEST_SUCC(vb21_sign_data(&sig, test_data, test_size, prik, NULL),
  69. "Sign good");
  70. TEST_PTR_NEQ(sig, NULL, " sig_ptr");
  71. TEST_EQ(0, strcmp(vb21_common_desc(sig), test_desc), " desc");
  72. TEST_EQ(0, memcmp(&sig->id, &test_id, sizeof(test_id)), " id");
  73. TEST_EQ(sig->data_size, test_size, " data_size");
  74. TEST_SUCC(vb21_sig_size_for_key(&size, prik, NULL), "Sig size");
  75. TEST_EQ(size, sig->c.total_size, " size");
  76. TEST_SUCC(vb21_verify_data(test_data, test_size, sig, pubk, &wb),
  77. "Verify good");
  78. free(sig);
  79. TEST_SUCC(vb21_sign_data(&sig, test_data, test_size, prik,
  80. test_sig_desc),
  81. "Sign with desc");
  82. TEST_EQ(0, strcmp(vb21_common_desc(sig), test_sig_desc), " desc");
  83. free(sig);
  84. TEST_SUCC(vb21_sign_data(&sig, test_data, test_size, prik, ""),
  85. "Sign with no desc");
  86. TEST_EQ(sig->c.desc_size, 0, " desc");
  87. TEST_SUCC(vb21_sig_size_for_key(&size, prik, ""), "Sig size");
  88. TEST_EQ(size, sig->c.total_size, " size");
  89. free(sig);
  90. TEST_SUCC(vb21_sign_data(&sig, test_data, test_size, prihash, NULL),
  91. "Sign with hash");
  92. TEST_SUCC(vb21_verify_data(test_data, test_size, sig, &pubhash, &wb),
  93. "Verify with hash");
  94. free(sig);
  95. prik2 = *prik;
  96. prik2.sig_alg = VB2_SIG_INVALID;
  97. TEST_EQ(vb21_sign_data(&sig, test_data, test_size, &prik2, NULL),
  98. VB2_SIGN_DATA_SIG_SIZE, "Sign bad sig alg");
  99. /* Sign an object with a little (24 bytes) data */
  100. c_sig_offs = sizeof(*c) + 24;
  101. TEST_SUCC(vb21_sig_size_for_key(&size, prik, NULL), "Sig size");
  102. bufsize = c_sig_offs + size;
  103. buf = calloc(1, bufsize);
  104. memset(buf + sizeof(*c), 0x12, 24);
  105. c = (struct vb21_struct_common *)buf;
  106. c->total_size = bufsize;
  107. TEST_SUCC(vb21_sign_object(buf, c_sig_offs, prik, NULL), "Sign object");
  108. sig = (struct vb21_signature *)(buf + c_sig_offs);
  109. TEST_SUCC(vb21_verify_data(buf, c_sig_offs, sig, pubk, &wb),
  110. "Verify object");
  111. TEST_EQ(vb21_sign_object(buf, c_sig_offs + 4, prik, NULL),
  112. VB2_SIGN_OBJECT_OVERFLOW, "Sign object overflow");
  113. free(buf);
  114. /* Multiply sign an object */
  115. TEST_SUCC(vb21_sig_size_for_keys(&size, priks, 2), "Sigs size");
  116. bufsize = c_sig_offs + size;
  117. buf = calloc(1, bufsize);
  118. memset(buf + sizeof(*c), 0x12, 24);
  119. c = (struct vb21_struct_common *)buf;
  120. c->total_size = bufsize;
  121. TEST_SUCC(vb21_sign_object_multiple(buf, c_sig_offs, priks, 2),
  122. "Sign multiple");
  123. sig = (struct vb21_signature *)(buf + c_sig_offs);
  124. TEST_SUCC(vb21_verify_data(buf, c_sig_offs, sig, pubk, &wb),
  125. "Verify object with sig 1");
  126. sig2 = (struct vb21_signature *)(buf + c_sig_offs + sig->c.total_size);
  127. TEST_SUCC(vb21_verify_data(buf, c_sig_offs, sig2, &pubhash, &wb),
  128. "Verify object with sig 2");
  129. c->total_size -= 4;
  130. TEST_EQ(vb21_sign_object_multiple(buf, c_sig_offs, priks, 2),
  131. VB2_SIGN_OBJECT_OVERFLOW, "Sign multple overflow");
  132. TEST_EQ(size, sig->c.total_size + sig2->c.total_size,
  133. "Sigs size total");
  134. free(buf);
  135. vb2_private_key_free(prik);
  136. vb2_public_key_free(pubk);
  137. }
  138. static int test_algorithm(const struct alg_combo *combo, const char *keys_dir)
  139. {
  140. int rsa_bits = vb2_rsa_sig_size(combo->sig_alg) * 8;
  141. char pemfile[1024];
  142. char keybfile[1024];
  143. printf("***Testing algorithm: %s\n", combo->name);
  144. snprintf(pemfile, sizeof(pemfile),
  145. "%s/key_rsa%d.pem", keys_dir, rsa_bits);
  146. snprintf(keybfile, sizeof(keybfile),
  147. "%s/key_rsa%d.keyb", keys_dir, rsa_bits);
  148. sig_tests(combo, pemfile, keybfile);
  149. return 0;
  150. }
  151. int main(int argc, char *argv[]) {
  152. if (argc == 2) {
  153. int i;
  154. for (i = 0; i < ARRAY_SIZE(test_algs); i++) {
  155. if (test_algorithm(test_algs + i, argv[1]))
  156. return 1;
  157. }
  158. } else {
  159. fprintf(stderr, "Usage: %s <keys_dir>", argv[0]);
  160. return -1;
  161. }
  162. return gTestSuccess ? 0 : 255;
  163. }