Skip to content
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

thiserror implementation #38

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
3 changes: 2 additions & 1 deletion argonautica-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ simd = []
[dependencies]
base64 = "0.10"
bitflags = "1.1"
failure = "0.1"
futures = "0.1"
futures-cpupool = "0.1"
libc = "0.2"
Expand All @@ -38,6 +37,8 @@ num_cpus = "1.10"
rand = "0.7"
scopeguard = "1.0"
serde = { version = "1.0", optional = true, features = ["derive"] }
# Error
thiserror = "1.0"

# benches
argon2rs = { version = "0.2.5", optional = true }
Expand Down
4 changes: 2 additions & 2 deletions argonautica-rs/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ extern crate bindgen;
extern crate cc;
#[macro_use]
extern crate cfg_if;
extern crate failure;
extern crate tempfile;

use std::env;
use std::error::Error;
use std::fs;
use std::path::Path;

Expand All @@ -17,7 +17,7 @@ cfg_if! {
}
}

fn main() -> Result<(), failure::Error> {
fn main() -> Result<(), Box<dyn Error>> {
let temp = tempfile::tempdir()?;
let temp_dir = temp.path();
let temp_dir_str = temp_dir.to_str().unwrap();
Expand Down
15 changes: 5 additions & 10 deletions argonautica-rs/src/backend/c/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::ffi::CStr;
use libc;

use output::HashRaw;
use {ffi, Error, ErrorKind};
use {ffi, Error};

pub(crate) fn encode_c(hash_raw: &HashRaw) -> Result<String, Error> {
let encoded_len = unsafe {
Expand Down Expand Up @@ -52,19 +52,14 @@ pub(crate) fn encode_c(hash_raw: &HashRaw) -> Result<String, Error> {
fn check_error(err: ffi::Argon2_ErrorCodes, hash_raw: &HashRaw) -> Result<(), Error> {
match err {
ffi::Argon2_ErrorCodes_ARGON2_OK => Ok(()),
ffi::Argon2_ErrorCodes_ARGON2_MEMORY_ALLOCATION_ERROR => {
Err(Error::new(ErrorKind::MemoryAllocationError))
}
ffi::Argon2_ErrorCodes_ARGON2_THREAD_FAIL => Err(Error::new(ErrorKind::ThreadError)),
ffi::Argon2_ErrorCodes_ARGON2_ENCODING_FAIL => {
Err(Error::new(ErrorKind::HashEncodeError)
.add_context(format!("HashRaw: {:?}", hash_raw)))
}
ffi::Argon2_ErrorCodes_ARGON2_MEMORY_ALLOCATION_ERROR => Err(Error::MemoryAllocationError),
ffi::Argon2_ErrorCodes_ARGON2_THREAD_FAIL => Err(Error::ThreadError),
ffi::Argon2_ErrorCodes_ARGON2_ENCODING_FAIL => Err(Error::HashEncodeError),
_ => {
let err_msg_ptr = unsafe { ffi::argon2_error_message(err) };
let err_msg_cstr = unsafe { CStr::from_ptr(err_msg_ptr) };
let err_msg = err_msg_cstr.to_str().unwrap(); // Safe; see argon2_error_message
Err(Error::new(ErrorKind::Bug).add_context(format!(
Err(Error::Bug(format!(
"Unhandled error from C. Error code: {}. Error {}",
err, err_msg,
)))
Expand Down
18 changes: 9 additions & 9 deletions argonautica-rs/src/backend/c/hash_raw.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::ffi::CStr;

use output::HashRaw;
use {ffi, Error, ErrorKind, Hasher};
use {ffi, Error, Hasher};

impl<'a> Hasher<'a> {
pub(crate) fn hash_raw_c(&mut self) -> Result<HashRaw, Error> {
Expand All @@ -17,7 +17,7 @@ impl<'a> Hasher<'a> {
password.as_bytes().as_ptr() as *mut u8,
password.len() as u32,
),
None => return Err(Error::new(ErrorKind::PasswordMissingError)),
None => return Err(Error::PasswordMissingError),
};
let (secret, secretlen) = match self.secret_key {
Some(ref mut secret_key) => (
Expand Down Expand Up @@ -66,19 +66,19 @@ impl<'a> Hasher<'a> {
fn check_error(err: ffi::Argon2_ErrorCodes) -> Result<(), Error> {
match err {
ffi::Argon2_ErrorCodes_ARGON2_OK => Ok(()),
ffi::Argon2_ErrorCodes_ARGON2_MEMORY_ALLOCATION_ERROR => {
Err(Error::new(ErrorKind::MemoryAllocationError))
}
ffi::Argon2_ErrorCodes_ARGON2_THREAD_FAIL => Err(Error::new(ErrorKind::ThreadError)),
ffi::Argon2_ErrorCodes_ARGON2_MEMORY_ALLOCATION_ERROR => Err(Error::MemoryAllocationError),
ffi::Argon2_ErrorCodes_ARGON2_THREAD_FAIL => Err(Error::ThreadError),
_ => {
let err_msg_ptr = unsafe { ffi::argon2_error_message(err) };
if err_msg_ptr.is_null() {
return Err(Error::new(ErrorKind::Bug)
.add_context(format!("Unhandled error from C. Error code: {}", err,)));
return Err(Error::Bug(format!(
"Unhandled error from C. Error code: {}",
err,
)));
}
let err_msg_cstr = unsafe { CStr::from_ptr(err_msg_ptr) };
let err_msg = err_msg_cstr.to_str().unwrap(); // Safe; see argon2_error_message
Err(Error::new(ErrorKind::Bug).add_context(format!(
Err(Error::Bug(format!(
"Unhandled error from C. Error code: {}. Error {}",
err, err_msg,
)))
Expand Down
14 changes: 6 additions & 8 deletions argonautica-rs/src/backend/rust/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@ use base64;

use config::{Variant, Version};
use output::HashRaw;
use {Error, ErrorKind};
use Error;

pub(crate) fn decode_rust(hash: &str) -> Result<HashRaw, Error> {
let (rest, intermediate) = parse_hash(hash).map_err(|_| {
Error::new(ErrorKind::HashDecodeError).add_context(format!("Hash: {}", &hash))
})?;
let raw_hash_bytes = base64::decode_config(rest, base64::STANDARD_NO_PAD).map_err(|_| {
Error::new(ErrorKind::HashDecodeError).add_context(format!("Hash: {}", &hash))
})?;
let (rest, intermediate) = parse_hash(hash).map_err(|e| Error::HashDecodeError(e))?;
let raw_hash_bytes = base64::decode_config(rest, base64::STANDARD_NO_PAD)
.map_err(|e| Error::HashDecodeError(e))?;
let hash_raw = HashRaw {
iterations: intermediate.iterations,
lanes: intermediate.lanes,
Expand Down Expand Up @@ -71,10 +68,11 @@ mod tests {
use rand::rngs::StdRng;
use rand::{RngCore, SeedableRng};

use super::*;
use backend::c::decode_c;
use hasher::Hasher;

use super::*;

#[test]
fn test_decode() {
let hash = "$argon2id$v=19$m=4096,t=128,p=2$gt4I/z7gnC8Ao0ofCFvz+2LGxI3it1TnCnlxn0PWKko$v6V587B9qbKraulhK/6vFUq93BGWugdzgRhtyap9tDM";
Expand Down
4 changes: 2 additions & 2 deletions argonautica-rs/src/config/backend.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use config::defaults::DEFAULT_BACKEND;
use {Error, ErrorKind};
use Error;

impl Default for Backend {
/// Returns [`Backend::C`](enum.Backend.html#variant.C)
Expand Down Expand Up @@ -35,7 +35,7 @@ impl Backend {
match x {
1 => Ok(Backend::C),
2 => Ok(Backend::Rust),
_ => Err(Error::new(ErrorKind::BackendEncodeError).add_context(format!("Int: {}", x))),
_ => Err(Error::BackendEncodeError),
}
}
}
Expand Down
32 changes: 10 additions & 22 deletions argonautica-rs/src/config/hasher_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use futures_cpupool::CpuPool;

use config::defaults::*;
use config::{Backend, Flags, Variant, Version};
use {Error, ErrorKind};
use Error;

const PANIC_WARNING: &str = "Your program will error if you use this configuration";

Expand Down Expand Up @@ -192,63 +192,51 @@ impl HasherConfig {
fn validate_backend(backend: Backend) -> Result<(), Error> {
match backend {
Backend::C => (),
Backend::Rust => return Err(Error::new(ErrorKind::BackendUnsupportedError)),
Backend::Rust => return Err(Error::BackendUnsupportedError),
}
Ok(())
}

fn validate_hash_len(hash_len: u32) -> Result<(), Error> {
if hash_len < 4 {
return Err(Error::new(ErrorKind::HashLenTooShortError)
.add_context(format!("Hash len: {}", hash_len)));
return Err(Error::HashLenTooShortError(hash_len));
}
Ok(())
}

fn validate_iterations(iterations: u32) -> Result<(), Error> {
if iterations == 0 {
return Err(Error::new(ErrorKind::IterationsTooFewError)
.add_context(format!("Iterations: {}", iterations)));
return Err(Error::IterationsTooFewError(iterations));
}
Ok(())
}

fn validate_lanes(lanes: u32) -> Result<(), Error> {
if lanes == 0 {
return Err(
Error::new(ErrorKind::LanesTooFewError).add_context(format!("Lanes: {}", lanes))
);
return Err(Error::LanesTooFewError(lanes));
}
if lanes > 0x00ff_ffff {
return Err(
Error::new(ErrorKind::LanesTooManyError).add_context(format!("Lanes: {}", lanes))
);
return Err(Error::LanesTooManyError(lanes));
}
Ok(())
}

fn validate_memory_size(lanes: u32, memory_size: u32) -> Result<(), Error> {
if memory_size < 8 * lanes {
return Err(Error::new(ErrorKind::MemorySizeTooSmallError)
.add_context(format!("Lanes: {}. Memory size: {}", lanes, memory_size)));
return Err(Error::MemorySizeTooSmallError { lanes, memory_size });
}
if !(memory_size.is_power_of_two()) {
return Err(Error::new(ErrorKind::MemorySizeInvalidError)
.add_context(format!("Memory size: {}", memory_size)));
return Err(Error::MemorySizeInvalidError(memory_size));
}
Ok(())
}

fn validate_threads(threads: u32) -> Result<(), Error> {
if threads == 0 {
return Err(
Error::new(ErrorKind::ThreadsTooFewError).add_context(format!("Threads: {}", threads))
);
return Err(Error::ThreadsTooFewError(threads));
}
if threads > 0x00ff_ffff {
return Err(
Error::new(ErrorKind::ThreadsTooManyError).add_context(format!("Threads: {}", threads))
);
return Err(Error::ThreadsTooManyError(threads));
}
Ok(())
}
Expand Down
6 changes: 2 additions & 4 deletions argonautica-rs/src/config/variant.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::str::FromStr;

use config::defaults::DEFAULT_VARIANT;
use {Error, ErrorKind};
use Error;

impl Default for Variant {
/// Returns [`Variant::Argon2id`](enum.Variant.html#variant.Argon2id)
Expand All @@ -24,9 +24,7 @@ impl FromStr for Variant {
"argon2d" => Ok(Variant::Argon2d),
"argon2i" => Ok(Variant::Argon2i),
"argon2id" => Ok(Variant::Argon2id),
_ => {
Err(Error::new(ErrorKind::VariantEncodeError).add_context(format!("String: {}", s)))
}
_ => Err(Error::VariantEncodeError(s.to_string())),
}
}
}
Expand Down
8 changes: 3 additions & 5 deletions argonautica-rs/src/config/version.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::str::FromStr;

use config::defaults::DEFAULT_VERSION;
use {Error, ErrorKind};
use Error;

impl Default for Version {
/// Returns [`Version::_0x13`](enum.Version.html#variant._0x13)
Expand All @@ -22,9 +22,7 @@ impl FromStr for Version {
match s {
"16" => Ok(Version::_0x10),
"19" => Ok(Version::_0x13),
_ => {
Err(Error::new(ErrorKind::VersionEncodeError).add_context(format!("String: {}", s)))
}
_ => Err(Error::VersionEncodeError(format!("String: {}", s))),
}
}
}
Expand Down Expand Up @@ -61,7 +59,7 @@ impl Version {
match x {
16 => Ok(Version::_0x10),
19 => Ok(Version::_0x13),
_ => Err(Error::new(ErrorKind::VersionEncodeError).add_context(format!("Int: {}", x))),
_ => Err(Error::VersionEncodeError(format!("Int: {}", x))),
}
}
}
Expand Down
Loading