X-Git-Url: https://git.cryptolib.org/?a=blobdiff_plain;f=rsa%2Frsa_basic.c;h=a166dfa2384909d6e3b2ce8f90cd277ab0421b52;hb=e5296441c98c35140f909ca1dbdb8d6852f996d3;hp=02947edf705370b7385379754968927bb64d3633;hpb=f3456452a0f13bfe6d332aaeeada5b626dac2739;p=arm-crypto-lib.git diff --git a/rsa/rsa_basic.c b/rsa/rsa_basic.c index 02947ed..a166dfa 100644 --- a/rsa/rsa_basic.c +++ b/rsa/rsa_basic.c @@ -24,16 +24,23 @@ #include "bigint_io.h" #include "rsa_basic.h" +#define DEBUG 0 + +#if DEBUG #include "cli.h" #include "uart_lowlevel.h" +#include "string-extras.h" +#endif void rsa_enc(bigint_t* data, rsa_publickey_t* key){ +/* cli_putstr("\r\n -->rsa_enc()\r\n m = "); bigint_print_hex(data); cli_putstr("\r\n e = "); bigint_print_hex(key->exponent); cli_putstr("\r\n n = "); bigint_print_hex(key->modulus); +*/ bigint_expmod_u(data, data, key->exponent, key->modulus); } @@ -47,34 +54,37 @@ m = m2 + q * h uint8_t rsa_dec_crt_mono(bigint_t* data, rsa_privatekey_t* key){ bigint_t m1, m2; - cli_putstr("\r\n --> rsa_dec_crt_mono()"); - uart_flush(0); m1.wordv = malloc(key->components[0]->length_B * sizeof(bigint_word_t)); m2.wordv = malloc(key->components[1]->length_B * sizeof(bigint_word_t)); if(!m1.wordv || !m2.wordv){ +#if DEBUG cli_putstr("\r\nERROR: OOM!"); +#endif free(m1.wordv); free(m2.wordv); return 1; } - cli_putc('a'); uart_flush(0); +#if DEBUG + cli_putstr("\r\nDBG: expmod m1 ..."); +#endif bigint_expmod_u(&m1, data, key->components[2], key->components[0]); - cli_putc('f'); uart_flush(0); +#if DEBUG + cli_putstr("expmod m2 ..."); +#endif bigint_expmod_u(&m2, data, key->components[3], key->components[1]); - cli_putc('b'); uart_flush(0); bigint_sub_s(&m1, &m1, &m2); while(BIGINT_NEG_MASK & m1.info){ bigint_add_s(&m1, &m1, key->components[0]); } - cli_putc('c'); uart_flush(0); +#if DEBUG + cli_putstr("\r\nDBG: reduce-mul ..."); +#endif bigint_reduce(&m1, key->components[0]); bigint_mul_u(data, &m1, key->components[4]); - cli_putc('d'); uart_flush(0); bigint_reduce(data, key->components[0]); bigint_mul_u(data, data, key->components[1]); bigint_add_u(data, data, &m2); - cli_putc('e'); uart_flush(0); free(m1.wordv); free(m2.wordv); return 0; @@ -99,8 +109,18 @@ uint8_t rsa_dec(bigint_t* data, rsa_privatekey_t* key){ } 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; +#else uint8_t off; - off = length_B % sizeof(bigint_word_t); + off = (sizeof(bigint_word_t) - length_B % sizeof(bigint_word_t)) % sizeof(bigint_word_t); +#if DEBUG + cli_putstr("\r\nDBG: off = 0x"); + cli_hexdump_byte(off); +#endif if(!data){ if(off){ dest->wordv = realloc(dest->wordv, length_B + sizeof(bigint_word_t) - off); @@ -108,19 +128,36 @@ void rsa_os2ip(bigint_t* dest, const void* data, uint32_t length_B){ memset(dest->wordv, 0, off); } }else{ + memcpy((uint8_t*)dest->wordv + off, data, length_B); if(off){ - memcpy((uint8_t*)dest->wordv + off, data, length_B); - memset(dest, 0, off); - }else{ - memcpy(dest->wordv, data, length_B); + memset(dest->wordv, 0, off); } } - dest->length_B = (length_B + sizeof(bigint_word_t) - 1) / sizeof(bigint_word_t); + dest->length_B = (length_B + off) / sizeof(bigint_word_t); +#if DEBUG + cli_putstr("\r\nDBG: dest->length_B = 0x"); + cli_hexdump_rev(&(dest->length_B), 2); +#endif +#endif bigint_changeendianess(dest); bigint_adjust(dest); } 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; + uint16_t i; + for(i=src->length_B; i>0; --i){ + *((uint8_t*)dest) = *--e; + dest = (uint8_t*)dest + 1; + } + }else{ + bigint_changeendianess(src); + } + + *out_length_B = src->length_B; +#else *out_length_B = bigint_get_first_set_bit(src) / 8 + 1; if(dest){ uint16_t i; @@ -138,5 +175,6 @@ void rsa_i2osp(void* dest, bigint_t* src, uint16_t* out_length_B){ memmove(src->wordv, (uint8_t*)src->wordv+off, *out_length_B); } } +#endif }