From 4e9b0b67ce6b6000a6bdb0fcb946dd29bc95c375 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Sat, 27 Feb 2016 16:19:38 +0100 Subject: [PATCH] Let `crypto_core_hsalsa20()` accept `NULL` for the default constants --- .../ref/before_curve25519xsalsa20poly1305.c | 5 +-- .../crypto_core/hsalsa20/ref2/core_hsalsa20.c | 24 +++++++++----- .../crypto_secretbox/crypto_secretbox_easy.c | 8 ++--- .../chacha20/ref/stream_chacha20_ref.c | 32 +++++++------------ .../salsa20/ref/stream_salsa20_ref.c | 8 ++--- .../salsa20/ref/xor_salsa20_ref.c | 8 ++--- .../salsa2012/ref/stream_salsa2012.c | 8 ++--- .../salsa2012/ref/xor_salsa2012.c | 8 ++--- .../salsa208/ref/stream_salsa208.c | 8 ++--- .../crypto_stream/salsa208/ref/xor_salsa208.c | 8 ++--- .../xsalsa20/ref/stream_xsalsa20.c | 6 +--- .../crypto_stream/xsalsa20/ref/xor_xsalsa20.c | 6 +--- 12 files changed, 45 insertions(+), 84 deletions(-) diff --git a/src/libsodium/crypto_box/curve25519xsalsa20poly1305/ref/before_curve25519xsalsa20poly1305.c b/src/libsodium/crypto_box/curve25519xsalsa20poly1305/ref/before_curve25519xsalsa20poly1305.c index 2440c82e..3835538f 100644 --- a/src/libsodium/crypto_box/curve25519xsalsa20poly1305/ref/before_curve25519xsalsa20poly1305.c +++ b/src/libsodium/crypto_box/curve25519xsalsa20poly1305/ref/before_curve25519xsalsa20poly1305.c @@ -2,9 +2,6 @@ #include "crypto_core_hsalsa20.h" #include "crypto_scalarmult_curve25519.h" -static const unsigned char sigma[16] = { - 'e', 'x', 'p', 'a', 'n', 'd', ' ', '3', '2', '-', 'b', 'y', 't', 'e', ' ', 'k' -}; static const unsigned char n[16] = {0}; int crypto_box_curve25519xsalsa20poly1305_beforenm( @@ -17,5 +14,5 @@ int crypto_box_curve25519xsalsa20poly1305_beforenm( if (crypto_scalarmult_curve25519(s,sk,pk) != 0) { return -1; } - return crypto_core_hsalsa20(k,n,s,sigma); + return crypto_core_hsalsa20(k,n,s,NULL); } diff --git a/src/libsodium/crypto_core/hsalsa20/ref2/core_hsalsa20.c b/src/libsodium/crypto_core/hsalsa20/ref2/core_hsalsa20.c index a3c304f5..19aac3c7 100644 --- a/src/libsodium/crypto_core/hsalsa20/ref2/core_hsalsa20.c +++ b/src/libsodium/crypto_core/hsalsa20/ref2/core_hsalsa20.c @@ -8,6 +8,7 @@ Public domain. #include "../../sodium/common.h" #define ROUNDS 20 +#define U32C(v) (v##U) typedef unsigned int uint32; @@ -26,22 +27,29 @@ int crypto_core_hsalsa20( uint32 x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15; int i; - x0 = LOAD32_LE(c + 0); + if (c == NULL) { + x0 = U32C(0x61707865); + x5 = U32C(0x3320646e); + x10 = U32C(0x79622d32); + x15 = U32C(0x6b206574); + } else { + x0 = LOAD32_LE(c + 0); + x5 = LOAD32_LE(c + 4); + x10 = LOAD32_LE(c + 8); + x15 = LOAD32_LE(c + 12); + } x1 = LOAD32_LE(k + 0); x2 = LOAD32_LE(k + 4); x3 = LOAD32_LE(k + 8); x4 = LOAD32_LE(k + 12); - x5 = LOAD32_LE(c + 4); - x6 = LOAD32_LE(in + 0); - x7 = LOAD32_LE(in + 4); - x8 = LOAD32_LE(in + 8); - x9 = LOAD32_LE(in + 12); - x10 = LOAD32_LE(c + 8); x11 = LOAD32_LE(k + 16); x12 = LOAD32_LE(k + 20); x13 = LOAD32_LE(k + 24); x14 = LOAD32_LE(k + 28); - x15 = LOAD32_LE(c + 12); + x6 = LOAD32_LE(in + 0); + x7 = LOAD32_LE(in + 4); + x8 = LOAD32_LE(in + 8); + x9 = LOAD32_LE(in + 12); for (i = ROUNDS;i > 0;i -= 2) { x4 ^= rotate( x0+x12, 7); diff --git a/src/libsodium/crypto_secretbox/crypto_secretbox_easy.c b/src/libsodium/crypto_secretbox/crypto_secretbox_easy.c index 7802b003..dcc21b33 100644 --- a/src/libsodium/crypto_secretbox/crypto_secretbox_easy.c +++ b/src/libsodium/crypto_secretbox/crypto_secretbox_easy.c @@ -11,10 +11,6 @@ #include "crypto_stream_salsa20.h" #include "utils.h" -static const unsigned char sigma[16] = { - 'e', 'x', 'p', 'a', 'n', 'd', ' ', '3', '2', '-', 'b', 'y', 't', 'e', ' ', 'k' -}; - int crypto_secretbox_detached(unsigned char *c, unsigned char *mac, const unsigned char *m, @@ -27,7 +23,7 @@ crypto_secretbox_detached(unsigned char *c, unsigned char *mac, unsigned long long i; unsigned long long mlen0; - crypto_core_hsalsa20(subkey, n, k, sigma); + crypto_core_hsalsa20(subkey, n, k, NULL); if (((uintptr_t) c >= (uintptr_t) m && (uintptr_t) c - (uintptr_t) m < mlen) || @@ -93,7 +89,7 @@ crypto_secretbox_open_detached(unsigned char *m, const unsigned char *c, unsigned long long i; unsigned long long mlen0; - crypto_core_hsalsa20(subkey, n, k, sigma); + crypto_core_hsalsa20(subkey, n, k, NULL); crypto_stream_salsa20(block0, crypto_stream_salsa20_KEYBYTES, n + 16, subkey); if (crypto_onetimeauth_poly1305_verify(mac, c, clen, block0) != 0) { diff --git a/src/libsodium/crypto_stream/chacha20/ref/stream_chacha20_ref.c b/src/libsodium/crypto_stream/chacha20/ref/stream_chacha20_ref.c index 9ccdb4ba..15cbae78 100644 --- a/src/libsodium/crypto_stream/chacha20/ref/stream_chacha20_ref.c +++ b/src/libsodium/crypto_stream/chacha20/ref/stream_chacha20_ref.c @@ -44,29 +44,21 @@ typedef struct chacha_ctx chacha_ctx; a = PLUS(a,b); d = ROTATE(XOR(d,a), 8); \ c = PLUS(c,d); b = ROTATE(XOR(b,c), 7); -static const unsigned char sigma[16] = { - 'e', 'x', 'p', 'a', 'n', 'd', ' ', '3', '2', '-', 'b', 'y', 't', 'e', ' ', 'k' -}; - static void chacha_keysetup(chacha_ctx *ctx, const u8 *k) { - const unsigned char *constants; - - ctx->input[4] = LOAD32_LE(k + 0); - ctx->input[5] = LOAD32_LE(k + 4); - ctx->input[6] = LOAD32_LE(k + 8); - ctx->input[7] = LOAD32_LE(k + 12); - k += 16; - constants = sigma; - ctx->input[8] = LOAD32_LE(k + 0); - ctx->input[9] = LOAD32_LE(k + 4); - ctx->input[10] = LOAD32_LE(k + 8); - ctx->input[11] = LOAD32_LE(k + 12); - ctx->input[0] = LOAD32_LE(constants + 0); - ctx->input[1] = LOAD32_LE(constants + 4); - ctx->input[2] = LOAD32_LE(constants + 8); - ctx->input[3] = LOAD32_LE(constants + 12); + ctx->input[0] = U32C(0x61707865); + ctx->input[1] = U32C(0x3320646e); + ctx->input[2] = U32C(0x79622d32); + ctx->input[3] = U32C(0x6b206574); + ctx->input[4] = LOAD32_LE(k + 0); + ctx->input[5] = LOAD32_LE(k + 4); + ctx->input[6] = LOAD32_LE(k + 8); + ctx->input[7] = LOAD32_LE(k + 12); + ctx->input[8] = LOAD32_LE(k + 16); + ctx->input[9] = LOAD32_LE(k + 20); + ctx->input[10] = LOAD32_LE(k + 24); + ctx->input[11] = LOAD32_LE(k + 28); } static void diff --git a/src/libsodium/crypto_stream/salsa20/ref/stream_salsa20_ref.c b/src/libsodium/crypto_stream/salsa20/ref/stream_salsa20_ref.c index 3e4f6eb0..b808446e 100644 --- a/src/libsodium/crypto_stream/salsa20/ref/stream_salsa20_ref.c +++ b/src/libsodium/crypto_stream/salsa20/ref/stream_salsa20_ref.c @@ -12,10 +12,6 @@ Public domain. typedef unsigned int uint32; -static const unsigned char sigma[16] = { - 'e', 'x', 'p', 'a', 'n', 'd', ' ', '3', '2', '-', 'b', 'y', 't', 'e', ' ', 'k' -}; - int crypto_stream_salsa20( unsigned char *c,unsigned long long clen, const unsigned char *n, @@ -35,7 +31,7 @@ int crypto_stream_salsa20( for (i = 8;i < 16;++i) in[i] = 0; while (clen >= 64) { - crypto_core_salsa20(c,in,kcopy,sigma); + crypto_core_salsa20(c,in,kcopy,NULL); u = 1; for (i = 8;i < 16;++i) { @@ -49,7 +45,7 @@ int crypto_stream_salsa20( } if (clen) { - crypto_core_salsa20(block,in,kcopy,sigma); + crypto_core_salsa20(block,in,kcopy,NULL); for (i = 0;i < (unsigned int) clen;++i) c[i] = block[i]; } sodium_memzero(block, sizeof block); diff --git a/src/libsodium/crypto_stream/salsa20/ref/xor_salsa20_ref.c b/src/libsodium/crypto_stream/salsa20/ref/xor_salsa20_ref.c index b998ae4c..d4082a78 100644 --- a/src/libsodium/crypto_stream/salsa20/ref/xor_salsa20_ref.c +++ b/src/libsodium/crypto_stream/salsa20/ref/xor_salsa20_ref.c @@ -14,10 +14,6 @@ Public domain. typedef unsigned int uint32; -static const unsigned char sigma[16] = { - 'e', 'x', 'p', 'a', 'n', 'd', ' ', '3', '2', '-', 'b', 'y', 't', 'e', ' ', 'k' -}; - int crypto_stream_salsa20_xor_ic( unsigned char *c, const unsigned char *m,unsigned long long mlen, @@ -41,7 +37,7 @@ int crypto_stream_salsa20_xor_ic( } while (mlen >= 64) { - crypto_core_salsa20(block,in,kcopy,sigma); + crypto_core_salsa20(block,in,kcopy,NULL); for (i = 0;i < 64;++i) c[i] = m[i] ^ block[i]; u = 1; @@ -57,7 +53,7 @@ int crypto_stream_salsa20_xor_ic( } if (mlen) { - crypto_core_salsa20(block,in,kcopy,sigma); + crypto_core_salsa20(block,in,kcopy,NULL); for (i = 0;i < (unsigned int) mlen;++i) c[i] = m[i] ^ block[i]; } sodium_memzero(block, sizeof block); diff --git a/src/libsodium/crypto_stream/salsa2012/ref/stream_salsa2012.c b/src/libsodium/crypto_stream/salsa2012/ref/stream_salsa2012.c index 5008ab69..986ec6e5 100644 --- a/src/libsodium/crypto_stream/salsa2012/ref/stream_salsa2012.c +++ b/src/libsodium/crypto_stream/salsa2012/ref/stream_salsa2012.c @@ -10,10 +10,6 @@ Public domain. typedef unsigned int uint32; -static const unsigned char sigma[16] = { - 'e', 'x', 'p', 'a', 'n', 'd', ' ', '3', '2', '-', 'b', 'y', 't', 'e', ' ', 'k' -}; - int crypto_stream_salsa2012( unsigned char *c,unsigned long long clen, const unsigned char *n, @@ -33,7 +29,7 @@ int crypto_stream_salsa2012( for (i = 8;i < 16;++i) in[i] = 0; while (clen >= 64) { - crypto_core_salsa2012(c,in,kcopy,sigma); + crypto_core_salsa2012(c,in,kcopy,NULL); u = 1; for (i = 8;i < 16;++i) { @@ -47,7 +43,7 @@ int crypto_stream_salsa2012( } if (clen) { - crypto_core_salsa2012(block,in,kcopy,sigma); + crypto_core_salsa2012(block,in,kcopy,NULL); for (i = 0;i < (unsigned int) clen;++i) c[i] = block[i]; } sodium_memzero(block, sizeof block); diff --git a/src/libsodium/crypto_stream/salsa2012/ref/xor_salsa2012.c b/src/libsodium/crypto_stream/salsa2012/ref/xor_salsa2012.c index ab471b33..3443ac20 100644 --- a/src/libsodium/crypto_stream/salsa2012/ref/xor_salsa2012.c +++ b/src/libsodium/crypto_stream/salsa2012/ref/xor_salsa2012.c @@ -10,10 +10,6 @@ Public domain. typedef unsigned int uint32; -static const unsigned char sigma[16] = { - 'e', 'x', 'p', 'a', 'n', 'd', ' ', '3', '2', '-', 'b', 'y', 't', 'e', ' ', 'k' -}; - int crypto_stream_salsa2012_xor( unsigned char *c, const unsigned char *m,unsigned long long mlen, @@ -34,7 +30,7 @@ int crypto_stream_salsa2012_xor( for (i = 8;i < 16;++i) in[i] = 0; while (mlen >= 64) { - crypto_core_salsa2012(block,in,kcopy,sigma); + crypto_core_salsa2012(block,in,kcopy,NULL); for (i = 0;i < 64;++i) c[i] = m[i] ^ block[i]; u = 1; @@ -50,7 +46,7 @@ int crypto_stream_salsa2012_xor( } if (mlen) { - crypto_core_salsa2012(block,in,kcopy,sigma); + crypto_core_salsa2012(block,in,kcopy,NULL); for (i = 0;i < (unsigned int) mlen;++i) c[i] = m[i] ^ block[i]; } sodium_memzero(block, sizeof block); diff --git a/src/libsodium/crypto_stream/salsa208/ref/stream_salsa208.c b/src/libsodium/crypto_stream/salsa208/ref/stream_salsa208.c index 7869b846..386d8daa 100644 --- a/src/libsodium/crypto_stream/salsa208/ref/stream_salsa208.c +++ b/src/libsodium/crypto_stream/salsa208/ref/stream_salsa208.c @@ -10,10 +10,6 @@ Public domain. typedef unsigned int uint32; -static const unsigned char sigma[16] = { - 'e', 'x', 'p', 'a', 'n', 'd', ' ', '3', '2', '-', 'b', 'y', 't', 'e', ' ', 'k' -}; - int crypto_stream_salsa208( unsigned char *c,unsigned long long clen, const unsigned char *n, @@ -33,7 +29,7 @@ int crypto_stream_salsa208( for (i = 8;i < 16;++i) in[i] = 0; while (clen >= 64) { - crypto_core_salsa208(c,in,kcopy,sigma); + crypto_core_salsa208(c,in,kcopy,NULL); u = 1; for (i = 8;i < 16;++i) { @@ -47,7 +43,7 @@ int crypto_stream_salsa208( } if (clen) { - crypto_core_salsa208(block,in,kcopy,sigma); + crypto_core_salsa208(block,in,kcopy,NULL); for (i = 0;i < (unsigned int) clen;++i) c[i] = block[i]; } sodium_memzero(block, sizeof block); diff --git a/src/libsodium/crypto_stream/salsa208/ref/xor_salsa208.c b/src/libsodium/crypto_stream/salsa208/ref/xor_salsa208.c index 706970f0..1e6b4e71 100644 --- a/src/libsodium/crypto_stream/salsa208/ref/xor_salsa208.c +++ b/src/libsodium/crypto_stream/salsa208/ref/xor_salsa208.c @@ -10,10 +10,6 @@ Public domain. typedef unsigned int uint32; -static const unsigned char sigma[16] = { - 'e', 'x', 'p', 'a', 'n', 'd', ' ', '3', '2', '-', 'b', 'y', 't', 'e', ' ', 'k' -}; - int crypto_stream_salsa208_xor( unsigned char *c, const unsigned char *m,unsigned long long mlen, @@ -34,7 +30,7 @@ int crypto_stream_salsa208_xor( for (i = 8;i < 16;++i) in[i] = 0; while (mlen >= 64) { - crypto_core_salsa208(block,in,kcopy,sigma); + crypto_core_salsa208(block,in,kcopy,NULL); for (i = 0;i < 64;++i) c[i] = m[i] ^ block[i]; u = 1; @@ -50,7 +46,7 @@ int crypto_stream_salsa208_xor( } if (mlen) { - crypto_core_salsa208(block,in,kcopy,sigma); + crypto_core_salsa208(block,in,kcopy,NULL); for (i = 0;i < (unsigned int) mlen;++i) c[i] = m[i] ^ block[i]; } sodium_memzero(block, sizeof block); diff --git a/src/libsodium/crypto_stream/xsalsa20/ref/stream_xsalsa20.c b/src/libsodium/crypto_stream/xsalsa20/ref/stream_xsalsa20.c index c6614cbb..1e6513a8 100644 --- a/src/libsodium/crypto_stream/xsalsa20/ref/stream_xsalsa20.c +++ b/src/libsodium/crypto_stream/xsalsa20/ref/stream_xsalsa20.c @@ -9,10 +9,6 @@ Public domain. #include "crypto_stream_xsalsa20.h" #include "utils.h" -static const unsigned char sigma[16] = { - 'e', 'x', 'p', 'a', 'n', 'd', ' ', '3', '2', '-', 'b', 'y', 't', 'e', ' ', 'k' -}; - int crypto_stream_xsalsa20( unsigned char *c,unsigned long long clen, const unsigned char *n, @@ -21,7 +17,7 @@ int crypto_stream_xsalsa20( { unsigned char subkey[32]; int ret; - crypto_core_hsalsa20(subkey,n,k,sigma); + crypto_core_hsalsa20(subkey,n,k,NULL); ret = crypto_stream_salsa20(c,clen,n + 16,subkey); sodium_memzero(subkey, sizeof subkey); return ret; diff --git a/src/libsodium/crypto_stream/xsalsa20/ref/xor_xsalsa20.c b/src/libsodium/crypto_stream/xsalsa20/ref/xor_xsalsa20.c index b38c6510..7a4562b6 100644 --- a/src/libsodium/crypto_stream/xsalsa20/ref/xor_xsalsa20.c +++ b/src/libsodium/crypto_stream/xsalsa20/ref/xor_xsalsa20.c @@ -9,10 +9,6 @@ Public domain. #include "crypto_stream_xsalsa20.h" #include "utils.h" -static const unsigned char sigma[16] = { - 'e', 'x', 'p', 'a', 'n', 'd', ' ', '3', '2', '-', 'b', 'y', 't', 'e', ' ', 'k' -}; - int crypto_stream_xsalsa20_xor_ic( unsigned char *c, const unsigned char *m,unsigned long long mlen, @@ -22,7 +18,7 @@ int crypto_stream_xsalsa20_xor_ic( { unsigned char subkey[32]; int ret; - crypto_core_hsalsa20(subkey,n,k,sigma); + crypto_core_hsalsa20(subkey,n,k,NULL); ret = crypto_stream_salsa20_xor_ic(c,m,mlen,n + 16,ic,subkey); sodium_memzero(subkey, sizeof subkey); return ret;