X-Git-Url: https://git.cryptolib.org/?a=blobdiff_plain;f=rsa%2Frsa_basic.c;h=a7c321964aa86602605a392cf6535332a5eae099;hb=01b5d29136b37105c7e533bd3ec2299d31551627;hp=98cdfb2ca5fa77498db0fe6e9c350e5559e9ac35;hpb=cc6b183296a5852449e3324737e2a2dece788786;p=avr-crypto-lib.git diff --git a/rsa/rsa_basic.c b/rsa/rsa_basic.c index 98cdfb2..a7c3219 100644 --- a/rsa/rsa_basic.c +++ b/rsa/rsa_basic.c @@ -28,9 +28,10 @@ #if DEBUG #include "cli.h" +#include #endif -void rsa_enc(bigint_t* data, rsa_publickey_t* key){ +void rsa_enc(bigint_t *data, const rsa_publickey_t *key){ /* cli_putstr_P(PSTR("\r\n -->rsa_enc()\r\n m = ")); bigint_print_hex(data); @@ -39,7 +40,7 @@ void rsa_enc(bigint_t* data, rsa_publickey_t* key){ cli_putstr_P(PSTR("\r\n n = ")); bigint_print_hex(key->modulus); */ - bigint_expmod_u(data, data, key->exponent, key->modulus); + bigint_expmod_u(data, data, &key->exponent, &key->modulus); } /* @@ -50,10 +51,10 @@ h = (m1 - m2) * qinv % p m = m2 + q * h */ -uint8_t rsa_dec_crt_mono(bigint_t* data, rsa_privatekey_t* key){ +uint8_t rsa_dec_crt_mono(bigint_t *data, const rsa_privatekey_t *key){ bigint_t m1, m2; - m1.wordv = malloc((key->components[0]->length_B + 1) * sizeof(bigint_word_t)); - m2.wordv = malloc((key->components[1]->length_B + 1) * sizeof(bigint_word_t)); + m1.wordv = malloc((key->components[0].length_W /* + 1 */) * sizeof(bigint_word_t)); + m2.wordv = malloc((key->components[1].length_W /* + 1 */) * sizeof(bigint_word_t)); if(!m1.wordv || !m2.wordv){ #if DEBUG cli_putstr_P(PSTR("\r\nERROR: OOM!")); @@ -67,24 +68,24 @@ uint8_t rsa_dec_crt_mono(bigint_t* data, rsa_privatekey_t* key){ cli_putstr_P(PSTR("\r\nexpmod(")); bigint_print_hex(data); cli_putc(','); - bigint_print_hex(key->components[2]); + bigint_print_hex(&(key->components[2])); cli_putc(','); - bigint_print_hex(key->components[0]); + bigint_print_hex(&(key->components[0])); cli_putstr_P(PSTR(") = ")); #endif - bigint_expmod_u(&m1, data, key->components[2], key->components[0]); + bigint_expmod_u(&m1, data, &(key->components[2]), &(key->components[0])); #if DEBUG bigint_print_hex(&m1); cli_putstr_P(PSTR("expmod m2 ...")); cli_putstr_P(PSTR("\r\nexpmod(")); bigint_print_hex(data); cli_putc(','); - bigint_print_hex(key->components[3]); + bigint_print_hex(&(key->components[3])); cli_putc(','); - bigint_print_hex(key->components[1]); + bigint_print_hex(&(key->components[1])); cli_putstr_P(PSTR(") = ")); #endif - bigint_expmod_u(&m2, data, key->components[3], key->components[1]); + bigint_expmod_u(&m2, data, &(key->components[3]), &(key->components[1])); #if DEBUG bigint_print_hex(&m2); cli_putstr_P(PSTR("\r\nDBG: sub ...")); @@ -101,48 +102,48 @@ uint8_t rsa_dec_crt_mono(bigint_t* data, rsa_privatekey_t* key){ while(BIGINT_NEG_MASK & m1.info){ #if DEBUG cli_putstr_P(PSTR("\r\nDBG: adding ")); - bigint_print_hex(key->components[0]); + bigint_print_hex(&key->components[0]); cli_putstr_P(PSTR("\r\nDBG: to ")); bigint_print_hex(&m1); #endif - bigint_add_s(&m1, &m1, key->components[0]); + bigint_add_s(&m1, &m1, &(key->components[0])); } #if DEBUG cli_putstr_P(PSTR("\r\nDBG: reduce-mul ...")); cli_putstr_P(PSTR("\r\nreduce(")); bigint_print_hex(&m1); cli_putc(','); - bigint_print_hex(key->components[0]); + bigint_print_hex(&(key->components[0])); cli_putstr_P(PSTR(") = ")); #endif - bigint_reduce(&m1, key->components[0]); + bigint_reduce(&m1, &(key->components[0])); #if DEBUG bigint_print_hex(&m1); cli_putstr_P(PSTR("\r\nmul(")); bigint_print_hex(&m1); cli_putc(','); - bigint_print_hex(key->components[4]); + bigint_print_hex(&(key->components[4])); cli_putstr_P(PSTR(") = ")); #endif - bigint_mul_u(data, &m1, key->components[4]); + bigint_mul_u(data, &m1, &(key->components[4])); #if DEBUG bigint_print_hex(data); cli_putstr_P(PSTR("\r\nreduce(")); bigint_print_hex(data); cli_putc(','); - bigint_print_hex(key->components[0]); + bigint_print_hex(&(key->components[0])); cli_putstr_P(PSTR(") = ")); #endif - bigint_reduce(data, key->components[0]); + bigint_reduce(data, &(key->components[0])); #if DEBUG bigint_print_hex(data); cli_putstr_P(PSTR("\r\nmul(")); bigint_print_hex(data); cli_putc(','); - bigint_print_hex(key->components[1]); + bigint_print_hex(&(key->components[1])); cli_putstr_P(PSTR(") = ")); #endif - bigint_mul_u(data, data, key->components[1]); + bigint_mul_u(data, data, &(key->components[1])); #if DEBUG bigint_print_hex(data); cli_putstr_P(PSTR("\r\nadd(")); @@ -160,9 +161,9 @@ uint8_t rsa_dec_crt_mono(bigint_t* data, rsa_privatekey_t* key){ return 0; } -uint8_t rsa_dec(bigint_t* data, rsa_privatekey_t* key){ +uint8_t rsa_dec(bigint_t *data, const rsa_privatekey_t *key){ if(key->n == 1){ - bigint_expmod_u(data, data, key->components[0], key->modulus); + bigint_expmod_u(data, data, &(key->components[0]), &key->modulus); return 0; } if(key->n == 5){ @@ -178,12 +179,12 @@ uint8_t rsa_dec(bigint_t* data, rsa_privatekey_t* key){ return 2; } -void rsa_os2ip(bigint_t* dest, const void* data, uint32_t length_B){ +void rsa_os2ip(bigint_t *dest, const void *data, uint32_t length_B){ #if BIGINT_WORD_SIZE == 8 if(data){ memcpy(dest->wordv, data, length_B); } - dest->length_B = length_B; + dest->length_W = length_B; #else uint8_t off; off = (sizeof(bigint_word_t) - length_B % sizeof(bigint_word_t)) % sizeof(bigint_word_t); @@ -203,10 +204,10 @@ void rsa_os2ip(bigint_t* dest, const void* data, uint32_t length_B){ memset(dest->wordv, 0, off); } } - dest->length_B = (length_B + off) / sizeof(bigint_word_t); + dest->length_W = (length_B + off) / sizeof(bigint_word_t); #if DEBUG - cli_putstr_P(PSTR("\r\nDBG: dest->length_B = 0x")); - cli_hexdump_rev(&(dest->length_B), 2); + cli_putstr_P(PSTR("\r\nDBG: dest->length_W = 0x")); + cli_hexdump_rev(&(dest->length_W), 2); #endif #endif dest->info = 0; @@ -214,12 +215,12 @@ void rsa_os2ip(bigint_t* dest, const void* data, uint32_t length_B){ bigint_adjust(dest); } -void rsa_i2osp(void* dest, bigint_t* src, uint16_t* out_length_B){ +void rsa_i2osp(void *dest, bigint_t *src, uint16_t *out_length_B){ #if BIGINT_WORD_SIZE == 8 if(dest){ - uint8_t *e = src->wordv + src->length_B; + uint8_t *e = src->wordv + src->length_W; uint16_t i; - for(i=src->length_B; i>0; --i){ + for(i=src->length_W; i>0; --i){ *((uint8_t*)dest) = *--e; dest = (uint8_t*)dest + 1; } @@ -227,7 +228,7 @@ void rsa_i2osp(void* dest, bigint_t* src, uint16_t* out_length_B){ bigint_changeendianess(src); } - *out_length_B = src->length_B; + *out_length_B = src->length_W; #else *out_length_B = bigint_get_first_set_bit(src) / 8 + 1; if(dest){