3 This file is part of the ARM-Crypto-Lib.
4 Copyright (C) 2010 Daniel Otte (daniel.otte@rub.de)
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.
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.
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/>.
29 #include "uart_lowlevel.h"
34 #include "noekeon_prng.h"
36 #include "bigint_io.h"
38 #include "dsa_key_blob.h"
40 #include "performance_test.h"
41 #include "hfal_sha1.h"
42 #include "base64_enc.h"
43 #include "base64_dec.h"
45 void uart0_putc(char byte){
46 uart_putc(UART_0, byte);
49 char uart0_getc(void){
50 return uart_getc(UART_0);
53 const char* algo_name = "DSA";
55 /*****************************************************************************
56 * additional validation-functions *
57 *****************************************************************************/
61 void load_fix_dsa(void){
62 load_dsa_key_blob(&dsa_ctx);
65 void dsa_print_item(bigint_t* a, const char* pstr){
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){
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];
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];
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];
105 char b64_b[size_o*4/3+5];
106 base64enc(b64_b, bin_b, size_o);
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");
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));
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};
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 ===");
138 cli_putstr(": message: ");
140 cli_hexdump(message, i);
142 cli_putstr("<empty>");
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]");
155 cli_putstr("[FAIL]");
160 void reset_prng(void){
164 cli_putstr("\r\nPRNG reset");
167 void testrun_performance_bigint(void){
170 /*****************************************************************************
172 *****************************************************************************/
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";
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 },
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);
198 cli_putstr("\r\n\r\nARM-Crypto-Lib VS (");
199 cli_putstr(algo_name);
201 cli_putstr(__DATE__);
203 cli_putstr(__TIME__);
204 cli_putstr(")\r\nloaded and running\r\n");
205 cmd_interface(cmdlist);