1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
use crate::{cipher::Cipher, Tag};
use aead::generic_array::{
typenum::{U0, U16, U24, U32},
GenericArray,
};
use aead::{Aead, Error, NewAead, Payload};
use alloc::vec::Vec;
use chacha20::{stream_cipher::NewStreamCipher, XChaCha20};
use zeroize::Zeroize;
#[derive(Clone)]
pub struct XChaCha20Poly1305 {
key: GenericArray<u8, U32>,
}
impl NewAead for XChaCha20Poly1305 {
type KeySize = U32;
fn new(key: GenericArray<u8, U32>) -> Self {
XChaCha20Poly1305 { key }
}
}
impl Aead for XChaCha20Poly1305 {
type NonceSize = U24;
type TagSize = U16;
type CiphertextOverhead = U0;
fn encrypt<'msg, 'aad>(
&self,
nonce: &GenericArray<u8, Self::NonceSize>,
plaintext: impl Into<Payload<'msg, 'aad>>,
) -> Result<Vec<u8>, Error> {
Cipher::new(XChaCha20::new(&self.key, nonce)).encrypt(plaintext.into())
}
fn decrypt<'msg, 'aad>(
&self,
nonce: &GenericArray<u8, Self::NonceSize>,
ciphertext: impl Into<Payload<'msg, 'aad>>,
) -> Result<Vec<u8>, Error> {
Cipher::new(XChaCha20::new(&self.key, nonce)).decrypt(ciphertext.into())
}
}
impl XChaCha20Poly1305 {
pub fn encrypt_in_place_detached(
&self,
nonce: &GenericArray<u8, <Self as Aead>::NonceSize>,
associated_data: &[u8],
buffer: &mut [u8],
) -> Result<Tag, Error> {
Cipher::new(XChaCha20::new(&self.key, nonce))
.encrypt_in_place_detached(associated_data, buffer)
}
pub fn decrypt_in_place_detached(
&self,
nonce: &GenericArray<u8, <Self as Aead>::NonceSize>,
associated_data: &[u8],
buffer: &mut [u8],
tag: &Tag,
) -> Result<(), Error> {
Cipher::new(XChaCha20::new(&self.key, nonce)).decrypt_in_place_detached(
associated_data,
buffer,
tag,
)
}
}
impl Drop for XChaCha20Poly1305 {
fn drop(&mut self) {
self.key.as_mut_slice().zeroize();
}
}