]> git.cryptolib.org Git - arm-crypto-lib.git/blob - test_src/main-sha1-test.c
more files
[arm-crypto-lib.git] / test_src / main-sha1-test.c
1 /* main-sha1-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  * SHA-1 test-suit
21  *
22 */
23
24 #include <stdint.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include "cli.h"
28 #include "dump.h"
29 #include "uart_lowlevel.h"
30 #include "sysclock.h"
31 #include "hw_gptm.h"
32
33 #include "shavs.h"
34 #include "nessie_hash_test.h"
35 #include "performance_test.h"
36 #include "hfal-nessie.h"
37 #include "hfal-performance.h"
38 #include "hfal-test.h"
39
40 #include "sha1.h"
41 #include "hfal_sha1.h"
42
43 void uart0_putc(char byte){
44         uart_putc(UART_0, byte);
45 }
46
47 char uart0_getc(void){
48         return uart_getc(UART_0);
49 }
50
51 const char* algo_name = "SHA-1";
52
53 const hfdesc_t* algolist[] = {
54         (hfdesc_t*)&sha1_desc,
55         NULL
56 };
57 /*****************************************************************************
58  *  additional validation-functions                                                                                      *
59  *****************************************************************************/
60
61 void testrun_nessie_sha1(void){
62         nessie_hash_ctx.hashsize_b  = 160;
63         nessie_hash_ctx.blocksize_B = 512/8;
64         nessie_hash_ctx.ctx_size_B  = sizeof(sha1_ctx_t);
65         nessie_hash_ctx.name = algo_name;
66         nessie_hash_ctx.hash_init = (nessie_hash_init_fpt)sha1_init;
67         nessie_hash_ctx.hash_next = (nessie_hash_next_fpt)sha1_nextBlock;
68         nessie_hash_ctx.hash_last = (nessie_hash_last_fpt)sha1_lastBlock;
69         nessie_hash_ctx.hash_conv = (nessie_hash_conv_fpt)sha1_ctx2hash;
70
71         nessie_hash_run();
72 }
73
74 /*****************************************************************************
75  *  self tests                                                                                                                           *
76  *****************************************************************************/
77
78 void sha1_ctx_dump(sha1_ctx_t *s){
79         uint8_t i;
80         cli_putstr("\r\n==== sha1_ctx_dump ====");
81         for(i=0;i<5;++i){
82                 cli_putstr("\r\na["); cli_hexdump(&i, 1); cli_putstr("]: ");
83                 cli_hexdump(&(s->h[i]), 4);
84         }
85         cli_putstr("\r\nlength"); cli_hexdump(&i, 8);
86 }
87
88 void testrun_sha1(void){
89         sha1_hash_t hash;
90         sha1(&hash,"abc",3*8);
91         cli_putstr("\r\nsha1(\"abc\") = \r\n\t");
92         cli_hexdump(hash,SHA1_HASH_BITS/8);
93
94         sha1(&hash,"\0\0\0\0\0\0\0\0", 8*8);
95         cli_putstr("\r\nsha1(8 x 0x00) = \r\n\t");
96         cli_hexdump(hash,SHA1_HASH_BITS/8);
97 /*
98    Len = 496
99    Msg = 46fe5ed326c8fe376fcc92dc9e2714e2240d3253b105ad
100          fbb256ff7a19bc40975c604ad7c0071c4fd78a7cb64786
101          e1bece548fa4833c04065fe593f6fb10
102    MD  = f220a7457f4588d639dc21407c942e9843f8e26b
103 */
104         sha1(&hash,"\x46\xfe\x5e\xd3\x26\xc8\xfe\x37"
105                    "\x6f\xcc\x92\xdc\x9e\x27\x14\xe2"
106                    "\x24\x0d\x32\x53\xb1\x05\xad\xfb"
107                    "\xb2\x56\xff\x7a\x19\xbc\x40\x97"
108                    "\x5c\x60\x4a\xd7\xc0\x07\x1c\x4f"
109                    "\xd7\x8a\x7c\xb6\x47\x86\xe1\xbe"
110                    "\xce\x54\x8f\xa4\x83\x3c\x04\x06"
111                    "\x5f\xe5\x93\xf6\xfb\x10", 496);
112         cli_putstr("\r\nsha1(tv_496) = \r\n\t");
113         cli_hexdump(hash,SHA1_HASH_BITS/8);
114
115         sha1(&hash,"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",448);
116 //      cli_putstr_P(PSTR("\r\nsha1(\"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq\") = \r\n\t"));
117         cli_hexdump(hash,SHA1_HASH_BITS/8);
118         cli_putstr("\r\nsha1(1,000,000 * 'a') = \r\n\t");
119         {
120                 uint8_t block[SHA1_BLOCK_BITS/8];
121                 uint16_t i;
122                 sha1_ctx_t s;
123                 memset(block,'a',SHA1_BLOCK_BITS/8);
124                 sha1_init(&s);
125                 for(i=0;i<15625; ++i){ /* (1000000/(SHA1_BLOCK_BITS/8)) */
126                         sha1_nextBlock(&s, block);
127                 }
128                 sha1_lastBlock(&s,block,0);
129                 sha1_ctx2hash(&hash, &s);
130         }
131         cli_hexdump(hash,SHA1_HASH_BITS/8);
132
133 }
134
135
136 void testrun_sha1_2(void){
137         sha1_ctx_t ctx;
138         sha1_hash_t hash;
139         sha1(&hash,"",0);
140         cli_putstr("\r\nsha1(NULL) = \r\n\t");
141         cli_hexdump(hash,SHA1_HASH_BYTES);
142
143         memset(hash, 0, SHA1_HASH_BYTES);
144
145         sha1_init(&ctx);
146         sha1_lastBlock(&ctx, "", 0);
147         sha1_ctx2hash(&hash, &ctx);
148         cli_putstr("\r\nsha1(NULL) = \r\n\t");
149         cli_hexdump(hash,SHA1_HASH_BYTES);
150 }
151
152 /*
153 Msg = a38b899cae4edb191d88d861c842b6e32b9b67db66bdbdde8911d2b30fafa765a8190b963c28bf162c46d7b5dbde63556d114f43ceab88c7f65560f96c0c34c0
154 MD = 722246b014af03ef3ba31364fc732a4ab8f38587
155 */
156
157 void testrun_sha1_506(void){
158         uint8_t data[] = {
159                 0xa3, 0x8b, 0x89, 0x9c, 0xae, 0x4e, 0xdb, 0x19,
160                 0x1d, 0x88, 0xd8, 0x61, 0xc8, 0x42, 0xb6, 0xe3,
161                 0x2b, 0x9b, 0x67, 0xdb, 0x66, 0xbd, 0xbd, 0xde,
162                 0x89, 0x11, 0xd2, 0xb3, 0x0f, 0xaf, 0xa7, 0x65,
163                 0xa8, 0x19, 0x0b, 0x96, 0x3c, 0x28, 0xbf, 0x16,
164                 0x2c, 0x46, 0xd7, 0xb5, 0xdb, 0xde, 0x63, 0x55,
165                 0x6d, 0x11, 0x4f, 0x43, 0xce, 0xab, 0x88, 0xc7,
166                 0xf6, 0x55, 0x60, 0xf9, 0x6c, 0x0c, 0x34, 0xc0 };
167         uint8_t ref[] = {
168                 0x72, 0x22, 0x46, 0xb0, 0x14, 0xaf, 0x03, 0xef,
169                 0x3b, 0xa3, 0x13, 0x64, 0xfc, 0x73, 0x2a, 0x4a,
170                 0xb8, 0xf3, 0x85, 0x87 };
171         sha1_hash_t hash;
172         sha1(&hash,data,506);
173         cli_putstr("\r\nsha1(<tv506>) = \r\n\t");
174         cli_hexdump(hash,SHA1_HASH_BYTES);
175         cli_putstr("\r\nshould        = \r\n\t");
176         cli_hexdump(ref,SHA1_HASH_BYTES);
177         if(memcmp(ref, hash, SHA1_HASH_BYTES)==0){
178                 cli_putstr("\r\n[ok]");
179         } else {
180                 cli_putstr("\r\n[fail]");
181         }
182 }
183
184 void testrun_performance_sha1(void){
185         hfal_performance_multiple(algolist);
186 }
187
188
189 /*****************************************************************************
190  *  main                                                                                                                                         *
191  *****************************************************************************/
192
193 const char nessie_str[]       = "nessie";
194 const char test_str[]         = "test";
195 const char test2_str[]        = "test2";
196 const char test506_str[]      = "test506";
197 const char performance_str[]  = "performance";
198 const char echo_str[]         = "echo";
199 const char shavs_list_str[]   = "shavs_list";
200 const char shavs_set_str[]    = "shavs_set";
201 const char shavs_test1_str[]  = "shavs_test1";
202 const char shavs_test2_str[]  = "shavs_test2";
203 const char dump_str[]         = "dump";
204
205
206 const cmdlist_entry_t cmdlist[]  = {
207         { nessie_str,          NULL, testrun_nessie_sha1},
208         { test_str,            NULL, testrun_sha1},
209         { test2_str,           NULL, testrun_sha1_2},
210         { test506_str,         NULL, testrun_sha1_506},
211         { performance_str,     NULL, testrun_performance_sha1},
212         { echo_str,        (void*)1, (void_fpt)echo_ctrl},
213         { shavs_list_str,      NULL, shavs_listalgos},
214         { shavs_set_str,   (void*)1, (void_fpt)shavs_setalgo},
215         { shavs_test1_str,     NULL, shavs_test1},
216         { shavs_test2_str,     NULL, shavs_test2},
217         { dump_str,        (void*)1, (void_fpt)dump},
218         { NULL,                NULL, NULL}
219 };
220
221 int main(void) {
222         sysclk_set_80MHz();
223         sysclk_mosc_verify_enable();
224     uart_init(UART_0, 115200, 8, UART_PARATY_NONE, UART_STOPBITS_ONE);
225     gptm_set_timer_32periodic(TIMER0);
226
227         cli_rx = uart0_getc;
228     cli_tx = uart0_putc;
229
230         shavs_algolist=(hfdesc_t**)algolist;
231         shavs_algo=(hfdesc_t*)&sha1_desc;
232
233         for(;;){
234                 cli_putstr("\r\n\r\nARM-Crypto-Lib VS (");
235                 cli_putstr(algo_name);
236                 cli_putstr("; ");
237                 cli_putstr(__DATE__);
238                 cli_putc(' ');
239                 cli_putstr(__TIME__);
240                 cli_putstr(")\r\nloaded and running\r\n");
241         cmd_interface(cmdlist);
242     }
243
244 }