1pub mod hash;
7pub mod signature;
8pub mod merkle;
9pub mod hsm;
10pub mod error;
11
12use serde::{Deserialize, Serialize};
13
14#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
16#[serde(rename_all = "UPPERCASE")]
17pub enum HashAlgorithm {
18 Sha256,
20 Sha512,
22 Blake3,
24}
25
26impl Default for HashAlgorithm {
27 fn default() -> Self {
28 HashAlgorithm::Sha256
29 }
30}
31
32#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
34#[serde(rename_all = "UPPERCASE")]
35pub enum SignatureAlgorithm {
36 RsaPss2048,
38 RsaPss4096,
40 Ed25519,
42 EcdsaP256,
44 EcdsaP384,
46}
47
48impl Default for SignatureAlgorithm {
49 fn default() -> Self {
50 SignatureAlgorithm::Ed25519
51 }
52}
53
54#[derive(Debug, Clone, Serialize, Deserialize)]
56pub struct KeyMetadata {
57 pub key_id: String,
59 pub algorithm: SignatureAlgorithm,
61 pub created_at: i64,
63 pub key_type: KeyType,
65 pub hsm_slot: Option<String>,
67}
68
69#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
71#[serde(rename_all = "snake_case")]
72pub enum KeyType {
73 Signing,
75 Verification,
77 Encryption,
79 HsmBacked,
81}
82
83#[derive(Debug, thiserror::Error)]
85pub enum CryptoError {
86 #[error("Hash error: {0}")]
87 HashError(String),
88
89 #[error("Signature error: {0}")]
90 SignatureError(String),
91
92 #[error("Key error: {0}")]
93 KeyError(String),
94
95 #[error("HSM error: {0}")]
96 HsmError(String),
97
98 #[error("Verification failed")]
99 VerificationFailed,
100
101 #[error("Invalid key")]
102 InvalidKey,
103}
104
105impl serde::Serialize for CryptoError {
106 fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
107 where
108 S: serde::Serializer,
109 {
110 serializer.serialize_str(&self.to_string())
111 }
112}
113
114pub type Result<T> = std::result::Result<T, CryptoError>;
115
116#[cfg(test)]
117mod tests {
118 use super::*;
119
120 #[test]
121 fn test_default_hash_algorithm() {
122 let algo: HashAlgorithm = serde_json::from_str("\"SHA256\"").unwrap();
123 assert_eq!(algo, HashAlgorithm::Sha256);
124 }
125
126 #[test]
127 fn test_default_signature_algorithm() {
128 let algo: SignatureAlgorithm = serde_json::from_str("\"ED25519\"").unwrap();
129 assert_eq!(algo, SignatureAlgorithm::Ed25519);
130 }
131}