Skip to content
Open
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
2 changes: 1 addition & 1 deletion src/ec/suite_b/ecdh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ mod tests {
// Test that a private key value exactly equal to the group order
// is rejected and that `generate` gives up after a while of only
// getting that value from the PRNG.
let mut n_bytes = [0u8; ec::SCALAR_MAX_BYTES];
let mut n_bytes = [test::UNINITIALIZED_U8; ec::SCALAR_MAX_BYTES];
let num_bytes = curve.elem_scalar_seed_len;
limb::big_endian_from_limbs(&ops.n.limbs[..ops.num_limbs], &mut n_bytes[..num_bytes]);
{
Expand Down
2 changes: 1 addition & 1 deletion src/rsa/padding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ mod test {

let rng = test::rand::FixedSliceRandom { bytes: &salt };

let mut m_out = vec![0u8; bit_len.as_usize_bytes_rounded_up()];
let mut m_out = vec![test::UNINITIALIZED_U8; bit_len.as_usize_bytes_rounded_up()];
let digest = digest::digest(alg.digest_alg(), &msg);
alg.encode(&digest, &mut m_out, bit_len, &rng).unwrap();
assert_eq!(m_out, encoded);
Expand Down
4 changes: 2 additions & 2 deletions src/rsa/signing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ impl RsaKeyPair {
mod tests {
// We intentionally avoid `use super::*` so that we are sure to use only
// the public API; this ensures that enough of the API is public.
use crate::{rand, signature};
use crate::{rand, signature, test};
use alloc::vec;

// `KeyPair::sign` requires that the output buffer is the same length as
Expand All @@ -624,7 +624,7 @@ mod tests {
let key_pair = signature::RsaKeyPair::from_der(PRIVATE_KEY_DER).unwrap();

// The output buffer is one byte too short.
let mut signature = vec![0; key_pair.public_modulus_len() - 1];
let mut signature = vec![test::UNINITIALIZED_U8; key_pair.public_modulus_len() - 1];

assert!(key_pair
.sign(&signature::RSA_PKCS1_SHA256, &rng, MESSAGE, &mut signature)
Expand Down
4 changes: 4 additions & 0 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ use crate::{bits, digest, error};
#[cfg(any(feature = "std", feature = "test_logging"))]
extern crate std;

/// `UNINITIALIZED_U8` is a non-zero value used to initialize test buffers
/// as a way to differentiate from zeroed-but-otherwise-unwritten memory.
pub const UNINITIALIZED_U8: u8 = 0xDE;

/// `compile_time_assert_clone::<T>();` fails to compile if `T` doesn't
/// implement `Clone`.
pub fn compile_time_assert_clone<T: Clone>() {}
Expand Down
13 changes: 7 additions & 6 deletions tests/aead_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ fn less_safe_key_open_within(
#[allow(clippy::range_plus_one)]
fn key_sizes(aead_alg: &'static aead::Algorithm) {
let key_len = aead_alg.key_len();
let key_data = vec![0u8; key_len * 2];
let key_data = vec![test::UNINITIALIZED_U8; key_len * 2];

// Key is the right size.
assert!(aead::UnboundKey::new(aead_alg, &key_data[..key_len]).is_ok());
Expand Down Expand Up @@ -394,7 +394,7 @@ fn key_sizes(aead_alg: &'static aead::Algorithm) {
#[test]
fn test_aead_nonce_sizes() {
let nonce_len = aead::NONCE_LEN;
let nonce = vec![0u8; nonce_len * 2];
let nonce = vec![test::UNINITIALIZED_U8; nonce_len * 2];

assert!(aead::Nonce::try_assume_unique_for_key(&nonce[..nonce_len]).is_ok());
assert!(aead::Nonce::try_assume_unique_for_key(&nonce[..(nonce_len - 1)]).is_err());
Expand All @@ -420,7 +420,8 @@ fn aead_chacha20_poly1305_openssh() {
// XXX: `polyfill::convert` isn't available here.
let key_bytes = {
let as_vec = test_case.consume_bytes("KEY");
let mut as_array = [0u8; aead::chacha20_poly1305_openssh::KEY_LEN];
let mut as_array =
[test::UNINITIALIZED_U8; aead::chacha20_poly1305_openssh::KEY_LEN];
as_array.copy_from_slice(&as_vec);
as_array
};
Expand All @@ -435,7 +436,7 @@ fn aead_chacha20_poly1305_openssh() {
// TODO: Add some tests for when things fail.
//let error = test_case.consume_optional_string("FAILS");

let mut tag = [0u8; aead::chacha20_poly1305_openssh::TAG_LEN];
let mut tag = [test::UNINITIALIZED_U8; aead::chacha20_poly1305_openssh::TAG_LEN];
let mut s_in_out = plaintext.clone();
let s_key = aead::chacha20_poly1305_openssh::SealingKey::new(&key_bytes);
s_key.seal_in_place(sequence_num, &mut s_in_out[..], &mut tag);
Expand Down Expand Up @@ -479,8 +480,8 @@ fn test_tag_traits() {
#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn test_aead_key_debug() {
let key_bytes = [0; 32];
let nonce = [0; aead::NONCE_LEN];
let key_bytes = [test::UNINITIALIZED_U8; 32];
let nonce = [test::UNINITIALIZED_U8; aead::NONCE_LEN];

let key = aead::UnboundKey::new(&aead::AES_256_GCM, &key_bytes).unwrap();
assert_eq!(
Expand Down
2 changes: 1 addition & 1 deletion tests/digest_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ macro_rules! test_i_u_f {
// TODO: #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[test]
fn $test_name() {
let mut input = [0; (digest::MAX_BLOCK_LEN + 1) * 3];
let mut input = [test::UNINITIALIZED_U8; (digest::MAX_BLOCK_LEN + 1) * 3];
let max = $alg.block_len + 1;
for i in 0..(max * 3) {
input[i] = (i & 0xff) as u8;
Expand Down
2 changes: 1 addition & 1 deletion tests/hkdf_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ impl hkdf::KeyType for My<usize> {

impl From<hkdf::Okm<'_, My<usize>>> for My<Vec<u8>> {
fn from(okm: hkdf::Okm<My<usize>>) -> Self {
let mut r = vec![0u8; okm.len().0];
let mut r = vec![test::UNINITIALIZED_U8; okm.len().0];
okm.fill(&mut r).unwrap();
My(r)
}
Expand Down
2 changes: 1 addition & 1 deletion tests/hmac_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ fn hmac_test_case_inner(
#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn hmac_debug() {
let key = hmac::Key::new(hmac::HMAC_SHA256, &[0; 32]);
let key = hmac::Key::new(hmac::HMAC_SHA256, &[test::UNINITIALIZED_U8; 32]);
assert_eq!("Key { algorithm: SHA256 }", format!("{:?}", &key));

let ctx = hmac::Context::with_key(&key);
Expand Down
2 changes: 1 addition & 1 deletion tests/pbkdf2_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub fn pbkdf2_tests() {
};

{
let mut out = vec![0u8; dk.len()];
let mut out = vec![test::UNINITIALIZED_U8; dk.len()];
pbkdf2::derive(algorithm, iterations, &salt, &secret, &mut out);
assert_eq!(dk == out, verify_expected_result.is_ok() || dk.is_empty());
}
Expand Down
4 changes: 2 additions & 2 deletions tests/quic_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ fn test_quic(alg: &'static quic::Algorithm, test_file: test::File) {
#[allow(clippy::range_plus_one)]
fn test_sample_len(alg: &'static quic::Algorithm) {
let key_len = alg.key_len();
let key_data = vec![0u8; key_len];
let key_data = vec![test::UNINITIALIZED_U8; key_len];

let key = quic::HeaderProtectionKey::new(alg, &key_data).unwrap();

let sample_len = 16;
let sample_data = vec![0u8; sample_len + 2];
let sample_data = vec![test::UNINITIALIZED_U8; sample_len + 2];

// Sample is the right size.
assert!(key.new_mask(&sample_data[..sample_len]).is_ok());
Expand Down
2 changes: 1 addition & 1 deletion tests/rand_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ fn test_system_random_lengths() {
];

for len in lengths.iter() {
let mut buf = vec![0; *len];
let mut buf = vec![test::UNINITIALIZED_U8; *len];

let rng = rand::SystemRandom::new();
assert!(rng.fill(&mut buf).is_ok());
Expand Down
4 changes: 2 additions & 2 deletions tests/rsa_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ fn test_signature_rsa_pkcs1_sign() {

// XXX: This test is too slow on Android ARM Travis CI builds.
// TODO: re-enable these tests on Android ARM.
let mut actual = vec![0u8; key_pair.public_modulus_len()];
let mut actual = vec![test::UNINITIALIZED_U8; key_pair.public_modulus_len()];
key_pair
.sign(alg, &rng, &msg, actual.as_mut_slice())
.unwrap();
Expand Down Expand Up @@ -124,7 +124,7 @@ fn test_signature_rsa_pss_sign() {

let rng = test::rand::FixedSliceRandom { bytes: &salt };

let mut actual = vec![0u8; key_pair.public_modulus_len()];
let mut actual = vec![test::UNINITIALIZED_U8; key_pair.public_modulus_len()];
key_pair.sign(alg, &rng, &msg, actual.as_mut_slice())?;
assert_eq!(actual.as_slice() == &expected[..], result == "Pass");
Ok(())
Expand Down