From 86d74efc37d5a83a1fea2be5589753e8130cf93e Mon Sep 17 00:00:00 2001 From: Ludovic FLAMENT Date: Thu, 24 Sep 2015 08:13:43 +0200 Subject: [PATCH 1/3] return IdeaCbc{Encrypt/Decrypt} error code --- src/internal.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/internal.c b/src/internal.c index bc48149fc..9c574a50c 100644 --- a/src/internal.c +++ b/src/internal.c @@ -5942,8 +5942,8 @@ static INLINE int Encrypt(WOLFSSL* ssl, byte* out, const byte* input, word16 sz) #ifdef HAVE_IDEA case wolfssl_idea: - wc_IdeaCbcEncrypt(ssl->encrypt.idea, out, input, sz); - break; + ret = wc_IdeaCbcEncrypt(ssl->encrypt.idea, out, input, sz); + break; #endif default: @@ -6103,7 +6103,7 @@ static INLINE int Decrypt(WOLFSSL* ssl, byte* plain, const byte* input, #ifdef HAVE_IDEA case wolfssl_idea: - wc_IdeaCbcDecrypt(ssl->decrypt.idea, plain, input, sz); + ret = wc_IdeaCbcDecrypt(ssl->decrypt.idea, plain, input, sz); break; #endif From ae6b4be1355adc84d18303b214608dc3d1c12df6 Mon Sep 17 00:00:00 2001 From: Ludovic FLAMENT Date: Thu, 24 Sep 2015 22:55:11 +0200 Subject: [PATCH 2/3] fix idea_mult move reg and tmp buffer to word32 --- wolfcrypt/src/idea.c | 27 ++++++++++++++------------- wolfssl/wolfcrypt/idea.h | 5 +++-- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/wolfcrypt/src/idea.c b/wolfcrypt/src/idea.c index 3daee1a10..0f411c532 100644 --- a/wolfcrypt/src/idea.c +++ b/wolfcrypt/src/idea.c @@ -46,17 +46,19 @@ */ static INLINE word16 idea_mult(word16 x, word16 y) { - word32 mul, res; + long mul, res; - mul = (word32)x * (word32)y; + mul = x * y; if (mul) { res = (mul & IDEA_MASK) - (mul >> 16); - res -= (res >> 16); return (word16) ((res <=0 ? res+IDEA_MODULO : res) & IDEA_MASK); } - /* x == 0 or y == 0 */ - return (-x -y + 1); + if (!x) + return (IDEA_MODULO - y); + + /* !y */ + return (IDEA_MODULO - x); } /* compute 1/a modulo 2^16+1 using Extended euclidean algorithm @@ -97,7 +99,7 @@ static INLINE word16 idea_invmod(word16 x) d -= b; } } while (u); - + /* d is now the inverse, put positive value if required */ if (d < 0) d += IDEA_MODULO; @@ -243,8 +245,8 @@ int wc_IdeaCbcEncrypt(Idea *idea, byte* out, const byte* in, word32 len) blocks = len / IDEA_BLOCK_SIZE; while (blocks--) { - xorbuf(idea->reg, in, IDEA_BLOCK_SIZE); - wc_IdeaCipher(idea, idea->reg, idea->reg); + xorbuf((byte*)idea->reg, in, IDEA_BLOCK_SIZE); + wc_IdeaCipher(idea, (byte*)idea->reg, (byte*)idea->reg); XMEMCPY(out, idea->reg, IDEA_BLOCK_SIZE); out += IDEA_BLOCK_SIZE; @@ -257,17 +259,16 @@ int wc_IdeaCbcEncrypt(Idea *idea, byte* out, const byte* in, word32 len) int wc_IdeaCbcDecrypt(Idea *idea, byte* out, const byte* in, word32 len) { int blocks; - byte tmp[IDEA_BLOCK_SIZE]; if (idea == NULL || out == NULL || in == NULL) return BAD_FUNC_ARG; blocks = len / IDEA_BLOCK_SIZE; while (blocks--) { - XMEMCPY(tmp, in, IDEA_BLOCK_SIZE); - wc_IdeaCipher(idea, out, tmp); - xorbuf(out, idea->reg, IDEA_BLOCK_SIZE); - XMEMCPY(idea->reg, tmp, IDEA_BLOCK_SIZE); + XMEMCPY((byte*)idea->tmp, in, IDEA_BLOCK_SIZE); + wc_IdeaCipher(idea, out, (byte*)idea->tmp); + xorbuf(out, (byte*)idea->reg, IDEA_BLOCK_SIZE); + XMEMCPY(idea->reg, idea->tmp, IDEA_BLOCK_SIZE); out += IDEA_BLOCK_SIZE; in += IDEA_BLOCK_SIZE; diff --git a/wolfssl/wolfcrypt/idea.h b/wolfssl/wolfcrypt/idea.h index c3e70c16b..7fcd2c051 100644 --- a/wolfssl/wolfcrypt/idea.h +++ b/wolfssl/wolfcrypt/idea.h @@ -45,8 +45,9 @@ enum { /* IDEA encryption and decryption */ typedef struct Idea { - byte reg[IDEA_BLOCK_SIZE]; /* for CBC mode */ - word16 skey[IDEA_SK_NUM]; /* 832 bits expanded key */ + word32 reg[IDEA_BLOCK_SIZE / sizeof(word32)]; /* for CBC mode */ + word32 tmp[IDEA_BLOCK_SIZE / sizeof(word32)]; /* for CBC mode */ + word16 skey[IDEA_SK_NUM]; /* 832 bits expanded key */ } Idea; WOLFSSL_API int wc_IdeaSetKey(Idea *idea, const byte* key, word16 keySz, From 71576aef142adb8ac2c859dd27a7d5cd77405619 Mon Sep 17 00:00:00 2001 From: Ludovic FLAMENT Date: Fri, 25 Sep 2015 23:52:08 +0200 Subject: [PATCH 3/3] fix bad computed values --- wolfcrypt/src/idea.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/wolfcrypt/src/idea.c b/wolfcrypt/src/idea.c index 0f411c532..449a3e4d0 100644 --- a/wolfcrypt/src/idea.c +++ b/wolfcrypt/src/idea.c @@ -48,17 +48,20 @@ static INLINE word16 idea_mult(word16 x, word16 y) { long mul, res; - mul = x * y; + mul = (long)x * (long)y; if (mul) { res = (mul & IDEA_MASK) - (mul >> 16); - return (word16) ((res <=0 ? res+IDEA_MODULO : res) & IDEA_MASK); + if (res <= 0) + res += IDEA_MODULO; + + return (word16) (res & IDEA_MASK); } if (!x) - return (IDEA_MODULO - y); + return ((IDEA_MODULO - y) & IDEA_MASK); /* !y */ - return (IDEA_MODULO - x); + return ((IDEA_MODULO - x) & IDEA_MASK); } /* compute 1/a modulo 2^16+1 using Extended euclidean algorithm @@ -98,10 +101,10 @@ static INLINE word16 idea_invmod(word16 x) v -= u; d -= b; } - } while (u); + } while (u != 0); /* d is now the inverse, put positive value if required */ - if (d < 0) + while (d < 0) d += IDEA_MODULO; return (word16)(d & IDEA_MASK);