fix chacha on bigendian, touchups

This commit is contained in:
toddouska
2015-04-02 09:40:22 -07:00
parent e1beca3e60
commit a54c916c8f
2 changed files with 18 additions and 17 deletions

View File

@@ -78,10 +78,6 @@
int wc_Chacha_SetIV(ChaCha* ctx, const byte* inIv, word32 counter) int wc_Chacha_SetIV(ChaCha* ctx, const byte* inIv, word32 counter)
{ {
word32 temp[3]; /* used for alignment of memory */ word32 temp[3]; /* used for alignment of memory */
XMEMSET(temp, 0, 12);
if (ctx == NULL)
return BAD_FUNC_ARG;
#ifdef CHACHA_AEAD_TEST #ifdef CHACHA_AEAD_TEST
word32 i; word32 i;
@@ -92,12 +88,15 @@ int wc_Chacha_SetIV(ChaCha* ctx, const byte* inIv, word32 counter)
printf("\n\n"); printf("\n\n");
#endif #endif
if (ctx == NULL)
return BAD_FUNC_ARG;
XMEMCPY(temp, inIv, 12); XMEMCPY(temp, inIv, 12);
ctx->X[12] = counter; /* block counter */ ctx->X[12] = LITTLE32(counter); /* block counter */
ctx->X[13] = temp[0]; /* fixed variable from nonce */ ctx->X[13] = LITTLE32(temp[0]); /* fixed variable from nonce */
ctx->X[14] = temp[1]; /* counter from nonce */ ctx->X[14] = LITTLE32(temp[1]); /* counter from nonce */
ctx->X[15] = temp[2]; /* counter from nonce */ ctx->X[15] = LITTLE32(temp[2]); /* counter from nonce */
return 0; return 0;
} }
@@ -115,14 +114,20 @@ int wc_Chacha_SetKey(ChaCha* ctx, const byte* key, word32 keySz)
const word32* constants; const word32* constants;
const byte* k; const byte* k;
#ifdef XSTREAM_ALIGN
word32 alignKey[8];
#endif
if (ctx == NULL) if (ctx == NULL)
return BAD_FUNC_ARG; return BAD_FUNC_ARG;
if (keySz != 16 && keySz != 32)
return BAD_FUNC_ARG;
#ifdef XSTREAM_ALIGN #ifdef XSTREAM_ALIGN
word32 alignKey[keySz / 4];
if ((wolfssl_word)key % 4) { if ((wolfssl_word)key % 4) {
WOLFSSL_MSG("wc_ChachaSetKey unaligned key"); WOLFSSL_MSG("wc_ChachaSetKey unaligned key");
XMEMCPY(alignKey, key, sizeof(alignKey)); XMEMCPY(alignKey, key, keySz);
k = (byte*)alignKey; k = (byte*)alignKey;
} }
else { else {
@@ -152,10 +157,6 @@ int wc_Chacha_SetKey(ChaCha* ctx, const byte* key, word32 keySz)
constants = sigma; constants = sigma;
} }
else { else {
/* key size of 128 */
if (keySz != 16)
return BAD_FUNC_ARG;
constants = tau; constants = tau;
} }
ctx->X[ 8] = U8TO32_LITTLE(k + 0); ctx->X[ 8] = U8TO32_LITTLE(k + 0);

View File

@@ -703,11 +703,11 @@
/* stream ciphers except arc4 need 32bit alignment, intel ok without */ /* stream ciphers except arc4 need 32bit alignment, intel ok without */
#ifndef XSTREAM_ALIGNMENT #ifndef XSTREAM_ALIGN
#if defined(__x86_64__) || defined(__ia64__) || defined(__i386__) #if defined(__x86_64__) || defined(__ia64__) || defined(__i386__)
#define NO_XSTREAM_ALIGNMENT #define NO_XSTREAM_ALIGN
#else #else
#define XSTREAM_ALIGNMENT #define XSTREAM_ALIGN
#endif #endif
#endif #endif