]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/main-ecdsa-test.c
nist P-192 optimized recude for ecdsa
[avr-crypto-lib.git] / test_src / main-ecdsa-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  * ECDSA 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 "nist_p192.h"
31
32 #include "performance_test.h"
33 #include "hfal_sha1.h"
34 #include "base64_enc.h"
35 #include "base64_dec.h"
36
37 char* algo_name = "ECDSA";
38
39 /*****************************************************************************
40  *  additional validation-functions                                                                                      *
41  *****************************************************************************/
42
43 void testrun_performance_bigint(void){
44     printf_P(PSTR("\n=== performance measurement ===\n"));
45     unsigned i, j;
46     bigint_t a,b,v;
47     bigint_word_t v_w[192 * 2 / BIGINT_WORD_SIZE];
48     bigint_word_t a_w[192 * 2 / BIGINT_WORD_SIZE];
49     bigint_word_t b_w[192 * 2 / BIGINT_WORD_SIZE];
50     uint32_t time_a, time_b;
51     int32_t time_diff;
52     int16_t faster_percent;
53     v.wordv = v_w;
54     for(j = 0; j < 32; ++j){
55         do{
56             for(i = 0; i < 192 * 2 / BIGINT_WORD_SIZE; ++i){
57                 ((uint8_t*)v_w)[i] = random();
58             }
59             v.length_W = 192 * 2 / BIGINT_WORD_SIZE;
60             v.info = 0;
61             bigint_adjust(&v);
62         }while(0);
63
64     //    printf_P(PSTR("candidate:\n"));
65     //    bigint_print_hex(&v);
66         a.wordv = a_w;
67         b.wordv = b_w;
68         calibrateTimer();
69
70     //    printf_P(PSTR("\n  going to test optimized version: ...\n"));
71         uart0_flush();
72         time_a = 0;
73         for(i = 0; i < 16; ++i){
74             bigint_copy(&a, &v);
75             startTimer(1);
76             START_TIMER;
77             bigint_reduce_p192(&a);
78             STOP_TIMER;
79             time_a += stopTimer();
80         }
81     //    printf_P(PSTR("    took: %"PRIu32" cycles\nresult:"), time);
82     //    bigint_print_hex(&a);
83
84
85     //    printf_P(PSTR("\n  going to test not-optimized version: ...\n"));
86     //    uart0_flush();
87         time_b = 0;
88         for(i = 0; i < 16; ++i){
89             bigint_copy(&b, &v);
90             startTimer(1);
91             START_TIMER;
92             bigint_reduce(&b, &nist_curve_p192_p);
93             STOP_TIMER;
94             time_b += stopTimer();
95         }
96     //    printf_P(PSTR("    took: %"PRIu32" cycles\nresult:"), time);
97     //    bigint_print_hex(&b);
98
99         time_diff = time_b - time_a;
100         faster_percent = (time_diff * 100) / time_b;
101
102         printf_P(PSTR("  delta: %7"PRId32"  (%3"PRId16"%%)  :-"), time_diff, faster_percent);
103         if(bigint_cmp_u(&a, &b)){
104             printf_P(PSTR("(\n"));
105         } else {
106             printf_P(PSTR(")\n"));
107         }
108         uart0_flush();
109     }
110 }
111
112 /*****************************************************************************
113  *  main                                                                                                                                         *
114  *****************************************************************************/
115
116 const char echo_test_str[]        PROGMEM = "echo-test";
117 const char reset_prng_str[]       PROGMEM = "reset-prng";
118 const char quick_test_str[]       PROGMEM = "quick-test";
119 const char performance_str[]      PROGMEM = "performance";
120 const char echo_str[]             PROGMEM = "echo";
121
122 const const cmdlist_entry_t cmdlist[] PROGMEM = {
123 //      { reset_prng_str,       NULL, reset_prng                    },
124 //      { quick_test_str,       NULL, quick_test                    },
125         { performance_str,      NULL, testrun_performance_bigint    },
126         { echo_str,         (void*)1, (void_fpt)echo_ctrl           },
127         { NULL,                 NULL, NULL                          }
128 };
129
130 int main (void){
131     int8_t r;
132     main_setup();
133
134     for(;;){
135         welcome_msg(algo_name);
136         r = cmd_interface(cmdlist);
137         printf("r = %"PRId8"\n", r);
138         cli_putstr_P(PSTR("\r\nHello!\r\n"));
139         }
140 }