From 7692332f0e9df279e7e87822327aefbbea3f47a2 Mon Sep 17 00:00:00 2001 From: orignal Date: Fri, 30 Nov 2018 21:31:06 -0500 Subject: [PATCH] don't inialize Chacha20 state twice --- libi2pd/ChaCha20.cpp | 32 +++++++++++++++++--------------- libi2pd/ChaCha20.h | 24 ++++++++++++------------ libi2pd/Crypto.cpp | 17 ++++++++++------- 3 files changed, 39 insertions(+), 34 deletions(-) diff --git a/libi2pd/ChaCha20.cpp b/libi2pd/ChaCha20.cpp index 0ee7cf75..30e02f42 100644 --- a/libi2pd/ChaCha20.cpp +++ b/libi2pd/ChaCha20.cpp @@ -1,12 +1,15 @@ -#include "ChaCha20.h" - -/** - This code is licensed under the MCGSI Public License - Copyright 2018 Jeff Becker - - Kovri go write your own code +/* +* Copyright (c) 2013-2018, The PurpleI2P Project +* +* This file is part of Purple i2pd project and licensed under BSD3 +* +* See full license text in LICENSE file at top of project tree +* +* Kovri go write your own code +* +*/ - */ +#include "ChaCha20.h" #if LEGACY_OPENSSL namespace i2p @@ -91,6 +94,12 @@ void Chacha20Init (Chacha20State& state, const uint8_t * nonce, const uint8_t * state.data[13 + i] = chacha::u8t32le(nonce + i * 4); } +void Chacha20SetCounter (Chacha20State& state, uint32_t counter) +{ + state.data[12] = counter; + state.offset = 0; +} + void Chacha20Encrypt (Chacha20State& state, uint8_t * buf, size_t sz) { if (state.offset > 0) @@ -121,13 +130,6 @@ void Chacha20Encrypt (Chacha20State& state, uint8_t * buf, size_t sz) } } // namespace chacha - - void chacha20(uint8_t * buf, size_t sz, const uint8_t * nonce, const uint8_t * key, uint32_t counter) - { - chacha::Chacha20State state; - chacha::Chacha20Init (state, nonce, key, counter); - chacha::Chacha20Encrypt (state, buf, sz); - } } } #endif diff --git a/libi2pd/ChaCha20.h b/libi2pd/ChaCha20.h index 0a2f3be0..a5a8aafc 100644 --- a/libi2pd/ChaCha20.h +++ b/libi2pd/ChaCha20.h @@ -1,10 +1,13 @@ -/** - This code is licensed under the MCGSI Public License - Copyright 2018 Jeff Becker - - Kovri go write your own code - - */ +/* +* Copyright (c) 2013-2018, The PurpleI2P Project +* +* This file is part of Purple i2pd project and licensed under BSD3 +* +* See full license text in LICENSE file at top of project tree +* +* Kovri go write your own code +* +*/ #ifndef LIBI2PD_CHACHA20_H #define LIBI2PD_CHACHA20_H #include @@ -59,12 +62,9 @@ namespace chacha }; void Chacha20Init (Chacha20State& state, const uint8_t * nonce, const uint8_t * key, uint32_t counter); - void Chacha20Encrypt (Chacha20State& state, uint8_t * buf, size_t sz); + void Chacha20SetCounter (Chacha20State& state, uint32_t counter); + void Chacha20Encrypt (Chacha20State& state, uint8_t * buf, size_t sz); // encrypt buf in place } - - /** encrypt buf in place with chacha20 */ - void chacha20(uint8_t * buf, size_t sz, const uint8_t * nonce, const uint8_t * key, uint32_t counter=1); - } } #endif diff --git a/libi2pd/Crypto.cpp b/libi2pd/Crypto.cpp index 659fddcc..28df1399 100644 --- a/libi2pd/Crypto.cpp +++ b/libi2pd/Crypto.cpp @@ -1087,11 +1087,12 @@ namespace crypto if (encrypt && len < msgLen + 16) return false; bool ret = true; #if LEGACY_OPENSSL + chacha::Chacha20State state; // generate one time poly key + chacha::Chacha20Init (state, nonce, key, 0); uint64_t polyKey[8]; memset(polyKey, 0, sizeof(polyKey)); - chacha20 ((uint8_t *)polyKey, 64, nonce, key, 0); - + chacha::Chacha20Encrypt (state, (uint8_t *)polyKey, 64); // create Poly1305 hash Poly1305 polyHash (polyKey); if (!ad) adLen = 0; @@ -1108,17 +1109,18 @@ namespace crypto } } // encrypt/decrypt data and add to hash + Chacha20SetCounter (state, 1); if (buf != msg) memcpy (buf, msg, msgLen); if (encrypt) { - chacha20 (buf, msgLen, nonce, key, 1); // encrypt + chacha::Chacha20Encrypt (state, buf, msgLen); // encrypt polyHash.Update (buf, msgLen); // after encryption } else { polyHash.Update (buf, msgLen); // before decryption - chacha20 (buf, msgLen, nonce, key, 1); // decrypt + chacha::Chacha20Encrypt (state, buf, msgLen); // decrypt } auto rem = msgLen & 0x0F; // %16 @@ -1176,14 +1178,15 @@ namespace crypto { if (bufs.empty ()) return; #if LEGACY_OPENSSL + chacha::Chacha20State state; // generate one time poly key + chacha::Chacha20Init (state, nonce, key, 0); uint64_t polyKey[8]; memset(polyKey, 0, sizeof(polyKey)); - chacha20 ((uint8_t *)polyKey, 64, nonce, key, 0); + chacha::Chacha20Encrypt (state, (uint8_t *)polyKey, 64); Poly1305 polyHash (polyKey); // encrypt buffers - chacha::Chacha20State state; - chacha::Chacha20Init (state, nonce, key, 1); + Chacha20SetCounter (state, 1); size_t size = 0; for (auto& it: bufs) {