cryptraits/
key.rs

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