xref: /aosp_15_r20/external/vboot_reference/tests/vb2_crypto_tests.c (revision 8617a60d3594060b7ecbd21bc622a7c14f3cf2bc)
1 /* Copyright 2020 The ChromiumOS Authors
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 generic crypto algorithm helper stuff.
6  */
7 
8 #include <stdio.h>
9 
10 #include "2crypto.h"
11 #include "2return_codes.h"
12 #include "2rsa.h"
13 #include "2sha.h"
14 #include "2sysincludes.h"
15 #include "common/tests.h"
16 #include "vboot_host.h"
17 
hash_algorithm_name_tests(void)18 static void hash_algorithm_name_tests(void)
19 {
20 	enum vb2_hash_algorithm alg;
21 	char test_name[256];
22 
23 	for (alg = 0; alg < VB2_HASH_ALG_COUNT; alg++) {
24 		sprintf(test_name, "%s (alg=%d)",
25 			vb2_get_hash_algorithm_name(alg), alg);
26 		TEST_STR_NEQ(vb2_get_hash_algorithm_name(alg),
27 			     VB2_INVALID_ALG_NAME, test_name);
28 	}
29 
30 	TEST_STR_EQ(vb2_get_hash_algorithm_name(VB2_HASH_ALG_COUNT),
31 		    VB2_INVALID_ALG_NAME, "hash alg name out of range");
32 }
33 
sig_algorithm_name_tests(void)34 static void sig_algorithm_name_tests(void)
35 {
36 	enum vb2_signature_algorithm alg;
37 	char test_name[256];
38 
39 	for (alg = 1; alg < VB2_SIG_ALG_COUNT; alg++) {
40 		sprintf(test_name, "%s (alg=%d)",
41 			vb2_get_sig_algorithm_name(alg), alg);
42 		TEST_STR_NEQ(vb2_get_sig_algorithm_name(alg),
43 			     VB2_INVALID_ALG_NAME, test_name);
44 	}
45 
46 	TEST_STR_EQ(vb2_get_sig_algorithm_name(VB2_SIG_INVALID),
47 		    VB2_INVALID_ALG_NAME, "sig alg name invalid");
48 	TEST_STR_EQ(vb2_get_sig_algorithm_name(VB2_SIG_ALG_COUNT),
49 		    VB2_INVALID_ALG_NAME, "sig alg name out of range");
50 }
51 
hash_algorithm_lookup_tests(void)52 static void hash_algorithm_lookup_tests(void)
53 {
54 	enum vb2_hash_algorithm alg, out;
55 	char test_name[256];
56 
57 	for (alg = 1; alg < VB2_HASH_ALG_COUNT; alg++) {
58 		sprintf(test_name, "%s (alg=%d)",
59 			vb2_get_hash_algorithm_name(alg), alg);
60 		TEST_EQ(vb2_lookup_hash_alg(vb2_get_hash_algorithm_name(alg),
61 			&out), 1, test_name);
62 		TEST_EQ(out, alg, "  algorithm correct");
63 	}
64 
65 	TEST_EQ(vb2_lookup_hash_alg(VB2_INVALID_ALG_NAME, &out), 0,
66 		"invalid algorithm cannot be parsed");
67 	TEST_EQ(vb2_lookup_hash_alg("RSA1024", &out), 0,
68 		"cannot parse hash  as signature");
69 	TEST_EQ(vb2_lookup_hash_alg("shA512", &out), 1,
70 		"case insensitivity spot check");
71 	TEST_EQ(out, VB2_HASH_SHA512, "  algorithm correct");
72 }
73 
sig_algorithm_lookup_tests(void)74 static void sig_algorithm_lookup_tests(void)
75 {
76 	enum vb2_signature_algorithm alg, out;
77 	char test_name[256];
78 
79 	for (alg = 1; alg < VB2_SIG_ALG_COUNT; alg++) {
80 		sprintf(test_name, "%s (alg=%d)",
81 			vb2_get_sig_algorithm_name(alg), alg);
82 		TEST_EQ(vb2_lookup_sig_alg(vb2_get_sig_algorithm_name(alg),
83 			&out), 1, test_name);
84 		TEST_EQ(out, alg, "  algorithm correct");
85 	}
86 
87 	TEST_EQ(vb2_lookup_sig_alg(VB2_INVALID_ALG_NAME, &out), 0,
88 		"invalid algorithm cannot be parsed");
89 	TEST_EQ(vb2_lookup_sig_alg("SHA256", &out), 0,
90 		"cannot parse signature as hash");
91 	TEST_EQ(vb2_lookup_sig_alg("rSa2048", &out), 1,
92 		"case insensitivity spot check");
93 	TEST_EQ(out, VB2_SIG_RSA2048, "  algorithm correct");
94 }
95 
main(int argc,char * argv[])96 int main(int argc, char *argv[])
97 {
98 	hash_algorithm_name_tests();
99 	sig_algorithm_name_tests();
100 	hash_algorithm_lookup_tests();
101 	sig_algorithm_lookup_tests();
102 
103 	return gTestSuccess ? 0 : 255;
104 }
105