2018-12-01 02:31:06 +00:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
*/
|
2018-05-24 18:27:26 +00:00
|
|
|
#ifndef LIBI2PD_CHACHA20_H
|
|
|
|
#define LIBI2PD_CHACHA20_H
|
|
|
|
#include <cstdint>
|
|
|
|
#include <cstring>
|
2018-11-30 19:41:14 +00:00
|
|
|
#include <inttypes.h>
|
|
|
|
#include <string.h>
|
2018-11-22 17:13:16 +00:00
|
|
|
#include "Crypto.h"
|
2018-05-24 18:27:26 +00:00
|
|
|
|
2018-11-22 17:13:16 +00:00
|
|
|
#if LEGACY_OPENSSL
|
2018-05-24 18:27:26 +00:00
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace crypto
|
|
|
|
{
|
2018-11-30 21:21:11 +00:00
|
|
|
const std::size_t CHACHA20_KEY_BYTES = 32;
|
|
|
|
const std::size_t CHACHA20_NOUNCE_BYTES = 12;
|
|
|
|
|
|
|
|
namespace chacha
|
|
|
|
{
|
|
|
|
constexpr std::size_t blocksize = 64;
|
|
|
|
constexpr int rounds = 20;
|
|
|
|
|
|
|
|
struct Chacha20State;
|
|
|
|
struct Chacha20Block
|
|
|
|
{
|
|
|
|
Chacha20Block () {};
|
|
|
|
Chacha20Block (Chacha20Block &&) = delete;
|
|
|
|
|
|
|
|
uint8_t data[blocksize];
|
|
|
|
|
|
|
|
void operator << (const Chacha20State & st);
|
|
|
|
};
|
2018-05-24 18:27:26 +00:00
|
|
|
|
2018-11-30 19:41:14 +00:00
|
|
|
struct Chacha20State
|
|
|
|
{
|
2018-11-30 21:21:11 +00:00
|
|
|
Chacha20State (): offset (0) {};
|
2018-11-30 19:41:14 +00:00
|
|
|
Chacha20State (Chacha20State &&) = delete;
|
|
|
|
|
|
|
|
Chacha20State & operator += (const Chacha20State & other)
|
|
|
|
{
|
|
|
|
for(int i = 0; i < 16; i++)
|
|
|
|
data[i] += other.data[i];
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Copy(const Chacha20State & other)
|
|
|
|
{
|
|
|
|
memcpy(data, other.data, sizeof(uint32_t) * 16);
|
|
|
|
}
|
|
|
|
uint32_t data[16];
|
2018-11-30 21:21:11 +00:00
|
|
|
Chacha20Block block;
|
|
|
|
size_t offset;
|
|
|
|
};
|
2018-11-30 19:41:14 +00:00
|
|
|
|
|
|
|
void Chacha20Init (Chacha20State& state, const uint8_t * nonce, const uint8_t * key, uint32_t counter);
|
2018-12-01 02:31:06 +00:00
|
|
|
void Chacha20SetCounter (Chacha20State& state, uint32_t counter);
|
|
|
|
void Chacha20Encrypt (Chacha20State& state, uint8_t * buf, size_t sz); // encrypt buf in place
|
2018-11-30 21:21:11 +00:00
|
|
|
}
|
2018-11-30 19:41:14 +00:00
|
|
|
}
|
2018-05-24 18:27:26 +00:00
|
|
|
}
|
2018-11-22 17:13:16 +00:00
|
|
|
#endif
|
2018-05-24 18:27:26 +00:00
|
|
|
|
|
|
|
#endif
|