]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/main-sha256-test.c
eafc4a390cb8dc3f5caaa0bbed79a99cd7e73240
[avr-crypto-lib.git] / test_src / main-sha256-test.c
1 /* main-sha256-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  * SHA-256 test-suit
21  * 
22 */
23
24 #include "config.h"
25 #include "serial-tools.h"
26 #include "uart.h"
27 #include "debug.h"
28
29 #include "sha256.h"
30 #include "nessie_hash_test.h"
31 #include "performance_test.h"
32
33 #include <stdint.h>
34 #include <string.h>
35 #include <stdlib.h>
36 #include "cli.h"
37 #include "shavs.h"
38 #include "hfal_sha256.h"
39 #include "dump.h"
40
41 char* algo_name = "SHA-256";
42
43 /*****************************************************************************
44  *  additional validation-functions                                                                                      *
45  *****************************************************************************/
46 void sha256_next_dummy(void* buffer, void* ctx){
47         sha256_nextBlock(ctx, buffer);
48 }
49
50 void sha256_last_dummy(void* buffer, uint16_t size_b, void* ctx){
51         sha256_lastBlock(ctx, buffer, size_b);
52 }
53
54 void testrun_nessie_sha256(void){
55         nessie_hash_ctx.hashsize_b  = 256;
56         nessie_hash_ctx.blocksize_B = 512/8;
57         nessie_hash_ctx.ctx_size_B  = sizeof(sha256_ctx_t);
58         nessie_hash_ctx.name = algo_name;
59         nessie_hash_ctx.hash_init = (nessie_hash_init_fpt)sha256_init;
60         nessie_hash_ctx.hash_next = (nessie_hash_next_fpt)sha256_next_dummy;
61         nessie_hash_ctx.hash_last = (nessie_hash_last_fpt)sha256_last_dummy;
62         nessie_hash_ctx.hash_conv = (nessie_hash_conv_fpt)sha256_ctx2hash;
63         
64         nessie_hash_run();
65 }
66
67 void testrun_performance_sha256(void){
68         uint64_t t;
69         char str[16];
70         uint8_t data[32];
71         sha256_ctx_t ctx;
72         
73         calibrateTimer();
74         print_overhead();
75         
76         memset(data, 0, 32);
77         
78         startTimer(1);
79         sha256_init(&ctx);
80         t = stopTimer();
81         cli_putstr_P(PSTR("\r\n\tctx-gen time: "));
82         ultoa((unsigned long)t, str, 10);
83         cli_putstr(str);
84         
85         
86         startTimer(1);
87         sha256_nextBlock(&ctx, data);
88         t = stopTimer();
89         cli_putstr_P(PSTR("\r\n\tone-block time: "));
90         ultoa((unsigned long)t, str, 10);
91         cli_putstr(str);
92         
93         
94         startTimer(1);
95         sha256_lastBlock(&ctx, data, 0);
96         t = stopTimer();
97         cli_putstr_P(PSTR("\r\n\tlast block time: "));
98         ultoa((unsigned long)t, str, 10);
99         cli_putstr(str);
100         
101         cli_putstr_P(PSTR("\r\n"));
102 }
103
104 /*****************************************************************************
105  *  main                                                                                                                                         *
106  *****************************************************************************/
107
108 const char nessie_str[]      PROGMEM = "nessie";
109 const char test_str[]        PROGMEM = "test";
110 const char performance_str[] PROGMEM = "performance";
111 const char echo_str[]        PROGMEM = "echo";
112 const char shavs_list_str[]  PROGMEM = "shavs_list";
113 const char shavs_set_str[]   PROGMEM = "shavs_set";
114 const char shavs_test1_str[] PROGMEM = "shavs_test1";
115 const char dump_str[]        PROGMEM = "dump";
116
117 const hfdesc_t* algolist[] PROGMEM = {
118         (hfdesc_t*)&sha256_desc,
119         NULL
120 };
121
122 cmdlist_entry_t cmdlist[] PROGMEM = {
123         { nessie_str,          NULL, testrun_nessie_sha256},
124         { test_str,            NULL, testrun_nessie_sha256},
125         { performance_str,     NULL, testrun_performance_sha256},
126         { echo_str,        (void*)1, (void_fpt)echo_ctrl},
127         { shavs_list_str,      NULL, shavs_listalgos},
128         { shavs_set_str,   (void*)1, (void_fpt)shavs_setalgo},
129         { shavs_test1_str,     NULL, shavs_test1},
130         { dump_str,        (void*)1, (void_fpt)dump},
131         { NULL,                NULL, NULL}
132 };
133
134 int main (void){
135         DEBUG_INIT();
136         
137         cli_rx = uart_getc;
138         cli_tx = uart_putc;             
139         shavs_algolist=(hfdesc_t**)algolist;
140         shavs_algo=(hfdesc_t*)&sha256_desc;
141         for(;;){
142                 cli_putstr_P(PSTR("\r\n\r\nCrypto-VS ("));
143                 cli_putstr(algo_name);
144                 cli_putstr_P(PSTR(")\r\nloaded and running\r\n"));
145                 cmd_interface(cmdlist);
146         }
147 }