]> git.cryptolib.org Git - avr-crypto-lib.git/blob - main-grain-test.c
insereated GPLv3 stub
[avr-crypto-lib.git] / main-grain-test.c
1 /* main-grain-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  * grain test-suit
21  * 
22 */
23
24 #include "config.h"
25 #include "serial-tools.h"
26 #include "uart.h"
27 #include "debug.h"
28 #include "cli.h"
29
30 #include "grain.h"
31 #include "nessie_stream_test.h"
32 #include "performance_test.h"
33
34 #include <stdlib.h>
35 #include <stdint.h>
36 #include <string.h>
37
38 char* cipher_name = "Grain";
39
40 /*****************************************************************************
41  *  additional validation-functions                                          *
42  *****************************************************************************/
43 void grain_genctx_dummy(uint8_t* key, uint16_t keysize_b, void* ctx){
44         uint8_t iv[8]={0};
45         grain_init(key, &iv, ctx);
46 }
47
48 uint8_t grain_getbyte_dummy(grain_ctx_t* ctx){
49         uint8_t i,ret=0;
50         for(i=0; i<8; ++i){
51                 ret<<=1;
52                 ret |= grain_enc(ctx);
53         }
54         return ret;
55 }
56
57 uint8_t grain_getbyte_dummy_rev(grain_ctx_t* ctx){
58         uint8_t i,ret=0;
59         for(i=0; i<8; ++i){
60                 ret >>= 1;
61                 ret |= grain_enc(ctx)?0x80:0x00;
62         }
63         return ret;
64 }
65
66 void testrun_nessie_grain(void){
67         nessie_stream_ctx.outsize_b =   8; /* actually unused */
68         nessie_stream_ctx.keysize_b =  80; /* this is the one we have refrence vectors for */
69         nessie_stream_ctx.ivsize_b  =  64;
70         nessie_stream_ctx.name = cipher_name;
71         nessie_stream_ctx.ctx_size_B = sizeof(grain_ctx_t);
72         nessie_stream_ctx.cipher_genctx = (nessie_stream_genctx_fpt)grain_genctx_dummy;
73         nessie_stream_ctx.cipher_enc = (nessie_stream_genenc_fpt)grain_getbyte_dummy_rev;
74         
75         nessie_stream_run();    
76 }
77
78
79 void testrun_std_grain(void){
80         grain_ctx_t ctx;
81         uint8_t i, key[10], iv[8], out[10];
82         
83         /* 1 */
84         memset(key, 0, 10);
85         memset(iv, 0, 8);
86         uart_putstr_P(PSTR("\r\n=== std test ==="));
87         uart_putstr_P(PSTR("\r\n key: "));
88         uart_hexdump(key, 10);
89         uart_putstr_P(PSTR("\r\n iv:  "));
90         uart_hexdump(key, 8);
91         grain_init(key, iv, &ctx);
92         for(i=0; i<10; ++i){
93                 out[i] = grain_getbyte_dummy(&ctx);
94         }
95         uart_putstr_P(PSTR("\r\n out: "));
96         uart_hexdump(out, 10);
97         
98         /* 2 */
99         for(i=0; i<8; ++i){
100                 key[i] = i*0x22+1;
101         }
102         key[8]=0x12;
103         key[9]=0x34;
104         
105         for(i=0; i<8; ++i){
106                 iv[i] = i*0x22+1;
107         }
108         uart_putstr_P(PSTR("\r\n\r\n key: "));
109         uart_hexdump(key, 10);
110         uart_putstr_P(PSTR("\r\n iv:  "));
111         uart_hexdump(key, 8);
112         grain_init(key, iv, &ctx);
113         for(i=0; i<10; ++i){
114                 out[i] = grain_getbyte_dummy(&ctx);
115         }
116         uart_putstr_P(PSTR("\r\n out: "));
117         uart_hexdump(out, 10);
118         
119         
120         uart_putstr_P(PSTR("\r\n\r\n"));
121 }
122
123 void testrun_performance_grain(void){
124         uint16_t i,c;
125         uint64_t t;
126         char str[16];
127         uint8_t key[10], iv[8];
128         grain_ctx_t ctx;
129         
130         calibrateTimer();
131         getOverhead(&c, &i);
132         uart_putstr_P(PSTR("\r\n\r\n=== benchmark ==="));
133         utoa(c, str, 10);
134         uart_putstr_P(PSTR("\r\n\tconst overhead:     "));
135         uart_putstr(str);
136         utoa(i, str, 10);
137         uart_putstr_P(PSTR("\r\n\tinterrupt overhead: "));
138         uart_putstr(str);       
139         
140         memset(key,  0, 10);
141         memset(iv,  0, 8);
142         
143         startTimer(1);
144         grain_init(key, iv, &ctx);
145         t = stopTimer();
146         uart_putstr_P(PSTR("\r\n\tctx-gen time: "));
147         ultoa((unsigned long)t, str, 10);
148         uart_putstr(str);       
149         
150         startTimer(1);
151         grain_enc(&ctx);
152         t = stopTimer();
153         uart_putstr_P(PSTR("\r\n\tencrypt time: "));
154         ultoa((unsigned long)t, str, 10);
155         uart_putstr(str);       
156         
157         uart_putstr_P(PSTR("\r\n"));
158 }
159
160 /*****************************************************************************
161  *  main                                                                     *
162  *****************************************************************************/
163
164 int main (void){
165         char  str[20];
166         DEBUG_INIT();
167         uart_putstr("\r\n");
168
169         uart_putstr_P(PSTR("\r\n\r\nCrypto-VS ("));
170         uart_putstr(cipher_name);
171         uart_putstr_P(PSTR(")\r\nloaded and running\r\n"));
172
173         PGM_P    u   = PSTR("nessie\0test\0performance\0");
174         void_fpt v[] = {testrun_nessie_grain, testrun_std_grain, testrun_performance_grain};
175
176         while(1){ 
177                 if (!getnextwordn(str,20)){DEBUG_S("DBG: W1\r\n"); goto error;}
178                 if(execcommand_d0_P(str, u, v)<0){
179                         uart_putstr_P(PSTR("\r\nunknown command\r\n"));
180                 }
181                 continue;
182         error:
183                 uart_putstr("ERROR\r\n");
184         }       
185 }
186
187
188