The implementation of cryptographic key chains using three primitives, i.e., HKDF, PRG, and XDRBG, in Rust v1.87.0.
use keychains_rs::{HashFunc::Sha256, key_chains::hkdf_keychain::HkdfKeyChain};
use rand::{RngCore, rngs::OsRng};
fn main() {
let output_length: usize = 64;
let hkdf_kc_obj: HkdfKeyChain = HkdfKeyChain::new(Sha256, Some(output_length), None, None).unwrap();
let mut initial_skm: [u8; 32] = [0u8; 32];
OsRng::fill_bytes(&mut OsRng, &mut initial_skm);
let kc_init_state: Vec<u8> = hkdf_kc_obj.key_chain_instantiate(&initial_skm, None, None).unwrap();
let mut input_param_1: [u8; 45] = [0u8; 45];
OsRng::fill_bytes(&mut OsRng, &mut input_param_1);
let (new_state_1, output_key_1) = hkdf_kc_obj.key_chain_update(&input_param_1, &kc_init_state, None, None).unwrap();
println!("First Key in the key chain using {:?}: {:?}",Sha256, output_key_1);
let mut input_param_2: [u8; 32] = [0u8; 32];
OsRng::fill_bytes(&mut OsRng, &mut input_param_2);
let (_new_state_2, output_key_2) = hkdf_kc_obj.key_chain_update(&input_param_2, &new_state_1, None, None).unwrap();
println!("Second Key in the key chain using {:?}: {:?}",Sha256, output_key_2);
// Similarly generate more output keys in the key chain
}For more insights, see examples.
The package comes with a default HashMap-based storage for quick prototyping. But a user should implement the Storage trait for their choice of storage backend.