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

Remove constants module #72

Merged
merged 2 commits into from
May 1, 2019
Merged
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
13 changes: 8 additions & 5 deletions src/aead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,11 @@ use crate::{
errors::UnknownCryptoError,
hazardous::{
aead,
constants::{POLY1305_BLOCKSIZE, XCHACHA_NONCESIZE},
stream::{chacha20, xchacha20::Nonce},
mac::poly1305::POLY1305_OUTSIZE,
stream::{
chacha20,
xchacha20::{Nonce, XCHACHA_NONCESIZE},
},
},
};

Expand All @@ -95,7 +98,7 @@ pub fn seal(secret_key: &SecretKey, plaintext: &[u8]) -> Result<Vec<u8>, Unknown

let nonce = Nonce::generate();

let mut dst_out = vec![0u8; plaintext.len() + (XCHACHA_NONCESIZE + POLY1305_BLOCKSIZE)];
let mut dst_out = vec![0u8; plaintext.len() + (XCHACHA_NONCESIZE + POLY1305_OUTSIZE)];
dst_out[..XCHACHA_NONCESIZE].copy_from_slice(nonce.as_ref());

aead::xchacha20poly1305::seal(
Expand All @@ -116,12 +119,12 @@ pub fn open(
ciphertext_with_tag_and_nonce: &[u8],
) -> Result<Vec<u8>, UnknownCryptoError> {
// `+ 1` to avoid empty ciphertexts
if ciphertext_with_tag_and_nonce.len() < (XCHACHA_NONCESIZE + POLY1305_BLOCKSIZE + 1) {
if ciphertext_with_tag_and_nonce.len() < (XCHACHA_NONCESIZE + POLY1305_OUTSIZE + 1) {
return Err(UnknownCryptoError);
}

let mut dst_out =
vec![0u8; ciphertext_with_tag_and_nonce.len() - (XCHACHA_NONCESIZE + POLY1305_BLOCKSIZE)];
vec![0u8; ciphertext_with_tag_and_nonce.len() - (XCHACHA_NONCESIZE + POLY1305_OUTSIZE)];

aead::xchacha20poly1305::open(
&chacha20::SecretKey::from_slice(secret_key.unprotected_as_bytes())?,
Expand Down
5 changes: 2 additions & 3 deletions src/hazardous/aead/chacha20poly1305.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ pub use crate::hazardous::stream::chacha20::{Nonce, SecretKey};
use crate::{
errors::UnknownCryptoError,
hazardous::{
constants::{POLY1305_KEYSIZE, POLY1305_OUTSIZE},
mac::poly1305::{self, OneTimeKey},
mac::poly1305::{self, OneTimeKey, POLY1305_KEYSIZE, POLY1305_OUTSIZE},
stream::chacha20,
},
util,
Expand Down Expand Up @@ -656,7 +655,7 @@ mod private {

mod test_poly1305_key_gen {
use super::*;
use crate::hazardous::constants::{CHACHA_KEYSIZE, IETF_CHACHA_NONCESIZE};
use crate::hazardous::stream::chacha20::{CHACHA_KEYSIZE, IETF_CHACHA_NONCESIZE};

#[test]
fn test_key_lengths() {
Expand Down
5 changes: 2 additions & 3 deletions src/hazardous/aead/xchacha20poly1305.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ use crate::{
errors::UnknownCryptoError,
hazardous::{
aead::chacha20poly1305,
constants::IETF_CHACHA_NONCESIZE,
stream::chacha20::{self, Nonce as IETFNonce},
stream::chacha20::{self, Nonce as IETFNonce, IETF_CHACHA_NONCESIZE},
},
};

Expand Down Expand Up @@ -159,7 +158,7 @@ pub fn open(
#[cfg(test)]
mod public {
use super::*;
use crate::hazardous::constants::POLY1305_OUTSIZE;
use crate::hazardous::mac::poly1305::POLY1305_OUTSIZE;
// One function tested per submodule.

mod test_seal {
Expand Down
60 changes: 0 additions & 60 deletions src/hazardous/constants.rs

This file was deleted.

8 changes: 7 additions & 1 deletion src/hazardous/hash/blake2b.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,15 @@
use crate::{
endianness::{load_u64_into_le, store_u64_into_le},
errors::UnknownCryptoError,
hazardous::constants::{BLAKE2B_BLOCKSIZE, BLAKE2B_KEYSIZE, BLAKE2B_OUTSIZE},
};

/// The blocksize for the hash function BLAKE2b.
const BLAKE2B_BLOCKSIZE: usize = 128;
/// The maximum key size for the hash function BLAKE2b when used in keyed mode.
const BLAKE2B_KEYSIZE: usize = 64;
/// The maximum output size for the hash function BLAKE2b.
const BLAKE2B_OUTSIZE: usize = 64;

construct_secret_key! {
/// A type to represent the `SecretKey` that BLAKE2b uses for keyed mode.
///
Expand Down
6 changes: 5 additions & 1 deletion src/hazardous/hash/sha512.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,13 @@
use crate::{
endianness::{load_u64_into_be, store_u64_into_be},
errors::UnknownCryptoError,
hazardous::constants::{SHA512_BLOCKSIZE, SHA512_OUTSIZE},
};

/// The blocksize for the hash function SHA512.
pub const SHA512_BLOCKSIZE: usize = 128;
/// The output size for the hash function SHA512.
pub const SHA512_OUTSIZE: usize = 64;

construct_public! {
/// A type to represent the `Digest` that SHA512 returns.
///
Expand Down
2 changes: 1 addition & 1 deletion src/hazardous/kdf/hkdf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
use crate::{
errors::UnknownCryptoError,
hazardous::{
constants::SHA512_OUTSIZE,
hash::sha512::SHA512_OUTSIZE,
mac::hmac::{self, SecretKey},
},
util,
Expand Down
4 changes: 2 additions & 2 deletions src/hazardous/kdf/pbkdf2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
use crate::{
errors::UnknownCryptoError,
hazardous::{
constants::{HLenArray, SHA512_BLOCKSIZE, SHA512_OUTSIZE},
hash::sha512::{SHA512_BLOCKSIZE, SHA512_OUTSIZE},
mac::hmac,
},
util,
Expand Down Expand Up @@ -105,7 +105,7 @@ fn function_f(
block_len: usize,
hmac: &mut hmac::Hmac,
) -> Result<(), UnknownCryptoError> {
let mut u_step: HLenArray = [0u8; 64];
let mut u_step: [u8; SHA512_OUTSIZE] = [0u8; 64];
hmac.update(salt)?;
hmac.update(&index.to_be_bytes())?;

Expand Down
9 changes: 3 additions & 6 deletions src/hazardous/mac/hmac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,7 @@
use crate::{
errors::UnknownCryptoError,
hazardous::{
constants::{BlocksizeArray, SHA512_BLOCKSIZE, SHA512_OUTSIZE},
hash::sha512,
},
hazardous::hash::sha512::{self, SHA512_BLOCKSIZE, SHA512_OUTSIZE},
};
use zeroize::Zeroize;

Expand Down Expand Up @@ -117,8 +114,8 @@ impl Hmac {
#[inline]
/// Pad `key` with `ipad` and `opad`.
fn pad_key_io(&mut self, key: &SecretKey) {
let mut ipad: BlocksizeArray = [0x36; SHA512_BLOCKSIZE];
let mut opad: BlocksizeArray = [0x5C; SHA512_BLOCKSIZE];
let mut ipad = [0x36; SHA512_BLOCKSIZE];
let mut opad = [0x5C; SHA512_BLOCKSIZE];
// `key` has already been padded with zeroes to a length of SHA512_BLOCKSIZE
// in SecretKey::from_slice
for (idx, itm) in key.unprotected_as_bytes().iter().enumerate() {
Expand Down
10 changes: 9 additions & 1 deletion src/hazardous/mac/poly1305.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,17 @@ extern crate core;
use crate::{
endianness::{load_u32_le, store_u32_into_le},
errors::UnknownCryptoError,
hazardous::constants::{Poly1305Tag, POLY1305_BLOCKSIZE, POLY1305_KEYSIZE, POLY1305_OUTSIZE},
};

/// The blocksize which Poly1305 operates on.
const POLY1305_BLOCKSIZE: usize = 16;
/// The output size for Poly1305.
pub const POLY1305_OUTSIZE: usize = 16;
/// The key size for Poly1305.
pub const POLY1305_KEYSIZE: usize = 32;
/// Type for a Poly1305 tag.
type Poly1305Tag = [u8; POLY1305_OUTSIZE];

construct_secret_key! {
/// A type to represent the `OneTimeKey` that Poly1305 uses for authentication.
///
Expand Down
3 changes: 0 additions & 3 deletions src/hazardous/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,5 @@ pub mod mac;
/// Function).
pub mod kdf;

/// Constant values and types.
pub mod constants;

/// Stream ciphers.
pub mod stream;
22 changes: 14 additions & 8 deletions src/hazardous/stream/chacha20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,23 @@
use crate::{
endianness::{load_u32_into_le, store_u32_into_le},
errors::UnknownCryptoError,
hazardous::constants::{
ChaChaState,
CHACHA_BLOCKSIZE,
CHACHA_KEYSIZE,
HCHACHA_NONCESIZE,
HCHACHA_OUTSIZE,
IETF_CHACHA_NONCESIZE,
},
};
use zeroize::Zeroize;

/// The key size for ChaCha20.
pub const CHACHA_KEYSIZE: usize = 32;
/// The nonce size for IETF ChaCha20.
pub const IETF_CHACHA_NONCESIZE: usize = 12;
/// The blocksize which ChaCha20 operates on.
const CHACHA_BLOCKSIZE: usize = 64;
/// The size of the subkey that HChaCha20 returns.
const HCHACHA_OUTSIZE: usize = 32;
/// The nonce size for HChaCha20.
const HCHACHA_NONCESIZE: usize = 16;
/// Type for a ChaCha state represented as an array of 16 32-bit unsigned
/// integers.
type ChaChaState = [u32; 16];

construct_secret_key! {
/// A type to represent the `SecretKey` that `chacha20`, `xchacha20`, `chacha20poly1305` and
/// `xchacha20poly1305` use.
Expand Down
8 changes: 4 additions & 4 deletions src/hazardous/stream/xchacha20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@
pub use crate::hazardous::stream::chacha20::SecretKey;
use crate::{
errors::UnknownCryptoError,
hazardous::{
constants::{IETF_CHACHA_NONCESIZE, XCHACHA_NONCESIZE},
stream::chacha20::{self, Nonce as IETFNonce},
},
hazardous::stream::chacha20::{self, Nonce as IETFNonce, IETF_CHACHA_NONCESIZE},
};

/// The nonce size for XChaCha20.
pub const XCHACHA_NONCESIZE: usize = 24;

construct_public! {
/// A type that represents a `Nonce` that XChaCha20 and XChaCha20Poly1305 use.
///
Expand Down
3 changes: 1 addition & 2 deletions src/typedefs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,8 +566,7 @@ macro_rules! construct_hmac_key {
#[must_use]
/// Make an object from a given byte slice.
pub fn from_slice(slice: &[u8]) -> Result<$name, UnknownCryptoError> {
use crate::hazardous::hash::sha512;
use crate::hazardous::constants::SHA512_OUTSIZE;
use crate::hazardous::hash::sha512::{self, SHA512_OUTSIZE};

let mut secret_key = [0u8; $size];

Expand Down
9 changes: 6 additions & 3 deletions tests/aead/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ use self::{
},
orion::{
errors::UnknownCryptoError,
hazardous::{aead, constants},
hazardous::{
aead,
stream::{chacha20::IETF_CHACHA_NONCESIZE, xchacha20::XCHACHA_NONCESIZE},
},
},
};

Expand Down Expand Up @@ -44,7 +47,7 @@ fn aead_test_runner(
}

// Determine variant based on NONCE size
if nonce.len() == constants::IETF_CHACHA_NONCESIZE {
if nonce.len() == IETF_CHACHA_NONCESIZE {
aead::chacha20poly1305::seal(
&SecretKey::from_slice(&key).unwrap(),
&chacha20poly1305::Nonce::from_slice(&nonce).unwrap(),
Expand All @@ -67,7 +70,7 @@ fn aead_test_runner(
assert!(dst_pt_out[..].as_ref() == input);

Ok(())
} else if nonce.len() == constants::XCHACHA_NONCESIZE {
} else if nonce.len() == XCHACHA_NONCESIZE {
aead::xchacha20poly1305::seal(
&SecretKey::from_slice(&key).unwrap(),
&xchacha20poly1305::Nonce::from_slice(&nonce).unwrap(),
Expand Down
10 changes: 5 additions & 5 deletions tests/stream/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ extern crate orion;
use self::{
chacha20::SecretKey,
hex::decode,
orion::hazardous::{
constants,
stream::{chacha20, xchacha20},
orion::hazardous::stream::{
chacha20::{self, IETF_CHACHA_NONCESIZE},
xchacha20::{self, XCHACHA_NONCESIZE},
},
};

Expand All @@ -26,7 +26,7 @@ pub fn chacha_test_runner(
let original_ct = ct.to_vec();

// Selecting variant based on nonce size
if nonce.len() == constants::IETF_CHACHA_NONCESIZE {
if nonce.len() == IETF_CHACHA_NONCESIZE {
chacha20::encrypt(
&SecretKey::from_slice(&key).unwrap(),
&chacha20::Nonce::from_slice(&nonce).unwrap(),
Expand All @@ -44,7 +44,7 @@ pub fn chacha_test_runner(
)
.unwrap();
}
if nonce.len() == constants::XCHACHA_NONCESIZE {
if nonce.len() == XCHACHA_NONCESIZE {
xchacha20::encrypt(
&SecretKey::from_slice(&key).unwrap(),
&xchacha20::Nonce::from_slice(&nonce).unwrap(),
Expand Down