Skip to content

Commit

Permalink
refactor again and again
Browse files Browse the repository at this point in the history
  • Loading branch information
yogh333 committed Feb 16, 2024
1 parent 53b76d4 commit 84a900d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 72 deletions.
89 changes: 43 additions & 46 deletions ledger_device_sdk/src/hash.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
use crate::ecc::CxError;
use core::fmt::{Error, Write};
use ledger_secure_sdk_sys::{cx_hash_get_size, cx_hash_no_throw, cx_hash_t, CX_LAST, CX_OK};
use ledger_secure_sdk_sys::{
cx_hash_get_size, cx_hash_info_t, cx_hash_no_throw, cx_hash_t, cx_sha256_t, cx_sha3_t,
cx_sha512_t, CX_LAST, CX_OK,
};

mod sha2;
mod sha3;

struct StringBuf {
buf: [u8; 256],
Expand All @@ -15,12 +21,12 @@ impl Write for StringBuf {
}

#[repr(C)]
pub struct Hasher<T> {
pub struct HashCtx<T> {
id: HashId,
ctx: T,
header: cx_hash_t,
}

#[derive(Copy, Clone)]
pub enum HashId {
Sha3224,
Sha3256,
Expand All @@ -35,41 +41,41 @@ pub enum HashId {
Sha2512,
}

trait Hash {
trait HashInit {
type Ctx;
fn init(id: HashId) -> Result<Self::Ctx, CxError>;

fn get_size(&self) -> usize;
/*fn get_size(&self) -> usize {
unsafe { cx_hash_get_size(&self.header as *const cx_hash_t) }
}*/

fn compute(&mut self, input: &[u8], output: &mut [u8]) -> Result<(), CxError>;

/*fn compute(&mut self, input: &[u8], output: &mut [u8]) -> Result<(), CxError> {
let err = unsafe {
cx_hash_no_throw(
&mut self.header as *mut cx_hash_t,
CX_LAST,
input.as_ptr(),
input.len(),
output.as_mut_ptr(),
output.len(),
)
};
if err != CX_OK {
crate::testing::debug_print("Error after hash()\n");
Err(CxError::GenericError)
} else {
Ok(())
}
}*/
//fn update(&mut self) -> Result<(), CxError>;
//fn end(&mut self) -> Result<(), CxError>;
fn hash(&mut self, input: &[u8], output: &mut [u8]) -> Result<(), CxError>;
}

mod sha2;
mod sha3;
pub fn get_sha3(id: HashId) -> Result<HashCtx<cx_sha3_t>, CxError> {
match id {
HashId::Keccak256 => HashCtx::<cx_sha3_t>::init(HashId::Keccak256),
HashId::Sha3224 => HashCtx::<cx_sha3_t>::init(HashId::Sha3224),
HashId::Sha3256 => HashCtx::<cx_sha3_t>::init(HashId::Sha3256),
HashId::Sha3384 => HashCtx::<cx_sha3_t>::init(HashId::Sha3384),
HashId::Sha3512 => HashCtx::<cx_sha3_t>::init(HashId::Sha3512),
HashId::Shake128 => HashCtx::<cx_sha3_t>::init(HashId::Shake128),
HashId::Shake256 => HashCtx::<cx_sha3_t>::init(HashId::Shake256),
_ => Err(CxError::GenericError),
}
}

pub fn get_sha256(id: HashId) -> Result<HashCtx<cx_sha256_t>, CxError> {
match id {
HashId::Sha2224 => HashCtx::<cx_sha256_t>::init(HashId::Sha2224),
HashId::Sha2256 => HashCtx::<cx_sha256_t>::init(HashId::Sha2256),
_ => Err(CxError::GenericError),
}
}

pub fn get_sha512(id: HashId) -> Result<HashCtx<cx_sha512_t>, CxError> {
match id {
HashId::Sha2384 => HashCtx::<cx_sha512_t>::init(HashId::Sha2384),
HashId::Sha2512 => HashCtx::<cx_sha512_t>::init(HashId::Sha2512),
_ => Err(CxError::GenericError),
}
}

#[cfg(test)]
mod tests {
Expand All @@ -83,23 +89,14 @@ mod tests {

#[test]
fn test_hash_keccak() {
let mut keccak = Hasher::<cx_sha3_t>::init(HashId::Keccak256).unwrap();
let mut keccak = get_sha3(HashId::Keccak256).unwrap();

let mut output: [u8; 32] = [0u8; 32];

let ouput_size = keccak.get_size();
assert_eq!(ouput_size, 32);

/* OK {
let size = unsafe { cx_hash_get_size(&keccak.ctx.header as *const cx_hash_t) };
assert_eq!(size, 32);
}*/
/* KO (Why ?????) {
let size = unsafe { cx_hash_get_size(&keccak.header as *const cx_hash_t) };
assert_eq!(size, 32);
}*/

let _ = keccak.compute(TEST_HASH, &mut output);
let _ = keccak.hash(TEST_HASH, &mut output);

let expected = [
0x1f, 0x20, 0x7c, 0xd9, 0xfd, 0x9f, 0x0b, 0x09, 0xb0, 0x04, 0x93, 0x6c, 0xa5, 0xe0,
Expand All @@ -111,14 +108,14 @@ mod tests {

#[test]
fn test_hash_sha2224() {
let mut sha2224 = Hasher::<cx_sha256_t>::init(HashId::Sha2224).unwrap();
let mut sha2224 = get_sha256(HashId::Sha2224).unwrap();

let mut output: [u8; 32] = [0u8; 32];

let ouput_size: usize = sha2224.get_size();
assert_eq!(ouput_size, 28);

let _ = sha2224.compute(TEST_HASH, &mut output);
let _ = sha2224.hash(TEST_HASH, &mut output);

let expected = [
0x5a, 0x5b, 0xea, 0xa1, 0x3f, 0x5d, 0xf3, 0xd8, 0x5a, 0xc8, 0x62, 0x44, 0x95, 0x9b,
Expand Down
26 changes: 9 additions & 17 deletions ledger_device_sdk/src/hash/sha2.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use super::{Hash, HashId, Hasher};
use super::{HashCtx, HashId, HashInit};
use crate::ecc::CxError;
use ledger_secure_sdk_sys::{
cx_hash_get_size, cx_hash_no_throw, cx_hash_t, cx_sha224_init_no_throw,
cx_sha256_init_no_throw, cx_sha256_t, cx_sha384_init_no_throw, cx_sha512_init_no_throw,
cx_sha512_t, CX_LAST, CX_OK,
};

impl Hash for Hasher<cx_sha256_t> {
type Ctx = Hasher<cx_sha256_t>;
impl HashInit for HashCtx<cx_sha256_t> {
type Ctx = HashCtx<cx_sha256_t>;

fn init(id: HashId) -> Result<Self::Ctx, CxError> {
let mut ctx: cx_sha256_t = Default::default();
Expand All @@ -20,19 +20,15 @@ impl Hash for Hasher<cx_sha256_t> {
crate::testing::debug_print("Error after init()\n");
Err(CxError::GenericError)
} else {
Ok(Hasher {
id,
ctx,
header: ctx.header,
})
Ok(HashCtx { id, ctx })
}
}

fn get_size(&self) -> usize {
unsafe { cx_hash_get_size(&self.ctx.header as *const cx_hash_t) }
}

fn compute(&mut self, input: &[u8], output: &mut [u8]) -> Result<(), CxError> {
fn hash(&mut self, input: &[u8], output: &mut [u8]) -> Result<(), CxError> {
let err = unsafe {
cx_hash_no_throw(
&mut self.ctx.header as *mut cx_hash_t,
Expand All @@ -52,8 +48,8 @@ impl Hash for Hasher<cx_sha256_t> {
}
}

impl Hash for Hasher<cx_sha512_t> {
type Ctx = Hasher<cx_sha512_t>;
impl HashInit for HashCtx<cx_sha512_t> {
type Ctx = HashCtx<cx_sha512_t>;

fn init(id: HashId) -> Result<Self::Ctx, CxError> {
let mut ctx: cx_sha512_t = Default::default();
Expand All @@ -66,19 +62,15 @@ impl Hash for Hasher<cx_sha512_t> {
crate::testing::debug_print("Error after init()\n");
Err(CxError::GenericError)
} else {
Ok(Hasher {
id,
ctx,
header: ctx.header,
})
Ok(HashCtx { id, ctx })
}
}

fn get_size(&self) -> usize {
unsafe { cx_hash_get_size(&self.ctx.header as *const cx_hash_t) }
}

fn compute(&mut self, input: &[u8], output: &mut [u8]) -> Result<(), CxError> {
fn hash(&mut self, input: &[u8], output: &mut [u8]) -> Result<(), CxError> {
let err = unsafe {
cx_hash_no_throw(
&mut self.ctx.header as *mut cx_hash_t,
Expand Down
14 changes: 5 additions & 9 deletions ledger_device_sdk/src/hash/sha3.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use super::{Hash, HashId, Hasher};
use super::{HashCtx, HashId, HashInit};
use crate::ecc::CxError;
use ledger_secure_sdk_sys::{
cx_hash_get_size, cx_hash_no_throw, cx_hash_t, cx_keccak_init_no_throw, cx_sha3_init_no_throw,
cx_sha3_t, cx_shake128_init_no_throw, cx_shake256_init_no_throw, CX_LAST, CX_OK,
};

impl Hash for Hasher<cx_sha3_t> {
type Ctx = Hasher<cx_sha3_t>;
impl HashInit for HashCtx<cx_sha3_t> {
type Ctx = HashCtx<cx_sha3_t>;

fn init(id: HashId) -> Result<Self::Ctx, CxError> {
let mut ctx: cx_sha3_t = Default::default();
Expand All @@ -24,19 +24,15 @@ impl Hash for Hasher<cx_sha3_t> {
crate::testing::debug_print("Error after init()\n");
Err(CxError::GenericError)
} else {
Ok(Hasher {
id,
ctx,
header: ctx.header,
})
Ok(HashCtx { id, ctx })
}
}

fn get_size(&self) -> usize {
unsafe { cx_hash_get_size(&self.ctx.header as *const cx_hash_t) }
}

fn compute(&mut self, input: &[u8], output: &mut [u8]) -> Result<(), CxError> {
fn hash(&mut self, input: &[u8], output: &mut [u8]) -> Result<(), CxError> {
let err = unsafe {
cx_hash_no_throw(
&mut self.ctx.header as *mut cx_hash_t,
Expand Down

0 comments on commit 84a900d

Please sign in to comment.