Skip to content

Commit a61e846

Browse files
committed
deduplicate quarter_round in xchacha, use soft backend version
1 parent 06aa0e9 commit a61e846

File tree

2 files changed

+9
-21
lines changed

2 files changed

+9
-21
lines changed

chacha20/src/backends/soft.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,13 @@ fn run_rounds<R: Unsigned>(state: &[u32; STATE_WORDS]) -> [u32; STATE_WORDS] {
5454
}
5555

5656
/// The ChaCha20 quarter round function
57-
fn quarter_round(a: usize, b: usize, c: usize, d: usize, state: &mut [u32; STATE_WORDS]) {
57+
pub(crate) fn quarter_round(
58+
a: usize,
59+
b: usize,
60+
c: usize,
61+
d: usize,
62+
state: &mut [u32; STATE_WORDS],
63+
) {
5864
state[a] = state[a].wrapping_add(state[b]);
5965
state[d] ^= state[a];
6066
state[d] = state[d].rotate_left(16);

chacha20/src/xchacha.rs

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use cipher::{
88
StreamCipherSeekCore, StreamClosure,
99
};
1010

11+
use crate::backends::soft::quarter_round;
12+
1113
#[cfg(feature = "zeroize")]
1214
use cipher::zeroize::ZeroizeOnDrop;
1315

@@ -144,26 +146,6 @@ pub fn hchacha<R: Unsigned>(key: &Key, input: &GenericArray<u8, U16>) -> Generic
144146
output
145147
}
146148

147-
/// The ChaCha20 quarter round function
148-
// for simplicity this function is copied from the software backend
149-
fn quarter_round(a: usize, b: usize, c: usize, d: usize, state: &mut [u32; STATE_WORDS]) {
150-
state[a] = state[a].wrapping_add(state[b]);
151-
state[d] ^= state[a];
152-
state[d] = state[d].rotate_left(16);
153-
154-
state[c] = state[c].wrapping_add(state[d]);
155-
state[b] ^= state[c];
156-
state[b] = state[b].rotate_left(12);
157-
158-
state[a] = state[a].wrapping_add(state[b]);
159-
state[d] ^= state[a];
160-
state[d] = state[d].rotate_left(8);
161-
162-
state[c] = state[c].wrapping_add(state[d]);
163-
state[b] ^= state[c];
164-
state[b] = state[b].rotate_left(7);
165-
}
166-
167149
#[cfg(test)]
168150
mod hchacha20_tests {
169151
use super::*;

0 commit comments

Comments
 (0)