3 This file is part of the Crypto-avr-lib/microcrypt-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"
34 nessie_bc_ctx_t nessie_bc_ctx;
36 void nessie_bc_init(void){
37 memset(&nessie_bc_ctx, 0, sizeof(nessie_bc_ctx_t));
40 void nessie_bc_free(void* ctx){
41 if(nessie_bc_ctx.cipher_free)
42 nessie_bc_ctx.cipher_free(ctx);
45 void nessie_bc_enc(uint8_t* key, uint8_t* pt){
46 uint8_t ctx[nessie_bc_ctx.ctx_size_B];
47 uint8_t buffer[nessie_bc_ctx.blocksize_B];
51 nessie_print_item("key", key, (nessie_bc_ctx.keysize_b+7)/8);
52 nessie_bc_ctx.cipher_genctx(key, nessie_bc_ctx.keysize_b, ctx);
54 memcpy(buffer, pt, nessie_bc_ctx.blocksize_B);
55 nessie_print_item("plain", buffer, nessie_bc_ctx.blocksize_B);
56 nessie_bc_ctx.cipher_enc(buffer, ctx);
57 nessie_print_item("cipher", buffer, nessie_bc_ctx.blocksize_B);
58 if(nessie_bc_ctx.cipher_dec){
59 nessie_bc_ctx.cipher_dec(buffer, ctx);
60 nessie_print_item("decrypted", buffer, nessie_bc_ctx.blocksize_B);
63 memcpy(buffer, pt, nessie_bc_ctx.blocksize_B);
65 nessie_bc_ctx.cipher_enc(buffer, ctx);
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);
73 nessie_print_item("Iterated 1000 times", buffer, nessie_bc_ctx.blocksize_B);
78 void nessie_bc_dec(uint8_t* key, uint8_t* ct){
79 uint8_t ctx[nessie_bc_ctx.ctx_size_B];
80 uint8_t buffer[nessie_bc_ctx.blocksize_B];
83 nessie_print_item("key", key, (nessie_bc_ctx.keysize_b+7)/8);
84 nessie_bc_ctx.cipher_genctx(key, nessie_bc_ctx.keysize_b, ctx);
85 memcpy(buffer, ct, nessie_bc_ctx.blocksize_B);
86 nessie_print_item("cipher", buffer, nessie_bc_ctx.blocksize_B);
87 nessie_bc_ctx.cipher_dec(buffer, ctx);
88 nessie_print_item("plain", buffer, nessie_bc_ctx.blocksize_B);
89 nessie_bc_ctx.cipher_enc(buffer, ctx);
90 nessie_print_item("encrypted", buffer, nessie_bc_ctx.blocksize_B);
94 void nessie_bc_run(void){
97 uint8_t key[(nessie_bc_ctx.keysize_b+7)/8];
98 uint8_t buffer[nessie_bc_ctx.blocksize_B];
100 nessie_print_header(nessie_bc_ctx.name, nessie_bc_ctx.keysize_b,
101 nessie_bc_ctx.blocksize_B*8, 0, 0, 0);
104 nessie_print_setheader(set);
105 for(i=0; i<nessie_bc_ctx.keysize_b; ++i){
106 nessie_print_set_vector(set, i);
107 memset(key, 0, (nessie_bc_ctx.keysize_b+7)/8);
108 key[i/8] |= 0x80>>(i%8);
109 memset(buffer, 0, nessie_bc_ctx.blocksize_B);
110 nessie_bc_enc(key, buffer);
114 nessie_print_setheader(set);
115 for(i=0; i<nessie_bc_ctx.blocksize_B*8; ++i){
116 nessie_print_set_vector(set, i);
117 memset(key, 0, (nessie_bc_ctx.keysize_b+7)/8);
118 memset(buffer, 0, nessie_bc_ctx.blocksize_B);
119 buffer[i/8] |= 0x80>>(i%8);
120 nessie_bc_enc(key, buffer);
124 nessie_print_setheader(set);
125 for(i=0; i<256; ++i){
126 nessie_print_set_vector(set, i);
127 memset(key, i, (nessie_bc_ctx.keysize_b+7)/8);
128 memset(buffer, i, nessie_bc_ctx.blocksize_B);
129 nessie_bc_enc(key, buffer);
133 nessie_print_setheader(set);
135 nessie_print_set_vector(set, 0);
136 for(i=0; i<(nessie_bc_ctx.keysize_b+7)/8; ++i){
139 for(i=0; i<nessie_bc_ctx.blocksize_B; ++i){
142 nessie_bc_enc(key, buffer);
144 nessie_print_set_vector(set, 1);
145 /* This is the test vectors in Kasumi */
146 static uint8_t kasumi_key[] = {
147 0x2B, 0xD6, 0x45, 0x9F, 0x82, 0xC5, 0xB3, 0x00,
148 0x95, 0x2C, 0x49, 0x10, 0x48, 0x81, 0xFF, 0x48 };
149 static uint8_t kasumi_plain[]={
150 0xEA, 0x02, 0x47, 0x14, 0xAD, 0x5C, 0x4D, 0x84 };
151 for(i=0; i<(nessie_bc_ctx.keysize_b+7)/8; ++i){
152 key[i]=kasumi_key[i%sizeof(kasumi_key)];
154 for(i=0; i<nessie_bc_ctx.blocksize_B; ++i){
155 buffer[i]=kasumi_plain[i%sizeof(kasumi_plain)];
157 nessie_bc_enc(key, buffer);
159 if(nessie_bc_ctx.cipher_dec==NULL)
163 nessie_print_setheader(set);
164 for(i=0; i<nessie_bc_ctx.keysize_b; ++i){
165 nessie_print_set_vector(set, i);
166 memset(key, 0, (nessie_bc_ctx.keysize_b+7)/8);
167 key[i/8] |= 0x80>>(i%8);
168 memset(buffer, 0, nessie_bc_ctx.blocksize_B);
169 nessie_bc_dec(key, buffer);
173 nessie_print_setheader(set);
174 for(i=0; i<nessie_bc_ctx.blocksize_B*8; ++i){
175 nessie_print_set_vector(set, i);
176 memset(key, 0, (nessie_bc_ctx.keysize_b+7)/8);
177 memset(buffer, 0, nessie_bc_ctx.blocksize_B);
178 buffer[i/8] |= 0x80>>(i%8);
179 nessie_bc_dec(key, buffer);
183 nessie_print_setheader(set);
184 for(i=0; i<256; ++i){
185 nessie_print_set_vector(set, i);
186 memset(key, i, (nessie_bc_ctx.keysize_b+7)/8);
187 memset(buffer, i, nessie_bc_ctx.blocksize_B);
188 nessie_bc_dec(key, buffer);
192 nessie_print_setheader(set);
194 nessie_print_set_vector(set, 0);
195 for(i=0; i<(nessie_bc_ctx.keysize_b+7)/8; ++i){
198 for(i=0; i<nessie_bc_ctx.blocksize_B; ++i){
201 nessie_bc_dec(key, buffer);
203 nessie_print_set_vector(set, 1);
204 for(i=0; i<(nessie_bc_ctx.keysize_b+7)/8; ++i){
205 key[i]=kasumi_key[i%sizeof(kasumi_key)];
207 for(i=0; i<nessie_bc_ctx.blocksize_B; ++i){
208 buffer[i]=kasumi_plain[i%sizeof(kasumi_plain)];
210 nessie_bc_dec(key, buffer);
211 nessie_print_footer();