]> git.cryptolib.org Git - arm-crypto-lib.git/blob - scal/scal-nessie.c
fixing some header comments
[arm-crypto-lib.git] / scal / scal-nessie.c
1 /* scal-nessie.c */
2 /*
3     This file is part of the ARM-Crypto-Lib.
4     Copyright (C) 2006-2011 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 #include <stdint.h>
21 #include <string.h>
22 #include "streamcipher_descriptor.h"
23 #include "scal-basic.h"
24 #include "nessie_common.h"
25 #include "memxor.h"
26
27 static
28 uint8_t estream_flag = 0;
29
30 void scal_nessie_set_estream(uint8_t v){
31         estream_flag = v?1:0;
32 }
33
34 uint8_t scal_nessie_get_estream(void){
35         return estream_flag?1:0;
36 }
37
38
39
40 static const uint8_t normal_hooks[] = {
41                 0, 192/64, 256/64, 448/64
42 };
43
44 static const uint16_t long_hooks[] = {
45                 0, 65472/64, 65536/64, 131008/64
46 };
47
48 static const char stream0_n[] = "stream[0..63]";
49 static const char stream1_n[] = "stream[192..255]";
50 static const char stream2_n[] = "stream[256..319]";
51 static const char stream3_n[] = "stream[448..511]";
52
53 static const char streamX_n_estream[] = "xor-digest";
54 static const char streamX_n_nessie[] = "stream[0..511]xored";
55
56 static const char* stream_n_str[] = {
57                 stream0_n,
58                 stream1_n,
59                 stream2_n,
60                 stream3_n,
61                 streamX_n_nessie
62 };
63
64 static const char stream1_l[] = "stream[65472..65535]";
65 static const char stream2_l[] = "stream[65536..65599]";
66 static const char stream3_l[] = "stream[131008..131071]";
67 static const char streamX_l_estream[] = "xor-digest";
68 static const char streamX_l_nessie[] = "stream[0..131071]xored";
69
70 static const char* stream_l_str[] = {
71                 stream0_n,
72                 stream1_l,
73                 stream2_l,
74                 stream3_l,
75                 streamX_l_nessie
76 };
77
78 static const uint8_t list_of_keys[][20] = {
79         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
80           0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
81         { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc,
82           0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0x00, 0x00, 0x00, 0x00 },
83         { 0x67, 0x45, 0x23, 0x01, 0xef, 0xcd, 0xab, 0x89, 0x98, 0xba,
84           0xdc, 0xfe, 0x10, 0x32, 0x54, 0x76, 0xc3, 0xd2, 0xe1, 0xf0 }
85 };
86
87 static const uint8_t list_of_ivs[][4] = {
88         { 0x00, 0x00, 0x00, 0x00 },
89         { 0x01, 0x01, 0x01, 0x01 },
90         { 0x01, 0x35, 0x77, 0xaf }
91 };
92
93 static
94 void normal_block(scgen_ctx_t *ctx){
95         uint8_t xor_block[64];
96         uint8_t block[64];
97         const uint8_t* hook_ptr = normal_hooks;
98         const char* *hook_str_ptr = stream_n_str;
99         uint8_t i;
100
101         memset(xor_block, 0, 64);
102         for(i=0; i<=448/64; ++i){
103                 scal_cipher_gen_fillblock(block, 64, ctx);
104                 memxor(xor_block, block, 64);
105                 if(i==*hook_ptr){
106                         ++hook_ptr;
107                         nessie_print_item(*(hook_str_ptr++), block, 64);
108                 }
109         }
110         nessie_print_item(*(hook_str_ptr), xor_block, 64);
111 }
112
113 static
114 void long_block(scgen_ctx_t *ctx){
115         uint8_t xor_block[64];
116         uint8_t block[64];
117         const uint16_t* hook_ptr = long_hooks;
118         const char* *hook_str_ptr = stream_l_str;
119         uint16_t i;
120
121         memset(xor_block, 0, 64);
122         for(i=0; i<=131008/64; ++i){
123                 scal_cipher_gen_fillblock(block, 64, ctx);
124                 memxor(xor_block, block, 64);
125                 if(i==*hook_ptr){
126                         ++hook_ptr;
127                         nessie_print_item(*(hook_str_ptr++), block, 64);
128                 }
129                 if(i%64==0){
130                         NESSIE_SEND_ALIVE;
131                 }
132         }
133         nessie_print_item(*(hook_str_ptr), xor_block, 64);
134 }
135
136 void scal_nessie_stream_run(const scdesc_t *desc, uint16_t keysize_b, uint16_t ivsize_b){
137         uint8_t key[(keysize_b+7)/8];
138         uint8_t iv[(ivsize_b+7)/8];
139         uint16_t v;
140         scgen_ctx_t ctx;
141         nessie_print_header(desc->name, keysize_b, 0, 0, 0, ivsize_b?ivsize_b:((uint16_t)-1));
142         if(estream_flag){
143                 stream_n_str[4] = streamX_n_estream;
144                 stream_l_str[4] = streamX_l_estream;
145         }else{
146                 stream_n_str[4] = streamX_n_nessie;
147                 stream_l_str[4] = streamX_l_nessie;
148         }
149         memset(iv, 0, (ivsize_b+7)/8);
150         memset(key, 0, (keysize_b+7)/8);
151         /***  Test SET 1 ***/
152         nessie_print_setheader(1);
153         for(v=0;v<keysize_b; v+=estream_flag?9:1){
154                 nessie_print_set_vector(1,v);
155                 key[v/8] |= 0x80>>(v&7);
156                 nessie_print_item("key", key, (keysize_b+7)/8);
157                 if(ivsize_b){
158                         nessie_print_item("IV", iv, (ivsize_b+7)/8);
159                 }
160                 scal_cipher_init(desc, key, keysize_b, iv, ivsize_b, &ctx);
161                 normal_block(&ctx);
162                 key[v/8] = 0;
163                 scal_cipher_free(&ctx);
164         }
165         /***  Test SET 2 ***/
166         nessie_print_setheader(2);
167         for(v=0;v<256; v+=estream_flag?9:1){
168                 nessie_print_set_vector(2,v);
169                 memset(key, v&0xff, (keysize_b+7)/8);
170                 nessie_print_item("key", key, (keysize_b+7)/8);
171                 if(ivsize_b){
172                         nessie_print_item("IV", iv, (ivsize_b+7)/8);
173                 }
174                 scal_cipher_init(desc, key, keysize_b, iv, ivsize_b, &ctx);
175                 normal_block(&ctx);
176                 scal_cipher_free(&ctx);
177         }
178         /***  Test SET 3 ***/
179         nessie_print_setheader(3);
180         for(v=0;v<256; v+=estream_flag?9:1){
181                 uint8_t i;
182                 nessie_print_set_vector(3,v);
183                 for(i=0; i<((keysize_b+7)/8); ++i){
184                         key[i]=(i+v)&0xff;
185                 }
186                 nessie_print_item("key", key, (keysize_b+7)/8);
187                 if(ivsize_b){
188                         nessie_print_item("IV", iv, (ivsize_b+7)/8);
189                 }
190                 scal_cipher_init(desc, key, keysize_b, iv, ivsize_b, &ctx);
191                 normal_block(&ctx);
192                 scal_cipher_free(&ctx);
193         }
194         /***  Test SET 4 ***/
195         nessie_print_setheader(4);
196         for(v=0;v<4; ++v){
197                 uint8_t i;
198                 nessie_print_set_vector(4,v);
199                 for(i=0; i<((keysize_b+7)/8); ++i){
200                         key[i]=(i*0x53+v*5)&0xff;
201                 }
202                 nessie_print_item("key", key, (keysize_b+7)/8);
203                 if(ivsize_b){
204                         nessie_print_item("IV", iv, (ivsize_b+7)/8);
205                 }
206                 scal_cipher_init(desc, key, keysize_b, iv, ivsize_b, &ctx);
207                 long_block(&ctx);
208                 scal_cipher_free(&ctx);
209         }
210         if(ivsize_b==0){ /* exit if there is no IV */
211                 nessie_print_footer();
212                 return;
213         }
214         /***  Test SET 5 ***/
215         nessie_print_setheader(5);
216         memset(key, 0, (keysize_b+7)/8);
217         for(v=0;v<ivsize_b; v+=estream_flag?9:1){
218                 nessie_print_set_vector(5,v);
219                 iv[v/8] |= 0x80>>(v&7);
220                 nessie_print_item("key", key, (keysize_b+7)/8);
221                 nessie_print_item("IV", iv, (ivsize_b+7)/8);
222                 scal_cipher_init(desc, key, keysize_b, iv, ivsize_b, &ctx);
223                 normal_block(&ctx);
224                 scal_cipher_free(&ctx);
225                 iv[v/8] = 0;
226         }
227         /***  Test SET 6 ***/
228         nessie_print_setheader(6);
229         for(v=0;v<4; ++v){
230                 uint8_t i;
231                 nessie_print_set_vector(6,v);
232                 for(i=0; i<((keysize_b+7)/8); ++i){
233                         key[i]=(i*0x53+v*5)&0xff;
234                 }
235                 for(i=0; i<((ivsize_b+7)/8); ++i){
236                         iv[i]=(i*0x67+v*9+13)&0xff;
237                 }
238                 nessie_print_item("key", key, (keysize_b+7)/8);
239                 nessie_print_item("IV", iv, (ivsize_b+7)/8);
240                 scal_cipher_init(desc, key, keysize_b, iv, ivsize_b, &ctx);
241                 long_block(&ctx);
242                 scal_cipher_free(&ctx);
243         }
244         /***  Test SET 7 ***/
245         if(!estream_flag){
246                 nessie_print_setheader(7);
247                 uint8_t u;
248                 for(v=0;v<3; ++v){
249                         for(u=0; u<3; ++u){
250                                 uint8_t i;
251                                 nessie_print_set_vector(7,v*3+u);
252                                 for(i=0; i<((keysize_b+7)/8); ++i){
253                                         key[i]=list_of_keys[v][i%20];
254                                 }
255                                 for(i=0; i<((ivsize_b+7)/8); ++i){
256                                         key[i]=*((uint8_t*)list_of_keys+4*u+(i%4));
257                                 }
258                         }
259                         nessie_print_item("key", key, (keysize_b+7)/8);
260                         nessie_print_item("IV", iv, (ivsize_b+7)/8);
261                         scal_cipher_init(desc, key, keysize_b, iv, ivsize_b, &ctx);
262                         long_block(&ctx);
263                         scal_cipher_free(&ctx);
264                 }
265         }
266         nessie_print_footer();
267 }
268
269 void scal_nessie_run(const scdesc_t* desc){
270         uint16_t keysizes_count, ivsizes_count,i,j;
271         uint16_t *keysizes=NULL, *ivsizes=NULL;
272         keysizes_count = get_keysizes(desc->valid_keysize_desc, &keysizes);
273         ivsizes_count =  get_keysizes(desc->valid_ivsize_desc,  &ivsizes);
274         for(i=0; i<keysizes_count; ++i){
275                 for(j=0; j<ivsizes_count; ++j){
276                         scal_nessie_stream_run(desc, keysizes[i], ivsizes[j]);
277                 }
278         }
279 }