logo
Expand description

Implementation of the HC-256 stream cipher.

Cipher functionality is accessed using traits from re-exported cipher crate.

⚠️ Security Warning: Hazmat!

This crate does not ensure ciphertexts are authentic! Thus ciphertext integrity is not verified, which can lead to serious vulnerabilities!

USE AT YOUR OWN RISK!

Example

use hc_256::Hc256;
// Import relevant traits
use hc_256::cipher::{KeyIvInit, StreamCipher};
use hex_literal::hex;

let key = [0x42; 32];
let nonce = [0x24; 32];
let plaintext = hex!("00010203 04050607 08090A0B 0C0D0E0F");
let ciphertext = hex!("ca982177 325cd40e bc208045 066c420f");

// Key and IV must be references to the `GenericArray` 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.clone();

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

let ciphertext = buffer.clone();

// 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);

Re-exports

pub use cipher;

Structs

The HC-256 stream cipher core

Type Definitions

The HC-256 stream cipher core