Skip to main content

Crate hc_256

Crate hc_256 

Source
Expand description

§RustCrypto: HC-256 Stream Cipher

Crate Docs Build Status Apache2/MIT licensed Rust Version Project Chat HAZMAT

Implementation of the HC-256 stream cipher.

§⚠️ Security Warning: Hazmat!

This crate does not ensure ciphertexts are authentic (i.e. by using a MAC to verify ciphertext integrity), which can lead to serious vulnerabilities if used incorrectly!

No security audits of this crate have ever been performed, and it has not been thoroughly assessed to ensure its operation is constant-time on common CPU architectures.

USE AT YOUR OWN RISK!

§Examples

use hc_256::Hc256;
use hc_256::cipher::{KeyIvInit, StreamCipher};
use hex_literal::hex;

let key = [0x42; 32];
let nonce = [0x24; 32];
let plaintext = hex!("000102030405060708090A0B0C0D0E0F");
let ciphertext = hex!("ca982177325cd40ebc208045066c420f");

// Key and IV must be references to the `Array` type.
// Here we use the `Into` trait to convert arrays into it.
let mut cipher = Hc256::new(&key.into(), &nonce.into());

let mut buffer = plaintext;

// apply keystream (encrypt)
cipher.apply_keystream(&mut buffer);
assert_eq!(buffer, ciphertext);

let ciphertext = buffer;

// decrypt ciphertext by applying keystream again
let mut cipher = Hc256::new(&key.into(), &nonce.into());
cipher.apply_keystream(&mut buffer);
assert_eq!(buffer, plaintext);

// stream ciphers can be used with streaming messages
let mut cipher = Hc256::new(&key.into(), &nonce.into());
for chunk in buffer.chunks_mut(3) {
    cipher.apply_keystream(chunk);
}
assert_eq!(buffer, ciphertext);

§License

Licensed under either of:

at your option.

§Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Re-exports§

pub use cipher;

Structs§

Hc256Core
Core state of the HC-256 stream cipher.

Type Aliases§

Hc256
The HC-256 stream cipher.
Iv
HC-256 stream cipher initialization vector.
Key
HC-256 stream cipher key.