-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move SHA-3 tests and add SHA-2 wrappers
- Loading branch information
Showing
3 changed files
with
212 additions
and
261 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,145 +1,110 @@ | ||
use super::{HashCtx, HashId, HashInit}; | ||
use super::HashInit; | ||
use crate::ecc::CxError; | ||
use ledger_secure_sdk_sys::{ | ||
cx_hash_final, cx_hash_get_size, cx_hash_no_throw, cx_hash_t, cx_hash_update, | ||
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, | ||
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_OK, | ||
}; | ||
|
||
impl HashInit for HashCtx<cx_sha256_t> { | ||
type Ctx = HashCtx<cx_sha256_t>; | ||
#[derive(Default)] | ||
pub struct Sha2224Ctx { | ||
ctx: cx_sha256_t, | ||
} | ||
|
||
fn init(id: HashId) -> Result<Self::Ctx, CxError> { | ||
let mut ctx: cx_sha256_t = Default::default(); | ||
let err = match id { | ||
HashId::Sha2224 => unsafe { cx_sha224_init_no_throw(&mut ctx) }, | ||
HashId::Sha2256 => unsafe { cx_sha256_init_no_throw(&mut ctx) }, | ||
_ => !CX_OK, | ||
}; | ||
if err != CX_OK { | ||
crate::testing::debug_print("Error after init()\n"); | ||
Err(CxError::GenericError) | ||
} else { | ||
Ok(HashCtx { id, ctx }) | ||
} | ||
impl HashInit for Sha2224Ctx { | ||
fn as_ctx_mut(&mut self) -> &mut cx_hash_t { | ||
&mut self.ctx.header | ||
} | ||
|
||
fn get_size(&self) -> usize { | ||
unsafe { cx_hash_get_size(&self.ctx.header as *const cx_hash_t) } | ||
fn as_ctx(&self) -> &cx_hash_t { | ||
&self.ctx.header | ||
} | ||
|
||
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, | ||
CX_LAST, | ||
input.as_ptr(), | ||
input.len(), | ||
output.as_mut_ptr(), | ||
output.len(), | ||
) | ||
}; | ||
fn init() -> Result<Self, CxError> { | ||
let mut ctx: Sha2224Ctx = Default::default(); | ||
let err = unsafe { cx_sha224_init_no_throw(&mut ctx.ctx) }; | ||
if err != CX_OK { | ||
crate::testing::debug_print("Error after hash()\n"); | ||
crate::testing::debug_print("Error after init()\n"); | ||
Err(CxError::GenericError) | ||
} else { | ||
Ok(()) | ||
Ok(ctx) | ||
} | ||
} | ||
} | ||
|
||
fn update(&mut self, input: &[u8]) -> Result<(), CxError> { | ||
let err = unsafe { | ||
cx_hash_update( | ||
&mut self.ctx.header as *mut cx_hash_t, | ||
input.as_ptr(), | ||
input.len(), | ||
) | ||
}; | ||
if err != CX_OK { | ||
crate::testing::debug_print("Error after update()\n"); | ||
Err(CxError::GenericError) | ||
} else { | ||
Ok(()) | ||
} | ||
#[derive(Default)] | ||
pub struct Sha2256Ctx { | ||
ctx: cx_sha256_t, | ||
} | ||
|
||
impl HashInit for Sha2256Ctx { | ||
fn as_ctx_mut(&mut self) -> &mut cx_hash_t { | ||
&mut self.ctx.header | ||
} | ||
|
||
fn as_ctx(&self) -> &cx_hash_t { | ||
&self.ctx.header | ||
} | ||
|
||
fn finalize(&mut self, output: &mut [u8]) -> Result<(), CxError> { | ||
let err = | ||
unsafe { cx_hash_final(&mut self.ctx.header as *mut cx_hash_t, output.as_mut_ptr()) }; | ||
fn init() -> Result<Self, CxError> { | ||
let mut ctx: Sha2256Ctx = Default::default(); | ||
let err = unsafe { cx_sha256_init_no_throw(&mut ctx.ctx) }; | ||
if err != CX_OK { | ||
crate::testing::debug_print("Error after final()\n"); | ||
crate::testing::debug_print("Error after init()\n"); | ||
Err(CxError::GenericError) | ||
} else { | ||
Ok(()) | ||
Ok(ctx) | ||
} | ||
} | ||
} | ||
|
||
impl HashInit for HashCtx<cx_sha512_t> { | ||
type Ctx = HashCtx<cx_sha512_t>; | ||
#[derive(Default)] | ||
pub struct Sha2384Ctx { | ||
ctx: cx_sha512_t, | ||
} | ||
|
||
fn init(id: HashId) -> Result<Self::Ctx, CxError> { | ||
let mut ctx: cx_sha512_t = Default::default(); | ||
let err = match id { | ||
HashId::Sha2224 => unsafe { cx_sha384_init_no_throw(&mut ctx) }, | ||
HashId::Sha2256 => unsafe { cx_sha512_init_no_throw(&mut ctx) }, | ||
_ => !CX_OK, | ||
}; | ||
if err != CX_OK { | ||
crate::testing::debug_print("Error after init()\n"); | ||
Err(CxError::GenericError) | ||
} else { | ||
Ok(HashCtx { id, ctx }) | ||
} | ||
impl HashInit for Sha2384Ctx { | ||
fn as_ctx_mut(&mut self) -> &mut cx_hash_t { | ||
&mut self.ctx.header | ||
} | ||
|
||
fn get_size(&self) -> usize { | ||
unsafe { cx_hash_get_size(&self.ctx.header as *const cx_hash_t) } | ||
fn as_ctx(&self) -> &cx_hash_t { | ||
&self.ctx.header | ||
} | ||
|
||
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, | ||
CX_LAST, | ||
input.as_ptr(), | ||
input.len(), | ||
output.as_mut_ptr(), | ||
output.len(), | ||
) | ||
}; | ||
fn init() -> Result<Self, CxError> { | ||
let mut ctx: Sha2384Ctx = Default::default(); | ||
let err = unsafe { cx_sha384_init_no_throw(&mut ctx.ctx) }; | ||
if err != CX_OK { | ||
crate::testing::debug_print("Error after hash()\n"); | ||
crate::testing::debug_print("Error after init()\n"); | ||
Err(CxError::GenericError) | ||
} else { | ||
Ok(()) | ||
Ok(ctx) | ||
} | ||
} | ||
} | ||
|
||
fn update(&mut self, input: &[u8]) -> Result<(), CxError> { | ||
let err = unsafe { | ||
cx_hash_update( | ||
&mut self.ctx.header as *mut cx_hash_t, | ||
input.as_ptr(), | ||
input.len(), | ||
) | ||
}; | ||
if err != CX_OK { | ||
crate::testing::debug_print("Error after update()\n"); | ||
Err(CxError::GenericError) | ||
} else { | ||
Ok(()) | ||
} | ||
#[derive(Default)] | ||
pub struct Sha2512Ctx { | ||
ctx: cx_sha512_t, | ||
} | ||
|
||
impl HashInit for Sha2512Ctx { | ||
fn as_ctx_mut(&mut self) -> &mut cx_hash_t { | ||
&mut self.ctx.header | ||
} | ||
|
||
fn as_ctx(&self) -> &cx_hash_t { | ||
&self.ctx.header | ||
} | ||
|
||
fn finalize(&mut self, output: &mut [u8]) -> Result<(), CxError> { | ||
let err = | ||
unsafe { cx_hash_final(&mut self.ctx.header as *mut cx_hash_t, output.as_mut_ptr()) }; | ||
fn init() -> Result<Self, CxError> { | ||
let mut ctx: Sha2512Ctx = Default::default(); | ||
let err = unsafe { cx_sha512_init_no_throw(&mut ctx.ctx) }; | ||
if err != CX_OK { | ||
crate::testing::debug_print("Error after final()\n"); | ||
crate::testing::debug_print("Error after init()\n"); | ||
Err(CxError::GenericError) | ||
} else { | ||
Ok(()) | ||
Ok(ctx) | ||
} | ||
} | ||
} |
Oops, something went wrong.