]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/main-sha1-test.c
e8ef85f7d54d6a069cdcc1b88723fd8bbeb367af
[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
108 void testrun_sha1_2(void){
109         sha1_ctx_t ctx;
110         sha1_hash_t hash;
111         sha1(&hash,"",0);
112         uart_putstr("\r\nsha1(NULL) = \r\n\t");
113         uart_hexdump(hash,SHA1_HASH_BYTES);
114
115         memset(hash, 0, SHA1_HASH_BYTES);
116
117         sha1_init(&ctx);
118         sha1_lastBlock(&ctx, "", 0);
119         sha1_ctx2hash(hash, &ctx); 
120         uart_putstr("\r\nsha1(NULL) = \r\n\t");
121         uart_hexdump(hash,SHA1_HASH_BYTES);
122 }
123
124
125 void testrun_performance_sha1(void){
126         uint64_t t;
127         char str[16];
128         uint8_t data[32];
129         sha1_ctx_t ctx;
130         
131         calibrateTimer();
132         print_overhead();
133         
134         memset(data, 0, 32);
135         
136         startTimer(1);
137         sha1_init(&ctx);
138         t = stopTimer();
139         uart_putstr_P(PSTR("\r\n\tctx-gen time: "));
140         ultoa((unsigned long)t, str, 10);
141         uart_putstr(str);
142         
143         
144         startTimer(1);
145         sha1_nextBlock(&ctx, data);
146         t = stopTimer();
147         uart_putstr_P(PSTR("\r\n\tone-block time: "));
148         ultoa((unsigned long)t, str, 10);
149         uart_putstr(str);
150         
151         
152         startTimer(1);
153         sha1_lastBlock(&ctx, data, 0);
154         t = stopTimer();
155         uart_putstr_P(PSTR("\r\n\tlast block time: "));
156         ultoa((unsigned long)t, str, 10);
157         uart_putstr(str);
158         
159         uart_putstr_P(PSTR("\r\n"));
160 }
161
162
163 /*****************************************************************************
164  *  main                                                                                                                                         *
165  *****************************************************************************/
166
167 const char nessie_str[]      PROGMEM = "nessie";
168 const char test_str[]        PROGMEM = "test";
169 const char test2_str[]       PROGMEM = "test2";
170 const char performance_str[] PROGMEM = "performance";
171 const char echo_str[]        PROGMEM = "echo";
172
173 cmdlist_entry_t cmdlist[] PROGMEM = {
174         { nessie_str,      NULL, testrun_nessie_sha1},
175         { test_str,        NULL, testrun_sha1},
176         { test2_str,       NULL, testrun_sha1_2},
177         { performance_str, NULL, testrun_performance_sha1},
178         { echo_str,    (void*)1, (void_fpt)echo_ctrl},
179         { NULL,            NULL, NULL}
180 };
181
182 int main (void){
183         DEBUG_INIT();
184         uart_putstr("\r\n");
185         cli_rx = uart_getc;
186         cli_tx = uart_putc;             
187         for(;;){
188                 uart_putstr_P(PSTR("\r\n\r\nCrypto-VS ("));
189                 uart_putstr(algo_name);
190                 uart_putstr_P(PSTR(")\r\nloaded and running\r\n"));
191                 cmd_interface(cmdlist);
192         }
193 }