]>
git.proxmox.com Git - ceph.git/blob - 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.
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
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
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.
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 **********************************************************************/
33 #include <string.h> // for memcmp
35 #include "gcm_vectors.h"
39 # define TEST_SEED 0x1234
42 int check_data(uint8_t * test
, uint8_t * expected
, uint64_t len
, char *data_name
)
47 mismatch
= memcmp(test
, expected
, len
);
50 printf(" expected results don't match %s \t\t", data_name
);
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
);
65 int test_gcm128_std_vectors_nt(gcm_vector
const *vector
)
67 struct gcm_key_data gkey
;
68 struct gcm_context_data gctx
;
70 // Temporary array for the calculated vectors
71 uint8_t *ct_test
= NULL
;
72 uint8_t *pt_test
= NULL
;
74 uint8_t *T_test
= NULL
;
75 uint8_t *T2_test
= NULL
;
76 uint64_t IV_alloc_len
= 0;
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");
87 IV_alloc_len
= vector
->IVlen
;
88 // Allocate space for the calculated ciphertext
89 IV_c
= malloc(IV_alloc_len
);
91 fprintf(stderr
, "Can't allocate ciphertext memory\n");
94 memcpy(IV_c
, vector
->IV
, vector
->IVlen
);
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");
102 // This is only required once for a given key
103 aes_gcm_pre_128(vector
->K
, &gkey
);
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)");
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
);
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)");
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");
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
);
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)");
153 memset(pt_test
, 0, vector
->Plen
);
156 aligned_free(ct_test
);
158 aligned_free(pt_test
);
169 int test_gcm256_std_vectors_nt(gcm_vector
const *vector
)
171 struct gcm_key_data gkey
;
172 struct gcm_context_data gctx
;
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;
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");
191 IV_alloc_len
= vector
->IVlen
;
192 // Allocate space for the calculated ciphertext
193 IV_c
= malloc(IV_alloc_len
);
195 fprintf(stderr
, "Can't allocate ciphertext memory\n");
198 memcpy(IV_c
, vector
->IV
, vector
->IVlen
);
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");
206 // This is only required once for a given key
207 aes_gcm_pre_256(vector
->K
, &gkey
);
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)");
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
);
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
);
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)");
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");
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
);
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)");
260 aligned_free(ct_test
);
262 aligned_free(pt_test
);
273 int test_gcm_std_vectors_nt(void)
275 int const vectors_cnt
= sizeof(gcm_vectors
) / sizeof(gcm_vectors
[0]);
279 printf("AES-GCM standard test vectors NT:\n");
280 for (vect
= 0; (vect
< vectors_cnt
); vect
++) {
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
);
290 if (BITS_128
== gcm_vectors
[vect
].Klen
)
291 OK
|= test_gcm128_std_vectors_nt(&gcm_vectors
[vect
]);
293 OK
|= test_gcm256_std_vectors_nt(&gcm_vectors
[vect
]);
301 int main(int argc
, char **argv
)
309 seed
= atoi(argv
[1]);
312 printf("SEED: %d\n", seed
);
314 errors
+= test_gcm_std_vectors_nt();