diff options
author | moneromooo-monero <moneromooo-monero@users.noreply.github.com> | 2017-12-07 13:27:11 +0000 |
---|---|---|
committer | moneromooo-monero <moneromooo-monero@users.noreply.github.com> | 2017-12-25 19:28:03 +0000 |
commit | 1e5491e942d0a372ebebe9ef3d40941fc4a4fbb6 (patch) | |
tree | d13f404893c3994db28935da90947fbea1fc7cd5 /src/crypto | |
parent | Merge pull request #2936 (diff) | |
download | monero-1e5491e942d0a372ebebe9ef3d40941fc4a4fbb6.tar.xz |
Add a chacha20 variant to go with chacha8
Diffstat (limited to 'src/crypto')
-rw-r--r-- | src/crypto/CMakeLists.txt | 4 | ||||
-rw-r--r-- | src/crypto/chacha.c (renamed from src/crypto/chacha8.c) | 16 | ||||
-rw-r--r-- | src/crypto/chacha.h (renamed from src/crypto/chacha8.h) | 29 |
3 files changed, 32 insertions, 17 deletions
diff --git a/src/crypto/CMakeLists.txt b/src/crypto/CMakeLists.txt index 1e06a0dfd..fd71a87e7 100644 --- a/src/crypto/CMakeLists.txt +++ b/src/crypto/CMakeLists.txt @@ -29,7 +29,7 @@ set(crypto_sources aesb.c blake256.c - chacha8.c + chacha.c crypto-ops-data.c crypto-ops.c crypto.cpp @@ -51,7 +51,7 @@ set(crypto_headers) set(crypto_private_headers blake256.h - chacha8.h + chacha.h crypto-ops.h crypto.h generic-ops.h diff --git a/src/crypto/chacha8.c b/src/crypto/chacha.c index df135af59..f573083be 100644 --- a/src/crypto/chacha8.c +++ b/src/crypto/chacha.c @@ -8,7 +8,7 @@ Public domain. #include <stdio.h> #include <sys/param.h> -#include "chacha8.h" +#include "chacha.h" #include "common/int-util.h" #include "warnings.h" @@ -40,7 +40,7 @@ static const char sigma[] = "expand 32-byte k"; DISABLE_GCC_AND_CLANG_WARNING(strict-aliasing) -void chacha8(const void* data, size_t length, const uint8_t* key, const uint8_t* iv, char* cipher) { +static void chacha(unsigned rounds, const void* data, size_t length, const uint8_t* key, const uint8_t* iv, char* cipher) { uint32_t x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15; uint32_t j0, j1, j2, j3, j4, j5, j6, j7, j8, j9, j10, j11, j12, j13, j14, j15; char* ctarget = 0; @@ -89,7 +89,7 @@ void chacha8(const void* data, size_t length, const uint8_t* key, const uint8_t* x13 = j13; x14 = j14; x15 = j15; - for (i = 8;i > 0;i -= 2) { + for (i = rounds;i > 0;i -= 2) { QUARTERROUND( x0, x4, x8,x12) QUARTERROUND( x1, x5, x9,x13) QUARTERROUND( x2, x6,x10,x14) @@ -168,3 +168,13 @@ void chacha8(const void* data, size_t length, const uint8_t* key, const uint8_t* data = (uint8_t*)data + 64; } } + +void chacha8(const void* data, size_t length, const uint8_t* key, const uint8_t* iv, char* cipher) +{ + chacha(8, data, length, key, iv, cipher); +} + +void chacha20(const void* data, size_t length, const uint8_t* key, const uint8_t* iv, char* cipher) +{ + chacha(20, data, length, key, iv, cipher); +} diff --git a/src/crypto/chacha8.h b/src/crypto/chacha.h index dcbe6a933..a9665030d 100644 --- a/src/crypto/chacha8.h +++ b/src/crypto/chacha.h @@ -33,8 +33,8 @@ #include <stdint.h> #include <stddef.h> -#define CHACHA8_KEY_SIZE 32 -#define CHACHA8_IV_SIZE 8 +#define CHACHA_KEY_SIZE 32 +#define CHACHA_IV_SIZE 8 #if defined(__cplusplus) #include <memory.h> @@ -46,33 +46,38 @@ namespace crypto { extern "C" { #endif void chacha8(const void* data, size_t length, const uint8_t* key, const uint8_t* iv, char* cipher); + void chacha20(const void* data, size_t length, const uint8_t* key, const uint8_t* iv, char* cipher); #if defined(__cplusplus) } - using chacha8_key = tools::scrubbed_arr<uint8_t, CHACHA8_KEY_SIZE>; + using chacha_key = tools::scrubbed_arr<uint8_t, CHACHA_KEY_SIZE>; #pragma pack(push, 1) - // MS VC 2012 doesn't interpret `class chacha8_iv` as POD in spite of [9.0.10], so it is a struct - struct chacha8_iv { - uint8_t data[CHACHA8_IV_SIZE]; + // MS VC 2012 doesn't interpret `class chacha_iv` as POD in spite of [9.0.10], so it is a struct + struct chacha_iv { + uint8_t data[CHACHA_IV_SIZE]; }; #pragma pack(pop) - static_assert(sizeof(chacha8_key) == CHACHA8_KEY_SIZE && sizeof(chacha8_iv) == CHACHA8_IV_SIZE, "Invalid structure size"); + static_assert(sizeof(chacha_key) == CHACHA_KEY_SIZE && sizeof(chacha_iv) == CHACHA_IV_SIZE, "Invalid structure size"); - inline void chacha8(const void* data, std::size_t length, const chacha8_key& key, const chacha8_iv& iv, char* cipher) { + inline void chacha8(const void* data, std::size_t length, const chacha_key& key, const chacha_iv& iv, char* cipher) { chacha8(data, length, key.data(), reinterpret_cast<const uint8_t*>(&iv), cipher); } - inline void generate_chacha8_key(const void *data, size_t size, chacha8_key& key) { - static_assert(sizeof(chacha8_key) <= sizeof(hash), "Size of hash must be at least that of chacha8_key"); + inline void chacha20(const void* data, std::size_t length, const chacha_key& key, const chacha_iv& iv, char* cipher) { + chacha20(data, length, key.data(), reinterpret_cast<const uint8_t*>(&iv), cipher); + } + + inline void generate_chacha_key(const void *data, size_t size, chacha_key& key) { + static_assert(sizeof(chacha_key) <= sizeof(hash), "Size of hash must be at least that of chacha_key"); tools::scrubbed_arr<char, HASH_SIZE> pwd_hash; crypto::cn_slow_hash(data, size, pwd_hash.data()); memcpy(&key, pwd_hash.data(), sizeof(key)); } - inline void generate_chacha8_key(std::string password, chacha8_key& key) { - return generate_chacha8_key(password.data(), password.size(), key); + inline void generate_chacha_key(std::string password, chacha_key& key) { + return generate_chacha_key(password.data(), password.size(), key); } } |