Skip to content

Commit 2d910fb

Browse files
committed
Refactored Docker files to eliminate redundant code and redundant definitions of Docker image versions.
1 parent eb819b2 commit 2d910fb

18 files changed

Lines changed: 157 additions & 234 deletions

.github/workflows/cargo-workflow.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ jobs:
128128
needs: [format, checks]
129129
strategy:
130130
matrix:
131-
branch: ["stable", "unstable", "snapshot"]
131+
branch: ["stable", "unstable", "bleeding-edge"]
132132
runs-on: ubuntu-${{ inputs.OS_VERS_MAX }}
133133
steps:
134134
- uses: actions/checkout@v7
@@ -137,7 +137,7 @@ jobs:
137137
toolchain: ${{ matrix.rust }}
138138
cache: false
139139
- run: |
140-
make docker.tests TSSFAPI_TEST_BRANCH=${{ matrix.branch }}
140+
make docker.tests RSTSS_BRANCH=${{ matrix.branch }}
141141
142142
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
143143
# Benchmarks

Cargo.lock

Lines changed: 17 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,14 @@ regex = "1.12.4"
2626

2727
[dev-dependencies]
2828
const-random = "0.1.18"
29+
constcat = "0.6.1"
2930
criterion = "=0.7.0"
3031
digest = "0.11.3"
3132
env_logger = "0.11.10"
3233
function_name = "0.3.0"
3334
hex = "0.4.3"
3435
memory-stats = "1.2.0"
35-
p256 = "=0.14.0-rc.12"
36+
p256 = "=0.14.0-rc.13"
3637
rand = "0.10.1"
3738
rand_chacha = "0.10.0"
3839
regex = "1.12.4"

benches/fapi_benchmark.rs

Lines changed: 91 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -4,136 +4,158 @@
44
* All rights reserved.
55
**********************************************************************************************/
66

7+
use constcat::concat;
78
use criterion::{Criterion, criterion_group, criterion_main};
89
use std::{
910
hint::black_box,
11+
num::NonZeroUsize,
1012
sync::atomic::{AtomicUsize, Ordering},
1113
time::Duration,
1214
};
1315
use tss2_fapi_rs::{BaseErrorCode, ErrorCode, FapiContext, KeyFlags};
1416

15-
const BASE_KEY_PATH: &str = "HS/SRK/bench";
16-
const SIGN_KEY_PATH: &str = "HS/SRK/sign_key";
17-
1817
// ==========================================================================
19-
// Bench #1: Create key
18+
// Benchmarks
2019
// ==========================================================================
2120

22-
fn bench_create_key(c: &mut Criterion) {
21+
const BASE_KEY_PATH: &str = "HS/SRK/my_bench";
22+
const SIGN_KEY_PATH: &str = concat!(BASE_KEY_PATH, "/sign_key");
23+
24+
/// Benchmark #1: Get random
25+
/// ------------------------
26+
/// This benchmark measures the performance of the [`FapiContext::get_random()`] function.
27+
fn bench_get_random(c: &mut Criterion) {
2328
// Create a new FAPI context
2429
let mut context = FapiContext::new().expect("Failed to create context!");
2530

26-
// Perform the provisioning, if it has not been done yet
27-
match context.provision(None, None, None) {
28-
Ok(_) => (),
29-
Err(ErrorCode::FapiError(BaseErrorCode::AlreadyProvisioned)) => (),
30-
Err(error) => panic!("Provisioning has failed: {:?}", error),
31-
}
31+
// Initialize TPM
32+
let manager = TpmManager::new(&mut context);
3233

33-
// Delete existing storage key
34-
match context.delete(BASE_KEY_PATH) {
35-
Ok(_) => (),
36-
Err(ErrorCode::FapiError(BaseErrorCode::BadPath)) => (),
37-
Err(ErrorCode::FapiError(BaseErrorCode::PathNotFound)) => (),
38-
Err(error) => panic!("Failed to delete: {:?}", error),
39-
}
34+
// Measurement!
35+
let counter: AtomicUsize = AtomicUsize::new(0usize);
36+
c.bench_function("get_random", |b| b.iter(|| _get_random(manager.0, black_box(counter.fetch_add(1usize, Ordering::Relaxed)))));
37+
}
38+
39+
fn _get_random(context: &mut FapiContext, _n: usize) {
40+
let length = unsafe { NonZeroUsize::new(32usize).unwrap_unchecked() };
41+
let _buffer = black_box(context.get_random(length).expect("Failed to get random data!"));
42+
}
43+
44+
/// Benchmark #2: Create key
45+
/// ------------------------
46+
/// This benchmark measures the performance of the [`FapiContext::create_key()`] function.
47+
fn bench_create_key(c: &mut Criterion) {
48+
// Create a new FAPI context
49+
let mut context = FapiContext::new().expect("Failed to create context!");
4050

41-
// Create storage key
42-
context.create_key(BASE_KEY_PATH, Some(&[KeyFlags::Decrypt, KeyFlags::Restricted, KeyFlags::NoDA]), None, None).expect("Failed to create parent key!");
51+
// Initialize TPM
52+
let manager = TpmManager::new(&mut context);
4353

44-
// Create the siging keys
54+
// Measurement!
4555
let counter: AtomicUsize = AtomicUsize::new(0usize);
46-
c.bench_function("create_key", |b| b.iter(|| _create_key(&mut context, black_box(counter.fetch_add(1usize, Ordering::Relaxed)))));
56+
c.bench_function("create_key", |b| b.iter(|| _create_key(manager.0, black_box(counter.fetch_add(1usize, Ordering::Relaxed)))));
4757
}
4858

4959
fn _create_key(context: &mut FapiContext, n: usize) {
5060
const MY_KEYFLAG: &[KeyFlags] = &[KeyFlags::Sign, KeyFlags::NoDA];
5161
context.create_key(&format!("{}/key_{}", BASE_KEY_PATH, n), Some(MY_KEYFLAG), None, None).expect("Failed to create key!")
5262
}
5363

54-
// ==========================================================================
55-
// Bench #2: Sign
56-
// ==========================================================================
57-
64+
/// Benchmark #3: Sign
65+
/// ------------------
66+
/// This benchmark measures the performance of the [`FapiContext::sign()`] function.
5867
fn bench_sign(c: &mut Criterion) {
5968
// Create a new FAPI context
6069
let mut context = FapiContext::new().expect("Failed to create context!");
6170

62-
// Perform the provisioning, if it has not been done yet
63-
match context.provision(None, None, None) {
64-
Ok(_) => (),
65-
Err(ErrorCode::FapiError(BaseErrorCode::AlreadyProvisioned)) => (),
66-
Err(error) => panic!("Provisioning has failed: {:?}", error),
67-
}
68-
69-
// Delete existing key
70-
match context.delete(SIGN_KEY_PATH) {
71-
Ok(_) => (),
72-
Err(ErrorCode::FapiError(BaseErrorCode::BadPath)) => (),
73-
Err(ErrorCode::FapiError(BaseErrorCode::PathNotFound)) => (),
74-
Err(error) => panic!("Failed to delete: {:?}", error),
75-
}
71+
// Initialize TPM
72+
let manager = TpmManager::new(&mut context);
7673

7774
// Create siging key
78-
context.create_key(SIGN_KEY_PATH, Some(&[KeyFlags::Sign, KeyFlags::NoDA]), None, None).expect("Failed to create sign key!");
75+
manager.0.create_key(SIGN_KEY_PATH, Some(&[KeyFlags::Sign, KeyFlags::NoDA]), None, None).expect("Failed to create sign key!");
7976

80-
// Sign message
77+
// Measurement!
8178
let counter: AtomicUsize = AtomicUsize::new(0usize);
82-
c.bench_function("sign", |b| b.iter(|| _sign(&mut context, black_box(counter.fetch_add(1usize, Ordering::Relaxed)))));
79+
c.bench_function("sign", |b| b.iter(|| _sign(manager.0, black_box(counter.fetch_add(1usize, Ordering::Relaxed)))));
8380
}
8481

8582
fn _sign(context: &mut FapiContext, n: usize) {
8683
let mut digest = [0u8; 32usize];
8784
digest[..size_of::<usize>()].copy_from_slice(&n.to_be_bytes());
88-
let _signature = context.sign(SIGN_KEY_PATH, None, &digest, false, false).expect("Failed to sign message!");
85+
let _signature = black_box(context.sign(SIGN_KEY_PATH, None, &digest, false, false).expect("Failed to sign message!"));
8986
}
9087

91-
// ==========================================================================
92-
// Bench #3: Verify
93-
// ==========================================================================
94-
88+
/// Benchmark #4: Verify signature
89+
/// ------------------------------
90+
/// This benchmark measures the performance of the [`FapiContext::verify_signature()`] function.
9591
fn bench_verify_signature(c: &mut Criterion) {
9692
// Create a new FAPI context
9793
let mut context = FapiContext::new().expect("Failed to create context!");
9894

99-
// Perform the provisioning, if it has not been done yet
100-
match context.provision(None, None, None) {
101-
Ok(_) => (),
102-
Err(ErrorCode::FapiError(BaseErrorCode::AlreadyProvisioned)) => (),
103-
Err(error) => panic!("Provisioning has failed: {:?}", error),
104-
}
105-
106-
// Delete existing key
107-
match context.delete(SIGN_KEY_PATH) {
108-
Ok(_) => (),
109-
Err(ErrorCode::FapiError(BaseErrorCode::BadPath)) => (),
110-
Err(ErrorCode::FapiError(BaseErrorCode::PathNotFound)) => (),
111-
Err(error) => panic!("Failed to delete: {:?}", error),
112-
}
95+
// Initialize TPM
96+
let manager = TpmManager::new(&mut context);
11397

11498
// Create siging key
115-
context.create_key(SIGN_KEY_PATH, Some(&[KeyFlags::Sign, KeyFlags::NoDA]), None, None).expect("Failed to create sign key!");
99+
manager.0.create_key(SIGN_KEY_PATH, Some(&[KeyFlags::Sign, KeyFlags::NoDA]), None, None).expect("Failed to create sign key!");
116100

117101
// Sign message
118102
let digest = [0u8; 32usize];
119-
let signature = context.sign(SIGN_KEY_PATH, None, &digest, false, false).expect("Failed to sign message!");
103+
let signature = manager.0.sign(SIGN_KEY_PATH, None, &digest, false, false).expect("Failed to sign message!");
120104

121-
// Verify signature
122-
c.bench_function("verify_signature", |b| b.iter(|| _verify_signature(&mut context, black_box(&digest), &black_box(&signature).sign_value)));
105+
// Measurement!
106+
c.bench_function("verify_signature", |b| b.iter(|| _verify_signature(manager.0, black_box(&digest), &black_box(&signature).sign_value)));
123107
}
124108

125109
fn _verify_signature(context: &mut FapiContext, digest: &[u8], signature: &[u8]) {
126-
let result = context.verify_signature(SIGN_KEY_PATH, digest, signature).expect("Failed to verify signature!");
110+
let result = black_box(context.verify_signature(SIGN_KEY_PATH, digest, signature).expect("Failed to verify signature!"));
127111
assert!(result);
128112
}
129113

114+
// ==========================================================================
115+
// Utility functions
116+
// ==========================================================================
117+
118+
struct TpmManager<'a>(pub &'a mut FapiContext);
119+
120+
impl<'a> TpmManager<'a> {
121+
pub fn new(context: &'a mut FapiContext) -> Self {
122+
// Perform the provisioning, if it has not been done yet
123+
match context.provision(None, None, None) {
124+
Ok(_) => (),
125+
Err(ErrorCode::FapiError(BaseErrorCode::AlreadyProvisioned)) => (),
126+
Err(error) => panic!("Provisioning has failed: {:?}", error),
127+
}
128+
129+
// Delete existing storage key, if it already exists
130+
Self::clean_up(context).expect("Failed to delete the existing key!");
131+
132+
// Create our storage key
133+
context.create_key(BASE_KEY_PATH, Some(&[KeyFlags::Decrypt, KeyFlags::Restricted, KeyFlags::NoDA]), None, None).expect("Failed to create parent key!");
134+
Self(context)
135+
}
136+
137+
fn clean_up(context: &mut FapiContext) -> Result<(), ErrorCode> {
138+
match context.delete(BASE_KEY_PATH) {
139+
Ok(_) => Ok(()),
140+
Err(ErrorCode::FapiError(BaseErrorCode::BadPath | BaseErrorCode::PathNotFound | BaseErrorCode::KeyNotFound)) => Ok(()),
141+
Err(error) => Err(error),
142+
}
143+
}
144+
}
145+
146+
impl<'a> Drop for TpmManager<'a> {
147+
fn drop(&mut self) {
148+
_ = Self::clean_up(self.0);
149+
}
150+
}
151+
130152
// ==========================================================================
131153
// Main
132154
// ==========================================================================
133155

134156
fn create_config() -> Criterion {
135-
Criterion::default().warm_up_time(Duration::from_secs(15)).measurement_time(Duration::from_secs(30)).noise_threshold(0.1).without_plots()
157+
Criterion::default().warm_up_time(Duration::from_secs(25)).measurement_time(Duration::from_secs(120)).noise_threshold(0.1).without_plots()
136158
}
137159

138-
criterion_group!(name = benches; config = create_config(); targets = bench_create_key, bench_sign, bench_verify_signature);
160+
criterion_group!(name = benches; config = create_config(); targets = bench_get_random, bench_create_key, bench_sign, bench_verify_signature);
139161
criterion_main!(benches);

tools/docker/build/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
include rstss.mk
2+
13
.PHONY: all run down
24

35
all: down run

tools/docker/build/build.Dockerfile

Lines changed: 0 additions & 8 deletions
This file was deleted.

tools/docker/build/docker-compose.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ services:
44
build:
55
build:
66
context: ./
7-
dockerfile: build.Dockerfile
7+
dockerfile: rstss.Dockerfile
88
args:
9-
BASE_VERSION: r29@sha256:c33d92b8bd58a5397cf6ca7af960a68658256209a3f05c7be89bd55fdf007b45
9+
IMAGE_VERSION_RSTSS: ${IMAGE_VERSION_RSTSS:?}
1010
volumes:
1111
- ../../../:/var/opt/rust/src:ro
1212
- out:/var/tmp/rust:rw
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Define default Rust-TSS2 base image version
2+
ARG IMAGE_VERSION_RSTSS=UNDEFINED
3+
4+
# Docker file for build-env
5+
FROM danieltrick/rust-tss2-docker@${IMAGE_VERSION_RSTSS}
6+
7+
# Default command
8+
CMD ["rebuild", "--release"]

tools/docker/build/rstss.mk

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
ifeq ($(filter-out stable,$(RSTSS_BRANCH)),)
2+
export IMAGE_VERSION_RSTSS := sha256:c33d92b8bd58a5397cf6ca7af960a68658256209a3f05c7be89bd55fdf007b45
3+
else ifeq ($(RSTSS_BRANCH),unstable)
4+
export IMAGE_VERSION_RSTSS := sha256:6e7f3de9c3b0298192a4db108d955cd203b360fd1fb40b5f6940c9e3ecf7cc0b
5+
else ifeq ($(RSTSS_BRANCH),bleeding-edge)
6+
export IMAGE_VERSION_RSTSS := sha256:5e02daff85e7678c6715db7b9c07e96d3117bf04e8ec7eaba4a8958181035017
7+
else
8+
$(error Unsupport RSTSS_BRANCH branch "$(RSTSS_BRANCH)" specified!)
9+
endif

tools/docker/swtpm/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
include swtpm.mk
2+
13
.PHONY: all up down
24

35
all: down up

0 commit comments

Comments
 (0)