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