diff --git a/bigint/bigint.c b/bigint/bigint.c index a7a2b59..f0d7d6e 100644 --- a/bigint/bigint.c +++ b/bigint/bigint.c @@ -956,7 +956,7 @@ void bigint_inverse(bigint_t* dest, const bigint_t* a, const bigint_t* m){ void bigint_changeendianess(bigint_t* a){ uint8_t t, *p, *q; p = (uint8_t*)(a->wordv); - q = ((uint8_t*)p)+a->length_B*sizeof(bigint_word_t)-1; + q = p + a->length_B * sizeof(bigint_word_t) - 1; while(pwordv, 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); @@ -117,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; @@ -147,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 } diff --git a/rsa/rsa_pkcs15.c b/rsa/rsa_pkcs15.c index 858b918..1db7d3f 100644 --- a/rsa/rsa_pkcs15.c +++ b/rsa/rsa_pkcs15.c @@ -32,11 +32,15 @@ #include "random_dummy.h" +uint16_t rsa_pkcs15_compute_padlength_B(bigint_t* modulus, uint16_t msg_length_B){ + return bigint_get_first_set_bit(modulus) / 8 + 1 - msg_length_B - 3; +} + uint8_t rsa_encrypt_pkcs15(void* dest, uint16_t* out_length, const void* src, uint16_t length_B, rsa_publickey_t* key, const void* pad){ int16_t pad_length; bigint_t x; - pad_length = (bigint_get_first_set_bit(key->modulus) + 7) / 8 - length_B - 3; + pad_length = rsa_pkcs15_compute_padlength_B(key->modulus, length_B); if(pad_length<8){ #if DEBUG cli_putstr("\r\nERROR: pad_length<8; pad_length: "); @@ -45,6 +49,9 @@ uint8_t rsa_encrypt_pkcs15(void* dest, uint16_t* out_length, const void* src, return 2; /* message to long */ } if(!pad){ +#if DEBUG + cli_putstr("\r\nauto-generating pad ..."); +#endif uint16_t i; uint8_t c; for(i=0; i=m_length){ + if(idx>=m_length){ return 1; } + if(((uint8_t*)x.wordv)[idx]!=2){ + return 3; + } + ++idx; while(((uint8_t*)x.wordv)[idx+pad_length]!=0 && (idx+pad_length) +#include "bigint.h" + +uint16_t rsa_pkcs15_compute_padlength_B(bigint_t* modulus, uint16_t msg_length_B); + uint8_t rsa_encrypt_pkcs15(void* dest, uint16_t* out_length, const void* src, uint16_t length_B, rsa_publickey_t* key, const void* pad); diff --git a/test_src/string-extras.c b/test_src/string-extras.c index 18cf67f..6ab077e 100644 --- a/test_src/string-extras.c +++ b/test_src/string-extras.c @@ -40,6 +40,7 @@ uint32_t stridentcnt(const char* a, const char* b){ a++; b++; } + return 0; } uint16_t firstword_length(const char* s){ @@ -155,3 +156,12 @@ void strlwr(char* s){ } } */ + +char* itoa(int a, char* buffer, uint8_t radix){ + if(a<0){ + *buffer = '-'; + a = -a; + } + ultoa(a, buffer + 1, radix); + return buffer; +} diff --git a/rsa/pkcs1v15crypt-vectors.txt b/testvectors/rsa-pkcs-1v2-1-vec/pkcs1v15crypt-vectors.txt similarity index 100% rename from rsa/pkcs1v15crypt-vectors.txt rename to testvectors/rsa-pkcs-1v2-1-vec/pkcs1v15crypt-vectors.txt