pyldb: protect PyErr_LDB_ERROR_IS_ERR_RAISE() with do {} while(0)
[Samba.git] / lib / crypto / aes_ccm_128_test.c
blob1a9fe411359b3474d1c27901313c1bc1e0400962
1 /*
2 AES-CCM-128 tests
4 Copyright (C) Stefan Metzmacher 2015
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"
22 #include "../lib/crypto/aes_test.h"
24 #ifndef AES_CCM_128_ONLY_TESTVECTORS
25 struct torture_context;
26 bool torture_local_crypto_aes_ccm_128(struct torture_context *torture);
29 This uses our own test values as we rely on a 11 byte nonce
30 and the values from rfc rfc3610 use 13 byte nonce.
32 bool torture_local_crypto_aes_ccm_128(struct torture_context *tctx)
34 bool ret = true;
35 uint32_t i;
36 struct aes_mode_testvector testarray[] = {
37 #endif /* AES_CCM_128_ONLY_TESTVECTORS */
38 #define AES_CCM_128_TESTVECTOR(_k, _n, _a, _p, _c, _t) \
39 AES_MODE_TESTVECTOR(aes_ccm_128, _k, _n, _a, _p, _c, _t)
41 AES_CCM_128_TESTVECTOR(
42 /* K */
43 "8BF9FBC2B8149484FF11AB1F3A544FF6",
44 /* N */
45 "010000000000000077F7A8",
46 /* A */
47 "010000000000000077F7A80000000000"
48 "A8000000000001004100002C00980000",
49 /* P */
50 "FE534D4240000100000000000B00811F"
51 "00000000000000000600000000000000"
52 "00000000010000004100002C00980000"
53 "00000000000000000000000000000000"
54 "3900000094010600FFFFFFFFFFFFFFFF"
55 "FFFFFFFFFFFFFFFF7800000030000000"
56 "000000007800000000000000FFFF0000"
57 "0100000000000000"
58 "03005C003100370032002E0033003100"
59 "2E0039002E003100380033005C006E00"
60 "650074006C006F0067006F006E000000",
61 /* C */
62 "25985364BF9AF90EB0B9C8FB55B7C446"
63 "780F310F1EC4677726BFBF34E38E6408"
64 "057EE228814F11CBAAB794A79F7A1F78"
65 "2DE73B7477985360A02D35A7A347ABF7"
66 "9F18DD8687767423BB08F18642B6EFEE"
67 "8B1543D83091AF5952F58BB4BD89FF6B"
68 "0206E7170481C7BC61F06653D0CF10F7"
69 "C78380389382C276"
70 "7B8BF34D687A5C3D4F783F926F7755C0"
71 "2D44C30848C69CFDD8E54395F1881611"
72 "E5502285870A7179068923105190C837",
73 /* T */
74 "3C11F652F8EA5600C8607D2E0FEAFD42"
76 AES_CCM_128_TESTVECTOR(
77 /* K */
78 "f9fdca4ac64fe7f014de0f43039c7571",
79 /* N */
80 "5a8aa485c316e947125478",
81 /* A */
82 "3796cf51b8726652a4204733b8fbb047"
83 "cf00fb91a9837e22ec22b1a268f88e2c",
84 /* P */
85 "a265480ca88d5f536db0dc6abc40faf0"
86 "d05be7a9669777682345647586786983",
87 /* C */
88 "65F8D8422006FB77FB7CCEFDFFF93729"
89 "B3EFCB06A0FAF3A2ABAB485723373F53",
90 /* T */
91 "2C62BD82AD231887A7B326E1E045BC91"
93 AES_CCM_128_TESTVECTOR(
94 /* K */
95 "197afb02ffbd8f699dacae87094d5243",
96 /* N */
97 "5a8aa485c316e947125478",
98 /* A */
99 "",
100 /* P */
101 "3796cf51b8726652a4204733b8fbb047"
102 "cf00fb91a9837e22",
103 /* C */
104 "CA53910394115C5DAB5D7250F04D6A27"
105 "2BCFA4329528F3AC",
106 /* T */
107 "38E3A318F9BA88D4DD2FAF3521820001"
109 AES_CCM_128_TESTVECTOR(
110 /* K */
111 "90929a4b0ac65b350ad1591611fe4829",
112 /* N */
113 "5a8aa485c316e9403aff85",
114 /* A */
116 /* P */
117 "a16a2e741f1cd9717285b6d882c1fc53"
118 "655e9773761ad697",
119 /* C */
120 "ACA5E98D2784D131AE76E3C8BF9C3988"
121 "35C0206C71893F26",
122 /* T */
123 "AE67C0EA38C5383BFDC7967F4E9D1678"
125 AES_CCM_128_TESTVECTOR(
126 /* K */
127 "f9fdca4ac64fe7f014de0f43039c7571",
128 /* N */
129 "5a8aa485c316e947125478",
130 /* A */
131 "3796cf51b8726652a4204733b8fbb047"
132 "cf00fb91a9837e22ec22b1a268f88e2c",
133 /* P */
134 "a265480ca88d5f536db0dc6abc40faf0"
135 "d05be7a966977768",
136 /* C */
137 "65F8D8422006FB77FB7CCEFDFFF93729"
138 "B3EFCB06A0FAF3A2",
139 /* T */
140 "03C6E244586AFAB9B60D9F6DBDF7EB1A"
142 AES_CCM_128_TESTVECTOR(
143 /* K */
144 "26511fb51fcfa75cb4b44da75a6e5a0e",
145 /* N */
146 "5a8aa485c316e9403aff85",
147 /* A */
148 "a16a2e741f1cd9717285b6d882c1fc53"
149 "655e9773761ad697a7ee6410184c7982",
150 /* P */
151 "8739b4bea1a099fe547499cbc6d1b13d"
152 "849b8084c9b6acc5",
153 /* C */
154 "D31F9FC23674D5272125375E0A2F5365"
155 "41B1FAF1DD68C819",
156 /* T */
157 "4F315233A76C4DD99972561C5158AB3B"
159 AES_CCM_128_TESTVECTOR(
160 /* K */
161 "f9fdca4ac64fe7f014de0f43039c7571",
162 /* N */
163 "5a8aa485c316e947125478",
164 /* A */
165 "3796cf51b8726652a4204733b8fbb047"
166 "cf00fb91a9837e22ec22b1a268",
167 /* P */
168 "a265480ca88d5f536db0dc6abc40faf0"
169 "d05be7a9669777682376345745",
170 /* C */
171 "65F8D8422006FB77FB7CCEFDFFF93729"
172 "B3EFCB06A0FAF3A2AB981875E0",
173 /* T */
174 "EA93AAEDA607226E9E79D2EE5C4B62F8"
176 AES_CCM_128_TESTVECTOR(
177 /* K */
178 "26511fb51fcfa75cb4b44da75a6e5a0e",
179 /* N */
180 "5a8aa485c316e9403aff85",
181 /* A */
182 "a16a2e741f1cd9717285b6d882c1fc53"
183 "65",
184 /* P */
185 "8739b4bea1a099fe547499cbc6d1b13d"
186 "84",
187 /* C */
188 "D31F9FC23674D5272125375E0A2F5365"
189 "41",
190 /* T */
191 "036F58DA2372B29BD0E01C58A0E7F9EE"
193 AES_CCM_128_TESTVECTOR(
194 /* K */
195 "00000000000000000000000000000000",
196 /* N */
197 "0000000000000000000000",
198 /* A */
200 /* P */
201 "00",
202 /* C */
203 "2E",
204 /* T */
205 "61787D2C432A58293B73D01154E61B6B"
207 AES_CCM_128_TESTVECTOR(
208 /* K */
209 "00000000000000000000000000000000",
210 /* N */
211 "0000000000000000000000",
212 /* A */
213 "00",
214 /* P */
215 "00",
216 /* C */
217 "2E",
218 /* T */
219 "E4284A0E813F0FFA146CF59F9ADAFBD7"
221 #ifndef AES_CCM_128_ONLY_TESTVECTORS
224 for (i=0; i < ARRAY_SIZE(testarray); i++) {
225 struct aes_ccm_128_context ctx;
226 uint8_t T[AES_BLOCK_SIZE];
227 DATA_BLOB _T = data_blob_const(T, sizeof(T));
228 DATA_BLOB C;
229 int e;
231 C = data_blob_dup_talloc(tctx, testarray[i].P);
233 aes_ccm_128_init(&ctx, testarray[i].K.data, testarray[i].N.data,
234 testarray[i].A.length, testarray[i].P.length);
235 aes_ccm_128_update(&ctx,
236 testarray[i].A.data,
237 testarray[i].A.length);
238 aes_ccm_128_update(&ctx, C.data, C.length);
239 aes_ccm_128_crypt(&ctx, C.data, C.length);
240 aes_ccm_128_digest(&ctx, T);
242 e = memcmp(testarray[i].T.data, T, sizeof(T));
243 if (e != 0) {
244 aes_mode_testvector_debug(&testarray[i], NULL, &C, &_T);
245 ret = false;
246 goto fail;
249 e = memcmp(testarray[i].C.data, C.data, C.length);
250 if (e != 0) {
251 aes_mode_testvector_debug(&testarray[i], NULL, &C, &_T);
252 ret = false;
253 goto fail;
257 for (i=0; i < ARRAY_SIZE(testarray); i++) {
258 struct aes_ccm_128_context ctx;
259 uint8_t T[AES_BLOCK_SIZE];
260 DATA_BLOB _T = data_blob_const(T, sizeof(T));
261 DATA_BLOB C;
262 int e;
263 size_t j;
265 C = data_blob_dup_talloc(tctx, testarray[i].P);
267 aes_ccm_128_init(&ctx, testarray[i].K.data, testarray[i].N.data,
268 testarray[i].A.length, testarray[i].P.length);
269 for (j=0; j < testarray[i].A.length; j++) {
270 aes_ccm_128_update(&ctx, NULL, 0);
271 aes_ccm_128_update(&ctx, &testarray[i].A.data[j], 1);
272 aes_ccm_128_update(&ctx, NULL, 0);
274 for (j=0; j < C.length; j++) {
275 aes_ccm_128_crypt(&ctx, NULL, 0);
276 aes_ccm_128_update(&ctx, NULL, 0);
277 aes_ccm_128_update(&ctx, &C.data[j], 1);
278 aes_ccm_128_crypt(&ctx, &C.data[j], 1);
279 aes_ccm_128_crypt(&ctx, NULL, 0);
280 aes_ccm_128_update(&ctx, NULL, 0);
282 aes_ccm_128_digest(&ctx, T);
284 e = memcmp(testarray[i].T.data, T, sizeof(T));
285 if (e != 0) {
286 aes_mode_testvector_debug(&testarray[i], NULL, &C, &_T);
287 ret = false;
288 goto fail;
291 e = memcmp(testarray[i].C.data, C.data, C.length);
292 if (e != 0) {
293 aes_mode_testvector_debug(&testarray[i], NULL, &C, &_T);
294 ret = false;
295 goto fail;
299 for (i=0; i < ARRAY_SIZE(testarray); i++) {
300 struct aes_ccm_128_context ctx;
301 uint8_t T[AES_BLOCK_SIZE];
302 DATA_BLOB _T = data_blob_const(T, sizeof(T));
303 DATA_BLOB P;
304 int e;
305 size_t j;
307 P = data_blob_dup_talloc(tctx, testarray[i].C);
309 aes_ccm_128_init(&ctx, testarray[i].K.data, testarray[i].N.data,
310 testarray[i].A.length, testarray[i].P.length);
311 for (j=0; j < testarray[i].A.length; j++) {
312 aes_ccm_128_update(&ctx, NULL, 0);
313 aes_ccm_128_update(&ctx, &testarray[i].A.data[j], 1);
314 aes_ccm_128_update(&ctx, NULL, 0);
316 for (j=0; j < P.length; j++) {
317 aes_ccm_128_crypt(&ctx, NULL, 0);
318 aes_ccm_128_update(&ctx, NULL, 0);
319 aes_ccm_128_crypt(&ctx, &P.data[j], 1);
320 aes_ccm_128_update(&ctx, &P.data[j], 1);
321 aes_ccm_128_crypt(&ctx, NULL, 0);
322 aes_ccm_128_update(&ctx, NULL, 0);
324 aes_ccm_128_digest(&ctx, T);
326 e = memcmp(testarray[i].T.data, T, sizeof(T));
327 if (e != 0) {
328 aes_mode_testvector_debug(&testarray[i], &P, NULL, &_T);
329 ret = false;
330 goto fail;
333 e = memcmp(testarray[i].P.data, P.data, P.length);
334 if (e != 0) {
335 aes_mode_testvector_debug(&testarray[i], &P, NULL, &_T);
336 ret = false;
337 goto fail;
341 for (i=0; i < ARRAY_SIZE(testarray); i++) {
342 struct aes_ccm_128_context ctx;
343 uint8_t T[AES_BLOCK_SIZE];
344 DATA_BLOB _T = data_blob_const(T, sizeof(T));
345 DATA_BLOB P;
346 int e;
348 P = data_blob_dup_talloc(tctx, testarray[i].C);
350 aes_ccm_128_init(&ctx, testarray[i].K.data, testarray[i].N.data,
351 testarray[i].A.length, testarray[i].P.length);
352 aes_ccm_128_update(&ctx, testarray[i].A.data, testarray[i].A.length);
353 aes_ccm_128_crypt(&ctx, P.data, P.length);
354 aes_ccm_128_update(&ctx, P.data, P.length);
355 aes_ccm_128_digest(&ctx, T);
357 e = memcmp(testarray[i].T.data, T, sizeof(T));
358 if (e != 0) {
359 aes_mode_testvector_debug(&testarray[i], &P, NULL, &_T);
360 ret = false;
361 goto fail;
364 e = memcmp(testarray[i].P.data, P.data, P.length);
365 if (e != 0) {
366 aes_mode_testvector_debug(&testarray[i], &P, NULL, &_T);
367 ret = false;
368 goto fail;
372 fail:
373 return ret;
376 #endif /* AES_CCM_128_ONLY_TESTVECTORS */