3 This file is part of the AVR-Crypto-Lib.
4 Copyright (C) 2008 Daniel Otte (daniel.otte@rub.de)
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/>.
22 * email: daniel.otte@rub.de
25 * a suit for running the nessie-tests for blockciphers
30 #include "nessie_bc_test.h"
31 #include "nessie_common.h"
33 nessie_bc_ctx_t nessie_bc_ctx;
35 void nessie_bc_init(void){
36 memset(&nessie_bc_ctx, 0, sizeof(nessie_bc_ctx_t));
39 void nessie_bc_free(void *ctx){
40 if(nessie_bc_ctx.cipher_free)
41 nessie_bc_ctx.cipher_free(ctx);
44 void nessie_bc_enc(uint8_t *key, uint8_t *pt){
45 uint8_t ctx[nessie_bc_ctx.ctx_size_B];
46 uint8_t buffer[nessie_bc_ctx.blocksize_B];
50 nessie_print_item("key", key, (nessie_bc_ctx.keysize_b+7)/8);
51 nessie_bc_ctx.cipher_genctx(key, nessie_bc_ctx.keysize_b, ctx);
53 memcpy(buffer, pt, nessie_bc_ctx.blocksize_B);
54 nessie_print_item("plain", buffer, nessie_bc_ctx.blocksize_B);
55 nessie_bc_ctx.cipher_enc(buffer, ctx);
56 nessie_print_item("cipher", buffer, nessie_bc_ctx.blocksize_B);
57 if(nessie_bc_ctx.cipher_dec){
58 nessie_bc_ctx.cipher_dec(buffer, ctx);
59 nessie_print_item("decrypted", buffer, nessie_bc_ctx.blocksize_B);
62 memcpy(buffer, pt, nessie_bc_ctx.blocksize_B);
64 nessie_bc_ctx.cipher_enc(buffer, ctx);
65 NESSIE_SEND_ALIVE_A(i);
67 nessie_print_item("Iterated 100 times", buffer, nessie_bc_ctx.blocksize_B);
68 #ifndef NESSIE_NO1KTEST
69 /* 1000 times test, we use the 100 precedig steps to fasten things a bit */
71 nessie_bc_ctx.cipher_enc(buffer, ctx);
72 NESSIE_SEND_ALIVE_A(i);
74 nessie_print_item("Iterated 1000 times", buffer, nessie_bc_ctx.blocksize_B);
79 void nessie_bc_dec(uint8_t *key, uint8_t *ct){
80 uint8_t ctx[nessie_bc_ctx.ctx_size_B];
81 uint8_t buffer[nessie_bc_ctx.blocksize_B];
84 nessie_print_item("key", key, (nessie_bc_ctx.keysize_b+7)/8);
85 nessie_bc_ctx.cipher_genctx(key, nessie_bc_ctx.keysize_b, ctx);
86 memcpy(buffer, ct, nessie_bc_ctx.blocksize_B);
87 nessie_print_item("cipher", buffer, nessie_bc_ctx.blocksize_B);
88 nessie_bc_ctx.cipher_dec(buffer, ctx);
89 nessie_print_item("plain", buffer, nessie_bc_ctx.blocksize_B);
90 nessie_bc_ctx.cipher_enc(buffer, ctx);
91 nessie_print_item("encrypted", buffer, nessie_bc_ctx.blocksize_B);
95 void nessie_bc_run(void){
98 uint8_t key[(nessie_bc_ctx.keysize_b+7)/8];
99 uint8_t buffer[nessie_bc_ctx.blocksize_B];
101 nessie_print_header(nessie_bc_ctx.name, nessie_bc_ctx.keysize_b,
102 nessie_bc_ctx.blocksize_B*8, 0, 0, 0);
105 nessie_print_setheader(set);
106 for(i=0; i<nessie_bc_ctx.keysize_b; ++i){
107 nessie_print_set_vector(set, i);
108 memset(key, 0, (nessie_bc_ctx.keysize_b+7)/8);
109 key[i/8] |= 0x80>>(i%8);
110 memset(buffer, 0, nessie_bc_ctx.blocksize_B);
111 nessie_bc_enc(key, buffer);
115 nessie_print_setheader(set);
116 for(i=0; i<nessie_bc_ctx.blocksize_B*8; ++i){
117 nessie_print_set_vector(set, i);
118 memset(key, 0, (nessie_bc_ctx.keysize_b+7)/8);
119 memset(buffer, 0, nessie_bc_ctx.blocksize_B);
120 buffer[i/8] |= 0x80>>(i%8);
121 nessie_bc_enc(key, buffer);
125 nessie_print_setheader(set);
126 for(i=0; i<256; ++i){
127 nessie_print_set_vector(set, i);
128 memset(key, i, (nessie_bc_ctx.keysize_b+7)/8);
129 memset(buffer, i, nessie_bc_ctx.blocksize_B);
130 nessie_bc_enc(key, buffer);
134 nessie_print_setheader(set);
136 nessie_print_set_vector(set, 0);
137 for(i=0; i<(nessie_bc_ctx.keysize_b+7)/8; ++i){
140 for(i=0; i<nessie_bc_ctx.blocksize_B; ++i){
143 nessie_bc_enc(key, buffer);
145 nessie_print_set_vector(set, 1);
146 /* This is the test vectors in Kasumi */
147 static uint8_t kasumi_key[] = {
148 0x2B, 0xD6, 0x45, 0x9F, 0x82, 0xC5, 0xB3, 0x00,
149 0x95, 0x2C, 0x49, 0x10, 0x48, 0x81, 0xFF, 0x48 };
150 static uint8_t kasumi_plain[]={
151 0xEA, 0x02, 0x47, 0x14, 0xAD, 0x5C, 0x4D, 0x84 };
152 for(i=0; i<(nessie_bc_ctx.keysize_b+7)/8; ++i){
153 key[i]=kasumi_key[i%sizeof(kasumi_key)];
155 for(i=0; i<nessie_bc_ctx.blocksize_B; ++i){
156 buffer[i]=kasumi_plain[i%sizeof(kasumi_plain)];
158 nessie_bc_enc(key, buffer);
160 if(nessie_bc_ctx.cipher_dec==NULL)
164 nessie_print_setheader(set);
165 for(i=0; i<nessie_bc_ctx.keysize_b; ++i){
166 nessie_print_set_vector(set, i);
167 memset(key, 0, (nessie_bc_ctx.keysize_b+7)/8);
168 key[i/8] |= 0x80>>(i%8);
169 memset(buffer, 0, nessie_bc_ctx.blocksize_B);
170 nessie_bc_dec(key, buffer);
174 nessie_print_setheader(set);
175 for(i=0; i<nessie_bc_ctx.blocksize_B*8; ++i){
176 nessie_print_set_vector(set, i);
177 memset(key, 0, (nessie_bc_ctx.keysize_b+7)/8);
178 memset(buffer, 0, nessie_bc_ctx.blocksize_B);
179 buffer[i/8] |= 0x80>>(i%8);
180 nessie_bc_dec(key, buffer);
184 nessie_print_setheader(set);
185 for(i=0; i<256; ++i){
186 nessie_print_set_vector(set, i);
187 memset(key, i, (nessie_bc_ctx.keysize_b+7)/8);
188 memset(buffer, i, nessie_bc_ctx.blocksize_B);
189 nessie_bc_dec(key, buffer);
193 nessie_print_setheader(set);
195 nessie_print_set_vector(set, 0);
196 for(i=0; i<(nessie_bc_ctx.keysize_b+7)/8; ++i){
199 for(i=0; i<nessie_bc_ctx.blocksize_B; ++i){
202 nessie_bc_dec(key, buffer);
204 nessie_print_set_vector(set, 1);
205 for(i=0; i<(nessie_bc_ctx.keysize_b+7)/8; ++i){
206 key[i]=kasumi_key[i%sizeof(kasumi_key)];
208 for(i=0; i<nessie_bc_ctx.blocksize_B; ++i){
209 buffer[i]=kasumi_plain[i%sizeof(kasumi_plain)];
211 nessie_bc_dec(key, buffer);
212 nessie_print_footer();