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
+43 -10
View File
@@ -1904,10 +1904,12 @@ int chacha_test(void)
{
ChaCha enc;
ChaCha dec;
byte cipher[32];
byte plain[32];
byte cipher[128];
byte plain[128];
byte sliver[64];
byte input[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
word32 keySz;
int ret = 0;
int i;
int times = 4;
@@ -1978,23 +1980,54 @@ int chacha_test(void)
XMEMSET(cipher, 0, 32);
XMEMCPY(cipher + 4, ivs[i], 8);
wc_Chacha_SetKey(&enc, keys[i], keySz);
wc_Chacha_SetKey(&dec, keys[i], keySz);
ret |= wc_Chacha_SetKey(&enc, keys[i], keySz);
ret |= wc_Chacha_SetKey(&dec, keys[i], keySz);
if (ret != 0)
return ret;
wc_Chacha_SetIV(&enc, cipher, 0);
wc_Chacha_SetIV(&dec, cipher, 0);
ret |= wc_Chacha_SetIV(&enc, cipher, 0);
ret |= wc_Chacha_SetIV(&dec, cipher, 0);
if (ret != 0)
return ret;
XMEMCPY(plain, input, 8);
wc_Chacha_Process(&enc, cipher, plain, (word32)8);
wc_Chacha_Process(&dec, plain, cipher, (word32)8);
ret |= wc_Chacha_Process(&enc, cipher, plain, (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;
if (memcmp(plain, input, 8))
if (XMEMCMP(plain, input, 8))
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;
}
#endif /* HAVE_CHACHA */