Skip to content

Commit bcf56a3

Browse files
authored
Support FIPS build w/ CPU Jitter (#657)
* Support FIPS build w/ CPU Jitter Entropy * Script verifying CPU Jitter Entropy
1 parent 34ba98f commit bcf56a3

File tree

6 files changed

+94
-10
lines changed

6 files changed

+94
-10
lines changed

.github/workflows/fips.yml

+19
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,22 @@ jobs:
8686
- name: Run cargo test
8787
working-directory: ./aws-lc-rs
8888
run: cargo test ${{ matrix.args }}
89+
cpu-jitter-entropy-test:
90+
if: github.repository_owner == 'aws'
91+
name: CPU Jitter Entropy Tests
92+
runs-on: ubuntu-latest
93+
env:
94+
AWS_LC_FIPS_SYS_CPU_JITTER_ENTROPY: 1
95+
steps:
96+
- uses: actions/checkout@v3
97+
with:
98+
submodules: 'recursive'
99+
- uses: dtolnay/rust-toolchain@master
100+
id: toolchain
101+
with:
102+
toolchain: nightly
103+
- uses: actions/setup-go@v4
104+
with:
105+
go-version: '>=1.18'
106+
- name: Run assert script
107+
run: ./scripts/tests/assert_cpu_jitter_entropy.rs

aws-lc-fips-sys/builder/cmake_builder.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

44
use crate::OutputLib::{Crypto, RustWrapper, Ssl};
55
use crate::{
6-
cargo_env, emit_warning, execute_command, is_no_asm, option_env, target, target_arch,
7-
target_env, target_family, target_os, target_underscored, target_vendor, OutputLibType,
8-
TestCommandResult,
6+
cargo_env, emit_rustc_cfg, emit_warning, execute_command, is_cpu_jitter_entropy, is_no_asm,
7+
option_env, target, target_arch, target_env, target_family, target_os, target_underscored,
8+
target_vendor, OutputLibType, TestCommandResult,
99
};
1010
use std::collections::HashMap;
1111
use std::env;
@@ -106,6 +106,11 @@ impl CmakeBuilder {
106106
cmake_cfg.define("BUILD_SHARED_LIBS", "0");
107107
}
108108

109+
if is_cpu_jitter_entropy() {
110+
cmake_cfg.define("ENABLE_FIPS_ENTROPY_CPU_JITTER", "ON");
111+
emit_rustc_cfg("cpu_jitter_entropy");
112+
}
113+
109114
let cc_build = cc::Build::new();
110115
let opt_level = cargo_env("OPT_LEVEL");
111116
if opt_level.ne("0") {

aws-lc-fips-sys/builder/main.rs

+8
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ static mut AWS_LC_FIPS_SYS_NO_PREFIX: bool = false;
364364
static mut AWS_LC_FIPS_SYS_PREGENERATING_BINDINGS: bool = false;
365365
static mut AWS_LC_FIPS_SYS_EXTERNAL_BINDGEN: bool = false;
366366
static mut AWS_LC_FIPS_SYS_NO_ASM: bool = false;
367+
static mut AWS_LC_FIPS_SYS_CPU_JITTER_ENTROPY: bool = false;
367368
fn initialize() {
368369
unsafe {
369370
AWS_LC_FIPS_SYS_NO_PREFIX = env_var_to_bool("AWS_LC_FIPS_SYS_NO_PREFIX").unwrap_or(false);
@@ -372,6 +373,8 @@ fn initialize() {
372373
AWS_LC_FIPS_SYS_EXTERNAL_BINDGEN =
373374
env_var_to_bool("AWS_LC_FIPS_SYS_EXTERNAL_BINDGEN").unwrap_or(false);
374375
AWS_LC_FIPS_SYS_NO_ASM = env_var_to_bool("AWS_LC_FIPS_SYS_NO_ASM").unwrap_or(false);
376+
AWS_LC_FIPS_SYS_CPU_JITTER_ENTROPY =
377+
env_var_to_bool("AWS_LC_FIPS_SYS_CPU_JITTER_ENTROPY").unwrap_or(false);
375378
}
376379

377380
// The conditions below should prevent use of pregenerated bindings in all cases where the
@@ -435,6 +438,10 @@ fn is_no_asm() -> bool {
435438
unsafe { AWS_LC_FIPS_SYS_NO_ASM }
436439
}
437440

441+
fn is_cpu_jitter_entropy() -> bool {
442+
unsafe { AWS_LC_FIPS_SYS_CPU_JITTER_ENTROPY }
443+
}
444+
438445
fn has_bindgen_feature() -> bool {
439446
cfg!(feature = "bindgen")
440447
}
@@ -449,6 +456,7 @@ fn prepare_cargo_cfg() {
449456
println!("cargo:rustc-check-cfg=cfg(aarch64_apple_darwin)");
450457
println!("cargo:rustc-check-cfg=cfg(aarch64_unknown_linux_gnu)");
451458
println!("cargo:rustc-check-cfg=cfg(aarch64_unknown_linux_musl)");
459+
println!("cargo:rustc-check-cfg=cfg(cpu_jitter_entropy)");
452460
println!("cargo:rustc-check-cfg=cfg(i686_unknown_linux_gnu)");
453461
println!("cargo:rustc-check-cfg=cfg(use_bindgen_generated)");
454462
println!("cargo:rustc-check-cfg=cfg(x86_64_apple_darwin)");

aws-lc-fips-sys/src/lib.rs

+6
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ pub fn ERR_GET_FUNC(packed_error: u32) -> i32 {
8484
unsafe { ERR_GET_FUNC_RUST(packed_error) }
8585
}
8686

87+
#[allow(non_snake_case)]
88+
#[must_use]
89+
pub fn CFG_CPU_JITTER_ENTROPY() -> bool {
90+
cfg!(cpu_jitter_entropy)
91+
}
92+
8793
#[allow(non_snake_case, clippy::not_unsafe_ptr_arg_deref)]
8894
pub fn BIO_get_mem_data(b: *mut BIO, pp: *mut *mut c_char) -> c_long {
8995
unsafe { BIO_ctrl(b, BIO_CTRL_INFO, 0, pp.cast::<c_void>()) }

aws-lc-rs/src/lib.rs

+40-7
Original file line numberDiff line numberDiff line change
@@ -233,14 +233,42 @@ pub fn fips_mode() {
233233
/// Indicates whether the underlying implementation is FIPS.
234234
///
235235
/// # Errors
236-
/// Return an error if the underlying implementation is not FIPS, otherwise ok
236+
/// Return an error if the underlying implementation is not FIPS, otherwise Ok.
237237
pub fn try_fips_mode() -> Result<(), &'static str> {
238238
init();
239-
unsafe {
240-
match FIPS_mode() {
241-
1 => Ok(()),
242-
_ => Err("FIPS mode not enabled!"),
243-
}
239+
match unsafe { FIPS_mode() } {
240+
1 => Ok(()),
241+
_ => Err("FIPS mode not enabled!"),
242+
}
243+
}
244+
245+
#[cfg(feature = "fips")]
246+
/// Panics if the underlying implementation is not using CPU jitter entropy, otherwise it returns.
247+
///
248+
/// # Panics
249+
/// Panics if the underlying implementation is not using CPU jitter entropy.
250+
pub fn fips_cpu_jitter_entropy() {
251+
try_fips_cpu_jitter_entropy().unwrap();
252+
}
253+
254+
/// Indicates whether the underlying implementation is FIPS.
255+
///
256+
/// # Errors
257+
/// Return an error if the underlying implementation is not using CPU jitter entropy, otherwise Ok.
258+
pub fn try_fips_cpu_jitter_entropy() -> Result<(), &'static str> {
259+
init();
260+
// TODO: Delete once FIPS_is_entropy_cpu_jitter() available on FIPS branch
261+
// https://github.com/aws/aws-lc/pull/2088
262+
#[cfg(feature = "fips")]
263+
if aws_lc::CFG_CPU_JITTER_ENTROPY() {
264+
Ok(())
265+
} else {
266+
Err("FIPS CPU Jitter Entropy not enabled!")
267+
}
268+
#[cfg(not(feature = "fips"))]
269+
match unsafe { aws_lc::FIPS_is_entropy_cpu_jitter() } {
270+
1 => Ok(()),
271+
_ => Err("FIPS CPU Jitter Entropy not enabled!"),
244272
}
245273
}
246274

@@ -292,12 +320,17 @@ mod tests {
292320
#[test]
293321
fn test_fips() {
294322
assert!({ crate::try_fips_mode().is_err() });
323+
assert!({ crate::try_fips_cpu_jitter_entropy().is_err() });
295324
}
296325

297326
#[test]
298327
// FIPS mode is disabled for an ASAN build
299-
#[cfg(all(feature = "fips", not(feature = "asan")))]
328+
#[cfg(feature = "fips")]
300329
fn test_fips() {
330+
#[cfg(not(feature = "asan"))]
301331
crate::fips_mode();
332+
if aws_lc::CFG_CPU_JITTER_ENTROPY() {
333+
crate::fips_cpu_jitter_entropy();
334+
}
302335
}
303336
}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env -S cargo +nightly -Zscript
2+
---cargo
3+
[dependencies]
4+
aws-lc-rs = { version = "1", path = "../../aws-lc-rs", default-features = false, features = ["fips"] }
5+
---
6+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
7+
// SPDX-License-Identifier: Apache-2.0 OR ISC
8+
9+
fn main() {
10+
println!("Checking for CPU Jitter Entropy");
11+
aws_lc_rs::fips_cpu_jitter_entropy();
12+
println!("CPU Jitter Entropy Success");
13+
}

0 commit comments

Comments
 (0)