]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/main-rsa-test.c
clearing up inclusion structure for easier integration in supercop
[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 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         uint8_t i;
107
108         print_rsa_ctx(&rsa_ctx);
109         m.wordv = mw;
110         c.wordv = cw;
111         m_.wordv = m_w;
112         m.length_B = rsa_ctx.modulus.length_B;
113         m.info = 0;
114         cli_putstr_P(PSTR("\r\ngenerating random message ..."));
115 /*      do{
116                 for(i=0; i<rsa_ctx.modulus.length_B/16; i+=1){
117                         random_block(&(mw[i*16]));
118                 }
119                 bigint_adjust(&m);
120         }while(bigint_cmp_s(&m, &(rsa_ctx.modulus))==1);
121 */
122         memcpy_P(mw, testmsg, 128);
123         bigint_changeendianess(&m);
124         bigint_adjust(&m);
125         rsa_print_item(&m,  PSTR("plain "));
126
127         cli_putstr_P(PSTR("\r\nencrypting ..."));
128         rsa_enc_bigint(&c, &m, &rsa_ctx);
129         rsa_print_item(&c,  PSTR("cipher"));
130
131         cli_putstr_P(PSTR("\r\ndecrypting ..."));
132         rsa_dec_bigint(&m_, &c, &rsa_ctx);
133
134         rsa_print_item(&m_, PSTR("plain'"));
135         cli_putstr_P(PSTR("\r\n plain == plain': "));
136         if(bigint_cmp_s(&m, &m_)){
137                 cli_putstr_P(PSTR("false"));
138         }else{
139                 cli_putstr_P(PSTR("true"));
140         }
141 }
142
143 void reset_prng(void){
144         uint8_t buf[16];
145         memset(buf, 0, 16);
146         random_seed(buf);
147         cli_putstr_P(PSTR("\r\nPRNG reset"));
148 }
149
150 void testrun_performance_bigint(void){
151
152 }
153 /*****************************************************************************
154  *  main                                                                                                                                         *
155  *****************************************************************************/
156
157 const char echo_test_str[]        PROGMEM = "echo-test";
158 const char reset_prng_str[]       PROGMEM = "reset-prng";
159 const char quick_test_str[]       PROGMEM = "quick-test";
160 const char performance_str[]      PROGMEM = "performance";
161 const char echo_str[]             PROGMEM = "echo";
162
163 cmdlist_entry_t cmdlist[] PROGMEM = {
164         { reset_prng_str,       NULL, reset_prng                    },
165         { quick_test_str,       NULL, quick_test                    },
166         { performance_str,      NULL, testrun_performance_bigint    },
167         { echo_str,         (void*)1, (void_fpt)echo_ctrl           },
168         { NULL,                 NULL, NULL                          }
169 };
170
171 int main (void){
172         DEBUG_INIT();
173
174         cli_rx = (cli_rx_fpt)uart0_getc;
175         cli_tx = (cli_tx_fpt)uart0_putc;
176         for(;;){
177                 cli_putstr_P(PSTR("\r\n\r\nCrypto-VS ("));
178                 cli_putstr(algo_name);
179                 cli_putstr_P(PSTR(")\r\nloaded and running\r\n"));
180                 cmd_interface(cmdlist);
181         }
182 }