]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/nessie_bc_test.c
new cli system
[avr-crypto-lib.git] / test_src / nessie_bc_test.c
1 /* nessie_bc_test.c */
2 /*
3     This file is part of the AVR-Crypto-Lib.
4     Copyright (C) 2008  Daniel Otte (daniel.otte@rub.de)
5
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.
10
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.
15
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/>.
18 */
19 /**
20  * 
21  * author: Daniel Otte
22  * email:  daniel.otte@rub.de
23  * license: GPLv3
24  * 
25  * a suit for running the nessie-tests for blockciphers
26  * 
27  * */
28 #include <stdint.h>
29 #include <string.h>
30 #include "nessie_bc_test.h"
31 #include "nessie_common.h"
32 #include "uart.h"
33
34 nessie_bc_ctx_t nessie_bc_ctx;
35
36 void nessie_bc_init(void){
37         memset(&nessie_bc_ctx, 0, sizeof(nessie_bc_ctx_t));     
38 }
39 static
40 void nessie_bc_free(void* ctx){
41         if(nessie_bc_ctx.cipher_free)
42                 nessie_bc_ctx.cipher_free(ctx);
43 }
44
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];
48         uint16_t i;
49         
50         /* single test */
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);
53         
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);
61         }
62         /* 100 times test */
63         memcpy(buffer, pt, nessie_bc_ctx.blocksize_B);
64         for(i=0; i<100; ++i){
65                 nessie_bc_ctx.cipher_enc(buffer, ctx);
66         }
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 */
70         for(; i<1000; ++i){
71                 nessie_bc_ctx.cipher_enc(buffer, ctx);
72         }
73         nessie_print_item("Iterated 1000 times", buffer, nessie_bc_ctx.blocksize_B);
74 #endif
75         nessie_bc_free(ctx);
76 }
77
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];
81         
82         /* single test */
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);
91         nessie_bc_free(ctx);
92 }
93
94 void nessie_bc_run(void){
95         uint16_t i;
96         uint8_t set;
97         uint8_t key[(nessie_bc_ctx.keysize_b+7)/8];
98         uint8_t buffer[nessie_bc_ctx.blocksize_B];
99         
100         nessie_print_header(nessie_bc_ctx.name, nessie_bc_ctx.keysize_b,
101                             nessie_bc_ctx.blocksize_B*8, 0, 0, 0);
102         /* test set 1 */
103         set=1;
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);
111         }
112         /* test set 2 */
113         set=2;
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);
121         }
122         /* test set 3 */
123         set=3;
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);
130         }
131         /* test set 4 */
132         set=4;
133         nessie_print_setheader(set);
134         /* 4 - 0*/
135         nessie_print_set_vector(set, 0);
136         for(i=0; i<(nessie_bc_ctx.keysize_b+7)/8; ++i){
137                 key[i]=i;
138         }
139         for(i=0; i<nessie_bc_ctx.blocksize_B; ++i){
140                 buffer[i]=i*0x11;
141         }
142         nessie_bc_enc(key, buffer);
143         /* 4 - 1 */
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)];
153         }
154         for(i=0; i<nessie_bc_ctx.blocksize_B; ++i){
155                 buffer[i]=kasumi_plain[i%sizeof(kasumi_plain)];
156         }
157         nessie_bc_enc(key, buffer);
158         /* half done ;-) */
159         if(nessie_bc_ctx.cipher_dec==NULL)
160                 return;
161         /* test set 5 */
162         set=5;
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);
170         }
171         /* test set 6 */
172         set=6;
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);
180         }
181         /* test set 7 */
182         set=7;
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);
189         }
190         /* test set 8 */
191         set=8;
192         nessie_print_setheader(set);
193         /* 8 - 0*/
194         nessie_print_set_vector(set, 0);
195         for(i=0; i<(nessie_bc_ctx.keysize_b+7)/8; ++i){
196                 key[i]=i;
197         }
198         for(i=0; i<nessie_bc_ctx.blocksize_B; ++i){
199                 buffer[i]=i*0x11;
200         }
201         nessie_bc_dec(key, buffer);
202         /* 8 - 1 */
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)];
206         }
207         for(i=0; i<nessie_bc_ctx.blocksize_B; ++i){
208                 buffer[i]=kasumi_plain[i%sizeof(kasumi_plain)];
209         }
210         nessie_bc_dec(key, buffer);
211         nessie_print_footer();
212 }