xref: /isa-l_crypto/aes/gcm_nt_std_vectors_test.c (revision 38e16e11defa07d24d9d593354096a7ef33c75b7)
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 #include "gcm_test_alloc.h"
38 
39 #ifndef TEST_SEED
40 #define TEST_SEED 0x1234
41 #endif
42 
43 #define NT_ALIGNMENT 64
44 
45 int
46 check_data(uint8_t *test, uint8_t *expected, uint64_t len, char *data_name)
47 {
48         int mismatch;
49         int OK = 0;
50 
51         mismatch = memcmp(test, expected, len);
52         if (mismatch) {
53                 OK = 1;
54                 printf("  expected results don't match %s \t\t", data_name);
55                 {
56                         uint64_t a;
57                         for (a = 0; a < len; a++) {
58                                 if (test[a] != expected[a]) {
59                                         printf(" '%x' != '%x' at %lx of %lx\n", test[a],
60                                                expected[a], a, len);
61                                         break;
62                                 }
63                         }
64                 }
65         }
66         return OK;
67 }
68 
69 int
70 test_gcm128_std_vectors_nt(gcm_vector const *vector)
71 {
72         struct gcm_key_data gkey;
73         struct gcm_context_data gctx;
74         int OK = 0;
75         // Temporary array for the calculated vectors
76         uint8_t *ct_test = NULL;
77         uint8_t *pt_test = NULL;
78         uint8_t *IV_c = NULL;
79         uint8_t *T_test = NULL;
80         uint8_t *T2_test = NULL;
81 
82         // Allocate required memory
83         void **alloc_tab[] = { (void **) &pt_test, (void **) &ct_test, (void **) &IV_c,
84                                (void **) &T_test, (void **) &T2_test };
85         const size_t align_tab[] = { NT_ALIGNMENT, NT_ALIGNMENT, 0, 0, 0 };
86         const size_t length_tab[] = { vector->Plen, vector->Plen, vector->IVlen, vector->Tlen,
87                                       vector->Tlen };
88 
89         if (vector_allocate(alloc_tab, length_tab, align_tab, DIM(alloc_tab)) != 0) {
90                 vector_free(alloc_tab, align_tab, DIM(alloc_tab));
91                 return 1;
92         }
93 
94         memcpy(IV_c, vector->IV, vector->IVlen);
95 
96         // This is only required once for a given key
97         aes_gcm_pre_128(vector->K, &gkey);
98 
99         ////
100         // ISA-l Encrypt
101         ////
102         memset(ct_test, 0, vector->Plen);
103         memcpy(pt_test, vector->P, vector->Plen);
104         aes_gcm_enc_128_nt(&gkey, &gctx, ct_test, pt_test, vector->Plen, IV_c, vector->A,
105                            vector->Alen, T_test, vector->Tlen);
106         OK |= check_data(ct_test, vector->C, vector->Plen, "ISA-L encrypted cypher text (C)");
107         OK |= check_data(T_test, vector->T, vector->Tlen, "ISA-L tag (T)");
108 
109         // test of in-place encrypt
110         memcpy(pt_test, vector->P, vector->Plen);
111         aes_gcm_enc_128_nt(&gkey, &gctx, pt_test, pt_test, vector->Plen, IV_c, vector->A,
112                            vector->Alen, T_test, vector->Tlen);
113         OK |= check_data(pt_test, vector->C, vector->Plen, "ISA-L encrypted cypher text(in-place)");
114         memset(ct_test, 0, vector->Plen);
115         memset(T_test, 0, vector->Tlen);
116 
117         ////
118         // ISA-l Decrypt
119         ////
120         memcpy(ct_test, vector->C, vector->Plen);
121         aes_gcm_dec_128_nt(&gkey, &gctx, pt_test, ct_test, vector->Plen, IV_c, vector->A,
122                            vector->Alen, T_test, vector->Tlen);
123         OK |= check_data(pt_test, vector->P, vector->Plen, "ISA-L decrypted plain text (P)");
124         // GCM decryption outputs a 16 byte tag value that must be verified against the expected tag
125         // value
126         OK |= check_data(T_test, vector->T, vector->Tlen, "ISA-L decrypted tag (T)");
127 
128         // test in in-place decrypt
129         memcpy(ct_test, vector->C, vector->Plen);
130         aes_gcm_dec_128_nt(&gkey, &gctx, ct_test, ct_test, vector->Plen, IV_c, vector->A,
131                            vector->Alen, T_test, vector->Tlen);
132         OK |= check_data(ct_test, vector->P, vector->Plen, "ISA-L plain text (P) - in-place");
133         OK |= check_data(T_test, vector->T, vector->Tlen, "ISA-L decrypted tag (T) - in-place");
134         // ISA-L enc -> ISA-L dec
135         memcpy(pt_test, vector->P, vector->Plen);
136         aes_gcm_enc_128_nt(&gkey, &gctx, ct_test, pt_test, vector->Plen, IV_c, vector->A,
137                            vector->Alen, T_test, vector->Tlen);
138         memset(pt_test, 0, vector->Plen);
139         aes_gcm_dec_128_nt(&gkey, &gctx, pt_test, ct_test, vector->Plen, IV_c, vector->A,
140                            vector->Alen, T2_test, vector->Tlen);
141         OK |= check_data(pt_test, vector->P, vector->Plen, "ISA-L self decrypted plain text (P)");
142         OK |= check_data(T_test, T2_test, vector->Tlen, "ISA-L self decrypted tag (T)");
143 
144         vector_free(alloc_tab, align_tab, DIM(alloc_tab));
145         return OK;
146 }
147 
148 int
149 test_gcm256_std_vectors_nt(gcm_vector const *vector)
150 {
151         struct gcm_key_data gkey;
152         struct gcm_context_data gctx;
153         int OK = 0;
154         // Temporary array for the calculated vectors
155         uint8_t *ct_test = NULL;
156         uint8_t *pt_test = NULL;
157         uint8_t *IV_c = NULL;
158         uint8_t *T_test = NULL;
159         uint8_t *T2_test = NULL;
160 
161         // Allocate required memory
162         void **alloc_tab[] = { (void **) &pt_test, (void **) &ct_test, (void **) &IV_c,
163                                (void **) &T_test, (void **) &T2_test };
164         const size_t align_tab[] = { NT_ALIGNMENT, NT_ALIGNMENT, 0, 0, 0 };
165         const size_t length_tab[] = { vector->Plen, vector->Plen, vector->IVlen, vector->Tlen,
166                                       vector->Tlen };
167 
168         if (vector_allocate(alloc_tab, length_tab, align_tab, DIM(alloc_tab)) != 0) {
169                 vector_free(alloc_tab, align_tab, DIM(alloc_tab));
170                 return 1;
171         }
172 
173         memcpy(IV_c, vector->IV, vector->IVlen);
174 
175         // This is only required once for a given key
176         aes_gcm_pre_256(vector->K, &gkey);
177 
178         ////
179         // ISA-l Encrypt
180         ////
181         memset(ct_test, 0, vector->Plen);
182         memcpy(pt_test, vector->P, vector->Plen);
183         aes_gcm_enc_256_nt(&gkey, &gctx, ct_test, pt_test, vector->Plen, IV_c, vector->A,
184                            vector->Alen, T_test, vector->Tlen);
185         OK |= check_data(ct_test, vector->C, vector->Plen, "ISA-L encrypted cypher text (C)");
186         OK |= check_data(T_test, vector->T, vector->Tlen, "ISA-L tag (T)");
187 
188         // test of in-place encrypt
189         memcpy(pt_test, vector->P, vector->Plen);
190         aes_gcm_enc_256_nt(&gkey, &gctx, pt_test, pt_test, vector->Plen, IV_c, vector->A,
191                            vector->Alen, T_test, vector->Tlen);
192         OK |= check_data(pt_test, vector->C, vector->Plen, "ISA-L encrypted cypher text(in-place)");
193         memset(ct_test, 0, vector->Plen);
194         memset(T_test, 0, vector->Tlen);
195 
196         ////
197         // ISA-l Decrypt
198         ////
199         memset(pt_test, 0, vector->Plen);
200         memcpy(ct_test, vector->C, vector->Plen);
201         aes_gcm_dec_256_nt(&gkey, &gctx, pt_test, ct_test, vector->Plen, IV_c, vector->A,
202                            vector->Alen, T_test, vector->Tlen);
203         OK |= check_data(pt_test, vector->P, vector->Plen, "ISA-L decrypted plain text (P)");
204         // GCM decryption outputs a 16 byte tag value that must be verified against the expected tag
205         // value
206         OK |= check_data(T_test, vector->T, vector->Tlen, "ISA-L decrypted tag (T)");
207 
208         // test in in-place decrypt
209         memcpy(ct_test, vector->C, vector->Plen);
210         aes_gcm_dec_256_nt(&gkey, &gctx, ct_test, ct_test, vector->Plen, IV_c, vector->A,
211                            vector->Alen, T_test, vector->Tlen);
212         OK |= check_data(ct_test, vector->P, vector->Plen, "ISA-L plain text (P) - in-place");
213         OK |= check_data(T_test, vector->T, vector->Tlen, "ISA-L decrypted tag (T) - in-place");
214         // ISA-L enc -> ISA-L dec
215         memcpy(pt_test, vector->P, vector->Plen);
216         aes_gcm_enc_256_nt(&gkey, &gctx, ct_test, pt_test, vector->Plen, IV_c, vector->A,
217                            vector->Alen, T_test, vector->Tlen);
218         memset(pt_test, 0, vector->Plen);
219         aes_gcm_dec_256_nt(&gkey, &gctx, pt_test, ct_test, vector->Plen, IV_c, vector->A,
220                            vector->Alen, T2_test, vector->Tlen);
221         OK |= check_data(pt_test, vector->P, vector->Plen, "ISA-L self decrypted plain text (P)");
222         OK |= check_data(T_test, T2_test, vector->Tlen, "ISA-L self decrypted tag (T)");
223         vector_free(alloc_tab, align_tab, DIM(alloc_tab));
224         return OK;
225 }
226 
227 int
228 test_gcm_std_vectors_nt(void)
229 {
230         int const vectors_cnt = sizeof(gcm_vectors) / sizeof(gcm_vectors[0]);
231         int vect;
232         int OK = 0;
233 
234         printf("AES-GCM standard test vectors NT:\n");
235         for (vect = 0; (vect < vectors_cnt); vect++) {
236 #ifdef DEBUG
237                 printf("Standard vector NT %d/%d"
238                        "  Keylen:%d IVlen:%d PTLen:%d AADlen:%d Tlen:%d\n",
239                        vect, vectors_cnt - 1, (int) gcm_vectors[vect].Klen,
240                        (int) gcm_vectors[vect].IVlen, (int) gcm_vectors[vect].Plen,
241                        (int) gcm_vectors[vect].Alen, (int) gcm_vectors[vect].Tlen);
242 #else
243                 printf(".");
244 #endif
245                 if (BITS_128 == gcm_vectors[vect].Klen)
246                         OK |= test_gcm128_std_vectors_nt(&gcm_vectors[vect]);
247                 else
248                         OK |= test_gcm256_std_vectors_nt(&gcm_vectors[vect]);
249                 if (0 != OK)
250                         return OK;
251         }
252         printf("\n");
253         return OK;
254 }
255 
256 int
257 main(int argc, char **argv)
258 {
259         int errors = 0;
260         int seed;
261 
262         if (argc == 1)
263                 seed = TEST_SEED;
264         else
265                 seed = atoi(argv[1]);
266 
267         srand(seed);
268         printf("SEED: %d\n", seed);
269 
270         errors += test_gcm_std_vectors_nt();
271 
272         if (0 == errors)
273                 printf("...Pass\n");
274         else
275                 printf("...Fail\n");
276 
277         return errors;
278 }
279