Skip to main content

crypto_core/
lib.rs

1//! Crypto Core - Cryptographic Primitives for R-SRP Ultra
2//! 
3//! Provides SHA-256/SHA-512, BLAKE3, Ed25519, and RSA-PSS implementations
4//! with HSM integration support.
5
6pub mod hash;
7pub mod signature;
8pub mod merkle;
9pub mod hsm;
10pub mod error;
11
12use serde::{Deserialize, Serialize};
13
14/// Hash algorithm selection
15#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
16#[serde(rename_all = "UPPERCASE")]
17pub enum HashAlgorithm {
18    /// SHA-256 (default)
19    Sha256,
20    /// SHA-512
21    Sha512,
22    /// BLAKE3 (high performance)
23    Blake3,
24}
25
26impl Default for HashAlgorithm {
27    fn default() -> Self {
28        HashAlgorithm::Sha256
29    }
30}
31
32/// Signature algorithm selection
33#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
34#[serde(rename_all = "UPPERCASE")]
35pub enum SignatureAlgorithm {
36    /// RSA-PSS 2048
37    RsaPss2048,
38    /// RSA-PSS 4096
39    RsaPss4096,
40    /// Ed25519
41    Ed25519,
42    /// ECDSA P-256
43    EcdsaP256,
44    /// ECDSA P-384
45    EcdsaP384,
46}
47
48impl Default for SignatureAlgorithm {
49    fn default() -> Self {
50        SignatureAlgorithm::Ed25519
51    }
52}
53
54/// Key metadata
55#[derive(Debug, Clone, Serialize, Deserialize)]
56pub struct KeyMetadata {
57    /// Key ID
58    pub key_id: String,
59    /// Algorithm
60    pub algorithm: SignatureAlgorithm,
61    /// Created at
62    pub created_at: i64,
63    /// Key type
64    pub key_type: KeyType,
65    /// HSM slot (if applicable)
66    pub hsm_slot: Option<String>,
67}
68
69/// Key type
70#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
71#[serde(rename_all = "snake_case")]
72pub enum KeyType {
73    /// Signing key
74    Signing,
75    /// Verification key
76    Verification,
77    /// Encryption key
78    Encryption,
79    /// HSM-backed key
80    HsmBacked,
81}
82
83/// Cryptographic service error
84#[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}