]> git.cryptolib.org Git - avr-crypto-lib.git/blob - nessie_stream_test.c
insereated GPLv3 stub
[avr-crypto-lib.git] / nessie_stream_test.c
1 /* nessie_stream_test.c */
2 /*
3     This file is part of the Crypto-avr-lib/microcrypt-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         }
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         }
135         
136         nessie_gen_block(ctx, buffer);
137         memxor(xorbuffer, buffer, BLOCKSIZE_B);
138         nessie_print_item("stream[131008..131071]", buffer, BLOCKSIZE_B);
139         
140         nessie_print_item("stream[0..131071]xored", xorbuffer, BLOCKSIZE_B);
141         
142 }
143
144 void nessie_stream_run(void){
145         uint16_t i;
146         uint8_t set;
147         uint8_t key[(nessie_stream_ctx.keysize_b+7)/8];
148         
149         nessie_print_header(nessie_stream_ctx.name, nessie_stream_ctx.keysize_b,
150                             0, 0, 0, nessie_stream_ctx.ivsize_b);
151         /* test set 1 */
152         set=1;
153         nessie_print_setheader(set);
154         for(i=0; i<nessie_stream_ctx.keysize_b; ++i){
155                 nessie_print_set_vector(set, i);
156                 memset(key, 0, (nessie_stream_ctx.keysize_b+7)/8);
157                 key[i/8] |= 0x80>>(i%8);
158                 nessie_stream_enc(key);
159         }
160         /* test set 2 */
161         set=2;
162         nessie_print_setheader(set);
163         for(i=0; i<256; ++i){
164                 nessie_print_set_vector(set, i);
165                 memset(key, i, (nessie_stream_ctx.keysize_b+7)/8);
166                 nessie_stream_enc(key);
167         }
168         /* test set 3 */
169         set=3;
170         nessie_print_setheader(set);
171         for(i=0; i<256; ++i){
172                 uint8_t j;
173                 nessie_print_set_vector(set, i);
174                 for(j=0; j<(nessie_stream_ctx.keysize_b+7)/8; ++j){
175                         key[j]=(i+j)&0xff;
176                 }
177                 nessie_stream_enc(key);
178         }
179         /* test set 4 */
180         set=4;
181         nessie_print_setheader(set);
182         for(i=0; i<4; ++i){
183                 uint8_t j;
184                 nessie_print_set_vector(set, i);
185                 for(j=0; j<(nessie_stream_ctx.keysize_b+7)/8; ++j){
186                         key[j]=(i*5+j*0x53)&0xff;
187                 }
188                 nessie_stream_enc_large(key);
189         }
190
191         nessie_print_footer();
192 }