pub struct PrivateKeyBase(/* private fields */);Expand description
A secure foundation for deriving multiple cryptographic keys.
PrivateKeyBase serves as a root of cryptographic material from which various
types of keys can be deterministically derived. It securely manages the underlying
key material and provides methods to derive specific cryptographic keys for different
purposes.
It supports:
- Deterministic derivation of signing keys (Schnorr, ECDSA, Ed25519)
- Deterministic derivation of encryption keys (X25519)
- SSH key generation for various algorithms (Ed25519, ECDSA, DSA, RSA)
- Key pair generation for both signing and encryption
This design allows a single master seed to generate multiple secure keys for different cryptographic operations, similar to the concept of an HD wallet in cryptocurrency systems.
§Security
PrivateKeyBase implements ZeroizeOnDrop to securely erase the sensitive key
material from memory when the object is dropped, reducing the risk of key
extraction via memory attacks.
§Examples
Creating and using a PrivateKeyBase:
use bc_components::PrivateKeyBase;
use bc_components::Signer;
use bc_components::{PrivateKeysProvider, PublicKeysProvider};
// Create a new random PrivateKeyBase
let key_base = PrivateKeyBase::new();
// Sign a message using the derived Schnorr key
let message = b"Hello, world!";
let signature = key_base.sign(message).unwrap();
// Generate a key pair for public/private key operations
let (private_keys, public_keys) = (key_base.private_keys(), key_base.public_keys());Implementations§
Source§impl PrivateKeyBase
impl PrivateKeyBase
Sourcepub fn new() -> PrivateKeyBase
pub fn new() -> PrivateKeyBase
Generate a new random PrivateKeyBase.
Sourcepub fn from_data(data: impl Into<Vec<u8>>) -> PrivateKeyBase
pub fn from_data(data: impl Into<Vec<u8>>) -> PrivateKeyBase
Restores a PrivateKeyBase from bytes.
Sourcepub fn from_optional_data(data: Option<impl Into<Vec<u8>>>) -> PrivateKeyBase
pub fn from_optional_data(data: Option<impl Into<Vec<u8>>>) -> PrivateKeyBase
Restores a PrivateKeyBase from an optional reference to an array of bytes.
If the data is None, a new random PrivateKeyBase is generated.
Sourcepub fn new_using(rng: &mut impl RandomNumberGenerator) -> PrivateKeyBase
pub fn new_using(rng: &mut impl RandomNumberGenerator) -> PrivateKeyBase
Generate a new random PrivateKeyBase using the given random number generator.
Sourcepub fn new_with_provider(
provider: impl PrivateKeyDataProvider,
) -> PrivateKeyBase
pub fn new_with_provider( provider: impl PrivateKeyDataProvider, ) -> PrivateKeyBase
Create a new PrivateKeyBase from the given private keys data provider.
Sourcepub fn ecdsa_signing_private_key(&self) -> SigningPrivateKey
pub fn ecdsa_signing_private_key(&self) -> SigningPrivateKey
Derive a new ECDSA SigningPrivateKey from this PrivateKeyBase.
Sourcepub fn schnorr_signing_private_key(&self) -> SigningPrivateKey
pub fn schnorr_signing_private_key(&self) -> SigningPrivateKey
Derive a new Schnorr SigningPrivateKey from this PrivateKeyBase.
Sourcepub fn ed25519_signing_private_key(&self) -> SigningPrivateKey
pub fn ed25519_signing_private_key(&self) -> SigningPrivateKey
Derive a new Ed25519 SigningPrivateKey from this PrivateKeyBase.
Sourcepub fn ssh_signing_private_key(
&self,
algorithm: Algorithm,
comment: impl Into<String>,
) -> Result<SigningPrivateKey, Error>
pub fn ssh_signing_private_key( &self, algorithm: Algorithm, comment: impl Into<String>, ) -> Result<SigningPrivateKey, Error>
Derive a new SSH SigningPrivateKey from this PrivateKeyBase.
Sourcepub fn x25519_private_key(&self) -> X25519PrivateKey
pub fn x25519_private_key(&self) -> X25519PrivateKey
Derive a new X25519PrivateKey from this PrivateKeyBase.
An X25519 key for public key encryption.
Sourcepub fn schnorr_private_keys(&self) -> PrivateKeys
pub fn schnorr_private_keys(&self) -> PrivateKeys
Derive a new PrivateKeys from this PrivateKeyBase.
- Includes a Schnorr private key for signing.
- Includes an X25519 private key for encryption.
Sourcepub fn schnorr_public_keys(&self) -> PublicKeys
pub fn schnorr_public_keys(&self) -> PublicKeys
Derive a new PublicKeys from this PrivateKeyBase.
- Includes a Schnorr public key for signing.
- Includes an X25519 public key encryption.
Sourcepub fn ecdsa_private_keys(&self) -> PrivateKeys
pub fn ecdsa_private_keys(&self) -> PrivateKeys
Derive a new PrivateKeys from this PrivateKeyBase.
- Includes an ECDSA private key for signing.
- Includes an X25519 private key for encryption.
Sourcepub fn ecdsa_public_keys(&self) -> PublicKeys
pub fn ecdsa_public_keys(&self) -> PublicKeys
Derive a new PublicKeys from this PrivateKeyBase.
- Includes an ECDSA public key for signing.
- Includes an X25519 public key for encryption.
Sourcepub fn ssh_private_keys(
&self,
algorithm: Algorithm,
comment: impl Into<String>,
) -> Result<PrivateKeys, Error>
pub fn ssh_private_keys( &self, algorithm: Algorithm, comment: impl Into<String>, ) -> Result<PrivateKeys, Error>
Derive a new PrivateKeys from this PrivateKeyBase.
- Includes an SSH private key for signing.
- Includes an X25519 private key for encryption.
Sourcepub fn ssh_public_keys(
&self,
algorithm: Algorithm,
comment: impl Into<String>,
) -> Result<PublicKeys, Error>
pub fn ssh_public_keys( &self, algorithm: Algorithm, comment: impl Into<String>, ) -> Result<PublicKeys, Error>
Derive a new PublicKeys from this PrivateKeyBase.
- Includes an SSH public key for signing.
- Includes an X25519 public key for encryption.
Trait Implementations§
Source§impl AsRef<[u8]> for PrivateKeyBase
impl AsRef<[u8]> for PrivateKeyBase
Source§impl AsRef<PrivateKeyBase> for PrivateKeyBase
impl AsRef<PrivateKeyBase> for PrivateKeyBase
Source§fn as_ref(&self) -> &PrivateKeyBase
fn as_ref(&self) -> &PrivateKeyBase
Source§impl CBORTagged for PrivateKeyBase
impl CBORTagged for PrivateKeyBase
Source§impl CBORTaggedDecodable for PrivateKeyBase
impl CBORTaggedDecodable for PrivateKeyBase
Source§fn from_untagged_cbor(untagged_cbor: CBOR) -> Result<PrivateKeyBase, Error>
fn from_untagged_cbor(untagged_cbor: CBOR) -> Result<PrivateKeyBase, Error>
Source§fn from_tagged_cbor(cbor: CBOR) -> Result<Self, Error>where
Self: Sized,
fn from_tagged_cbor(cbor: CBOR) -> Result<Self, Error>where
Self: Sized,
Source§impl CBORTaggedEncodable for PrivateKeyBase
impl CBORTaggedEncodable for PrivateKeyBase
Source§fn untagged_cbor(&self) -> CBOR
fn untagged_cbor(&self) -> CBOR
Source§fn tagged_cbor(&self) -> CBOR
fn tagged_cbor(&self) -> CBOR
Source§impl Clone for PrivateKeyBase
impl Clone for PrivateKeyBase
Source§fn clone(&self) -> PrivateKeyBase
fn clone(&self) -> PrivateKeyBase
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for PrivateKeyBase
impl Debug for PrivateKeyBase
Source§impl Decrypter for PrivateKeyBase
impl Decrypter for PrivateKeyBase
Source§fn encapsulation_private_key(&self) -> EncapsulationPrivateKey
fn encapsulation_private_key(&self) -> EncapsulationPrivateKey
Source§impl Default for PrivateKeyBase
impl Default for PrivateKeyBase
Source§fn default() -> PrivateKeyBase
fn default() -> PrivateKeyBase
Source§impl Drop for PrivateKeyBase
impl Drop for PrivateKeyBase
Source§impl<'a> From<&'a PrivateKeyBase> for &'a [u8]
impl<'a> From<&'a PrivateKeyBase> for &'a [u8]
Source§impl From<PrivateKeyBase> for CBOR
impl From<PrivateKeyBase> for CBOR
Source§fn from(value: PrivateKeyBase) -> CBOR
fn from(value: PrivateKeyBase) -> CBOR
Source§impl From<PrivateKeyBase> for Envelope
impl From<PrivateKeyBase> for Envelope
Source§fn from(value: PrivateKeyBase) -> Self
fn from(value: PrivateKeyBase) -> Self
Converts this value into an envelope.
Source§impl PartialEq for PrivateKeyBase
impl PartialEq for PrivateKeyBase
Source§impl PrivateKeysProvider for PrivateKeyBase
impl PrivateKeysProvider for PrivateKeyBase
Source§fn private_keys(&self) -> PrivateKeys
fn private_keys(&self) -> PrivateKeys
Source§impl PublicKeysProvider for PrivateKeyBase
impl PublicKeysProvider for PrivateKeyBase
Source§fn public_keys(&self) -> PublicKeys
fn public_keys(&self) -> PublicKeys
Source§impl Signer for PrivateKeyBase
impl Signer for PrivateKeyBase
Source§impl TryFrom<CBOR> for PrivateKeyBase
impl TryFrom<CBOR> for PrivateKeyBase
Source§impl TryFrom<Envelope> for PrivateKeyBase
impl TryFrom<Envelope> for PrivateKeyBase
Source§impl Verifier for PrivateKeyBase
impl Verifier for PrivateKeyBase
impl Eq for PrivateKeyBase
impl StructuralPartialEq for PrivateKeyBase
Auto Trait Implementations§
impl Freeze for PrivateKeyBase
impl RefUnwindSafe for PrivateKeyBase
impl Send for PrivateKeyBase
impl Sync for PrivateKeyBase
impl Unpin for PrivateKeyBase
impl UnwindSafe for PrivateKeyBase
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CBOREncodable for T
impl<T> CBOREncodable for T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> EnvelopeEncodable for T
impl<T> EnvelopeEncodable for T
Source§fn into_envelope(self) -> Envelope
fn into_envelope(self) -> Envelope
Converts the value into an envelope by using its Into<Envelope> implementation.
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> ToHex for T
impl<T> ToHex for T
Source§fn encode_hex<U>(&self) -> Uwhere
U: FromIterator<char>,
fn encode_hex<U>(&self) -> Uwhere
U: FromIterator<char>,
self into the result. Lower case
letters are used (e.g. f9b4ca)Source§fn encode_hex_upper<U>(&self) -> Uwhere
U: FromIterator<char>,
fn encode_hex_upper<U>(&self) -> Uwhere
U: FromIterator<char>,
self into the result. Upper case
letters are used (e.g. F9B4CA)