]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/main-grain-test.c
trivium fixed; further migrating to SCAL
[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_run(&grain_desc);
70 }
71
72
73 void testrun_std_grain(void){
74         grain_ctx_t ctx;
75         uint8_t i, key[10], iv[8], out[10];
76         
77         /* 1 */
78         memset(key, 0, 10);
79         memset(iv, 0, 8);
80         cli_putstr_P(PSTR("\r\n=== std test ==="));
81         cli_putstr_P(PSTR("\r\n key: "));
82         cli_hexdump(key, 10);
83         cli_putstr_P(PSTR("\r\n iv:  "));
84         cli_hexdump(key, 8);
85         grain_init(key, iv, &ctx);
86         for(i=0; i<10; ++i){
87                 out[i] = grain_getbyte_dummy(&ctx);
88         }
89         cli_putstr_P(PSTR("\r\n out: "));
90         cli_hexdump(out, 10);
91         
92         /* 2 */
93         for(i=0; i<8; ++i){
94                 key[i] = i*0x22+1;
95         }
96         key[8]=0x12;
97         key[9]=0x34;
98         
99         for(i=0; i<8; ++i){
100                 iv[i] = i*0x22+1;
101         }
102         cli_putstr_P(PSTR("\r\n\r\n key: "));
103         cli_hexdump(key, 10);
104         cli_putstr_P(PSTR("\r\n iv:  "));
105         cli_hexdump(key, 8);
106         grain_init(key, iv, &ctx);
107         for(i=0; i<10; ++i){
108                 out[i] = grain_getbyte_dummy(&ctx);
109         }
110         cli_putstr_P(PSTR("\r\n out: "));
111         cli_hexdump(out, 10);
112         
113         
114         cli_putstr_P(PSTR("\r\n\r\n"));
115 }
116
117 void testrun_performance_grain(void){
118         uint64_t t;
119         char str[16];
120         uint8_t key[10], iv[8];
121         grain_ctx_t ctx;
122         
123         calibrateTimer();
124         print_overhead();       
125         
126         memset(key,  0, 10);
127         memset(iv,  0, 8);
128         
129         startTimer(1);
130         grain_init(key, iv, &ctx);
131         t = stopTimer();
132         cli_putstr_P(PSTR("\r\n\tctx-gen time: "));
133         ultoa((unsigned long)t, str, 10);
134         cli_putstr(str);        
135         
136         startTimer(1);
137         grain_enc(&ctx);
138         t = stopTimer();
139         cli_putstr_P(PSTR("\r\n\tencrypt time: "));
140         ultoa((unsigned long)t, str, 10);
141         cli_putstr(str);        
142         
143         cli_putstr_P(PSTR("\r\n"));
144 }
145
146 /*****************************************************************************
147  *  main                                                                     *
148  *****************************************************************************/
149
150 const char nessie_str[]      PROGMEM = "nessie";
151 const char test_str[]        PROGMEM = "test";
152 const char performance_str[] PROGMEM = "performance";
153 const char echo_str[]        PROGMEM = "echo";
154
155 cmdlist_entry_t cmdlist[] PROGMEM = {
156         { nessie_str,      NULL, testrun_nessie_grain },
157         { test_str,        NULL, testrun_std_grain},
158         { performance_str, NULL, testrun_performance_grain},
159         { echo_str,    (void*)1, (void_fpt)echo_ctrl},
160         { NULL,            NULL, NULL}
161 };
162
163 int main (void){
164         DEBUG_INIT();
165         
166         cli_rx = (cli_rx_fpt)uart0_getc;
167         cli_tx = (cli_tx_fpt)uart0_putc;                
168         for(;;){
169                 cli_putstr_P(PSTR("\r\n\r\nCrypto-VS ("));
170                 cli_putstr(algo_name);
171                 cli_putstr_P(PSTR(")\r\nloaded and running\r\n"));
172                 cmd_interface(cmdlist);
173         }
174 }