]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/main-mqq160-sign-test.c
a lot of fixes
[avr-crypto-lib.git] / test_src / main-mqq160-sign-test.c
1 /* main-mqq160-sign-test.c */
2 /*
3     This file is part of the AVR-Crypto-Lib.
4     Copyright (C) 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  * MQQ160-sign test-suit
21  *
22 */
23
24 #include "config.h"
25 #include "uart_i.h"
26 #include "debug.h"
27
28 #include "cli.h"
29 #include "mqq160-sign.h"
30 #include "mqq160-sign_P.h"
31 #include "mqq160-sign_testkey.h"
32 #include "performance_test.h"
33 #include <stdint.h>
34 #include <string.h>
35 #include <stdlib.h>
36 #include <avr/pgmspace.h>
37 #include "stack_measuring.h"
38
39 char* algo_name = "MQQ160-sign";
40
41
42 /*****************************************************************************
43  *  additional validation-functions                                                                                      *
44  *****************************************************************************/
45 const uint8_t test_hash[20] PROGMEM =
46 {
47         (uint8_t)0x64, (uint8_t)0xFE, (uint8_t)0x2A, (uint8_t)0x85,
48         (uint8_t)0xBB, (uint8_t)0x8C, (uint8_t)0x54, (uint8_t)0x5C,
49         (uint8_t)0x65, (uint8_t)0x74, (uint8_t)0xA0, (uint8_t)0xF3,
50         (uint8_t)0xD0, (uint8_t)0xAF, (uint8_t)0x96, (uint8_t)0xB9,
51         (uint8_t)0x0F, (uint8_t)0x17, (uint8_t)0xF3, (uint8_t)0xAD
52 };
53
54 void performance_mqq(void){
55         uint8_t hash[20];
56         uint8_t signature[20];
57         long t;
58         char str[3*sizeof(long)+2];
59         uint8_t tmp;
60         uint16_t s1, s2;
61         stack_measuring_ctx_t smctx;
62         memcpy_P(hash, test_hash, 20);
63
64         uint8_t key_buffer[MQQ160SIGN_KEY_SIZE];
65         mqq160_sign_key_t key;
66         mqq_load_pgm_key(key_buffer, &key, &testkey_P);
67
68         cli_putstr_P(PSTR("\r\n=== Performance of MQQ160-SIGN ==="));
69         calibrateTimer();
70         startTimer(0);
71         START_TIMER;
72         mqq160_sign_P(signature, hash, &testkey_P);
73         STOP_TIMER;
74         t = stopTimer();
75         ltoa(t, str, 10);
76         cli_putstr_P(PSTR("\r\n cycles for mqq160_sign_P: "));
77         tmp = 12-strlen(str);
78         while(tmp--){
79                 cli_putc(' ');
80         }
81         cli_putstr(str);
82
83         calibrateTimer();
84         startTimer(0);
85         START_TIMER;
86         mqq160_sign(signature, hash, &key);
87         STOP_TIMER;
88         t = stopTimer();
89         ltoa(t, str, 10);
90         cli_putstr_P(PSTR("\r\n cycles for mqq160_sign:   "));
91         tmp = 12-strlen(str);
92         while(tmp--){
93                 cli_putc(' ');
94         }
95         cli_putstr(str);
96
97         stack_measure_init(&smctx, 0xAA);
98         mqq160_sign_P(signature, hash, &testkey_P);
99         s1 = stack_measure_final(&smctx);
100         stack_measure_init(&smctx, 0x55);
101         mqq160_sign_P(signature, hash, &testkey_P);
102         s2 = stack_measure_final(&smctx);
103         s1 = (s1>s2)?s1:s2;
104         ltoa((long)s1, str, 10);
105         cli_putstr_P(PSTR("\r\n stack for mqq160_sign_P:  "));
106         tmp = 12-strlen(str);
107         while(tmp--){
108                 cli_putc(' ');
109         }
110         cli_putstr(str);
111         stack_measure_init(&smctx, 0xAA);
112         mqq160_sign(signature, hash, &key);
113         s1 = stack_measure_final(&smctx);
114         stack_measure_init(&smctx, 0x55);
115         mqq160_sign_P(signature, hash, &testkey_P);
116         s2 = stack_measure_final(&smctx);
117         s1 = (s1>s2)?s1:s2;
118         ltoa((long)s1, str, 10);
119         cli_putstr_P(PSTR("\r\n stack for mqq160_sign:    "));
120         tmp = 12-strlen(str);
121         while(tmp--){
122                 cli_putc(' ');
123         }
124         cli_putstr(str);
125
126         cli_putstr_P(PSTR("\r\n=== End of performance figures ==="));
127 }
128
129 void testrun_mqq_mem(void){
130         uint8_t hash[20];
131         uint8_t signature[20];
132         memcpy_P(hash, test_hash, 20);
133
134         uint8_t key_buffer[MQQ160SIGN_KEY_SIZE];
135         mqq160_sign_key_t key;
136         mqq_load_pgm_key(key_buffer, &key, &testkey_P);
137         mqq160_sign(signature, hash, &key);
138         cli_putstr_P(PSTR("\r\ntest signature (RAM):   "));
139         cli_hexdump(signature, 20);
140 }
141
142 void testrun_mqq_flash(void){
143         uint8_t hash[20];
144         uint8_t signature[20];
145         memcpy_P(hash, test_hash, 20);
146
147         mqq160_sign_P(signature, hash, &testkey_P);
148         cli_putstr_P(PSTR("\r\ntest signature (FLASH): "));
149         cli_hexdump(signature, 20);
150 }
151
152 void testrun_mqq(void){
153         uint8_t hash[20];
154         uint8_t signature[20];
155         memcpy_P(hash, test_hash, 20);
156
157         uint8_t key_buffer[MQQ160SIGN_KEY_SIZE];
158         mqq160_sign_key_t key;
159         mqq_load_pgm_key(key_buffer, &key, &testkey_P);
160         mqq160_sign(signature, hash, &key);
161         cli_putstr_P(PSTR("\r\ntest signature (RAM):   "));
162         cli_hexdump(signature, 20);
163
164         mqq160_sign_P(signature, hash, &testkey_P);
165         cli_putstr_P(PSTR("\r\ntest signature (FLASH): "));
166         cli_hexdump(signature, 20);
167 }
168
169 /*****************************************************************************
170  *  main                                                                                                                                         *
171  *****************************************************************************/
172
173 const char test_str[]        PROGMEM = "test";
174 const char test_flash_str[]  PROGMEM = "flash";
175 const char test_mem_str[]    PROGMEM = "mem";
176 const char performance_str[] PROGMEM = "performance";
177 const char echo_str[]        PROGMEM = "echo";
178
179 const cmdlist_entry_t cmdlist[] PROGMEM = {
180         { test_str,                    NULL, testrun_mqq                   },
181         { test_flash_str,              NULL, testrun_mqq_flash             },
182         { test_mem_str,                NULL, testrun_mqq_mem               },
183         { performance_str,             NULL, performance_mqq               },
184         { echo_str,                (void*)1, (void_fpt)echo_ctrl           },
185         { NULL,                        NULL, NULL                          }
186 };
187
188 int main (void){
189         DEBUG_INIT();
190
191         cli_rx = (cli_rx_fpt)uart0_getc;
192         cli_tx = (cli_tx_fpt)uart0_putc;
193         for(;;){
194                 cli_putstr_P(PSTR("\r\n\r\nCrypto-VS ("));
195                 cli_putstr(algo_name);
196                 cli_putstr_P(PSTR("; "));
197                 cli_putstr(__DATE__);
198                 cli_putstr_P(PSTR(" "));
199                 cli_putstr(__TIME__);
200                 cli_putstr_P(PSTR(")\r\nloaded and running\r\n"));
201
202                 cmd_interface(cmdlist);
203         }
204 }