mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-02-02 11:04:00 +01:00
don't inialize Chacha20 state twice
This commit is contained in:
parent
ef6db64e9f
commit
7692332f0e
|
@ -1,13 +1,16 @@
|
||||||
|
/*
|
||||||
|
* 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"
|
#include "ChaCha20.h"
|
||||||
|
|
||||||
/**
|
|
||||||
This code is licensed under the MCGSI Public License
|
|
||||||
Copyright 2018 Jeff Becker
|
|
||||||
|
|
||||||
Kovri go write your own code
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
#if LEGACY_OPENSSL
|
#if LEGACY_OPENSSL
|
||||||
namespace i2p
|
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);
|
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)
|
void Chacha20Encrypt (Chacha20State& state, uint8_t * buf, size_t sz)
|
||||||
{
|
{
|
||||||
if (state.offset > 0)
|
if (state.offset > 0)
|
||||||
|
@ -121,13 +130,6 @@ void Chacha20Encrypt (Chacha20State& state, uint8_t * buf, size_t sz)
|
||||||
}
|
}
|
||||||
} // namespace chacha
|
} // 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
|
#endif
|
||||||
|
|
|
@ -1,10 +1,13 @@
|
||||||
/**
|
/*
|
||||||
This code is licensed under the MCGSI Public License
|
* Copyright (c) 2013-2018, The PurpleI2P Project
|
||||||
Copyright 2018 Jeff Becker
|
*
|
||||||
|
* This file is part of Purple i2pd project and licensed under BSD3
|
||||||
Kovri go write your own code
|
*
|
||||||
|
* See full license text in LICENSE file at top of project tree
|
||||||
*/
|
*
|
||||||
|
* Kovri go write your own code
|
||||||
|
*
|
||||||
|
*/
|
||||||
#ifndef LIBI2PD_CHACHA20_H
|
#ifndef LIBI2PD_CHACHA20_H
|
||||||
#define LIBI2PD_CHACHA20_H
|
#define LIBI2PD_CHACHA20_H
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
@ -59,12 +62,9 @@ namespace chacha
|
||||||
};
|
};
|
||||||
|
|
||||||
void Chacha20Init (Chacha20State& state, const uint8_t * nonce, const uint8_t * key, uint32_t counter);
|
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
|
#endif
|
||||||
|
|
|
@ -1087,11 +1087,12 @@ namespace crypto
|
||||||
if (encrypt && len < msgLen + 16) return false;
|
if (encrypt && len < msgLen + 16) return false;
|
||||||
bool ret = true;
|
bool ret = true;
|
||||||
#if LEGACY_OPENSSL
|
#if LEGACY_OPENSSL
|
||||||
|
chacha::Chacha20State state;
|
||||||
// generate one time poly key
|
// generate one time poly key
|
||||||
|
chacha::Chacha20Init (state, nonce, key, 0);
|
||||||
uint64_t polyKey[8];
|
uint64_t polyKey[8];
|
||||||
memset(polyKey, 0, sizeof(polyKey));
|
memset(polyKey, 0, sizeof(polyKey));
|
||||||
chacha20 ((uint8_t *)polyKey, 64, nonce, key, 0);
|
chacha::Chacha20Encrypt (state, (uint8_t *)polyKey, 64);
|
||||||
|
|
||||||
// create Poly1305 hash
|
// create Poly1305 hash
|
||||||
Poly1305 polyHash (polyKey);
|
Poly1305 polyHash (polyKey);
|
||||||
if (!ad) adLen = 0;
|
if (!ad) adLen = 0;
|
||||||
|
@ -1108,17 +1109,18 @@ namespace crypto
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// encrypt/decrypt data and add to hash
|
// encrypt/decrypt data and add to hash
|
||||||
|
Chacha20SetCounter (state, 1);
|
||||||
if (buf != msg)
|
if (buf != msg)
|
||||||
memcpy (buf, msg, msgLen);
|
memcpy (buf, msg, msgLen);
|
||||||
if (encrypt)
|
if (encrypt)
|
||||||
{
|
{
|
||||||
chacha20 (buf, msgLen, nonce, key, 1); // encrypt
|
chacha::Chacha20Encrypt (state, buf, msgLen); // encrypt
|
||||||
polyHash.Update (buf, msgLen); // after encryption
|
polyHash.Update (buf, msgLen); // after encryption
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
polyHash.Update (buf, msgLen); // before decryption
|
polyHash.Update (buf, msgLen); // before decryption
|
||||||
chacha20 (buf, msgLen, nonce, key, 1); // decrypt
|
chacha::Chacha20Encrypt (state, buf, msgLen); // decrypt
|
||||||
}
|
}
|
||||||
|
|
||||||
auto rem = msgLen & 0x0F; // %16
|
auto rem = msgLen & 0x0F; // %16
|
||||||
|
@ -1176,14 +1178,15 @@ namespace crypto
|
||||||
{
|
{
|
||||||
if (bufs.empty ()) return;
|
if (bufs.empty ()) return;
|
||||||
#if LEGACY_OPENSSL
|
#if LEGACY_OPENSSL
|
||||||
|
chacha::Chacha20State state;
|
||||||
// generate one time poly key
|
// generate one time poly key
|
||||||
|
chacha::Chacha20Init (state, nonce, key, 0);
|
||||||
uint64_t polyKey[8];
|
uint64_t polyKey[8];
|
||||||
memset(polyKey, 0, sizeof(polyKey));
|
memset(polyKey, 0, sizeof(polyKey));
|
||||||
chacha20 ((uint8_t *)polyKey, 64, nonce, key, 0);
|
chacha::Chacha20Encrypt (state, (uint8_t *)polyKey, 64);
|
||||||
Poly1305 polyHash (polyKey);
|
Poly1305 polyHash (polyKey);
|
||||||
// encrypt buffers
|
// encrypt buffers
|
||||||
chacha::Chacha20State state;
|
Chacha20SetCounter (state, 1);
|
||||||
chacha::Chacha20Init (state, nonce, key, 1);
|
|
||||||
size_t size = 0;
|
size_t size = 0;
|
||||||
for (auto& it: bufs)
|
for (auto& it: bufs)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue