Skip to content

Commit

Permalink
Fix implementation proper
Browse files Browse the repository at this point in the history
  • Loading branch information
thomwiggers committed May 21, 2019
1 parent c82812d commit e4f636a
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 78 deletions.
44 changes: 6 additions & 38 deletions pqcrypto-internals/include/fips202.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,76 +13,44 @@
* XOF state for the ``_absorb`` and ``_squeezeblocks`` functions.
*/
typedef struct {
void *state;
uint8_t __state[216];
} shake128ctx;

/**
* Incremental XOF state
*/
typedef enum {
S128Absorb,
S128Squeeze,
} shake128incctx_Tag;

typedef struct {
void *_0;
} S128Absorb_Body;

typedef struct {
void *_0;
} S128Squeeze_Body;

typedef struct {
shake128incctx_Tag tag;
union {
S128Absorb_Body absorb;
S128Squeeze_Body squeeze;
};
uint8_t __state[384];
} shake128incctx;

/**
* XOF state for the ``_absorb`` and ``_squeezeblocks`` functions.
*/
typedef struct {
void *state;
uint8_t __state[216];
} shake256ctx;

/**
* Incremental XOF state
*/
typedef enum {
S256Absorb,
S256Squeeze,
} shake256incctx_Tag;

typedef struct {
void *_0;
} S256Absorb_Body;

typedef struct {
void *_0;
} S256Squeeze_Body;

typedef struct {
shake256incctx_Tag tag;
union {
S256Absorb_Body absorb;
S256Squeeze_Body squeeze;
};
uint8_t __state[352];
} shake256incctx;

/**
* The state for the hash function
*/
typedef struct {
void *state;
uint8_t __state[352];
} sha3_256incctx;

/**
* The state for the hash function
*/
typedef struct {
void *state;
uint8_t __state[288];
} sha3_512incctx;


Expand Down
9 changes: 4 additions & 5 deletions pqcrypto-internals/include/sha2.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,31 @@
* The state for the hash function
*/
typedef struct {
void *state;
uint8_t __state[120];
} sha224ctx;

/**
* The state for the hash function
*/
typedef struct {
void *state;
uint8_t __state[120];
} sha256ctx;

/**
* The state for the hash function
*/
typedef struct {
void *state;
uint8_t __state[224];
} sha384ctx;

/**
* The state for the hash function
*/
typedef struct {
void *state;
uint8_t __state[224];
} sha512ctx;



/**
* Directly obtain the digest of input
*/
Expand Down
12 changes: 11 additions & 1 deletion pqcrypto-internals/src/fips202.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,21 @@ mod tests {
use super::*;
use sha3::{digest::ExtendableOutput, digest::Input, Shake128};

#[test]
fn test_sizes() {
assert_eq!(std::mem::size_of::<Sha3256IncState>(), 352, "sha3-256");
assert_eq!(std::mem::size_of::<Sha3512IncState>(), 288, "sha3-512");
assert_eq!(std::mem::size_of::<Shake128State>(), 216, "shake128State");
assert_eq!(std::mem::size_of::<Shake128IncState>(), 384, "shake128IncState");
assert_eq!(std::mem::size_of::<Shake256State>(), 216, "Shake256State");
assert_eq!(std::mem::size_of::<Shake256IncState>(), 352, "Shake256IncState");
}

#[test]
fn test_shake128_inc_api() {
use digest::XofReader;

let mut state: Shake128IncState = Shake128IncState::Absorb(std::ptr::null_mut());
let mut state: Shake128IncState = unsafe { std::mem::uninitialized() };
let state_ptr = &mut state as *mut Shake128IncState;
let input = b"hello world";
let mut output1 = [0u8; 32];
Expand Down
9 changes: 5 additions & 4 deletions pqcrypto-internals/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@

mod randombytes;
pub use randombytes::*;
#[macro_use]
mod macros;
//#[macro_use]
//mod macros;

pub mod fips202;
pub mod sha2;
//pub mod fips202;
//pub mod sha2;
46 changes: 19 additions & 27 deletions pqcrypto-internals/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ macro_rules! implement_hash {
$name_state: ident, $hash: ident, $output_bytes: expr) => {
/// The state for the hash function
#[repr(C)]
#[derive(Clone)]
pub struct $name_state {
state: *mut libc::c_void,
state: Option<$hash>,
}

/// Directly obtain the digest of input
Expand All @@ -34,8 +35,7 @@ macro_rules! implement_hash {
#[no_mangle]
pub unsafe extern "C" fn $name_inc_init(state: *mut $name_state) {
use std::default::Default;
let hash_state = Box::new($hash::default());
(*state).state = Box::into_raw(hash_state) as *mut libc::c_void;
(*state).state = Some($hash::default());
}

/// Add 64-bytes blocks to the state
Expand All @@ -46,9 +46,9 @@ macro_rules! implement_hash {
inlen: libc::size_t,
) {
use digest::Digest;
let digest = (*state).state as *mut $hash;
let digest = (*state).state.as_mut().unwrap();
let input: &[u8] = std::slice::from_raw_parts(inblocks, (inlen as usize) * 64);
(*digest).input(input);
(digest).input(input);
}

/// Finalize the state and obtain the hash result.
Expand All @@ -62,8 +62,7 @@ macro_rules! implement_hash {
inlen: libc::size_t,
) {
use digest::Digest;
let digest = std::mem::replace(&mut (*state).state, std::ptr::null_mut());
let mut digest = Box::from_raw(digest as *mut $hash);
let mut digest = (&mut (*state).state).take().unwrap();
let input: &[u8] = std::slice::from_raw_parts(inbytes, inlen as usize);
digest.input(input);
let mut out_slice = std::slice::from_raw_parts_mut(out, $output_bytes);
Expand All @@ -81,26 +80,26 @@ macro_rules! implement_xof {
/// XOF state for the ``_absorb`` and ``_squeezeblocks`` functions.
#[repr(C)]
pub struct $state_name {
state: *mut libc::c_void,
reader: Sha3XofReader,
}

/// Incremental XOF state
#[repr(C)]
pub enum $inc_state_name {
Absorb(*mut libc::c_void),
Squeeze(*mut libc::c_void),
Absorb($xof),
Squeeze(Sha3XofReader),
}

impl $inc_state_name {
unsafe fn get_absorb(&self) -> *mut $xof {
unsafe fn get_absorb(&mut self) -> &mut $xof {
match self {
$inc_state_name::Absorb(state) => (*state) as *mut $xof,
$inc_state_name::Absorb(ref mut state) => state,
_ => std::process::abort(),
}
}
unsafe fn get_squeeze(&self) -> *mut Sha3XofReader {
unsafe fn get_squeeze(&mut self) -> &mut Sha3XofReader {
match self {
$inc_state_name::Squeeze(state) => (*state) as *mut Sha3XofReader,
$inc_state_name::Squeeze(ref mut reader) => reader,
_ => std::process::abort(),
}
}
Expand Down Expand Up @@ -131,24 +130,22 @@ macro_rules! implement_xof {
pub unsafe extern "C" fn $name_absorb(state: *mut $state_name, input: *const libc::uint8_t, input_len: libc::size_t) {
use sha3::digest::{Input, ExtendableOutput};
let input = std::slice::from_raw_parts(input, input_len as usize);
let xof_state = Box::into_raw(Box::new($xof::default().chain(input).xof_result())) as *mut libc::c_void;
(*state).state = xof_state;
let xof_state = $xof::default().chain(input).xof_result();
(*state).reader = xof_state;
}

/// Squeeze out output from the XOF which already absorbed things through ``_absorb``.
#[no_mangle]
pub unsafe extern "C" fn $name_squeezeblocks(output: *mut libc::uint8_t, nblocks: libc::size_t, state: *mut $state_name) {
use digest::XofReader;
let mut output = std::slice::from_raw_parts_mut(output, $rate * nblocks as usize);
let xofreader = (*state).state as *mut Sha3XofReader;
(*xofreader).read(&mut output);
(*state).reader.read(&mut output);
}

/// Initialize the incremental XOF state
#[no_mangle]
pub unsafe extern "C" fn $name_inc_init(state: *mut $inc_state_name) {
let hash_state = Box::new($xof::default());
(*state) = $inc_state_name::Absorb(Box::into_raw(hash_state) as *mut libc::c_void);
(*state) = $inc_state_name::Absorb($xof::default());
}

/// Absorb ``input`` into the XOF state
Expand All @@ -161,20 +158,15 @@ macro_rules! implement_xof {
use sha2::digest::Input;
let digest = (*state).get_absorb();
let input: &[u8] = std::slice::from_raw_parts(input, inlen as usize);
(*digest).input(input);
digest.input(input);
}

/// Finalize the XOF state to prepare for squeezing.
/// After this you can't absorb anymore.
#[no_mangle]
pub unsafe extern "C" fn $name_inc_finalize(state: *mut $inc_state_name) {
use digest::ExtendableOutput;
let digest =
std::mem::replace(&mut (*state).get_absorb(), std::ptr::null_mut()) as *mut $xof;
let digest = Box::from_raw(digest);
*state = $inc_state_name::Squeeze(
Box::into_raw(Box::new(digest.xof_result())) as *mut libc::c_void
);
*state = $inc_state_name::Squeeze((*state).get_absorb().clone().xof_result());
}

/// Squeeze out ``outlen`` bytes
Expand Down
10 changes: 9 additions & 1 deletion pqcrypto-internals/src/sha2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ implement_hash!(
mod tests {
use super::*;

#[test]
fn test_size() {
assert_eq!(std::mem::size_of::<Sha224IncState>(), 120, "sha224");
assert_eq!(std::mem::size_of::<Sha256IncState>(), 120, "sha256");
assert_eq!(std::mem::size_of::<Sha384IncState>(), 224, "sha384");
assert_eq!(std::mem::size_of::<Sha512IncState>(), 224, "sha512");
}

#[test]
fn test_sha256() {
use digest::Digest;
Expand All @@ -63,7 +71,7 @@ mod tests {
fn test_sha256_inc_api() {
use digest::Digest;
let mut state: Sha256IncState = Sha256IncState {
state: std::ptr::null_mut(),
state: None
};
let state_ptr = &mut state as *mut Sha256IncState;
let input = b"hello world";
Expand Down
3 changes: 1 addition & 2 deletions pqcrypto-sphincsplus/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ use std::path::Path;
fn main() {
let common_dir = Path::new("pqclean/common");
let common_files = [
common_dir.join("fips202.c"),
common_dir.join("aes.c"),
common_dir.join("sha2.c"),
];

let target_sphincsharaka128ssimple_dir =
Expand Down Expand Up @@ -336,6 +334,7 @@ fn main() {
)
.unwrap();
cc::Build::new()
.include("../pqcrypto-internals/include")
.include("pqclean/common")
.flag("-std=c99")
.flag("-O3")
Expand Down

0 comments on commit e4f636a

Please sign in to comment.