Skip to main content

scintia_96/
lib.rs

1#![no_std]
2#![cfg_attr(docsrs, feature(doc_cfg))]
3
4//! # Scintia-96
5//!
6//! A lightweight, keyed 96-bit permutation based on the Speck cipher.
7//!
8//! Scintia-96 is designed for use cases where you need a one-to-one mapping of a 96-bit input
9//! (such as a hardware unique ID) to a 96-bit output (such as a USB serial number),
10//! ensuring that uniqueness is preserved without collisions.
11//!
12//!
13//! ## Features
14//!
15//! - **`cipher`** (optional): Enables the RustCrypto cipher traits implementation for server environments.
16//!   This includes the `Scintia96Cipher` struct which precomputes the key schedule for efficiency
17//!   and enables **reversing (decrypting)** the permutation.
18//!
19//! ## Example
20//!
21//! ```rust
22//! use scintia_96::Scintia96;
23//!
24//! // 128-bit key (4 x u32)
25//! const KEY: [u32; 4] = [0x01020304, 0x05060708, 0x090a0b0c, 0x0d0e0f10];
26//! const PERMUTATION: Scintia96 = Scintia96::new(KEY);
27//!
28//! fn main() {
29//!     // 96-bit block as raw bytes
30//!     let mut block = [0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xfe, 0xed];
31//!     PERMUTATION.permute_block(&mut block);
32//! }
33//! ```
34//!
35//! For more advanced usage or server-side environments, enable the `cipher` feature to use `Scintia96Cipher`.
36
37mod key_schedule;
38mod scintia_96;
39pub(crate) mod utils;
40
41#[cfg(feature = "cipher")]
42mod block_cipher;
43
44pub use scintia_96::Scintia96;
45
46#[cfg(feature = "cipher")]
47pub use block_cipher::Scintia96Cipher;
48
49/// A trait for types that can perform the Scintia-96 96-bit keyed permutation.
50///
51/// Most users should use the inherent methods on [`Scintia96`] or [`Scintia96Cipher`]
52/// instead of this trait directly.
53pub trait Scintia96Permuter {
54    /// Permutes a 96-bit block represented as three 32-bit words.
55    fn permute(&self, block: [u32; 3]) -> [u32; 3];
56
57    /// Permutes a 96-bit block in place.
58    fn permute_block(&self, block: &mut [u8; 12]) {
59        let words = utils::bytes_to_words(block);
60        let out = self.permute(words);
61        utils::words_to_bytes(out, block);
62    }
63}
64
65/// A trait for types that can perform the inverse Scintia-96 96-bit keyed permutation.
66///
67/// Most users should use the inherent methods on [`Scintia96Cipher`] instead of this
68/// trait directly.
69pub trait Scintia96Unpermuter {
70    /// Un-permutes a 96-bit block represented as three 32-bit words.
71    fn unpermute(&self, block: [u32; 3]) -> [u32; 3];
72
73    /// Un-permutes a 96-bit block in place.
74    fn unpermute_block(&self, block: &mut [u8; 12]) {
75        let words = utils::bytes_to_words(block);
76        let out = self.unpermute(words);
77        utils::words_to_bytes(out, block);
78    }
79}
80
81pub(crate) const ROUNDS: u32 = 32;