]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/main-sha1-test.c
new cli system
[avr-crypto-lib.git] / test_src / main-sha1-test.c
1 /* main-sha1-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-1 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 "sha1.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
38 char* algo_name = "SHA-1";
39
40 /*****************************************************************************
41  *  additional validation-functions                                                                                      *
42  *****************************************************************************/
43 void sha1_next_dummy(void* buffer, void* ctx){
44         sha1_nextBlock(ctx, buffer);
45 }
46
47 void sha1_last_dummy(void* buffer, uint16_t size_b, void* ctx){
48         sha1_lastBlock(ctx, buffer, size_b);
49 }
50
51 void testrun_nessie_sha1(void){
52         nessie_hash_ctx.hashsize_b  = 160;
53         nessie_hash_ctx.blocksize_B = 512/8;
54         nessie_hash_ctx.ctx_size_B  = sizeof(sha1_ctx_t);
55         nessie_hash_ctx.name = algo_name;
56         nessie_hash_ctx.hash_init = (nessie_hash_init_fpt)sha1_init;
57         nessie_hash_ctx.hash_next = (nessie_hash_next_fpt)sha1_next_dummy;
58         nessie_hash_ctx.hash_last = (nessie_hash_last_fpt)sha1_last_dummy;
59         nessie_hash_ctx.hash_conv = (nessie_hash_conv_fpt)sha1_ctx2hash;
60         
61         nessie_hash_run();
62 }
63
64 /*****************************************************************************
65  *  self tests                                                                                                                           *
66  *****************************************************************************/
67
68 void sha1_ctx_dump(sha1_ctx_t *s){
69         uint8_t i;
70         uart_putstr("\r\n==== sha1_ctx_dump ====");
71         for(i=0;i<5;++i){
72                 uart_putstr("\r\na["); uart_hexdump(&i, 1); uart_putstr("]: ");
73                 uart_hexdump(&(s->h[i]), 4);
74         }
75         uart_putstr("\r\nlength"); uart_hexdump(&i, 8);
76
77
78 void testrun_sha1(void){
79         sha1_hash_t hash;
80         sha1(&hash,"abc",3*8);
81         uart_putstr("\r\nsha1(\"abc\") = \r\n\t");
82         uart_hexdump(hash,SHA1_HASH_BITS/8);
83         
84         sha1(&hash,"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",448);
85         uart_putstr("\r\nsha1(\"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq\") = \r\n\t");
86         uart_hexdump(hash,SHA1_HASH_BITS/8);
87         
88         uart_putstr("\r\nsha1(1,000,000 * 'a') = \r\n\t");
89         {
90                 uint8_t block[SHA1_BLOCK_BITS/8];
91                 uint16_t i;
92                 sha1_ctx_t s;
93                 memset(block,'a',SHA1_BLOCK_BITS/8);
94                 sha1_init(&s);
95                 for(i=0;i<15625; ++i){ /* (1000000/(SHA1_BLOCK_BITS/8)) */
96                         sha1_nextBlock(&s, block);
97                 }
98                 sha1_lastBlock(&s,block,0);
99                 sha1_ctx2hash(&hash, &s);
100         }
101         uart_hexdump(hash,SHA1_HASH_BITS/8);
102         
103
104         uart_putstr("\r\nx");
105 }
106
107 void testrun_performance_sha1(void){
108         uint64_t t;
109         char str[16];
110         uint8_t data[32];
111         sha1_ctx_t ctx;
112         
113         calibrateTimer();
114         print_overhead();
115         
116         memset(data, 0, 32);
117         
118         startTimer(1);
119         sha1_init(&ctx);
120         t = stopTimer();
121         uart_putstr_P(PSTR("\r\n\tctx-gen time: "));
122         ultoa((unsigned long)t, str, 10);
123         uart_putstr(str);
124         
125         
126         startTimer(1);
127         sha1_nextBlock(&ctx, data);
128         t = stopTimer();
129         uart_putstr_P(PSTR("\r\n\tone-block time: "));
130         ultoa((unsigned long)t, str, 10);
131         uart_putstr(str);
132         
133         
134         startTimer(1);
135         sha1_lastBlock(&ctx, data, 0);
136         t = stopTimer();
137         uart_putstr_P(PSTR("\r\n\tlast block time: "));
138         ultoa((unsigned long)t, str, 10);
139         uart_putstr(str);
140         
141         uart_putstr_P(PSTR("\r\n"));
142 }
143
144
145 /*****************************************************************************
146  *  main                                                                                                                                         *
147  *****************************************************************************/
148
149 const char nessie_str[]      PROGMEM = "nessie";
150 const char test_str[]        PROGMEM = "test";
151 const char performance_str[] PROGMEM = "performance";
152 const char echo_str[]        PROGMEM = "echo";
153
154 cmdlist_entry_t cmdlist[] PROGMEM = {
155         { nessie_str,      NULL, testrun_nessie_sha1},
156         { test_str,        NULL, testrun_sha1},
157         { performance_str, NULL, testrun_performance_sha1},
158         { echo_str,    (void*)1, (void_fpt)echo_ctrl},
159         { NULL,            NULL, NULL}
160 };
161
162 int main (void){
163         DEBUG_INIT();
164         uart_putstr("\r\n");
165         cli_rx = uart_getc;
166         cli_tx = uart_putc;             
167         for(;;){
168                 uart_putstr_P(PSTR("\r\n\r\nCrypto-VS ("));
169                 uart_putstr(algo_name);
170                 uart_putstr_P(PSTR(")\r\nloaded and running\r\n"));
171                 cmd_interface(cmdlist);
172         }
173 }