Browse Source

don't inialize Chacha20 state twice

pull/1274/head
orignal 6 years ago
parent
commit
7692332f0e
  1. 32
      libi2pd/ChaCha20.cpp
  2. 22
      libi2pd/ChaCha20.h
  3. 17
      libi2pd/Crypto.cpp

32
libi2pd/ChaCha20.cpp

@ -1,13 +1,16 @@ @@ -1,13 +1,16 @@
#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 * @@ -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) @@ -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

22
libi2pd/ChaCha20.h

@ -1,9 +1,12 @@ @@ -1,9 +1,12 @@
/**
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
@ -59,12 +62,9 @@ namespace chacha @@ -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

17
libi2pd/Crypto.cpp

@ -1087,11 +1087,12 @@ namespace crypto @@ -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 @@ -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 @@ -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)
{

Loading…
Cancel
Save