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