Skip to content

Commit

Permalink
excise scrypt C source
Browse files Browse the repository at this point in the history
  • Loading branch information
habnabit committed Jul 4, 2024
1 parent 5deb4d8 commit 5a16e65
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 45 deletions.
137 changes: 137 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ rand = "0.8"
num-bigint = { version = "0.4", features = ["rand"] }
thiserror = "1.0.61"
num-traits = "0.2.19"
scrypt = { version = "0.11.0", default-features = false }

[build-dependencies]
cc = "1.0.104"
Expand Down
16 changes: 2 additions & 14 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
fn will_compile_p(cfg: &cc::Build, file: &str) -> bool {
match cfg.get_compiler()
.to_command()
.arg("-c")
.arg(file)
.output()
{
match cfg.get_compiler().to_command().arg("-c").arg(file).output() {
Ok(o) => o.status.success() && o.stderr.is_empty(),
Err(e) => panic!("compilation failed to start for {file:?}: {e:#?}"),
}
Expand All @@ -30,26 +25,19 @@ fn main() {
cfg.define("HAVE_POSIX_MEMALIGN", "1");
}
}
if will_compile_p(&cfg, "src/scrypt/crypto_scrypt-sse.c") {
cfg.file("src/scrypt/crypto_scrypt-sse.c");
} else {
cfg.file("src/scrypt/crypto_scrypt-nosse.c");
}
if will_compile_p(&cfg, "src/keccak/KeccakF-1600-opt64.c") {
cfg.file("src/keccak/KeccakF-1600-opt64.c");
} else {
cfg.file("src/keccak/KeccakF-1600-opt32.c");
}

cfg
.file("src/keccak/KeccakSponge.c")
cfg.file("src/keccak/KeccakSponge.c")
.file("src/skein/skein.c")
.file("src/skein/skeinBlockNo3F.c")
.file("src/skein/threefish256Block.c")
.file("src/skein/threefish512Block.c")
.file("src/skein/threefish1024Block.c")
.file("src/skein/skeinApi.c")
.file("src/skein/threefishApi.c")
.file("src/scrypt/sha256.c")
.compile("passacre_deps");
}
37 changes: 11 additions & 26 deletions src/passacre.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,38 +69,23 @@ enum State {
}

pub enum Kdf {
Scrypt { n: u64, r: u32, p: u32 },
Scrypt(scrypt::Params),
}

impl Kdf {
pub fn new_scrypt(n: u64, r: u32, p: u32) -> Kdf {
Kdf::Scrypt { n: n, r: r, p: p }
pub fn new_scrypt(n: u64, r: u32, p: u32) -> PassacreResult<Kdf> {
let log_n = (n as f64).log2() as u8;
let params = scrypt::Params::new(log_n, r, p, SCRYPT_BUFFER_SIZE).map_err(|_| UserError)?;
Ok(Kdf::Scrypt(params))
}

pub fn derive(&mut self, username: &[u8], password: &[u8]) -> PassacreResult<Vec<u8>> {
pub fn derive(&self, username: &[u8], password: &[u8]) -> PassacreResult<Vec<u8>> {
match self {
&mut Kdf::Scrypt { n, r, p } => {
testing_fail!(n == 99 && r == 99 && p == 99, ScryptError);
let mut scrypt_result = vec![0u8; SCRYPT_BUFFER_SIZE];
{
decompose!(username);
decompose!(password);
decompose!(mut scrypt_result);
check_eq!(0, ScryptError, unsafe {
crate::deps::crypto_scrypt(
password.0,
password.1,
username.0,
username.1,
n,
r,
p,
scrypt_result.0,
scrypt_result.1,
)
});
}
Ok(scrypt_result)
Kdf::Scrypt(params) => {
// testing_fail!(n == 99 && r == 99 && p == 99, ScryptError);
let mut ret = vec![0u8; SCRYPT_BUFFER_SIZE];
scrypt::scrypt(password, username, params, &mut ret).map_err(|_| InternalError)?;
Ok(ret)
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ fn derive(
nulls += item_value.extract::<usize>()?;
}
"scrypt" => {
generator.use_kdf(Kdf::Scrypt {
n: item_value.get_item("n")?.extract()?,
r: item_value.get_item("r")?.extract()?,
p: item_value.get_item("p")?.extract()?,
})?;
generator.use_kdf(Kdf::new_scrypt(
item_value.get_item("n")?.extract()?,
item_value.get_item("r")?.extract()?,
item_value.get_item("p")?.extract()?,
)?)?;
}
_ => {}
}
Expand Down

0 comments on commit 5a16e65

Please sign in to comment.