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