kcipher2/lib.rs
1// SPDX-FileCopyrightText: 2026 Shun Sakai
2//
3// SPDX-License-Identifier: Apache-2.0 OR MIT
4
5//! The `kcipher2` crate is an implementation of the [KCipher-2] stream cipher
6//! as described in [RFC 7008].
7//!
8//! Cipher functionality is accessed using traits from re-exported [`cipher`]
9//! crate.
10//!
11//! # Examples
12//!
13//! ```
14//! use hex_literal::hex;
15//! use kcipher2::{
16//! KCipher2,
17//! cipher::{KeyIvInit, StreamCipher},
18//! };
19//!
20//! let key = [0x42; 16];
21//! let nonce = [0x24; 16];
22//! let plaintext = hex!("00010203 04050607 08090A0B 0C0D0E0F");
23//! let ciphertext = hex!("471694B5 EB93E4A6 EABA73DF A6F77057");
24//!
25//! // Key and IV must be references to the `Array` type. Here we use the `Into`
26//! // trait to convert arrays into it.
27//! let mut cipher = KCipher2::new(&key.into(), &nonce.into());
28//!
29//! let mut buf = plaintext;
30//!
31//! // Apply keystream (encrypt).
32//! cipher.apply_keystream(&mut buf);
33//! assert_eq!(buf, ciphertext);
34//!
35//! let ciphertext = buf;
36//!
37//! // Decrypt ciphertext by applying keystream again.
38//! let mut cipher = KCipher2::new(&key.into(), &nonce.into());
39//! cipher.apply_keystream(&mut buf);
40//! assert_eq!(buf, plaintext);
41//!
42//! // Stream ciphers can be used with streaming messages.
43//! let mut cipher = KCipher2::new(&key.into(), &nonce.into());
44//! for chunk in buf.chunks_mut(3) {
45//! cipher.apply_keystream(chunk);
46//! }
47//! assert_eq!(buf, ciphertext);
48//! ```
49//!
50//! [KCipher-2]: https://en.wikipedia.org/wiki/KCipher-2
51//! [RFC 7008]: https://datatracker.ietf.org/doc/html/rfc7008
52
53#![doc(html_root_url = "https://docs.rs/kcipher2/0.1.3/")]
54#![no_std]
55#![cfg_attr(docsrs, feature(doc_cfg))]
56// Lint levels of rustc.
57#![deny(missing_docs)]
58
59mod consts;
60mod kcipher2;
61mod utils;
62
63pub use cipher;
64
65pub use crate::kcipher2::{Iv, KCipher2, KCipher2Core, Key};