Skip to content

Commit ffe1018

Browse files
committed
wip: set chacha20black3 as default cipher mode
1 parent 0911f59 commit ffe1018

File tree

8 files changed

+39
-38
lines changed

8 files changed

+39
-38
lines changed

benches/src/chacha20.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ fn bench(c: &mut Criterion) {
3737
chacha20.init(&key, &iv);
3838

3939
group.bench_with_input(BenchmarkId::new("process", size), size, |b, &_size| {
40-
let mut bytes = vec![0u8; *size];
41-
b.iter(|| chacha20.process(&mut bytes));
40+
let bytes = vec![0u8; *size];
41+
b.iter(|| chacha20.process(bytes.clone()));
4242
});
4343
}
4444

benches/src/cipher-signed-vs-unsigned.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use criterion::{
2-
criterion_group, criterion_main, BenchmarkId, Criterion, PlotConfiguration, Throughput,
2+
black_box, criterion_group, criterion_main, BenchmarkId, Criterion, PlotConfiguration, Throughput,
33
};
44
use secured_cipher::{
55
algorithm::{chacha20::CHACHA20_NONCE_SIZE, Blake3Mac},
@@ -14,7 +14,7 @@ fn bench(c: &mut Criterion) {
1414
let plot_config = PlotConfiguration::default().summary_scale(criterion::AxisScale::Logarithmic);
1515
group.plot_config(plot_config);
1616

17-
let data_size = 100 * MB;
17+
let data_size = 1024 * MB;
1818
let blocks_per_thread_options = [1000];
1919

2020
for &blocks_per_thread in &blocks_per_thread_options {
@@ -71,7 +71,7 @@ fn bench(c: &mut Criterion) {
7171
&data_size,
7272
|b, &data_size| {
7373
let mut bytes = vec![0u8; data_size];
74-
b.iter(|| unsigned_cipher.encrypt_in_place(&mut bytes));
74+
b.iter(|| unsigned_cipher.encrypt_in_place(black_box(&mut bytes)));
7575
},
7676
);
7777
}

cipher/src/algorithm/blake3/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ impl AlgorithmKeyInit for Blake3Mac {
2222
}
2323

2424
impl AlgorithmProcess for Blake3Mac {
25-
fn process(&mut self, data: &[u8]) -> Vec<u8> {
25+
fn process(&mut self, data: Vec<u8>) -> Vec<u8> {
2626
let mut hasher = Hasher::new_keyed(&self.key);
27-
hasher.update_rayon(data);
27+
hasher.update_rayon(&data);
2828
hasher.finalize().as_bytes().to_vec()
2929
}
3030
}

cipher/src/algorithm/chacha20/mod.rs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ impl AlgorithmProcess for ChaCha20 {
164164
/// for both encryption and decryption due to the reversible nature of the XOR operation.
165165
///
166166
/// # Arguments
167-
/// * `bytes_in` - A slice of bytes representing the input data to be processed (either plaintext for encryption
167+
/// * `bytes_in` - A mutable vector of bytes representing the input data to be processed (either plaintext for encryption
168168
/// or ciphertext for decryption).
169169
///
170170
/// # Returns
@@ -185,12 +185,9 @@ impl AlgorithmProcess for ChaCha20 {
185185
/// # Notes
186186
/// It's important to use the same nonce and key for decrypting the data that were used for encryption.
187187
/// The output size will be equal to the input size, as ChaCha20 is a stream cipher.
188-
fn process(&mut self, bytes_in: &[u8]) -> Vec<u8> {
189-
// Clone the input bytes to prepare the output vector
190-
let mut out = bytes_in.to_owned();
191-
188+
fn process(&mut self, mut bytes_in: Vec<u8>) -> Vec<u8> {
192189
// Process each 64-byte block of the input data
193-
out
190+
bytes_in
194191
.par_chunks_mut(CHACHA20_BLOCK_SIZE * 100)
195192
.enumerate()
196193
.for_each(|(i, par_chunk)| {
@@ -203,7 +200,7 @@ impl AlgorithmProcess for ChaCha20 {
203200
});
204201

205202
// Return the processed data
206-
out.to_vec()
203+
bytes_in
207204
}
208205
}
209206

@@ -310,7 +307,7 @@ mod tests {
310307
],
311308
);
312309

313-
let encrypted_data = chacha20.process(&PLAINTEXT);
310+
let encrypted_data = chacha20.process(PLAINTEXT.to_vec());
314311

315312
assert_eq!(encrypted_data, CIPHERTEXT);
316313
}
@@ -319,10 +316,10 @@ mod tests {
319316
fn it_can_reverse_encryption() {
320317
let mut chacha20 = ChaCha20::default();
321318
chacha20.init(&[1u8; 32], &[2u8; CHACHA20_NONCE_SIZE]);
322-
let data = [0u8; 64];
319+
let data = vec![0u8; 64];
323320

324-
let encrypted_data = chacha20.process(&data);
325-
let decrypted_data = chacha20.process(&encrypted_data);
321+
let encrypted_data = chacha20.process(data.clone());
322+
let decrypted_data = chacha20.process(encrypted_data);
326323

327324
assert_eq!(decrypted_data, data);
328325
}
@@ -331,10 +328,10 @@ mod tests {
331328
fn it_can_reverse_encryption_for_data_smaller_than_a_chunk() {
332329
let mut chacha20 = ChaCha20::default();
333330
chacha20.init(&[1u8; 32], &[2u8; CHACHA20_NONCE_SIZE]);
334-
let data = [0u8; 1];
331+
let data = vec![0u8; 1];
335332

336-
let encrypted_data = chacha20.process(&data);
337-
let decrypted_data = chacha20.process(&encrypted_data);
333+
let encrypted_data = chacha20.process(data.clone());
334+
let decrypted_data = chacha20.process(encrypted_data);
338335

339336
assert_eq!(decrypted_data, data);
340337
}
@@ -345,11 +342,11 @@ mod tests {
345342
chacha20_1.init(&[0u8; 32], &[0u8; CHACHA20_NONCE_SIZE]);
346343
let mut chacha20_2 = ChaCha20::default();
347344
chacha20_2.init(&[0u8; 32], &[0u8; CHACHA20_NONCE_SIZE]);
348-
let mut data = [0u8; 64 * 1000];
345+
let mut data = vec![0u8; 64 * 1000];
349346
let data2 = data.clone();
350347

351348
chacha20_1.process_in_place(&mut data);
352-
let encrypted_sync = chacha20_2.process(&data2);
349+
let encrypted_sync = chacha20_2.process(data2);
353350

354351
assert_eq!(data.to_vec(), encrypted_sync);
355352
}

cipher/src/algorithm/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub trait AlgorithmProcess {
5050
///
5151
/// # Returns
5252
/// A vector of bytes representing the processed data.
53-
fn process(&mut self, data: &[u8]) -> Vec<u8>;
53+
fn process(&mut self, data: Vec<u8>) -> Vec<u8>;
5454
}
5555

5656
pub trait AlgorithmProcessInPlace {

cipher/src/algorithm/poly1305/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,11 @@ impl AlgorithmProcess for Poly1305 {
118118
/// 16-byte blocks, the final block is padded as necessary.
119119
///
120120
/// # Arguments
121-
/// * `data` - A byte slice representing the data to be processed.
121+
/// * `data` - A byte vector representing the data to be processed.
122122
///
123123
/// # Returns
124124
/// A vector of bytes (`Vec<u8>`) containing the computed MAC.
125-
fn process(&mut self, data: &[u8]) -> Vec<u8> {
125+
fn process(&mut self, data: Vec<u8>) -> Vec<u8> {
126126
let blocks = data.chunks_exact(16);
127127
let partial = blocks.remainder();
128128

cipher/src/lib.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ impl Cipher {
125125
// The Poly1305 authenticator uses a subkey derived from the cipher's key.
126126
// This subkey is generated by running the ChaCha20 permutation on a block of zeros.
127127
if let Some(aead) = &mut self.aead {
128-
aead.init(&self.encryption.process(&[0; 64]));
128+
aead.init(&self.encryption.process(vec![0; 64]));
129129
}
130130

131131
self
@@ -134,11 +134,11 @@ impl Cipher {
134134
/// Encrypts the provided data.
135135
///
136136
/// # Arguments
137-
/// * `data` - A slice of data to be encrypted.
137+
/// * `data` - A vector of bytes to be encrypted.
138138
///
139139
/// # Returns
140140
/// Encrypted data as a vector of bytes (`Bytes`).
141-
pub fn encrypt(&mut self, data: &[u8]) -> Vec<u8> {
141+
pub fn encrypt(&mut self, data: Vec<u8>) -> Vec<u8> {
142142
self.encryption.process(data)
143143
}
144144

@@ -173,7 +173,7 @@ impl Cipher {
173173
payload.extend_from_slice(&header);
174174
payload.extend_from_slice(&data);
175175

176-
let mac = aead.process(&payload);
176+
let mac = aead.process(payload);
177177

178178
SignedEnvelope { header, data, mac }
179179
}
@@ -183,11 +183,11 @@ impl Cipher {
183183
/// use cases should be covered by `decrypt_and_verify()` instead.
184184
///
185185
/// # Arguments
186-
/// * `data` - A slice of data to be decrypted.
186+
/// * `data` - A vector of bytes to be decrypted.
187187
///
188188
/// # Returns
189189
/// Decrypted data as a vector of bytes (`Bytes`).
190-
pub fn decrypt(&mut self, data: &[u8]) -> Vec<u8> {
190+
pub fn decrypt(&mut self, data: Vec<u8>) -> Vec<u8> {
191191
// Decrypt the data using the ChaCha20 permutation
192192
self.encryption.process(data)
193193
}
@@ -199,20 +199,24 @@ impl Cipher {
199199
///
200200
/// # Returns
201201
/// Decrypted data as a vector of bytes (`Bytes`), or an error in case of decryption failure.
202-
pub fn decrypt_and_verify(&mut self, envelope: &SignedEnvelope) -> Result<Vec<u8>, CipherError> {
202+
pub fn decrypt_and_verify(&mut self, envelope: SignedEnvelope) -> Result<Vec<u8>, CipherError> {
203203
// Ensure the AEAD algorithm is available
204204
let aead = self
205205
.aead
206206
.as_mut()
207207
.expect("AEAD algorithm is not initialized");
208208

209209
// Check the MAC (message authentication code) to ensure the integrity of the data
210-
if envelope.mac != aead.process(&[envelope.header.clone(), envelope.data.clone()].concat()) {
210+
let mut payload = Vec::with_capacity(envelope.header.len() + envelope.data.len());
211+
payload.extend_from_slice(&envelope.header);
212+
payload.extend_from_slice(&envelope.data);
213+
let expected_mac = aead.process(payload);
214+
if envelope.mac != expected_mac {
211215
return Err(CipherError::AuthenticationFailed);
212216
}
213217

214218
// Decrypt the data using the ChaCha20 permutation
215-
Ok(self.encryption.process(&envelope.data))
219+
Ok(self.encryption.process(envelope.data))
216220
}
217221
}
218222

@@ -222,7 +226,7 @@ impl Default for Cipher {
222226
/// # Returns
223227
/// A new instance of `Cipher` with XChaCha20 mode.
224228
fn default() -> Self {
225-
Self::new(CipherMode::ChaCha20Poly1305)
229+
Self::new(CipherMode::ChaCha20Blake3)
226230
}
227231
}
228232

enclave/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ where
5353
let mut cipher = Cipher::default();
5454
cipher.init(&key, &nonce);
5555

56-
let encrypted_bytes = cipher.encrypt(&plain_bytes);
56+
let encrypted_bytes = cipher.encrypt(plain_bytes);
5757
let envelope: Vec<u8> = cipher.sign(metadata.clone().into(), encrypted_bytes).into();
5858

5959
Ok(Enclave {
@@ -76,7 +76,7 @@ where
7676
Ok(
7777
Cipher::default()
7878
.init(&key, &self.nonce)
79-
.decrypt_and_verify(&envelope)?,
79+
.decrypt_and_verify(envelope)?,
8080
)
8181
}
8282

0 commit comments

Comments
 (0)