uwrap: Small optimalization of uwrap_init().
[Samba.git] / lib / crypto / aes_gcm_128_test.c
blob703ad8698cdb4c02b3320e873dea62eef972dd8f
1 /*
2 AES-GCM-128 tests
4 Copyright (C) Stefan Metzmacher 2014
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "replace.h"
20 #include "../lib/util/samba_util.h"
21 #include "../lib/crypto/crypto.h"
23 struct torture_context;
24 bool torture_local_crypto_aes_gcm_128(struct torture_context *torture);
27 This uses the test values from ...
29 bool torture_local_crypto_aes_gcm_128(struct torture_context *torture)
31 bool ret = true;
32 uint32_t i;
33 struct {
34 DATA_BLOB K;
35 DATA_BLOB IV;
36 DATA_BLOB A;
37 DATA_BLOB P;
38 DATA_BLOB C;
39 DATA_BLOB T;
40 } testarray[5];
42 TALLOC_CTX *tctx = talloc_new(torture);
43 if (!tctx) { return false; };
45 ZERO_STRUCT(testarray);
47 testarray[0].K = strhex_to_data_blob(tctx,
48 "00000000000000000000000000000000");
49 testarray[0].IV = strhex_to_data_blob(tctx,
50 "000000000000000000000000");
51 testarray[0].A = data_blob_null;
52 testarray[0].P = data_blob_null;
53 testarray[0].C = data_blob_null;
54 testarray[0].T = strhex_to_data_blob(tctx,
55 "58e2fccefa7e3061367f1d57a4e7455a");
57 testarray[1].K = strhex_to_data_blob(tctx,
58 "00000000000000000000000000000000");
59 testarray[1].IV = strhex_to_data_blob(tctx,
60 "000000000000000000000000");
61 testarray[1].A = data_blob_null;
62 testarray[1].P = strhex_to_data_blob(tctx,
63 "00000000000000000000000000000000");
64 testarray[1].C = strhex_to_data_blob(tctx,
65 "0388dace60b6a392f328c2b971b2fe78");
66 testarray[1].T = strhex_to_data_blob(tctx,
67 "ab6e47d42cec13bdf53a67b21257bddf");
69 testarray[2].K = strhex_to_data_blob(tctx,
70 "feffe9928665731c6d6a8f9467308308");
71 testarray[2].IV = strhex_to_data_blob(tctx,
72 "cafebabefacedbaddecaf888");
73 testarray[2].A = data_blob_null;
74 testarray[2].P = strhex_to_data_blob(tctx,
75 "d9313225f88406e5a55909c5aff5269a"
76 "86a7a9531534f7da2e4c303d8a318a72"
77 "1c3c0c95956809532fcf0e2449a6b525"
78 "b16aedf5aa0de657ba637b391aafd255");
79 testarray[2].C = strhex_to_data_blob(tctx,
80 "42831ec2217774244b7221b784d0d49c"
81 "e3aa212f2c02a4e035c17e2329aca12e"
82 "21d514b25466931c7d8f6a5aac84aa05"
83 "1ba30b396a0aac973d58e091473f5985");
84 testarray[2].T = strhex_to_data_blob(tctx,
85 "4d5c2af327cd64a62cf35abd2ba6fab4");
87 testarray[3].K = strhex_to_data_blob(tctx,
88 "feffe9928665731c6d6a8f9467308308");
89 testarray[3].IV = strhex_to_data_blob(tctx,
90 "cafebabefacedbaddecaf888");
91 testarray[3].A = strhex_to_data_blob(tctx,
92 "feedfacedeadbeeffeedfacedeadbeef"
93 "abaddad2");
94 testarray[3].P = strhex_to_data_blob(tctx,
95 "d9313225f88406e5a55909c5aff5269a"
96 "86a7a9531534f7da2e4c303d8a318a72"
97 "1c3c0c95956809532fcf0e2449a6b525"
98 "b16aedf5aa0de657ba637b39");
99 testarray[3].C = strhex_to_data_blob(tctx,
100 "42831ec2217774244b7221b784d0d49c"
101 "e3aa212f2c02a4e035c17e2329aca12e"
102 "21d514b25466931c7d8f6a5aac84aa05"
103 "1ba30b396a0aac973d58e091");
104 testarray[3].T = strhex_to_data_blob(tctx,
105 "5bc94fbc3221a5db94fae95ae7121a47");
107 for (i=1; testarray[i].T.length != 0; i++) {
108 struct aes_gcm_128_context ctx;
109 uint8_t T[AES_BLOCK_SIZE];
110 DATA_BLOB C;
111 int e;
113 C = data_blob_dup_talloc(tctx, testarray[i].P);
115 aes_gcm_128_init(&ctx, testarray[i].K.data, testarray[i].IV.data);
116 aes_gcm_128_updateA(&ctx,
117 testarray[i].A.data,
118 testarray[i].A.length);
119 aes_gcm_128_crypt(&ctx, C.data, C.length);
120 aes_gcm_128_updateC(&ctx, C.data, C.length);
121 aes_gcm_128_digest(&ctx, T);
123 e = memcmp(testarray[i].T.data, T, sizeof(T));
124 if (e != 0) {
125 printf("%s: aes_gcm_128 test[%u]: failed\n", __location__, i);
126 printf("K\n");
127 dump_data(0, testarray[i].K.data, testarray[i].K.length);
128 printf("IV\n");
129 dump_data(0, testarray[i].IV.data, testarray[i].IV.length);
130 printf("A\n");
131 dump_data(0, testarray[i].A.data, testarray[i].A.length);
132 printf("P\n");
133 dump_data(0, testarray[i].P.data, testarray[i].P.length);
134 printf("C1\n");
135 dump_data(0, testarray[i].C.data, testarray[i].C.length);
136 printf("C2\n");
137 dump_data(0, C.data, C.length);
138 printf("T1\n");
139 dump_data(0, testarray[i].T.data, testarray[i].T.length);
140 printf("T2\n");
141 dump_data(0, T, sizeof(T));
142 ret = false;
143 goto fail;
146 e = memcmp(testarray[i].C.data, C.data, C.length);
147 if (e != 0) {
148 printf("%s: aes_gcm_128 test[%u]: failed\n", __location__, i);
149 printf("K\n");
150 dump_data(0, testarray[i].K.data, testarray[i].K.length);
151 printf("IV\n");
152 dump_data(0, testarray[i].IV.data, testarray[i].IV.length);
153 printf("A\n");
154 dump_data(0, testarray[i].A.data, testarray[i].A.length);
155 printf("P\n");
156 dump_data(0, testarray[i].P.data, testarray[i].P.length);
157 printf("C1\n");
158 dump_data(0, testarray[i].C.data, testarray[i].C.length);
159 printf("C2\n");
160 dump_data(0, C.data, C.length);
161 printf("T1\n");
162 dump_data(0, testarray[i].T.data, testarray[i].T.length);
163 printf("T2\n");
164 dump_data(0, T, sizeof(T));
165 ret = false;
166 goto fail;
170 for (i=1; testarray[i].T.length != 0; i++) {
171 struct aes_gcm_128_context ctx;
172 uint8_t T[AES_BLOCK_SIZE];
173 DATA_BLOB C;
174 int e;
175 size_t j;
177 C = data_blob_dup_talloc(tctx, testarray[i].P);
179 aes_gcm_128_init(&ctx, testarray[i].K.data, testarray[i].IV.data);
180 for (j=0; j < testarray[i].A.length; j++) {
181 aes_gcm_128_updateA(&ctx, &testarray[i].A.data[j], 1);
183 for (j=0; j < C.length; j++) {
184 aes_gcm_128_crypt(&ctx, &C.data[j], 1);
185 aes_gcm_128_updateC(&ctx, &C.data[j], 1);
187 aes_gcm_128_digest(&ctx, T);
189 e = memcmp(testarray[i].T.data, T, sizeof(T));
190 if (e != 0) {
191 printf("%s: aes_gcm_128 test[%u]: failed\n", __location__, i);
192 printf("K\n");
193 dump_data(0, testarray[i].K.data, testarray[i].K.length);
194 printf("IV\n");
195 dump_data(0, testarray[i].IV.data, testarray[i].IV.length);
196 printf("A\n");
197 dump_data(0, testarray[i].A.data, testarray[i].A.length);
198 printf("P\n");
199 dump_data(0, testarray[i].P.data, testarray[i].P.length);
200 printf("C1\n");
201 dump_data(0, testarray[i].C.data, testarray[i].C.length);
202 printf("C2\n");
203 dump_data(0, C.data, C.length);
204 printf("T1\n");
205 dump_data(0, testarray[i].T.data, testarray[i].T.length);
206 printf("T2\n");
207 dump_data(0, T, sizeof(T));
208 ret = false;
209 goto fail;
212 e = memcmp(testarray[i].C.data, C.data, C.length);
213 if (e != 0) {
214 printf("%s: aes_gcm_128 test[%u]: failed\n", __location__, i);
215 printf("K\n");
216 dump_data(0, testarray[i].K.data, testarray[i].K.length);
217 printf("IV\n");
218 dump_data(0, testarray[i].IV.data, testarray[i].IV.length);
219 printf("A\n");
220 dump_data(0, testarray[i].A.data, testarray[i].A.length);
221 printf("P\n");
222 dump_data(0, testarray[i].P.data, testarray[i].P.length);
223 printf("C1\n");
224 dump_data(0, testarray[i].C.data, testarray[i].C.length);
225 printf("C2\n");
226 dump_data(0, C.data, C.length);
227 printf("T1\n");
228 dump_data(0, testarray[i].T.data, testarray[i].T.length);
229 printf("T2\n");
230 dump_data(0, T, sizeof(T));
231 ret = false;
232 goto fail;
236 for (i=1; testarray[i].T.length != 0; i++) {
237 struct aes_gcm_128_context ctx;
238 uint8_t T[AES_BLOCK_SIZE];
239 DATA_BLOB P;
240 int e;
241 size_t j;
243 P = data_blob_dup_talloc(tctx, testarray[i].C);
245 aes_gcm_128_init(&ctx, testarray[i].K.data, testarray[i].IV.data);
246 for (j=0; j < testarray[i].A.length; j++) {
247 aes_gcm_128_updateA(&ctx, &testarray[i].A.data[j], 1);
249 for (j=0; j < P.length; j++) {
250 aes_gcm_128_updateC(&ctx, &P.data[j], 1);
251 aes_gcm_128_crypt(&ctx, &P.data[j], 1);
253 aes_gcm_128_digest(&ctx, T);
255 e = memcmp(testarray[i].T.data, T, sizeof(T));
256 if (e != 0) {
257 printf("%s: aes_gcm_128 test[%u]: failed\n", __location__, i);
258 printf("K\n");
259 dump_data(0, testarray[i].K.data, testarray[i].K.length);
260 printf("IV\n");
261 dump_data(0, testarray[i].IV.data, testarray[i].IV.length);
262 printf("A\n");
263 dump_data(0, testarray[i].A.data, testarray[i].A.length);
264 printf("P1\n");
265 dump_data(0, testarray[i].P.data, testarray[i].P.length);
266 printf("P2\n");
267 dump_data(0, P.data, P.length);
268 printf("C\n");
269 dump_data(0, testarray[i].C.data, testarray[i].C.length);
270 printf("T1\n");
271 dump_data(0, testarray[i].T.data, testarray[i].T.length);
272 printf("T2\n");
273 dump_data(0, T, sizeof(T));
274 ret = false;
275 goto fail;
278 e = memcmp(testarray[i].P.data, P.data, P.length);
279 if (e != 0) {
280 printf("%s: aes_gcm_128 test[%u]: failed\n", __location__, i);
281 printf("K\n");
282 dump_data(0, testarray[i].K.data, testarray[i].K.length);
283 printf("IV\n");
284 dump_data(0, testarray[i].IV.data, testarray[i].IV.length);
285 printf("A\n");
286 dump_data(0, testarray[i].A.data, testarray[i].A.length);
287 printf("P1\n");
288 dump_data(0, testarray[i].P.data, testarray[i].P.length);
289 printf("P2\n");
290 dump_data(0, P.data, P.length);
291 printf("C\n");
292 dump_data(0, testarray[i].C.data, testarray[i].C.length);
293 printf("T1\n");
294 dump_data(0, testarray[i].T.data, testarray[i].T.length);
295 printf("T2\n");
296 dump_data(0, T, sizeof(T));
297 ret = false;
298 goto fail;
302 for (i=1; testarray[i].T.length != 0; i++) {
303 struct aes_gcm_128_context ctx;
304 uint8_t T[AES_BLOCK_SIZE];
305 DATA_BLOB P;
306 int e;
308 P = data_blob_dup_talloc(tctx, testarray[i].C);
310 aes_gcm_128_init(&ctx, testarray[i].K.data, testarray[i].IV.data);
311 aes_gcm_128_updateA(&ctx, testarray[i].A.data, testarray[i].A.length);
312 aes_gcm_128_updateC(&ctx, P.data, P.length);
313 aes_gcm_128_crypt(&ctx, P.data, P.length);
314 aes_gcm_128_digest(&ctx, T);
316 e = memcmp(testarray[i].T.data, T, sizeof(T));
317 if (e != 0) {
318 printf("%s: aes_gcm_128 test[%u]: failed\n", __location__, i);
319 printf("K\n");
320 dump_data(0, testarray[i].K.data, testarray[i].K.length);
321 printf("IV\n");
322 dump_data(0, testarray[i].IV.data, testarray[i].IV.length);
323 printf("A\n");
324 dump_data(0, testarray[i].A.data, testarray[i].A.length);
325 printf("P1\n");
326 dump_data(0, testarray[i].P.data, testarray[i].P.length);
327 printf("P2\n");
328 dump_data(0, P.data, P.length);
329 printf("C\n");
330 dump_data(0, testarray[i].C.data, testarray[i].C.length);
331 printf("T1\n");
332 dump_data(0, testarray[i].T.data, testarray[i].T.length);
333 printf("T2\n");
334 dump_data(0, T, sizeof(T));
335 ret = false;
336 goto fail;
339 e = memcmp(testarray[i].P.data, P.data, P.length);
340 if (e != 0) {
341 printf("%s: aes_gcm_128 test[%u]: failed\n", __location__, i);
342 printf("K\n");
343 dump_data(0, testarray[i].K.data, testarray[i].K.length);
344 printf("IV\n");
345 dump_data(0, testarray[i].IV.data, testarray[i].IV.length);
346 printf("A\n");
347 dump_data(0, testarray[i].A.data, testarray[i].A.length);
348 printf("P1\n");
349 dump_data(0, testarray[i].P.data, testarray[i].P.length);
350 printf("P2\n");
351 dump_data(0, P.data, P.length);
352 printf("C\n");
353 dump_data(0, testarray[i].C.data, testarray[i].C.length);
354 printf("T1\n");
355 dump_data(0, testarray[i].T.data, testarray[i].T.length);
356 printf("T2\n");
357 dump_data(0, T, sizeof(T));
358 ret = false;
359 goto fail;
363 fail:
364 talloc_free(tctx);
365 return ret;