]> git.cryptolib.org Git - arm-crypto-lib.git/blob - test_src/main-sha512-test.c
'hardening' infrastucture against toolchain bugs
[arm-crypto-lib.git] / test_src / main-sha512-test.c
1 /* main-sha512-test.c */
2 /*
3     This file is part of the ARM-Crypto-Lib.
4     Copyright (C) 2006-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  * SHA-512 test-suit
21  *
22 */
23
24 #include "main-test-common.h"
25 #include "uart_lowlevel.h"
26
27 #include "shavs.h"
28 #include "nessie_hash_test.h"
29 #include "performance_test.h"
30 #include "hfal-nessie.h"
31 #include "hfal-performance.h"
32 #include "hfal-test.h"
33
34 #include "sha512.h"
35 #include "hfal_sha512.h"
36
37 const char* algo_name = "SHA-512";
38
39 const hfdesc_t* algolist[] = {
40         (hfdesc_t*)&sha512_desc,
41         NULL
42 };
43
44 /*****************************************************************************
45  *  additional validation-functions                                                                                      *
46  *****************************************************************************/
47
48 void testrun_nessie_sha512(void){
49         hfal_nessie_multiple(algolist);
50 }
51
52 void testrun_performance_sha512(void){
53         hfal_performance_multiple(algolist);
54 }
55
56 void simple_test(void){
57         const char *msg = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"
58                                   "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
59
60         uint8_t hash[64];
61         sha512_ctx_t ctx;
62         cli_putstr("\r\nDBG: init ..."); uart_flush(0);
63         sha512_init(&ctx);
64         cli_putstr("\r\nDBG: init done"); uart_flush(0);
65         sha512_lastBlock(&ctx, msg, 3*8);
66         cli_putstr("\r\nDBG: lastBlock done"); uart_flush(0);
67         sha512_ctx2hash(hash, &ctx);
68         cli_putstr("\r\n hash = ");
69         cli_hexdump(hash, 64);
70
71
72         cli_putstr("\r\nDBG: init ..."); uart_flush(0);
73         sha512_init(&ctx);
74         cli_putstr("\r\nDBG: init done"); uart_flush(0);
75         sha512_lastBlock(&ctx, msg, 896);
76         cli_putstr("\r\nDBG: lastBlock done"); uart_flush(0);
77         sha512_ctx2hash(hash, &ctx);
78         cli_putstr("\r\n hash = ");
79         cli_hexdump(hash, 64);
80
81         uint32_t c=0;
82         uint8_t buffer[128];
83         memset(buffer, 'a', 128);
84         cli_putstr("\r\nDBG: init ..."); uart_flush(0);
85         sha512_init(&ctx);
86         cli_putstr("\r\nDBG: init done"); uart_flush(0);
87         do{
88                 sha512_nextBlock(&ctx, buffer);
89                 c += 128;
90         }while(c+128<1000000L);
91         sha512_lastBlock(&ctx, buffer, (1000000-c)*8);
92         cli_putstr("\r\nDBG: lastBlock done"); uart_flush(0);
93         sha512_ctx2hash(hash, &ctx);
94         cli_putstr("\r\n hash = ");
95         cli_hexdump(hash, 64);
96
97
98 }
99
100 /*
101 void test_monte(void){
102         uint8_t data1[] = {
103      0xF4, 0x1E, 0xCE, 0x26, 0x13, 0xE4, 0x57, 0x39,
104      0x15, 0x69, 0x6B, 0x5A, 0xDC, 0xD5, 0x1C, 0xA3,
105          0x28, 0xBE, 0x3B, 0xF5, 0x66, 0xA9, 0xCA, 0x99,
106          0xC9, 0xCE, 0xB0, 0x27, 0x9C, 0x1C, 0xB0, 0xA7,
107      0xF4, 0x1E, 0xCE, 0x26, 0x13, 0xE4, 0x57, 0x39,
108      0x15, 0x69, 0x6B, 0x5A, 0xDC, 0xD5, 0x1C, 0xA3,
109          0x28, 0xBE, 0x3B, 0xF5, 0x66, 0xA9, 0xCA, 0x99,
110          0xC9, 0xCE, 0xB0, 0x27, 0x9C, 0x1C, 0xB0, 0xA7,
111      0xF4, 0x1E, 0xCE, 0x26, 0x13, 0xE4, 0x57, 0x39,
112      0x15, 0x69, 0x6B, 0x5A, 0xDC, 0xD5, 0x1C, 0xA3,
113          0x28, 0xBE, 0x3B, 0xF5, 0x66, 0xA9, 0xCA, 0x99,
114          0xC9, 0xCE, 0xB0, 0x27, 0x9C, 0x1C, 0xB0, 0xA7 };
115
116    uint8_t data2[] = {
117      0xF4, 0x1E, 0xCE, 0x26, 0x13, 0xE4, 0x57, 0x39,
118      0x15, 0x69, 0x6B, 0x5A, 0xDC, 0xD5, 0x1C, 0xA3,
119          0x28, 0xBE, 0x3B, 0xF5, 0x66, 0xA9, 0xCA, 0x99,
120          0xC9, 0xCE, 0xB0, 0x27, 0x9C, 0x1C, 0xB0, 0xA7,
121      0xF4, 0x1E, 0xCE, 0x26, 0x13, 0xE4, 0x57, 0x39,
122      0x15, 0x69, 0x6B, 0x5A, 0xDC, 0xD5, 0x1C, 0xA3,
123          0x28, 0xBE, 0x3B, 0xF5, 0x66, 0xA9, 0xCA, 0x99,
124          0xC9, 0xCE, 0xB0, 0x27, 0x9C, 0x1C, 0xB0, 0xA7,
125      0xFD, 0xDF, 0x1B, 0x37, 0xDD, 0x34, 0xB3, 0xB2,
126      0x01, 0xD4, 0x3C, 0x57, 0xBC, 0xDE, 0x11, 0x58,
127      0x38, 0xF0, 0xDF, 0x70, 0x1D, 0xA9, 0x3C, 0x3B,
128      0xF2, 0xC9, 0xC8, 0x68, 0x96, 0xE7, 0xE6, 0xC7 };
129    uint8_t hash[SHA256_HASH_BYTES];
130    sha256((sha256_hash_t*)hash, data1, 3*32*8);
131    cli_putstr("\r\n hash(data1) = ");
132    cli_hexdump(hash, 32);
133    sha256((sha256_hash_t*)hash, data2, 3*32*8);
134    cli_putstr("\r\n hash(data2) = ");
135    cli_hexdump(hash, 32);
136 }
137
138 void test_monte2(void){
139         uint8_t data[] = {
140         0x6c, 0xd4, 0xc0, 0xc5, 0xcb, 0x2c, 0xa2, 0xa0,
141         0xf1, 0xd1, 0xae, 0xce, 0xba, 0xc0, 0x3b, 0x52,
142         0xe6, 0x4e, 0xa0, 0x3d, 0x1a, 0x16, 0x54, 0x37,
143         0x29, 0x36, 0x54, 0x5b, 0x92, 0xbb, 0xc5, 0x48,
144         0x4a, 0x59, 0xdb, 0x74, 0xbb, 0x60, 0xf9, 0xc4,
145         0x0c, 0xeb, 0x1a, 0x5a, 0xa3, 0x5a, 0x6f, 0xaf,
146         0xe8, 0x03, 0x49, 0xe1, 0x4c, 0x25, 0x3a, 0x4e,
147         0x8b, 0x1d, 0x77, 0x61, 0x2d, 0xdd, 0x81, 0xac,
148         0xe9, 0x26, 0xae, 0x8b, 0x0a, 0xf6, 0xe5, 0x31,
149         0x76, 0xdb, 0xff, 0xcc, 0x2a, 0x6b, 0x88, 0xc6,
150         0xbd, 0x76, 0x5f, 0x93, 0x9d, 0x3d, 0x17, 0x8a,
151         0x9b, 0xde, 0x9e, 0xf3, 0xaa, 0x13, 0x1c, 0x61,
152         0xe3, 0x1c, 0x1e, 0x42, 0xcd, 0xfa, 0xf4, 0xb4,
153         0xdc, 0xde, 0x57, 0x9a, 0x37, 0xe1, 0x50, 0xef,
154         0xbe, 0xf5, 0x55, 0x5b, 0x4c, 0x1c, 0xb4, 0x04,
155         0x39, 0xd8, 0x35, 0xa7, 0x24, 0xe2, 0xfa, 0xe7 };
156
157    uint8_t hash[SHA256_HASH_BYTES];
158    sha256((sha256_hash_t*)hash, data, 1024);
159    cli_putstr("\r\n hash(data) = ");
160    cli_hexdump(hash, 32);
161 }
162 */
163 /*****************************************************************************
164  *  main                                                                                                                                         *
165  *****************************************************************************/
166
167 const char nessie_str[]       = "nessie";
168 const char test_str[]         = "test";
169 //const char monte_str[]        = "monte";
170 //const char monte2_str[]       = "monte2";
171 const char performance_str[]  = "performance";
172 const char echo_str[]         = "echo";
173 const char shavs_list_str[]   = "shavs_list";
174 const char shavs_set_str[]    = "shavs_set";
175 const char shavs_test1_str[]  = "shavs_test1";
176 const char shavs_test2_str[]  = "shavs_test2";
177 const char shavs_test3_str[]  = "shavs_test3";
178 const char dump_str[]         = "dump";
179
180 const cmdlist_entry_t cmdlist[]  = {
181         { nessie_str,          NULL, testrun_nessie_sha512          },
182         { test_str,            NULL, simple_test                    },
183 //      { monte_str,           NULL, test_monte                     },
184 //      { monte2_str,          NULL, test_monte2                    },
185         { performance_str,     NULL, testrun_performance_sha512     },
186         { echo_str,        (void*)1, (void_fpt)echo_ctrl            },
187         { shavs_list_str,      NULL, shavs_listalgos                },
188         { shavs_set_str,   (void*)1, (void_fpt)shavs_setalgo        },
189         { shavs_test1_str,     NULL, shavs_test1                    },
190         { shavs_test2_str,     NULL, shavs_test2                    },
191         { shavs_test3_str,     NULL, shavs_test3                    },
192         { dump_str,        (void*)1, (void_fpt)dump                 },
193         { NULL,                NULL, NULL                           }
194 };
195
196 int main(void) {
197         main_setup();
198
199         shavs_algolist=(hfdesc_t**)algolist;
200         shavs_algo=(hfdesc_t*)&sha512_desc;
201
202         for(;;){
203                 welcome_msg(algo_name);
204                 cmd_interface(cmdlist);
205     }
206
207 }