Avoid get_ in functions

Also simplify the hashing function
This commit is contained in:
Bilal Elmoussaoui 2025-02-16 16:44:23 +01:00
parent a1241272c8
commit 6382ec4c68
5 changed files with 18 additions and 25 deletions

View file

@ -134,23 +134,23 @@ pub enum HashAlgo {
impl HashAlgo {
/// returns a function capable of hashing a byte array
/// necessary to be able to validate keys before enrolling them on the hardware key
pub fn get_hash_fun(&self) -> impl Fn(&[u8]) -> Vec<u8> {
pub(crate) fn hash(&self, key: &[u8]) -> Vec<u8> {
match self {
Self::Sha1 => |m: &[u8]| {
Self::Sha1 => {
let mut hasher = sha1::Sha1::new();
hasher.update(m);
hasher.update(key);
hasher.finalize().to_vec()
},
Self::Sha256 => |m: &[u8]| {
}
Self::Sha256 => {
let mut hasher = sha2::Sha256::new();
hasher.update(m);
hasher.update(key);
hasher.finalize().to_vec()
},
Self::Sha512 => |m: &[u8]| {
}
Self::Sha512 => {
let mut hasher = sha2::Sha512::new();
hasher.update(m);
hasher.update(key);
hasher.finalize().to_vec()
},
}
}
}

View file

@ -60,7 +60,7 @@ fn main() {
println!("freshly defined oath: {}", calculated);
session.delete_code(cred).unwrap(); */
println!("YubiKey version is {:?}", session.get_version());
println!("YubiKey version is {:?}", session.version());
for c in session.list_oath_codes().unwrap() {
println!("{}", c);
}

View file

@ -32,7 +32,7 @@ fn hmac_sha1(key: &[u8], message: &[u8]) -> Vec<u8> {
fn hmac_shorten_key(key: &[u8], algo: HashAlgo) -> Vec<u8> {
if key.len() > algo.digest_size() {
algo.get_hash_fun()(key)
algo.hash(key)
} else {
key.to_vec()
}
@ -82,7 +82,7 @@ impl OathSession {
})
}
pub fn get_version(&self) -> &[u8] {
pub fn version(&self) -> &[u8] {
&self.version
}
@ -270,7 +270,7 @@ impl OathSession {
if cred.id_data.oath_type == OathType::Totp {
data.extend(to_tlv(
Tag::Challenge,
&time_challenge(Some(timestamp), cred.id_data.get_period()),
&time_challenge(Some(timestamp), cred.id_data.period()),
));
}

View file

@ -56,7 +56,7 @@ impl CredentialIDData {
}
/// Returns the defined period or default
pub fn get_period(&self) -> Duration {
pub fn period(&self) -> Duration {
self.period.unwrap_or(DEFAULT_PERIOD)
}

View file

@ -66,18 +66,11 @@ impl<'a> RefreshableOathCredential<'a> {
.duration_since(SystemTime::UNIX_EPOCH)
.as_ref()
.map_or(0, Duration::as_secs);
let time_step = timestamp_seconds / (self.cred.id_data.get_period().as_secs());
let time_step = timestamp_seconds / (self.cred.id_data.period().as_secs());
let valid_from = SystemTime::UNIX_EPOCH
.checked_add(
self.cred
.id_data
.get_period()
.saturating_mul(time_step as u32),
)
.unwrap();
let valid_to = valid_from
.checked_add(self.cred.id_data.get_period())
.checked_add(self.cred.id_data.period().saturating_mul(time_step as u32))
.unwrap();
let valid_to = valid_from.checked_add(self.cred.id_data.period()).unwrap();
valid_from..valid_to
}
OathType::Hotp => {