3 This file is part of the AVR-Crypto-Lib.
4 Copyright (C) 2006-2015 Daniel Otte (bg@nerilex.org)
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/>.
27 #include "main-test-common.h"
30 #include <bcal_aes128_enconly.h>
31 #include <bcal_aes192_enconly.h>
32 #include <bcal_aes256_enconly.h>
33 #include "performance_test.h"
35 char *algo_name = "GCM-AES128";
37 /*****************************************************************************
38 * additional validation-functions *
39 *****************************************************************************/
41 #define DUMP_LEN(x, len) do { printf("%s", "\n\n" #x ":"); \
42 cli_hexdump_block((x), (len), 4, 16); } while (0)
44 #define DUMP(x) DUMP_LEN(x, sizeof(x))
46 #define elementsof(t) (sizeof(t) / sizeof(t[0]))
48 /*****************************************************************************
50 *****************************************************************************/
52 const uint8_t zero_block[] PROGMEM = {
53 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
54 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
55 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
56 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
59 const uint8_t key_b[] PROGMEM = {
60 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
61 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
62 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
63 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08
67 const uint8_t ad_block[] PROGMEM = {
68 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
69 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
70 0xab, 0xad, 0xda, 0xd2
73 const uint8_t pt_block[] PROGMEM = {
74 0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5,
75 0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a,
76 0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda,
77 0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72,
78 0x1c, 0x3c, 0x0c, 0x95, 0x95, 0x68, 0x09, 0x53,
79 0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25,
80 0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57,
81 0xba, 0x63, 0x7b, 0x39, 0x1a, 0xaf, 0xd2, 0x55
84 /*****************************************************************************/
86 const uint8_t iv_block[] PROGMEM = {
87 0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad,
88 0xde, 0xca, 0xf8, 0x88
91 /*****************************************************************************/
93 const uint8_t iv_6[] PROGMEM = {
94 0x93, 0x13, 0x22, 0x5d, 0xf8, 0x84, 0x06, 0xe5,
95 0x55, 0x90, 0x9c, 0x5a, 0xff, 0x52, 0x69, 0xaa,
96 0x6a, 0x7a, 0x95, 0x38, 0x53, 0x4f, 0x7d, 0xa1,
97 0xe4, 0xc3, 0x03, 0xd2, 0xa3, 0x18, 0xa7, 0x28,
98 0xc3, 0xc0, 0xc9, 0x51, 0x56, 0x80, 0x95, 0x39,
99 0xfc, 0xf0, 0xe2, 0x42, 0x9a, 0x6b, 0x52, 0x54,
100 0x16, 0xae, 0xdb, 0xf5, 0xa0, 0xde, 0x6a, 0x57,
101 0xa6, 0x37, 0xb3, 0x9b
106 uint16_t key_length_b;
108 uint16_t iv_length_b;
110 uint16_t ad_length_b;
112 uint16_t pt_length_b;
115 const test_t test_set[] PROGMEM = {
116 { zero_block, 128, zero_block, 96, NULL, 0, NULL, 0 },
117 { zero_block, 128, zero_block, 96, NULL, 0, zero_block, 128 },
118 { key_b, 128, iv_block, 96, NULL, 0, pt_block, 512 },
119 { key_b, 128, iv_block, 96, ad_block, 160, pt_block, 480 },
120 { key_b, 128, iv_block, 64, ad_block, 160, pt_block, 480 },
121 { key_b, 128, iv_6, sizeof(iv_6) * 8, ad_block, 160, pt_block, 480 },
123 { zero_block, 192, zero_block, 96, NULL, 0, NULL, 0 },
124 { zero_block, 192, zero_block, 96, NULL, 0, zero_block, 128 },
125 { key_b, 192, iv_block, 96, NULL, 0, pt_block, 512 },
126 { key_b, 192, iv_block, 96, ad_block, 160, pt_block, 480 },
127 { key_b, 192, iv_block, 64, ad_block, 160, pt_block, 480 },
128 { key_b, 192, iv_6, sizeof(iv_6) * 8, ad_block, 160, pt_block, 480 },
130 { zero_block, 256, zero_block, 96, NULL, 0, NULL, 0 },
131 { zero_block, 256, zero_block, 96, NULL, 0, zero_block, 128 },
132 { key_b, 256, iv_block, 96, NULL, 0, pt_block, 512 },
133 { key_b, 256, iv_block, 96, ad_block, 160, pt_block, 480 },
134 { key_b, 256, iv_block, 64, ad_block, 160, pt_block, 480 },
135 { key_b, 256, iv_6, sizeof(iv_6) * 8, ad_block, 160, pt_block, 480 },
138 /*****************************************************************************
140 *****************************************************************************/
143 int8_t gcm128_simple(
146 uint16_t iv_length_b,
148 uint16_t ad_length_b,
151 uint16_t src_length_b,
153 uint8_t tag_length_b)
157 DUMP_LEN(iv, (iv_length_b + 7) / 8);
158 DUMP_LEN(ad, (ad_length_b + 7) / 8);
159 DUMP_LEN(src, (src_length_b + 7) / 8);
160 if (gcm128_init(&ctx, &aes128_desc, key, 128, iv, iv_length_b)) {
163 gcm128_add_ad_final_block(&ctx, ad, ad_length_b);
164 gcm128_encrypt_final_block(&ctx, dest, src, src_length_b);
165 gcm128_finalize(&ctx, tag, tag_length_b);
167 DUMP_LEN(dest, (src_length_b + 7) / 8);
169 DUMP_LEN(tag, (tag_length_b + 7) / 8);
173 int8_t gcm128_simple_progmem(
175 uint16_t key_length_b,
177 uint16_t iv_length_b,
179 uint16_t ad_length_b,
181 uint16_t src_length_b,
183 uint8_t tag_length_b)
187 gcm128_ctx_t ctx, dec_ctx;
188 const bcdesc_t *cipher;
189 switch (key_length_b) {
190 case 128: cipher = &aes128_desc; break;
191 case 192: cipher = &aes192_desc; break;
192 case 256: cipher = &aes256_desc; break;
196 uint8_t key[key_length_b / 8];
197 uint8_t iv[(iv_length_b + 7) / 8];
198 memcpy_P(key, key_p, key_length_b / 8);
199 memcpy_P(iv, iv_p, (iv_length_b + 7) / 8);
200 if ((r = gcm128_init(&ctx, cipher, key, key_length_b, iv, iv_length_b))) {
201 printf_P(PSTR("DBG: (Oooops) Error: %"PRId8"\n"), r);
205 if ((r = gcm128_init(&dec_ctx, cipher, key, key_length_b, iv, iv_length_b))) {
206 printf_P(PSTR("DBG: (Oooops) Error: %"PRId8"\n"), r);
212 uint8_t tmp[GCM128_BLOCK_BYTES];
213 while (ad_length_b >= GCM128_BLOCK_BITS) {
214 memcpy_P(tmp, ad_p, GCM128_BLOCK_BYTES);
215 ad_p = &((uint8_t*)ad_p)[GCM128_BLOCK_BYTES];
216 ad_length_b -= GCM128_BLOCK_BITS;
217 gcm128_add_ad_block(&ctx, tmp);
218 gcm128_add_ad_block(&dec_ctx, tmp);
220 memcpy_P(tmp, ad_p, (ad_length_b + 7) / 8);
221 gcm128_add_ad_final_block(&ctx, tmp, ad_length_b);
222 gcm128_add_ad_final_block(&dec_ctx, tmp, ad_length_b);
224 while (src_length_b >= GCM128_BLOCK_BITS) {
225 memcpy_P(tmp, src_p, GCM128_BLOCK_BYTES);
226 src_length_b -= GCM128_BLOCK_BITS;
227 gcm128_encrypt_block(&ctx, tmp, tmp);
228 gcm128_decrypt_block(&dec_ctx, tmp, tmp);
229 if (memcmp_P(tmp, src_p, GCM128_BLOCK_BYTES)) {
230 printf("DBG: Error: decryption error");
234 src_p = &((uint8_t*)src_p)[GCM128_BLOCK_BYTES];
236 memcpy_P(tmp, src_p, (src_length_b + 7) / 8);
237 gcm128_encrypt_final_block(&ctx, tmp, tmp, src_length_b);
238 gcm128_decrypt_final_block(&dec_ctx, tmp, tmp, src_length_b);
239 if (src_length_b > 0) {
240 // DUMP_LEN(tmp, (src_length_b + 7) / 8);
241 if (memcmp_P(tmp, src_p, (src_length_b + 7) / 8)) {
242 printf("DBG: Error: decryption error");
243 DUMP_LEN(tmp, (src_length_b + 7) / 8);
246 gcm128_finalize(&dec_ctx, dec_tag, tag_length_b);
247 gcm128_finalize(&ctx, tag, tag_length_b);
248 if (memcmp(tag, dec_tag, (tag_length_b + 7) / 8)) {
249 printf("DBG: Error: tag error");
250 DUMP_LEN(tag, (tag_length_b + 7) / 8);
251 DUMP_LEN(dec_tag, (tag_length_b + 7) / 8);
256 void testrun_gcm128(void)
263 memset(key, 0, sizeof(key));
264 memset(iv, 0, sizeof(iv));
265 memset(plain, 0, sizeof(plain));
266 gcm128_simple(key, iv, 96, NULL, 0, NULL, NULL, 0, tag, 128);
267 gcm128_simple(key, iv, 96, NULL, 0, cipher, plain, 128, tag, 128);
272 void testrun_gcm128_progmem(void)
277 for (i = 0; i < elementsof(test_set); ++i) {
278 printf_P(PSTR("== Test %"PRId8" ==\n"), i + 1);
280 memcpy_P(&t, &test_set[i], sizeof(t));
281 gcm128_simple_progmem(
282 t.key, t.key_length_b,
293 /*****************************************************************************
295 *****************************************************************************/
297 const char nessie_str[] PROGMEM = "nessie";
298 const char test_str[] PROGMEM = "test";
299 const char ftest_str[] PROGMEM = "ftest";
300 const char gtest_str[] PROGMEM = "gtest";
301 const char performance_str[] PROGMEM = "performance";
302 const char echo_str[] PROGMEM = "echo";
304 const cmdlist_entry_t cmdlist[] PROGMEM = {
305 // { nessie_str, NULL, NULL },
306 { test_str, NULL, testrun_gcm128_progmem},
307 // { ftest_str, NULL, testrun_f32},
308 // { gtest_str, NULL, testrun_g32},
309 // { performance_str, NULL, testrun_performance_arcfour},
310 { echo_str, (void*)1, (void_fpt)echo_ctrl},
318 welcome_msg(algo_name);
319 cmd_interface(cmdlist);