Skip to content

Commit

Permalink
jank salsa20 refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
conradludgate committed Jul 12, 2024
1 parent a92aec7 commit 353384d
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 188 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/yescrypt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ jobs:
include:
# 32-bit Linux
- target: i686-unknown-linux-gnu
rust: 1.60.0 # MSRV
rust: 1.71.0 # MSRV
deps: sudo apt update && sudo apt install gcc-multilib
- target: i686-unknown-linux-gnu
rust: stable
deps: sudo apt update && sudo apt install gcc-multilib

# 64-bit Linux
- target: x86_64-unknown-linux-gnu
rust: 1.60.0 # MSRV
rust: 1.71.0 # MSRV
- target: x86_64-unknown-linux-gnu
rust: stable
steps:
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions yescrypt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ rust-version = "1.60"

[dependencies]
libc = "0.2"
# salsa20 = "0.10.2"
sha2 = "0.11.0-pre.3"
hmac = "0.13.0-pre.3"
pbkdf2 = { version = "0.13.0-pre.0", path = "../pbkdf2" }
hmac = { version = "0.13.0-pre.3", default-features = false }
pbkdf2 = { version = "=0.13.0-pre.0", path = "../pbkdf2" }
salsa20 = { version = "=0.11.0-pre", default-features = false }
sha2 = { version = "=0.11.0-pre.3", default-features = false }

#[dev-dependencies]
hex-literal = "0.4"
Expand Down
2 changes: 1 addition & 1 deletion yescrypt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Pure Rust implementation of the [yescrypt] password hashing function.

## Minimum Supported Rust Version

Rust **1.60** or higher.
Rust **1.71** or higher.

Minimum supported Rust version can be changed in the future, but it will be
done with a minor version bump.
Expand Down
203 changes: 22 additions & 181 deletions yescrypt/src/salsa20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,188 +5,29 @@ use crate::{

pub(crate) unsafe fn salsa20(mut B: *mut uint32_t, mut rounds: uint32_t) {
let mut x: [uint32_t; 16] = [0; 16];
let mut i: size_t = 0;
i = 0 as libc::c_int as size_t;
while i < 16 as libc::c_int as libc::c_ulong {
x[i.wrapping_mul(5 as libc::c_int as libc::c_ulong)
.wrapping_rem(16 as libc::c_int as libc::c_ulong) as usize] = *B.offset(i as isize);
i = i.wrapping_add(1);
i;
for i in 0..16 {
x[i * 5 % 16] = *B.offset(i as isize);
}
i = 0 as libc::c_int as size_t;
while i < rounds as libc::c_ulong {
x[4 as libc::c_int as usize] ^= (x[0 as libc::c_int as usize])
.wrapping_add(x[12 as libc::c_int as usize])
<< 7 as libc::c_int
| (x[0 as libc::c_int as usize]).wrapping_add(x[12 as libc::c_int as usize])
>> 32 as libc::c_int - 7 as libc::c_int;
x[8 as libc::c_int as usize] ^= (x[4 as libc::c_int as usize])
.wrapping_add(x[0 as libc::c_int as usize])
<< 9 as libc::c_int
| (x[4 as libc::c_int as usize]).wrapping_add(x[0 as libc::c_int as usize])
>> 32 as libc::c_int - 9 as libc::c_int;
x[12 as libc::c_int as usize] ^= (x[8 as libc::c_int as usize])
.wrapping_add(x[4 as libc::c_int as usize])
<< 13 as libc::c_int
| (x[8 as libc::c_int as usize]).wrapping_add(x[4 as libc::c_int as usize])
>> 32 as libc::c_int - 13 as libc::c_int;
x[0 as libc::c_int as usize] ^= (x[12 as libc::c_int as usize])
.wrapping_add(x[8 as libc::c_int as usize])
<< 18 as libc::c_int
| (x[12 as libc::c_int as usize]).wrapping_add(x[8 as libc::c_int as usize])
>> 32 as libc::c_int - 18 as libc::c_int;
x[9 as libc::c_int as usize] ^= (x[5 as libc::c_int as usize])
.wrapping_add(x[1 as libc::c_int as usize])
<< 7 as libc::c_int
| (x[5 as libc::c_int as usize]).wrapping_add(x[1 as libc::c_int as usize])
>> 32 as libc::c_int - 7 as libc::c_int;
x[13 as libc::c_int as usize] ^= (x[9 as libc::c_int as usize])
.wrapping_add(x[5 as libc::c_int as usize])
<< 9 as libc::c_int
| (x[9 as libc::c_int as usize]).wrapping_add(x[5 as libc::c_int as usize])
>> 32 as libc::c_int - 9 as libc::c_int;
x[1 as libc::c_int as usize] ^= (x[13 as libc::c_int as usize])
.wrapping_add(x[9 as libc::c_int as usize])
<< 13 as libc::c_int
| (x[13 as libc::c_int as usize]).wrapping_add(x[9 as libc::c_int as usize])
>> 32 as libc::c_int - 13 as libc::c_int;
x[5 as libc::c_int as usize] ^= (x[1 as libc::c_int as usize])
.wrapping_add(x[13 as libc::c_int as usize])
<< 18 as libc::c_int
| (x[1 as libc::c_int as usize]).wrapping_add(x[13 as libc::c_int as usize])
>> 32 as libc::c_int - 18 as libc::c_int;
x[14 as libc::c_int as usize] ^= (x[10 as libc::c_int as usize])
.wrapping_add(x[6 as libc::c_int as usize])
<< 7 as libc::c_int
| (x[10 as libc::c_int as usize]).wrapping_add(x[6 as libc::c_int as usize])
>> 32 as libc::c_int - 7 as libc::c_int;
x[2 as libc::c_int as usize] ^= (x[14 as libc::c_int as usize])
.wrapping_add(x[10 as libc::c_int as usize])
<< 9 as libc::c_int
| (x[14 as libc::c_int as usize]).wrapping_add(x[10 as libc::c_int as usize])
>> 32 as libc::c_int - 9 as libc::c_int;
x[6 as libc::c_int as usize] ^= (x[2 as libc::c_int as usize])
.wrapping_add(x[14 as libc::c_int as usize])
<< 13 as libc::c_int
| (x[2 as libc::c_int as usize]).wrapping_add(x[14 as libc::c_int as usize])
>> 32 as libc::c_int - 13 as libc::c_int;
x[10 as libc::c_int as usize] ^= (x[6 as libc::c_int as usize])
.wrapping_add(x[2 as libc::c_int as usize])
<< 18 as libc::c_int
| (x[6 as libc::c_int as usize]).wrapping_add(x[2 as libc::c_int as usize])
>> 32 as libc::c_int - 18 as libc::c_int;
x[3 as libc::c_int as usize] ^= (x[15 as libc::c_int as usize])
.wrapping_add(x[11 as libc::c_int as usize])
<< 7 as libc::c_int
| (x[15 as libc::c_int as usize]).wrapping_add(x[11 as libc::c_int as usize])
>> 32 as libc::c_int - 7 as libc::c_int;
x[7 as libc::c_int as usize] ^= (x[3 as libc::c_int as usize])
.wrapping_add(x[15 as libc::c_int as usize])
<< 9 as libc::c_int
| (x[3 as libc::c_int as usize]).wrapping_add(x[15 as libc::c_int as usize])
>> 32 as libc::c_int - 9 as libc::c_int;
x[11 as libc::c_int as usize] ^= (x[7 as libc::c_int as usize])
.wrapping_add(x[3 as libc::c_int as usize])
<< 13 as libc::c_int
| (x[7 as libc::c_int as usize]).wrapping_add(x[3 as libc::c_int as usize])
>> 32 as libc::c_int - 13 as libc::c_int;
x[15 as libc::c_int as usize] ^= (x[11 as libc::c_int as usize])
.wrapping_add(x[7 as libc::c_int as usize])
<< 18 as libc::c_int
| (x[11 as libc::c_int as usize]).wrapping_add(x[7 as libc::c_int as usize])
>> 32 as libc::c_int - 18 as libc::c_int;
x[1 as libc::c_int as usize] ^= (x[0 as libc::c_int as usize])
.wrapping_add(x[3 as libc::c_int as usize])
<< 7 as libc::c_int
| (x[0 as libc::c_int as usize]).wrapping_add(x[3 as libc::c_int as usize])
>> 32 as libc::c_int - 7 as libc::c_int;
x[2 as libc::c_int as usize] ^= (x[1 as libc::c_int as usize])
.wrapping_add(x[0 as libc::c_int as usize])
<< 9 as libc::c_int
| (x[1 as libc::c_int as usize]).wrapping_add(x[0 as libc::c_int as usize])
>> 32 as libc::c_int - 9 as libc::c_int;
x[3 as libc::c_int as usize] ^= (x[2 as libc::c_int as usize])
.wrapping_add(x[1 as libc::c_int as usize])
<< 13 as libc::c_int
| (x[2 as libc::c_int as usize]).wrapping_add(x[1 as libc::c_int as usize])
>> 32 as libc::c_int - 13 as libc::c_int;
x[0 as libc::c_int as usize] ^= (x[3 as libc::c_int as usize])
.wrapping_add(x[2 as libc::c_int as usize])
<< 18 as libc::c_int
| (x[3 as libc::c_int as usize]).wrapping_add(x[2 as libc::c_int as usize])
>> 32 as libc::c_int - 18 as libc::c_int;
x[6 as libc::c_int as usize] ^= (x[5 as libc::c_int as usize])
.wrapping_add(x[4 as libc::c_int as usize])
<< 7 as libc::c_int
| (x[5 as libc::c_int as usize]).wrapping_add(x[4 as libc::c_int as usize])
>> 32 as libc::c_int - 7 as libc::c_int;
x[7 as libc::c_int as usize] ^= (x[6 as libc::c_int as usize])
.wrapping_add(x[5 as libc::c_int as usize])
<< 9 as libc::c_int
| (x[6 as libc::c_int as usize]).wrapping_add(x[5 as libc::c_int as usize])
>> 32 as libc::c_int - 9 as libc::c_int;
x[4 as libc::c_int as usize] ^= (x[7 as libc::c_int as usize])
.wrapping_add(x[6 as libc::c_int as usize])
<< 13 as libc::c_int
| (x[7 as libc::c_int as usize]).wrapping_add(x[6 as libc::c_int as usize])
>> 32 as libc::c_int - 13 as libc::c_int;
x[5 as libc::c_int as usize] ^= (x[4 as libc::c_int as usize])
.wrapping_add(x[7 as libc::c_int as usize])
<< 18 as libc::c_int
| (x[4 as libc::c_int as usize]).wrapping_add(x[7 as libc::c_int as usize])
>> 32 as libc::c_int - 18 as libc::c_int;
x[11 as libc::c_int as usize] ^= (x[10 as libc::c_int as usize])
.wrapping_add(x[9 as libc::c_int as usize])
<< 7 as libc::c_int
| (x[10 as libc::c_int as usize]).wrapping_add(x[9 as libc::c_int as usize])
>> 32 as libc::c_int - 7 as libc::c_int;
x[8 as libc::c_int as usize] ^= (x[11 as libc::c_int as usize])
.wrapping_add(x[10 as libc::c_int as usize])
<< 9 as libc::c_int
| (x[11 as libc::c_int as usize]).wrapping_add(x[10 as libc::c_int as usize])
>> 32 as libc::c_int - 9 as libc::c_int;
x[9 as libc::c_int as usize] ^= (x[8 as libc::c_int as usize])
.wrapping_add(x[11 as libc::c_int as usize])
<< 13 as libc::c_int
| (x[8 as libc::c_int as usize]).wrapping_add(x[11 as libc::c_int as usize])
>> 32 as libc::c_int - 13 as libc::c_int;
x[10 as libc::c_int as usize] ^= (x[9 as libc::c_int as usize])
.wrapping_add(x[8 as libc::c_int as usize])
<< 18 as libc::c_int
| (x[9 as libc::c_int as usize]).wrapping_add(x[8 as libc::c_int as usize])
>> 32 as libc::c_int - 18 as libc::c_int;
x[12 as libc::c_int as usize] ^= (x[15 as libc::c_int as usize])
.wrapping_add(x[14 as libc::c_int as usize])
<< 7 as libc::c_int
| (x[15 as libc::c_int as usize]).wrapping_add(x[14 as libc::c_int as usize])
>> 32 as libc::c_int - 7 as libc::c_int;
x[13 as libc::c_int as usize] ^= (x[12 as libc::c_int as usize])
.wrapping_add(x[15 as libc::c_int as usize])
<< 9 as libc::c_int
| (x[12 as libc::c_int as usize]).wrapping_add(x[15 as libc::c_int as usize])
>> 32 as libc::c_int - 9 as libc::c_int;
x[14 as libc::c_int as usize] ^= (x[13 as libc::c_int as usize])
.wrapping_add(x[12 as libc::c_int as usize])
<< 13 as libc::c_int
| (x[13 as libc::c_int as usize]).wrapping_add(x[12 as libc::c_int as usize])
>> 32 as libc::c_int - 13 as libc::c_int;
x[15 as libc::c_int as usize] ^= (x[14 as libc::c_int as usize])
.wrapping_add(x[13 as libc::c_int as usize])
<< 18 as libc::c_int
| (x[14 as libc::c_int as usize]).wrapping_add(x[13 as libc::c_int as usize])
>> 32 as libc::c_int - 18 as libc::c_int;
i = (i as libc::c_ulong).wrapping_add(2 as libc::c_int as libc::c_ulong) as size_t
as size_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());
}
i = 0 as libc::c_int as size_t;
while i < 16 as libc::c_int as libc::c_ulong {
let ref mut fresh4 = *B.offset(i as isize);
*fresh4 = (*fresh4 as libc::c_uint).wrapping_add(
x[i.wrapping_mul(5 as libc::c_int as libc::c_ulong)
.wrapping_rem(16 as libc::c_int as libc::c_ulong) as usize],
) as uint32_t as uint32_t;
i = i.wrapping_add(1);
i;

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);
}

for i in 0..16 {
let x = (*B.offset(i as isize)).wrapping_add(x[i * 5 % 16]);
B.offset(i as isize).write(x)
}
}

Expand All @@ -210,7 +51,7 @@ pub(crate) unsafe fn blockmix_salsa8(mut B: *mut uint32_t, mut Y: *mut uint32_t,
&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,
);
salsa20(X.as_mut_ptr(), 8 as libc::c_int as uint32_t);
salsa20(X.as_mut_ptr(), 8);
blkcpy(
&mut *Y.offset(i.wrapping_mul(16 as libc::c_int as libc::c_ulong) as isize),
X.as_mut_ptr(),
Expand Down

0 comments on commit 353384d

Please sign in to comment.