ciphern 0.2.1

Enterprise-grade cryptographic library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
// Copyright (c) 2025 Kirky.X
//
// Licensed under the MIT License
// See LICENSE file in the project root for full license information.

//! FIPS 140-3 合规模块
//!
//! 提供 FIPS 140-3 Level 1 合规性支持,包括:
//! - 算法白名单验证
//! - 自检测试引擎 (POST, Conditional, Periodic)
//! - 错误状态管理
//! - 审计日志

#[cfg(feature = "encrypt")]
use crate::error::CryptoError;
use crate::error::Result;
use crate::types::Algorithm;
use std::collections::HashMap;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};

pub mod self_test;
#[cfg(feature = "encrypt")]
pub mod validator;

#[cfg(test)]
mod tests;

pub use self_test::SelfTestResult;

/// FIPS 模式状态
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FipsMode {
    Enabled,
    Disabled,
}

/// FIPS 错误状态
#[derive(Debug, thiserror::Error, Clone)]
pub enum FipsError {
    #[error("Algorithm not FIPS 140-3 approved: {0}")]
    AlgorithmNotApproved(String),

    #[error("Self test failed: {0}")]
    SelfTestFailed(String),

    #[error("FIPS mode required but disabled")]
    FipsModeRequired,

    #[error("Invalid key size for FIPS algorithm: {0}")]
    InvalidKeySize(String),

    #[error("Memory protection violation in FIPS mode: {0}")]
    MemoryProtectionViolation(String),

    #[error("Random number generator health check failed: {0}")]
    RngHealthCheckFailed(String),

    #[error("Conditional self test failed for algorithm {0}: {1}")]
    ConditionalSelfTestFailed(String, String),
}

/// FIPS 错误状态管理
#[derive(Debug)]
pub struct FipsErrorState {
    is_error: Arc<AtomicBool>,
    error_type: Arc<Mutex<Option<FipsError>>>,
    error_count: Arc<AtomicUsize>,
}

impl Default for FipsErrorState {
    fn default() -> Self {
        Self {
            is_error: Arc::new(AtomicBool::new(false)),
            error_type: Arc::new(Mutex::new(None)),
            error_count: Arc::new(AtomicUsize::new(0)),
        }
    }
}

impl Clone for FipsErrorState {
    fn clone(&self) -> Self {
        Self {
            is_error: self.is_error.clone(),
            error_type: self.error_type.clone(),
            error_count: self.error_count.clone(),
        }
    }
}

impl FipsErrorState {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn set_error(&self, error: FipsError) {
        self.is_error.store(true, Ordering::SeqCst);
        *self.error_type.lock().unwrap() = Some(error.clone());
        self.error_count.fetch_add(1, Ordering::SeqCst);

        // 记录到审计日志
        crate::audit::AuditLogger::log(
            "FIPS_ERROR",
            None,
            None,
            Err(crate::CryptoError::FipsError(error.to_string())),
        );
    }

    pub fn is_in_error_state(&self) -> bool {
        self.is_error.load(Ordering::SeqCst)
    }

    pub fn get_error(&self) -> Option<FipsError> {
        self.error_type.lock().unwrap().clone()
    }

    pub fn clear_error(&self) {
        self.is_error.store(false, Ordering::SeqCst);
        *self.error_type.lock().unwrap() = None;
    }
}

/// FIPS 上下文管理器
#[derive(Clone)]
pub struct FipsContext {
    mode: FipsMode,
    #[cfg(feature = "encrypt")]
    _validator: self::validator::FipsAlgorithmValidator,
    #[cfg(feature = "encrypt")]
    self_test_engine: self::self_test::FipsSelfTestEngine,
    error_state: FipsErrorState,
    algorithm_usage_stats: Arc<Mutex<HashMap<Algorithm, usize>>>,
}

impl FipsContext {
    /// 创建新的 FIPS 上下文
    pub fn new(mode: FipsMode) -> Result<Self> {
        let error_state = FipsErrorState::new();
        let algorithm_usage_stats = Arc::new(Mutex::new(HashMap::new()));

        #[cfg(feature = "encrypt")]
        {
            let validator = self::validator::FipsAlgorithmValidator;
            let self_test_engine = self::self_test::FipsSelfTestEngine::new();
            let context = Self {
                mode,
                _validator: validator,
                self_test_engine,
                error_state,
                algorithm_usage_stats,
            };

            // 如果启用 FIPS 模式,运行上电自检
            if mode == FipsMode::Enabled {
                context.self_test_engine.run_power_on_self_tests()?;
            }
            Ok(context)
        }
        #[cfg(not(feature = "encrypt"))]
        {
            Ok(Self {
                mode,
                error_state,
                algorithm_usage_stats,
            })
        }
    }

    /// 启用 FIPS 模式
    pub fn enable() -> Result<()> {
        // 创建临时上下文进行自检
        let _temp_context = Self::new(FipsMode::Enabled)?;

        // 设置全局 FIPS 模式
        set_fips_mode(FipsMode::Enabled);

        // 记录到审计日志
        crate::audit::AuditLogger::log("FIPS_MODE_ENABLED", None, None, Ok(()));

        Ok(())
    }

    /// 禁用 FIPS 模式
    pub fn disable() -> Result<()> {
        set_fips_mode(FipsMode::Disabled);

        // 记录到审计日志
        crate::audit::AuditLogger::log("FIPS_MODE_DISABLED", None, None, Ok(()));

        Ok(())
    }

    /// 检查是否启用 FIPS 模式
    pub fn is_enabled() -> bool {
        get_fips_mode() == FipsMode::Enabled
    }

    /// 验证算法是否符合 FIPS 要求
    pub fn validate_algorithm(&self, algorithm: &Algorithm) -> Result<()> {
        if self.mode != FipsMode::Enabled {
            return Ok(());
        }

        // 验证算法是否在白名单中
        self.validate_fips_compliance(algorithm)?;

        // 记录算法使用统计
        {
            let mut stats = self.algorithm_usage_stats.lock().unwrap();
            *stats.entry(*algorithm).or_insert(0) += 1;
        }

        // 执行条件自检
        #[cfg(feature = "encrypt")]
        {
            if let Err(e) = self.self_test_engine.run_conditional_self_test(*algorithm) {
                self.error_state
                    .set_error(FipsError::ConditionalSelfTestFailed(
                        format!("{:?}", algorithm),
                        e.to_string(),
                    ));
                return Err(e);
            }
        }

        Ok(())
    }

    /// 运行条件自检
    pub fn run_conditional_self_test(&self, algorithm: Algorithm) -> Result<()> {
        #[cfg(feature = "encrypt")]
        {
            self.self_test_engine.run_conditional_self_test(algorithm)
        }
        #[cfg(not(feature = "encrypt"))]
        {
            let _ = algorithm;
            Ok(())
        }
    }

    /// 验证密钥大小是否符合 FIPS 要求
    pub fn validate_key_size(&self, algorithm: &Algorithm, key_size: usize) -> Result<()> {
        if self.mode != FipsMode::Enabled {
            return Ok(());
        }

        #[cfg(feature = "encrypt")]
        {
            validator::FipsAlgorithmValidator::validate_key_size(algorithm, key_size)
        }
        #[cfg(not(feature = "encrypt"))]
        {
            let _ = (algorithm, key_size);
            Ok(())
        }
    }

    /// 获取算法使用统计
    pub fn get_algorithm_usage_stats(&self) -> HashMap<Algorithm, usize> {
        self.algorithm_usage_stats.lock().unwrap().clone()
    }

    pub fn validate_fips_compliance(&self, algorithm: &Algorithm) -> Result<()> {
        #[cfg(feature = "encrypt")]
        {
            validator::FipsAlgorithmValidator::validate_fips_compliance(algorithm)
        }
        #[cfg(not(feature = "encrypt"))]
        {
            let _ = algorithm;
            Ok(())
        }
    }

    /// 获取自检测试结果
    pub fn get_self_test_results(&self) -> HashMap<String, SelfTestResult> {
        #[cfg(feature = "encrypt")]
        {
            self.self_test_engine.get_test_results()
        }
        #[cfg(not(feature = "encrypt"))]
        {
            HashMap::new()
        }
    }

    /// 获取特定测试的结果
    pub fn get_self_test_result(&self, test_name: &str) -> Option<SelfTestResult> {
        #[cfg(feature = "encrypt")]
        {
            self.self_test_engine.get_test_result(test_name)
        }
        #[cfg(not(feature = "encrypt"))]
        {
            let _ = test_name;
            None
        }
    }

    /// 检查是否所有必需的测试都通过
    pub fn all_required_tests_passed(&self) -> bool {
        #[cfg(feature = "encrypt")]
        {
            self.self_test_engine.all_required_tests_passed()
        }
        #[cfg(not(feature = "encrypt"))]
        {
            true
        }
    }

    /// 检查是否处于错误状态
    pub fn is_in_error_state(&self) -> bool {
        self.error_state.is_in_error_state()
    }

    /// 获取错误状态
    pub fn get_error_state(&self) -> Option<FipsError> {
        self.error_state.get_error()
    }

    /// 清除错误状态
    pub fn clear_error_state(&self) {
        self.error_state.clear_error()
    }

    /// 运行定期自检 (应该定期调用,例如每小时)
    pub fn run_periodic_self_tests(&self) -> Result<()> {
        if self.mode != FipsMode::Enabled {
            return Ok(());
        }

        #[cfg(feature = "encrypt")]
        {
            // 1. 运行 RNG 健康检查
            let rng_test = self.self_test_engine.rng_health_test()?;
            if !rng_test.passed {
                self.error_state.set_error(FipsError::RngHealthCheckFailed(
                    rng_test
                        .error_message
                        .unwrap_or_else(|| "Unknown RNG error".to_string()),
                ));
                return Err(CryptoError::FipsError(
                    "Periodic self test failed: RNG health check".to_string(),
                ));
            }

            // 2. 运行自检引擎中的其他定期测试
            self.self_test_engine.run_periodic_tests()?;
        }

        Ok(())
    }
}

/// 全局 FIPS 模式状态
static FIPS_MODE: std::sync::OnceLock<FipsMode> = std::sync::OnceLock::new();

/// 全局 FIPS 上下文
static FIPS_CONTEXT: std::sync::OnceLock<FipsContext> = std::sync::OnceLock::new();

/// 设置全局 FIPS 模式
fn set_fips_mode(mode: FipsMode) {
    let _ = FIPS_MODE.set(mode);
}

/// 获取全局 FIPS 模式
fn get_fips_mode() -> FipsMode {
    *FIPS_MODE.get().unwrap_or(&FipsMode::Disabled)
}

/// 获取全局 FIPS 上下文
pub fn get_fips_context() -> Option<&'static FipsContext> {
    FIPS_CONTEXT.get()
}

/// 设置全局 FIPS 上下文(仅用于测试)
#[cfg(test)]
#[allow(dead_code)]
pub fn set_fips_context(context: FipsContext) {
    let _ = FIPS_CONTEXT.set(context);
}

/// 初始化全局 FIPS 上下文
pub(crate) fn init_fips_context() -> Result<()> {
    let context = FipsContext::new(FipsMode::Enabled)?;
    let _ = FIPS_CONTEXT.set(context);
    Ok(())
}

/// 验证算法是否在 FIPS 模式下被允许
pub fn validate_algorithm_fips(_algorithm: &Algorithm) -> Result<()> {
    #[cfg(feature = "encrypt")]
    {
        if is_fips_enabled() {
            self::validator::FipsAlgorithmValidator::validate_fips_compliance(_algorithm)?;
        }
    }
    Ok(())
}

/// 检查是否启用 FIPS 模式
pub fn is_fips_enabled() -> bool {
    get_fips_mode() == FipsMode::Enabled
}

/// 获取 FIPS 批准的算法列表
pub fn get_fips_approved_algorithms() -> Vec<Algorithm> {
    #[cfg(feature = "encrypt")]
    {
        self::validator::FipsAlgorithmValidator::get_approved_algorithms()
    }
    #[cfg(not(feature = "encrypt"))]
    {
        vec![]
    }
}