diff --git a/ledger_device_sdk/src/hash.rs b/ledger_device_sdk/src/hash.rs index a5cef43c..a8877ed6 100644 --- a/ledger_device_sdk/src/hash.rs +++ b/ledger_device_sdk/src/hash.rs @@ -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], @@ -15,12 +21,12 @@ impl Write for StringBuf { } #[repr(C)] -pub struct Hasher { +pub struct HashCtx { id: HashId, ctx: T, - header: cx_hash_t, } +#[derive(Copy, Clone)] pub enum HashId { Sha3224, Sha3256, @@ -35,41 +41,41 @@ pub enum HashId { Sha2512, } -trait Hash { +trait HashInit { type Ctx; fn init(id: HashId) -> Result; - 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, CxError> { + match id { + HashId::Keccak256 => HashCtx::::init(HashId::Keccak256), + HashId::Sha3224 => HashCtx::::init(HashId::Sha3224), + HashId::Sha3256 => HashCtx::::init(HashId::Sha3256), + HashId::Sha3384 => HashCtx::::init(HashId::Sha3384), + HashId::Sha3512 => HashCtx::::init(HashId::Sha3512), + HashId::Shake128 => HashCtx::::init(HashId::Shake128), + HashId::Shake256 => HashCtx::::init(HashId::Shake256), + _ => Err(CxError::GenericError), + } +} + +pub fn get_sha256(id: HashId) -> Result, CxError> { + match id { + HashId::Sha2224 => HashCtx::::init(HashId::Sha2224), + HashId::Sha2256 => HashCtx::::init(HashId::Sha2256), + _ => Err(CxError::GenericError), + } +} + +pub fn get_sha512(id: HashId) -> Result, CxError> { + match id { + HashId::Sha2384 => HashCtx::::init(HashId::Sha2384), + HashId::Sha2512 => HashCtx::::init(HashId::Sha2512), + _ => Err(CxError::GenericError), + } +} #[cfg(test)] mod tests { @@ -83,23 +89,14 @@ mod tests { #[test] fn test_hash_keccak() { - let mut keccak = Hasher::::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, @@ -111,14 +108,14 @@ mod tests { #[test] fn test_hash_sha2224() { - let mut sha2224 = Hasher::::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, diff --git a/ledger_device_sdk/src/hash/sha2.rs b/ledger_device_sdk/src/hash/sha2.rs index de47b102..3835252b 100644 --- a/ledger_device_sdk/src/hash/sha2.rs +++ b/ledger_device_sdk/src/hash/sha2.rs @@ -1,4 +1,4 @@ -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, @@ -6,8 +6,8 @@ use ledger_secure_sdk_sys::{ cx_sha512_t, CX_LAST, CX_OK, }; -impl Hash for Hasher { - type Ctx = Hasher; +impl HashInit for HashCtx { + type Ctx = HashCtx; fn init(id: HashId) -> Result { let mut ctx: cx_sha256_t = Default::default(); @@ -20,11 +20,7 @@ impl Hash for Hasher { crate::testing::debug_print("Error after init()\n"); Err(CxError::GenericError) } else { - Ok(Hasher { - id, - ctx, - header: ctx.header, - }) + Ok(HashCtx { id, ctx }) } } @@ -32,7 +28,7 @@ impl Hash for Hasher { 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, @@ -52,8 +48,8 @@ impl Hash for Hasher { } } -impl Hash for Hasher { - type Ctx = Hasher; +impl HashInit for HashCtx { + type Ctx = HashCtx; fn init(id: HashId) -> Result { let mut ctx: cx_sha512_t = Default::default(); @@ -66,11 +62,7 @@ impl Hash for Hasher { crate::testing::debug_print("Error after init()\n"); Err(CxError::GenericError) } else { - Ok(Hasher { - id, - ctx, - header: ctx.header, - }) + Ok(HashCtx { id, ctx }) } } @@ -78,7 +70,7 @@ impl Hash for Hasher { 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, diff --git a/ledger_device_sdk/src/hash/sha3.rs b/ledger_device_sdk/src/hash/sha3.rs index 69247847..0981fc95 100644 --- a/ledger_device_sdk/src/hash/sha3.rs +++ b/ledger_device_sdk/src/hash/sha3.rs @@ -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 { - type Ctx = Hasher; +impl HashInit for HashCtx { + type Ctx = HashCtx; fn init(id: HashId) -> Result { let mut ctx: cx_sha3_t = Default::default(); @@ -24,11 +24,7 @@ impl Hash for Hasher { crate::testing::debug_print("Error after init()\n"); Err(CxError::GenericError) } else { - Ok(Hasher { - id, - ctx, - header: ctx.header, - }) + Ok(HashCtx { id, ctx }) } } @@ -36,7 +32,7 @@ impl Hash for Hasher { 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,