veridian_kernel/crypto/
mod.rs1#![allow(dead_code)]
6
7pub mod asymmetric;
8pub mod cipher_suite;
9pub mod constant_time;
10pub mod hash;
11pub mod keystore;
12pub mod post_quantum;
13pub mod pq_params;
14pub mod random;
15pub mod symmetric;
16
17use crate::error::KernelError;
18
19pub(crate) fn init() -> Result<(), KernelError> {
21 crate::println!("[CRYPTO] Initializing cryptographic subsystem...");
22
23 random::init().map_err(|_| KernelError::InvalidState {
25 expected: "initialized",
26 actual: "failed_to_init_random",
27 })?;
28
29 keystore::init().map_err(|_| KernelError::InvalidState {
31 expected: "initialized",
32 actual: "failed_to_init_keystore",
33 })?;
34
35 crate::println!("[CRYPTO] Cryptographic subsystem initialized");
36 Ok(())
37}
38
39pub(crate) type CryptoResult<T> = Result<T, CryptoError>;
41
42#[derive(Debug, Clone, Copy, PartialEq, Eq)]
44pub(crate) enum CryptoError {
45 InvalidKeySize,
46 InvalidNonceSize,
47 InvalidTagSize,
48 EncryptionFailed,
49 DecryptionFailed,
50 SignatureFailed,
51 VerificationFailed,
52 KeyGenerationFailed,
53 InvalidKey,
54 InsufficientEntropy,
55}
56
57pub(crate) fn validate() -> bool {
61 let expected: [u8; 32] = [
65 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22,
66 0x23, 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c, 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00,
67 0x15, 0xad,
68 ];
69
70 let result = hash::sha256(b"abc");
71 result.as_bytes() == &expected
72}
73
74impl core::fmt::Display for CryptoError {
75 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
76 match self {
77 CryptoError::InvalidKeySize => write!(f, "Invalid key size"),
78 CryptoError::InvalidNonceSize => write!(f, "Invalid nonce size"),
79 CryptoError::InvalidTagSize => write!(f, "Invalid authentication tag size"),
80 CryptoError::EncryptionFailed => write!(f, "Encryption failed"),
81 CryptoError::DecryptionFailed => write!(f, "Decryption failed"),
82 CryptoError::SignatureFailed => write!(f, "Signature generation failed"),
83 CryptoError::VerificationFailed => write!(f, "Signature verification failed"),
84 CryptoError::KeyGenerationFailed => write!(f, "Key generation failed"),
85 CryptoError::InvalidKey => write!(f, "Invalid cryptographic key"),
86 CryptoError::InsufficientEntropy => write!(f, "Insufficient entropy"),
87 }
88 }
89}