add tests to bcrypt and fix some corner cases

This commit is contained in:
bunnie 2021-07-30 15:28:47 +08:00
parent 919e9e87ff
commit 151d1ee229
4 changed files with 79 additions and 27 deletions

View file

@ -30,3 +30,10 @@ utralib = { path = "../../utralib"}
[features]
default = []
[lib]
test = true
[[bin]]
name = "root-keys"
test = false

View file

@ -24,12 +24,33 @@ fn setup(cost: u32, salt: &[u8], key: &[u8]) -> Blowfish {
state
}
pub fn bcrypt(cost: u32, salt: &[u8], password: &[u8], output: &mut [u8]) {
pub fn bcrypt(cost: u32, salt: &[u8], pw: &str, output: &mut [u8]) {
assert!(salt.len() == 16);
assert!(!password.is_empty() && password.len() <= 72);
assert!(output.len() == 24);
let state = setup(cost, salt, password);
let pw_len = if pw.len() > 72 {
log::warn!("password of length {} is truncated to 72 bytes [reason: bcrypt limitation]", pw.len());
72
} else {
pw.len() + 1
};
let mut plaintext_copy: [u8; 73] = [0; 73];
for (src, dst) in pw.bytes().zip(plaintext_copy.iter_mut()) {
*dst = src;
}
plaintext_copy[72] = 0; // always null terminate
let state = setup(cost, salt, &plaintext_copy[..pw_len]);
// erase the plaintext copy as soon as we're done with it
// an unsafe method is used because the compiler will correctly reason that plaintext_copy goes out of scope
// and these writes are never read, and therefore they may be optimized out.
let pt_ptr = plaintext_copy.as_mut_ptr();
for i in 0..plaintext_copy.len() {
unsafe{pt_ptr.add(i).write_volatile(core::mem::zeroed());}
}
core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst);
// OrpheanBeholderScryDoubt
#[allow(clippy::unreadable_literal)]
let mut ctext = [
@ -49,6 +70,3 @@ pub fn bcrypt(cost: u32, salt: &[u8], password: &[u8], output: &mut [u8]) {
output[(i + 1) * 4..][..4].copy_from_slice(&buf);
}
}
//// TODO: add at least one test vector to confirm this implementation isn't
//// totally braindead...!

View file

@ -211,18 +211,8 @@ impl RootKeys {
}
}
/// plaintext password is passed as a &str. Any copies internally are destroyed.
/// plaintext password is passed as a &str. Any copies internally are destroyed. Caller is responsible for destroying the &str original.
pub fn hash_and_save_password(&mut self, pw: &str, pw_type: PasswordType) {
let pw_len = if pw.len() > 72 {
log::warn!("password of length {} is truncated to 72 bytes [bcrypt]", pw.len());
72
} else {
pw.len()
};
let mut plaintext_copy: [u8; 72] = [0; 72];
for (src, dst) in pw.bytes().zip(plaintext_copy.iter_mut()) {
*dst = src;
}
let mut hashed_password: [u8; 24] = [0; 24];
let salt = self.read_key_128(KeyRomLocations::PEPPER);
@ -231,7 +221,7 @@ impl RootKeys {
// create any extra copies of the plaintext anywhere -- the &[u8] gets dereferenced and mixed up
// in some key expansion immediately
let start_time = timer.elapsed_ms();
bcrypt(BCRYPT_COST, &salt, &plaintext_copy[..pw_len], &mut hashed_password);
bcrypt(BCRYPT_COST, &salt, pw, &mut hashed_password); // note: this internally makes a copy of the password, and destroys it
let elapsed = timer.elapsed_ms() - start_time;
log::info!("bcrypt cost: {} time: {}ms", BCRYPT_COST, elapsed); // benchmark to figure out how to set cost parameter
@ -252,14 +242,6 @@ impl RootKeys {
}
}
}
// an unsafe method is used because the compiler will correctly reason that plaintext_copy goes out of scope
// and these writes are never read, and therefore they may be optimized out.
let pt_ptr = plaintext_copy.as_mut_ptr();
for i in 0..plaintext_copy.len() {
unsafe{pt_ptr.add(i).write_volatile(core::mem::zeroed());}
}
core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst);
}
fn update_progress(&mut self, maybe_cid: Option<xous::CID>, current_step: u32, total_steps: u32, finished: bool) {

View file

@ -1,4 +1,4 @@
#![cfg_attr(target_os = "none", no_std)]
#![cfg_attr(all(target_os = "none", not(test)), no_std)]
pub mod api;
use api::*;
@ -198,3 +198,48 @@ fn progress_cb_server(sid0: usize, sid1: usize, sid2: usize, sid3: usize) {
}
xous::destroy_server(sid).unwrap();
}
#[cfg(test)]
mod bcrypt;
// some short tests to just confirm we're not totally broken.
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn hash_with_fixed_salt() {
let salt: [u8; 16] = [
38, 113, 212, 141, 108, 213, 195, 166, 201, 38, 20, 13, 47, 40, 104, 18,
];
let mut output: [u8; 24] = [0; 24];
let pw = "My S3cre7 P@55w0rd!";
crate::bcrypt::bcrypt(5, &salt, pw, &mut output);
assert_eq!(output, [22, 80, 102, 192, 193, 204, 118, 167, 41, 102, 241, 75, 103, 49, 4, 245, 194, 145, 85, 104, 179, 60, 88, 53]);
}
#[test]
fn hash_with_max_len() {
let salt: [u8; 16] = [
38, 113, 212, 141, 108, 213, 195, 166, 201, 38, 20, 13, 47, 40, 104, 18,
];
let mut output: [u8; 24] = [0; 24];
let pw = "this is a test of a very long password that is exactly 72 characters lon";
crate::bcrypt::bcrypt(10, &salt, pw, &mut output);
assert_eq!(output, [46, 39, 41, 217, 39, 103, 62, 189, 120, 3, 248, 84, 175, 40, 134, 190, 76, 43, 232, 147, 129, 237, 116, 61]);
}
#[test]
fn hash_with_longer_than_max_len() {
let salt: [u8; 16] = [
38, 113, 212, 141, 108, 213, 195, 166, 201, 38, 20, 13, 47, 40, 104, 18,
];
let mut output: [u8; 24] = [0; 24];
let pw = "this is a test of a very long password that is exactly 72 characters long, but this one is even longer";
crate::bcrypt::bcrypt(10, &salt, pw, &mut output);
assert_eq!(output, [46, 39, 41, 217, 39, 103, 62, 189, 120, 3, 248, 84, 175, 40, 134, 190, 76, 43, 232, 147, 129, 237, 116, 61]);
}
}