• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
6 /* FIPS 180-2 Tests for message digest functions. */
7 
8 #include "2sysincludes.h"
9 #include "2rsa.h"
10 #include "2sha.h"
11 #include "2return_codes.h"
12 
13 #include "sha_test_vectors.h"
14 #include "test_common.h"
15 
vb2_digest(const uint8_t * buf,uint32_t size,enum vb2_hash_algorithm hash_alg,uint8_t * digest,uint32_t digest_size)16 static int vb2_digest(const uint8_t *buf,
17 		      uint32_t size,
18 		      enum vb2_hash_algorithm hash_alg,
19 		      uint8_t *digest,
20 		      uint32_t digest_size)
21 {
22 	struct vb2_digest_context dc;
23 	int rv;
24 
25 	rv = vb2_digest_init(&dc, hash_alg);
26 	if (rv)
27 		return rv;
28 
29 	rv = vb2_digest_extend(&dc, buf, size);
30 	if (rv)
31 		return rv;
32 
33 	return vb2_digest_finalize(&dc, digest, digest_size);
34 }
35 
sha1_tests(void)36 void sha1_tests(void)
37 {
38 	uint8_t digest[VB2_SHA1_DIGEST_SIZE];
39 	uint8_t *test_inputs[3];
40 	int i;
41 
42 	test_inputs[0] = (uint8_t *) oneblock_msg;
43 	test_inputs[1] = (uint8_t *) multiblock_msg1;
44 	test_inputs[2] = (uint8_t *) long_msg;
45 
46 	for (i = 0; i < 3; i++) {
47 		TEST_SUCC(vb2_digest(test_inputs[i],
48 				     strlen((char *)test_inputs[i]),
49 				     VB2_HASH_SHA1, digest, sizeof(digest)),
50 			  "vb2_digest() SHA1");
51 		TEST_EQ(memcmp(digest, sha1_results[i], sizeof(digest)),
52 			0, "SHA1 digest");
53 	}
54 
55 	TEST_EQ(vb2_digest(test_inputs[0], strlen((char *)test_inputs[0]),
56 			   VB2_HASH_SHA1, digest, sizeof(digest) - 1),
57 		VB2_ERROR_SHA_FINALIZE_DIGEST_SIZE, "vb2_digest() too small");
58 }
59 
sha256_tests(void)60 void sha256_tests(void)
61 {
62 	uint8_t digest[VB2_SHA256_DIGEST_SIZE];
63 	uint8_t *test_inputs[3];
64 	int i;
65 
66 	test_inputs[0] = (uint8_t *) oneblock_msg;
67 	test_inputs[1] = (uint8_t *) multiblock_msg1;
68 	test_inputs[2] = (uint8_t *) long_msg;
69 
70 	for (i = 0; i < 3; i++) {
71 		TEST_SUCC(vb2_digest(test_inputs[i],
72 				     strlen((char *)test_inputs[i]),
73 				     VB2_HASH_SHA256, digest, sizeof(digest)),
74 			  "vb2_digest() SHA256");
75 		TEST_EQ(memcmp(digest, sha256_results[i], sizeof(digest)),
76 			0, "SHA-256 digest");
77 	}
78 
79 	TEST_EQ(vb2_digest(test_inputs[0], strlen((char *)test_inputs[0]),
80 			   VB2_HASH_SHA256, digest, sizeof(digest) - 1),
81 		VB2_ERROR_SHA_FINALIZE_DIGEST_SIZE, "vb2_digest() too small");
82 }
83 
sha512_tests(void)84 void sha512_tests(void)
85 {
86 	uint8_t digest[VB2_SHA512_DIGEST_SIZE];
87 	uint8_t *test_inputs[3];
88 	int i;
89 
90 	test_inputs[0] = (uint8_t *) oneblock_msg;
91 	test_inputs[1] = (uint8_t *) multiblock_msg2;
92 	test_inputs[2] = (uint8_t *) long_msg;
93 
94 	for (i = 0; i < 3; i++) {
95 		TEST_SUCC(vb2_digest(test_inputs[i],
96 				     strlen((char *)test_inputs[i]),
97 				     VB2_HASH_SHA512, digest,
98 				     sizeof(digest)),
99 			  "vb2_digest() SHA512");
100 		TEST_EQ(memcmp(digest, sha512_results[i], sizeof(digest)),
101 			0, "SHA-512 digest");
102 	}
103 
104 	TEST_EQ(vb2_digest(test_inputs[0], strlen((char *)test_inputs[0]),
105 			   VB2_HASH_SHA512, digest, sizeof(digest) - 1),
106 		VB2_ERROR_SHA_FINALIZE_DIGEST_SIZE, "vb2_digest() too small");
107 }
108 
misc_tests(void)109 void misc_tests(void)
110 {
111 	uint8_t digest[VB2_SHA512_DIGEST_SIZE];
112 	struct vb2_digest_context dc;
113 
114 	/* Crypto algorithm to hash algorithm mapping */
115 	TEST_EQ(vb2_crypto_to_hash(VB2_ALG_RSA1024_SHA1), VB2_HASH_SHA1,
116 		"Crypto map to SHA1");
117 	TEST_EQ(vb2_crypto_to_hash(VB2_ALG_RSA2048_SHA256), VB2_HASH_SHA256,
118 		"Crypto map to SHA256");
119 	TEST_EQ(vb2_crypto_to_hash(VB2_ALG_RSA4096_SHA256), VB2_HASH_SHA256,
120 		"Crypto map to SHA256 2");
121 	TEST_EQ(vb2_crypto_to_hash(VB2_ALG_RSA8192_SHA512), VB2_HASH_SHA512,
122 		"Crypto map to SHA512");
123 	TEST_EQ(vb2_crypto_to_hash(VB2_ALG_COUNT), VB2_HASH_INVALID,
124 		"Crypto map to invalid");
125 
126 	TEST_EQ(vb2_digest_size(VB2_HASH_INVALID), 0,
127 		"digest size invalid alg");
128 
129 	TEST_EQ(vb2_digest((uint8_t *)oneblock_msg, strlen(oneblock_msg),
130 			   VB2_HASH_INVALID, digest, sizeof(digest)),
131 		VB2_ERROR_SHA_INIT_ALGORITHM,
132 		"vb2_digest() invalid alg");
133 
134 	/* Test bad algorithm inside extend and finalize */
135 	vb2_digest_init(&dc, VB2_HASH_SHA256);
136 	dc.hash_alg = VB2_HASH_INVALID;
137 	TEST_EQ(vb2_digest_extend(&dc, digest, sizeof(digest)),
138 		VB2_ERROR_SHA_EXTEND_ALGORITHM,
139 		"vb2_digest_extend() invalid alg");
140 	TEST_EQ(vb2_digest_finalize(&dc, digest, sizeof(digest)),
141 		VB2_ERROR_SHA_FINALIZE_ALGORITHM,
142 		"vb2_digest_finalize() invalid alg");
143 }
144 
main(int argc,char * argv[])145 int main(int argc, char *argv[])
146 {
147 	/* Initialize long_msg with 'a' x 1,000,000 */
148 	long_msg = (char *) malloc(1000001);
149 	memset(long_msg, 'a', 1000000);
150 	long_msg[1000000]=0;
151 
152 	sha1_tests();
153 	sha256_tests();
154 	sha512_tests();
155 	misc_tests();
156 
157 	free(long_msg);
158 
159 	return gTestSuccess ? 0 : 255;
160 }
161