kk_crypto/lib.rs
1// Copyright (c) 2026 John A Keeney, Entrouter. All rights reserved.
2// Licensed under the Apache License, Version 2.0 with Additional Terms.
3// NO COMMERCIAL USE without prior written authorization from Entrouter.
4// Unauthorized commercial use will be prosecuted to the fullest extent of the law.
5// See the LICENSE file in the project root for full license information.
6// NOTICE: Removal of this header is a violation of the license.
7
8//! # KK, Keeney Kode
9//!
10//! A novel cryptographic primitive where symbol values are temporal
11//! functions of universal entropy.
12//!
13//! ## Core Principle
14//!
15//! In all existing cryptography, symbol 'A' has a fixed value and encryption
16//! hides what 'A' means. In KK, symbol 'A' has no fixed value:
17//!
18//! ```text
19//! KK(S) = S^ε where ε = universal entropy at moment of creation
20//! ```
21//!
22//! The symbol's fundamental value is a function of the universe
23//! at the instant it was born. The same symbol encoded twice produces
24//! two cryptographically unrelated values.
25//!
26//! ## Quick Start
27//!
28//! ```rust
29//! use kk_crypto::{encode, decode};
30//!
31//! // Both parties share a secret
32//! let shared_secret = b"our-shared-secret";
33//!
34//! // Encode: symbol values become functions of this cosmic instant
35//! let packet = encode(shared_secret, b"Hello KK!").unwrap();
36//!
37//! // Transmit packet.to_bytes() to receiver...
38//!
39//! // Decode: same secret, same moment reference, same values
40//! let plaintext = decode(shared_secret, &packet).unwrap();
41//! assert_eq!(plaintext, b"Hello KK!");
42//! ```
43//!
44//! ## Architecture
45//!
46//! ```text
47//! Entropy Sources → KK-Mix → Per-Symbol Derivation → Temporal Binding → Encoding
48//! (entropy.rs) (kk_mix.rs) (kdf.rs) (temporal.rs) (codec.rs)
49//! ```
50//!
51//! Every cryptographic operation is built from a single novel primitive:
52//! the KK permutation (Multiply-Fold-Rotate sponge construction).
53//! No SHA-256, no HKDF, no HMAC, 100% original KK.
54//!
55//! ## Security Model
56//!
57//! **Threat model:** KK assumes a pre-shared secret between sender and
58//! receiver. An attacker may observe, replay, or modify ciphertext in
59//! transit but does not know the shared secret.
60//!
61//! **Confidentiality:** Each encoding captures a unique `EntropySnapshot`
62//! (CPU counters, thread jitter, OS randomness). The snapshot feeds the
63//! KK-KDF to derive per-chunk keystream, ensuring the same plaintext
64//! never produces the same ciphertext twice.
65//!
66//! **Integrity:** Every `KkPacket` carries a KK-MAC tag over
67//! (ciphertext ‖ entropy snapshot). `decode` rejects any packet whose
68//! tag does not verify, preventing silent tampering.
69//!
70//! **Temporal binding:** The `TemporalCommitment` in each packet commits
71//! to the entropy used during encoding. The receiver re-derives the
72//! commitment from the embedded snapshot and the shared secret, rejecting
73//! packets if the commitment does not match.
74//!
75//! **Key hygiene:** Intermediate keys (commit keys, chunk keystream) are
76//! zeroized via the `zeroize` crate immediately after use. The output
77//! buffer is zeroized on error paths to prevent partial plaintext leaks.
78//!
79//! **Limitations:**
80//! - KK is a novel, un-audited primitive, it has **not** been reviewed
81//! by third-party cryptographers. Do not use for production security.
82//! - The base codec has no forward secrecy. Use the `session` module's
83//! Rope Ratchet (`encode_session`/`decode_session`) for ~192-bit
84//! forward secrecy via 4-strand ratcheting.
85//! - Replay protection is **not** built in; callers must add sequence
86//! numbers or timestamps at the protocol layer.
87//!
88//! J.A. Keeney, Australia, 2026
89
90#![cfg_attr(not(feature = "std"), no_std)]
91
92#[cfg(not(feature = "std"))]
93extern crate alloc;
94
95#[cfg(feature = "std")]
96pub mod codec;
97#[cfg(feature = "cuda")]
98pub mod cuda;
99#[cfg(feature = "std")]
100pub mod eka;
101#[cfg(feature = "std")]
102pub mod entropy;
103#[cfg(feature = "std")]
104pub mod entropy_pool;
105pub mod error;
106#[cfg(feature = "gpu")]
107pub mod gpu;
108#[cfg(feature = "std")]
109pub mod kdf;
110pub mod kk_mix;
111#[cfg(all(target_arch = "x86_64", feature = "std"))]
112pub(crate) mod kk_mix_avx512;
113#[cfg(feature = "std")]
114pub mod qkd;
115pub mod rng;
116#[cfg(feature = "std")]
117pub mod session;
118#[cfg(feature = "std")]
119pub mod temporal;
120
121// Re-export the primary API
122#[cfg(feature = "std")]
123pub use codec::{decode, encode, KkPacket};
124#[cfg(feature = "std")]
125pub use codec::{decode_aead, encode_aead, KkAeadPacket};
126#[cfg(feature = "std")]
127pub use codec::{decode_aead_batch, encode_aead_batch};
128#[cfg(feature = "std")]
129pub use codec::{decode_bound, encode_bound, KkBoundPacket};
130#[cfg(feature = "std")]
131pub use codec::{decode_parallel, encode_parallel, KkParallelPacket, PARALLEL_CHUNK_SIZE};
132#[cfg(feature = "std")]
133pub use codec::{decode_split, encode_split, KkSealedMessage};
134#[cfg(feature = "std")]
135pub use codec::{encode_aead_pooled, encode_pooled};
136#[cfg(feature = "std")]
137#[doc(hidden)]
138pub use codec::{encode_aead_with_snapshot, encode_with_snapshot};
139#[cfg(feature = "std")]
140pub use codec::{StreamDecoder, StreamEncoder};
141#[cfg(feature = "std")]
142pub use entropy::EntropySnapshot;
143#[cfg(feature = "std")]
144pub use entropy_pool::EntropyPool;
145pub use error::KkError;
146#[cfg(feature = "std")]
147pub use temporal::{generate_challenge, TemporalProof, GENESIS_MAC};
148
149// Session (forward secrecy) re-exports
150#[cfg(feature = "std")]
151pub use session::{decode_session, encode_session, RopePacket, RopeRatchet, RopeStep};
152#[cfg(feature = "std")]
153pub use session::{decode_session_aead, encode_session_aead, RopeAeadPacket};
154
155// QKD re-exports
156#[cfg(feature = "std")]
157pub use qkd::{
158 alice_prepare, bob_measure, decrypt_epsilon, distill_key, encrypt_epsilon, eve_intercept,
159 Basis, Bb84Result, Qubit,
160};
161
162// EKA (Entropy Key Agreement) re-exports
163#[cfg(feature = "std")]
164pub use eka::{EkaInitiator, EkaMsg1, EkaMsg2, EkaMsg3, EkaResponder};
165
166// RNG re-exports
167pub use rng::KkRng;
168#[cfg(feature = "std")]
169pub use rng::KkRngPool;