Skip to content

Improve byte array serialization #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ description = "Authentication extension and backend for Trussed"

[dependencies]
serde = { version = "1", default-features = false }
serde-byte-array = "0.1.0"
sha2 = { version = "0.10.6", default-features = false }
subtle = { version = "2.4.1", default-features = false }
trussed = { git = "https://github.com/trussed-dev/trussed", rev = "1c55b3b2dd6a9e1cfc55758635baf0d0bbf387d1", features = ["serde-extensions"] }
Expand Down
29 changes: 21 additions & 8 deletions src/backend/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use core::ops::Deref;

use serde::{Deserialize, Serialize};
use serde_byte_array::ByteArray;
use sha2::{Digest as _, Sha256};
use subtle::ConstantTimeEq as _;
use trussed::{
Expand All @@ -19,8 +20,8 @@ const SIZE: usize = 256;
const SALT_LEN: usize = 16;
const HASH_LEN: usize = 32;

type Salt = [u8; SALT_LEN];
type Hash = [u8; HASH_LEN];
type Salt = ByteArray<SALT_LEN>;
type Hash = ByteArray<HASH_LEN>;

#[derive(Debug, Deserialize, Serialize)]
pub(crate) struct PinData {
Expand All @@ -37,7 +38,7 @@ impl PinData {
R: CryptoRng + RngCore,
{
let mut salt = Salt::default();
rng.fill_bytes(&mut salt);
rng.fill_bytes(salt.as_mut());
let hash = hash(id, pin, &salt);
Self {
id,
Expand Down Expand Up @@ -111,7 +112,10 @@ impl<'a> PinDataMut<'a> {
if self.is_blocked() {
return false;
}
let success = hash(self.id, pin, &self.salt).ct_eq(&self.hash).into();
let success = hash(self.id, pin, &self.salt)
.as_ref()
.ct_eq(self.hash.as_ref())
.into();
if let Some(retries) = &mut self.data.retries {
if success {
if retries.reset() {
Expand Down Expand Up @@ -169,8 +173,8 @@ fn hash(id: PinId, pin: &Pin, salt: &Salt) -> Hash {
digest.update([u8::from(id)]);
digest.update([pin_len(pin)]);
digest.update(pin);
digest.update(salt);
digest.finalize().into()
digest.update(salt.as_ref());
Hash::new(digest.finalize().into())
}

fn pin_len(pin: &Pin) -> u8 {
Expand All @@ -191,10 +195,19 @@ mod tests {
max: u8::MAX,
left: u8::MAX,
}),
salt: [u8::MAX; SALT_LEN],
hash: [u8::MAX; HASH_LEN],
salt: [u8::MAX; SALT_LEN].into(),
hash: [u8::MAX; HASH_LEN].into(),
};
let serialized = trussed::cbor_serialize_bytes::<_, 1024>(&data).unwrap();
assert!(serialized.len() <= SIZE);
}

#[test]
#[allow(clippy::unwrap_used)]
fn test_salt_size() {
// We allow one byte overhead for byte array serialization
let salt = Salt::from([u8::MAX; SALT_LEN]);
let serialized = trussed::cbor_serialize_bytes::<_, 1024>(&salt).unwrap();
assert!(serialized.len() <= SALT_LEN + 1, "{}", serialized.len());
}
}