]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/main-grain-test.c
a lot of fixes
[avr-crypto-lib.git] / test_src / main-grain-test.c
1 /* main-grain-test.c */
2 /*
3     This file is part of the AVR-Crypto-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
26 #include "uart_i.h"
27 #include "debug.h"
28 #include "cli.h"
29
30 #include "grain.h"
31 #include "scal_grain.h"
32 #include "scal-basic.h"
33 #include "scal-nessie.h"
34 #include "performance_test.h"
35
36 #include <stdlib.h>
37 #include <stdint.h>
38 #include <string.h>
39
40 char* algo_name = "Grain";
41
42 /*****************************************************************************
43  *  additional validation-functions                                          *
44  *****************************************************************************/
45 void grain_genctx_dummy(uint8_t* key, uint16_t keysize_b, void* ctx){
46         uint8_t iv[8]={0};
47         grain_init(key, &iv, ctx);
48 }
49
50 uint8_t grain_getbyte_dummy(grain_ctx_t* ctx){
51         uint8_t i,ret=0;
52         for(i=0; i<8; ++i){
53                 ret<<=1;
54                 ret |= grain_enc(ctx);
55         }
56         return ret;
57 }
58
59 uint8_t grain_getbyte_dummy_rev(grain_ctx_t* ctx){
60         uint8_t i,ret=0;
61         for(i=0; i<8; ++i){
62                 ret >>= 1;
63                 ret |= grain_enc(ctx)?0x80:0x00;
64         }
65         return ret;
66 }
67
68 void testrun_nessie_grain(void){
69         scal_nessie_set_estream(1);
70         scal_nessie_run(&grain_desc);
71 }
72
73
74 void testrun_std_grain(void){
75         grain_ctx_t ctx;
76         uint8_t i, key[10], iv[8], out[10];
77         
78         /* 1 */
79         memset(key, 0, 10);
80         memset(iv, 0, 8);
81         cli_putstr_P(PSTR("\r\n=== std test ==="));
82         cli_putstr_P(PSTR("\r\n key: "));
83         cli_hexdump(key, 10);
84         cli_putstr_P(PSTR("\r\n iv:  "));
85         cli_hexdump(key, 8);
86         grain_init(key, iv, &ctx);
87         for(i=0; i<10; ++i){
88                 out[i] = grain_getbyte_dummy(&ctx);
89         }
90         cli_putstr_P(PSTR("\r\n out: "));
91         cli_hexdump(out, 10);
92         
93         /* 2 */
94         for(i=0; i<8; ++i){
95                 key[i] = i*0x22+1;
96         }
97         key[8]=0x12;
98         key[9]=0x34;
99         
100         for(i=0; i<8; ++i){
101                 iv[i] = i*0x22+1;
102         }
103         cli_putstr_P(PSTR("\r\n\r\n key: "));
104         cli_hexdump(key, 10);
105         cli_putstr_P(PSTR("\r\n iv:  "));
106         cli_hexdump(key, 8);
107         grain_init(key, iv, &ctx);
108         for(i=0; i<10; ++i){
109                 out[i] = grain_getbyte_dummy(&ctx);
110         }
111         cli_putstr_P(PSTR("\r\n out: "));
112         cli_hexdump(out, 10);
113         
114         
115         cli_putstr_P(PSTR("\r\n\r\n"));
116 }
117
118 void testrun_performance_grain(void){
119         uint64_t t;
120         char str[16];
121         uint8_t key[10], iv[8];
122         grain_ctx_t ctx;
123         
124         calibrateTimer();
125         print_overhead();       
126         
127         memset(key,  0, 10);
128         memset(iv,  0, 8);
129         
130         startTimer(1);
131         grain_init(key, iv, &ctx);
132         t = stopTimer();
133         cli_putstr_P(PSTR("\r\n\tctx-gen time: "));
134         ultoa((unsigned long)t, str, 10);
135         cli_putstr(str);        
136         
137         startTimer(1);
138         grain_enc(&ctx);
139         t = stopTimer();
140         cli_putstr_P(PSTR("\r\n\tencrypt time: "));
141         ultoa((unsigned long)t, str, 10);
142         cli_putstr(str);        
143         
144         cli_putstr_P(PSTR("\r\n"));
145 }
146
147 /*****************************************************************************
148  *  main                                                                     *
149  *****************************************************************************/
150
151 const char nessie_str[]      PROGMEM = "nessie";
152 const char test_str[]        PROGMEM = "test";
153 const char performance_str[] PROGMEM = "performance";
154 const char echo_str[]        PROGMEM = "echo";
155
156 const cmdlist_entry_t cmdlist[] PROGMEM = {
157         { nessie_str,      NULL, testrun_nessie_grain },
158         { test_str,        NULL, testrun_std_grain},
159         { performance_str, NULL, testrun_performance_grain},
160         { echo_str,    (void*)1, (void_fpt)echo_ctrl},
161         { NULL,            NULL, NULL}
162 };
163
164 int main (void){
165         DEBUG_INIT();
166         
167         cli_rx = (cli_rx_fpt)uart0_getc;
168         cli_tx = (cli_tx_fpt)uart0_putc;                
169         for(;;){
170                 cli_putstr_P(PSTR("\r\n\r\nCrypto-VS ("));
171                 cli_putstr(algo_name);
172                 cli_putstr_P(PSTR(")\r\nloaded and running\r\n"));
173                 cmd_interface(cmdlist);
174         }
175 }