-
Notifications
You must be signed in to change notification settings - Fork 258
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
sha2: RISC-V scalar crypto extension support #614
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b37cfa0
sha2: RISC-V scalar crypto support
newpavlov b263635
Replace macros with fns
newpavlov 14158e9
Add compact versions
newpavlov 55538b5
Add compilation error for zknh on non-RISC-V targets
newpavlov 8347f71
Add helper functions for block conversion
newpavlov a414a1c
fix Clippy lints
newpavlov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
use crate::consts::K32; | ||
|
||
#[cfg(target_arch = "riscv32")] | ||
use core::arch::riscv32::*; | ||
#[cfg(target_arch = "riscv64")] | ||
use core::arch::riscv64::*; | ||
|
||
#[cfg(not(target_feature = "zknh"))] | ||
compile_error!("riscv-zknh backend requires enabled zknh target feature"); | ||
|
||
#[inline(always)] | ||
fn ch(x: u32, y: u32, z: u32) -> u32 { | ||
(x & y) ^ (!x & z) | ||
} | ||
|
||
#[inline(always)] | ||
fn maj(x: u32, y: u32, z: u32) -> u32 { | ||
(x & y) ^ (x & z) ^ (y & z) | ||
} | ||
|
||
#[allow(clippy::identity_op)] | ||
fn round<const R: usize>(state: &mut [u32; 8], block: &[u32; 16]) { | ||
let n = K32.len() - R; | ||
#[allow(clippy::identity_op)] | ||
let a = (n + 0) % 8; | ||
let b = (n + 1) % 8; | ||
let c = (n + 2) % 8; | ||
let d = (n + 3) % 8; | ||
let e = (n + 4) % 8; | ||
let f = (n + 5) % 8; | ||
let g = (n + 6) % 8; | ||
let h = (n + 7) % 8; | ||
|
||
state[h] = state[h] | ||
.wrapping_add(unsafe { sha256sum1(state[e]) }) | ||
.wrapping_add(ch(state[e], state[f], state[g])) | ||
// Force reading of constants from the static to prevent bad codegen | ||
.wrapping_add(unsafe { core::ptr::read_volatile(&K32[R]) }) | ||
.wrapping_add(block[R % 16]); | ||
state[d] = state[d].wrapping_add(state[h]); | ||
state[h] = state[h] | ||
.wrapping_add(unsafe { sha256sum0(state[a]) }) | ||
.wrapping_add(maj(state[a], state[b], state[c])) | ||
} | ||
|
||
fn round_schedule<const R: usize>(state: &mut [u32; 8], block: &mut [u32; 16]) { | ||
round::<R>(state, block); | ||
|
||
block[R % 16] = block[R % 16] | ||
.wrapping_add(unsafe { sha256sig1(block[(R + 14) % 16]) }) | ||
.wrapping_add(block[(R + 9) % 16]) | ||
.wrapping_add(unsafe { sha256sig0(block[(R + 1) % 16]) }); | ||
} | ||
|
||
fn compress_block(state: &mut [u32; 8], mut block: [u32; 16]) { | ||
let s = &mut state.clone(); | ||
let b = &mut block; | ||
|
||
round_schedule::<0>(s, b); | ||
round_schedule::<1>(s, b); | ||
round_schedule::<2>(s, b); | ||
round_schedule::<3>(s, b); | ||
round_schedule::<4>(s, b); | ||
round_schedule::<5>(s, b); | ||
round_schedule::<6>(s, b); | ||
round_schedule::<7>(s, b); | ||
round_schedule::<8>(s, b); | ||
round_schedule::<9>(s, b); | ||
round_schedule::<10>(s, b); | ||
round_schedule::<11>(s, b); | ||
round_schedule::<12>(s, b); | ||
round_schedule::<13>(s, b); | ||
round_schedule::<14>(s, b); | ||
round_schedule::<15>(s, b); | ||
round_schedule::<16>(s, b); | ||
round_schedule::<17>(s, b); | ||
round_schedule::<18>(s, b); | ||
round_schedule::<19>(s, b); | ||
round_schedule::<20>(s, b); | ||
round_schedule::<21>(s, b); | ||
round_schedule::<22>(s, b); | ||
round_schedule::<23>(s, b); | ||
round_schedule::<24>(s, b); | ||
round_schedule::<25>(s, b); | ||
round_schedule::<26>(s, b); | ||
round_schedule::<27>(s, b); | ||
round_schedule::<28>(s, b); | ||
round_schedule::<29>(s, b); | ||
round_schedule::<30>(s, b); | ||
round_schedule::<31>(s, b); | ||
round_schedule::<32>(s, b); | ||
round_schedule::<33>(s, b); | ||
round_schedule::<34>(s, b); | ||
round_schedule::<35>(s, b); | ||
round_schedule::<36>(s, b); | ||
round_schedule::<37>(s, b); | ||
round_schedule::<38>(s, b); | ||
round_schedule::<39>(s, b); | ||
round_schedule::<40>(s, b); | ||
round_schedule::<41>(s, b); | ||
round_schedule::<42>(s, b); | ||
round_schedule::<43>(s, b); | ||
round_schedule::<44>(s, b); | ||
round_schedule::<45>(s, b); | ||
round_schedule::<46>(s, b); | ||
round_schedule::<47>(s, b); | ||
round::<48>(s, b); | ||
round::<49>(s, b); | ||
round::<50>(s, b); | ||
round::<51>(s, b); | ||
round::<52>(s, b); | ||
round::<53>(s, b); | ||
round::<54>(s, b); | ||
round::<55>(s, b); | ||
round::<56>(s, b); | ||
round::<57>(s, b); | ||
round::<58>(s, b); | ||
round::<59>(s, b); | ||
round::<60>(s, b); | ||
round::<61>(s, b); | ||
round::<62>(s, b); | ||
round::<63>(s, b); | ||
|
||
for i in 0..8 { | ||
state[i] = state[i].wrapping_add(s[i]); | ||
} | ||
} | ||
|
||
pub fn compress(state: &mut [u32; 8], blocks: &[[u8; 64]]) { | ||
for block in blocks.iter().map(super::to_u32s) { | ||
compress_block(state, block); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
use crate::consts::K32; | ||
|
||
#[cfg(target_arch = "riscv32")] | ||
use core::arch::riscv32::*; | ||
#[cfg(target_arch = "riscv64")] | ||
use core::arch::riscv64::*; | ||
|
||
#[cfg(not(target_feature = "zknh"))] | ||
compile_error!("riscv-zknh backend requires enabled zknh target feature"); | ||
|
||
#[inline(always)] | ||
fn ch(x: u32, y: u32, z: u32) -> u32 { | ||
(x & y) ^ (!x & z) | ||
} | ||
|
||
#[inline(always)] | ||
fn maj(x: u32, y: u32, z: u32) -> u32 { | ||
(x & y) ^ (x & z) ^ (y & z) | ||
} | ||
|
||
#[inline(always)] | ||
fn round(state: &mut [u32; 8], block: &[u32; 16], r: usize) { | ||
let n = K32.len() - r; | ||
#[allow(clippy::identity_op)] | ||
let a = (n + 0) % 8; | ||
let b = (n + 1) % 8; | ||
let c = (n + 2) % 8; | ||
let d = (n + 3) % 8; | ||
let e = (n + 4) % 8; | ||
let f = (n + 5) % 8; | ||
let g = (n + 6) % 8; | ||
let h = (n + 7) % 8; | ||
|
||
state[h] = state[h] | ||
.wrapping_add(unsafe { sha256sum1(state[e]) }) | ||
.wrapping_add(ch(state[e], state[f], state[g])) | ||
.wrapping_add(K32[r]) | ||
.wrapping_add(block[r % 16]); | ||
state[d] = state[d].wrapping_add(state[h]); | ||
state[h] = state[h] | ||
.wrapping_add(unsafe { sha256sum0(state[a]) }) | ||
.wrapping_add(maj(state[a], state[b], state[c])) | ||
} | ||
|
||
#[inline(always)] | ||
fn round_schedule(state: &mut [u32; 8], block: &mut [u32; 16], r: usize) { | ||
round(state, block, r); | ||
|
||
block[r % 16] = block[r % 16] | ||
.wrapping_add(unsafe { sha256sig1(block[(r + 14) % 16]) }) | ||
.wrapping_add(block[(r + 9) % 16]) | ||
.wrapping_add(unsafe { sha256sig0(block[(r + 1) % 16]) }); | ||
} | ||
|
||
#[inline(always)] | ||
fn compress_block(state: &mut [u32; 8], mut block: [u32; 16]) { | ||
let s = &mut state.clone(); | ||
let b = &mut block; | ||
|
||
for i in 0..48 { | ||
round_schedule(s, b, i); | ||
} | ||
for i in 48..64 { | ||
round(s, b, i); | ||
} | ||
|
||
for i in 0..8 { | ||
state[i] = state[i].wrapping_add(s[i]); | ||
} | ||
} | ||
|
||
pub fn compress(state: &mut [u32; 8], blocks: &[[u8; 64]]) { | ||
for block in blocks.iter().map(super::to_u32s) { | ||
compress_block(state, block); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that we do not run tests on RV32 because
cross
currently does not support theriscv32gc-unknown-linux-gnu
target.