Skip to content

Commit

Permalink
a little less jank salsa20
Browse files Browse the repository at this point in the history
  • Loading branch information
conradludgate committed Jul 12, 2024
1 parent 353384d commit 7079e5d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 47 deletions.
8 changes: 2 additions & 6 deletions yescrypt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1665,10 +1665,7 @@ unsafe fn blockmix_pwxform(mut B: *mut uint32_t, mut ctx: *mut PwxformCtx, mut r
.wrapping_sub(1 as libc::c_int as libc::c_ulong)
.wrapping_mul((4 as libc::c_int * 2 as libc::c_int * 8 as libc::c_int) as libc::c_ulong)
.wrapping_div(64 as libc::c_int as libc::c_ulong);
salsa20::salsa20(
&mut *B.offset(i.wrapping_mul(16 as libc::c_int as libc::c_ulong) as isize),
2 as libc::c_int as uint32_t,
);
salsa20::salsa20_2(&mut *B.offset(i.wrapping_mul(16 as libc::c_int as libc::c_ulong) as isize));
i = i.wrapping_add(1);
i;
while i < (2 as libc::c_int as libc::c_ulong).wrapping_mul(r) {
Expand All @@ -1680,9 +1677,8 @@ unsafe fn blockmix_pwxform(mut B: *mut uint32_t, mut ctx: *mut PwxformCtx, mut r
),
16 as libc::c_int as size_t,
);
salsa20::salsa20(
salsa20::salsa20_2(
&mut *B.offset(i.wrapping_mul(16 as libc::c_int as libc::c_ulong) as isize),
2 as libc::c_int as uint32_t,
);
i = i.wrapping_add(1);
i;
Expand Down
66 changes: 25 additions & 41 deletions yescrypt/src/salsa20.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
use salsa20::cipher::Unsigned;

use crate::{
common::{blkcpy, blkxor},
size_t, uint32_t,
};

pub(crate) unsafe fn salsa20(mut B: *mut uint32_t, mut rounds: uint32_t) {
pub(crate) unsafe fn salsa20_2(mut B: *mut uint32_t) {
salsa20::<salsa20::cipher::consts::U1>(B);
}

unsafe fn salsa20<R: Unsigned>(mut B: *mut uint32_t) {
let mut x: [uint32_t; 16] = [0; 16];
for i in 0..16 {
x[i * 5 % 16] = *B.offset(i as isize);
Expand All @@ -12,14 +18,7 @@ pub(crate) unsafe fn salsa20(mut B: *mut uint32_t, mut rounds: uint32_t) {
use salsa20::cipher::StreamCipherCore;

let mut block = [0u8; 64];

if rounds == 2 {
salsa20::SalsaCore::<salsa20::cipher::consts::U1>::from_raw_state(x)
.write_keystream_block((&mut block).into());
} else if rounds == 8 {
salsa20::SalsaCore::<salsa20::cipher::consts::U4>::from_raw_state(x)
.write_keystream_block((&mut block).into());
}
salsa20::SalsaCore::<R>::from_raw_state(x).write_keystream_block((&mut block).into());

for (c, b) in block.chunks_exact(4).zip(x.iter_mut()) {
*b = u32::from_le_bytes(c.try_into().expect("4 bytes is 1 u32")).wrapping_sub(*b);
Expand All @@ -36,56 +35,41 @@ pub(crate) unsafe fn blockmix_salsa8(mut B: *mut uint32_t, mut Y: *mut uint32_t,
let mut i: size_t = 0;
blkcpy(
X.as_mut_ptr(),
&mut *B.offset(
(2 as libc::c_int as libc::c_ulong)
.wrapping_mul(r)
.wrapping_sub(1 as libc::c_int as libc::c_ulong)
.wrapping_mul(16 as libc::c_int as libc::c_ulong) as isize,
),
16 as libc::c_int as size_t,
&mut *B.offset(2_u64.wrapping_mul(r).wrapping_sub(1).wrapping_mul(16) as isize),
16,
);
i = 0 as libc::c_int as size_t;
while i < (2 as libc::c_int as libc::c_ulong).wrapping_mul(r) {
i = 0;
while i < 2_u64.wrapping_mul(r) {
blkxor(
X.as_mut_ptr(),
&mut *B.offset(i.wrapping_mul(16 as libc::c_int as libc::c_ulong) as isize),
16 as libc::c_int as size_t,
&mut *B.offset(i.wrapping_mul(16) as isize),
16,
);
salsa20(X.as_mut_ptr(), 8);
salsa20::<salsa20::cipher::consts::U4>(X.as_mut_ptr());
blkcpy(
&mut *Y.offset(i.wrapping_mul(16 as libc::c_int as libc::c_ulong) as isize),
&mut *Y.offset(i.wrapping_mul(16) as isize),
X.as_mut_ptr(),
16 as libc::c_int as size_t,
16,
);
i = i.wrapping_add(1);
i;
}
i = 0 as libc::c_int as size_t;
i = 0;
while i < r {
blkcpy(
&mut *B.offset(i.wrapping_mul(16 as libc::c_int as libc::c_ulong) as isize),
&mut *Y.offset(
i.wrapping_mul(2 as libc::c_int as libc::c_ulong)
.wrapping_mul(16 as libc::c_int as libc::c_ulong) as isize,
),
16 as libc::c_int as size_t,
&mut *B.offset(i.wrapping_mul(16) as isize),
&mut *Y.offset(i.wrapping_mul(2).wrapping_mul(16) as isize),
16,
);
i = i.wrapping_add(1);
i;
}
i = 0 as libc::c_int as size_t;
i = 0;
while i < r {
blkcpy(
&mut *B.offset(
i.wrapping_add(r)
.wrapping_mul(16 as libc::c_int as libc::c_ulong) as isize,
),
&mut *Y.offset(
i.wrapping_mul(2 as libc::c_int as libc::c_ulong)
.wrapping_add(1 as libc::c_int as libc::c_ulong)
.wrapping_mul(16 as libc::c_int as libc::c_ulong) as isize,
),
16 as libc::c_int as size_t,
&mut *B.offset(i.wrapping_add(r).wrapping_mul(16) as isize),
&mut *Y.offset(i.wrapping_mul(2).wrapping_add(1).wrapping_mul(16) as isize),
16,
);
i = i.wrapping_add(1);
i;
Expand Down

0 comments on commit 7079e5d

Please sign in to comment.