mirror of https://github.com/wolfSSL/wolfssl.git
parent
86d74efc37
commit
ae6b4be135
|
@ -46,17 +46,19 @@
|
||||||
*/
|
*/
|
||||||
static INLINE word16 idea_mult(word16 x, word16 y)
|
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) {
|
if (mul) {
|
||||||
res = (mul & IDEA_MASK) - (mul >> 16);
|
res = (mul & IDEA_MASK) - (mul >> 16);
|
||||||
res -= (res >> 16);
|
|
||||||
return (word16) ((res <=0 ? res+IDEA_MODULO : res) & IDEA_MASK);
|
return (word16) ((res <=0 ? res+IDEA_MODULO : res) & IDEA_MASK);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* x == 0 or y == 0 */
|
if (!x)
|
||||||
return (-x -y + 1);
|
return (IDEA_MODULO - y);
|
||||||
|
|
||||||
|
/* !y */
|
||||||
|
return (IDEA_MODULO - x);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* compute 1/a modulo 2^16+1 using Extended euclidean algorithm
|
/* compute 1/a modulo 2^16+1 using Extended euclidean algorithm
|
||||||
|
@ -97,7 +99,7 @@ static INLINE word16 idea_invmod(word16 x)
|
||||||
d -= b;
|
d -= b;
|
||||||
}
|
}
|
||||||
} while (u);
|
} while (u);
|
||||||
|
|
||||||
/* d is now the inverse, put positive value if required */
|
/* d is now the inverse, put positive value if required */
|
||||||
if (d < 0)
|
if (d < 0)
|
||||||
d += IDEA_MODULO;
|
d += IDEA_MODULO;
|
||||||
|
@ -243,8 +245,8 @@ int wc_IdeaCbcEncrypt(Idea *idea, byte* out, const byte* in, word32 len)
|
||||||
|
|
||||||
blocks = len / IDEA_BLOCK_SIZE;
|
blocks = len / IDEA_BLOCK_SIZE;
|
||||||
while (blocks--) {
|
while (blocks--) {
|
||||||
xorbuf(idea->reg, in, IDEA_BLOCK_SIZE);
|
xorbuf((byte*)idea->reg, in, IDEA_BLOCK_SIZE);
|
||||||
wc_IdeaCipher(idea, idea->reg, idea->reg);
|
wc_IdeaCipher(idea, (byte*)idea->reg, (byte*)idea->reg);
|
||||||
XMEMCPY(out, idea->reg, IDEA_BLOCK_SIZE);
|
XMEMCPY(out, idea->reg, IDEA_BLOCK_SIZE);
|
||||||
|
|
||||||
out += 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 wc_IdeaCbcDecrypt(Idea *idea, byte* out, const byte* in, word32 len)
|
||||||
{
|
{
|
||||||
int blocks;
|
int blocks;
|
||||||
byte tmp[IDEA_BLOCK_SIZE];
|
|
||||||
|
|
||||||
if (idea == NULL || out == NULL || in == NULL)
|
if (idea == NULL || out == NULL || in == NULL)
|
||||||
return BAD_FUNC_ARG;
|
return BAD_FUNC_ARG;
|
||||||
|
|
||||||
blocks = len / IDEA_BLOCK_SIZE;
|
blocks = len / IDEA_BLOCK_SIZE;
|
||||||
while (blocks--) {
|
while (blocks--) {
|
||||||
XMEMCPY(tmp, in, IDEA_BLOCK_SIZE);
|
XMEMCPY((byte*)idea->tmp, in, IDEA_BLOCK_SIZE);
|
||||||
wc_IdeaCipher(idea, out, tmp);
|
wc_IdeaCipher(idea, out, (byte*)idea->tmp);
|
||||||
xorbuf(out, idea->reg, IDEA_BLOCK_SIZE);
|
xorbuf(out, (byte*)idea->reg, IDEA_BLOCK_SIZE);
|
||||||
XMEMCPY(idea->reg, tmp, IDEA_BLOCK_SIZE);
|
XMEMCPY(idea->reg, idea->tmp, IDEA_BLOCK_SIZE);
|
||||||
|
|
||||||
out += IDEA_BLOCK_SIZE;
|
out += IDEA_BLOCK_SIZE;
|
||||||
in += IDEA_BLOCK_SIZE;
|
in += IDEA_BLOCK_SIZE;
|
||||||
|
|
|
@ -45,8 +45,9 @@ enum {
|
||||||
|
|
||||||
/* IDEA encryption and decryption */
|
/* IDEA encryption and decryption */
|
||||||
typedef struct Idea {
|
typedef struct Idea {
|
||||||
byte reg[IDEA_BLOCK_SIZE]; /* for CBC mode */
|
word32 reg[IDEA_BLOCK_SIZE / sizeof(word32)]; /* for CBC mode */
|
||||||
word16 skey[IDEA_SK_NUM]; /* 832 bits expanded key */
|
word32 tmp[IDEA_BLOCK_SIZE / sizeof(word32)]; /* for CBC mode */
|
||||||
|
word16 skey[IDEA_SK_NUM]; /* 832 bits expanded key */
|
||||||
} Idea;
|
} Idea;
|
||||||
|
|
||||||
WOLFSSL_API int wc_IdeaSetKey(Idea *idea, const byte* key, word16 keySz,
|
WOLFSSL_API int wc_IdeaSetKey(Idea *idea, const byte* key, word16 keySz,
|
||||||
|
|
Loading…
Reference in New Issue