#include "performance_test.h"
-#define DEBUG 0
+#define DEBUG 1
-const char* algo_name = "RSAES-PKCS1V15";
+const char *algo_name = "RSAES-PKCS1V15";
#define BIGINT_CEIL(x) ((((x) + sizeof(bigint_word_t) - 1) / sizeof(bigint_word_t)) * sizeof(bigint_word_t))
#define BIGINT_OFF(x) ((sizeof(bigint_word_t) - (x) % sizeof(bigint_word_t)) % sizeof(bigint_word_t))
const char *block_ignore_string=" \t\r\n,;";
#define BUFFER_LIMIT 120
-uint16_t read_os(void* dst, uint16_t length, const char* ignore_string){
+uint16_t read_os(void *dst, uint16_t length, const char *ignore_string){
uint16_t counter = 0;
uint16_t c;
uint8_t v, tmp = 0, idx = 0;
return counter;
}
-uint16_t own_atou(const char* str){
+uint16_t own_atou(const char *str){
uint16_t r=0;
while(*str && *str >= '0' && *str <= '9'){
r *= 10;
return r;
}
-uint8_t read_bigint(bigint_t* a, char* prompt){
+uint8_t read_bigint(bigint_t *a, char *prompt){
uint16_t read_length, actual_length;
uint8_t off;
uint8_t *buffer;
return 1;
}
a->wordv = (bigint_word_t*)buffer;
- a->length_B = (read_length + sizeof(bigint_word_t) - 1) / sizeof(bigint_word_t);
+ a->length_W = (read_length + sizeof(bigint_word_t) - 1) / sizeof(bigint_word_t);
a->info = 0;
bigint_changeendianess(a);
bigint_adjust(a);
return 0;
}
+uint8_t pre_alloc_key_crt(void){
+ priv_key.n = 5;
+ priv_key.components = malloc(5 * sizeof(bigint_t));
+ if(!priv_key.components){
+ cli_putstr_P(PSTR("\r\nERROR: OOM!"));
+ return 2;
+ }
+ return 0;
+}
+
+void free_key(void){
+ uint8_t c;
+ free(pub_key.modulus.wordv);
+ free(pub_key.exponent.wordv);
+ for(c = 0; c < priv_key.n; ++c){
+ free(priv_key.components[c].wordv);
+ }
+ free(priv_key.components);
+ keys_allocated = 0;
+}
+
+uint8_t read_key_crt(void){
+ uint8_t r;
+ cli_putstr_P(PSTR("\r\n== reading key (crt) =="));
+ r = pre_alloc_key_crt();
+ if(r) return r;
+ r = read_bigint(&pub_key.modulus,"\r\n = module =");
+ if(r) return r;
+ memcpy(&priv_key.modulus, &pub_key.modulus, sizeof(bigint_t));
+ r = read_bigint(&pub_key.exponent,"\r\n = public exponent =");
+ if(r) return r;
+ r = read_bigint(&(priv_key.components[0]),"\r\n = p (first prime) =");
+ if(r) return r;
+ r = read_bigint(&(priv_key.components[1]),"\r\n = q (second prime) =");
+ if(r) return r;
+ r = read_bigint(&(priv_key.components[2]),"\r\n = dp (p's exponent) =");
+ if(r) return r;
+ r = read_bigint(&(priv_key.components[3]),"\r\n = dq (q's exponent) =");
+ if(r) return r;
+ r = read_bigint(&(priv_key.components[4]),"\r\n = qInv (q' coefficient) =");
+ return r;
+}
+
+uint8_t read_key_conv(void){
+ uint8_t r;
+ priv_key.components = malloc(sizeof(bigint_t));
+ if(!priv_key.components){
+ cli_putstr_P(PSTR("\r\nERROR: OOM!"));
+ return 2;
+ }
+ cli_putstr_P(PSTR("\r\n== reading key (conv) =="));
+ r = read_bigint(&pub_key.modulus,"\r\n = module =");
+ if(r) return r;
+ memcpy(&priv_key.modulus, &pub_key.modulus, sizeof(bigint_t));
+ priv_key.n = 1;
+ r = read_bigint(&pub_key.exponent,"\r\n = public exponent =");
+ if(r) return r;
+ r = read_bigint(priv_key.components,"\r\n = private exponent =");
+ return r;
+}
+
+uint8_t load_bigint_from_os(bigint_t *a, PGM_VOID_P os, uint16_t length_B){
+ a->length_W = BIGINT_CEIL(length_B) / sizeof(bigint_word_t);
+ a->wordv = malloc(BIGINT_CEIL(length_B));
+ if(!a->wordv){
+ cli_putstr_P(PSTR("\r\nOOM!\r\n"));
+ return 1;
+ }
+ memset(a->wordv, 0, sizeof(bigint_word_t));
+ memcpy_P((uint8_t*)a->wordv + BIGINT_OFF(length_B), os, length_B);
+ a->info = 0;
+ bigint_changeendianess(a);
+ bigint_adjust(a);
+ return 0;
+}
+
+void load_fix_rsa(void){
+ if(keys_allocated){
+ free_key();
+ }
+ keys_allocated = 1;
+
+ if(pre_alloc_key_crt()){
+ cli_putstr_P(PSTR("\r\nOOM!\r\n"));
+ return;
+ }
+
+ load_bigint_from_os(&pub_key.modulus, MODULUS, sizeof(MODULUS));
+ memcpy(&priv_key.modulus, &pub_key.modulus, sizeof(bigint_t));
+ load_bigint_from_os(&pub_key.exponent, PUB_EXPONENT, sizeof(PUB_EXPONENT));
+ priv_key.n = 5;
+ load_bigint_from_os(&(priv_key.components[0]), P, sizeof(P));
+ load_bigint_from_os(&(priv_key.components[1]), Q, sizeof(Q));
+ load_bigint_from_os(&(priv_key.components[2]), DP, sizeof(DP));
+ load_bigint_from_os(&(priv_key.components[3]), DQ, sizeof(DQ));
+ load_bigint_from_os(&(priv_key.components[4]), QINV, sizeof(QINV));
+}
+
+/*
+
uint8_t pre_alloc_key_crt(void){
uint8_t c;
pub_key.modulus = malloc(sizeof(bigint_t));
cli_putstr_P(PSTR("\r\nERROR: OOM!"));
return;
}
- epriv->length_B = (sizeof(PRIV_EXPONENT) + sizeof(bigint_word_t) - 1) / sizeof(bigint_word_t);
- epriv->wordv = malloc(epriv->length_B * sizeof(bigint_word_t));
+ epriv->length_W = (sizeof(PRIV_EXPONENT) + sizeof(bigint_word_t) - 1) / sizeof(bigint_word_t);
+ epriv->wordv = malloc(epriv->length_W * sizeof(bigint_word_t));
if(!epriv->wordv){
cli_putstr_P(PSTR("\r\nERROR: OOM!"));
return;
for(i=0; i<5; ++i){
v[i] = malloc(sizeof(bigint_t));
v[i]->info = 0;
- v[i]->length_B = (sv[i] + sizeof(bigint_word_t) - 1) / sizeof(bigint_word_t);
- v[i]->wordv = calloc(v[i]->length_B , sizeof(bigint_word_t));
+ v[i]->length_W = (sv[i] + sizeof(bigint_word_t) - 1) / sizeof(bigint_word_t);
+ v[i]->wordv = calloc(v[i]->length_W , sizeof(bigint_word_t));
if(!v[i]->wordv){
cli_putstr_P(PSTR("\r\nERROR: OOM!"));
return;
}
}
-uint8_t load_bigint_from_os(bigint_t* a, PGM_VOID_P os, uint16_t length_B){
- a->length_B = BIGINT_CEIL(length_B) / sizeof(bigint_word_t);
+uint8_t load_bigint_from_os(bigint_t *a, PGM_VOID_P os, uint16_t length_B){
+ a->length_W = BIGINT_CEIL(length_B) / sizeof(bigint_word_t);
a->wordv = malloc(BIGINT_CEIL(length_B));
if(!a->wordv){
cli_putstr_P(PSTR("\r\nOOM!\r\n"));
// load_priv_conventional();
// load_priv_crt_mono();
}
+*/
void quick_test(void){
uint8_t *ciphertext, *plaintext, rc;
if(!keys_allocated){
load_fix_rsa();
}
- ciphertext = malloc(clen = pub_key.modulus->length_B * sizeof(bigint_word_t));
- plaintext = malloc(pub_key.modulus->length_B * sizeof(bigint_word_t));
+ ciphertext = malloc(clen = bigint_length_B(&pub_key.modulus));
+ plaintext = malloc(clen);
memcpy_P(plaintext, MSG, sizeof(MSG));
memcpy_P(seed, SEED, sizeof(SEED));
cli_putstr_P(PSTR("\r\nplaintext:"));
cli_putstr_P(PSTR("\r\n\r\nciphertext:"));
cli_hexdump_block(ciphertext, clen, 4, 16);
- if(clen!=sizeof(ENCRYPTED)){
+ if(clen != sizeof(ENCRYPTED)){
cli_putstr_P(PSTR("\r\n>>FAIL (no size match)<<"));
+ return;
}else{
if(memcmp_P(ciphertext, ENCRYPTED, clen)){
cli_putstr_P(PSTR("\r\n>>FAIL (no content match)<<"));
+ return;
}else{
cli_putstr_P(PSTR("\r\n>>OK<<"));
}
cli_putstr_P(PSTR("\r\n length: "));
cli_getsn(read_int_str, 16);
msg_len = own_atou(read_int_str);
- seed_len = rsa_pkcs1v15_compute_padlength_B(pub_key.modulus, msg_len);
+ seed_len = rsa_pkcs1v15_compute_padlength_B(&pub_key.modulus, msg_len);
seed = malloc(seed_len);
#if DEBUG
cli_putstr_P(PSTR("\r\nDBG: @seed: 0x"));
cli_putstr_P(PSTR("\r\nERROR: OOM!"));
return;
}
- ciph = malloc(bigint_length_B(pub_key.modulus));
+ ciph = malloc(bigint_length_B(&pub_key.modulus));
#if DEBUG
cli_putstr_P(PSTR("\r\nDBG: @ciph: 0x"));
cli_hexdump_rev(&ciph, 2);
cli_putstr_P(PSTR("\r\nERROR: OOM!"));
return;
}
- msg_ = malloc(bigint_length_B(pub_key.modulus) + sizeof(bigint_word_t));
+ msg_ = malloc(bigint_length_B(&pub_key.modulus) /* + sizeof(bigint_word_t) */ );
#if DEBUG
cli_putstr_P(PSTR("\r\nDBG: @msg_: 0x"));
cli_hexdump_rev(&msg_, 2);
*/
#if DEBUG
cli_putstr_P(PSTR("\r\n first prime:"));
- bigint_print_hex(priv_key.components[0]);
+ bigint_print_hex(&priv_key.components[0]);
#endif
rsa_encrypt_pkcs1v15(ciph, &ciph_len, msg, msg_len, &pub_key, seed);
cli_putstr_P(PSTR("\r\n ciphertext:"));
cli_hexdump_block(ciph, ciph_len, 4, 16);
#if DEBUG
cli_putstr_P(PSTR("\r\n first prime:"));
- bigint_print_hex(priv_key.components[0]);
+ bigint_print_hex(&priv_key.components[0]);
#endif
cli_putstr_P(PSTR("\r\n decrypting ... "));
cli_putstr_P(PSTR("\r\ndumping 0x"));
cli_hexdump_rev(&len, 2);
cli_putstr_P(PSTR(" byte:"));
- cli_hexdump_block(pub_key.modulus->wordv, len, 4, 8);
+ cli_hexdump_block(pub_key.modulus.wordv, len, 4, 8);
}
/*****************************************************************************