-
Notifications
You must be signed in to change notification settings - Fork 34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Safe memory storage #1997
Safe memory storage #1997
Conversation
/** | ||
* Copyright Quadrivium LLC | ||
* All Rights Reserved | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include "crypto/common.hpp" | ||
|
||
namespace kagome::crypto {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove empty .cpp file
|
||
namespace kagome::crypto::bip39 { | ||
namespace constants { | ||
constexpr size_t BIP39_SEED_LEN_512 = 64u; | ||
} // namespace constants | ||
|
||
using Bip39Seed = common::Buffer; | ||
struct Bip39Tag; | ||
using Bip39Seed = PrivateKey<constants::BIP39_SEED_LEN_512, Bip39Tag>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Revert, Bip39Seed
is variable size.
https://github.com/paritytech/polkadot-sdk/blob/8f8297e9debe79139e3a41be8b54a5fcd603f783/substrate/primitives/core/src/crypto.rs#L966-L991
from_string_with_seed() { if let Some(hex) { Self::Seed::fromHex(hex) } }
@@ -71,7 +70,7 @@ namespace kagome::crypto { | |||
* Create a seed from its bytes | |||
*/ | |||
virtual outcome::result<Seed> toSeed( | |||
common::BufferView bytes) const noexcept = 0; | |||
SecureCleanGuard<uint8_t> bytes) const noexcept = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Revert to (span)
(or )?(const SecureCleanGuard &)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why? I want to make the user to clean up the memory used to iniitalize the seed.
template <std::output_iterator<uint8_t> Iter> | ||
outcome::result<void> unhex_to(std::string_view hex, Iter out) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
out
is vector
or array
or span
template <std::output_iterator<uint8_t> Iter> | |
outcome::result<void> unhex_to(std::string_view hex, Iter out) { | |
template <typename T> | |
outcome::result<void> unhex_to(std::string_view hex, T &out) { | |
if constexpr (requires { out.resize(); }) { | |
out.resize(hex.size() / 2); | |
} else if (hex.size() != out.size() * 2) { | |
return ERROR; | |
} |
.secret_key = crypto::Ed25519PrivateKey::from( | ||
crypto::SecureCleanGuard(private_key_bytes)), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.secret_key = crypto::Ed25519PrivateKey::from( | |
crypto::SecureCleanGuard(private_key_bytes)), | |
.secret_key = fromSpan<crypto::Ed25519PrivateKey>(libp2p_key.privateKey.data).value(), |
template <size_t Offset, size_t Length> | ||
BufferView view() const { | ||
return std::span(*this).template subspan<Offset, Length>(); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Don't add
Buffer
member function. - Use
std::span{}.subspan<_, _>()
directly. - Must check size.
- May make free function for any
span
.
template <size_t Offset, size_t Length> | |
BufferView view() const { | |
return std::span(*this).template subspan<Offset, Length>(); | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But why?
core/crypto/bip39/mnemonic.cpp
Outdated
if (seed.size() != HEX_SEED_STR_LENGTH) { | ||
return bip39::MnemonicError::INVALID_SEED_LENGTH; | ||
} | ||
SecureBuffer<> bytes(HEX_SEED_BIT_LENGTH, 0); | ||
OUTCOME_TRY(common::unhexWith0x(seed, bytes.begin())); | ||
OUTCOME_TRY(seed, Bip39Seed::from(std::move(bytes))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If Mnemonic.seed
is Bip39Seed
, and Bip39Seed
is not vector
but byte[64]
if (seed.size() != HEX_SEED_STR_LENGTH) { | |
return bip39::MnemonicError::INVALID_SEED_LENGTH; | |
} | |
SecureBuffer<> bytes(HEX_SEED_BIT_LENGTH, 0); | |
OUTCOME_TRY(common::unhexWith0x(seed, bytes.begin())); | |
OUTCOME_TRY(seed, Bip39Seed::from(std::move(bytes))); | |
Bip39Seed seed; | |
// ed25519/sr25519/ecdsa/bandersnatch seeds are 32 bytes | |
OUTCOME_TRY(common::unhexWith0x(seed, std::span{seed}.first(32)); | |
mnemonic.seed = std::move(seed); |
@@ -29,12 +30,12 @@ namespace kagome::crypto::bip39 { | |||
struct Mnemonic { | |||
using Words = std::vector<std::string>; | |||
|
|||
boost::variant<common::Buffer, Words> seed; | |||
std::variant<Words, Bip39Seed> seed; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not Bip39Seed
(byte[64]
).
May rename to entropy
.
Must be variant<Words, vector>
or variant<Words, byte[32]>
(because current crypto types use 32 bytes seed).
Referenced issues
resolves https://github.com/qdrvm/KAGOME-audit/issues/29
Description of the Change
Storage for private keys and seeds is allocated in secure OpenSSL heap.
Benefits
Possible Drawbacks
Usage Examples or Tests
Alternate Designs