Skip to main content

dcrypt_algorithms/
lib.rs

1//! Cryptographic primitive implementations and adapters.
2//!
3//! This crate provides implementations of various cryptographic primitives
4//! with explicit validation and zeroization boundaries. Constant-time behavior
5//! and `no_std` support are primitive- and feature-specific; the crate does not
6//! make blanket guarantees for either property.
7//!
8//! # Security Features
9//!
10//! Relevant defensive patterns include:
11//!
12//! - Secure memory handling with automatic zeroization
13//! - Constant-time comparison operations
14//! - Memory barrier utilities
15//! - Secure operation patterns
16
17#![cfg_attr(not(feature = "std"), no_std)]
18#![forbid(unsafe_code)]
19#![deny(missing_docs)]
20
21#[cfg(feature = "alloc")]
22#[macro_use]
23extern crate alloc;
24
25#[cfg(feature = "alloc")]
26mod alloc_prelude {
27    pub(crate) use alloc::{
28        string::{String, ToString},
29        vec::Vec,
30    };
31}
32
33// Error module and re-exports
34pub mod error;
35pub use error::{validate, Error, Result};
36
37// Block cipher implementations
38pub mod block;
39pub use block::{Aes128, Aes192, Aes256, Cbc, Ctr};
40
41// Hash function implementations
42pub mod hash;
43pub use hash::{
44    Blake2b, Blake2s, Keccak256, Sha1, Sha224, Sha256, Sha384, Sha3_224, Sha3_256, Sha3_384,
45    Sha3_512, Sha512, Shake128, Shake256,
46};
47
48// AEAD cipher implementations
49#[cfg(feature = "alloc")]
50pub mod aead;
51#[cfg(feature = "alloc")]
52pub use aead::{AeadCipher, ChaCha20Poly1305, ChaCha20Poly1305Cipher, Gcm, XChaCha20Poly1305};
53
54// MAC implementations
55pub mod mac;
56pub use mac::{Hmac, Poly1305};
57
58// Stream cipher implementations
59pub mod stream;
60pub use stream::chacha::chacha20::ChaCha20;
61
62// KDF implementations
63#[cfg(feature = "alloc")]
64pub mod kdf;
65#[cfg(feature = "alloc")]
66pub use kdf::{Argon2, Hkdf, KeyDerivationFunction, PasswordHashFunction, Pbkdf2};
67
68// Elliptic Curve primitives
69pub mod ec;
70pub use ec::{
71    // Re-export curve-specific modules
72    p256,
73    p384,
74    p521,
75    // Re-export common EC types
76    P256Point,
77    P256Scalar,
78    P384Point,
79    P384Scalar,
80    P521Point,
81    P521Scalar,
82};
83
84// Type system
85pub mod types;
86pub use types::{
87    ByteSerializable, ConstantTimeEq, Digest, FixedSize, Nonce, RandomGeneration, Salt,
88    SecretBytes, SecureZeroingType, Tag,
89};
90
91// Re-export security types from dcrypt-core
92pub use dcrypt_common::security::{
93    barrier, EphemeralSecret, SecretBuffer, SecretVec, SecureCompare, ZeroizeGuard,
94};
95
96// Algorithm types and compatibility traits
97pub use types::{
98    // Algorithm marker types
99    algorithms::{
100        Aes128 as Aes128Algorithm, Aes256 as Aes256Algorithm, ChaCha20 as ChaCha20Algorithm,
101        ChaCha20Poly1305 as ChaCha20Poly1305Algorithm, Ed25519 as Ed25519Algorithm,
102        P521 as P521Algorithm, X25519 as X25519Algorithm,
103    },
104
105    digest::{Blake2bCompatible, Keccak256Compatible, Sha256Compatible, Sha512Compatible},
106    // Key types
107    key::{AsymmetricPublicKey, AsymmetricSecretKey, SymmetricKey},
108
109    // Compatibility traits for specific algorithms
110    nonce::{AesCtrCompatible, AesGcmCompatible, ChaCha20Compatible, XChaCha20Compatible},
111    salt::{Argon2Compatible, HkdfCompatible, Pbkdf2Compatible},
112    tag::{ChaCha20Poly1305Compatible, GcmCompatible, HmacCompatible, Poly1305Compatible},
113};
114
115// XOF implementations (if enabled)
116#[cfg(feature = "xof")]
117pub mod xof;
118#[cfg(feature = "xof")]
119pub use xof::{Blake3Xof, ExtendableOutputFunction, ShakeXof128, ShakeXof256};
120
121// **NEW** PQC Math Primitive Modules
122#[cfg(feature = "alloc")] // Polynomial arithmetic often benefits from dynamic allocation
123pub mod poly;
124
125// Re-export polynomial types for easier access
126#[cfg(feature = "alloc")]
127pub use poly::{
128    ntt::{montgomery_reduce, CooleyTukeyNtt, InverseNttOperator, NttOperator},
129    params::{MlDsaParams, Modulus, NttModulus},
130    polynomial::Polynomial,
131    prelude,
132    sampling::{CbdSampler, DefaultSamplers, UniformSampler},
133    serialize::{CoefficientPacker, CoefficientUnpacker, DefaultCoefficientSerde},
134};