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 "rsa_key_blob.h"
37 #include "performance_test.h"
42 char* algo_name = "RSA";
44 /*****************************************************************************
45 * additional validation-functions *
46 *****************************************************************************/
50 void load_fix_rsa(void){
51 load_rsa_key_blob(&rsa_ctx);
54 void rsa_print_item(bigint_t* a, PGM_P pstr){
56 cli_putstr_P(PSTR("\r\n"));
58 cli_putstr_P(PSTR(": "));
60 p = a->wordv + a->length_B -1;
61 for(i=0; i<a->length_B-1; ++i){
63 cli_putstr_P(PSTR("\r\n "));
70 cli_putstr_P(PSTR("\r\n "));
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"));
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
101 void quick_test(void){
105 uint8_t mw[rsa_ctx.modulus.length_B], cw[rsa_ctx.modulus.length_B], m_w[rsa_ctx.modulus.length_B];
107 print_rsa_ctx(&rsa_ctx);
111 m.length_B = rsa_ctx.modulus.length_B;
113 cli_putstr_P(PSTR("\r\ngenerating random message ..."));
115 for(i=0; i<rsa_ctx.modulus.length_B/16; i+=1){
116 random_block(&(mw[i*16]));
119 }while(bigint_cmp_s(&m, &(rsa_ctx.modulus))==1);
121 memcpy_P(mw, testmsg, 128);
122 bigint_changeendianess(&m);
124 rsa_print_item(&m, PSTR("plain "));
126 cli_putstr_P(PSTR("\r\nencrypting ..."));
127 rsa_enc_bigint(&c, &m, &rsa_ctx);
128 rsa_print_item(&c, PSTR("cipher"));
130 cli_putstr_P(PSTR("\r\ndecrypting ..."));
131 rsa_dec_bigint(&m_, &c, &rsa_ctx);
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"));
138 cli_putstr_P(PSTR("true"));
142 void reset_prng(void){
146 cli_putstr_P(PSTR("\r\nPRNG reset"));
149 void testrun_performance_bigint(void){
152 /*****************************************************************************
154 *****************************************************************************/
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";
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 },
173 cli_rx = (cli_rx_fpt)uart0_getc;
174 cli_tx = (cli_tx_fpt)uart0_putc;
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);