1 /* Copyright 2014 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
6 #include <stdint.h>
7 #include <stdio.h>
8
9 #include "2common.h"
10 #include "2rsa.h"
11 #include "2rsa_private.h"
12 #include "2sysincludes.h"
13 #include "common/tests.h"
14 #include "file_keys.h"
15 #include "rsa_padding_test.h"
16 #include "vboot_api.h"
17
18 /**
19 * Test RSA utility funcs
20 */
test_utils(void)21 static void test_utils(void)
22 {
23 uint8_t sig[RSA1024NUMBYTES];
24 struct vb2_public_key kbad = {.sig_alg = VB2_SIG_INVALID,
25 .hash_alg = VB2_HASH_INVALID};
26
27 /* Verify old and new algorithm count constants match */
28 TEST_EQ(VB2_ALG_COUNT, VB2_ALG_COUNT, "Algorithm counts");
29
30 /* Crypto algorithm to sig algorithm mapping */
31 TEST_EQ(vb2_crypto_to_signature(VB2_ALG_RSA1024_SHA1),
32 VB2_SIG_RSA1024, "Crypto map to RSA1024");
33 TEST_EQ(vb2_crypto_to_signature(VB2_ALG_RSA2048_SHA256),
34 VB2_SIG_RSA2048, "Crypto map to RSA2048");
35 TEST_EQ(vb2_crypto_to_signature(VB2_ALG_RSA4096_SHA256),
36 VB2_SIG_RSA4096, "Crypto map to RSA4096");
37 TEST_EQ(vb2_crypto_to_signature(VB2_ALG_RSA8192_SHA512),
38 VB2_SIG_RSA8192, "Crypto map to RSA8192");
39 TEST_EQ(vb2_crypto_to_signature(VB2_ALG_COUNT),
40 VB2_SIG_INVALID, "Crypto map to invalid");
41
42 /* Sig size */
43 TEST_EQ(vb2_rsa_sig_size(VB2_SIG_RSA1024), RSA1024NUMBYTES,
44 "Sig size RSA1024");
45 TEST_EQ(vb2_rsa_sig_size(VB2_SIG_RSA2048), RSA2048NUMBYTES,
46 "Sig size RSA2048");
47 TEST_EQ(vb2_rsa_sig_size(VB2_SIG_RSA4096), RSA4096NUMBYTES,
48 "Sig size RSA4096");
49 TEST_EQ(vb2_rsa_sig_size(VB2_SIG_RSA8192), RSA8192NUMBYTES,
50 "Sig size RSA8192");
51 TEST_EQ(vb2_rsa_sig_size(VB2_SIG_RSA2048_EXP3), RSA2048NUMBYTES,
52 "Sig size RSA2048_EXP3");
53 TEST_EQ(vb2_rsa_sig_size(VB2_SIG_RSA3072_EXP3), RSA3072NUMBYTES,
54 "Sig size RSA3072_EXP3");
55 TEST_EQ(vb2_rsa_sig_size(VB2_SIG_INVALID), 0,
56 "Sig size invalid algorithm");
57 TEST_EQ(vb2_rsa_sig_size(VB2_SIG_NONE), 0,
58 "Sig size no signing algorithm");
59
60 /* Packed key size */
61 TEST_EQ(vb2_packed_key_size(VB2_SIG_RSA1024),
62 RSA1024NUMBYTES * 2 + sizeof(uint32_t) * 2,
63 "Packed key size VB2_SIG_RSA1024");
64 TEST_EQ(vb2_packed_key_size(VB2_SIG_RSA2048),
65 RSA2048NUMBYTES * 2 + sizeof(uint32_t) * 2,
66 "Packed key size VB2_SIG_RSA2048");
67 TEST_EQ(vb2_packed_key_size(VB2_SIG_RSA4096),
68 RSA4096NUMBYTES * 2 + sizeof(uint32_t) * 2,
69 "Packed key size VB2_SIG_RSA4096");
70 TEST_EQ(vb2_packed_key_size(VB2_SIG_RSA8192),
71 RSA8192NUMBYTES * 2 + sizeof(uint32_t) * 2,
72 "Packed key size VB2_SIG_RSA8192");
73 TEST_EQ(vb2_packed_key_size(VB2_SIG_RSA2048_EXP3),
74 RSA2048NUMBYTES * 2 + sizeof(uint32_t) * 2,
75 "Packed key size VB2_SIG_RSA2048_EXP3");
76 TEST_EQ(vb2_packed_key_size(VB2_SIG_RSA3072_EXP3),
77 RSA3072NUMBYTES * 2 + sizeof(uint32_t) * 2,
78 "Packed key size VB2_SIG_RSA3072_EXP3");
79 TEST_EQ(vb2_packed_key_size(VB2_SIG_INVALID), 0,
80 "Packed key size invalid algorithm");
81 TEST_EQ(vb2_packed_key_size(VB2_SIG_NONE), 0,
82 "Packed key size no signing algorithm");
83
84 /* Test padding check with bad algorithm */
85 memcpy(sig, signatures[0], sizeof(sig));
86 TEST_EQ(vb2_check_padding(sig, &kbad),
87 VB2_ERROR_RSA_PADDING_SIZE,
88 "vb2_check_padding() bad padding algorithm/size");
89
90 /* Test safe memcmp */
91 TEST_EQ(vb2_safe_memcmp("foo", "foo", 3), 0, "vb2_safe_memcmp() good");
92 TEST_NEQ(vb2_safe_memcmp("foo", "bar", 3), 0, "vb2_safe_memcmp() bad");
93 TEST_EQ(vb2_safe_memcmp("foo", "bar", 0), 0, "vb2_safe_memcmp() zero");
94
95 /* Test Montgomery >= */
96 {
97 uint32_t n[4] = {4, 4, 4, 4};
98 uint32_t a[4] = {4, 4, 4, 4};
99 struct vb2_public_key k = {
100 .arrsize = 4,
101 .n = n,
102 };
103 TEST_EQ(vb2_mont_ge(&k, a), 1, "mont_ge equal");
104
105 a[2] = 3;
106 TEST_EQ(vb2_mont_ge(&k, a), 0, "mont_ge less");
107
108 a[1] = 5;
109 TEST_EQ(vb2_mont_ge(&k, a), 0, "mont_ge greater");
110 }
111 }
112
main(int argc,char * argv[])113 int main(int argc, char* argv[])
114 {
115 /* Run tests */
116 test_utils();
117
118 return gTestSuccess ? 0 : 255;
119 }
120