Skip to content

Commit

Permalink
feat(hmac): add support for sha224 and sha384
Browse files Browse the repository at this point in the history
  • Loading branch information
bboilot-ledger committed Aug 19, 2024
1 parent 672e5b9 commit dfe8eb3
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 8 deletions.
30 changes: 29 additions & 1 deletion ledger_device_sdk/src/hmac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,42 @@ macro_rules! impl_hmac {

fn new(key: &[u8]) -> Self {
let mut ctx: $typename = Default::default();
let _err = unsafe { $initfname(&mut ctx.ctx, key.as_ptr(), key.len()) };
let _err = unsafe { $initfname(&mut ctx.ctx, key.as_ptr(), key.len() as u32) };
ctx
}
}
};
}
pub(crate) use impl_hmac;

/// Same macro as impl_hmac. The only difference is that key.len() is not casted as usize
/// because C SDK init fonction suffixed with _no_throw uses a size_t type for key length variable.
macro_rules! impl_hmac_no_throw {
($typename:ident, $ctxname:ident, $initfname:ident) => {
#[derive(Default)]
#[allow(non_camel_case_types)]
pub struct $typename {
ctx: $ctxname,
}
impl HMACInit for $typename {
fn as_ctx_mut(&mut self) -> &mut cx_hmac_t {
unsafe { mem::transmute::<&mut $ctxname, &mut cx_hmac_t>(&mut self.ctx) }
}

fn as_ctx(&self) -> &cx_hmac_t {
unsafe { mem::transmute::<&$ctxname, &cx_hmac_t>(&self.ctx) }
}

fn new(key: &[u8]) -> Self {
let mut ctx: $typename = Default::default();
let _err = unsafe { $initfname(&mut ctx.ctx, key.as_ptr(), key.len()) };
ctx
}
}
};
}
pub(crate) use impl_hmac_no_throw;

#[cfg(test)]
mod tests {
use crate::assert_eq_err as assert_eq;
Expand Down
4 changes: 2 additions & 2 deletions ledger_device_sdk/src/hmac/ripemd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use super::HMACInit;
use core::mem;
use ledger_secure_sdk_sys::{cx_hmac_ripemd160_init_no_throw, cx_hmac_ripemd160_t, cx_hmac_t};

use super::impl_hmac;
impl_hmac!(
use super::impl_hmac_no_throw;
impl_hmac_no_throw!(
Ripemd160,
cx_hmac_ripemd160_t,
cx_hmac_ripemd160_init_no_throw
Expand Down
44 changes: 39 additions & 5 deletions ledger_device_sdk/src/hmac/sha2.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use super::HMACInit;
use core::mem;
use ledger_secure_sdk_sys::{
cx_hmac_sha256_init_no_throw, cx_hmac_sha256_t, cx_hmac_sha512_init_no_throw, cx_hmac_sha512_t,
cx_hmac_t,
cx_hmac_sha224_init, cx_hmac_sha256_init_no_throw, cx_hmac_sha256_t, cx_hmac_sha384_init,
cx_hmac_sha512_init_no_throw, cx_hmac_sha512_t, cx_hmac_t,
};

use super::impl_hmac;
impl_hmac!(Sha2_256, cx_hmac_sha256_t, cx_hmac_sha256_init_no_throw);
impl_hmac!(Sha2_512, cx_hmac_sha512_t, cx_hmac_sha512_init_no_throw);
use super::{impl_hmac, impl_hmac_no_throw};
impl_hmac!(Sha2_224, cx_hmac_sha256_t, cx_hmac_sha224_init);
impl_hmac_no_throw!(Sha2_256, cx_hmac_sha256_t, cx_hmac_sha256_init_no_throw);
impl_hmac!(Sha2_384, cx_hmac_sha512_t, cx_hmac_sha384_init);
impl_hmac_no_throw!(Sha2_512, cx_hmac_sha512_t, cx_hmac_sha512_init_no_throw);

#[cfg(test)]
mod tests {
Expand All @@ -19,6 +21,21 @@ mod tests {
const TEST_MSG: &[u8; 29] = b"Not your keys, not your coins";
const TEST_KEY: &[u8; 16] = b"hmac test key!!!";

#[test]
fn test_hmac_sha224() {
let mut mac = Sha2_224::new(TEST_KEY);

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

let _ = mac.hmac(TEST_MSG, &mut output);

let expected = [
0xc4, 0x64, 0x80, 0xfb, 0xea, 0xc7, 0x75, 0x6d, 0xee, 0xc1, 0x6a, 0xcb, 0x6d, 0xae,
0x6a, 0xfa, 0x5d, 0x03, 0x17, 0x73, 0xd6, 0x4d, 0x49, 0xea, 0xa8, 0x5e, 0x4c, 0x1d,
];
assert_eq!(&output, &expected);
}

#[test]
fn test_hmac_sha256() {
let mut mac = Sha2_256::new(TEST_KEY);
Expand All @@ -35,6 +52,23 @@ mod tests {
assert_eq!(&output, &expected);
}

#[test]
fn test_hmac_sha384() {
let mut mac = Sha2_384::new(TEST_KEY);

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

let _ = mac.hmac(TEST_MSG, &mut output);

let expected = [
0x20, 0x6d, 0x0d, 0xfd, 0xfd, 0x22, 0x43, 0xde, 0xb0, 0x23, 0xf8, 0x56, 0x63, 0xd1,
0xa2, 0x1e, 0xc1, 0x6a, 0x72, 0x6b, 0xa7, 0x8e, 0xc2, 0x25, 0xe2, 0x1e, 0x3e, 0x3b,
0xb2, 0xf0, 0x55, 0x1d, 0x4e, 0xde, 0x5f, 0x81, 0xf6, 0xa1, 0xff, 0x8e, 0x76, 0x86,
0xf1, 0x0f, 0x7a, 0xec, 0xbe, 0x35,
];
assert_eq!(&output, &expected);
}

#[test]
fn test_hmac_sha512() {
let mut mac = Sha2_512::new(TEST_KEY);
Expand Down

0 comments on commit dfe8eb3

Please sign in to comment.