1 /*
2 * Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the License); you may
5 * not use this file except in compliance with the License.
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 */
9
10
11 #include <stdio.h>
12 #include <string.h>
13 #include <stdlib.h>
14 #include <gmssl/gcm.h>
15 #include <gmssl/hex.h>
16 #include <gmssl/rand.h>
17 #include <gmssl/block_cipher.h>
18 #include <gmssl/error.h>
19
20
21 struct {
22 char *H;
23 char *A;
24 char *C;
25 char *T;
26 } ghash_tests[] = {
27 // test 1
28 {
29 "66e94bd4ef8a2c3b884cfa59ca342b2e",
30 "",
31 "",
32 "00000000000000000000000000000000",
33 },
34 // test 2
35 {
36 "66e94bd4ef8a2c3b884cfa59ca342b2e",
37 "",
38 "0388dace60b6a392f328c2b971b2fe78",
39 "f38cbb1ad69223dcc3457ae5b6b0f885",
40
41 },
42 // test 3
43 {
44 "b83b533708bf535d0aa6e52980d53b78",
45 "",
46 "42831ec2217774244b7221b784d0d49c"
47 "e3aa212f2c02a4e035c17e2329aca12e"
48 "21d514b25466931c7d8f6a5aac84aa05"
49 "1ba30b396a0aac973d58e091473f5985",
50 "7f1b32b81b820d02614f8895ac1d4eac",
51 },
52 // test 4
53 {
54 "b83b533708bf535d0aa6e52980d53b78",
55 "feedfacedeadbeeffeedfacedeadbeef"
56 "abaddad2",
57 "42831ec2217774244b7221b784d0d49c"
58 "e3aa212f2c02a4e035c17e2329aca12e"
59 "21d514b25466931c7d8f6a5aac84aa05"
60 "1ba30b396a0aac973d58e091",
61 "698e57f70e6ecc7fd9463b7260a9ae5f",
62 },
63 // test 5
64 {
65 "b83b533708bf535d0aa6e52980d53b78",
66 "feedfacedeadbeeffeedfacedeadbeef"
67 "abaddad2",
68 "61353b4c2806934a777ff51fa22a4755"
69 "699b2a714fcdc6f83766e5f97b6c7423"
70 "73806900e49f24b22b097544d4896b42"
71 "4989b5e1ebac0f07c23f4598",
72 "df586bb4c249b92cb6922877e444d37b",
73 },
74 // test 6
75 {
76 "b83b533708bf535d0aa6e52980d53b78",
77 "feedfacedeadbeeffeedfacedeadbeef"
78 "abaddad2",
79 "8ce24998625615b603a033aca13fb894"
80 "be9112a5c3a211a8ba262a3cca7e2ca7"
81 "01e4a9a4fba43c90ccdcb281d48c7c6f"
82 "d62875d2aca417034c34aee5",
83 "1c5afe9760d3932f3c9a878aac3dc3de",
84 },
85 };
86
test_ghash(void)87 int test_ghash(void)
88 {
89 uint8_t H[16];
90 uint8_t A[32];
91 uint8_t C[64];
92 uint8_t T[16];
93 uint8_t out[16];
94 size_t Hlen, Alen, Clen, Tlen;
95 int i;
96
97 for (i = 0; i < sizeof(ghash_tests)/sizeof(ghash_tests[0]); i++) {
98 hex_to_bytes(ghash_tests[i].H, strlen(ghash_tests[i].H), H, &Hlen);
99 hex_to_bytes(ghash_tests[i].A, strlen(ghash_tests[i].A), A, &Alen);
100 hex_to_bytes(ghash_tests[i].C, strlen(ghash_tests[i].C), C, &Clen);
101 hex_to_bytes(ghash_tests[i].T, strlen(ghash_tests[i].T), T, &Tlen);
102 ghash(H, A, Alen, C, Clen, out);
103
104 if (memcmp(out, T, Tlen) != 0) {
105 format_print(stderr, 0, 0, "test %d failed\n", i + 1);
106 format_print(stderr, 0, 2, "H = %s\n", ghash_tests[i].H);
107 format_print(stderr, 0, 2, "A = %s\n", ghash_tests[i].A);
108 format_print(stderr, 0, 2, "C = %s\n", ghash_tests[i].C);
109 format_bytes(stderr, 0, 2, "GHASH(H,A,C) = ", out, 16);
110 format_print(stderr, 0, 2, " = %s\n\n", ghash_tests[i].T);
111 }
112 }
113
114 printf("%s() ok\n", __FUNCTION__);
115 return 1;
116 }
117
test_gcm(void)118 int test_gcm(void)
119 {
120 BLOCK_CIPHER_KEY block_key;
121 uint8_t key[16];
122 uint8_t iv[12];
123 uint8_t aad[64];
124 uint8_t in[100];
125 uint8_t out[sizeof(in)];
126 uint8_t buf[sizeof(in)];
127 uint8_t tag[16];
128
129 rand_bytes(key, sizeof(key));
130 rand_bytes(iv, sizeof(iv));
131 rand_bytes(aad, sizeof(aad));
132 rand_bytes(in, sizeof(in));
133
134 memset(out, 0, sizeof(out));
135 memset(buf, 0, sizeof(buf));
136 memset(tag, 0, sizeof(tag));
137
138 if (block_cipher_set_encrypt_key(&block_key, BLOCK_CIPHER_aes128(), key) != 1) {
139 error_print();
140 return -1;
141 }
142 if (gcm_encrypt(&block_key, iv, sizeof(iv), aad, sizeof(aad), in, sizeof(in), out, sizeof(tag), tag) != 1) {
143 error_print();
144 return -1;
145 }
146 if (gcm_decrypt(&block_key, iv, sizeof(iv), aad, sizeof(aad), out, sizeof(out), tag, sizeof(tag), buf) != 1) {
147 error_print();
148 return -1;
149 }
150 if (memcmp(buf, in, sizeof(in)) != 0) {
151 error_print();
152 return -1;
153 }
154
155 memset(out, 0, sizeof(out));
156 memset(buf, 0, sizeof(buf));
157 memset(tag, 0, sizeof(tag));
158
159 if (block_cipher_set_encrypt_key(&block_key, BLOCK_CIPHER_sm4(), key) != 1) {
160 error_print();
161 return -1;
162 }
163 if (gcm_encrypt(&block_key, iv, sizeof(iv), aad, sizeof(aad), in, sizeof(in), out, sizeof(tag), tag) != 1) {
164 error_print();
165 return -1;
166 }
167 if (gcm_decrypt(&block_key, iv, sizeof(iv), aad, sizeof(aad), out, sizeof(out), tag, sizeof(tag), buf) != 1) {
168 error_print();
169 return -1;
170 }
171 if (memcmp(buf, in, sizeof(in)) != 0) {
172 error_print();
173 return -1;
174 }
175
176 printf("%s() ok\n", __FUNCTION__);
177 return 1;
178 }
179
180
181
182
183
main(int argc,char ** argv)184 int main(int argc, char **argv)
185 {
186 if (test_ghash() != 1) goto err;
187 if (test_gcm() != 1) goto err;
188 printf("%s all tests passed\n", __FILE__);
189 return 0;
190 err:
191 error_print();
192 return -1;
193 }
194