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