]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/nessie_stream_test.c
new hash function abstraction layer + shavs + dump util + ...
[avr-crypto-lib.git] / test_src / nessie_stream_test.c
1 /* nessie_stream_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 streamciphers
26  * 
27  * */
28 #include <stdint.h>
29 #include <string.h>
30 #include "nessie_stream_test.h"
31 #include "nessie_common.h"
32 #include "uart.h"
33
34 nessie_stream_ctx_t nessie_stream_ctx;
35
36
37 #define BLOCKSIZE_B 64
38
39 static
40 void memxor(void* dest, void* src, uint8_t length){
41         while(length--){
42                 *((uint8_t*)dest) ^= *((uint8_t*)src);
43                 dest = (uint8_t*)dest +1;
44                 src  = (uint8_t*)src +1;
45         }
46 }
47
48
49 static 
50 void nessie_gen_block(void* ctx, uint8_t* block){
51         uint16_t i;
52         for(i=0; i<BLOCKSIZE_B; ++i){
53                 block[i] = nessie_stream_ctx.cipher_enc(ctx);
54         }
55 }
56
57 static
58 void nessie_stream_enc(uint8_t* key){
59         uint8_t ctx[nessie_stream_ctx.ctx_size_B];
60         uint8_t buffer[BLOCKSIZE_B];
61         uint8_t xorbuffer[BLOCKSIZE_B];
62         uint8_t i;
63         
64         memset(xorbuffer, 0, BLOCKSIZE_B);
65         
66         nessie_print_item("key", key, (nessie_stream_ctx.keysize_b+7)/8);
67         
68         nessie_stream_ctx.cipher_genctx(key, nessie_stream_ctx.keysize_b, ctx);
69         
70         nessie_gen_block(ctx, buffer);
71         memxor(xorbuffer, buffer, BLOCKSIZE_B);
72         nessie_print_item("stream[0..63]", buffer, BLOCKSIZE_B);
73         
74         for(i=0; i<((192-0)/BLOCKSIZE_B-1); ++i){
75                 nessie_gen_block(ctx, buffer);
76                 memxor(xorbuffer, buffer, BLOCKSIZE_B);
77         }
78         
79         nessie_gen_block(ctx, buffer);
80         memxor(xorbuffer, buffer, BLOCKSIZE_B);
81         nessie_print_item("stream[192..255]", buffer, BLOCKSIZE_B);
82         
83         nessie_gen_block(ctx, buffer);
84         memxor(xorbuffer, buffer, BLOCKSIZE_B);
85         nessie_print_item("stream[256..319]", buffer, BLOCKSIZE_B);
86         
87         for(i=0; i<((448-256)/BLOCKSIZE_B-1); ++i){
88                 nessie_gen_block(ctx, buffer);
89                 memxor(xorbuffer, buffer, BLOCKSIZE_B);
90         }
91         
92         nessie_gen_block(ctx, buffer);
93         memxor(xorbuffer, buffer, BLOCKSIZE_B);
94         nessie_print_item("stream[448..511]", buffer, BLOCKSIZE_B);
95         
96         nessie_print_item("stream[0..511]xored", xorbuffer, BLOCKSIZE_B);
97         
98 }
99
100
101 static
102 void nessie_stream_enc_large(uint8_t* key){
103         uint8_t ctx[nessie_stream_ctx.ctx_size_B];
104         uint8_t buffer[BLOCKSIZE_B];
105         uint8_t xorbuffer[BLOCKSIZE_B];
106         uint32_t i;
107         
108         memset(xorbuffer, 0, BLOCKSIZE_B);
109         
110         nessie_print_item("key", key, (nessie_stream_ctx.keysize_b+7)/8);
111         
112         nessie_stream_ctx.cipher_genctx(key, nessie_stream_ctx.keysize_b, ctx);
113         
114         nessie_gen_block(ctx, buffer);
115         memxor(xorbuffer, buffer, BLOCKSIZE_B);
116         nessie_print_item("stream[0..63]", buffer, BLOCKSIZE_B);
117         
118         for(i=0; i<((65472-0)/BLOCKSIZE_B-1); ++i){
119                 nessie_gen_block(ctx, buffer);
120                 memxor(xorbuffer, buffer, BLOCKSIZE_B);
121                 NESSIE_SEND_ALIVE_A(i);
122         }
123         
124         nessie_gen_block(ctx, buffer);
125         memxor(xorbuffer, buffer, BLOCKSIZE_B);
126         nessie_print_item("stream[65472..65535]", buffer, BLOCKSIZE_B);
127         
128         nessie_gen_block(ctx, buffer);
129         memxor(xorbuffer, buffer, BLOCKSIZE_B);
130         nessie_print_item("stream[65536..65599]", buffer, BLOCKSIZE_B);
131         
132         for(i=0; i<((131008-65536)/BLOCKSIZE_B-1); ++i){
133                 nessie_gen_block(ctx, buffer);
134                 memxor(xorbuffer, buffer, BLOCKSIZE_B);
135                 NESSIE_SEND_ALIVE_A(i);
136         }
137         
138         nessie_gen_block(ctx, buffer);
139         memxor(xorbuffer, buffer, BLOCKSIZE_B);
140         nessie_print_item("stream[131008..131071]", buffer, BLOCKSIZE_B);
141         
142         nessie_print_item("stream[0..131071]xored", xorbuffer, BLOCKSIZE_B);
143         
144 }
145
146 void nessie_stream_run(void){
147         uint16_t i;
148         uint8_t set;
149         uint8_t key[(nessie_stream_ctx.keysize_b+7)/8];
150         
151         nessie_print_header(nessie_stream_ctx.name, nessie_stream_ctx.keysize_b,
152                             0, 0, 0, nessie_stream_ctx.ivsize_b);
153         /* test set 1 */
154         set=1;
155         nessie_print_setheader(set);
156         for(i=0; i<nessie_stream_ctx.keysize_b; ++i){
157                 nessie_print_set_vector(set, i);
158                 memset(key, 0, (nessie_stream_ctx.keysize_b+7)/8);
159                 key[i/8] |= 0x80>>(i%8);
160                 nessie_stream_enc(key);
161         }
162         /* test set 2 */
163         set=2;
164         nessie_print_setheader(set);
165         for(i=0; i<256; ++i){
166                 nessie_print_set_vector(set, i);
167                 memset(key, i, (nessie_stream_ctx.keysize_b+7)/8);
168                 nessie_stream_enc(key);
169         }
170         /* test set 3 */
171         set=3;
172         nessie_print_setheader(set);
173         for(i=0; i<256; ++i){
174                 uint8_t j;
175                 nessie_print_set_vector(set, i);
176                 for(j=0; j<(nessie_stream_ctx.keysize_b+7)/8; ++j){
177                         key[j]=(i+j)&0xff;
178                 }
179                 nessie_stream_enc(key);
180         }
181         /* test set 4 */
182         set=4;
183         nessie_print_setheader(set);
184         for(i=0; i<4; ++i){
185                 uint8_t j;
186                 nessie_print_set_vector(set, i);
187                 for(j=0; j<(nessie_stream_ctx.keysize_b+7)/8; ++j){
188                         key[j]=(i*5+j*0x53)&0xff;
189                 }
190                 nessie_stream_enc_large(key);
191         }
192
193         nessie_print_footer();
194 }