@@ -6,16 +6,12 @@ use crate::{Error, SYMMETRIC_KEY_SIZE, SYMMETRIC_NONCE_SIZE, SYMMETRIC_AUTH_SIZE
6
6
/// additional authenticated data (AAD).
7
7
///
8
8
/// 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 ] > ,
11
11
key : & [ u8 ; SYMMETRIC_KEY_SIZE ] ,
12
12
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 ] ) {
19
15
let cipher = ChaCha20Poly1305 :: new ( key. into ( ) ) ;
20
16
let mut buffer = plaintext. as_ref ( ) . to_vec ( ) ;
21
17
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>(
25
21
/// Symmetrically encrypts the given plaintext using ChaCha20-Poly1305.
26
22
///
27
23
/// 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 ] > ,
30
26
key : & [ u8 ; SYMMETRIC_KEY_SIZE ] ,
31
27
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, [ ] )
37
30
}
38
31
39
32
/// Symmetrically decrypts the given ciphertext using ChaCha20-Poly1305 and the given
@@ -87,7 +80,7 @@ mod tests {
87
80
const AUTH : [ u8 ; 16 ] = hex ! ( "1ae10b594f09e26a7e902ecbd0600691" ) ;
88
81
89
82
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 )
91
84
}
92
85
93
86
#[ test]
@@ -104,7 +97,7 @@ mod tests {
104
97
fn test_random_key_and_nonce ( ) {
105
98
let key = random_data ( 32 ) . try_into ( ) . unwrap ( ) ;
106
99
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 ) ;
108
101
let decrypted_plaintext = aead_chacha20_poly1305_decrypt_with_aad ( ciphertext, & key, & nonce, AAD , & auth) . unwrap ( ) ;
109
102
assert_eq ! ( PLAINTEXT , decrypted_plaintext. as_slice( ) ) ;
110
103
}
@@ -113,7 +106,7 @@ mod tests {
113
106
fn test_empty_data ( ) {
114
107
let key = random_data ( 32 ) . try_into ( ) . unwrap ( ) ;
115
108
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, [ ] ) ;
117
110
let decrypted_plaintext = aead_chacha20_poly1305_decrypt_with_aad ( ciphertext, & key, & nonce, [ ] , & auth) . unwrap ( ) ;
118
111
assert_eq ! ( Vec :: <u8 >:: new( ) , decrypted_plaintext. as_slice( ) ) ;
119
112
}
0 commit comments