Skip to content

Commit

Permalink
fix review
Browse files Browse the repository at this point in the history
  • Loading branch information
supermassive committed Jan 21, 2025
1 parent 7b251b0 commit bc49e74
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,12 @@
#pragma allow_unsafe_buffers
#endif

// https://datatracker.ietf.org/doc/html/rfc8032#section-5.1.5
// requires scalar to follow this requirements 'The lowest 3 bits of the first
// octet are cleared, the highest bit of the last octet is cleared, and the
// second highest bit of the last octet is set'.

int ED25519_is_scalar_pruned(const uint8_t scalar[32]) {
return (scalar[0] & 0b00000111) == 0b00000000 &&
(scalar[31] & 0b11000000) == 0b01000000;
}

// Produces pubkey form scalar.
// Function fails if `scalar` is not pruned. https://www.rfc-editor.org/rfc/rfc8032.html#section-5.1.5
// See `ED25519_keypair_from_seed` as origin.
int ED25519_pubkey_from_scalar(uint8_t out_public_key[32],
const uint8_t scalar[32]) {
if (!ED25519_is_scalar_pruned(scalar)) {
Expand All @@ -39,9 +33,6 @@ int ED25519_pubkey_from_scalar(uint8_t out_public_key[32],
return 1;
}

// Same as `ED25519_sign` but without hashing private key. `scalar` and `prefix`
// come from ED25519_BIP32 algorithm.
// Function fails if `scalar` is not pruned. https://www.rfc-editor.org/rfc/rfc8032.html#section-5.1.5
int ED25519_sign_with_scalar_and_prefix(uint8_t out_sig[64],
const uint8_t* message,
size_t message_len,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,23 @@
extern "C" {
#endif

// These functions support custom ED25519 keys generation and signing for
// Cardano.
// https://datatracker.ietf.org/doc/html/rfc8032#section-5.1.5
// requires scalar to follow this requirements 'The lowest 3 bits of the first
// octet are cleared, the highest bit of the last octet is cleared, and the
// second highest bit of the last octet is set'.
OPENSSL_EXPORT int ED25519_is_scalar_pruned(const uint8_t scalar[32]);

// Produces pubkey form scalar.
// Function fails if `scalar` is not pruned.
// https://www.rfc-editor.org/rfc/rfc8032.html#section-5.1.5 See
// `ED25519_keypair_from_seed` as origin.
OPENSSL_EXPORT int ED25519_pubkey_from_scalar(uint8_t out_public_key[32],
const uint8_t scalar[32]);

// Same as `ED25519_sign` but without hashing private key. `scalar` and `prefix`
// come from ED25519_BIP32 algorithm.
// Function fails if `scalar` is not pruned.
// https://www.rfc-editor.org/rfc/rfc8032.html#section-5.1.5
OPENSSL_EXPORT int ED25519_sign_with_scalar_and_prefix(
uint8_t out_sig[64],
const uint8_t* message,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
#include "brave/components/brave_wallet/browser/internal/hd_key_ed25519_slip23.h"

#include <array>
#include <memory>
#include <string>
#include <utility>
#include <vector>

#include "base/check.h"
#include "base/containers/span.h"
#include "base/containers/span_writer.h"
#include "base/memory/ptr_util.h"
#include "base/strings/string_number_conversions.h"
#include "crypto/hmac.h"
#include "third_party/boringssl/src/include/openssl/curve25519.h"
Expand Down Expand Up @@ -105,6 +105,7 @@ HDKeyEd25519Slip23::~HDKeyEd25519Slip23() = default;

// Child key derivation constructor.
HDKeyEd25519Slip23::HDKeyEd25519Slip23(
PassKey,
base::span<const uint8_t, kSlip23ScalarSize> scalar,
base::span<const uint8_t, kSlip23PrefixSize> prefix,
base::span<const uint8_t, kSlip23ChainCodeSize> chain_code,
Expand Down Expand Up @@ -160,11 +161,11 @@ std::unique_ptr<HDKeyEd25519Slip23> HDKeyEd25519Slip23::DeriveChild(
return nullptr;
}

return base::WrapUnique(new HDKeyEd25519Slip23(
derived_scalar,
return std::make_unique<HDKeyEd25519Slip23>(
PassKey(), derived_scalar,
CalculateDerivedPrefix(prefix_,
base::span(z_hmac).last<kSlip23PrefixSize>()),
CalculateDerivedChainCode(cc_hmac), *pubkey));
CalculateDerivedChainCode(cc_hmac), *pubkey);
}

// static
Expand All @@ -190,9 +191,10 @@ HDKeyEd25519Slip23::GenerateMasterKeyFromBip39Entropy(
return nullptr;
}

return base::WrapUnique(new HDKeyEd25519Slip23(
scalar, xprv_span.subspan<kSlip23ScalarSize, kSlip23PrefixSize>(),
xprv_span.last<kSlip23ChainCodeSize>(), *pubkey));
return std::make_unique<HDKeyEd25519Slip23>(
PassKey(), scalar,
xprv_span.subspan<kSlip23ScalarSize, kSlip23PrefixSize>(),
xprv_span.last<kSlip23ChainCodeSize>(), *pubkey);
}

std::optional<std::array<uint8_t, kEd25519SignatureSize>>
Expand Down
15 changes: 9 additions & 6 deletions components/brave_wallet/browser/internal/hd_key_ed25519_slip23.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "base/containers/span.h"
#include "base/gtest_prod_util.h"
#include "base/types/pass_key.h"
#include "brave/components/brave_wallet/browser/internal/hd_key_common.h"

namespace brave_wallet {
Expand All @@ -24,7 +25,15 @@ inline constexpr size_t kSlip23ChainCodeSize = 32;
// https://github.com/satoshilabs/slips/blob/master/slip-0023.md
class HDKeyEd25519Slip23 {
public:
using PassKey = base::PassKey<HDKeyEd25519Slip23>;

HDKeyEd25519Slip23();
HDKeyEd25519Slip23(
PassKey,
base::span<const uint8_t, kSlip23ScalarSize> scalar,
base::span<const uint8_t, kSlip23PrefixSize> prefix,
base::span<const uint8_t, kSlip23ChainCodeSize> chain_code,
base::span<const uint8_t, kEd25519PublicKeySize> public_key);
~HDKeyEd25519Slip23();
HDKeyEd25519Slip23(const HDKeyEd25519Slip23&) = delete;
HDKeyEd25519Slip23& operator=(const HDKeyEd25519Slip23&) = delete;
Expand All @@ -49,12 +58,6 @@ class HDKeyEd25519Slip23 {
private:
FRIEND_TEST_ALL_PREFIXES(HDKeyEd25519Slip23UnitTest, TestVectorZ);

HDKeyEd25519Slip23(
base::span<const uint8_t, kSlip23ScalarSize> scalar,
base::span<const uint8_t, kSlip23PrefixSize> prefix,
base::span<const uint8_t, kSlip23ChainCodeSize> chain_code,
base::span<const uint8_t, kEd25519PublicKeySize> public_key);

static std::unique_ptr<HDKeyEd25519Slip23> FromBip32Entropy(
base::span<const uint8_t> seed,
std::string_view hd_path);
Expand Down

0 comments on commit bc49e74

Please sign in to comment.