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