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