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