Skip to content

Commit

Permalink
Replace T::max_value() with MAX (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
u5surf committed Dec 19, 2020
1 parent 7ee5bb9 commit 2557c2e
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 36 deletions.
10 changes: 5 additions & 5 deletions src/hazardous/aead/streaming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
//!
//! # Panics:
//! A panic will occur if:
//! - 64 + (`ciphertext.len()` - [`ABYTES`]) overflows `u64::max_value()` when decrypting.
//! - 64 + (`ciphertext.len()` - [`ABYTES`]) overflows `u64::MAX` when decrypting.
//!
//! # Security:
//! - It is critical for security that a given nonce is not re-used with a given key.
Expand Down Expand Up @@ -1300,7 +1300,7 @@ mod private {
];

let mut ctx = StreamXChaCha20Poly1305::new(&SecretKey::from(KEY), &Nonce::from(NONCE));
ctx.counter = u32::max_value();
ctx.counter = u32::MAX;
assert_eq!(ctx.key, before_internal_key.as_ref());
assert_eq!(ctx.get_nonce(), before_internal_nonce.as_ref());
assert_eq!(ctx.counter, u32::from_le_bytes(before_internal_counter));
Expand Down Expand Up @@ -1345,7 +1345,7 @@ mod private {
];

let mut ctx = StreamXChaCha20Poly1305::new(&SecretKey::from(KEY), &Nonce::from(NONCE));
ctx.counter = u32::max_value();
ctx.counter = u32::MAX;
assert_eq!(ctx.key.unprotected_as_bytes(), before_internal_key.as_ref());
assert_eq!(ctx.get_nonce(), before_internal_nonce.as_ref());
assert_eq!(ctx.counter, u32::from_le_bytes(before_internal_counter));
Expand Down Expand Up @@ -1390,7 +1390,7 @@ mod private {
];

let mut ctx = StreamXChaCha20Poly1305::new(&SecretKey::from(KEY), &Nonce::from(NONCE));
ctx.counter = u32::max_value();
ctx.counter = u32::MAX;
assert_eq!(ctx.key, before_internal_key.as_ref());
assert_eq!(ctx.get_nonce(), before_internal_nonce.as_ref());
assert_eq!(ctx.counter, u32::from_le_bytes(before_internal_counter));
Expand Down Expand Up @@ -1435,7 +1435,7 @@ mod private {
];

let mut ctx = StreamXChaCha20Poly1305::new(&SecretKey::from(KEY), &Nonce::from(NONCE));
ctx.counter = u32::max_value();
ctx.counter = u32::MAX;
assert_eq!(ctx.key, before_internal_key.as_ref());
assert_eq!(ctx.get_nonce(), before_internal_nonce.as_ref());
assert_eq!(ctx.counter, u32::from_le_bytes(before_internal_counter));
Expand Down
6 changes: 3 additions & 3 deletions src/hazardous/hash/blake2b.rs
Original file line number Diff line number Diff line change
Expand Up @@ -968,7 +968,7 @@ mod private {
context.increment_offset(12);
assert!(context.t == [30u64, 0u64]);
// Overflow
context.increment_offset(u64::max_value());
context.increment_offset(u64::MAX);
assert!(context.t == [29u64, 1u64]);
}

Expand All @@ -980,14 +980,14 @@ mod private {
internal_state: IV,
buffer: [0u8; BLAKE2B_BLOCKSIZE],
leftover: 0,
t: [1u64, u64::max_value()],
t: [1u64, u64::MAX],
f: [0u64; 2],
is_finalized: false,
is_keyed: false,
size: 1,
};

context.increment_offset(u64::max_value());
context.increment_offset(u64::MAX);
}
}
}
10 changes: 5 additions & 5 deletions src/hazardous/hash/sha512.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,10 @@ impl Sha512 {
/// Increment the message length during processing of data.
fn increment_mlen(&mut self, length: u64) {
// The checked shift checks that the right-hand side is a legal shift.
// The result can still overflow if length > u64::max_value() / 8.
// The result can still overflow if length > u64::MAX / 8.
// Should be impossible for a user to trigger, because update() processes
// in SHA512_BLOCKSIZE chunks.
debug_assert!(length <= u64::max_value() / 8);
debug_assert!(length <= u64::MAX / 8);

// left-shift to get bit-sized representation of length
// using .unwrap() because it should not panic in practice
Expand Down Expand Up @@ -528,7 +528,7 @@ mod private {
context.increment_mlen(12);
assert!(context.message_len == [0u64, 240u64]);
// Overflow
context.increment_mlen(u64::max_value() / 8);
context.increment_mlen(u64::MAX / 8);
assert!(context.message_len == [1u64, 232u64]);
}

Expand All @@ -539,10 +539,10 @@ mod private {
working_state: H0,
buffer: [0u8; SHA512_BLOCKSIZE],
leftover: 0,
message_len: [u64::max_value(), u64::max_value() - 7],
message_len: [u64::MAX, u64::MAX - 7],
is_finalized: false,
};
// u64::max_value() - 7, to leave so that the length represented
// u64::MAX - 7, to leave so that the length represented
// in bites should overflow by exactly one.
context.increment_mlen(1);
}
Expand Down
10 changes: 5 additions & 5 deletions src/hazardous/kdf/argon2i.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@
//!
//! # Errors:
//! An error will be returned if:
//! - The length of the `password` is greater than `u32::max_value()`.
//! - The length of the `salt` is greater than `u32::max_value()` or less than `8`.
//! - The length of the `secret` is greater than `u32::max_value()`.
//! - The length of the `ad` is greater than `u32::max_value()`.
//! - The length of `dst_out` is greater than `u32::max_value()` or less than `4`.
//! - The length of the `password` is greater than `u32::MAX`.
//! - The length of the `salt` is greater than `u32::MAX` or less than `8`.
//! - The length of the `secret` is greater than `u32::MAX`.
//! - The length of the `ad` is greater than `u32::MAX`.
//! - The length of `dst_out` is greater than `u32::MAX` or less than `4`.
//! - `iterations` is less than `1`.
//! - `memory` is less than `8`.
//! - The hashed password does not match the expected when verifying.
Expand Down
6 changes: 3 additions & 3 deletions src/hazardous/stream/chacha20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -967,7 +967,7 @@ mod private {

chacha_state_hchacha.keystream_block(0, &mut hchacha_keystream_block_zero);
chacha_state_hchacha
.keystream_block(u32::max_value(), &mut hchacha_keystream_block_max);
.keystream_block(u32::MAX, &mut hchacha_keystream_block_max);

assert_eq!(hchacha_keystream_block_zero, hchacha_keystream_block_max);
}
Expand Down Expand Up @@ -1010,7 +1010,7 @@ mod private {
U32x4(0, 0, 0, 0),
U32x4(0, 0, 0, 0),
],
internal_counter: (u32::max_value() - 128),
internal_counter: (u32::MAX - 128),
is_ietf: true,
};

Expand All @@ -1031,7 +1031,7 @@ mod private {
U32x4(0, 0, 0, 0),
U32x4(0, 0, 0, 0),
],
internal_counter: (u32::max_value() - 128),
internal_counter: (u32::MAX - 128),
is_ietf: false,
};

Expand Down
2 changes: 1 addition & 1 deletion src/high_level/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ mod public {
/// in `authenticate/authenticate_verify`.
fn prop_authenticate_key_size(input: Vec<u8>) -> bool {
let sec_key_res = SecretKey::from_slice(&input);
if input.len() == 0 || input.len() >= u32::max_value() as usize {
if input.len() == 0 || input.len() >= u32::MAX as usize {
return sec_key_res.is_err();
}
let sec_key = sec_key_res.unwrap();
Expand Down
6 changes: 3 additions & 3 deletions src/high_level/hltypes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ construct_secret_key_variable_size! {
/// An error will be returned if:
/// - `slice` is empty.
/// - `length` is 0.
/// - `length` is not less than `u32::max_value()`.
/// - `length` is not less than `u32::MAX`.
///
/// # Panics:
/// A panic will occur if:
Expand All @@ -50,7 +50,7 @@ construct_salt_variable_size! {
/// An error will be returned if:
/// - `slice` is empty.
/// - `length` is 0.
/// - `length` is not less than `u32::max_value()`.
/// - `length` is not less than `u32::MAX`.
///
/// # Panics:
/// A panic will occur if:
Expand Down Expand Up @@ -78,7 +78,7 @@ construct_secret_key_variable_size! {
/// An error will be returned if:
/// - `slice` is empty.
/// - `length` is 0.
/// - `length` is not less than `u32::max_value()`.
/// - `length` is not less than `u32::MAX`.
///
/// # Panics:
/// A panic will occur if:
Expand Down
4 changes: 2 additions & 2 deletions src/high_level/kdf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@
//! - `iterations` is less than 3.
//! - `length` is less than 4.
//! - `memory` is less than 8.
//! - The length of the `password` or `expected` is greater than `u32::max_value()`.
//! - The length of the `salt` is greater than `u32::max_value()` or less than `8`.
//! - The length of the `password` or `expected` is greater than `u32::MAX`.
//! - The length of the `salt` is greater than `u32::MAX` or less than `8`.
//! - The `expected` does not match the derived key.
//!
//! # Security:
Expand Down
6 changes: 3 additions & 3 deletions src/high_level/pwhash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
//! An error will be returned if:
//! - `memory` is less than 8.
//! - `iterations` is less than 3.
//! - The length of the `password` is greater than `u32::max_value()`.
//! - The length of the `password` is greater than `u32::MAX`.
//! - The password hash does not match `expected`.
//!
//! # Panics:
Expand Down Expand Up @@ -604,7 +604,7 @@ mod public {
let exact_min = "$argon2i$v=19$m=8,t=3,p=1$cHBwcHBwcHBwcHBwcHBwcA$MDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDA";
let less = "$argon2i$v=19$m=7,t=3,p=1$cHBwcHBwcHBwcHBwcHBwcA$MDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDA";
// Throws error during parsing as u32
let u32_overflow = format!("$argon2i$v=19$m={},t=3,p=1$cHBwcHBwcHBwcHBwcHBwcA$MDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDA", u64::max_value());
let u32_overflow = format!("$argon2i$v=19$m={},t=3,p=1$cHBwcHBwcHBwcHBwcHBwcA$MDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDA", u64::MAX);

assert!(PasswordHash::from_encoded(exact_min).is_ok());
assert!(PasswordHash::from_encoded(less).is_err());
Expand All @@ -616,7 +616,7 @@ mod public {
let exact_min = "$argon2i$v=19$m=65536,t=3,p=1$cHBwcHBwcHBwcHBwcHBwcA$MDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDA";
let less = "$argon2i$v=19$m=65536,t=2,p=1$cHBwcHBwcHBwcHBwcHBwcA$MDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDA";
// Throws error during parsing as u32
let u32_overflow = format!("$argon2i$v=19$m=65536,t={},p=1$cHBwcHBwcHBwcHBwcHBwcA$MDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDA", u64::max_value());
let u32_overflow = format!("$argon2i$v=19$m=65536,t={},p=1$cHBwcHBwcHBwcHBwcHBwcA$MDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDA", u64::MAX);

assert!(PasswordHash::from_encoded(exact_min).is_ok());
assert!(PasswordHash::from_encoded(less).is_err());
Expand Down
8 changes: 4 additions & 4 deletions src/test_framework/streamcipher_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,15 +212,15 @@ fn initial_counter_overflow_err<Encryptor, Decryptor, Key, Nonce>(
assert!(encryptor(
key,
nonce,
u32::max_value(),
u32::MAX,
&[0u8; 65], // CHACHA_BLOCKSIZE + 1 one to trigger internal block counter addition.
&mut dst_out
)
.is_err());
assert!(decryptor(
key,
nonce,
u32::max_value(),
u32::MAX,
&[0u8; 65], // CHACHA_BLOCKSIZE + 1 one to trigger internal block counter addition.
&mut dst_out
)
Expand All @@ -242,15 +242,15 @@ fn initial_counter_max_ok<Encryptor, Decryptor, Key, Nonce>(
assert!(encryptor(
key,
nonce,
u32::max_value(),
u32::MAX,
&[0u8; 64], // Only needs to process one keystream
&mut dst_out
)
.is_ok());
assert!(decryptor(
key,
nonce,
u32::max_value(),
u32::MAX,
&[0u8; 64], // Only needs to process one keystream
&mut dst_out
)
Expand Down
4 changes: 2 additions & 2 deletions src/typedefs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ macro_rules! func_generate_variable_size (($name:ident) => (
#[cfg(feature = "safe_api")]
/// Randomly generate using a CSPRNG. Not available in `no_std` context.
pub fn generate(length: usize) -> Result<$name, UnknownCryptoError> {
if length < 1 || length >= (u32::max_value() as usize) {
if length < 1 || length >= (u32::MAX as usize) {
return Err(UnknownCryptoError);
}

Expand Down Expand Up @@ -362,7 +362,7 @@ macro_rules! test_generate_variable (($name:ident) => (
#[cfg(feature = "safe_api")]
fn test_generate_variable() {
assert!($name::generate(0).is_err());
assert!($name::generate(usize::max_value()).is_err());
assert!($name::generate(usize::MAX).is_err());
assert!($name::generate(1).is_ok());
assert!($name::generate(64).is_ok());

Expand Down

0 comments on commit 2557c2e

Please sign in to comment.