diff --git a/ctaocrypt/src/aes.c b/ctaocrypt/src/aes.c index cc0e815e8..90a602ba8 100644 --- a/ctaocrypt/src/aes.c +++ b/ctaocrypt/src/aes.c @@ -2865,6 +2865,8 @@ int AesSetIV(Aes* aes, const byte* iv) if (iv) XMEMCPY(aes->reg, iv, AES_BLOCK_SIZE); + else + XMEMSET(aes->reg, 0, AES_BLOCK_SIZE); return 0; } diff --git a/ctaocrypt/src/camellia.c b/ctaocrypt/src/camellia.c index 162625cfc..ecc2c937f 100644 --- a/ctaocrypt/src/camellia.c +++ b/ctaocrypt/src/camellia.c @@ -1517,6 +1517,8 @@ int CamelliaSetIV(Camellia* cam, const byte* iv) if (iv) XMEMCPY(cam->reg, iv, CAMELLIA_BLOCK_SIZE); + else + XMEMSET(cam->reg, 0, CAMELLIA_BLOCK_SIZE); return 0; } diff --git a/ctaocrypt/src/des3.c b/ctaocrypt/src/des3.c index 473e5e007..644f64e05 100644 --- a/ctaocrypt/src/des3.c +++ b/ctaocrypt/src/des3.c @@ -761,6 +761,8 @@ void Des_SetIV(Des* des, const byte* iv) { if (des && iv) XMEMCPY(des->reg, iv, DES_BLOCK_SIZE); + else if (des) + XMEMSET(des->reg, 0, DES_BLOCK_SIZE); } @@ -768,6 +770,8 @@ void Des3_SetIV(Des3* des, const byte* iv) { if (des && iv) XMEMCPY(des->reg, iv, DES_BLOCK_SIZE); + else if (des) + XMEMSET(des->reg, 0, DES_BLOCK_SIZE); } diff --git a/ctaocrypt/src/hc128.c b/ctaocrypt/src/hc128.c index 7d5090b89..a28453247 100644 --- a/ctaocrypt/src/hc128.c +++ b/ctaocrypt/src/hc128.c @@ -229,12 +229,18 @@ static void setup_update(HC128* ctx) /*each time 16 steps*/ -static void Hc128_SetIV(HC128* ctx, const byte* iv) +static void Hc128_SetIV(HC128* ctx, const byte* inIv) { word32 i; - + word32 iv[4]; + + if (inIv) + XMEMCPY(iv, inIv, sizeof(iv)); + else + XMEMSET(iv, 0, sizeof(iv)); + for (i = 0; i < (128 >> 5); i++) - ctx->iv[i] = LITTLE32(((word32*)iv)[i]); + ctx->iv[i] = LITTLE32(iv[i]); for (; i < 8; i++) ctx->iv[i] = ctx->iv[i-4]; @@ -284,16 +290,15 @@ static INLINE int DoKey(HC128* ctx, const byte* key, const byte* iv) int Hc128_SetKey(HC128* ctx, const byte* key, const byte* iv) { #ifdef XSTREAM_ALIGN - if ((word)key % 4 || (word)iv % 4) { + if ((word)key % 4) { int alignKey[4]; - int alignIv[4]; - CYASSL_MSG("Hc128SetKey unaligned key/iv"); + /* iv gets aligned in SetIV */ + CYASSL_MSG("Hc128SetKey unaligned key"); XMEMCPY(alignKey, key, sizeof(alignKey)); - XMEMCPY(alignIv, iv, sizeof(alignIv)); - return DoKey(ctx, (const byte*)alignKey, (const byte*)alignIv); + return DoKey(ctx, (const byte*)alignKey, iv); } #endif /* XSTREAM_ALIGN */ diff --git a/ctaocrypt/src/rabbit.c b/ctaocrypt/src/rabbit.c index baa465eb7..947ac201a 100644 --- a/ctaocrypt/src/rabbit.c +++ b/ctaocrypt/src/rabbit.c @@ -104,14 +104,20 @@ static void RABBIT_next_state(RabbitCtx* ctx) /* IV setup */ -static void RabbitSetIV(Rabbit* ctx, const byte* iv) +static void RabbitSetIV(Rabbit* ctx, const byte* inIv) { /* Temporary variables */ word32 i0, i1, i2, i3, i; + word32 iv[2]; + + if (inIv) + XMEMCPY(iv, inIv, sizeof(iv)); + else + XMEMSET(iv, 0, sizeof(iv)); /* Generate four subvectors */ - i0 = LITTLE32(*(word32*)(iv+0)); - i2 = LITTLE32(*(word32*)(iv+4)); + i0 = LITTLE32(iv[0]); + i2 = LITTLE32(iv[1]); i1 = (i0>>16) | (i2&0xFFFF0000); i3 = (i2<<16) | (i0&0x0000FFFF); @@ -186,7 +192,7 @@ static INLINE int DoKey(Rabbit* ctx, const byte* key, const byte* iv) } ctx->workCtx.carry = ctx->masterCtx.carry; - if (iv) RabbitSetIV(ctx, iv); + RabbitSetIV(ctx, iv); return 0; } @@ -196,17 +202,13 @@ static INLINE int DoKey(Rabbit* ctx, const byte* key, const byte* iv) int RabbitSetKey(Rabbit* ctx, const byte* key, const byte* iv) { #ifdef XSTREAM_ALIGN - if ((word)key % 4 || (iv && (word)iv % 4)) { + if ((word)key % 4) { int alignKey[4]; - int alignIv[2]; - CYASSL_MSG("RabbitSetKey unaligned key/iv"); + /* iv aligned in SetIV */ + CYASSL_MSG("RabbitSetKey unaligned key"); XMEMCPY(alignKey, key, sizeof(alignKey)); - if (iv) { - XMEMCPY(alignIv, iv, sizeof(alignIv)); - iv = (const byte*)alignIv; - } return DoKey(ctx, (const byte*)alignKey, iv); } diff --git a/ctaocrypt/test/test.c b/ctaocrypt/test/test.c index 5137b0d91..c98e6d569 100644 --- a/ctaocrypt/test/test.c +++ b/ctaocrypt/test/test.c @@ -1548,7 +1548,7 @@ int rabbit_test(void) b.outLen = 8; c.input = "\x00\x00\x00\x00\x00\x00\x00\x00"; - c.output = "\x9C\x51\xE2\x87\x84\xC3\x7F\xE9"; + c.output = "\x04\xCE\xCA\x7A\x1A\x86\x6E\x77"; c.inLen = 8; c.outLen = 8; @@ -2202,9 +2202,9 @@ int camellia_test(void) if (XMEMCMP(cam.reg, ivc, CAMELLIA_BLOCK_SIZE)) return -1; - /* Setting the IV to NULL should leave the IV unchanged */ + /* Setting the IV to NULL should be same as all zeros IV */ if (CamelliaSetIV(&cam, NULL) != 0 || - XMEMCMP(cam.reg, ivc, CAMELLIA_BLOCK_SIZE)) + XMEMCMP(cam.reg, ive, CAMELLIA_BLOCK_SIZE)) return -1; /* First parameter should never be null */