]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/nessie_bc_test.c
new hash function abstraction layer + shavs + dump util + ...
[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                 NESSIE_SEND_ALIVE_A(i);
67         }
68         nessie_print_item("Iterated 100 times", buffer, nessie_bc_ctx.blocksize_B);
69 #ifndef NESSIE_NO1KTEST 
70         /* 1000 times test, we use the 100 precedig steps to fasten things a bit */
71         for(; i<1000; ++i){
72                 nessie_bc_ctx.cipher_enc(buffer, ctx);
73                 NESSIE_SEND_ALIVE_A(i);
74         }
75         nessie_print_item("Iterated 1000 times", buffer, nessie_bc_ctx.blocksize_B);
76 #endif
77         nessie_bc_free(ctx);
78 }
79
80 void nessie_bc_dec(uint8_t* key, uint8_t* ct){
81         uint8_t ctx[nessie_bc_ctx.ctx_size_B];
82         uint8_t buffer[nessie_bc_ctx.blocksize_B];
83         
84         /* single test */
85         nessie_print_item("key", key, (nessie_bc_ctx.keysize_b+7)/8);
86         nessie_bc_ctx.cipher_genctx(key, nessie_bc_ctx.keysize_b, ctx);
87         memcpy(buffer, ct, nessie_bc_ctx.blocksize_B);
88         nessie_print_item("cipher", buffer, nessie_bc_ctx.blocksize_B);
89         nessie_bc_ctx.cipher_dec(buffer, ctx);
90         nessie_print_item("plain", buffer, nessie_bc_ctx.blocksize_B);
91         nessie_bc_ctx.cipher_enc(buffer, ctx);
92         nessie_print_item("encrypted", buffer, nessie_bc_ctx.blocksize_B);
93         nessie_bc_free(ctx);
94 }
95
96 void nessie_bc_run(void){
97         uint16_t i;
98         uint8_t set;
99         uint8_t key[(nessie_bc_ctx.keysize_b+7)/8];
100         uint8_t buffer[nessie_bc_ctx.blocksize_B];
101         
102         nessie_print_header(nessie_bc_ctx.name, nessie_bc_ctx.keysize_b,
103                             nessie_bc_ctx.blocksize_B*8, 0, 0, 0);
104         /* test set 1 */
105         set=1;
106         nessie_print_setheader(set);
107         for(i=0; i<nessie_bc_ctx.keysize_b; ++i){
108                 nessie_print_set_vector(set, i);
109                 memset(key, 0, (nessie_bc_ctx.keysize_b+7)/8);
110                 key[i/8] |= 0x80>>(i%8);
111                 memset(buffer, 0, nessie_bc_ctx.blocksize_B);
112                 nessie_bc_enc(key, buffer);
113         }
114         /* test set 2 */
115         set=2;
116         nessie_print_setheader(set);
117         for(i=0; i<nessie_bc_ctx.blocksize_B*8; ++i){
118                 nessie_print_set_vector(set, i);
119                 memset(key, 0, (nessie_bc_ctx.keysize_b+7)/8);
120                 memset(buffer, 0, nessie_bc_ctx.blocksize_B);
121                 buffer[i/8] |= 0x80>>(i%8);
122                 nessie_bc_enc(key, buffer);
123         }
124         /* test set 3 */
125         set=3;
126         nessie_print_setheader(set);
127         for(i=0; i<256; ++i){
128                 nessie_print_set_vector(set, i);
129                 memset(key, i, (nessie_bc_ctx.keysize_b+7)/8);
130                 memset(buffer, i, nessie_bc_ctx.blocksize_B);
131                 nessie_bc_enc(key, buffer);
132         }
133         /* test set 4 */
134         set=4;
135         nessie_print_setheader(set);
136         /* 4 - 0*/
137         nessie_print_set_vector(set, 0);
138         for(i=0; i<(nessie_bc_ctx.keysize_b+7)/8; ++i){
139                 key[i]=i;
140         }
141         for(i=0; i<nessie_bc_ctx.blocksize_B; ++i){
142                 buffer[i]=i*0x11;
143         }
144         nessie_bc_enc(key, buffer);
145         /* 4 - 1 */
146         nessie_print_set_vector(set, 1);        
147     /* This is the test vectors in Kasumi */
148     static uint8_t kasumi_key[] = {
149            0x2B, 0xD6, 0x45, 0x9F, 0x82, 0xC5, 0xB3, 0x00,
150            0x95, 0x2C, 0x49, 0x10, 0x48, 0x81, 0xFF, 0x48 };
151     static uint8_t kasumi_plain[]={
152            0xEA, 0x02, 0x47, 0x14, 0xAD, 0x5C, 0x4D, 0x84 };
153         for(i=0; i<(nessie_bc_ctx.keysize_b+7)/8; ++i){
154                 key[i]=kasumi_key[i%sizeof(kasumi_key)];
155         }
156         for(i=0; i<nessie_bc_ctx.blocksize_B; ++i){
157                 buffer[i]=kasumi_plain[i%sizeof(kasumi_plain)];
158         }
159         nessie_bc_enc(key, buffer);
160         /* half done ;-) */
161         if(nessie_bc_ctx.cipher_dec==NULL)
162                 return;
163         /* test set 5 */
164         set=5;
165         nessie_print_setheader(set);
166         for(i=0; i<nessie_bc_ctx.keysize_b; ++i){
167                 nessie_print_set_vector(set, i);
168                 memset(key, 0, (nessie_bc_ctx.keysize_b+7)/8);
169                 key[i/8] |= 0x80>>(i%8);
170                 memset(buffer, 0, nessie_bc_ctx.blocksize_B);
171                 nessie_bc_dec(key, buffer);
172         }
173         /* test set 6 */
174         set=6;
175         nessie_print_setheader(set);
176         for(i=0; i<nessie_bc_ctx.blocksize_B*8; ++i){
177                 nessie_print_set_vector(set, i);
178                 memset(key, 0, (nessie_bc_ctx.keysize_b+7)/8);
179                 memset(buffer, 0, nessie_bc_ctx.blocksize_B);
180                 buffer[i/8] |= 0x80>>(i%8);
181                 nessie_bc_dec(key, buffer);
182         }
183         /* test set 7 */
184         set=7;
185         nessie_print_setheader(set);
186         for(i=0; i<256; ++i){
187                 nessie_print_set_vector(set, i);
188                 memset(key, i, (nessie_bc_ctx.keysize_b+7)/8);
189                 memset(buffer, i, nessie_bc_ctx.blocksize_B);
190                 nessie_bc_dec(key, buffer);
191         }
192         /* test set 8 */
193         set=8;
194         nessie_print_setheader(set);
195         /* 8 - 0*/
196         nessie_print_set_vector(set, 0);
197         for(i=0; i<(nessie_bc_ctx.keysize_b+7)/8; ++i){
198                 key[i]=i;
199         }
200         for(i=0; i<nessie_bc_ctx.blocksize_B; ++i){
201                 buffer[i]=i*0x11;
202         }
203         nessie_bc_dec(key, buffer);
204         /* 8 - 1 */
205         nessie_print_set_vector(set, 1);        
206         for(i=0; i<(nessie_bc_ctx.keysize_b+7)/8; ++i){
207                 key[i]=kasumi_key[i%sizeof(kasumi_key)];
208         }
209         for(i=0; i<nessie_bc_ctx.blocksize_B; ++i){
210                 buffer[i]=kasumi_plain[i%sizeof(kasumi_plain)];
211         }
212         nessie_bc_dec(key, buffer);
213         nessie_print_footer();
214 }