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

feat: Migrate to memsec #224

Merged
merged 7 commits into from
Jan 3, 2024
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
28 changes: 24 additions & 4 deletions .github/workflows/qc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,26 @@ jobs:
- name: Run Rust Formatting Script
run: bash format_rust_code.sh --mode check

cargo-bench:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/cache@v3
with:
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- name: Install libsodium
run: sudo apt-get install -y libsodium-dev
# liboqs requires quite a lot of stack memory, thus we adjust
# the default stack size picked for new threads (which is used
# by `cargo test`) to be _big enough_. Setting it to 8 MiB
- run: RUST_MIN_STACK=8388608 cargo bench --no-run --workspace

cargo-audit:
runs-on: ubuntu-latest
steps:
Expand Down Expand Up @@ -101,7 +121,7 @@ jobs:
# liboqs requires quite a lot of stack memory, thus we adjust
# the default stack size picked for new threads (which is used
# by `cargo test`) to be _big enough_. Setting it to 8 MiB
- run: RUST_MIN_STACK=8388608 cargo test
- run: RUST_MIN_STACK=8388608 cargo test --workspace

cargo-test-nix-devshell-x86_64-linux:
runs-on:
Expand All @@ -124,7 +144,7 @@ jobs:
with:
name: rosenpass
authToken: ${{ secrets.CACHIX_AUTH_TOKEN }}
- run: nix develop --command cargo test
- run: nix develop --command cargo test --workspace

cargo-fuzz:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -154,5 +174,5 @@ jobs:
cargo fuzz run fuzz_handle_msg -- -max_total_time=5
ulimit -s 8192000 && RUST_MIN_STACK=33554432000 && cargo fuzz run fuzz_kyber_encaps -- -max_total_time=5
cargo fuzz run fuzz_mceliece_encaps -- -max_total_time=5
cargo fuzz run fuzz_box_sodium_alloc -- -max_total_time=5
cargo fuzz run fuzz_vec_sodium_alloc -- -max_total_time=5
cargo fuzz run fuzz_box_secret_alloc -- -max_total_time=5
cargo fuzz run fuzz_vec_secret_alloc -- -max_total_time=5
95 changes: 92 additions & 3 deletions Cargo.lock

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

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,14 @@ doc-comment = "0.3.3"
base64 = "0.21.5"
zeroize = "1.7.0"
memoffset = "0.9.0"
lazy_static = "1.4.0"
thiserror = "1.0.50"
paste = "1.0.14"
env_logger = "0.10.1"
toml = "0.7.8"
static_assertions = "1.1.0"
allocator-api2 = "0.2.16"
allocator-api2 = "0.2.14"
allocator-api2-tests = "0.2.14"
memsec = "0.6.3"
rand = "0.8.5"
log = { version = "0.4.20" }
clap = { version = "4.4.10", features = ["derive"] }
Expand Down
2 changes: 1 addition & 1 deletion constant-time/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use rosenpass_to::{with_destination, To};
///
/// If source and destination are of different sizes.
#[inline]
pub fn xor<'a>(src: &'a [u8]) -> impl To<[u8], ()> + 'a {
pub fn xor(src: &[u8]) -> impl To<[u8], ()> + '_ {
with_destination(|dst: &mut [u8]| {
assert!(src.len() == dst.len());
for (dv, sv) in dst.iter_mut().zip(src.iter()) {
Expand Down
8 changes: 4 additions & 4 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ test = false
doc = false

[[bin]]
name = "fuzz_box_sodium_alloc"
path = "fuzz_targets/box_sodium_alloc.rs"
name = "fuzz_box_secret_alloc"
path = "fuzz_targets/box_secret_alloc.rs"
test = false
doc = false

[[bin]]
name = "fuzz_vec_sodium_alloc"
path = "fuzz_targets/vec_sodium_alloc.rs"
name = "fuzz_vec_secret_alloc"
path = "fuzz_targets/vec_secret_alloc.rs"
test = false
doc = false
8 changes: 8 additions & 0 deletions fuzz/fuzz_targets/box_secret_alloc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#![no_main]

use libfuzzer_sys::fuzz_target;
use rosenpass_secret_memory::alloc::secret_box;

fuzz_target!(|data: &[u8]| {
let _ = secret_box(data);
});
12 changes: 0 additions & 12 deletions fuzz/fuzz_targets/box_sodium_alloc.rs

This file was deleted.

9 changes: 9 additions & 0 deletions fuzz/fuzz_targets/vec_secret_alloc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#![no_main]

use libfuzzer_sys::fuzz_target;
use rosenpass_secret_memory::alloc::secret_vec;

fuzz_target!(|data: &[u8]| {
let mut vec = secret_vec();
vec.extend_from_slice(data);
});
13 changes: 0 additions & 13 deletions fuzz/fuzz_targets/vec_sodium_alloc.rs

This file was deleted.

14 changes: 6 additions & 8 deletions rosenpass/benches/handshake.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use anyhow::Result;
use rosenpass::pqkem::KEM;
use rosenpass::{
pqkem::StaticKEM,
protocol::{CryptoServer, HandleMsgResult, MsgBuf, PeerPtr, SPk, SSk, SymKey},
sodium::sodium_init,
};
use rosenpass::protocol::{CryptoServer, HandleMsgResult, MsgBuf, PeerPtr, SPk, SSk, SymKey};

use rosenpass_cipher_traits::Kem;
use rosenpass_ciphers::kem::StaticKem;

use criterion::{black_box, criterion_group, criterion_main, Criterion};

Expand Down Expand Up @@ -41,7 +39,7 @@ fn hs(ini: &mut CryptoServer, res: &mut CryptoServer) -> Result<()> {

fn keygen() -> Result<(SSk, SPk)> {
let (mut sk, mut pk) = (SSk::zero(), SPk::zero());
StaticKEM::keygen(sk.secret_mut(), pk.secret_mut())?;
StaticKem::keygen(sk.secret_mut(), pk.secret_mut())?;
Ok((sk, pk))
}

Expand All @@ -58,7 +56,7 @@ fn make_server_pair() -> Result<(CryptoServer, CryptoServer)> {
}

fn criterion_benchmark(c: &mut Criterion) {
sodium_init().unwrap();
rosenpass_sodium::init().unwrap();
let (mut a, mut b) = make_server_pair().unwrap();
c.bench_function("cca_secret_alloc", |bench| {
bench.iter(|| {
Expand Down
2 changes: 1 addition & 1 deletion rosenpass/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ impl Cli {
// Manual arg parsing, since clap wants to prefix flags with "--"
let mut args = args.into_iter();
loop {
match (args.next().as_ref().map(String::as_str), args.next()) {
match (args.next().as_deref(), args.next()) {
(Some("private-key"), Some(opt)) | (Some("secret-key"), Some(opt)) => {
secret_key = Some(opt.into());
}
Expand Down
2 changes: 1 addition & 1 deletion rosenpass/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ mod test {
use super::*;

fn split_str(s: &str) -> Vec<String> {
s.split(" ").map(|s| s.to_string()).collect()
s.split(' ').map(|s| s.to_string()).collect()
}

#[test]
Expand Down
Loading
Loading