From 5da8f4fbc6996867f0291c75a17740822bf7993f Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Mon, 18 Sep 2017 19:39:41 +0200 Subject: [PATCH] Add a global xor_buf() private helper function --- .../secretstream_xchacha20poly1305.c | 24 +++++++------------ src/libsodium/include/sodium/private/common.h | 11 +++++++++ 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/src/libsodium/crypto_secretstream/xchacha20poly1305/secretstream_xchacha20poly1305.c b/src/libsodium/crypto_secretstream/xchacha20poly1305/secretstream_xchacha20poly1305.c index eaea7026..441d3594 100644 --- a/src/libsodium/crypto_secretstream/xchacha20poly1305/secretstream_xchacha20poly1305.c +++ b/src/libsodium/crypto_secretstream/xchacha20poly1305/secretstream_xchacha20poly1305.c @@ -76,17 +76,13 @@ crypto_secretstream_xchacha20poly1305_rekey { unsigned char new_key_and_inonce[crypto_stream_chacha20_ietf_KEYBYTES + crypto_secretstream_xchacha20poly1305_INONCEBYTES]; - size_t i; crypto_stream_chacha20_ietf(new_key_and_inonce, sizeof new_key_and_inonce, state->nonce, state->k); - for (i = 0U; i < crypto_stream_chacha20_ietf_KEYBYTES; i++) { - state->k[i] ^= new_key_and_inonce[i]; - } - for (i = 0U; i < crypto_secretstream_xchacha20poly1305_INONCEBYTES; i++) { - STATE_INONCE(state)[i] ^= - new_key_and_inonce[crypto_stream_chacha20_ietf_KEYBYTES + i]; - } + XOR_BUF(state->k, new_key_and_inonce, crypto_stream_chacha20_ietf_KEYBYTES); + XOR_BUF(STATE_INONCE(state), + new_key_and_inonce + crypto_stream_chacha20_ietf_KEYBYTES, + crypto_secretstream_xchacha20poly1305_INONCEBYTES); memset(STATE_COUNTER(state), 0, crypto_secretstream_xchacha20poly1305_COUNTERBYTES); } @@ -103,7 +99,6 @@ crypto_secretstream_xchacha20poly1305_push unsigned char slen[8U]; unsigned char *c; unsigned char *mac; - unsigned int i; if (outlen_p != NULL) { *outlen_p = 0U; @@ -143,9 +138,8 @@ crypto_secretstream_xchacha20poly1305_push COMPILER_ASSERT(crypto_onetimeauth_poly1305_BYTES >= crypto_secretstream_xchacha20poly1305_INONCEBYTES); - for (i = 0U; i < crypto_secretstream_xchacha20poly1305_INONCEBYTES; i++) { - STATE_INONCE(state)[i] ^= mac[i]; - } + XOR_BUF(STATE_INONCE(state), mac, + crypto_secretstream_xchacha20poly1305_INONCEBYTES); sodium_increment(STATE_COUNTER(state), crypto_secretstream_xchacha20poly1305_COUNTERBYTES); if ((tag & crypto_secretstream_xchacha20poly1305_TAG_REKEY) != 0 || @@ -173,7 +167,6 @@ crypto_secretstream_xchacha20poly1305_pull const unsigned char *c; const unsigned char *stored_mac; unsigned long long mlen; - unsigned int i; unsigned char tag; if (mlen_p != NULL) { @@ -225,9 +218,8 @@ crypto_secretstream_xchacha20poly1305_pull } crypto_stream_chacha20_ietf_xor_ic(m, c, mlen, state->nonce, 2U, state->k); - for (i = 0U; i < crypto_secretstream_xchacha20poly1305_INONCEBYTES; i++) { - STATE_INONCE(state)[i] ^= mac[i]; - } + XOR_BUF(STATE_INONCE(state), mac, + crypto_secretstream_xchacha20poly1305_INONCEBYTES); sodium_increment(STATE_COUNTER(state), crypto_secretstream_xchacha20poly1305_COUNTERBYTES); if ((tag & crypto_secretstream_xchacha20poly1305_TAG_REKEY) != 0 || diff --git a/src/libsodium/include/sodium/private/common.h b/src/libsodium/include/sodium/private/common.h index 5e27e574..7e9919fd 100644 --- a/src/libsodium/include/sodium/private/common.h +++ b/src/libsodium/include/sodium/private/common.h @@ -177,6 +177,17 @@ store32_be(uint8_t dst[4], uint32_t w) #endif } +#define XOR_BUF(OUT, IN, N) xor_buf((OUT), (IN), (N)) +static inline void +xor_buf(unsigned char *out, const unsigned char *in, size_t n) +{ + size_t i; + + for (i = 0; i < n; i++) { + out[i] ^= in[i]; + } +} + #ifndef __GNUC__ # ifdef __attribute__ # undef __attribute__