1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//! HMAC-based Hierarchical Key Derivation: deterministically derive a
//! hierarchy of symmetric keys from initial keying material through
//! repeated applications of the Hash-based Message Authentication Code
//! (HMAC) construction.
//!
//! This library implements a fully symmetric construction inspired by
//! [BIP-0032: Hierarchical Deterministic Wallets][bip32].
//!
//! # Usage
//!
//! To derive a key using HKD32, you'll need the following:
//!
//! - `hkd32::KeyMaterial`: a 32-byte (256-bit) uniformly random value
//! - `hkd32::Path` or `hkd32::PathBuf`: path to the child key
//!
//! Derivation paths can be raw bytestrings but also support a Unix path-like
//! syntax which can be parsed using the `String::parse` method:
//!
//! ```rust
//! let path = "/foo/bar/baz".parse::<hkd32::PathBuf>().unwrap();
//! ```
//!
//! # Example
//!
//! ```rust
//! // Parent key
//! let input_key_material = hkd32::KeyMaterial::random();
//!
//! // Path to the child key
//! let derivation_path = "/foo/bar/baz".parse::<hkd32::PathBuf>().unwrap();
//!
//! // Derive subkey from the parent key. Call `as_bytes()` on this to obtain
//! // a byte slice containing the derived key.
//! let output_key_material = input_key_material.derive_subkey(derivation_path);
//! ```
//!
//! [bip32]: https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki

#![no_std]
#![deny(
    missing_docs,
    rust_2018_idioms,
    unused_lifetimes,
    unused_qualifications
)]
#![doc(html_root_url = "https://docs.rs/hkd32/0.3.1")]

#[cfg(feature = "alloc")]
#[cfg_attr(any(feature = "bip39", test), macro_use)]
extern crate alloc;

mod key_material;
#[cfg(feature = "mnemonic")]
pub mod mnemonic;
mod path;
#[cfg(feature = "alloc")]
mod pathbuf;

#[cfg(feature = "alloc")]
pub use self::pathbuf::PathBuf;
pub use self::{key_material::*, path::*};

/// Delimiter used for strings containing paths
pub const DELIMITER: char = '/';

/// Size of input key material and derived keys.
///
/// Note: the name HKD32 is both a play on this size and "BIP32".
pub const KEY_SIZE: usize = 32;

/// Opaque error type
#[derive(Copy, Clone, Debug)]
pub struct Error;