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
//! # arcana — classical cryptography for the `krypteia` workspace
//!
//! Pure-Rust implementations of widely-used classical primitives —
//! hash functions, symmetric ciphers, AEAD modes, RSA, ECDSA / ECDH,
//! EdDSA, X25519, and X448 — sharing the same side-channel
//! countermeasure toolkit ([`silentops`](https://docs.rs/silentops))
//! used by the post-quantum side of the workspace
//! ([`quantica`](https://docs.rs/quantica)).
//!
//! Zero external runtime dependencies (only `std` / `alloc` and the
//! workspace-local `silentops`); constant-time on the data path.
//!
//! # Algorithms
//!
//! ## Hash functions ([`hash`])
//!
//! | Algorithm | Output | Module |
//! |--------------|---------|-------------------------------------------------|
//! | SHA-1 | 160 b | [`hash::sha1::Sha1`] (legacy) |
//! | SHA-224 | 224 b | [`hash::sha224::Sha224`] |
//! | SHA-256 | 256 b | [`hash::sha256::Sha256`] |
//! | SHA-384 | 384 b | [`hash::sha384::Sha384`] |
//! | SHA-512 | 512 b | [`hash::sha512::Sha512`] |
//! | SHA-512/224 | 224 b | [`hash::sha512_trunc::Sha512_224`] |
//! | SHA-512/256 | 256 b | [`hash::sha512_trunc::Sha512_256`] |
//! | SHA3-224 | 224 b | [`hash::sha3::Sha3_224`] |
//! | SHA3-256 | 256 b | [`hash::sha3::Sha3_256`] |
//! | SHA3-384 | 384 b | [`hash::sha3::Sha3_384`] |
//! | SHA3-512 | 512 b | [`hash::sha3::Sha3_512`] |
//! | SHAKE128 | XOF | [`hash::sha3::Shake128`] |
//! | SHAKE256 | XOF | [`hash::sha3::Shake256`] |
//! | cSHAKE128 | XOF | [`hash::sha3::CShake128`] |
//! | cSHAKE256 | XOF | [`hash::sha3::CShake256`] |
//! | BLAKE2b | 1-512 b | [`hash::blake2::Blake2b`] |
//! | BLAKE2s | 1-256 b | [`hash::blake2::Blake2s`] |
//! | RIPEMD-160 | 160 b | [`hash::ripemd160::Ripemd160`] (legacy) |
//!
//! ## Symmetric ciphers and modes ([`cipher`])
//!
//! | Algorithm | Module |
//! |-----------------------------------|----------------------------------------------|
//! | AES-128 / 192 / 256 | [`cipher::aes`] |
//! | DES, Triple-DES (EDE) | [`cipher::des`] |
//! | ECB, CBC, CTR, GCM modes | [`cipher::modes`] |
//! | **AES-CCM** (RFC 3610) AEAD | [`cipher::ccm`] |
//! | **AES-XTS** (IEEE 1619) disk crypto| [`cipher::xts`] |
//! | **ChaCha20** (RFC 8439) | [`cipher::chacha20`] |
//! | **Poly1305** one-time MAC | [`cipher::poly1305`] |
//! | **ChaCha20-Poly1305** AEAD | [`cipher::chacha20poly1305`] |
//! | **XChaCha20-Poly1305** AEAD | [`cipher::xchacha20poly1305`] (24-byte nonce) |
//!
//! In addition to these one-shot, function-oriented APIs, a
//! stateful **streaming `Cipher` object** lives in
//! [`cipher::ctx`]. It wraps the AES / DES / 3DES block ciphers
//! with the ECB / CBC / CTR modes behind a uniform
//! `init / update / finalize` cycle, with caller-provided output
//! buffers (no allocation required) and configurable padding
//! (`None`, `Pkcs7`, `Iso9797M1`, `Iso9797M2`, `AnsiX923`). AEAD
//! modes intentionally stay function-oriented to avoid releasing
//! unverified plaintext during streaming decryption.
//!
//! ## Message authentication codes ([`mac`])
//!
//! A streaming MAC object lives in [`mac::ctx`], exposing a
//! uniform `init / update / sign | verify` cycle across four
//! families:
//!
//! | Family | Algorithms | Standard |
//! |--------------|------------------------------------------------------------------|-------------------|
//! | HMAC | SHA-1, SHA-256, SHA-384, SHA-512, SHA3-256/384/512, RIPEMD-160 | RFC 2104, FIPS 198-1 |
//! | CMAC | AES-128 / 192 / 256, Triple-DES | NIST SP 800-38B, RFC 4493 |
//! | KMAC | KMAC128, KMAC256 | FIPS SP 800-185 |
//! | GMAC | AES-128 / 192 / 256 | NIST SP 800-38D |
//!
//! Three init variants distinguish families that need different
//! inputs: `init(key)` (HMAC, CMAC, KMAC default),
//! `init_kmac(key, custom)` (KMAC with explicit customization
//! string), and `init_with_nonce(key, nonce)` (GMAC, 12-byte
//! unique nonce). `verify` accepts truncated tags and runs in
//! constant time. Poly1305 is intentionally **not** routed
//! through `Mac` because it is a one-time MAC and would be unsafe
//! behind an "init then reuse" object.
//!
//! ## RSA ([`rsa`])
//!
//! | Padding | Module |
//! |-----------------------------------|----------------------------------------------|
//! | PKCS#1 v1.5 encryption + signature| [`rsa::pkcs1`] (8 hash functions supported) |
//! | OAEP encryption (PKCS#1 v2.2) | [`rsa::oaep`] |
//! | RSASSA-PSS signature (PKCS#1 v2.2)| [`rsa::pss`] |
//!
//! ## Elliptic curve cryptography ([`ecc`])
//!
//! ECDSA / ECDH / SEC1-compressed / signature DER are all unified
//! behind a single [`ecc::curves::Curve`] trait, implemented by:
//!
//! | Wrapper | Curve |
//! |-------------------------------------------|--------------------|
//! | [`ecc::curves::P256`] | NIST P-256 |
//! | [`ecc::curves::P384`] | NIST P-384 |
//! | [`ecc::curves::P521`] | NIST P-521 |
//! | [`ecc::curves::Secp256k1`] | secp256k1 (SECG) |
//! | [`ecc::curves::BrainpoolP256r1`] | BSI / RFC 5639 |
//! | [`ecc::curves::BrainpoolP384r1`] | BSI / RFC 5639 |
//! | [`ecc::curves::BrainpoolP512r1`] | BSI / RFC 5639 |
//!
//! Edwards / Montgomery curves live in standalone modules because
//! their algebraic shape doesn't fit the short-Weierstrass `Curve`
//! trait:
//!
//! | Algorithm | Module |
//! |-----------------------------------|-----------------------|
//! | Ed25519, Ed25519ctx, Ed25519ph | [`ecc::eddsa`] |
//! | X25519 ECDH (Curve25519) | [`ecc::x25519`] |
//! | X448 ECDH (Curve448) | [`ecc::x448`] |
//!
//! Ed448 is planned but not yet implemented.
//!
//! # Cargo features
//!
//! | Feature | Default | Effect |
//! |----------------------|---------|---------------------------------------------------------|
//! | `std` | no | Reserved for future `no_std` work (currently a no-op). |
//! | `rust-crypto-traits` | no | Pulls in `digest 0.10` / `cipher 0.4` / `signature 2.0` and activates the `bridge` module, which wraps every hash in a `digest::Digest` impl for ecosystem interop (HMAC, HKDF, PBKDF2, Argon2, ...). |
//!
//! Default builds are **zero-dependency** (only the workspace-local
//! `silentops` crate). The `rust-crypto-traits` feature is opt-in
//! and pulls in definitions only (no crypto code).
//!
//! # Quick start
//!
//! ```rust
//! // SHA-256 one-shot hash
//! use arcana::hash::sha256::Sha256;
//! use arcana::Hasher;
//! let digest = Sha256::hash(b"hello, arcana");
//! assert_eq!(digest.len(), 32);
//! ```
//!
//! ```rust
//! // AES-128-GCM AEAD
//! use arcana::cipher::aes::Aes128;
//! use arcana::cipher::modes::Gcm;
//! use arcana::BlockCipher;
//!
//! let key = [0x42u8; 16];
//! let nonce = [0u8; 12];
//! let cipher = Aes128::new(&key);
//! let (ct, tag) = Gcm::encrypt(&cipher, &nonce, b"aad", b"plaintext");
//! let pt = Gcm::decrypt(&cipher, &nonce, b"aad", &ct, &tag).unwrap();
//! assert_eq!(pt, b"plaintext");
//! ```
//!
//! ```rust
//! // X25519 ECDH key exchange
//! use arcana::ecc::x25519::{x25519_derive_public, x25519_ecdh};
//! let alice_sk = [0x77u8; 32];
//! let bob_sk = [0x88u8; 32];
//! let alice_pk = x25519_derive_public(&alice_sk);
//! let bob_pk = x25519_derive_public(&bob_sk);
//! let shared_a = x25519_ecdh(&alice_sk, &bob_pk);
//! let shared_b = x25519_ecdh(&bob_sk, &alice_pk);
//! assert_eq!(shared_a, shared_b);
//! ```
//!
//! # Examples
//!
//! ```sh
//! cargo run -p arcana --release --example hash_demo
//! cargo run -p arcana --release --example aes_demo
//! cargo run -p arcana --release --example rsa_demo
//! cargo run -p arcana --release --example ecdsa_demo
//! cargo run -p arcana --release --example eddsa_demo
//! cargo run -p arcana --release --example x25519_demo
//! ```
//!
//! # Side-channel guarantees
//!
//! The full threat model and per-algorithm countermeasure roadmap
//! lives in `arcana/doc/sca/` (HTML rendered by `./gendoc.sh
//! arcana`). The roadmap items referenced here (`T1-A`, `T1-C`,
//! `T2-D` …) are defined there. This section is a quick-reference
//! summary; for evaluation evidence, refer to the annex.
//!
//! ## Always-on
//!
//! | Defence | Scope |
//! |------------------------------------|----------------------------------------------------------------------------------------|
//! | Constant-time tag comparison | All AEAD decrypt, MAC verify, ECDSA verify (via `silentops::ct_eq`) |
//! | Constant-time field arithmetic | ECC (P-256, P-384, P-521, secp256k1, Brainpool), Ed25519, X25519, X448 |
//! | **CT Montgomery ladder** | [`ecc::curve::scalar_mul_point`] — branch-free across all 7 short-Weierstrass curves |
//! | `core::hint::black_box` shielding | [`ecc::field`] mask selects, to keep LLVM from recovering branches at `opt-level >= 2` |
//! | No secret-dependent branches | Hash, symmetric, HMAC, CMAC, KMAC, GMAC |
//! | RFC 6979 deterministic nonce | ECDSA — eliminates nonce-reuse attacks (but see fault-attack note below) |
//! | `silentops::ct_zeroize` available | Caller can zeroize sensitive buffers explicitly |
//!
//! ## Open vulnerabilities (evaluation gaps — track in roadmap)
//!
//! | Primitive | Issue | Roadmap item |
//! |---------------------|----------------------------------------------------------------------|--------------|
//! | AES S-box | Table-based lookup — cache-line leaks on CPUs with shared L1 | `T1-A` (fixsliced AES port from Adomnicai-Peyrin TCHES 2021/1) |
//! | RSA-CRT decrypt | Bellcore single-fault → factorisation of `N` | `T1-C` (Aumüller 2002, formally verified) |
//! | RSA bigint | `Montgomery_mul`, `cmp`, `pow_mod` not formally CT-audited | `T1-E` |
//! | ECDSA / ECDH | Minerva-class bit-length leak audit not yet completed | `T1-B` |
//! | Ed25519 / ECDSA-RFC 6979 | Single-fault on deterministic signing → key recovery | `T1-D` (hedged signatures, CFRG draft) |
//! | HMAC-SHA-2 | Carry-based DPA breaks unmasked HMAC-SHA-2 in 30 K – 275 K traces | `T2-D` (first-order Boolean masking) |
//! | DES / 3DES S-boxes | Table-based; legacy only | unscoped — avoid on SCA-sensitive targets |
//!
//! ## Not yet implemented
//!
//! - **Zeroize-on-Drop**: typed key wrappers ([`ecc::curves::SecretKey`],
//! [`ecc::eddsa::Ed25519SecretKey`], [`rsa::rsa::RsaSecretKey`])
//! currently **do not** implement `Drop` with
//! `silentops::ct_zeroize`. Callers should zeroize sensitive
//! buffers explicitly. Roadmap item `T2-E`.
//! - **DPA / EM / multi-fault**: out of scope for the current
//! evaluation profile; tier-4 items in the SCA annex.
//!
//! # Native API vs RustCrypto bridge
//!
//! By default, this crate exposes its own trait hierarchy
//! ([`Hasher`], [`Xof`], [`BlockCipher`], ...). Enable
//! `rust-crypto-traits` to additionally activate the `bridge`
//! module which implements `digest::Digest` for the hash
//! functions. The two universes are intentionally separate so the
//! native modules never depend on a specific external crate
//! version.
// ============================================================
// Our own trait definitions (zero-dependency)
// ============================================================
/// Trait for hash functions (fixed-output).
/// Trait for extendable-output functions (XOF) such as SHAKE128 / SHAKE256.
///
/// Unlike a fixed-output [`Hasher`], an XOF can be `squeeze`-d for any
/// number of bytes after absorption is complete. The internal sponge
/// state is mutated by every `squeeze` call, so successive squeezes
/// extend the output stream rather than restart it.
/// Trait for symmetric block ciphers operating on a fixed block size.
///
/// Implementors include the AES family ([`cipher::Aes`], [`cipher::Aes128`],
/// [`cipher::Aes192`], [`cipher::Aes256`]) and the DES family
/// ([`cipher::Des`], [`cipher::TripleDes`]).
/// Trait for digital signature schemes that produce fixed-shape keys
/// and signatures (no per-call hash parameter).
///
/// **Note**: this trait is currently informational. Most signature
/// schemes in this crate (ECDSA, EdDSA, RSA-PSS, RSA-PKCS1) take a
/// hash function as a generic or runtime parameter and therefore do
/// not implement this trait directly -- they expose their own
/// curve/key types and sign/verify functions. The trait is kept as
/// the canonical "shape" of the simplest signature scheme for
/// future extensions and for documentation purposes.
/// Trait for public-key encryption schemes.
///
/// Like [`SignatureScheme`], this trait is currently informational --
/// the RSA encryption helpers in [`rsa::pkcs1`] and [`rsa::oaep`] take
/// a parameter of type [`rsa::rsa::RsaPublicKey`] / `RsaSecretKey`
/// directly rather than going through this trait, because OAEP and
/// PKCS#1 v1.5 each take additional protocol parameters (label, RNG)
/// that don't fit the simple shape below.
// ============================================================
// Modules
// ============================================================
/// Hash functions: SHA-1, SHA-2, SHA-3, RIPEMD-160.
/// Symmetric ciphers: AES, DES, 3DES.
/// Elliptic curve cryptography: ECDSA, EdDSA.
/// RSA encryption and signatures.
/// Message authentication codes (HMAC, CMAC, KMAC, GMAC).
/// Key serialization: DER, PEM, PKCS#1, PKCS#8, SEC1, SPKI.
/// RustCrypto trait bridges (feature-gated).