seal_crypto/traits/
key.rs1use crate::errors::Error;
5use crate::traits::algorithm::Algorithm;
6use zeroize::Zeroize;
7
8#[cfg(feature = "serde")]
9use serde::{Deserialize, Serialize};
10
11#[derive(Debug, PartialEq, Eq)]
15#[cfg_attr(feature = "std", derive(thiserror::Error))]
16pub enum KeyError {
17 #[cfg_attr(feature = "std", error("Key generation failed"))]
21 GenerationFailed,
22 #[cfg_attr(feature = "std", error("Invalid key encoding"))]
26 InvalidEncoding,
27 #[cfg_attr(feature = "std", error("Invalid key encoding"))]
31 InvalidLength,
32}
33
34#[cfg(feature = "serde")]
35pub trait ConditionallySerde: Serialize + for<'de> Deserialize<'de> {}
39#[cfg(feature = "serde")]
40impl<T: Serialize + for<'de> Deserialize<'de>> ConditionallySerde for T {}
41
42#[cfg(not(feature = "serde"))]
43pub trait ConditionallySerde {}
47#[cfg(not(feature = "serde"))]
48impl<T> ConditionallySerde for T {}
49
50pub trait Key: Sized + Send + Sync + 'static + Clone + ConditionallySerde {
54 fn from_bytes(bytes: &[u8]) -> Result<Self, Error>;
58
59 fn to_bytes(&self) -> Result<Vec<u8>, Error>;
63}
64
65pub trait PublicKey: Key + Clone + for<'a> From<&'a Self> {}
69
70pub trait PrivateKey<P: PublicKey>: Key + Zeroize {}
74
75pub trait AsymmetricKeySet: Algorithm {
79 type PublicKey: PublicKey;
80 type PrivateKey: PrivateKey<Self::PublicKey>;
81}
82
83pub trait SymmetricKeySet: Algorithm {
87 type Key: Key;
88}