libeufin

Integration and sandbox testing for FinTech APIs and data formats
Log | Files | Refs | Submodules | README | LICENSE

commit c3742bdc581de04665aeb114e30542608e806520
parent ba1bef8f271453ecf5da960f2ba5ccca54fffa7f
Author: Antoine A <>
Date:   Thu, 19 Nov 2026 12:18:37 +0100

libeufin-bank: constant-time password hash comparison

Diffstat:
Mlibeufin-bank/src/pw.rs | 15++++++++-------
1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/libeufin-bank/src/pw.rs b/libeufin-bank/src/pw.rs @@ -15,7 +15,7 @@ */ use anyhow::anyhow; -use aws_lc_rs::digest::SHA256; +use aws_lc_rs::{constant_time::verify_slices_are_equal, digest::SHA256}; use rand::{TryRng as _, rngs::SysRng}; use taler_api::error::{ApiResult, failure}; use taler_common::{encoding::base64, error_code::ErrorCode}; @@ -99,14 +99,12 @@ impl PwCrypto { "sha256" => { let [hash] = split_n(args, '$').ok_or(anyhow!("bad password hash format"))?; let pwh = aws_lc_rs::digest::digest(&SHA256, pw.as_bytes()); - let pwh = base64::encode(pwh); - (pwh == hash, true) + (hash_matches(pwh, hash), true) } "sha256-salted" => { let [salt, hash] = split_n(args, '$').ok_or(anyhow!("bad password hash format"))?; let pwh = aws_lc_rs::digest::digest(&SHA256, format!("{salt}|{pw}").as_bytes()); - let pwh = base64::encode(pwh); - (pwh == hash, true) + (hash_matches(pwh, hash), true) } "bcrypt" => { let [cost, salt, hash] = @@ -116,9 +114,8 @@ impl PwCrypto { .try_into() .map_err(|_| anyhow!("bad password hash format"))?; let pwh = bcrypt(cost, salt, pw.as_bytes()); - let pwh = base64::encode(pwh); ( - pwh == hash, + hash_matches(pwh, hash), match self { PwCrypto::Bcrypt { cost: expected } => cost != *expected, PwCrypto::Sha256 => false, @@ -132,6 +129,10 @@ impl PwCrypto { } } +fn hash_matches(computed: impl AsRef<[u8]>, stored: &str) -> bool { + verify_slices_are_equal(base64::encode(computed).as_bytes(), stored.as_bytes()).is_ok() +} + fn split_n<const N: usize>(input: &str, sep: char) -> Option<[&str; N]> { let mut iter = input.split(sep);