]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/main-whirlpool-test.c
now including whirlpool
[avr-crypto-lib.git] / test_src / main-whirlpool-test.c
1 /* main-whirlpool-test.c */
2 /*
3     This file is part of the AVR-Crypto-Lib.
4     Copyright (C) 2011  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  * Whirlpool test-suit
21  *
22 */
23
24 #include "config.h"
25
26 #include "uart_i.h"
27 #include "debug.h"
28
29 #include "whirlpool.h"
30 #include "nessie_hash_test.h"
31 #include "performance_test.h"
32 #include "hfal-performance.h"
33 #include "hfal-nessie.h"
34
35 #include <stdint.h>
36 #include <string.h>
37 #include <stdlib.h>
38 #include "cli.h"
39 #include "shavs.h"
40 #include "hfal_whirlpool.h"
41 #include "dump.h"
42
43 char* algo_name = "Whirlpool";
44
45 const hfdesc_t* algolist[] PROGMEM = {
46         (hfdesc_t*)&whirlpool_desc,
47         NULL
48 };
49
50 /*****************************************************************************
51  *  additional validation-functions                                                                                      *
52  *****************************************************************************/
53
54 void testrun_nessie_whirlpool(void){
55         hfal_nessie_multiple(algolist);
56 }
57
58 void testrun_whirlpool(void){
59         whirlpool_ctx_t ctx;
60         uint8_t hash[64];
61         uint8_t data[64];
62         memset(data, 0, 64);
63         data[0] = 'a';
64         data[1] = 'b';
65         data[2] = 'c';
66         whirlpool_init(&ctx);
67         whirlpool_lastBlock(&ctx, data, 3*8);
68         whirlpool_ctx2hash(hash, &ctx);
69         cli_putstr_P(PSTR("\r\nEmpty message hash:"));
70         cli_hexdump_block(hash, 64, 4, 16);
71 }
72
73 void testrun_performance_whirlpool(void){
74         hfal_performance_multiple(algolist);
75 }
76
77 void test_sbox(void){
78         uint8_t i=0;
79         cli_putstr_P(PSTR("\r\nWhirlpool S-Box:"));
80         do{
81                 if(i%16==0){
82                         cli_putstr_P(PSTR("\r\n\t"));
83                 }
84                 cli_hexdump_byte(whirlpool_sbox(i++));
85                 cli_putc(' ');
86         }while(i);
87 }
88
89 void testrun_nessie2(void){
90         uint16_t i;
91         uint8_t j;
92         uint8_t data[128], hash[64];
93         whirlpool_ctx_t ctx;
94         char str[8];
95         memset(data, 0, 128);
96         cli_putstr_P(PSTR("\r\nMessage digests of strings of 0-bits and length L:"));
97         for(i=0; i<1024; ++i){
98                 cli_putstr_P(PSTR("\r\n    L = "));
99                 itoa(i, str, 10);
100                 j=4;
101                 j-= strlen(str);
102                 if(j>3){
103                         j=0;
104                 }
105                 while(j--){
106                         cli_putc(' ');
107                 }
108                 cli_putstr(str);
109                 cli_putstr_P(PSTR(": "));
110                 whirlpool_init(&ctx);
111                 whirlpool_lastBlock(&ctx, data, i);
112                 whirlpool_ctx2hash(hash, &ctx);
113                 cli_hexdump(hash, 64);
114         }
115         cli_putstr_P(PSTR("\r\nMessage digests of all 512-bit strings S containing a single 1-bit:"));
116         for(i=0; i<512; ++i){
117                 cli_putstr_P(PSTR("\r\n    S = "));
118                 data[i/8] = 0x80>>(i&7);
119                 cli_hexdump(data, 64);
120                 cli_putstr_P(PSTR(": "));
121                 whirlpool_init(&ctx);
122                 whirlpool_lastBlock(&ctx, data, 512);
123                 whirlpool_ctx2hash(hash, &ctx);
124                 data[i/8] = 0x00;
125                 cli_hexdump(hash, 64);
126         }
127         cli_putstr_P(PSTR("\r\nIterated message digest computation (100000000 times): "));
128         uint32_t c;
129         memset(hash, 0, 64);
130         for(c=0; c<1000000; ++c){
131                 whirlpool_init(&ctx);
132                 whirlpool_lastBlock(&ctx, hash, 512);
133                 whirlpool_ctx2hash(hash, &ctx);
134         }
135         cli_hexdump(hash, 64);
136         cli_putstr_P(PSTR("\r\n/* finish generating testvectors */\r\n"));
137 }
138
139 /*****************************************************************************
140  *  main                                                                                                                                         *
141  *****************************************************************************/
142
143 const char nessie_str[]      PROGMEM = "nessie";
144 const char nessie2_str[]     PROGMEM = "nessie2";
145 const char test_str[]        PROGMEM = "test";
146 const char test_sbox_str[]   PROGMEM = "test_sbox";
147 const char performance_str[] PROGMEM = "performance";
148 const char echo_str[]        PROGMEM = "echo";
149 const char shavs_list_str[]  PROGMEM = "shavs_list";
150 const char shavs_set_str[]   PROGMEM = "shavs_set";
151 const char shavs_test1_str[] PROGMEM = "shavs_test1";
152 const char shavs_test2_str[] PROGMEM = "shavs_test2";
153 const char shavs_test3_str[] PROGMEM = "shavs_test3";
154 const char dump_str[]        PROGMEM = "dump";
155
156 cmdlist_entry_t cmdlist[] PROGMEM = {
157         { nessie_str,          NULL, testrun_nessie_whirlpool       },
158         { nessie2_str,         NULL, testrun_nessie2                },
159         { test_str,            NULL, testrun_whirlpool              },
160         { test_sbox_str,       NULL, test_sbox                      },
161         { performance_str,     NULL, testrun_performance_whirlpool  },
162         { echo_str,        (void*)1, (void_fpt)echo_ctrl            },
163         { shavs_list_str,      NULL, shavs_listalgos                },
164         { shavs_set_str,   (void*)1, (void_fpt)shavs_setalgo        },
165         { shavs_test1_str,     NULL, shavs_test1                    },
166         { shavs_test2_str,     NULL, shavs_test2                    },
167         { shavs_test3_str,     NULL, shavs_test3                    },
168         { dump_str,        (void*)1, (void_fpt)dump                 },
169         { NULL,                NULL, NULL                           }
170 };
171
172 int main (void){
173         DEBUG_INIT();
174
175         cli_rx = (cli_rx_fpt)uart0_getc;
176         cli_tx = (cli_tx_fpt)uart0_putc;
177         shavs_algolist=(hfdesc_t**)algolist;
178         shavs_algo=(hfdesc_t*)&whirlpool_desc;
179         for(;;){
180                 cli_putstr_P(PSTR("\r\n\r\nCrypto-VS ("));
181                 cli_putstr(algo_name);
182                 cli_putstr_P(PSTR(")\r\nloaded and running\r\n"));
183                 cmd_interface(cmdlist);
184         }
185 }