Skip to content
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

Add opt_size feature for embedded environments. #2

Merged
merged 1 commit into from
Mar 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ digest = { version = "0.9", optional = true }

[features]
default = []
opt_size = []
traits = ["digest"]

[profile.release]
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ A small, self-contained SHA256 and HMAC-SHA256 implementation in Rust.
Optional features:

* `traits`: enable support for the `Digest` trait from the `digest` crate.
* `opt_size`: enable size optimizations. Based on benchmarks, the `.text`
section size is reduced by 75%, at the cost of approximately 16% performance.
8 changes: 5 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ impl W {
x.rotate_right(17) ^ x.rotate_right(19) ^ (x >> 10)
}

#[inline(always)]
#[cfg_attr(feature = "opt_size", inline(never))]
#[cfg_attr(not(feature = "opt_size"), inline(always))]
fn M(&mut self, a: usize, b: usize, c: usize, d: usize) {
let w = &mut self.0;
w[a] = w[a]
Expand Down Expand Up @@ -99,7 +100,8 @@ impl W {
self.M(15, (15 + 14) & 15, (15 + 9) & 15, (15 + 1) & 15);
}

#[inline(always)]
#[cfg_attr(feature = "opt_size", inline(never))]
#[cfg_attr(not(feature = "opt_size"), inline(always))]
fn F(&mut self, state: &mut State, i: usize, k: u32) {
let t = &mut state.0;
t[(16 - i + 7) & 7] = t[(16 - i + 7) & 7]
Expand Down Expand Up @@ -306,7 +308,7 @@ impl HMAC {
ih.update(input);

let mut oh = Hash::new();
let mut padded = [0x5c; 64];
padded = [0x5c; 64];
for (p, &k) in padded.iter_mut().zip(k2.iter()) {
*p ^= k;
}
Expand Down