Skip to main content

lib_q_core/providers/
hash_provider.rs

1//! Hash provider implementation
2//!
3//! This module provides the LibQHashProvider that implements hash operations
4//! with proper security validation and algorithm routing.
5
6#[cfg(feature = "alloc")]
7use alloc::{
8    string::ToString,
9    vec::Vec,
10};
11
12use crate::api::{
13    Algorithm,
14    HashOperations,
15};
16use crate::error::Result;
17use crate::security::SecurityValidator;
18
19/// lib-Q hash provider implementation
20///
21/// This provider implements hash operations for lib-Q with proper security validation
22/// and algorithm routing.
23#[cfg(feature = "alloc")]
24#[derive(Clone)]
25pub struct LibQHashProvider {
26    security_validator: SecurityValidator,
27}
28
29#[cfg(feature = "alloc")]
30impl LibQHashProvider {
31    /// Create a new hash provider
32    ///
33    /// # Returns
34    ///
35    /// A new instance of LibQHashProvider with security validation initialized.
36    ///
37    /// # Errors
38    ///
39    /// Returns an error if the security validator fails to initialize.
40    pub fn new() -> Result<Self> {
41        Ok(Self {
42            security_validator: SecurityValidator::new()?,
43        })
44    }
45}
46
47#[cfg(feature = "alloc")]
48impl HashOperations for LibQHashProvider {
49    fn hash(&self, algorithm: Algorithm, data: &[u8]) -> Result<Vec<u8>> {
50        // Validate algorithm category
51        self.security_validator
52            .validate_algorithm_category(algorithm, crate::api::AlgorithmCategory::Hash)?;
53
54        // Validate data
55        self.security_validator.validate_hash_input(data)?;
56
57        // Route to specific algorithm implementation
58        // Note: Actual implementations are provided by the main lib-q crate
59        match algorithm {
60            Algorithm::Sha3_224 |
61            Algorithm::Sha3_256 |
62            Algorithm::Sha3_384 |
63            Algorithm::Sha3_512 => Err(crate::error::Error::NotImplemented {
64                feature: "SHA3 implementations are provided by the main lib-q crate".to_string(),
65            }),
66            Algorithm::Shake128 | Algorithm::Shake256 => Err(crate::error::Error::NotImplemented {
67                feature: "SHAKE implementations are provided by the main lib-q crate".to_string(),
68            }),
69            Algorithm::CShake128 | Algorithm::CShake256 => {
70                Err(crate::error::Error::NotImplemented {
71                    feature: "cSHAKE implementations are provided by the main lib-q crate"
72                        .to_string(),
73                })
74            }
75            Algorithm::Kmac128 | Algorithm::Kmac256 => Err(crate::error::Error::NotImplemented {
76                feature: "KMAC implementations are provided by the main lib-q crate".to_string(),
77            }),
78            Algorithm::TupleHash128 | Algorithm::TupleHash256 => {
79                Err(crate::error::Error::NotImplemented {
80                    feature: "TupleHash implementations are provided by the main lib-q crate"
81                        .to_string(),
82                })
83            }
84            Algorithm::ParallelHash128 | Algorithm::ParallelHash256 => {
85                Err(crate::error::Error::NotImplemented {
86                    feature: "ParallelHash implementations are provided by the main lib-q crate"
87                        .to_string(),
88                })
89            }
90            Algorithm::Keccak224 |
91            Algorithm::Keccak256 |
92            Algorithm::Keccak384 |
93            Algorithm::Keccak512 => Err(crate::error::Error::NotImplemented {
94                feature: "Keccak implementations are provided by the main lib-q crate".to_string(),
95            }),
96            Algorithm::Kt128 | Algorithm::Kt256 => Err(crate::error::Error::NotImplemented {
97                feature: "KT128/KT256 (lib-q-k12) is provided by the lib-q-hash / lib-q meta crate"
98                    .to_string(),
99            }),
100            Algorithm::TurboShake128 | Algorithm::TurboShake256 => {
101                Err(crate::error::Error::NotImplemented {
102                    feature: "TurboShake implementations are provided by the main lib-q crate"
103                        .to_string(),
104                })
105            }
106            Algorithm::Sha224 |
107            Algorithm::Sha256 |
108            Algorithm::Sha384 |
109            Algorithm::Sha512 |
110            Algorithm::Sha512_224 |
111            Algorithm::Sha512_256 => Err(crate::error::Error::NotImplemented {
112                feature: "SHA-2 implementations are provided by the main lib-q crate".to_string(),
113            }),
114            _ => Err(crate::error::Error::InvalidAlgorithm {
115                algorithm: "Algorithm not supported for hash operations",
116            }),
117        }
118    }
119}
120
121#[cfg(test)]
122mod tests {
123    use super::*;
124
125    #[test]
126    fn test_hash_provider_creation() {
127        let provider = LibQHashProvider::new();
128        assert!(
129            provider.is_ok(),
130            "LibQHashProvider should be created successfully"
131        );
132    }
133
134    #[test]
135    fn test_hash_provider_unsupported_algorithm() {
136        let provider = LibQHashProvider::new().unwrap();
137        let result = provider.hash(Algorithm::MlKem512, b"test data");
138        assert!(
139            result.is_err(),
140            "Should return error for unsupported algorithm"
141        );
142
143        if let Err(crate::error::Error::InvalidAlgorithm { .. }) = result {
144            // Expected error type
145        } else {
146            panic!("Expected InvalidAlgorithm error");
147        }
148    }
149
150    #[test]
151    fn test_hash_provider_supported_algorithm() {
152        let provider = LibQHashProvider::new().unwrap();
153        let result = provider.hash(Algorithm::Sha3_256, b"test data");
154        assert!(
155            result.is_err(),
156            "Should return NotImplemented error for unimplemented algorithm"
157        );
158
159        if let Err(crate::error::Error::NotImplemented { .. }) = result {
160            // Expected error type
161        } else {
162            panic!("Expected NotImplemented error");
163        }
164    }
165}