--- /dev/null
+/* rsa_key_blob.c */
+/*
+ This file is part of the AVR-Crypto-Lib.
+ Copyright (C) 2010 Daniel Otte (daniel.otte@rub.de)
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include <stdlib.h>
+#include <stdint.h>
+#include <avr/pgmspace.h>
+#include "cli.h"
+#include "rsa.h"
+#include "bigint/bigint.h"
+
+#define RSA_KEYSIZE 1024
+
+#if RSA_KEYSIZE == 2048
+#define N_LEN_B (32*8)
+#define E_LEN_B (3)
+#define D_LEN_B (32*8)
+#define ALL_LEN_B (N_LEN_B+E_LEN_B+D_LEN_B)
+
+static uint8_t rsa_key_blob[] PROGMEM = {
+ /* n */
+ 0xDA, 0xBC, 0x4A, 0x42, 0x02, 0x6F, 0x7F, 0xC2,
+ 0xEB, 0x5A, 0x73, 0x2A, 0x84, 0x25, 0xB5, 0x56,
+ 0xC7, 0x99, 0x25, 0x0A, 0x50, 0x6F, 0x00, 0xAE,
+ 0x84, 0x5C, 0x19, 0x30, 0xC6, 0xDE, 0xAF, 0xC6,
+ 0x30, 0xC8, 0xD0, 0x67, 0x96, 0x14, 0xF1, 0x5F,
+ 0xFF, 0x0C, 0x49, 0xC1, 0x4A, 0xA6, 0x91, 0x52,
+ 0x83, 0xAE, 0x46, 0xA6, 0x2B, 0xCC, 0x38, 0x90,
+ 0xBD, 0xE3, 0xB0, 0x10, 0x30, 0x9F, 0x02, 0x69,
+ 0xCF, 0x34, 0x3E, 0x9C, 0x38, 0x82, 0x2A, 0x21,
+ 0xB9, 0x98, 0x01, 0x9F, 0x60, 0x97, 0x7B, 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 0x4F, 0xE3,
+ 0xA0, 0x04, 0x48, 0x04, 0x55, 0x4B, 0xCE, 0x8B,
+ 0x11, 0x00, 0xCB, 0x85, 0x93, 0x8B, 0x06, 0xCE,
+ 0x33, 0x0D, 0xA6, 0xB8, 0xBA, 0x5F, 0xC0, 0x3C,
+ 0x5A, 0xB0, 0x0B, 0x0C, 0x68, 0xE2, 0x4F, 0x4D,
+ 0xCC, 0x94, 0x54, 0x85, 0xCD, 0x8D, 0x46, 0x79,
+ 0xD7, 0x2C, 0xD9, 0x67, 0x27, 0xED, 0x1C, 0x22,
+ 0x6C, 0x90, 0xC2, 0xB1, 0x23, 0xD2, 0x38, 0x59,
+ 0x79, 0x12, 0x54, 0xAD, 0xEE, 0xFA, 0xE3, 0x31,
+ 0x87, 0x51, 0x88, 0x15, 0xB9, 0x24, 0x73, 0x5A,
+ 0x32, 0xA4, 0xD3, 0xCE, 0xEB, 0xC5, 0xAE, 0x9C,
+ 0x8E, 0xD6, 0x64, 0x00, 0x9E, 0x04, 0x5D, 0x28,
+ 0xED, 0x99, 0x90, 0xF9, 0xA0, 0xBA, 0x55, 0x81
+};
+#endif /* RSA_KEYSIZE == 2048 */
+
+#if RSA_KEYSIZE == 1024
+
+#define N_LEN_B (16*8)
+#define E_LEN_B (3)
+#define D_LEN_B (16*8)
+#define ALL_LEN_B (N_LEN_B+E_LEN_B+D_LEN_B)
+
+static uint8_t rsa_key_blob[] PROGMEM = {
+ /* n */
+ 0xC8, 0xE2, 0x5C, 0xE3, 0x38, 0xE2, 0x05, 0x03,
+ 0xBA, 0xFD, 0x33, 0x9A, 0xC4, 0x71, 0x5A, 0xD9,
+ 0x04, 0x8D, 0xF0, 0xDD, 0x03, 0x14, 0xCF, 0x73,
+ 0xCD, 0xE0, 0xBA, 0xFB, 0x21, 0xFF, 0x62, 0xF6,
+ 0x45, 0x17, 0x39, 0xBC, 0xDC, 0x9D, 0x06, 0x9F,
+ 0xC8, 0xED, 0xA0, 0xFE, 0xC4, 0xD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xBC, 0x98, 0x2F, 0xF0, 0x8A,
+ 0x3A, 0x3D, 0xFF, 0xB2, 0xF7, 0x12, 0xA8, 0x71,
+ 0x4C, 0xF8, 0x21, 0x3B, 0xD4, 0x9C, 0x78, 0x93,
+ 0x5A, 0xC1, 0x25, 0xB4, 0x5F, 0x95, 0x59, 0xD4,
+ 0x8D, 0x7D, 0xF4, 0x07, 0x3C, 0xB5, 0x8A, 0x4C,
+ 0xFF, 0x94, 0x68, 0xCD, 0x1A, 0x12, 0xC5, 0xC1
+};
+
+#endif /* RSA_KEYSIZE */
+
+void load_rsa_key_blob(rsa_ctx_t* ctx){
+ if(ctx->modulus.wordv){
+ free(ctx->modulus.wordv);
+ }
+ ctx->modulus.wordv = malloc(ALL_LEN_B);
+ if(ctx->modulus.wordv==NULL){
+ cli_putstr_P(PSTR("\r\nERROR: OUT OF MEMORY!!!"));
+ return;
+ }
+ ctx->modulus.info = ctx->privexp.info = ctx->pubexp.info = 0;
+ memcpy_P(ctx->modulus.wordv, rsa_key_blob, ALL_LEN_B);
+ ctx->modulus.length_B=N_LEN_B;
+ ctx->pubexp.wordv = ctx->modulus.wordv+N_LEN_B;
+ ctx->pubexp.length_B = E_LEN_B;
+ ctx->privexp.wordv = ctx->pubexp.wordv+E_LEN_B;
+ ctx->privexp.length_B = D_LEN_B;
+
+ bigint_changeendianess(&(ctx->modulus));
+ bigint_changeendianess(&(ctx->pubexp));
+ bigint_changeendianess(&(ctx->privexp));
+
+ bigint_adjust(&(ctx->modulus));
+ bigint_adjust(&(ctx->pubexp));
+ bigint_adjust(&(ctx->privexp));
+}
--- /dev/null
+/* main-rsa-test.c */
+/*
+ This file is part of the AVR-Crypto-Lib.
+ Copyright (C) 2010 Daniel Otte (daniel.otte@rub.de)
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+/*
+ * RSA test-suit
+ *
+*/
+
+#include "config.h"
+
+#include "uart_i.h"
+#include "debug.h"
+
+#include "noekeon/noekeon.h"
+#include "noekeon/noekeon_prng.h"
+#include "bigint/bigint.h"
+#include "bigint/bigint_io.h"
+#include "rsa.h"
+#include "rsa_key_blob.h"
+
+#include "cli.h"
+#include "performance_test.h"
+#include <stdint.h>
+#include <string.h>
+#include <stdlib.h>
+
+char* algo_name = "RSA";
+
+/*****************************************************************************
+ * additional validation-functions *
+ *****************************************************************************/
+
+rsa_ctx_t rsa_ctx;
+
+void load_fix_rsa(void){
+ load_rsa_key_blob(&rsa_ctx);
+}
+
+void rsa_print_item(bigint_t* a, PGM_P pstr){
+ uint8_t *p;
+ cli_putstr_P(PSTR("\r\n"));
+ cli_putstr_P(pstr);
+ cli_putstr_P(PSTR(": "));
+ uint16_t i;
+ p = a->wordv + a->length_B -1;
+ for(i=0; i<a->length_B-1; ++i){
+ if(i%16==0){
+ cli_putstr_P(PSTR("\r\n "));
+ }
+ cli_hexdump(p, 1);
+ cli_putc(':');
+ --p;
+ }
+ if(i%16==0){
+ cli_putstr_P(PSTR("\r\n "));
+ }
+ cli_hexdump(p, 1);
+}
+
+void print_rsa_ctx(rsa_ctx_t* ctx){
+ cli_putstr_P(PSTR("\r\n === RSA context ==="));
+ rsa_print_item(&(ctx->modulus), PSTR("modulus"));
+ rsa_print_item(&(ctx->pubexp), PSTR("public exponent"));
+ rsa_print_item(&(ctx->privexp), PSTR("private exponent"));
+}
+
+uint8_t testmsg[128] PROGMEM = {
+ 0x25, 0x11, 0x1e, 0x08, 0x1c, 0x72, 0x57, 0xbf,
+ 0x46, 0xbb, 0x56, 0xe8, 0x0b, 0x24, 0x0d, 0x72,
+ 0x77, 0x11, 0x12, 0x10, 0xec, 0xb9, 0xe4, 0x6f,
+ 0xe0, 0x6c, 0x83, 0x65, 0xe2, 0xe5, 0x1a, 0x3c,
+ 0xdb, 0x6c, 0x51, 0x8d, 0xbc, 0xd0, 0x30, 0xb6,
+ 0xca, 0x01, 0xb4, 0x03, 0x03, 0xe9, 0x86, 0x86,
+ 0xfe, 0x76, 0x4e, 0xb7, 0x47, 0xb5, 0x06, 0x15,
+ 0x92, 0x38, 0xc2, 0x0d, 0x3c, 0xfa, 0x53, 0xe3,
+ 0xe3, 0x57, 0xeb, 0x34, 0xb8, 0xda, 0x7b, 0x0e,
+ 0x06, 0x28, 0x1d, 0x4a, 0x14, 0xcc, 0x56, 0x8d,
+ 0xf3, 0x0f, 0x40, 0x2e, 0x23, 0x45, 0xe7, 0xcd,
+ 0xc5, 0x83, 0x8f, 0xaa, 0x55, 0x55, 0x7d, 0xbd,
+ 0x19, 0x63, 0xbd, 0x17, 0xda, 0xfd, 0x18, 0xd1,
+ 0x62, 0x43, 0xc7, 0x33, 0x39, 0x2c, 0xda, 0x64,
+ 0x27, 0x96, 0xa0, 0x83, 0xa0, 0xf9, 0x3b, 0x1e,
+ 0x17, 0x71, 0x59, 0xaa, 0x43, 0x3b, 0xeb, 0x80
+};
+
+void quick_test(void){
+ load_fix_rsa();
+
+ bigint_t m,c,m_;
+ uint8_t mw[rsa_ctx.modulus.length_B], cw[rsa_ctx.modulus.length_B], m_w[rsa_ctx.modulus.length_B];
+ uint8_t i;
+
+ print_rsa_ctx(&rsa_ctx);
+ m.wordv = mw;
+ c.wordv = cw;
+ m_.wordv = m_w;
+ m.length_B = rsa_ctx.modulus.length_B;
+ m.info = 0;
+ cli_putstr_P(PSTR("\r\ngenerating random message ..."));
+/* do{
+ for(i=0; i<rsa_ctx.modulus.length_B/16; i+=1){
+ random_block(&(mw[i*16]));
+ }
+ bigint_adjust(&m);
+ }while(bigint_cmp_s(&m, &(rsa_ctx.modulus))==1);
+*/
+ memcpy_P(mw, testmsg, 128);
+ bigint_changeendianess(&m);
+ bigint_adjust(&m);
+ rsa_print_item(&m, PSTR("plain "));
+
+ cli_putstr_P(PSTR("\r\nencrypting ..."));
+ rsa_enc_bigint(&c, &m, &rsa_ctx);
+ rsa_print_item(&c, PSTR("cipher"));
+
+ cli_putstr_P(PSTR("\r\ndecrypting ..."));
+ rsa_dec_bigint(&m_, &c, &rsa_ctx);
+
+ rsa_print_item(&m_, PSTR("plain'"));
+ cli_putstr_P(PSTR("\r\n plain == plain': "));
+ if(bigint_cmp_s(&m, &m_)){
+ cli_putstr_P(PSTR("false"));
+ }else{
+ cli_putstr_P(PSTR("true"));
+ }
+}
+
+void reset_prng(void){
+ uint8_t buf[16];
+ memset(buf, 0, 16);
+ random_seed(buf);
+ cli_putstr_P(PSTR("\r\nPRNG reset"));
+}
+
+void testrun_performance_bigint(void){
+
+}
+/*****************************************************************************
+ * main *
+ *****************************************************************************/
+
+const char echo_test_str[] PROGMEM = "echo-test";
+const char reset_prng_str[] PROGMEM = "reset-prng";
+const char quick_test_str[] PROGMEM = "quick-test";
+const char performance_str[] PROGMEM = "performance";
+const char echo_str[] PROGMEM = "echo";
+
+cmdlist_entry_t cmdlist[] PROGMEM = {
+ { reset_prng_str, NULL, reset_prng },
+ { quick_test_str, NULL, quick_test },
+ { performance_str, NULL, testrun_performance_bigint },
+ { echo_str, (void*)1, (void_fpt)echo_ctrl },
+ { NULL, NULL, NULL }
+};
+
+int main (void){
+ DEBUG_INIT();
+
+ cli_rx = (cli_rx_fpt)uart0_getc;
+ cli_tx = (cli_tx_fpt)uart0_putc;
+ for(;;){
+ cli_putstr_P(PSTR("\r\n\r\nCrypto-VS ("));
+ cli_putstr(algo_name);
+ cli_putstr_P(PSTR(")\r\nloaded and running\r\n"));
+ cmd_interface(cmdlist);
+ }
+}