]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/main-dsa-test.c
stack usage measurement
[avr-crypto-lib.git] / test_src / main-dsa-test.c
1 /* main-dsa-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  * DSA test-suit
21  *
22 */
23
24 #include "config.h"
25
26 #include "uart_i.h"
27 #include "debug.h"
28
29 #include "noekeon.h"
30 #include "noekeon_prng.h"
31 #include "bigint.h"
32 #include "bigint_io.h"
33 #include "dsa.h"
34 #include "dsa_key_blob.h"
35
36 #include "cli.h"
37 #include "performance_test.h"
38 #include "hfal_sha1.h"
39 #include "base64_enc.h"
40 #include "base64_dec.h"
41 #include <stdint.h>
42 #include <string.h>
43 #include <stdlib.h>
44
45 char* algo_name = "DSA";
46
47 /*****************************************************************************
48  *  additional validation-functions                                                                                      *
49  *****************************************************************************/
50
51 dsa_ctx_t dsa_ctx;
52
53 void load_fix_dsa(void){
54         load_dsa_key_blob(&dsa_ctx);
55 }
56
57 void dsa_print_item(bigint_t* a, PGM_P pstr){
58         uint8_t *p;
59         cli_putstr_P(PSTR("\r\n"));
60         cli_putstr_P(pstr);
61         cli_putstr_P(PSTR(": "));
62         uint16_t i;
63         p = a->wordv + a->length_B -1;
64         for(i=0; i<a->length_B-1; ++i){
65                 if(i%16==0){
66                         cli_putstr_P(PSTR("\r\n    "));
67                 }
68                 cli_hexdump(p, 1);
69                 cli_putc(':');
70                 --p;
71         }
72         if(i%16==0){
73                 cli_putstr_P(PSTR("\r\n    "));
74         }
75         cli_hexdump(p, 1);
76 }
77
78 void dsa_print_signature_b64(dsa_signature_t* s){
79         uint16_t size_r, size_s, size_o, i,j;
80         size_r = s->r.length_B +2;
81         size_s = s->s.length_B +2;
82         size_o = size_r + size_s +2;
83         uint8_t bin_b[size_o];
84         bin_b[0] = 0x30;
85         bin_b[1] = size_o -2;
86         bin_b[2] = 0x02;
87         bin_b[3] = size_r-2;
88         j=4;
89         for(i=s->r.length_B; i>0;  --i){
90                 bin_b[j++] = s->r.wordv[i-1];
91         }
92         bin_b[j++] = 0x02;
93         bin_b[j++] = size_s -2;
94         for(i=s->s.length_B; i>0;  --i){
95                 bin_b[j++] = s->s.wordv[i-1];
96         }
97         char b64_b[size_o*4/3+5];
98         base64enc(b64_b, bin_b, size_o);
99         cli_putstr(b64_b);
100 }
101
102 void dsa_print_ctx(dsa_ctx_t* ctx){
103         dsa_print_item(&(ctx->priv), PSTR("private"));
104         dsa_print_item(&(ctx->pub), PSTR("public"));
105         dsa_print_item(&(ctx->domain.p), PSTR("P"));
106         dsa_print_item(&(ctx->domain.q), PSTR("Q"));
107         dsa_print_item(&(ctx->domain.g), PSTR("G"));
108 }
109
110 void dsa_print_signature(const dsa_signature_t* sig){
111         cli_putstr_P(PSTR("\r\nDSA-Signature:\r\n r:"));
112         bigint_print_hex(&(sig->r));
113         cli_putstr_P(PSTR("\r\n s:"));
114         bigint_print_hex(&(sig->s));
115 }
116
117 void quick_test(void){
118         dsa_signature_t dsa_sig;
119         uint8_t i, t=0, message[] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef};
120         load_fix_dsa();
121         uint8_t dsa_sig_s_b[dsa_ctx.domain.q.length_B],
122                 dsa_sig_r_b[dsa_ctx.domain.q.length_B];
123         dsa_print_ctx(&dsa_ctx);
124         dsa_sig.r.wordv = dsa_sig_r_b;
125         dsa_sig.s.wordv = dsa_sig_s_b;
126         cli_putstr_P(PSTR("\r\n\r\n=== DSA QUICK TEST ==="));
127         for(i=0; i<8; ++i){
128                 cli_putstr_P(PSTR("\r\n"));
129                 cli_putc('1'+i);
130                 cli_putstr_P(PSTR(": message: "));
131                 if (i){
132                         cli_hexdump(message, i);
133                 }else{
134                         cli_putstr_P(PSTR("<empty>"));
135                 }
136                 cli_putstr_P(PSTR("\r\n computing signature ... "));
137                 dsa_sign_message(&dsa_sig, message, i*8, &sha1_desc, &dsa_ctx, random8);
138                 dsa_print_signature(&dsa_sig);
139                 cli_putstr_P(PSTR("\r\n base64:\r\n--- SIGNATURE ---\r\n "));
140                 dsa_print_signature_b64(&dsa_sig);
141                 cli_putstr_P(PSTR("\r\n verifying signature ... "));
142                 t = dsa_verify_message(&dsa_sig, message, i*8, &sha1_desc, &dsa_ctx);
143                 cli_putstr_P(PSTR("\r\n verification: "));
144                 if(t==DSA_SIGNATURE_OK){
145                         cli_putstr_P(PSTR("[PASS]"));
146                 }else{
147                         cli_putstr_P(PSTR("[FAIL]"));
148                 }
149         }
150 }
151
152 void reset_prng(void){
153         uint8_t buf[16];
154         memset(buf, 0, 16);
155         random_seed(buf);
156         cli_putstr_P(PSTR("\r\nPRNG reset"));
157 }
158
159 void testrun_performance_bigint(void){
160
161 }
162 /*****************************************************************************
163  *  main                                                                                                                                         *
164  *****************************************************************************/
165
166 const char echo_test_str[]        PROGMEM = "echo-test";
167 const char reset_prng_str[]       PROGMEM = "reset-prng";
168 const char quick_test_str[]       PROGMEM = "quick-test";
169 const char performance_str[]      PROGMEM = "performance";
170 const char echo_str[]             PROGMEM = "echo";
171
172 cmdlist_entry_t cmdlist[] PROGMEM = {
173         { reset_prng_str,       NULL, reset_prng                    },
174         { quick_test_str,       NULL, quick_test                    },
175         { performance_str,      NULL, testrun_performance_bigint    },
176         { echo_str,         (void*)1, (void_fpt)echo_ctrl           },
177         { NULL,                 NULL, NULL                          }
178 };
179
180 int main (void){
181         DEBUG_INIT();
182
183         cli_rx = (cli_rx_fpt)uart0_getc;
184         cli_tx = (cli_tx_fpt)uart0_putc;
185         for(;;){
186                 cli_putstr_P(PSTR("\r\n\r\nCrypto-VS ("));
187                 cli_putstr(algo_name);
188                 cli_putstr_P(PSTR(")\r\nloaded and running\r\n"));
189                 cmd_interface(cmdlist);
190         }
191 }