Skip to content

Commit

Permalink
review
Browse files Browse the repository at this point in the history
  • Loading branch information
franziskuskiefer committed Mar 20, 2023
1 parent 64fdfcf commit 6d1ba85
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 13 deletions.
39 changes: 29 additions & 10 deletions rust/src/hazmat/drbg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ pub enum Algorithm {
}

pub enum Error {
/// Invalid input, e.g. input values are too large.
InvalidInput,

/// Unable to generate the requested randomness.
UnableToGenerate,
}
Expand All @@ -28,37 +31,50 @@ impl Drbg {
/// Create a new DRBG state with the given hash function.
/// This also initializes the DRBG state with the given entropy, nonce and
/// personalization string.
pub fn new(alg: Algorithm, entropy: &[u8], nonce: &[u8], personalization: &str) -> Self {
pub fn new(
alg: Algorithm,
entropy: &[u8],
nonce: &[u8],
personalization: &str,
) -> Result<Self, Error> {
let state = unsafe { Hacl_HMAC_DRBG_create_in(alg as u8) };
unsafe {
Hacl_HMAC_DRBG_instantiate(
alg as u8,
state,
entropy.len().try_into().unwrap(),
entropy.len().try_into().map_err(|_| Error::InvalidInput)?,
entropy.as_ptr() as _,
nonce.len().try_into().unwrap(),
nonce.len().try_into().map_err(|_| Error::InvalidInput)?,
nonce.as_ptr() as _,
personalization.len().try_into().unwrap(),
personalization
.len()
.try_into()
.map_err(|_| Error::InvalidInput)?,
personalization.as_bytes().as_ptr() as _,
);
}
Self { state, alg }
Ok(Self { state, alg })
}

/// Reseed the DRBG state.
///
/// It is very unlikely that you will need this function.
pub fn reseed(&mut self, entropy: &[u8], additional_input: &[u8]) {
pub fn reseed(&mut self, entropy: &[u8], additional_input: &[u8]) -> Result<(), Error> {
unsafe {
Hacl_HMAC_DRBG_reseed(
self.alg as u8,
self.state,
entropy.len().try_into().unwrap(),
entropy.len().try_into().map_err(|_| Error::InvalidInput)?,
entropy.as_ptr() as _,
additional_input.len().try_into().unwrap(),
additional_input
.len()
.try_into()
.map_err(|_| Error::InvalidInput)?,
additional_input.as_ptr() as _,
);
}

Ok(())
}

/// Generate random bytes.
Expand All @@ -68,8 +84,11 @@ impl Drbg {
self.alg as u8,
output.as_mut_ptr(),
self.state,
output.len().try_into().unwrap(),
additional_input.len().try_into().unwrap(),
output.len().try_into().map_err(|_| Error::InvalidInput)?,
additional_input
.len()
.try_into()
.map_err(|_| Error::InvalidInput)?,
additional_input.as_ptr() as _,
)
} {
Expand Down
4 changes: 2 additions & 2 deletions rust/src/hazmat/p256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ pub mod ecdsa {
if unsafe {
$fun_sign(
result.as_mut_ptr(),
payload.len().try_into().unwrap(),
payload.len().try_into().map_err(|_| Error::InvalidInput)?,
payload.as_ptr() as _,
private_key.as_ptr() as _,
nonce.as_ptr() as _,
Expand All @@ -179,7 +179,7 @@ pub mod ecdsa {
) -> Result<(), Error> {
if unsafe {
$fun_verify(
payload.len().try_into().unwrap(),
payload.len().try_into().map_err(|_| Error::InvalidInput)?,
payload.as_ptr() as _,
public_key.as_ptr() as _,
signature_r.as_ptr() as _,
Expand Down
4 changes: 3 additions & 1 deletion rust/tests/test_aead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ fn key_gen_self_test() {
#[cfg(feature = "hazmat")]
#[test]
fn raw_self_test() {
use hacl_star::hazmat::{aesgcm, chacha20_poly1305};
use hacl_star::hazmat::chacha20_poly1305;

let msg = b"HACL rules";
let aad = b"associated data";
Expand Down Expand Up @@ -245,6 +245,8 @@ fn raw_self_test() {

#[cfg(aes_ni)]
{
use hacl_star::hazmat::aesgcm;

if aesgcm::hardware_support().is_ok() {
let mut io = *msg;
let tag = aesgcm::encrypt_256(key, &mut io, *iv, aad).unwrap();
Expand Down

0 comments on commit 6d1ba85

Please sign in to comment.