From 01b5d29136b37105c7e533bd3ec2299d31551627 Mon Sep 17 00:00:00 2001 From: bg Date: Sat, 15 Feb 2014 01:23:43 +0100 Subject: [PATCH] fixing problem with shifting 0 --- bigint/bigint.c | 32 ++++++++++++++++++-------------- bigint/bigint.h | 4 ++-- mkfiles/rsaes_pkcs1v15.mk | 2 +- rsa/rsa_basic.c | 3 ++- rsa/rsa_basic.h | 1 - 5 files changed, 23 insertions(+), 19 deletions(-) diff --git a/bigint/bigint.c b/bigint/bigint.c index c5f799e..8524daa 100644 --- a/bigint/bigint.c +++ b/bigint/bigint.c @@ -97,9 +97,9 @@ bigint_length_t bigint_length_B(const bigint_t *a){ /******************************************************************************/ -uint32_t bigint_get_first_set_bit(const bigint_t *a){ +int32_t bigint_get_first_set_bit(const bigint_t *a){ if(a->length_W == 0) { - return (uint32_t)(-1); + return -1; } return (a->length_W-1) * sizeof(bigint_word_t) * CHAR_BIT + GET_FBS(a); } @@ -107,24 +107,24 @@ uint32_t bigint_get_first_set_bit(const bigint_t *a){ /******************************************************************************/ -uint32_t bigint_get_last_set_bit(const bigint_t *a){ - uint32_t r=0; - uint8_t b=0; - bigint_word_t x=1; - if(a->length_W==0){ - return (uint32_t)(-1); +int32_t bigint_get_last_set_bit(const bigint_t *a){ + uint32_t r = 0; + uint8_t b = 0; + bigint_word_t x = 1; + if (a->length_W == 0) { + return -1; } - while(a->wordv[r]==0 && rlength_W){ + while (a->wordv[r] == 0 && r < a->length_W) { ++r; } - if(a->wordv[r] == 0){ + if (a->wordv[r] == 0) { return (uint32_t)(-1); } - while((x&a->wordv[r])==0){ + while ((x&a->wordv[r])==0) { ++b; x <<= 1; } - return r*BIGINT_WORD_SIZE+b; + return r * BIGINT_WORD_SIZE + b; } /******************************************************************************/ @@ -374,7 +374,7 @@ void bigint_shiftleft(bigint_t *a, bigint_length_t shift){ bigint_word_t *p; bigint_wordplus_t t = 0; - if (shift == 0) { + if (a->length_W == 0 || shift == 0) { return; } byteshift = shift / 8; @@ -416,6 +416,10 @@ void bigint_shiftright(bigint_t *a, bigint_length_t shift){ byteshift = shift / 8; bitshift = shift & 7; + if (a->length_W == 0) { + return; + } + if(bigint_get_first_set_bit(a) < shift){ /* we would shift out more than we have */ bigint_set_zero(a); return; @@ -424,9 +428,9 @@ void bigint_shiftright(bigint_t *a, bigint_length_t shift){ if(byteshift){ memmove(a->wordv, (uint8_t*)a->wordv + byteshift, a->length_W * sizeof(bigint_word_t) - byteshift); memset((uint8_t*)&a->wordv[a->length_W] - byteshift, 0, byteshift); + a->length_W -= byteshift / sizeof(bigint_word_t); } - a->length_W -= byteshift / sizeof(bigint_word_t); if(bitshift != 0 && a->length_W){ /* shift to the right */ diff --git a/bigint/bigint.h b/bigint/bigint.h index f909795..82e10f8 100644 --- a/bigint/bigint.h +++ b/bigint/bigint.h @@ -68,8 +68,8 @@ typedef struct{ /******************************************************************************/ void bigint_adjust(bigint_t *a); -uint32_t bigint_get_first_set_bit(const bigint_t *a); -uint32_t bigint_get_last_set_bit(const bigint_t *a); +int32_t bigint_get_first_set_bit(const bigint_t *a); +int32_t bigint_get_last_set_bit(const bigint_t *a); bigint_length_t bigint_length_b(const bigint_t *a); bigint_length_t bigint_length_B(const bigint_t *a); void bigint_copy(bigint_t *dest, const bigint_t *src); diff --git a/mkfiles/rsaes_pkcs1v15.mk b/mkfiles/rsaes_pkcs1v15.mk index 8d15289..b2c14fd 100644 --- a/mkfiles/rsaes_pkcs1v15.mk +++ b/mkfiles/rsaes_pkcs1v15.mk @@ -2,7 +2,7 @@ ALGO_NAME := RSAES_PKCS1V15 # comment out the following line for removement of RSA from the build process -SIGNATURE += $(ALGO_NAME) +PK_CIPHERS += $(ALGO_NAME) $(ALGO_NAME)_DIR := rsa/ $(ALGO_NAME)_INCDIR := memxor/ bigint/ noekeon/ diff --git a/rsa/rsa_basic.c b/rsa/rsa_basic.c index 3d6581f..a7c3219 100644 --- a/rsa/rsa_basic.c +++ b/rsa/rsa_basic.c @@ -28,6 +28,7 @@ #if DEBUG #include "cli.h" +#include #endif void rsa_enc(bigint_t *data, const rsa_publickey_t *key){ @@ -101,7 +102,7 @@ uint8_t rsa_dec_crt_mono(bigint_t *data, const 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 diff --git a/rsa/rsa_basic.h b/rsa/rsa_basic.h index 200c0fb..ccce4b0 100644 --- a/rsa/rsa_basic.h +++ b/rsa/rsa_basic.h @@ -33,7 +33,6 @@ typedef struct { bigint_t *components; } rsa_privatekey_t; - typedef struct { rsa_privatekey_t priv; rsa_publickey_t pub; -- 2.39.5