Skip to content

Commit

Permalink
Move trait to crate root
Browse files Browse the repository at this point in the history
  • Loading branch information
keks committed Nov 18, 2024
1 parent 0b6d0e0 commit 4f9ecfc
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 64 deletions.
64 changes: 63 additions & 1 deletion libcrux-hkdf/src/hkdf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,69 @@ pub mod hacl;
#[cfg(feature = "hacl")]
mod impl_hacl;

pub use impl_hacl::{HkdfMode, HkdfSha2_256, HkdfSha2_384, HkdfSha2_512};
pub use impl_hacl::{HkdfSha2_256, HkdfSha2_384, HkdfSha2_512};

pub trait HkdfMode<const HASH_LEN: usize> {
/// The hash algorithm used in this HKDF mode.
const MODE: Algorithm;

/// HKDF extract using the `salt` and the input key material `ikm`.
/// The result is written to `prk`.
///
/// Note that this function panics if `salt` or `ikm` is longer than (2**32 - 1) bytes.
fn extract(prk: &mut [u8; HASH_LEN], salt: &[u8], ikm: &[u8]) -> Result<(), Error>;

/// HKDF expand using the pre-key material `prk` and `info`. The output length
/// is defined through the type of the `okm` parameter, that the output is written to.
///
/// Returns nothing on success.
/// Returns [`Error::OkmTooLarge`] if the requested `okm_len` is too large.
/// Returns [`Error::ArgumentsTooLarge`] if one of `salt` or `ikm` are longer than, or
/// `OKM_LEN` is larger than (2**32 - 1) bytes.
fn expand<const OKM_LEN: usize>(
okm: &mut [u8; OKM_LEN],
prk: &[u8],
info: &[u8],
) -> Result<(), Error>;

/// HKDF expand using the pre-key material `prk` and `info`. The output length
/// is defined by the parameter `okm_len`.
///
/// Returns the key material in an array of length `okm_len` on success.
/// Returns [`Error::OkmTooLarge`] if the requested `okm_len` is too large.
/// Returns [`Error::ArgumentsTooLarge`] if `salt` or `ikm` is longer than
/// (2**32 - 1) bytes.
fn expand_vec(prk: &[u8], info: &[u8], okm_len: usize) -> Result<Vec<u8>, Error>;

/// HKDF using the `salt`, input key material `ikm`, `info`. The output length
/// is defined through the result type.
/// Calls `extract` and `expand` with the given input.
///
/// Returns the key material in an array of length `okm_len`.
/// Note that this function panics if `salt` or `ikm` is longer than (2**32 - 1) bytes.
fn hkdf<const OKM_LEN: usize>(
okm: &mut [u8; OKM_LEN],
salt: &[u8],
ikm: &[u8],
info: &[u8],
) -> Result<(), Error> {
let mut prk = [0u8; HASH_LEN];
Self::extract(&mut prk, salt, ikm)?;
Self::expand(okm, &prk, info)
}

/// HKDF using the `salt`, input key material `ikm`, `info`. The output length
/// is defined by the parameter `okm_len`.
/// Calls `extract` and `expand` with the given input.
///
/// Returns the key material in an array of length `okm_len`.
/// Note that this function panics if `salt` or `ikm` is longer than (2**32 - 1) bytes.
fn hkdf_vec(salt: &[u8], ikm: &[u8], info: &[u8], okm_len: usize) -> Result<Vec<u8>, Error> {
let mut prk = [0u8; HASH_LEN];
Self::extract(&mut prk, salt, ikm)?;
Self::expand_vec(&prk, info, okm_len)
}
}

/// The HKDF algorithm defining the used hash function.
#[derive(Copy, Clone, Debug, PartialEq)]
Expand Down
64 changes: 1 addition & 63 deletions libcrux-hkdf/src/impl_hacl.rs
Original file line number Diff line number Diff line change
@@ -1,68 +1,6 @@
#![allow(dead_code)]

use crate::{Algorithm, Error};

pub trait HkdfMode<const HASH_LEN: usize> {
/// The hash algorithm used in this HKDF mode.
const MODE: Algorithm;

/// HKDF extract using the `salt` and the input key material `ikm`.
/// The result is written to `prk`.
///
/// Note that this function panics if `salt` or `ikm` is longer than (2**32 - 1) bytes.
fn extract(prk: &mut [u8; HASH_LEN], salt: &[u8], ikm: &[u8]) -> Result<(), Error>;

/// HKDF expand using the pre-key material `prk` and `info`. The output length
/// is defined through the type of the `okm` parameter, that the output is written to.
///
/// Returns nothing on success.
/// Returns [`Error::OkmTooLarge`] if the requested `okm_len` is too large.
/// Returns [`Error::ArgumentsTooLarge`] if one of `salt` or `ikm` are longer than, or
/// `OKM_LEN` is larger than (2**32 - 1) bytes.
fn expand<const OKM_LEN: usize>(
okm: &mut [u8; OKM_LEN],
prk: &[u8],
info: &[u8],
) -> Result<(), Error>;

/// HKDF expand using the pre-key material `prk` and `info`. The output length
/// is defined by the parameter `okm_len`.
///
/// Returns the key material in an array of length `okm_len` on success.
/// Returns [`Error::OkmTooLarge`] if the requested `okm_len` is too large.
/// Returns [`Error::ArgumentsTooLarge`] if `salt` or `ikm` is longer than
/// (2**32 - 1) bytes.
fn expand_vec(prk: &[u8], info: &[u8], okm_len: usize) -> Result<Vec<u8>, Error>;

/// HKDF using the `salt`, input key material `ikm`, `info`. The output length
/// is defined through the result type.
/// Calls `extract` and `expand` with the given input.
///
/// Returns the key material in an array of length `okm_len`.
/// Note that this function panics if `salt` or `ikm` is longer than (2**32 - 1) bytes.
fn hkdf<const OKM_LEN: usize>(
okm: &mut [u8; OKM_LEN],
salt: &[u8],
ikm: &[u8],
info: &[u8],
) -> Result<(), Error> {
let mut prk = [0u8; HASH_LEN];
Self::extract(&mut prk, salt, ikm)?;
Self::expand(okm, &prk, info)
}

/// HKDF using the `salt`, input key material `ikm`, `info`. The output length
/// is defined by the parameter `okm_len`.
/// Calls `extract` and `expand` with the given input.
///
/// Returns the key material in an array of length `okm_len`.
/// Note that this function panics if `salt` or `ikm` is longer than (2**32 - 1) bytes.
fn hkdf_vec(salt: &[u8], ikm: &[u8], info: &[u8], okm_len: usize) -> Result<Vec<u8>, Error> {
let mut prk = [0u8; HASH_LEN];
Self::extract(&mut prk, salt, ikm)?;
Self::expand_vec(&prk, info, okm_len)
}
}
use crate::{Algorithm, Error, HkdfMode};

macro_rules! impl_hkdf {
($sname:ident,$name:ident, $mode:path, $extract:ident, $expand:ident,$hash_len:literal) => {
Expand Down

0 comments on commit 4f9ecfc

Please sign in to comment.