]> git.proxmox.com Git - ceph.git/blob - ceph/src/crypto/isa-l/isa-l_crypto/aes/gcm_nt_std_vectors_test.c
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / crypto / isa-l / isa-l_crypto / aes / gcm_nt_std_vectors_test.c
1 /**********************************************************************
2 Copyright(c) 2011-2017 Intel Corporation All rights reserved.
3
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
6 are met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in
11 the documentation and/or other materials provided with the
12 distribution.
13 * Neither the name of Intel Corporation nor the names of its
14 contributors may be used to endorse or promote products derived
15 from this software without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 **********************************************************************/
29
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <stdint.h>
33 #include <string.h> // for memcmp
34 #include <aes_gcm.h>
35 #include "gcm_vectors.h"
36 #include "types.h"
37
38 #ifndef TEST_SEED
39 # define TEST_SEED 0x1234
40 #endif
41
42 int check_data(uint8_t * test, uint8_t * expected, uint64_t len, char *data_name)
43 {
44 int mismatch;
45 int OK = 0;
46
47 mismatch = memcmp(test, expected, len);
48 if (mismatch) {
49 OK = 1;
50 printf(" expected results don't match %s \t\t", data_name);
51 {
52 uint64_t a;
53 for (a = 0; a < len; a++) {
54 if (test[a] != expected[a]) {
55 printf(" '%x' != '%x' at %lx of %lx\n",
56 test[a], expected[a], a, len);
57 break;
58 }
59 }
60 }
61 }
62 return OK;
63 }
64
65 int test_gcm128_std_vectors_nt(gcm_vector const *vector)
66 {
67 struct gcm_key_data gkey;
68 struct gcm_context_data gctx;
69 int OK = 0;
70 // Temporary array for the calculated vectors
71 uint8_t *ct_test = NULL;
72 uint8_t *pt_test = NULL;
73 uint8_t *IV_c = NULL;
74 uint8_t *T_test = NULL;
75 uint8_t *T2_test = NULL;
76 uint64_t IV_alloc_len = 0;
77 int ret;
78
79 // Allocate space for the calculated ciphertext
80 ret = posix_memalign((void **)&ct_test, 32, vector->Plen);
81 // Allocate space for the calculated plaintext
82 ret |= posix_memalign((void **)&pt_test, 32, vector->Plen);
83 if ((ret != 0) || (ct_test == NULL) || (pt_test == NULL)) {
84 fprintf(stderr, "Can't allocate ciphertext or plaintext memory\n");
85 return 1;
86 }
87 IV_alloc_len = vector->IVlen;
88 // Allocate space for the calculated ciphertext
89 IV_c = malloc(IV_alloc_len);
90 if (IV_c == NULL) {
91 fprintf(stderr, "Can't allocate ciphertext memory\n");
92 return 1;
93 }
94 memcpy(IV_c, vector->IV, vector->IVlen);
95
96 T_test = malloc(vector->Tlen);
97 T2_test = malloc(vector->Tlen);
98 if ((T_test == NULL) || (T2_test == NULL)) {
99 fprintf(stderr, "Can't allocate tag memory\n");
100 return 1;
101 }
102 // This is only required once for a given key
103 aes_gcm_pre_128(vector->K, &gkey);
104
105 ////
106 // ISA-l Encrypt
107 ////
108 memset(ct_test, 0, vector->Plen);
109 memcpy(pt_test, vector->P, vector->Plen);
110 aes_gcm_enc_128_nt(&gkey, &gctx, ct_test, pt_test, vector->Plen,
111 IV_c, vector->A, vector->Alen, T_test, vector->Tlen);
112 OK |= check_data(ct_test, vector->C, vector->Plen, "ISA-L encrypted cypher text (C)");
113 OK |= check_data(T_test, vector->T, vector->Tlen, "ISA-L tag (T)");
114
115 // test of in-place encrypt
116 memcpy(pt_test, vector->P, vector->Plen);
117 aes_gcm_enc_128_nt(&gkey, &gctx, pt_test, pt_test, vector->Plen, IV_c,
118 vector->A, vector->Alen, T_test, vector->Tlen);
119 OK |= check_data(pt_test, vector->C, vector->Plen,
120 "ISA-L encrypted cypher text(in-place)");
121 memset(ct_test, 0, vector->Plen);
122 memset(T_test, 0, vector->Tlen);
123
124 ////
125 // ISA-l Decrypt
126 ////
127 memcpy(ct_test, vector->C, vector->Plen);
128 aes_gcm_dec_128_nt(&gkey, &gctx, pt_test, ct_test, vector->Plen,
129 IV_c, vector->A, vector->Alen, T_test, vector->Tlen);
130 OK |= check_data(pt_test, vector->P, vector->Plen, "ISA-L decrypted plain text (P)");
131 // GCM decryption outputs a 16 byte tag value that must be verified against the expected tag value
132 OK |= check_data(T_test, vector->T, vector->Tlen, "ISA-L decrypted tag (T)");
133
134 // test in in-place decrypt
135 memcpy(ct_test, vector->C, vector->Plen);
136 aes_gcm_dec_128_nt(&gkey, &gctx, ct_test, ct_test, vector->Plen, IV_c,
137 vector->A, vector->Alen, T_test, vector->Tlen);
138 OK |= check_data(ct_test, vector->P, vector->Plen, "ISA-L plain text (P) - in-place");
139 OK |=
140 check_data(T_test, vector->T, vector->Tlen, "ISA-L decrypted tag (T) - in-place");
141 // ISA-L enc -> ISA-L dec
142 memcpy(pt_test, vector->P, vector->Plen);
143 aes_gcm_enc_128_nt(&gkey, &gctx, ct_test, pt_test, vector->Plen,
144 IV_c, vector->A, vector->Alen, T_test, vector->Tlen);
145 memset(pt_test, 0, vector->Plen);
146 aes_gcm_dec_128_nt(&gkey, &gctx, pt_test, ct_test, vector->Plen, IV_c,
147 vector->A, vector->Alen, T2_test, vector->Tlen);
148 OK |=
149 check_data(pt_test, vector->P, vector->Plen,
150 "ISA-L self decrypted plain text (P)");
151 OK |= check_data(T_test, T2_test, vector->Tlen, "ISA-L self decrypted tag (T)");
152
153 memset(pt_test, 0, vector->Plen);
154
155 if (NULL != ct_test)
156 aligned_free(ct_test);
157 if (NULL != pt_test)
158 aligned_free(pt_test);
159 if (NULL != IV_c)
160 free(IV_c);
161 if (NULL != T_test)
162 free(T_test);
163 if (NULL != T2_test)
164 free(T2_test);
165
166 return OK;
167 }
168
169 int test_gcm256_std_vectors_nt(gcm_vector const *vector)
170 {
171 struct gcm_key_data gkey;
172 struct gcm_context_data gctx;
173 int OK = 0;
174 // Temporary array for the calculated vectors
175 uint8_t *ct_test = NULL;
176 uint8_t *pt_test = NULL;
177 uint8_t *IV_c = NULL;
178 uint8_t *T_test = NULL;
179 uint8_t *T2_test = NULL;
180 uint64_t IV_alloc_len = 0;
181 int ret;
182
183 // Allocate space for the calculated ciphertext
184 ret = posix_memalign((void **)&ct_test, 32, vector->Plen);
185 // Allocate space for the calculated plaintext
186 ret |= posix_memalign((void **)&pt_test, 32, vector->Plen);
187 if ((ret != 0) || (ct_test == NULL) || (pt_test == NULL)) {
188 fprintf(stderr, "Can't allocate ciphertext or plaintext memory\n");
189 return 1;
190 }
191 IV_alloc_len = vector->IVlen;
192 // Allocate space for the calculated ciphertext
193 IV_c = malloc(IV_alloc_len);
194 if (IV_c == NULL) {
195 fprintf(stderr, "Can't allocate ciphertext memory\n");
196 return 1;
197 }
198 memcpy(IV_c, vector->IV, vector->IVlen);
199
200 T_test = malloc(vector->Tlen);
201 T2_test = malloc(vector->Tlen);
202 if (T_test == NULL) {
203 fprintf(stderr, "Can't allocate tag memory\n");
204 return 1;
205 }
206 // This is only required once for a given key
207 aes_gcm_pre_256(vector->K, &gkey);
208
209 ////
210 // ISA-l Encrypt
211 ////
212 memset(ct_test, 0, vector->Plen);
213 memcpy(pt_test, vector->P, vector->Plen);
214 aes_gcm_enc_256_nt(&gkey, &gctx, ct_test, pt_test, vector->Plen,
215 IV_c, vector->A, vector->Alen, T_test, vector->Tlen);
216 OK |= check_data(ct_test, vector->C, vector->Plen, "ISA-L encrypted cypher text (C)");
217 OK |= check_data(T_test, vector->T, vector->Tlen, "ISA-L tag (T)");
218
219 // test of in-place encrypt
220 memcpy(pt_test, vector->P, vector->Plen);
221 aes_gcm_enc_256_nt(&gkey, &gctx, pt_test, pt_test, vector->Plen, IV_c,
222 vector->A, vector->Alen, T_test, vector->Tlen);
223 OK |=
224 check_data(pt_test, vector->C, vector->Plen,
225 "ISA-L encrypted cypher text(in-place)");
226 memset(ct_test, 0, vector->Plen);
227 memset(T_test, 0, vector->Tlen);
228
229 ////
230 // ISA-l Decrypt
231 ////
232 memset(pt_test, 0, vector->Plen);
233 memcpy(ct_test, vector->C, vector->Plen);
234 aes_gcm_dec_256_nt(&gkey, &gctx, pt_test, ct_test, vector->Plen,
235 IV_c, vector->A, vector->Alen, T_test, vector->Tlen);
236 OK |= check_data(pt_test, vector->P, vector->Plen, "ISA-L decrypted plain text (P)");
237 // GCM decryption outputs a 16 byte tag value that must be verified against the expected tag value
238 OK |= check_data(T_test, vector->T, vector->Tlen, "ISA-L decrypted tag (T)");
239
240 // test in in-place decrypt
241 memcpy(ct_test, vector->C, vector->Plen);
242 aes_gcm_dec_256_nt(&gkey, &gctx, ct_test, ct_test, vector->Plen, IV_c,
243 vector->A, vector->Alen, T_test, vector->Tlen);
244 OK |= check_data(ct_test, vector->P, vector->Plen, "ISA-L plain text (P) - in-place");
245 OK |=
246 check_data(T_test, vector->T, vector->Tlen, "ISA-L decrypted tag (T) - in-place");
247 // ISA-L enc -> ISA-L dec
248 memcpy(pt_test, vector->P, vector->Plen);
249 aes_gcm_enc_256_nt(&gkey, &gctx, ct_test, pt_test, vector->Plen,
250 IV_c, vector->A, vector->Alen, T_test, vector->Tlen);
251 memset(pt_test, 0, vector->Plen);
252 aes_gcm_dec_256_nt(&gkey, &gctx, pt_test, ct_test, vector->Plen, IV_c,
253 vector->A, vector->Alen, T2_test, vector->Tlen);
254 OK |=
255 check_data(pt_test, vector->P, vector->Plen,
256 "ISA-L self decrypted plain text (P)");
257 OK |= check_data(T_test, T2_test, vector->Tlen, "ISA-L self decrypted tag (T)");
258
259 if (NULL != ct_test)
260 aligned_free(ct_test);
261 if (NULL != pt_test)
262 aligned_free(pt_test);
263 if (NULL != IV_c)
264 free(IV_c);
265 if (NULL != T_test)
266 free(T_test);
267 if (NULL != T2_test)
268 free(T2_test);
269
270 return OK;
271 }
272
273 int test_gcm_std_vectors_nt(void)
274 {
275 int const vectors_cnt = sizeof(gcm_vectors) / sizeof(gcm_vectors[0]);
276 int vect;
277 int OK = 0;
278
279 printf("AES-GCM standard test vectors NT:\n");
280 for (vect = 0; (vect < vectors_cnt); vect++) {
281 #ifdef DEBUG
282 printf("Standard vector NT %d/%d"
283 " Keylen:%d IVlen:%d PTLen:%d AADlen:%d Tlen:%d\n",
284 vect, vectors_cnt - 1, (int)gcm_vectors[vect].Klen,
285 (int)gcm_vectors[vect].IVlen, (int)gcm_vectors[vect].Plen,
286 (int)gcm_vectors[vect].Alen, (int)gcm_vectors[vect].Tlen);
287 #else
288 printf(".");
289 #endif
290 if (BITS_128 == gcm_vectors[vect].Klen)
291 OK |= test_gcm128_std_vectors_nt(&gcm_vectors[vect]);
292 else
293 OK |= test_gcm256_std_vectors_nt(&gcm_vectors[vect]);
294 if (0 != OK)
295 return OK;
296 }
297 printf("\n");
298 return OK;
299 }
300
301 int main(int argc, char **argv)
302 {
303 int errors = 0;
304 int seed;
305
306 if (argc == 1)
307 seed = TEST_SEED;
308 else
309 seed = atoi(argv[1]);
310
311 srand(seed);
312 printf("SEED: %d\n", seed);
313
314 errors += test_gcm_std_vectors_nt();
315
316 if (0 == errors)
317 printf("...Pass\n");
318 else
319 printf("...Fail\n");
320
321 return errors;
322 }