vb20_common2_tests.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 image library.
  6. */
  7. #include <stdint.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include "2sysincludes.h"
  11. #include "2rsa.h"
  12. #include "file_keys.h"
  13. #include "host_common.h"
  14. #include "host_key2.h"
  15. #include "vb2_common.h"
  16. #include "vboot_common.h"
  17. #include "test_common.h"
  18. static const uint8_t test_data[] = "This is some test data to sign.";
  19. static const uint32_t test_size = sizeof(test_data);
  20. static void test_unpack_key(const struct vb2_packed_key *key1)
  21. {
  22. struct vb2_public_key pubk;
  23. /*
  24. * Key data follows the header for a newly allocated key, so we can
  25. * calculate the buffer size by looking at how far the key data goes.
  26. */
  27. uint32_t size = key1->key_offset + key1->key_size;
  28. uint8_t *buf = malloc(size);
  29. struct vb2_packed_key *key = (struct vb2_packed_key *)buf;
  30. memcpy(key, key1, size);
  31. TEST_SUCC(vb2_unpack_key_buffer(&pubk, buf, size),
  32. "vb2_unpack_key_buffer() ok");
  33. TEST_EQ(pubk.sig_alg, vb2_crypto_to_signature(key->algorithm),
  34. "vb2_unpack_key_buffer() sig_alg");
  35. TEST_EQ(pubk.hash_alg, vb2_crypto_to_hash(key->algorithm),
  36. "vb2_unpack_key_buffer() hash_alg");
  37. memcpy(key, key1, size);
  38. key->algorithm = VB2_ALG_COUNT;
  39. TEST_EQ(vb2_unpack_key_buffer(&pubk, buf, size),
  40. VB2_ERROR_UNPACK_KEY_SIG_ALGORITHM,
  41. "vb2_unpack_key_buffer() invalid algorithm");
  42. memcpy(key, key1, size);
  43. key->key_size--;
  44. TEST_EQ(vb2_unpack_key_buffer(&pubk, buf, size),
  45. VB2_ERROR_UNPACK_KEY_SIZE,
  46. "vb2_unpack_key_buffer() invalid size");
  47. memcpy(key, key1, size);
  48. key->key_offset++;
  49. TEST_EQ(vb2_unpack_key_buffer(&pubk, buf, size + 1),
  50. VB2_ERROR_UNPACK_KEY_ALIGN,
  51. "vb2_unpack_key_buffer() unaligned data");
  52. memcpy(key, key1, size);
  53. *(uint32_t *)(buf + key->key_offset) /= 2;
  54. TEST_EQ(vb2_unpack_key_buffer(&pubk, buf, size),
  55. VB2_ERROR_UNPACK_KEY_ARRAY_SIZE,
  56. "vb2_unpack_key_buffer() invalid key array size");
  57. memcpy(key, key1, size);
  58. TEST_EQ(vb2_unpack_key_buffer(&pubk, buf, size - 1),
  59. VB2_ERROR_INSIDE_DATA_OUTSIDE,
  60. "vb2_unpack_key_buffer() buffer too small");
  61. free(key);
  62. TEST_EQ(vb2_unpack_key(&pubk, NULL),
  63. VB2_ERROR_UNPACK_KEY_BUFFER,
  64. "vb2_unpack_key_() buffer NULL");
  65. }
  66. static void test_verify_data(const struct vb2_packed_key *key1,
  67. const struct vb2_signature *sig)
  68. {
  69. uint8_t workbuf[VB2_VERIFY_DATA_WORKBUF_BYTES]
  70. __attribute__ ((aligned (VB2_WORKBUF_ALIGN)));
  71. struct vb2_workbuf wb;
  72. struct vb2_public_key pubk, pubk_orig;
  73. uint32_t sig_total_size = sig->sig_offset + sig->sig_size;
  74. struct vb2_signature *sig2;
  75. vb2_workbuf_init(&wb, workbuf, sizeof(workbuf));
  76. /* Allocate signature copy for tests */
  77. sig2 = (struct vb2_signature *)malloc(sig_total_size);
  78. TEST_SUCC(vb2_unpack_key(&pubk, key1), "vb2_verify_data() unpack key");
  79. pubk_orig = pubk;
  80. memcpy(sig2, sig, sig_total_size);
  81. pubk.sig_alg = VB2_SIG_INVALID;
  82. TEST_NEQ(vb2_verify_data(test_data, test_size, sig2, &pubk, &wb),
  83. 0, "vb2_verify_data() bad sig alg");
  84. pubk.sig_alg = pubk_orig.sig_alg;
  85. memcpy(sig2, sig, sig_total_size);
  86. pubk.hash_alg = VB2_HASH_INVALID;
  87. TEST_NEQ(vb2_verify_data(test_data, test_size, sig2, &pubk, &wb),
  88. 0, "vb2_verify_data() bad hash alg");
  89. pubk.hash_alg = pubk_orig.hash_alg;
  90. vb2_workbuf_init(&wb, workbuf, 4);
  91. memcpy(sig2, sig, sig_total_size);
  92. TEST_NEQ(vb2_verify_data(test_data, test_size, sig2, &pubk, &wb),
  93. 0, "vb2_verify_data() workbuf too small");
  94. vb2_workbuf_init(&wb, workbuf, sizeof(workbuf));
  95. memcpy(sig2, sig, sig_total_size);
  96. TEST_EQ(vb2_verify_data(test_data, test_size, sig2, &pubk, &wb),
  97. 0, "vb2_verify_data() ok");
  98. memcpy(sig2, sig, sig_total_size);
  99. sig2->sig_size -= 16;
  100. TEST_NEQ(vb2_verify_data(test_data, test_size, sig2, &pubk, &wb),
  101. 0, "vb2_verify_data() wrong sig size");
  102. memcpy(sig2, sig, sig_total_size);
  103. TEST_NEQ(vb2_verify_data(test_data, test_size - 1, sig2, &pubk, &wb),
  104. 0, "vb2_verify_data() input buffer too small");
  105. memcpy(sig2, sig, sig_total_size);
  106. vb2_signature_data(sig2)[0] ^= 0x5A;
  107. TEST_NEQ(vb2_verify_data(test_data, test_size, sig2, &pubk, &wb),
  108. 0, "vb2_verify_data() wrong sig");
  109. free(sig2);
  110. }
  111. int test_algorithm(int key_algorithm, const char *keys_dir)
  112. {
  113. char filename[1024];
  114. int rsa_bits = 8 * vb2_rsa_sig_size(
  115. vb2_crypto_to_signature(key_algorithm));
  116. struct vb2_private_key *private_key = NULL;
  117. struct vb2_signature *sig = NULL;
  118. struct vb2_packed_key *key1 = NULL;
  119. int retval = 1;
  120. printf("***Testing algorithm: %s\n",
  121. vb2_get_crypto_algorithm_name(key_algorithm));
  122. snprintf(filename, sizeof(filename),
  123. "%s/key_rsa%d.pem", keys_dir, rsa_bits);
  124. private_key = vb2_read_private_key_pem(filename, key_algorithm);
  125. if (!private_key) {
  126. fprintf(stderr, "Error reading private_key: %s\n", filename);
  127. goto cleanup_algorithm;
  128. }
  129. snprintf(filename, sizeof(filename),
  130. "%s/key_rsa%d.keyb", keys_dir, rsa_bits);
  131. key1 = vb2_read_packed_keyb(filename, key_algorithm, 1);
  132. if (!key1) {
  133. fprintf(stderr, "Error reading public_key: %s\n", filename);
  134. goto cleanup_algorithm;
  135. }
  136. /* Calculate good signatures */
  137. sig = vb2_calculate_signature(test_data, sizeof(test_data),
  138. private_key);
  139. TEST_PTR_NEQ(sig, 0, "Calculate signature");
  140. if (!sig)
  141. goto cleanup_algorithm;
  142. test_unpack_key(key1);
  143. test_verify_data(key1, sig);
  144. retval = 0;
  145. cleanup_algorithm:
  146. if (key1)
  147. free(key1);
  148. if (private_key)
  149. free(private_key);
  150. if (sig)
  151. free(sig);
  152. return retval;
  153. }
  154. /* Test only the algorithms we use */
  155. const int key_algs[] = {
  156. VB2_ALG_RSA2048_SHA256,
  157. VB2_ALG_RSA4096_SHA256,
  158. VB2_ALG_RSA8192_SHA512,
  159. };
  160. int main(int argc, char *argv[]) {
  161. if (argc == 2) {
  162. int i;
  163. for (i = 0; i < ARRAY_SIZE(key_algs); i++) {
  164. if (test_algorithm(key_algs[i], argv[1]))
  165. return 1;
  166. }
  167. } else if (argc == 3 && !strcasecmp(argv[2], "--all")) {
  168. /* Test all the algorithms */
  169. int alg;
  170. for (alg = 0; alg < VB2_ALG_COUNT; alg++) {
  171. if (test_algorithm(alg, argv[1]))
  172. return 1;
  173. }
  174. } else {
  175. fprintf(stderr, "Usage: %s <keys_dir> [--all]", argv[0]);
  176. return -1;
  177. }
  178. return gTestSuccess ? 0 : 255;
  179. }