191191//! ```
192192
193193#![ forbid( unsafe_code) ]
194+ #![ forbid( missing_docs) ]
194195
195196use bouncycastle_core:: errors:: { KDFError , KeyMaterialError , MACError , SuspendableError } ;
196197use bouncycastle_core:: key_material;
@@ -213,19 +214,26 @@ use bouncycastle_core::traits::XOF;
213214/*** Constants ***/
214215// Slightly hacky, but set this to accommodate the underlying hash primitive with the largest output size.
215216// Would be better to somehow pull that at compile time from H, but I'm not sure how to do that.
217+ // todo can I delete this?
216218const HMAC_BLOCK_LEN : usize = 64 ;
217219
218220/*** String constants ***/
219221
222+ ///
220223pub const HKDF_SHA256_NAME : & str = "HKDF-SHA256" ;
224+ ///
221225pub const HKDF_SHA512_NAME : & str = "HKDF-SHA512" ;
222226
223227/*** Types ***/
228+ /// Public type for HKDF using SHA256.
224229#[ allow( non_camel_case_types) ]
225230pub type HKDF_SHA256 = HKDF < SHA256 > ;
231+ /// Public type for HKDF using SHA512.
226232#[ allow( non_camel_case_types) ]
227233pub type HKDF_SHA512 = HKDF < SHA512 > ;
228234
235+ /// Internal struct for HKDF.
236+ /// Can, in theory, be instantiated with hash functions other than the ones provided by this crate (even custom ones).
229237#[ derive( Clone ) ]
230238pub struct HKDF < H : Hash + HashAlgParams + Default > {
231239 // Optional because we can't construct an HMAC until they give us a key
@@ -336,6 +344,7 @@ impl<H: Hash + HashAlgParams + Default> Default for HKDF<H> {
336344}
337345
338346impl < H : Hash + HashAlgParams + Default > HKDF < H > {
347+ /// Creates a new, uninstantiated HKDF object.
339348 pub fn new ( ) -> Self {
340349 Self { hmac : None , entropy : HkdfEntropyTracker :: new ( ) , state : HkdfStates :: Uninitialized }
341350 }
@@ -379,10 +388,11 @@ impl<H: Hash + HashAlgParams + Default> HKDF<H> {
379388 }
380389
381390 /// Same as [HKDF::extract], but writes the output to a provided KeyMaterial buffer.
391+ /// Note that the provided KeyMaterial must be correctly sized to the hash function output length.
382392 pub fn extract_out (
383393 salt : & impl KeyMaterialTrait ,
384394 ikm : & impl KeyMaterialTrait ,
385- prk : & mut impl KeyMaterialTrait ,
395+ prk : & mut KeyMaterial < H :: OUTPUT_LEN > ,
386396 ) -> Result < usize , MACError > {
387397 // PRK = HMAC-Hash(salt, IKM)
388398
@@ -625,18 +635,24 @@ impl<H: Hash + HashAlgParams + Default> HKDF<H> {
625635 Ok ( 0 )
626636 }
627637
638+ /// Finish the HKDF-Extract phase and produce the output `prk`.
628639 #[ allow( non_snake_case) ]
629- pub fn do_extract_final ( self ) -> Result < impl KeyMaterialTrait , MACError > {
640+ pub fn do_extract_final ( self ) -> Result < KeyMaterial < HMAC_BLOCK_LEN > , MACError > {
630641 let mut okm = KeyMaterial :: < HMAC_BLOCK_LEN > :: new ( ) ;
631642 self . do_extract_final_out ( & mut okm) ?;
632643 Ok ( okm)
633644 }
634645
646+ /// Finish the HKDF-Extract phase and fill the provided `prk`.
647+ /// Note that the provided KeyMaterial must be correctly sized to the HMAC block length.
635648 #[ allow( non_snake_case) ]
636- pub fn do_extract_final_out ( self , okm : & mut impl KeyMaterialTrait ) -> Result < usize , MACError > {
649+ pub fn do_extract_final_out (
650+ self ,
651+ okm : & mut KeyMaterial < HMAC_BLOCK_LEN > ,
652+ ) -> Result < usize , MACError > {
637653 if self . state == HkdfStates :: Uninitialized {
638654 return Err ( MACError :: InvalidState (
639- "Must call do_extract_init() before calling do_extract_complete ()." ,
655+ "Must call do_extract_init() before calling do_extract_final ()." ,
640656 ) ) ;
641657 } ;
642658 debug_assert ! ( self . hmac. is_some( ) ) ;
0 commit comments