From 7d756fab960d01e27bc6d1fcd5649703f43a78c1 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Mon, 18 Sep 2017 19:25:06 +0200 Subject: [PATCH] xor the key and the nonce on rekey for better separation --- .../secretstream_xchacha20poly1305.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/libsodium/crypto_secretstream/xchacha20poly1305/secretstream_xchacha20poly1305.c b/src/libsodium/crypto_secretstream/xchacha20poly1305/secretstream_xchacha20poly1305.c index bdca9c63..eaea7026 100644 --- a/src/libsodium/crypto_secretstream/xchacha20poly1305/secretstream_xchacha20poly1305.c +++ b/src/libsodium/crypto_secretstream/xchacha20poly1305/secretstream_xchacha20poly1305.c @@ -76,13 +76,17 @@ 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); - memcpy(state->k, new_key_and_inonce, crypto_stream_chacha20_ietf_KEYBYTES); - memcpy(STATE_INONCE(state), - new_key_and_inonce + crypto_stream_chacha20_ietf_KEYBYTES, - crypto_secretstream_xchacha20poly1305_INONCEBYTES); + 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]; + } memset(STATE_COUNTER(state), 0, crypto_secretstream_xchacha20poly1305_COUNTERBYTES); }