Skip to content

Commit 9ee394e

Browse files
committed
deduplicate quarter_round in xchacha, use soft backend version
1 parent e1269b1 commit 9ee394e

File tree

3 files changed

+32
-41
lines changed

3 files changed

+32
-41
lines changed

chacha20/src/backends/soft.rs

+1-20
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Portable implementation which does not rely on architecture-specific
22
//! intrinsics.
33
4-
use crate::{ChaChaCore, Rounds, Variant, STATE_WORDS};
4+
use crate::{quarter_round, ChaChaCore, Rounds, Variant, STATE_WORDS};
55

66
#[cfg(feature = "cipher")]
77
use crate::chacha::Block;
@@ -74,22 +74,3 @@ fn run_rounds<R: Rounds>(state: &[u32; STATE_WORDS]) -> [u32; STATE_WORDS] {
7474
}
7575
res
7676
}
77-
78-
/// The ChaCha20 quarter round function
79-
fn quarter_round(a: usize, b: usize, c: usize, d: usize, state: &mut [u32; STATE_WORDS]) {
80-
state[a] = state[a].wrapping_add(state[b]);
81-
state[d] ^= state[a];
82-
state[d] = state[d].rotate_left(16);
83-
84-
state[c] = state[c].wrapping_add(state[d]);
85-
state[b] ^= state[c];
86-
state[b] = state[b].rotate_left(12);
87-
88-
state[a] = state[a].wrapping_add(state[b]);
89-
state[d] ^= state[a];
90-
state[d] = state[d].rotate_left(8);
91-
92-
state[c] = state[c].wrapping_add(state[d]);
93-
state[b] ^= state[c];
94-
state[b] = state[b].rotate_left(7);
95-
}

chacha20/src/lib.rs

+28
Original file line numberDiff line numberDiff line change
@@ -343,3 +343,31 @@ impl<R: Rounds, V: Variant> Drop for ChaChaCore<R, V> {
343343
#[cfg(feature = "zeroize")]
344344
#[cfg_attr(docsrs, doc(cfg(feature = "zeroize")))]
345345
impl<R: Rounds, V: Variant> ZeroizeOnDrop for ChaChaCore<R, V> {}
346+
347+
/// The ChaCha20 quarter round function
348+
///
349+
/// We located this function in the root of the crate as we want it to be available
350+
/// for the soft backend and for xchacha.
351+
pub(crate) fn quarter_round(
352+
a: usize,
353+
b: usize,
354+
c: usize,
355+
d: usize,
356+
state: &mut [u32; STATE_WORDS],
357+
) {
358+
state[a] = state[a].wrapping_add(state[b]);
359+
state[d] ^= state[a];
360+
state[d] = state[d].rotate_left(16);
361+
362+
state[c] = state[c].wrapping_add(state[d]);
363+
state[b] ^= state[c];
364+
state[b] = state[b].rotate_left(12);
365+
366+
state[a] = state[a].wrapping_add(state[b]);
367+
state[d] ^= state[a];
368+
state[d] = state[d].rotate_left(8);
369+
370+
state[c] = state[c].wrapping_add(state[d]);
371+
state[b] ^= state[c];
372+
state[b] = state[b].rotate_left(7);
373+
}

chacha20/src/xchacha.rs

+3-21
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ use cipher::{
77
StreamCipherSeekCore, StreamClosure,
88
};
99

10-
use crate::{variants::Ietf, ChaChaCore, Rounds, CONSTANTS, R12, R20, R8, STATE_WORDS};
10+
use crate::{
11+
quarter_round, variants::Ietf, ChaChaCore, Rounds, CONSTANTS, R12, R20, R8, STATE_WORDS,
12+
};
1113

1214
#[cfg(feature = "zeroize")]
1315
use zeroize::ZeroizeOnDrop;
@@ -151,26 +153,6 @@ pub fn hchacha<R: Rounds>(key: &Key, input: &Array<u8, U16>) -> Array<u8, U32> {
151153
output
152154
}
153155

154-
/// The ChaCha20 quarter round function
155-
// for simplicity this function is copied from the software backend
156-
fn quarter_round(a: usize, b: usize, c: usize, d: usize, state: &mut [u32; STATE_WORDS]) {
157-
state[a] = state[a].wrapping_add(state[b]);
158-
state[d] ^= state[a];
159-
state[d] = state[d].rotate_left(16);
160-
161-
state[c] = state[c].wrapping_add(state[d]);
162-
state[b] ^= state[c];
163-
state[b] = state[b].rotate_left(12);
164-
165-
state[a] = state[a].wrapping_add(state[b]);
166-
state[d] ^= state[a];
167-
state[d] = state[d].rotate_left(8);
168-
169-
state[c] = state[c].wrapping_add(state[d]);
170-
state[b] ^= state[c];
171-
state[b] = state[b].rotate_left(7);
172-
}
173-
174156
#[cfg(test)]
175157
mod hchacha20_tests {
176158
use super::*;

0 commit comments

Comments
 (0)