diff --git a/ctaocrypt/src/poly1305.c b/ctaocrypt/src/poly1305.c index c73a9cb49..1bf0304ee 100644 --- a/ctaocrypt/src/poly1305.c +++ b/ctaocrypt/src/poly1305.c @@ -22,13 +22,13 @@ * and Daniel J. Bernstein */ -#ifdef HAVE_POLY1305 - #ifdef HAVE_CONFIG_H #include #endif #include + +#ifdef HAVE_POLY1305 #include #include #include @@ -129,6 +129,7 @@ static void poly1305_blocks(Poly1305* ctx, const unsigned char *m, size_t bytes) { + #ifdef POLY130564 const word64 hibit = (ctx->final) ? 0 : ((word64)1 << 40); /* 1 << 128 */ @@ -138,7 +139,20 @@ static void poly1305_blocks(Poly1305* ctx, const unsigned char *m, word64 c; word128 d0,d1,d2,d; - r0 = ctx->r[0]; +#else + + const word32 hibit = (ctx->final) ? 0 : (1 << 24); /* 1 << 128 */ + word32 r0,r1,r2,r3,r4; + word32 s1,s2,s3,s4; + word32 h0,h1,h2,h3,h4; + word64 d0,d1,d2,d3,d4; + word32 c; + +#endif + +#ifdef POLY130564 + + r0 = ctx->r[0]; r1 = ctx->r[1]; r2 = ctx->r[2]; @@ -181,14 +195,7 @@ static void poly1305_blocks(Poly1305* ctx, const unsigned char *m, ctx->h[2] = h2; #else /* if not 64 bit then use 32 bit */ - - const word32 hibit = (ctx->final) ? 0 : (1 << 24); /* 1 << 128 */ - word32 r0,r1,r2,r3,r4; - word32 s1,s2,s3,s4; - word32 h0,h1,h2,h3,h4; - word64 d0,d1,d2,d3,d4; - word32 c; - + r0 = ctx->r[0]; r1 = ctx->r[1]; r2 = ctx->r[2]; @@ -251,8 +258,9 @@ static void poly1305_blocks(Poly1305* ctx, const unsigned char *m, int Poly1305SetKey(Poly1305* ctx, const byte* key, word32 keySz) { - if (keySz != 32 || ctx == NULL) - return BAD_FUNC_ARG; +#if defined(POLY130564) + word64 t0,t1; +#endif #ifdef CHACHA_AEAD_TEST word32 k; @@ -265,9 +273,10 @@ int Poly1305SetKey(Poly1305* ctx, const byte* key, word32 keySz) { printf("\n"); #endif + if (keySz != 32 || ctx == NULL) + return BAD_FUNC_ARG; + #if defined(POLY130564) - - word64 t0,t1; /* r &= 0xffffffc0ffffffc0ffffffc0fffffff */ t0 = U8TO64(key + 0); @@ -319,15 +328,26 @@ int Poly1305SetKey(Poly1305* ctx, const byte* key, word32 keySz) { int Poly1305Final(Poly1305* ctx, byte* mac) { - if (ctx == NULL) - return BAD_FUNC_ARG; - #if defined(POLY130564) word64 h0,h1,h2,c; word64 g0,g1,g2; word64 t0,t1; +#else + + word32 h0,h1,h2,h3,h4,c; + word32 g0,g1,g2,g3,g4; + word64 f; + word32 mask; + +#endif + + if (ctx == NULL) + return BAD_FUNC_ARG; + +#if defined(POLY130564) + /* process the remaining block */ if (ctx->leftover) { size_t i = ctx->leftover; @@ -396,11 +416,6 @@ int Poly1305Final(Poly1305* ctx, byte* mac) { #else /* if not 64 bit then use 32 bit */ - word32 h0,h1,h2,h3,h4,c; - word32 g0,g1,g2,g3,g4; - word64 f; - word32 mask; - /* process the remaining block */ if (ctx->leftover) { size_t i = ctx->leftover; @@ -487,8 +502,7 @@ int Poly1305Final(Poly1305* ctx, byte* mac) { int Poly1305Update(Poly1305* ctx, const byte* m, word32 bytes) { - if (ctx == NULL) - return BAD_FUNC_ARG; + size_t i; #ifdef CHACHA_AEAD_TEST word32 k; @@ -500,7 +514,9 @@ int Poly1305Update(Poly1305* ctx, const byte* m, word32 bytes) { } printf("\n"); #endif - size_t i; + + if (ctx == NULL) + return BAD_FUNC_ARG; /* handle leftover */ if (ctx->leftover) { diff --git a/ctaocrypt/test/test.c b/ctaocrypt/test/test.c index 73011d6da..78817abe7 100644 --- a/ctaocrypt/test/test.c +++ b/ctaocrypt/test/test.c @@ -1837,6 +1837,8 @@ int rabbit_test(void) #ifdef HAVE_CHACHA int chacha_test(void) { + ChaCha enc; + ChaCha dec; byte cipher[32]; byte plain[32]; byte input[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; @@ -1844,7 +1846,7 @@ int chacha_test(void) int i; int times = 4; - const byte key1[] = + static const byte key1[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, @@ -1852,7 +1854,7 @@ int chacha_test(void) 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }; - const byte key2[] = + static const byte key2[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, @@ -1860,7 +1862,7 @@ int chacha_test(void) 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01 }; - const byte key3[] = + static const byte key3[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, @@ -1869,7 +1871,7 @@ int chacha_test(void) }; /* 128 bit key */ - const byte key4[] = + static const byte key4[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 @@ -1878,10 +1880,10 @@ int chacha_test(void) const byte* keys[] = {key1, key2, key3, key4}; - const byte ivs1[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; - const byte ivs2[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; - const byte ivs3[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01}; - const byte ivs4[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; + static const byte ivs1[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; + static const byte ivs2[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; + static const byte ivs3[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01}; + static const byte ivs4[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; const byte* ivs[] = {ivs1, ivs2, ivs3, ivs4}; @@ -1906,8 +1908,6 @@ int chacha_test(void) else { keySz = 16; } - ChaCha enc; - ChaCha dec; XMEMCPY(plain, keys[i], keySz); XMEMSET(cipher, 0, 32); @@ -1916,8 +1916,8 @@ int chacha_test(void) Chacha_SetKey(&enc, keys[i], keySz); Chacha_SetKey(&dec, keys[i], keySz); - Chacha_SetIV(&enc, cipher,0); - Chacha_SetIV(&dec, cipher,0); + Chacha_SetIV(&enc, cipher, 0); + Chacha_SetIV(&dec, cipher, 0); XMEMCPY(plain, input, 8); Chacha_Process(&enc, cipher, plain, (word32)8); diff --git a/cyassl/ctaocrypt/chacha.h b/cyassl/ctaocrypt/chacha.h index baad676af..2eb8065fd 100644 --- a/cyassl/ctaocrypt/chacha.h +++ b/cyassl/ctaocrypt/chacha.h @@ -19,8 +19,8 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ -#ifndef CHACHA_H -#define CHACHA_H +#ifndef CTAO_CRYPT_CHACHA_H +#define CTAO_CRYPT_CHACHA_H #include "types.h" diff --git a/cyassl/ctaocrypt/poly1305.h b/cyassl/ctaocrypt/poly1305.h index 8f5df5caa..600156279 100644 --- a/cyassl/ctaocrypt/poly1305.h +++ b/cyassl/ctaocrypt/poly1305.h @@ -34,7 +34,8 @@ /* auto detect between 32bit / 64bit */ #define HAS_SIZEOF_INT128_64BIT (defined(__SIZEOF_INT128__) && defined(__LP64__)) #define HAS_MSVC_64BIT (defined(_MSC_VER) && defined(_M_X64)) -#define HAS_GCC_4_4_64BIT (defined(__GNUC__) && defined(__LP64__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 4)))) +#define HAS_GCC_4_4_64BIT (defined(__GNUC__) && defined(__LP64__) && \ + ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 4)))) #if (HAS_SIZEOF_INT128_64BIT || HAS_MSVC_64BIT || HAS_GCC_4_4_64BIT) #define POLY130564 diff --git a/src/internal.c b/src/internal.c index 7b635f1c7..2615fd5c0 100644 --- a/src/internal.c +++ b/src/internal.c @@ -4891,7 +4891,10 @@ static int ChachaAEADEncrypt(CYASSL* ssl, byte* out, const byte* input, byte additional[CHACHA20_BLOCK_SIZE]; byte nonce[AEAD_NONCE_SZ]; byte cipher[CHACHA20_256_KEY_SIZE]; /* generated key for poly1305 */ - + #ifdef CHACHA_AEAD_TEST + int i; + #endif + XMEMSET(tag, 0, sizeof(tag)); XMEMSET(nonce, 0, AEAD_NONCE_SZ); XMEMSET(cipher, 0, sizeof(cipher)); @@ -4916,7 +4919,6 @@ static int ChachaAEADEncrypt(CYASSL* ssl, byte* out, const byte* input, XMEMCPY(additional + AEAD_TYPE_OFFSET, additionalSrc, 3); #ifdef CHACHA_AEAD_TEST - int i; printf("Encrypt Additional : "); for (i = 0; i < CHACHA20_BLOCK_SIZE; i++) { printf("%02x", additional[i]);