]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/nessie_bc_test.c
fixing E-Mail-Address & Copyright
[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) 2006-2015 Daniel Otte (bg@nerilex.org)
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:  bg@nerilex.org
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
33 nessie_bc_ctx_t nessie_bc_ctx;
34
35 void nessie_bc_init(void){
36         memset(&nessie_bc_ctx, 0, sizeof(nessie_bc_ctx_t));     
37 }
38 static
39 void nessie_bc_free(void *ctx){
40         if(nessie_bc_ctx.cipher_free)
41                 nessie_bc_ctx.cipher_free(ctx);
42 }
43
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];
47         uint16_t i;
48         
49         /* single test */
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);
52         
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);
60         }
61         /* 100 times test */
62         memcpy(buffer, pt, nessie_bc_ctx.blocksize_B);
63         for(i=0; i<100; ++i){
64                 nessie_bc_ctx.cipher_enc(buffer, ctx);
65                 NESSIE_SEND_ALIVE_A(i);
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                 NESSIE_SEND_ALIVE_A(i);
73         }
74         nessie_print_item("Iterated 1000 times", buffer, nessie_bc_ctx.blocksize_B);
75 #endif
76         nessie_bc_free(ctx);
77 }
78
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];
82         
83         /* single test */
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);
92         nessie_bc_free(ctx);
93 }
94
95 void nessie_bc_run(void){
96         uint16_t i;
97         uint8_t set;
98         uint8_t key[(nessie_bc_ctx.keysize_b+7)/8];
99         uint8_t buffer[nessie_bc_ctx.blocksize_B];
100         
101         nessie_print_header(nessie_bc_ctx.name, nessie_bc_ctx.keysize_b,
102                             nessie_bc_ctx.blocksize_B*8, 0, 0, 0);
103         /* test set 1 */
104         set=1;
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);
112         }
113         /* test set 2 */
114         set=2;
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);
122         }
123         /* test set 3 */
124         set=3;
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);
131         }
132         /* test set 4 */
133         set=4;
134         nessie_print_setheader(set);
135         /* 4 - 0*/
136         nessie_print_set_vector(set, 0);
137         for(i=0; i<(nessie_bc_ctx.keysize_b+7)/8; ++i){
138                 key[i]=i;
139         }
140         for(i=0; i<nessie_bc_ctx.blocksize_B; ++i){
141                 buffer[i]=i*0x11;
142         }
143         nessie_bc_enc(key, buffer);
144         /* 4 - 1 */
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)];
154         }
155         for(i=0; i<nessie_bc_ctx.blocksize_B; ++i){
156                 buffer[i]=kasumi_plain[i%sizeof(kasumi_plain)];
157         }
158         nessie_bc_enc(key, buffer);
159         /* half done ;-) */
160         if(nessie_bc_ctx.cipher_dec==NULL)
161                 return;
162         /* test set 5 */
163         set=5;
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);
171         }
172         /* test set 6 */
173         set=6;
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);
181         }
182         /* test set 7 */
183         set=7;
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);
190         }
191         /* test set 8 */
192         set=8;
193         nessie_print_setheader(set);
194         /* 8 - 0*/
195         nessie_print_set_vector(set, 0);
196         for(i=0; i<(nessie_bc_ctx.keysize_b+7)/8; ++i){
197                 key[i]=i;
198         }
199         for(i=0; i<nessie_bc_ctx.blocksize_B; ++i){
200                 buffer[i]=i*0x11;
201         }
202         nessie_bc_dec(key, buffer);
203         /* 8 - 1 */
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)];
207         }
208         for(i=0; i<nessie_bc_ctx.blocksize_B; ++i){
209                 buffer[i]=kasumi_plain[i%sizeof(kasumi_plain)];
210         }
211         nessie_bc_dec(key, buffer);
212         nessie_print_footer();
213 }