capsule_core/crypto/mod.rs
1//! Capsule's cryptographic data plane.
2//!
3//! Every primitive identity (hash, KDF, AEAD, signature scheme, suite id) is the
4//! single source of truth declared in the design docs under `design/cryptography/`
5//! and pinned in code by [`primitives`]. Submodules are layered strictly in
6//! dependency order:
7//!
8//! ```text
9//! hash · primitives · rng · kdf · pwkdf (foundation, no internal deps)
10//! └─ keys ─ encryption (key hierarchy + AEAD)
11//! └─ keys ─ membership (the owner-signed album roster)
12//! └─ authority ─┐
13//! └─ provenance ┴─ verify_asset (the single acknowledgement chokepoint)
14//! └─ verify_derivative (its derivative-manifest arm)
15//! ```
16//!
17//! The cryptographic layer is deliberately self-contained and side-effect-free
18//! (no network, no global state), so the whole data plane is unit-testable offline
19//! against RFC / FIPS known-answer vectors and exhaustive negative cases.
20
21pub mod authority;
22pub mod encryption;
23pub mod hash;
24pub mod kdf;
25pub mod keys;
26pub mod membership;
27pub mod primitives;
28pub mod provenance;
29pub mod pwkdf;
30pub mod receipts;
31pub mod revoke;
32pub mod rng;
33pub mod storage_attestation;
34pub mod upgrade;
35pub mod verify_asset;
36pub mod verify_derivative;
37
38// Every submodule above is the one public path to its items: none is re-exported here, so a
39// type has exactly one name (`crypto::hash::Hash32`, never also `crypto::Hash32`).
40use thiserror::Error;
41
42/// Errors surfaced by the cryptographic layer.
43///
44/// Variants are intentionally coarse and carry a `&'static str` reason code rather
45/// than free-form strings, so a rejection is greppable in logs and stable across
46/// refactors (see [Threat Model — Validation] structured reason codes).
47///
48/// [Threat Model — Validation]: https://docs/design/threat-model/validation/
49#[derive(Debug, Error, PartialEq, Eq)]
50pub enum CryptoError {
51 /// An AEAD open / signature verification failed authentication.
52 #[error("authentication failed: {0}")]
53 Auth(&'static str),
54
55 /// A structure declared a `crypto_suite_id` this build does not implement.
56 #[error("unknown crypto suite id: {0:#06x}")]
57 UnknownSuite(u16),
58
59 /// A wire/on-disk structure was malformed (wrong length, bad framing, ...).
60 #[error("malformed input: {0}")]
61 Malformed(&'static str),
62
63 /// A key could not be derived, decoded, or reconstructed from bytes.
64 #[error("key error: {0}")]
65 Key(&'static str),
66
67 /// A re-keying writer drew a fresh nonce equal to the one it supersedes — refused as
68 /// defense in depth on top of the CSPRNG draw (SSoT: [Encryption § Re-keying on Rewrite]).
69 ///
70 /// [Encryption § Re-keying on Rewrite]: https://docs/design/cryptography/encryption/#re-keying-on-rewrite
71 #[error("refusing to reuse the nonce of the record being replaced")]
72 NonceReuse,
73}