Skip to content

Commit

Permalink
Rename random to random_bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
eoger committed Aug 14, 2019
1 parent 7bef73f commit 677617d
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/aes128gcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl Aes128GcmEceWebPush {
Some(salt) => salt,
None => {
let mut salt = [0u8; ECE_SALT_LENGTH];
cryptographer.random(&mut salt)?;
cryptographer.random_bytes(&mut salt)?;
salt.to_vec()
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/aesgcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ impl AesGcmEceWebPush {
let cryptographer = crypto::holder::get_cryptographer();
let salt = {
let mut salt = [0u8; ECE_SALT_LENGTH];
cryptographer.random(&mut salt)?;
cryptographer.random_bytes(&mut salt)?;
salt.to_vec()
};
let raw_local_pub_key = local_prv_key.pub_as_raw()?;
Expand Down
3 changes: 1 addition & 2 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,7 @@ pub trait EceWebPush {
last_record,
)?;
let cryptographer = crypto::holder::get_cryptographer();
let mut record =
cryptographer.aes_gcm_128_encrypt(&key, &iv, &block)?;
let mut record = cryptographer.aes_gcm_128_encrypt(&key, &iv, &block)?;
ciphertext.append(&mut record);
plaintext_start = plaintext_end;
counter += 1;
Expand Down
9 changes: 2 additions & 7 deletions src/crypto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,12 @@ pub trait Cryptographer: Send + Sync + 'static {
) -> Result<Vec<u8>>;
fn hkdf_sha256(&self, salt: &[u8], secret: &[u8], info: &[u8], len: usize) -> Result<Vec<u8>>;
/// Should return [ciphertext, auth_tag].
fn aes_gcm_128_encrypt(
&self,
key: &[u8],
iv: &[u8],
data: &[u8],
) -> Result<Vec<u8>>;
fn aes_gcm_128_encrypt(&self, key: &[u8], iv: &[u8], data: &[u8]) -> Result<Vec<u8>>;
fn aes_gcm_128_decrypt(
&self,
key: &[u8],
iv: &[u8],
ciphertext_and_tag: &[u8],
) -> Result<Vec<u8>>;
fn random(&self, dest: &mut [u8]) -> Result<()>;
fn random_bytes(&self, dest: &mut [u8]) -> Result<()>;
}
9 changes: 2 additions & 7 deletions src/crypto/openssl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,7 @@ impl Cryptographer for OpensslCryptographer {
Ok(okm)
}

fn aes_gcm_128_encrypt(
&self,
key: &[u8],
iv: &[u8],
data: &[u8],
) -> Result<Vec<u8>> {
fn aes_gcm_128_encrypt(&self, key: &[u8], iv: &[u8], data: &[u8]) -> Result<Vec<u8>> {
let cipher = Cipher::aes_128_gcm();
let mut c = Crypter::new(cipher, Mode::Encrypt, key, Some(iv))?;
let mut out = vec![0u8; data.len() + cipher.block_size()];
Expand Down Expand Up @@ -202,7 +197,7 @@ impl Cryptographer for OpensslCryptographer {
Ok(out)
}

fn random(&self, dest: &mut [u8]) -> Result<()> {
fn random_bytes(&self, dest: &mut [u8]) -> Result<()> {
Ok(rand_bytes(dest)?)
}
}
10 changes: 5 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub fn generate_keypair_and_auth_secret(
let cryptographer = crypto::holder::get_cryptographer();
let local_key_pair = cryptographer.generate_ephemeral_keypair()?;
let mut auth_secret = [0u8; ECE_WEBPUSH_AUTH_SECRET_LENGTH];
cryptographer.random(&mut auth_secret)?;
cryptographer.random_bytes(&mut auth_secret)?;
Ok((local_key_pair, auth_secret))
}

Expand All @@ -38,7 +38,7 @@ pub fn encrypt(remote_pub: &[u8], remote_auth: &[u8], salt: &[u8], data: &[u8])
let remote_key = cryptographer.import_public_key(remote_pub)?;
let local_key_pair = cryptographer.generate_ephemeral_keypair()?;
let mut padr = [0u8; 2];
cryptographer.random(&mut padr)?;
cryptographer.random_bytes(&mut padr)?;
// since it's a sampled random, endian doesn't really matter.
let pad = ((usize::from(padr[0]) + (usize::from(padr[1]) << 8)) % 4095) + 1;
let params = WebPushParams::new(4096, pad, Vec::from(salt));
Expand Down Expand Up @@ -131,7 +131,7 @@ mod aes128gcm_tests {
let plaintext = b"When I grow up, I want to be a watermelon";
let mut auth_secret = vec![0u8; 16];
let cryptographer = crypto::holder::get_cryptographer();
cryptographer.random(&mut auth_secret).unwrap();
cryptographer.random_bytes(&mut auth_secret).unwrap();
let remote_public = cryptographer
.import_public_key(&remote_key.pub_as_raw().unwrap())
.unwrap();
Expand All @@ -155,7 +155,7 @@ mod aes128gcm_tests {
let plaintext = b"Mary had a little lamb, with some nice mint jelly";
let mut salt = vec![0u8; 16];
let cryptographer = crypto::holder::get_cryptographer();
cryptographer.random(&mut salt)?;
cryptographer.random_bytes(&mut salt)?;
let encoded = encrypt(&local_key.pub_as_raw()?, &auth, &salt, plaintext).unwrap();
let decoded = decrypt(&local_key.raw_components()?, &auth, &encoded)?;
assert_eq!(decoded, plaintext.to_vec());
Expand Down Expand Up @@ -374,7 +374,7 @@ mod aesgcm_tests {
let plaintext = b"When I grow up, I want to be a watermelon";
let mut auth_secret = vec![0u8; 16];
let cryptographer = crypto::holder::get_cryptographer();
cryptographer.random(&mut auth_secret).unwrap();
cryptographer.random_bytes(&mut auth_secret).unwrap();
let remote_public = cryptographer
.import_public_key(&remote_key.pub_as_raw().unwrap())
.unwrap();
Expand Down

0 comments on commit 677617d

Please sign in to comment.