1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
//! Key management related traits.
#[cfg(feature = "std")]
use std::borrow::Cow;
#[cfg(not(feature = "std"))]
use alloc::fmt::Display;
#[cfg(feature = "std")]
use std::fmt::Display;
#[cfg(not(feature = "std"))]
use alloc::fmt::Debug;
use rand_core::{CryptoRng, RngCore};
#[cfg(feature = "std")]
use std::fmt::Debug;
#[cfg(not(feature = "std"))]
use alloc::string::String;
use zeroize::Zeroize;
use crate::error::Error;
/// Trait represents a public key.
pub trait PublicKey: Debug + Display + Clone + Copy + PartialEq + Zeroize {
/// Returns a byte slice of `PublicKey`.
fn as_bytes(&self) -> &[u8];
}
/// Trait represents a secret key.
pub trait SecretKey: Clone + Zeroize + Debug {
type PK: PublicKey;
/// Derives the `PublicKey` corresponding to this `SecretKey`.
fn to_public(&self) -> Self::PK;
}
/// Trait represents a shared secret key (e.g. obtained via DH exchange).
pub trait SharedSecretKey: Clone + Zeroize + Debug {}
/// Trait represents a keypair.
pub trait KeyPair: Clone + Zeroize {
type SK: SecretKey;
/// Get a `PublicKey` of `KeyPair`.
fn public(&self) -> &<Self::SK as SecretKey>::PK;
/// Derives the `PublicKey` corresponding to `KeyPair` `SK`;
fn to_public(&self) -> <Self::SK as SecretKey>::PK;
/// Get a `SecretKey` of `KeyPair`.
fn secret(&self) -> &Self::SK;
}
pub trait Generate {
/// Generate an "unbiased" `SecretKey`;
fn generate() -> Self;
/// Generates an "unbiased" `SecretKey` directly from a user
/// suplied `csprng` uniformly.
fn generate_with<R: CryptoRng + RngCore>(csprng: R) -> Self
where
Self: Sized;
}
/// Generate and construct a value with mnemonic phrase and optional password.
pub trait WithPhrase {
type E: Error;
/// Generate a new value of `word_count` words and optional password.
///
/// Returns tuple of generated value and a phrase or error.
fn generate_with_phrase(
word_count: usize,
password: Option<&str>,
) -> Result<(Self, String), Self::E>
where
Self: Sized;
/// Construct a value from mnemonic phrase and optional password.
fn from_phrase<'a, S: Into<Cow<'a, str>>>(
s: S,
password: Option<&str>,
) -> Result<Self, Self::E>
where
Self: Sized;
/// Generate a new value of `word_count` words and optional password witn `rng` PRF.
///
/// Returns tuple of generated value and a phrase or error.
fn generate_in_with<R>(
rng: &mut R,
word_count: usize,
password: Option<&str>,
) -> Result<(Self, String), Self::E>
where
Self: Sized,
R: RngCore + CryptoRng;
}
/// Construct a value from user-provided entropy.
pub trait FromEntropy {
type E: Error;
/// Construct a value from user-provided entropy.
///
/// Returns a value from entropy or error.
fn from_entropy(entropy: &[u8]) -> Result<Self, Self::E>
where
Self: Sized;
}
/// Perform a blinding operation on keys.
pub trait Blind {
type E: Error;
/// Perform a blinding operation on the key with the given blinding factor.
fn blind(&mut self, blinding_factor: &[u8]) -> Result<(), Self::E>;
/// Perform a blinding operation on the key with the given blinding factor.
fn to_blind(&self, blinding_factor: &[u8]) -> Result<Self, Self::E>
where
Self: Sized;
}