]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/main-rsa-test.c
a lot of fixes
[avr-crypto-lib.git] / test_src / main-rsa-test.c
1 /* main-rsa-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  * RSA test-suit
21  *
22 */
23
24 #include "config.h"
25
26 #include "uart_i.h"
27 #include "debug.h"
28
29 #include "noekeon.h"
30 #include "noekeon_prng.h"
31 #include "bigint.h"
32 #include "bigint_io.h"
33 #include "rsa.h"
34 #include "rsa_key_blob.h"
35
36 #include "cli.h"
37 #include "performance_test.h"
38 #include <stdint.h>
39 #include <string.h>
40 #include <stdlib.h>
41
42 char* algo_name = "RSA";
43
44 /*****************************************************************************
45  *  additional validation-functions                                                                                      *
46  *****************************************************************************/
47
48 rsa_ctx_t rsa_ctx;
49
50 void load_fix_rsa(void){
51         load_rsa_key_blob(&rsa_ctx);
52 }
53
54 void rsa_print_item(bigint_t* a, PGM_P pstr){
55         uint8_t *p;
56         cli_putstr_P(PSTR("\r\n"));
57         cli_putstr_P(pstr);
58         cli_putstr_P(PSTR(": "));
59         uint16_t i;
60         p = a->wordv + a->length_B -1;
61         for(i=0; i<a->length_B-1; ++i){
62                 if(i%16==0){
63                         cli_putstr_P(PSTR("\r\n    "));
64                 }
65                 cli_hexdump(p, 1);
66                 cli_putc(':');
67                 --p;
68         }
69         if(i%16==0){
70                 cli_putstr_P(PSTR("\r\n    "));
71         }
72         cli_hexdump(p, 1);
73 }
74
75 void print_rsa_ctx(rsa_ctx_t* ctx){
76         cli_putstr_P(PSTR("\r\n === RSA context ==="));
77         rsa_print_item(&(ctx->modulus), PSTR("modulus"));
78         rsa_print_item(&(ctx->pubexp),  PSTR("public exponent"));
79         rsa_print_item(&(ctx->privexp), PSTR("private exponent"));
80 }
81
82 const uint8_t testmsg[128] PROGMEM = {
83                 0x25, 0x11, 0x1e, 0x08, 0x1c, 0x72, 0x57, 0xbf,
84                 0x46, 0xbb, 0x56, 0xe8, 0x0b, 0x24, 0x0d, 0x72,
85                 0x77, 0x11, 0x12, 0x10, 0xec, 0xb9, 0xe4, 0x6f,
86                 0xe0, 0x6c, 0x83, 0x65, 0xe2, 0xe5, 0x1a, 0x3c,
87                 0xdb, 0x6c, 0x51, 0x8d, 0xbc, 0xd0, 0x30, 0xb6,
88                 0xca, 0x01, 0xb4, 0x03, 0x03, 0xe9, 0x86, 0x86,
89                 0xfe, 0x76, 0x4e, 0xb7, 0x47, 0xb5, 0x06, 0x15,
90                 0x92, 0x38, 0xc2, 0x0d, 0x3c, 0xfa, 0x53, 0xe3,
91                 0xe3, 0x57, 0xeb, 0x34, 0xb8, 0xda, 0x7b, 0x0e,
92                 0x06, 0x28, 0x1d, 0x4a, 0x14, 0xcc, 0x56, 0x8d,
93                 0xf3, 0x0f, 0x40, 0x2e, 0x23, 0x45, 0xe7, 0xcd,
94                 0xc5, 0x83, 0x8f, 0xaa, 0x55, 0x55, 0x7d, 0xbd,
95                 0x19, 0x63, 0xbd, 0x17, 0xda, 0xfd, 0x18, 0xd1,
96                 0x62, 0x43, 0xc7, 0x33, 0x39, 0x2c, 0xda, 0x64,
97                 0x27, 0x96, 0xa0, 0x83, 0xa0, 0xf9, 0x3b, 0x1e,
98                 0x17, 0x71, 0x59, 0xaa, 0x43, 0x3b, 0xeb, 0x80
99 };
100
101 void quick_test(void){
102         load_fix_rsa();
103
104         bigint_t m,c,m_;
105         uint8_t mw[rsa_ctx.modulus.length_B], cw[rsa_ctx.modulus.length_B], m_w[rsa_ctx.modulus.length_B];
106
107         print_rsa_ctx(&rsa_ctx);
108         m.wordv = mw;
109         c.wordv = cw;
110         m_.wordv = m_w;
111         m.length_B = rsa_ctx.modulus.length_B;
112         m.info = 0;
113         cli_putstr_P(PSTR("\r\ngenerating random message ..."));
114 /*      do{
115                 for(i=0; i<rsa_ctx.modulus.length_B/16; i+=1){
116                         random_block(&(mw[i*16]));
117                 }
118                 bigint_adjust(&m);
119         }while(bigint_cmp_s(&m, &(rsa_ctx.modulus))==1);
120 */
121         memcpy_P(mw, testmsg, 128);
122         bigint_changeendianess(&m);
123         bigint_adjust(&m);
124         rsa_print_item(&m,  PSTR("plain "));
125
126         cli_putstr_P(PSTR("\r\nencrypting ..."));
127         rsa_enc_bigint(&c, &m, &rsa_ctx);
128         rsa_print_item(&c,  PSTR("cipher"));
129
130         cli_putstr_P(PSTR("\r\ndecrypting ..."));
131         rsa_dec_bigint(&m_, &c, &rsa_ctx);
132
133         rsa_print_item(&m_, PSTR("plain'"));
134         cli_putstr_P(PSTR("\r\n plain == plain': "));
135         if(bigint_cmp_s(&m, &m_)){
136                 cli_putstr_P(PSTR("false"));
137         }else{
138                 cli_putstr_P(PSTR("true"));
139         }
140 }
141
142 void reset_prng(void){
143         uint8_t buf[16];
144         memset(buf, 0, 16);
145         random_seed(buf);
146         cli_putstr_P(PSTR("\r\nPRNG reset"));
147 }
148
149 void testrun_performance_bigint(void){
150
151 }
152 /*****************************************************************************
153  *  main                                                                                                                                         *
154  *****************************************************************************/
155
156 const char echo_test_str[]        PROGMEM = "echo-test";
157 const char reset_prng_str[]       PROGMEM = "reset-prng";
158 const char quick_test_str[]       PROGMEM = "quick-test";
159 const char performance_str[]      PROGMEM = "performance";
160 const char echo_str[]             PROGMEM = "echo";
161
162 const cmdlist_entry_t cmdlist[] PROGMEM = {
163         { reset_prng_str,       NULL, reset_prng                    },
164         { quick_test_str,       NULL, quick_test                    },
165         { performance_str,      NULL, testrun_performance_bigint    },
166         { echo_str,         (void*)1, (void_fpt)echo_ctrl           },
167         { NULL,                 NULL, NULL                          }
168 };
169
170 int main (void){
171         DEBUG_INIT();
172
173         cli_rx = (cli_rx_fpt)uart0_getc;
174         cli_tx = (cli_tx_fpt)uart0_putc;
175         for(;;){
176                 cli_putstr_P(PSTR("\r\n\r\nCrypto-VS ("));
177                 cli_putstr(algo_name);
178                 cli_putstr_P(PSTR(")\r\nloaded and running\r\n"));
179                 cmd_interface(cmdlist);
180         }
181 }