Skip to content

Commit 728169e

Browse files
committed
Cleanup.
1 parent 95b5c8a commit 728169e

File tree

6 files changed

+29
-84
lines changed

6 files changed

+29
-84
lines changed

src/ecdsa_keys.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@ pub fn ecdsa_compress_public_key(uncompressed_public_key: &[u8; UNCOMPRESSED_PUB
3636
/// Derives the ECDSA private key from the given key material.
3737
///
3838
/// Uses the HKDF algorithm to derive the private key from the given key material.
39-
pub fn ecdsa_derive_private_key<D>(key_material: D) -> Vec<u8>
40-
where D: AsRef<[u8]>
41-
{
39+
pub fn ecdsa_derive_private_key(key_material: impl AsRef<[u8]>) -> Vec<u8> {
4240
hkdf_hmac_sha256(key_material, "signing".as_bytes(), 32)
4341
}
4442

src/ecdsa_signing.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ use secp256k1::{SecretKey, Secp256k1, PublicKey, Message, ecdsa::Signature};
22
use crate::{hash::double_sha256, ECDSA_PRIVATE_KEY_SIZE, ECDSA_PUBLIC_KEY_SIZE, ECDSA_SIGNATURE_SIZE};
33

44
/// ECDSA signs the given message using the given private key.
5-
pub fn ecdsa_sign<T>(private_key: &[u8; ECDSA_PRIVATE_KEY_SIZE], message: T) -> [u8; ECDSA_SIGNATURE_SIZE]
6-
where T: AsRef<[u8]>,
7-
{
5+
pub fn ecdsa_sign(private_key: &[u8; ECDSA_PRIVATE_KEY_SIZE], message: impl AsRef<[u8]>) -> [u8; ECDSA_SIGNATURE_SIZE] {
86
let secp = Secp256k1::new();
97
let sk = SecretKey::from_slice(private_key).expect("32 bytes, within curve order");
108
let hash = double_sha256(message.as_ref());
@@ -16,9 +14,7 @@ pub fn ecdsa_sign<T>(private_key: &[u8; ECDSA_PRIVATE_KEY_SIZE], message: T) ->
1614
/// Verifies the given ECDSA signature using the given public key.
1715
///
1816
/// Returns `true` if the signature is valid, `false` otherwise.
19-
pub fn ecdsa_verify<T>(public_key: &[u8; ECDSA_PUBLIC_KEY_SIZE], signature: &[u8; ECDSA_SIGNATURE_SIZE], message: T) -> bool
20-
where T: AsRef<[u8]>,
21-
{
17+
pub fn ecdsa_verify(public_key: &[u8; ECDSA_PUBLIC_KEY_SIZE], signature: &[u8; ECDSA_SIGNATURE_SIZE], message: impl AsRef<[u8]>) -> bool {
2218
let secp = Secp256k1::new();
2319
let pk = PublicKey::from_slice(public_key)
2420
.expect("33 or 65 bytes, serialized according to the spec");

src/hash.rs

Lines changed: 9 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,13 @@ use hkdf::Hkdf;
66
use crate::{CRC32_SIZE, SHA256_SIZE, SHA512_SIZE};
77

88
/// Computes the CRC-32 checksum of the given data.
9-
pub fn crc32<D>(data: D) -> u32
10-
where D: AsRef<[u8]>
11-
{
9+
pub fn crc32(data: impl AsRef<[u8]>) -> u32 {
1210
crc32fast::hash(data.as_ref())
1311
}
1412

1513
/// Computes the SHA-256 hash of the given data, returning the hash as a
1614
/// 4-byte vector that can be returned in either big-endian or little-endian format.
17-
pub fn crc32_data_opt<D>(data: D, little_endian: bool) -> [u8; CRC32_SIZE]
18-
where D: AsRef<[u8]>
19-
{
15+
pub fn crc32_data_opt(data: impl AsRef<[u8]>, little_endian: bool) -> [u8; CRC32_SIZE] {
2016
let checksum: u32 = crc32(data);
2117
let mut result = [0u8; 4];
2218
if little_endian {
@@ -28,16 +24,12 @@ pub fn crc32_data_opt<D>(data: D, little_endian: bool) -> [u8; CRC32_SIZE]
2824
}
2925
/// Computes the SHA-256 hash of the given data, returning the hash as a
3026
/// 4-byte vector in big-endian format.
31-
pub fn crc32_data<D>(data: D) -> [u8; CRC32_SIZE]
32-
where D: AsRef<[u8]>
33-
{
27+
pub fn crc32_data(data: impl AsRef<[u8]>) -> [u8; CRC32_SIZE] {
3428
crc32_data_opt(data, false)
3529
}
3630

3731
/// Computes the SHA-256 digest of the input buffer.
38-
pub fn sha256<D>(data: D) -> [u8; SHA256_SIZE]
39-
where D: AsRef<[u8]>
40-
{
32+
pub fn sha256(data: impl AsRef<[u8]>) -> [u8; SHA256_SIZE] {
4133
let mut hasher = Sha256::new();
4234
hasher.update(data);
4335
let result = hasher.finalize();
@@ -52,9 +44,7 @@ pub fn double_sha256(message: &[u8]) -> [u8; SHA256_SIZE] {
5244
}
5345

5446
/// Computes the SHA-512 digest of the input buffer.
55-
pub fn sha512<D>(data: D) -> [u8; SHA512_SIZE]
56-
where D: AsRef<[u8]>
57-
{
47+
pub fn sha512(data: impl AsRef<[u8]>) -> [u8; SHA512_SIZE] {
5848
let mut hasher = Sha512::new();
5949
hasher.update(data);
6050
let result = hasher.finalize();
@@ -64,46 +54,30 @@ pub fn sha512<D>(data: D) -> [u8; SHA512_SIZE]
6454
}
6555

6656
/// Computes the HMAC-SHA-256 for the given key and message.
67-
pub fn hmac_sha256<D1, D2>(key: D1, message: D2) -> [u8; SHA256_SIZE]
68-
where
69-
D1: AsRef<[u8]>,
70-
D2: AsRef<[u8]>
71-
{
57+
pub fn hmac_sha256(key: impl AsRef<[u8]>, message: impl AsRef<[u8]>) -> [u8; SHA256_SIZE] {
7258
let mut mac = Hmac::<Sha256>::new_from_slice(key.as_ref()).unwrap();
7359
mac.update(message.as_ref());
7460
let result = mac.finalize();
7561
result.into_bytes().into()
7662
}
7763

7864
/// Computes the HMAC-SHA-512 for the given key and message.
79-
pub fn hmac_sha512<D1, D2>(key: D1, message: D2) -> [u8; SHA512_SIZE]
80-
where
81-
D1: AsRef<[u8]>,
82-
D2: AsRef<[u8]>
83-
{
65+
pub fn hmac_sha512(key: impl AsRef<[u8]>, message: impl AsRef<[u8]>) -> [u8; SHA512_SIZE] {
8466
let mut mac = Hmac::<Sha512>::new_from_slice(key.as_ref()).unwrap();
8567
mac.update(message.as_ref());
8668
let result = mac.finalize();
8769
result.into_bytes().into()
8870
}
8971

9072
/// Computes the PBKDF2-HMAC-SHA-256 for the given password.
91-
pub fn pbkdf2_hmac_sha256<D1, D2>(pass: D1, salt: D2, iterations: u32, key_len: usize) -> Vec<u8>
92-
where
93-
D1: AsRef<[u8]>,
94-
D2: AsRef<[u8]>
95-
{
73+
pub fn pbkdf2_hmac_sha256(pass: impl AsRef<[u8]>, salt: impl AsRef<[u8]>, iterations: u32, key_len: usize) -> Vec<u8> {
9674
let mut key = vec![0u8; key_len];
9775
pbkdf2_hmac::<Sha256>(pass.as_ref(), salt.as_ref(), iterations, &mut key);
9876
key
9977
}
10078

10179
/// Computes the HKDF-HMAC-SHA-256 for the given key material.
102-
pub fn hkdf_hmac_sha256<D1, D2>(key_material: D1, salt: D2, key_len: usize) -> Vec<u8>
103-
where
104-
D1: AsRef<[u8]>,
105-
D2: AsRef<[u8]>
106-
{
80+
pub fn hkdf_hmac_sha256(key_material: impl AsRef<[u8]>, salt: impl AsRef<[u8]>, key_len: usize) -> Vec<u8> {
10781
let mut key = vec![0u8; key_len];
10882
let hkdf = Hkdf::<Sha256>::new(Some(salt.as_ref()), key_material.as_ref());
10983
hkdf.expand(&[], &mut key).unwrap();

src/public_key_encryption.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,12 @@ pub fn x25519_agreement_public_key_from_private_key(agreement_private_key: &[u8;
1515
}
1616

1717
/// Derive an X25519 agreement private key from the given key material.
18-
pub fn x25519_derive_agreement_private_key<D>(key_material: D) -> [u8; X25519_PRIVATE_KEY_SIZE]
19-
where D: AsRef<[u8]>
20-
{
18+
pub fn x25519_derive_agreement_private_key(key_material: impl AsRef<[u8]>) -> [u8; X25519_PRIVATE_KEY_SIZE] {
2119
hkdf_hmac_sha256(key_material, "agreement".as_bytes(), X25519_PRIVATE_KEY_SIZE).try_into().unwrap()
2220
}
2321

2422
/// Derive an X25519 signing private key from the given key material.
25-
pub fn x25519_derive_signing_private_key<D>(key_material: D) -> [u8; X25519_PRIVATE_KEY_SIZE]
26-
where D: AsRef<[u8]>
27-
{
23+
pub fn x25519_derive_signing_private_key(key_material: impl AsRef<[u8]>) -> [u8; X25519_PRIVATE_KEY_SIZE] {
2824
hkdf_hmac_sha256(key_material, "signing".as_bytes(), X25519_PRIVATE_KEY_SIZE).try_into().unwrap()
2925
}
3026

src/schnorr_signing.rs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,22 @@ use crate::{hash::sha256, SCHNORR_SIGNATURE_SIZE, SCHNORR_PUBLIC_KEY_SIZE, ECDSA
55
/// Compute a tagged hash as defined in BIP-340.
66
///
77
/// SHA256(SHA256(tag)||SHA256(tag)||msg)
8-
fn tagged_sha256<D1, D2>(msg: D1, tag: D2) -> [u8; 32]
9-
where D1: AsRef<[u8]>,
10-
D2: AsRef<[u8]>
11-
{
8+
fn tagged_sha256(msg: impl AsRef<[u8]>, tag: impl AsRef<[u8]>) -> [u8; 32] {
129
let mut tag_hash = sha256(tag.as_ref()).to_vec();
1310
tag_hash.extend(tag_hash.clone());
1411
tag_hash.extend(msg.as_ref());
1512
sha256(tag_hash)
1613
}
1714

1815
/// Schnorr signs the given message using the given private key and user-defined tag.
19-
pub fn schnorr_sign<D1, D2>(ecdsa_private_key: &[u8; ECDSA_PRIVATE_KEY_SIZE], message: D1, tag: D2) -> [u8; SCHNORR_SIGNATURE_SIZE]
20-
where D1: AsRef<[u8]>,
21-
D2: AsRef<[u8]>,
22-
{
16+
pub fn schnorr_sign(ecdsa_private_key: &[u8; ECDSA_PRIVATE_KEY_SIZE], message: impl AsRef<[u8]>, tag: impl AsRef<[u8]>) -> [u8; SCHNORR_SIGNATURE_SIZE] {
2317
let mut rng = SecureRandomNumberGenerator;
2418
schnorr_sign_using(ecdsa_private_key, message, tag, &mut rng)
2519
}
2620

2721
/// Schnorr signs the given message using the given private key, user-defined tag,
2822
/// and random number generator.
29-
pub fn schnorr_sign_using<D1, D2>(ecdsa_private_key: &[u8; ECDSA_PRIVATE_KEY_SIZE], message: D1, tag: D2, rng: &mut impl RandomNumberGenerator) -> [u8; SCHNORR_SIGNATURE_SIZE]
30-
where D1: AsRef<[u8]>,
31-
D2: AsRef<[u8]>,
32-
{
23+
pub fn schnorr_sign_using(ecdsa_private_key: &[u8; ECDSA_PRIVATE_KEY_SIZE], message: impl AsRef<[u8]>, tag: impl AsRef<[u8]>, rng: &mut impl RandomNumberGenerator) -> [u8; SCHNORR_SIGNATURE_SIZE] {
3324
let mut secp = Secp256k1::new();
3425
let seed: [u8; 32] = rng.random_data(32).try_into().unwrap();
3526
secp.seeded_randomize(&seed);
@@ -46,10 +37,7 @@ pub fn schnorr_sign_using<D1, D2>(ecdsa_private_key: &[u8; ECDSA_PRIVATE_KEY_SIZ
4637

4738
/// Verifies the given Schnorr signature against the given message, public key,
4839
/// and user-defined tag, which must match the tag used to create the signature.
49-
pub fn schnorr_verify<D1, D2>(schnorr_public_key: &[u8; SCHNORR_PUBLIC_KEY_SIZE], schnorr_signature: &[u8; SCHNORR_SIGNATURE_SIZE], message: D1, tag: D2) -> bool
50-
where D1: AsRef<[u8]>,
51-
D2: AsRef<[u8]>,
52-
{
40+
pub fn schnorr_verify(schnorr_public_key: &[u8; SCHNORR_PUBLIC_KEY_SIZE], schnorr_signature: &[u8; SCHNORR_SIGNATURE_SIZE], message: impl AsRef<[u8]>, tag: impl AsRef<[u8]>) -> bool {
5341
let secp = Secp256k1::new();
5442
let hash = tagged_sha256(message.as_ref(), tag.as_ref());
5543
let msg = Message::from_slice(&hash)

src/symmetric_encryption.rs

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,12 @@ use crate::{Error, SYMMETRIC_KEY_SIZE, SYMMETRIC_NONCE_SIZE, SYMMETRIC_AUTH_SIZE
66
/// additional authenticated data (AAD).
77
///
88
/// Returns the ciphertext and the authentication tag.
9-
pub fn aead_chacha20_poly1305_encrypt_with_aad<D1, D2>(
10-
plaintext: D1,
9+
pub fn aead_chacha20_poly1305_encrypt_with_aad(
10+
plaintext: impl AsRef<[u8]>,
1111
key: &[u8; SYMMETRIC_KEY_SIZE],
1212
nonce: &[u8; SYMMETRIC_NONCE_SIZE],
13-
aad: &D2
14-
) -> (Vec<u8>, [u8; SYMMETRIC_AUTH_SIZE])
15-
where
16-
D1: AsRef<[u8]>,
17-
D2: AsRef<[u8]>,
18-
{
13+
aad: impl AsRef<[u8]>
14+
) -> (Vec<u8>, [u8; SYMMETRIC_AUTH_SIZE]) {
1915
let cipher = ChaCha20Poly1305::new(key.into());
2016
let mut buffer = plaintext.as_ref().to_vec();
2117
let auth = cipher.encrypt_in_place_detached(nonce.into(), aad.as_ref(), &mut buffer).unwrap();
@@ -25,15 +21,12 @@ pub fn aead_chacha20_poly1305_encrypt_with_aad<D1, D2>(
2521
/// Symmetrically encrypts the given plaintext using ChaCha20-Poly1305.
2622
///
2723
/// Returns the ciphertext and the authentication tag.
28-
pub fn aead_chacha20_poly1305_encrypt<D>(
29-
plaintext: D,
24+
pub fn aead_chacha20_poly1305_encrypt(
25+
plaintext: impl AsRef<[u8]>,
3026
key: &[u8; SYMMETRIC_KEY_SIZE],
3127
nonce: &[u8; SYMMETRIC_NONCE_SIZE],
32-
) -> (Vec<u8>, [u8; SYMMETRIC_AUTH_SIZE])
33-
where
34-
D: AsRef<[u8]>,
35-
{
36-
aead_chacha20_poly1305_encrypt_with_aad(plaintext, key, nonce, &[])
28+
) -> (Vec<u8>, [u8; SYMMETRIC_AUTH_SIZE]) {
29+
aead_chacha20_poly1305_encrypt_with_aad(plaintext, key, nonce, [])
3730
}
3831

3932
/// Symmetrically decrypts the given ciphertext using ChaCha20-Poly1305 and the given
@@ -87,7 +80,7 @@ mod tests {
8780
const AUTH: [u8; 16] = hex!("1ae10b594f09e26a7e902ecbd0600691");
8881

8982
fn encrypted() -> (Vec<u8>, [u8; SYMMETRIC_AUTH_SIZE]) {
90-
aead_chacha20_poly1305_encrypt_with_aad(PLAINTEXT, &KEY, &NONCE, &AAD)
83+
aead_chacha20_poly1305_encrypt_with_aad(PLAINTEXT, &KEY, &NONCE, AAD)
9184
}
9285

9386
#[test]
@@ -104,7 +97,7 @@ mod tests {
10497
fn test_random_key_and_nonce() {
10598
let key = random_data(32).try_into().unwrap();
10699
let nonce = random_data(12).try_into().unwrap();
107-
let (ciphertext, auth) = aead_chacha20_poly1305_encrypt_with_aad(PLAINTEXT, &key, &nonce, &AAD);
100+
let (ciphertext, auth) = aead_chacha20_poly1305_encrypt_with_aad(PLAINTEXT, &key, &nonce, AAD);
108101
let decrypted_plaintext = aead_chacha20_poly1305_decrypt_with_aad(ciphertext, &key, &nonce, AAD, &auth).unwrap();
109102
assert_eq!(PLAINTEXT, decrypted_plaintext.as_slice());
110103
}
@@ -113,7 +106,7 @@ mod tests {
113106
fn test_empty_data() {
114107
let key = random_data(32).try_into().unwrap();
115108
let nonce = random_data(12).try_into().unwrap();
116-
let (ciphertext, auth) = aead_chacha20_poly1305_encrypt_with_aad([], &key, &nonce, &[]);
109+
let (ciphertext, auth) = aead_chacha20_poly1305_encrypt_with_aad([], &key, &nonce, []);
117110
let decrypted_plaintext = aead_chacha20_poly1305_decrypt_with_aad(ciphertext, &key, &nonce, [], &auth).unwrap();
118111
assert_eq!(Vec::<u8>::new(), decrypted_plaintext.as_slice());
119112
}

0 commit comments

Comments
 (0)