diff --git a/wolfcrypt/src/chacha.c b/wolfcrypt/src/chacha.c index 25e42a1d4..ab148597d 100644 --- a/wolfcrypt/src/chacha.c +++ b/wolfcrypt/src/chacha.c @@ -78,10 +78,6 @@ int wc_Chacha_SetIV(ChaCha* ctx, const byte* inIv, word32 counter) { word32 temp[3]; /* used for alignment of memory */ - XMEMSET(temp, 0, 12); - - if (ctx == NULL) - return BAD_FUNC_ARG; #ifdef CHACHA_AEAD_TEST word32 i; @@ -92,12 +88,15 @@ int wc_Chacha_SetIV(ChaCha* ctx, const byte* inIv, word32 counter) printf("\n\n"); #endif + if (ctx == NULL) + return BAD_FUNC_ARG; + XMEMCPY(temp, inIv, 12); - ctx->X[12] = counter; /* block counter */ - ctx->X[13] = temp[0]; /* fixed variable from nonce */ - ctx->X[14] = temp[1]; /* counter from nonce */ - ctx->X[15] = temp[2]; /* counter from nonce */ + ctx->X[12] = LITTLE32(counter); /* block counter */ + ctx->X[13] = LITTLE32(temp[0]); /* fixed variable from nonce */ + ctx->X[14] = LITTLE32(temp[1]); /* counter from nonce */ + ctx->X[15] = LITTLE32(temp[2]); /* counter from nonce */ return 0; } @@ -115,14 +114,20 @@ int wc_Chacha_SetKey(ChaCha* ctx, const byte* key, word32 keySz) const word32* constants; const byte* k; +#ifdef XSTREAM_ALIGN + word32 alignKey[8]; +#endif + if (ctx == NULL) return BAD_FUNC_ARG; + if (keySz != 16 && keySz != 32) + return BAD_FUNC_ARG; + #ifdef XSTREAM_ALIGN - word32 alignKey[keySz / 4]; if ((wolfssl_word)key % 4) { WOLFSSL_MSG("wc_ChachaSetKey unaligned key"); - XMEMCPY(alignKey, key, sizeof(alignKey)); + XMEMCPY(alignKey, key, keySz); k = (byte*)alignKey; } else { @@ -152,10 +157,6 @@ int wc_Chacha_SetKey(ChaCha* ctx, const byte* key, word32 keySz) constants = sigma; } else { - /* key size of 128 */ - if (keySz != 16) - return BAD_FUNC_ARG; - constants = tau; } ctx->X[ 8] = U8TO32_LITTLE(k + 0); diff --git a/wolfssl/wolfcrypt/settings.h b/wolfssl/wolfcrypt/settings.h index 23924ca2c..4a773844a 100644 --- a/wolfssl/wolfcrypt/settings.h +++ b/wolfssl/wolfcrypt/settings.h @@ -703,11 +703,11 @@ /* stream ciphers except arc4 need 32bit alignment, intel ok without */ -#ifndef XSTREAM_ALIGNMENT +#ifndef XSTREAM_ALIGN #if defined(__x86_64__) || defined(__ia64__) || defined(__i386__) - #define NO_XSTREAM_ALIGNMENT + #define NO_XSTREAM_ALIGN #else - #define XSTREAM_ALIGNMENT + #define XSTREAM_ALIGN #endif #endif