| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include <stdint.h> |
| 34 |
#include <stddef.h> |
| 35 |
|
| 36 |
#define CHACHA_KEY_SIZE 32 |
| 37 |
#define CHACHA_IV_SIZE 8 |
| 38 |
|
| 39 |
#if defined(__cplusplus) |
| 40 |
#include <memory.h> |
| 41 |
|
| 42 |
#include "memwipe.h" |
| 43 |
#include "mlocker.h" |
| 44 |
#include "hash.h" |
| 45 |
|
| 46 |
namespace crypto { |
| 47 |
extern "C" { |
| 48 |
#endif |
| 49 |
void chacha8(const void* data, size_t length, const uint8_t* key, const uint8_t* iv, char* cipher); |
| 50 |
void chacha20(const void* data, size_t length, const uint8_t* key, const uint8_t* iv, char* cipher); |
| 51 |
#if defined(__cplusplus) |
| 52 |
} |
| 53 |
|
| 54 |
using chacha_key = epee::mlocked<tools::scrubbed_arr<uint8_t, CHACHA_KEY_SIZE>>; |
| 55 |
|
| 56 |
#pragma pack(push, 1) |
| 57 |
|
| 58 |
struct chacha_iv { |
| 59 |
uint8_t data[CHACHA_IV_SIZE]; |
| 60 |
}; |
| 61 |
#pragma pack(pop) |
| 62 |
|
| 63 |
static_assert(sizeof(chacha_key) == CHACHA_KEY_SIZE && sizeof(chacha_iv) == CHACHA_IV_SIZE, "Invalid structure size"); |
| 64 |
|
| 65 |
inline void chacha8(const void* data, std::size_t length, const chacha_key& key, const chacha_iv& iv, char* cipher) { |
| 66 |
chacha8(data, length, key.data(), reinterpret_cast<const uint8_t*>(&iv), cipher); |
| 67 |
} |
| 68 |
|
| 69 |
inline void chacha20(const void* data, std::size_t length, const chacha_key& key, const chacha_iv& iv, char* cipher) { |
| 70 |
chacha20(data, length, key.data(), reinterpret_cast<const uint8_t*>(&iv), cipher); |
| 71 |
} |
| 72 |
|
| 73 |
inline void generate_chacha_key(const void *data, size_t size, chacha_key& key, uint64_t kdf_rounds) { |
| 74 |
static_assert(sizeof(chacha_key) <= sizeof(hash), "Size of hash must be at least that of chacha_key"); |
| 75 |
epee::mlocked<tools::scrubbed_arr<char, HASH_SIZE>> pwd_hash; |
| 76 |
crypto::cn_slow_hash(data, size, pwd_hash.data(), 0, 0, 0); |
| 77 |
for (uint64_t n = 1; n < kdf_rounds; ++n) |
| 78 |
crypto::cn_slow_hash(pwd_hash.data(), pwd_hash.size(), pwd_hash.data(), 0, 0, 0); |
| 79 |
memcpy(&unwrap(unwrap(key)), pwd_hash.data(), sizeof(key)); |
| 80 |
} |
| 81 |
|
| 82 |
inline void generate_chacha_key_prehashed(const void *data, size_t size, chacha_key& key, uint64_t kdf_rounds) { |
| 83 |
static_assert(sizeof(chacha_key) <= sizeof(hash), "Size of hash must be at least that of chacha_key"); |
| 84 |
epee::mlocked<tools::scrubbed_arr<char, HASH_SIZE>> pwd_hash; |
| 85 |
crypto::cn_slow_hash(data, size, pwd_hash.data(), 0, 1, 0); |
| 86 |
for (uint64_t n = 1; n < kdf_rounds; ++n) |
| 87 |
crypto::cn_slow_hash(pwd_hash.data(), pwd_hash.size(), pwd_hash.data(), 0, 0, 0); |
| 88 |
memcpy(&unwrap(unwrap(key)), pwd_hash.data(), sizeof(key)); |
| 89 |
} |
| 90 |
|
| 91 |
inline void generate_chacha_key(std::string password, chacha_key& key, uint64_t kdf_rounds) { |
| 92 |
return generate_chacha_key(password.data(), password.size(), key, kdf_rounds); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
#endif |