-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bare (very bare) HKDF implementation
* Dropping the sha2 and hkdf deps in favor of internal implemenation, but this has a long way to go. The packet tests cover the *exact* path for now though, so actually gives good coverage.
- Loading branch information
Showing
4 changed files
with
56 additions
and
47 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use bitcoin_hashes::{sha256, Hash, HashEngine, Hmac, HmacEngine}; | ||
use core::fmt; | ||
|
||
/// Structure for InvalidLength, used for output error handling. | ||
#[derive(Copy, Clone, Debug)] | ||
pub struct InvalidLength; | ||
|
||
impl fmt::Display for InvalidLength { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
write!(f, "invalid number of blocks, too large output") | ||
} | ||
} | ||
|
||
// Hardcoding to SHA256 hash and hmac implemenation. | ||
pub struct Hkdf { | ||
prk: Hmac<sha256::Hash>, | ||
} | ||
|
||
impl Hkdf { | ||
// TODO: make salt optional. | ||
pub fn new(salt: &[u8], ikm: &[u8]) -> Self { | ||
let mut hmac_engine: HmacEngine<sha256::Hash> = HmacEngine::new(salt); | ||
hmac_engine.input(ikm); | ||
Hkdf { | ||
prk: Hmac::from_engine(hmac_engine), | ||
} | ||
} | ||
pub fn expand(&self, info: &[u8], okm: &mut [u8]) -> Result<(), InvalidLength> { | ||
// TODO: actually loop and do not assume exact 32 byte match. | ||
let mut hmac_engine: HmacEngine<sha256::Hash> = HmacEngine::new(&self.prk.to_byte_array()); | ||
hmac_engine.input(info); | ||
hmac_engine.input(&[1u8]); | ||
let t = Hmac::from_engine(hmac_engine); | ||
okm.copy_from_slice(&t.to_byte_array()); | ||
return Ok(()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters