i2pd/libi2pd/ChaCha20.cpp

138 lines
2.8 KiB
C++
Raw Normal View History

2018-12-01 02:31:06 +00:00
/*
* Copyright (c) 2013-2020, The PurpleI2P Project
2018-12-01 02:31:06 +00:00
*
* 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
*
*/
2018-05-24 18:27:26 +00:00
2019-03-29 22:45:31 +00:00
#include "I2PEndian.h"
2018-12-01 02:31:06 +00:00
#include "ChaCha20.h"
#if !OPENSSL_AEAD_CHACHA20_POLY1305
2018-05-24 18:27:26 +00:00
namespace i2p
{
namespace crypto
{
namespace chacha
{
void u32t8le(uint32_t v, uint8_t * p)
2018-05-24 18:27:26 +00:00
{
p[0] = v & 0xff;
p[1] = (v >> 8) & 0xff;
p[2] = (v >> 16) & 0xff;
p[3] = (v >> 24) & 0xff;
2018-05-24 18:27:26 +00:00
}
uint32_t u8t32le(const uint8_t * p)
2018-05-24 18:27:26 +00:00
{
uint32_t value = p[3];
2018-05-24 18:27:26 +00:00
value = (value << 8) | p[2];
value = (value << 8) | p[1];
value = (value << 8) | p[0];
2018-05-24 18:27:26 +00:00
return value;
2018-05-24 18:27:26 +00:00
}
uint32_t rotl32(uint32_t x, int n)
2018-05-24 18:27:26 +00:00
{
return x << n | (x >> (-n & 31));
2018-05-24 18:27:26 +00:00
}
void quarterround(uint32_t *x, int a, int b, int c, int d)
2018-05-24 18:27:26 +00:00
{
x[a] += x[b]; x[d] = rotl32(x[d] ^ x[a], 16);
x[c] += x[d]; x[b] = rotl32(x[b] ^ x[c], 12);
x[a] += x[b]; x[d] = rotl32(x[d] ^ x[a], 8);
x[c] += x[d]; x[b] = rotl32(x[b] ^ x[c], 7);
2018-05-24 18:27:26 +00:00
}
2018-11-30 21:21:11 +00:00
void Chacha20Block::operator << (const Chacha20State & st)
{
2018-11-30 21:21:11 +00:00
int i;
for (i = 0; i < 16; i++)
2018-11-30 21:21:11 +00:00
u32t8le(st.data[i], data + (i << 2));
}
2018-05-24 18:27:26 +00:00
2018-11-30 21:21:11 +00:00
void block (Chacha20State &input, int rounds)
2018-05-24 18:27:26 +00:00
{
int i;
Chacha20State x;
x.Copy(input);
2018-05-24 18:27:26 +00:00
for (i = rounds; i > 0; i -= 2)
{
quarterround(x.data, 0, 4, 8, 12);
quarterround(x.data, 1, 5, 9, 13);
quarterround(x.data, 2, 6, 10, 14);
quarterround(x.data, 3, 7, 11, 15);
quarterround(x.data, 0, 5, 10, 15);
quarterround(x.data, 1, 6, 11, 12);
quarterround(x.data, 2, 7, 8, 13);
quarterround(x.data, 3, 4, 9, 14);
}
x += input;
input.block << x;
2018-05-24 18:27:26 +00:00
}
2018-11-30 21:21:11 +00:00
void Chacha20Init (Chacha20State& state, const uint8_t * nonce, const uint8_t * key, uint32_t counter)
{
state.data[0] = 0x61707865;
state.data[1] = 0x3320646e;
state.data[2] = 0x79622d32;
state.data[3] = 0x6b206574;
for (size_t i = 0; i < 8; i++)
state.data[4 + i] = chacha::u8t32le(key + i * 4);
2019-03-29 22:45:31 +00:00
state.data[12] = htole32 (counter);
for (size_t i = 0; i < 3; i++)
state.data[13 + i] = chacha::u8t32le(nonce + i * 4);
2018-11-30 21:21:11 +00:00
}
2018-12-01 02:31:06 +00:00
void Chacha20SetCounter (Chacha20State& state, uint32_t counter)
{
2019-03-29 22:45:31 +00:00
state.data[12] = htole32 (counter);
2018-12-01 02:31:06 +00:00
state.offset = 0;
}
2018-12-01 02:31:06 +00:00
2018-11-30 21:21:11 +00:00
void Chacha20Encrypt (Chacha20State& state, uint8_t * buf, size_t sz)
{
2018-11-30 21:21:11 +00:00
if (state.offset > 0)
{
// previous block if any
auto s = chacha::blocksize - state.offset;
2018-11-30 21:21:11 +00:00
if (sz < s) s = sz;
for (size_t i = 0; i < s; i++)
buf[i] ^= state.block.data[state.offset + i];
buf += s;
sz -= s;
state.offset += s;
if (state.offset >= chacha::blocksize) state.offset = 0;
}
for (size_t i = 0; i < sz; i += chacha::blocksize)
{
chacha::block(state, chacha::rounds);
state.data[12]++;
for (size_t j = i; j < i + chacha::blocksize; j++)
{
if (j >= sz)
2018-11-30 21:21:11 +00:00
{
state.offset = j & 0x3F; // % 64
break;
}
buf[j] ^= state.block.data[j - i];
}
}
2018-11-30 21:21:11 +00:00
}
2018-11-30 21:21:11 +00:00
} // namespace chacha
} // namespace crypto
} // namespace i2p
2018-11-30 21:21:11 +00:00
#endif