From 89bc6418b1f16880af38cbd2f27759b972138169 Mon Sep 17 00:00:00 2001 From: SChernykh Date: Wed, 16 Jun 2021 00:10:34 +0200 Subject: [PATCH] Secret key derivation --- src/base/tools/cryptonote/Signatures.cpp | 51 +++++++++++++++++++++++- src/base/tools/cryptonote/Signatures.h | 3 ++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/src/base/tools/cryptonote/Signatures.cpp b/src/base/tools/cryptonote/Signatures.cpp index ced225616..8c44fe699 100644 --- a/src/base/tools/cryptonote/Signatures.cpp +++ b/src/base/tools/cryptonote/Signatures.cpp @@ -77,6 +77,27 @@ static void hash_to_scalar(const void* data, size_t length, ec_scalar& res) } +static void derivation_to_scalar(const uint8_t* derivation, size_t output_index, ec_scalar& res) +{ + struct { + uint8_t derivation[32]; + uint8_t output_index[(sizeof(size_t) * 8 + 6) / 7]; + } buf; + + uint8_t* end = buf.output_index; + memcpy(buf.derivation, derivation, sizeof(buf.derivation)); + + size_t k = output_index; + while (k >= 0x80) { + *(end++) = (static_cast(k) & 0x7F) | 0x80; + k >>= 7; + } + *(end++) = static_cast(k); + + hash_to_scalar(&buf, end - reinterpret_cast(&buf), res); +} + + namespace xmrig { @@ -130,8 +151,9 @@ bool check_signature(const uint8_t* prefix_hash, const uint8_t* pub, const uint8 ge_tobytes((uint8_t*)&buf.comm, &tmp2); static const ec_point infinity = { { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }; - if (memcmp(&buf.comm, &infinity, 32) == 0) + if (memcmp(&buf.comm, &infinity, 32) == 0) { return false; + } hash_to_scalar(&buf, sizeof(s_comm), c); sc_sub((uint8_t*)&c, (uint8_t*)&c, (const uint8_t*)&sig.c); @@ -140,5 +162,32 @@ bool check_signature(const uint8_t* prefix_hash, const uint8_t* pub, const uint8 } +bool generate_key_derivation(const uint8_t* key1, const uint8_t* key2, uint8_t* derivation) +{ + ge_p3 point; + ge_p2 point2; + ge_p1p1 point3; + + if (ge_frombytes_vartime(&point, key1) != 0) { + return false; + } + + ge_scalarmult(&point2, key2, &point); + ge_mul8(&point3, &point2); + ge_p1p1_to_p2(&point2, &point3); + ge_tobytes(derivation, &point2); + + return true; +} + + +void derive_secret_key(const uint8_t* derivation, size_t output_index, const uint8_t* base, uint8_t* derived_key) +{ + ec_scalar scalar; + + derivation_to_scalar(derivation, output_index, scalar); + sc_add(derived_key, base, (uint8_t*) &scalar); +} + } /* namespace xmrig */ diff --git a/src/base/tools/cryptonote/Signatures.h b/src/base/tools/cryptonote/Signatures.h index a9db90462..7bc9755f6 100644 --- a/src/base/tools/cryptonote/Signatures.h +++ b/src/base/tools/cryptonote/Signatures.h @@ -31,6 +31,9 @@ namespace xmrig { void generate_signature(const uint8_t* prefix_hash, const uint8_t* pub, const uint8_t* sec, uint8_t* sig); bool check_signature(const uint8_t* prefix_hash, const uint8_t* pub, const uint8_t* sig); +bool generate_key_derivation(const uint8_t* key1, const uint8_t* key2, uint8_t* derivation); +void derive_secret_key(const uint8_t* derivation, size_t output_index, const uint8_t* base, uint8_t* derived_key); + } /* namespace xmrig */