3 This file is part of the AVR-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/>.
30 #include "noekeon_prng.h"
32 #include "bigint_io.h"
34 #include "dsa_key_blob.h"
37 #include "performance_test.h"
38 #include "hfal_sha1.h"
39 #include "base64_enc.h"
40 #include "base64_dec.h"
45 char* algo_name = "DSA";
47 /*****************************************************************************
48 * additional validation-functions *
49 *****************************************************************************/
53 void load_fix_dsa(void){
54 load_dsa_key_blob(&dsa_ctx);
57 void dsa_print_item(bigint_t* a, PGM_P pstr){
59 cli_putstr_P(PSTR("\r\n"));
61 cli_putstr_P(PSTR(": "));
63 p = a->wordv + a->length_B -1;
64 for(i=0; i<a->length_B-1; ++i){
66 cli_putstr_P(PSTR("\r\n "));
73 cli_putstr_P(PSTR("\r\n "));
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];
89 for(i=s->r.length_B; i>0; --i){
90 bin_b[j++] = s->r.wordv[i-1];
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];
97 char b64_b[size_o*4/3+5];
98 base64enc(b64_b, bin_b, size_o);
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"));
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));
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};
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 ==="));
128 cli_putstr_P(PSTR("\r\n"));
130 cli_putstr_P(PSTR(": message: "));
132 cli_hexdump(message, i);
134 cli_putstr_P(PSTR("<empty>"));
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]"));
147 cli_putstr_P(PSTR("[FAIL]"));
152 void reset_prng(void){
156 cli_putstr_P(PSTR("\r\nPRNG reset"));
159 void testrun_performance_bigint(void){
162 /*****************************************************************************
164 *****************************************************************************/
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";
172 const const 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 },
183 cli_rx = (cli_rx_fpt)uart0_getc;
184 cli_tx = (cli_tx_fpt)uart0_putc;
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);