Big Endian System ChaCha20 counter, auto tests added for ChaCha20 counter

This commit is contained in:
Jacob Barthelmeh
2015-07-06 15:29:53 -06:00
parent 88fa36e3c0
commit 304982a597
2 changed files with 44 additions and 11 deletions

View File

@@ -93,7 +93,7 @@ int wc_Chacha_SetIV(ChaCha* ctx, const byte* inIv, word32 counter)
XMEMCPY(temp, inIv, 12); XMEMCPY(temp, inIv, 12);
ctx->X[12] = LITTLE32(counter); /* block counter */ ctx->X[12] = counter; /* block counter */
ctx->X[13] = LITTLE32(temp[0]); /* fixed variable from nonce */ ctx->X[13] = LITTLE32(temp[0]); /* fixed variable from nonce */
ctx->X[14] = LITTLE32(temp[1]); /* counter from nonce */ ctx->X[14] = LITTLE32(temp[1]); /* counter from nonce */
ctx->X[15] = LITTLE32(temp[2]); /* counter from nonce */ ctx->X[15] = LITTLE32(temp[2]); /* counter from nonce */

View File

@@ -1904,10 +1904,12 @@ int chacha_test(void)
{ {
ChaCha enc; ChaCha enc;
ChaCha dec; ChaCha dec;
byte cipher[32]; byte cipher[128];
byte plain[32]; byte plain[128];
byte sliver[64];
byte input[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; byte input[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
word32 keySz; word32 keySz;
int ret = 0;
int i; int i;
int times = 4; int times = 4;
@@ -1978,23 +1980,54 @@ int chacha_test(void)
XMEMSET(cipher, 0, 32); XMEMSET(cipher, 0, 32);
XMEMCPY(cipher + 4, ivs[i], 8); XMEMCPY(cipher + 4, ivs[i], 8);
wc_Chacha_SetKey(&enc, keys[i], keySz); ret |= wc_Chacha_SetKey(&enc, keys[i], keySz);
wc_Chacha_SetKey(&dec, keys[i], keySz); ret |= wc_Chacha_SetKey(&dec, keys[i], keySz);
if (ret != 0)
return ret;
wc_Chacha_SetIV(&enc, cipher, 0); ret |= wc_Chacha_SetIV(&enc, cipher, 0);
wc_Chacha_SetIV(&dec, cipher, 0); ret |= wc_Chacha_SetIV(&dec, cipher, 0);
if (ret != 0)
return ret;
XMEMCPY(plain, input, 8); XMEMCPY(plain, input, 8);
wc_Chacha_Process(&enc, cipher, plain, (word32)8); ret |= wc_Chacha_Process(&enc, cipher, plain, (word32)8);
wc_Chacha_Process(&dec, plain, cipher, (word32)8); ret |= wc_Chacha_Process(&dec, plain, cipher, (word32)8);
if (ret != 0)
return ret;
if (memcmp(test_chacha[i], cipher, 8)) if (XMEMCMP(test_chacha[i], cipher, 8))
return -130 - 5 - i; return -130 - 5 - i;
if (memcmp(plain, input, 8)) if (XMEMCMP(plain, input, 8))
return -130 - i; return -130 - i;
} }
/* test of starting at a diffrent counter
encrypts all of the information and decrypts starting at 2nd chunck */
XMEMSET(plain, 0, sizeof(plain));
XMEMSET(sliver, 1, sizeof(sliver)); /* set as 1's to not match plain */
XMEMSET(cipher, 0, sizeof(cipher));
XMEMCPY(cipher + 4, ivs[0], 8);
ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
if (ret != 0)
return ret;
ret |= wc_Chacha_SetIV(&enc, cipher, 0);
ret |= wc_Chacha_SetIV(&dec, cipher, 1);
if (ret != 0)
return ret;
ret |= wc_Chacha_Process(&enc, cipher, plain, sizeof(plain));
ret |= wc_Chacha_Process(&dec, sliver, cipher + 64, sizeof(sliver));
if (ret != 0)
return ret;
if (XMEMCMP(plain + 64, sliver, 64))
return -140;
return 0; return 0;
} }
#endif /* HAVE_CHACHA */ #endif /* HAVE_CHACHA */