Added ChaCha20 implementation with SSE2 opcodes.

This commit is contained in:
Thomas Pornin 2017-07-26 15:58:01 +02:00
parent 5414fd525e
commit 24c6f09bf8
9 changed files with 429 additions and 17 deletions

View file

@ -4178,12 +4178,16 @@ static const struct {
};
static void
test_ChaCha20_ct(void)
test_ChaCha20_generic(const char *name, br_chacha20_run cr)
{
size_t u;
printf("Test ChaCha20_ct: ");
printf("Test %s: ", name);
fflush(stdout);
if (cr == 0) {
printf("UNAVAILABLE\n");
return;
}
for (u = 0; KAT_CHACHA20[u].skey; u ++) {
unsigned char key[32], nonce[12], plain[400], cipher[400];
@ -4199,10 +4203,11 @@ test_ChaCha20_ct(void)
for (v = 0; v < len; v ++) {
unsigned char tmp[400];
size_t w;
uint32_t cc2;
memset(tmp, 0, sizeof tmp);
memcpy(tmp, plain, v);
if (br_chacha20_ct_run(key, nonce, cc, tmp, v)
if (cr(key, nonce, cc, tmp, v)
!= cc + (uint32_t)((v + 63) >> 6))
{
fprintf(stderr, "ChaCha20: wrong counter\n");
@ -4218,7 +4223,21 @@ test_ChaCha20_ct(void)
exit(EXIT_FAILURE);
}
}
br_chacha20_ct_run(key, nonce, cc, tmp, v);
for (w = 0, cc2 = cc; w < v; w += 64, cc2 ++) {
size_t x;
x = v - w;
if (x > 64) {
x = 64;
}
if (cr(key, nonce, cc2, tmp + w, x)
!= (cc2 + 1))
{
fprintf(stderr, "ChaCha20:"
" wrong counter (2)\n");
exit(EXIT_FAILURE);
}
}
if (memcmp(tmp, plain, v) != 0) {
fprintf(stderr, "ChaCha20 KAT fail (2)\n");
exit(EXIT_FAILURE);
@ -4233,6 +4252,18 @@ test_ChaCha20_ct(void)
fflush(stdout);
}
static void
test_ChaCha20_ct(void)
{
test_ChaCha20_generic("ChaCha20_ct", &br_chacha20_ct_run);
}
static void
test_ChaCha20_sse2(void)
{
test_ChaCha20_generic("ChaCha20_sse2", br_chacha20_sse2_get());
}
static const struct {
const char *splain;
const char *saad;
@ -6173,6 +6204,7 @@ static const struct {
STU(DES_tab),
STU(DES_ct),
STU(ChaCha20_ct),
STU(ChaCha20_sse2),
STU(Poly1305_ctmul),
STU(Poly1305_ctmul32),
STU(Poly1305_ctmulq),

View file

@ -175,17 +175,24 @@ test_speed_ ## fname(void) \
static void \
test_speed_ ## fname(void) \
{ \
br_chacha20_run bc; \
unsigned char key[32]; \
unsigned char buf[8192]; \
unsigned char iv[12]; \
int i; \
long num; \
\
bc = br_ ## fname ## _get(); \
if (bc == 0) { \
printf("%-30s UNAVAILABLE\n", #Name); \
fflush(stdout); \
return; \
} \
memset(key, 'T', sizeof key); \
memset(buf, 'P', sizeof buf); \
memset(iv, 'X', sizeof iv); \
for (i = 0; i < 10; i ++) { \
br_ ## fname ## _run(key, iv, i, buf, sizeof buf); \
bc(key, iv, i, buf, sizeof buf); \
} \
num = 10; \
for (;;) { \
@ -195,8 +202,7 @@ test_speed_ ## fname(void) \
\
begin = clock(); \
for (k = num; k > 0; k --) { \
br_ ## fname ## _run(key, iv, \
(uint32_t)k, buf, sizeof buf); \
bc(key, iv, (uint32_t)k, buf, sizeof buf); \
} \
end = clock(); \
tt = (double)(end - begin) / CLOCKS_PER_SEC; \
@ -232,6 +238,7 @@ SPEED_HASH(SHA-512, sha512)
#define br_aes_ct64_cbcenc_get_vtable() (&br_aes_ct64_cbcenc_vtable)
#define br_aes_ct64_cbcdec_get_vtable() (&br_aes_ct64_cbcdec_vtable)
#define br_aes_ct64_ctr_get_vtable() (&br_aes_ct64_ctr_vtable)
#define br_chacha20_ct_get() (&br_chacha20_ct_run)
#define SPEED_AES(iname) \
SPEED_BLOCKCIPHER_CBC(AES-128 CBC encrypt (iname), aes128_ ## iname ## _cbcenc, aes_ ## iname, 16, enc) \
@ -265,7 +272,8 @@ SPEED_BLOCKCIPHER_CBC(3DES CBC decrypt (iname), 3des_ ## iname ## _cbcdec, des_
SPEED_DES(tab)
SPEED_DES(ct)
SPEED_CHACHA20(ChaCha20, chacha20_ct)
SPEED_CHACHA20(ChaCha20 (ct), chacha20_ct)
SPEED_CHACHA20(ChaCha20 (sse2), chacha20_sse2)
static void
test_speed_ghash_inner(char *name, br_ghash gh)
@ -1279,6 +1287,7 @@ static const struct {
STU(3des_ct_cbcdec),
STU(chacha20_ct),
STU(chacha20_sse2),
STU(ghash_ctmul),
STU(ghash_ctmul32),