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/>.
24 #include "main-test-common.h"
27 #include "noekeon_prng.h"
29 #include "bigint_io.h"
31 #include "dsa_key_blob.h"
33 #include "performance_test.h"
34 #include "hfal_sha1.h"
35 #include "base64_enc.h"
36 #include "base64_dec.h"
38 char* algo_name = "DSA";
40 /*****************************************************************************
41 * additional validation-functions *
42 *****************************************************************************/
46 void load_fix_dsa(void){
47 load_dsa_key_blob(&dsa_ctx);
50 void dsa_print_item(bigint_t* a, PGM_P pstr){
52 cli_putstr_P(PSTR("\r\n"));
54 cli_putstr_P(PSTR(": "));
56 p = a->wordv + a->length_W -1;
57 for(i=0; i<a->length_W-1; ++i){
59 cli_putstr_P(PSTR("\r\n "));
66 cli_putstr_P(PSTR("\r\n "));
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];
82 for(i=s->r.length_W; i>0; --i){
83 bin_b[j++] = s->r.wordv[i-1];
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];
90 char b64_b[size_o*4/3+5];
91 base64enc(b64_b, bin_b, size_o);
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"));
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));
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};
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 ==="));
121 cli_putstr_P(PSTR("\r\n"));
123 cli_putstr_P(PSTR(": message: "));
125 cli_hexdump(message, i);
127 cli_putstr_P(PSTR("<empty>"));
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]"));
140 cli_putstr_P(PSTR("[FAIL]"));
145 void reset_prng(void){
149 cli_putstr_P(PSTR("\r\nPRNG reset"));
152 void testrun_performance_bigint(void){
155 /*****************************************************************************
157 *****************************************************************************/
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";
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 },
177 welcome_msg(algo_name);
178 cmd_interface(cmdlist);