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, PrivateKeysProvider, PublicKeysProvider, Signer,
};
// 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 from_optional_data(data: Option<impl AsRef<[u8]>>) -> Self
pub fn from_optional_data(data: Option<impl AsRef<[u8]>>) -> Self
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) -> Self
pub fn new_using(rng: &mut impl RandomNumberGenerator) -> Self
Generate a new random PrivateKeyBase
using the given random number
generator.
Sourcepub fn new_with_provider(provider: impl PrivateKeyDataProvider) -> Self
pub fn new_with_provider(provider: impl PrivateKeyDataProvider) -> Self
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: SSHAlgorithm,
comment: impl Into<String>,
) -> Result<SigningPrivateKey>
pub fn ssh_signing_private_key( &self, algorithm: SSHAlgorithm, comment: impl Into<String>, ) -> Result<SigningPrivateKey>
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: SSHAlgorithm,
comment: impl Into<String>,
) -> Result<PrivateKeys>
pub fn ssh_private_keys( &self, algorithm: SSHAlgorithm, comment: impl Into<String>, ) -> Result<PrivateKeys>
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: SSHAlgorithm,
comment: impl Into<String>,
) -> Result<PublicKeys>
pub fn ssh_public_keys( &self, algorithm: SSHAlgorithm, comment: impl Into<String>, ) -> Result<PublicKeys>
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<Self>
fn from_untagged_cbor(untagged_cbor: CBOR) -> Result<Self>
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§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§fn from(value: &'a PrivateKeyBase) -> Self
fn from(value: &'a PrivateKeyBase) -> Self
Source§impl From<&PrivateKeyBase> for XID
Implements conversion from PrivateKeyBase reference to XID via the Schnorr
signing key.
impl From<&PrivateKeyBase> for XID
Implements conversion from PrivateKeyBase reference to XID via the Schnorr signing key.
Source§fn from(key: &PrivateKeyBase) -> Self
fn from(key: &PrivateKeyBase) -> Self
Source§impl From<PrivateKeyBase> for CBOR
impl From<PrivateKeyBase> for CBOR
Source§fn from(value: PrivateKeyBase) -> Self
fn from(value: PrivateKeyBase) -> Self
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§fn sign_with_options(
&self,
message: &dyn AsRef<[u8]>,
options: Option<SigningOptions>,
) -> Result<Signature>
fn sign_with_options( &self, message: &dyn AsRef<[u8]>, options: Option<SigningOptions>, ) -> Result<Signature>
Source§impl TryFrom<CBOR> for PrivateKeyBase
impl TryFrom<CBOR> 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> CBORDecodable for T
impl<T> CBORDecodable for 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> 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
)