anya_core/security/
mod.rs1pub mod system_hardening;
9
10pub mod constant_time;
11
12pub mod crypto;
14
15pub mod encryption {
17 pub use super::crypto::symmetric::*;
18}
19
20#[cfg(feature = "hsm")]
22pub mod hsm;
23
24#[cfg(not(feature = "hsm"))]
26pub mod hsm_shim;
27
28pub use system_hardening::ConfigStatus;
30pub use system_hardening::HardeningConfig;
31pub use system_hardening::SecurityLevel;
32pub use system_hardening::SystemHardening;
33
34#[cfg(feature = "hsm")]
36pub use hsm::config::HsmConfig;
37#[cfg(feature = "hsm")]
38pub use hsm::provider::{HsmProvider, KeyGenParams, KeyType, SigningAlgorithm};
39#[cfg(feature = "hsm")]
40pub use hsm::{HsmManager, HsmStatus};
41
42#[cfg(not(feature = "hsm"))]
44pub use hsm_shim::{HsmManager, HsmStatus, HsmStubError, KeyType, SigningAlgorithm};
45
46pub fn create_system_hardening() -> SystemHardening {
48 SystemHardening::new(20)
49}
50
51pub fn create_basic_security_config(
53 component_name: &str,
54) -> std::collections::HashMap<String, String> {
55 let mut settings = std::collections::HashMap::new();
56 settings.insert("firewall".to_string(), "enabled".to_string());
58 settings.insert("encryption".to_string(), "enabled".to_string());
59 settings.insert("access_control".to_string(), "strict".to_string());
60 settings.insert("audit_logging".to_string(), "enabled".to_string());
61 settings.insert("intrusion_detection".to_string(), "enabled".to_string());
62
63 match component_name {
65 "network" => {
66 settings.insert(
67 "port_scanning_protection".to_string(),
68 "enabled".to_string(),
69 );
70 settings.insert("ddos_protection".to_string(), "enabled".to_string());
71 }
72 "database" => {
73 settings.insert("query_sanitization".to_string(), "strict".to_string());
74 settings.insert("data_encryption".to_string(), "aes-256".to_string());
75 }
76 "api" => {
77 settings.insert("rate_limiting".to_string(), "enabled".to_string());
78 settings.insert("input_validation".to_string(), "strict".to_string());
79 }
80 _ => {
81 settings.insert("default_deny".to_string(), "enabled".to_string());
83 }
84 }
85
86 settings
87}
88
89use log::info;
94#[cfg(feature = "hsm")]
99pub use hsm::{
100 audit::{AuditEvent, AuditLoggerConfig, AuditStorageType},
101 error::HsmError,
120};
121
122pub async fn initialize() -> Result<(), Box<dyn std::error::Error>> {
139 info!("Initializing security subsystem");
140
141 info!("Security subsystem initialized");
149 Ok(())
150}
151
152#[cfg(feature = "hsm")]
163pub async fn create_bitcoin_hsm_provider(
164 _base_provider: std::sync::Arc<dyn hsm::provider::HsmProvider>,
165) -> Result<hsm::providers::bitcoin::BitcoinHsmProvider, hsm::error::HsmError> {
166 #[cfg(feature = "hsm")]
167 let config = hsm::config::BitcoinConfig {
168 network: hsm::config::BitcoinNetworkType::Testnet, rpc_url: Some("http://127.0.0.1:18332".to_string()),
170 rpc_username: Some("user".to_string()),
171 rpc_password: Some("password".to_string()),
172 derivation_path_template: "m/84'/0'/0'/{index}".to_string(),
173 use_segwit: true,
174 use_taproot: true,
175 confirm_transactions: false,
176 default_fee_rate: 5,
177 };
178
179 hsm::providers::bitcoin::BitcoinHsmProvider::new(&config).await
180}
181
182#[cfg(not(feature = "hsm"))]
183pub fn create_bitcoin_hsm_provider(
184 _base_provider: std::sync::Arc<dyn hsm_shim::HsmProvider>,
185) -> hsm_shim::BitcoinHsmProvider {
186 #[allow(clippy::default_constructed_unit_structs)]
187 hsm_shim::BitcoinHsmProvider::default()
188}
189
190#[cfg(feature = "hsm")]
202pub async fn verify_bitcoin_payment(
203 _bitcoin_provider: &hsm::providers::bitcoin::BitcoinHsmProvider,
204 _proof_data: Vec<u8>,
205) -> Result<bool, hsm::error::HsmError> {
206 Ok(true)
209}
210
211#[cfg(not(feature = "hsm"))]
212pub async fn verify_bitcoin_payment(
214 _bitcoin_provider: &hsm_shim::BitcoinHsmProvider,
215 _proof_data: Vec<u8>,
216) -> Result<bool, hsm_shim::HsmStubError> {
217 Err(hsm_shim::HsmStubError::feature_disabled())
220}
221
222#[cfg(feature = "hsm")]
235pub async fn create_taproot_asset(
236 bitcoin_provider: &hsm::providers::bitcoin::BitcoinHsmProvider,
237 metadata: &str,
238 supply: u64,
239) -> Result<String, hsm::error::HsmError> {
240 let mut attributes = std::collections::HashMap::new();
242 attributes.insert("metadata".to_string(), metadata.to_string());
243 attributes.insert("supply".to_string(), supply.to_string());
244
245 let key_params = hsm::provider::KeyGenParams {
246 id: Some("asset".to_string()),
247 label: Some(format!("Asset key for {}", metadata)),
248 key_type: hsm::provider::KeyType::Ec {
249 curve: hsm::provider::EcCurve::Secp256k1,
250 },
251 extractable: false,
252 usages: vec![hsm::provider::KeyUsage::Sign],
253 expires_at: None,
254 attributes,
255 };
256
257 let (key_pair, _key_info) = bitcoin_provider.generate_key(key_params).await?;
259 Ok(key_pair.id)
260}
261
262#[cfg(not(feature = "hsm"))]
263pub async fn create_taproot_asset(
265 _bitcoin_provider: &hsm_shim::BitcoinHsmProvider,
266 _metadata: &str,
267 _supply: u64,
268) -> Result<String, hsm_shim::HsmStubError> {
269 Err(hsm_shim::HsmStubError::feature_disabled())
272}
273
274#[cfg(test)]
275mod tests {
276 use super::*;
277
278 #[test]
279 fn test_basic_security_config() {
280 let network_config = create_basic_security_config("network");
281 let db_config = create_basic_security_config("database");
282
283 assert_eq!(network_config.get("firewall"), Some(&"enabled".to_string()));
285 assert_eq!(db_config.get("firewall"), Some(&"enabled".to_string()));
286
287 assert_eq!(
289 network_config.get("ddos_protection"),
290 Some(&"enabled".to_string())
291 );
292 assert_eq!(
293 db_config.get("data_encryption"),
294 Some(&"aes-256".to_string())
295 );
296 }
297}