Skip to content

Commit

Permalink
add ctgrind-like testing of CodePoint::decode_secret
Browse files Browse the repository at this point in the history
  • Loading branch information
ctz committed Sep 5, 2024
1 parent 5be98ca commit 590829b
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ jobs:
with:
toolchain: ${{ matrix.rust }}

- name: Install valgrind
if: runner.os == 'Linux'
run: sudo apt-get update && sudo apt-get install -y valgrind

- name: cargo test (debug; default features)
run: cargo test
env:
Expand Down Expand Up @@ -166,3 +170,33 @@ jobs:
for target in $(cargo fuzz list) ; do
cargo fuzz run $target -- -max_total_time=10
done
valgrind:
name: Check side-channels on base64 decoder
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v4
with:
persist-credentials: false

- name: Install stable toolchain
uses: dtolnay/rust-toolchain@stable

- name: Install valgrind
if: runner.os == 'Linux'
run: sudo apt-get update && sudo apt-get install -y valgrind

- name: Build and run test
run: >
cargo test --all-features --lib
valgrind
--error-exitcode=99
--exit-on-first-error=yes
$(cargo test --all-features --no-run --message-format json |
jq --slurp --raw-output
'.[] | '
'select(.reason == "compiler-artifact") | '
'select(.target.name == "rustls_pki_types") | '
'select(.profile.test) | '
'.executable')
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ alloc = []
std = ["alloc"]
web = ["web-time"]

[target.'cfg(all(target_os = "linux", target_arch = "x86_64"))'.dev-dependencies]
crabgrind = "0.1"

[target.'cfg(all(target_family = "wasm", target_os = "unknown"))'.dependencies]
web-time = { version = "1", optional = true }

Expand Down
21 changes: 21 additions & 0 deletions src/base64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,27 @@ mod tests {
}
}

#[test]
fn codepoint_decode_secret_does_not_branch_or_index_on_secret_input() {
// this is using the same theory as <https://github.com/agl/ctgrind>
use crabgrind as cg;

if matches!(cg::run_mode(), cg::RunMode::Native) {
std::println!("SKIPPED: must be run under valgrind");
return;
}

let input = [b'a'];
cg::monitor_command(format!(
"make_memory undefined {:p} {}",
input.as_ptr(),
input.len()
))
.unwrap();

core::hint::black_box(CodePoint::decode_secret(input[0]));
}

#[track_caller]
fn decode(input: &[u8]) -> alloc::vec::Vec<u8> {
let length = decoded_length(input.len());
Expand Down

0 comments on commit 590829b

Please sign in to comment.