sentc_crypto_std_keys/core/sign/
mod.rs

1use alloc::vec::Vec;
2
3use sentc_crypto_core::cryptomat::{CryptoAlg, Sig, SignK, SignKeyComposer, SignKeyPair, SymKey, VerifyK};
4use sentc_crypto_core::Error;
5use sha2::Digest;
6
7use crate::core::sign::ed25519::{Ed25519Sig, Ed25519SignK, Ed25519VerifyK};
8use crate::core::sign::ed25519_dilithium_hybrid::{Ed25519DilithiumHybridSig, Ed25519DilithiumHybridSignK, Ed25519DilithiumHybridVerifyKey};
9use crate::core::sign::pqc_dilithium::{DilithiumSig, DilithiumSignKey, DilithiumVerifyKey};
10
11pub(crate) mod ed25519;
12pub(crate) mod ed25519_dilithium_hybrid;
13pub(crate) mod pqc_dilithium;
14
15macro_rules! deref_macro {
16    ($self:expr, $method:ident $(, $args:expr)*) => {
17        match $self {
18            Self::Ed25519(inner) => inner.$method($($args),*),
19            Self::Dilithium(inner) => inner.$method($($args),*),
20			Self::Ed25519DilithiumHybrid(inner) => inner.$method($($args),*),
21        }
22    };
23}
24
25macro_rules! crypto_alg_impl {
26	($name:ty) => {
27		impl CryptoAlg for $name
28		{
29			fn get_alg_str(&self) -> &'static str
30			{
31				deref_macro!(self, get_alg_str)
32			}
33		}
34	};
35}
36
37macro_rules! get_inner_key {
38	($st:ty,$t:ident) => {
39		impl $st
40		{
41			pub fn ed25519_from_bytes_owned(bytes: Vec<u8>) -> Result<Self, Error>
42			{
43				Ok(Self::Ed25519(bytes.try_into()?))
44			}
45
46			pub fn dilithium_from_bytes_owned(bytes: Vec<u8>) -> Result<Self, Error>
47			{
48				Ok(Self::Dilithium(bytes.try_into()?))
49			}
50
51			pub fn ed25519_dilithium_hybrid_from_bytes_owned(bytes_x: Vec<u8>, bytes_k: Vec<u8>) -> Result<Self, Error>
52			{
53				Ok(Self::Ed25519DilithiumHybrid($t::from_bytes_owned(bytes_x, bytes_k)?))
54			}
55		}
56	};
57}
58
59pub enum SignKey
60{
61	Ed25519(Ed25519SignK),
62	Dilithium(DilithiumSignKey),
63	Ed25519DilithiumHybrid(Ed25519DilithiumHybridSignK),
64}
65
66get_inner_key!(SignKey, Ed25519DilithiumHybridSignK);
67crypto_alg_impl!(SignKey);
68
69impl SignK for SignKey
70{
71	type Signature = Signature;
72
73	fn encrypt_by_master_key<M: SymKey>(&self, master_key: &M) -> Result<Vec<u8>, Error>
74	{
75		deref_macro!(self, encrypt_by_master_key, master_key)
76	}
77
78	fn sign(&self, data: &[u8]) -> Result<Vec<u8>, Error>
79	{
80		deref_macro!(self, sign, data)
81	}
82
83	fn sign_only<D: AsRef<[u8]>>(&self, data: D) -> Result<Self::Signature, Error>
84	{
85		let out: Signature = match self {
86			Self::Ed25519(inner) => inner.sign_only(data)?.into(),
87			Self::Dilithium(inner) => inner.sign_only(data)?.into(),
88			Self::Ed25519DilithiumHybrid(inner) => inner.sign_only(data)?.into(),
89		};
90
91		Ok(out)
92	}
93}
94
95impl SignKeyPair for SignKey
96{
97	type SignKey = Self;
98	type VerifyKey = VerifyKey;
99
100	fn generate_key_pair() -> Result<(Self::SignKey, Self::VerifyKey), Error>
101	{
102		#[cfg(feature = "ed25519_dilithium_hybrid")]
103		let (sk, vk) = ed25519_dilithium_hybrid::Ed25519DilithiumHybridKeyPair::generate_key_pair()?;
104
105		#[cfg(feature = "ed25519")]
106		let (sk, vk) = ed25519::Ed25519KeyPair::generate_key_pair()?;
107
108		Ok((sk.into(), vk.into()))
109	}
110}
111
112impl SignKeyComposer for SignKey
113{
114	type Key = Self;
115
116	fn decrypt_by_master_key<M: SymKey>(master_key: &M, encrypted_key: &[u8], alg_str: &str) -> Result<Self::Key, Error>
117	{
118		let key = master_key.decrypt(encrypted_key)?;
119
120		let key = match alg_str {
121			ed25519::ED25519_OUTPUT => Self::Ed25519(key.try_into()?),
122			pqc_dilithium::DILITHIUM_OUTPUT => Self::Dilithium(key.try_into()?),
123			ed25519_dilithium_hybrid::ED25519_DILITHIUM_HYBRID_OUTPUT => Self::Ed25519DilithiumHybrid(key.try_into()?),
124			_ => return Err(Error::AlgNotFound),
125		};
126
127		Ok(key)
128	}
129}
130
131pub enum VerifyKey
132{
133	Ed25519(Ed25519VerifyK),
134	Dilithium(DilithiumVerifyKey),
135	Ed25519DilithiumHybrid(Ed25519DilithiumHybridVerifyKey),
136}
137
138get_inner_key!(VerifyKey, Ed25519DilithiumHybridVerifyKey);
139crypto_alg_impl!(VerifyKey);
140
141impl VerifyK for VerifyKey
142{
143	type Signature = Signature;
144
145	fn verify<'a>(&self, data_with_sig: &'a [u8]) -> Result<(&'a [u8], bool), Error>
146	{
147		deref_macro!(self, verify, data_with_sig)
148	}
149
150	fn verify_only(&self, sig: &Self::Signature, data: &[u8]) -> Result<bool, Error>
151	{
152		match (self, sig) {
153			(Self::Ed25519(inner), Signature::Ed25519(s)) => inner.verify_only(s, data),
154			(Self::Dilithium(inner), Signature::Dilithium(s)) => inner.verify_only(s, data),
155			(Self::Ed25519DilithiumHybrid(inner), Signature::Ed25519DilithiumHybrid(s)) => inner.verify_only(s, data),
156			_ => Err(Error::AlgNotFound),
157		}
158	}
159
160	fn create_hash<D: Digest>(&self, hasher: &mut D)
161	{
162		deref_macro!(self, create_hash, hasher)
163	}
164}
165
166pub enum Signature
167{
168	Ed25519(Ed25519Sig),
169	Dilithium(DilithiumSig),
170	Ed25519DilithiumHybrid(Ed25519DilithiumHybridSig),
171}
172
173impl Signature
174{
175	pub fn split_sig_and_data<'a>(alg: &str, data_with_sign: &'a [u8]) -> Result<(&'a [u8], &'a [u8]), Error>
176	{
177		match alg {
178			ed25519::ED25519_OUTPUT => ed25519::split_sig_and_data(data_with_sign),
179			pqc_dilithium::DILITHIUM_OUTPUT => pqc_dilithium::split_sig_and_data(data_with_sign),
180			ed25519_dilithium_hybrid::ED25519_DILITHIUM_HYBRID_OUTPUT => ed25519_dilithium_hybrid::split_sig_and_data(data_with_sign),
181			_ => Err(Error::AlgNotFound),
182		}
183	}
184}
185
186crypto_alg_impl!(Signature);
187get_inner_key!(Signature, Ed25519DilithiumHybridSig);
188
189impl Into<Vec<u8>> for Signature
190{
191	fn into(self) -> Vec<u8>
192	{
193		deref_macro!(self, into)
194	}
195}
196
197impl Sig for Signature
198{
199	// fn split_sig_and_data<'a>(&self) -> Result<(&'a [u8], &'a [u8]), Error>
200	// {
201	// 	deref_macro!(self, split_sig_and_data)
202	// }
203	//
204	// fn get_raw(&self) -> &[u8]
205	// {
206	// 	deref_macro!(self, get_raw)
207	// }
208}