Skip to content

Commit 5a0f772

Browse files
Merge pull request #510 from parallaxsecond/tg/random-fix-main
Get random authvalues locally instead of from the TPM
2 parents 7899a72 + e8220a0 commit 5a0f772

14 files changed

+148
-91
lines changed

SECURITY.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Security policy
2+
3+
Security is of paramount importance to the tss-esapi project. We do all we can to identify and fix
4+
issues, however some problems might slip through the cracks. Any efforts towards responsible
5+
disclosure of security problems are greatly appreciated and your contributions will be acknowledged.
6+
7+
## Our disclosure policy
8+
9+
All security vulnerabilities affecting the tss-esapi project - including those reported using the
10+
steps highlighted below, those discovered during routine testing, and those found in our dependency
11+
tree either through `cargo-audit` or otherwise - will receive
12+
[security advisories](https://github.com/parallaxsecond/rust-tss-esapi/security) in a timely
13+
manner. The advisories should include sufficient information about the cause, effect, and possible
14+
mitigations for the vulnerability. If any information is missing, or you would like to raise a
15+
question about the advisories, please open an issue in
16+
[our repo](https://github.com/parallaxsecond/rust-tss-esapi).
17+
18+
Efforts to mitigate for the reported vulnerabilities will be tracked using GitHub issues linked to
19+
the corresponding advisories.
20+
21+
## Reporting a vulnerability
22+
23+
To report a vulnerability, please send an email to
24+
[cncf-parsec-maintainers@lists.cncf.io](mailto:cncf-parsec-maintainers@lists.cncf.io). We will
25+
promptly reply to your report and we will strive to keep you in the loop as we try to reach a
26+
resolution.
27+
28+
# Security considerations for the use of the software
29+
30+
The authvalue provided to the TPM to perform certain operations like creating Primary Keys is
31+
currently randomly generated by [getrandom](https://crates.io/crates/getrandom), which assumes
32+
"that the system always provides high-quality cryptographically secure random data, ideally backed
33+
by hardware entropy sources."
34+
35+
The user of this software should take this into consideration when setting up their system and using
36+
this software.

tss-esapi/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ cfg-if = "1.0.0"
3434
strum = { version = "0.25.0", optional = true }
3535
strum_macros = { version = "0.25.0", optional = true }
3636
paste = "1.0.14"
37+
getrandom = "0.2.11"
3738

3839
[dev-dependencies]
3940
env_logger = "0.9.0"

tss-esapi/src/abstraction/transient/mod.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ impl TransientKeyContext {
137137
///
138138
/// # Errors
139139
/// * if the authentication size is larger than 32 a `WrongParamSize` wrapper error is returned
140+
/// * if there is an error when obtaining random numbers from the local machine
140141
pub fn create_key(
141142
&mut self,
142143
key_params: KeyParams,
@@ -147,8 +148,12 @@ impl TransientKeyContext {
147148
}
148149
let key_auth = if auth_size > 0 {
149150
self.set_session_attrs()?;
150-
let random_bytes = self.context.get_random(auth_size)?;
151-
Some(Auth::from_bytes(random_bytes.as_bytes())?)
151+
let mut random_bytes = vec![0u8; auth_size];
152+
getrandom::getrandom(&mut random_bytes).map_err(|_| {
153+
log::error!("Failed to obtain a random authvalue for key creation");
154+
Error::WrapperError(ErrorKind::InternalError)
155+
})?;
156+
Some(Auth::from_bytes(random_bytes.as_slice())?)
152157
} else {
153158
None
154159
};
@@ -636,7 +641,7 @@ impl TransientKeyContextBuilder {
636641
/// Bootstrap the TransientKeyContext.
637642
///
638643
/// The root key is created as a primary key in the provided hierarchy and thus authentication is
639-
/// needed for said hierarchy. The authentication value for the key is generated by the TPM itself,
644+
/// needed for said hierarchy. The authentication value for the key is generated locally in the machine,
640645
/// with a configurable length, and never exposed outside the context.
641646
///
642647
/// # Warning
@@ -649,9 +654,9 @@ impl TransientKeyContextBuilder {
649654
/// * `root_key_auth_size` must be at most 32
650655
///
651656
/// # Errors
652-
/// * errors are returned if any method calls return an error: `Context::get_random`,
653-
/// `Context::start_auth_session`, `Context::create_primary`, `Context::flush_context`,
654-
/// `Context::set_handle_auth`
657+
/// * errors are returned if any method calls return an error: `Context::start_auth_session`
658+
/// `Context::create_primary`, `Context::flush_context`, `Context::set_handle_auth`
659+
/// or if an internal error occurs when getting random numbers from the local machine
655660
/// * if the root key authentication size is given greater than 32 or if the root key size is
656661
/// not 1024, 2048, 3072 or 4096, a `InvalidParam` wrapper error is returned
657662
pub fn build(mut self) -> Result<TransientKeyContext> {
@@ -664,8 +669,12 @@ impl TransientKeyContextBuilder {
664669
let mut context = Context::new(self.tcti_name_conf)?;
665670

666671
let root_key_auth = if self.root_key_auth_size > 0 {
667-
let random = context.get_random(self.root_key_auth_size)?;
668-
Some(Auth::from_bytes(random.as_bytes())?)
672+
let mut random = vec![0u8; self.root_key_auth_size];
673+
getrandom::getrandom(&mut random).map_err(|_| {
674+
log::error!("Failed to obtain a random value for root key authentication");
675+
Error::WrapperError(ErrorKind::InternalError)
676+
})?;
677+
Some(Auth::from_bytes(random.as_slice())?)
669678
} else {
670679
None
671680
};

tss-esapi/src/context/tpm_commands/asymmetric_primitives.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,9 @@ impl Context {
127127
/// # context.tr_sess_set_attributes(session, session_attributes, session_attributes_mask)
128128
/// # .expect("Failed to set attributes on session");
129129
/// # context.set_sessions((Some(session), None, None));
130-
/// # let random_digest = context.get_random(16).unwrap();
131-
/// # let key_auth = Auth::from_bytes(random_digest.as_bytes()).unwrap();
130+
/// # let mut random_digest = vec![0u8; 16];
131+
/// # getrandom::getrandom(&mut random_digest).unwrap();
132+
/// # let key_auth = Auth::from_bytes(random_digest.as_slice()).unwrap();
132133
/// #
133134
/// // Create a key suitable for ECDH key generation
134135
/// let ecc_parms = PublicEccParametersBuilder::new()
@@ -262,8 +263,9 @@ impl Context {
262263
/// # context.tr_sess_set_attributes(session, session_attributes, session_attributes_mask)
263264
/// # .expect("Failed to set attributes on session");
264265
/// # context.set_sessions((Some(session), None, None));
265-
/// # let random_digest = context.get_random(16).unwrap();
266-
/// # let key_auth = Auth::from_bytes(random_digest.as_bytes()).unwrap();
266+
/// # let mut random_digest = vec![0u8; 16];
267+
/// # getrandom::getrandom(&mut random_digest).unwrap();
268+
/// # let key_auth = Auth::from_bytes(random_digest.as_slice()).unwrap();
267269
/// #
268270
/// // Create a key suitable for ECDH key generation
269271
/// let ecc_parms = PublicEccParametersBuilder::new()

tss-esapi/src/context/tpm_commands/context_management.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,9 @@ impl Context {
112112
///
113113
/// // Execute context methods using the session
114114
/// context.execute_with_session(Some(session), |ctx| {
115-
/// let random_digest = ctx.get_random(16)
116-
/// .expect("Call to get_random failed");
117-
/// let key_auth = Auth::from_bytes(random_digest.as_bytes())
118-
/// .expect("Failed to create Auth");
115+
/// let mut random_digest = vec![0u8; 16];
116+
/// getrandom::getrandom(&mut random_digest).expect("Call to getrandom failed");
117+
/// let key_auth = Auth::from_bytes(random_digest.as_slice()).expect("Failed to create Auth");
119118
/// let key_handle = ctx
120119
/// .create_primary(
121120
/// Hierarchy::Owner,

tss-esapi/src/context/tpm_commands/symmetric_primitives.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,11 @@ impl Context {
5656
/// # .tr_set_auth(tss_esapi::interface_types::reserved_handles::Hierarchy::Owner.into(), Auth::default())
5757
/// # .expect("Failed to set auth to empty for owner");
5858
/// # // Create primary key auth
59-
/// # let primary_key_auth = Auth::try_from(
60-
/// # context
61-
/// # .get_random(16)
62-
/// # .expect("get_rand call failed")
63-
/// # .as_bytes()
64-
/// # .to_vec(),
59+
/// # let mut random_digest = vec![0u8; 16];
60+
/// # getrandom::getrandom(&mut random_digest).expect("get_rand call failed");
61+
/// # let primary_key_auth = Auth::from_bytes(
62+
/// # random_digest
63+
/// # .as_slice()
6564
/// # )
6665
/// # .expect("Failed to create primary key auth");
6766
/// # // Create primary key
@@ -103,12 +102,11 @@ impl Context {
103102
/// # .build()
104103
/// # .expect("Failed to create public for symmetric key public");
105104
/// # // Create auth for the symmetric key
106-
/// # let symmetric_key_auth = Auth::try_from(
107-
/// # context
108-
/// # .get_random(16)
109-
/// # .expect("get_rand call failed")
110-
/// # .as_bytes()
111-
/// # .to_vec(),
105+
/// # let mut random_digest = vec![0u8; 16];
106+
/// # getrandom::getrandom(&mut random_digest).expect("get_rand call failed");
107+
/// # let symmetric_key_auth = Auth::from_bytes(
108+
/// # random_digest
109+
/// # .as_slice()
112110
/// # )
113111
/// # .expect("Failed to create symmetric key auth");
114112
/// # // Create symmetric key data

tss-esapi/tests/integration_tests/abstraction_tests/transient_key_context_tests.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -502,8 +502,9 @@ fn ctx_migration_test() {
502502
// Create two key contexts using `Context`, one for an RSA keypair,
503503
// one for just the public part of the key
504504
let mut basic_ctx = crate::common::create_ctx_with_session();
505-
let random_digest = basic_ctx.get_random(16).unwrap();
506-
let key_auth = Auth::from_bytes(random_digest.as_bytes()).unwrap();
505+
let mut random_digest = vec![0u8; 16];
506+
getrandom::getrandom(&mut random_digest).unwrap();
507+
let key_auth = Auth::from_bytes(random_digest.as_slice()).unwrap();
507508
let prim_key_handle = basic_ctx
508509
.create_primary(
509510
Hierarchy::Owner,

tss-esapi/tests/integration_tests/context_tests/tpm_commands/asymmetric_primitives_tests.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ mod test_rsa_encrypt_decrypt {
1919
#[test]
2020
fn test_encrypt_decrypt() {
2121
let mut context = create_ctx_with_session();
22-
let random_digest = context.get_random(16).unwrap();
23-
let key_auth = Auth::from_bytes(random_digest.as_bytes()).unwrap();
22+
let mut random_digest = vec![0u8; 16];
23+
getrandom::getrandom(&mut random_digest).unwrap();
24+
let key_auth = Auth::from_bytes(random_digest.as_slice()).unwrap();
2425

2526
let key_handle = context
2627
.create_primary(
@@ -59,8 +60,9 @@ mod test_rsa_encrypt_decrypt {
5960
#[test]
6061
fn test_ecdh() {
6162
let mut context = create_ctx_with_session();
62-
let random_digest = context.get_random(16).unwrap();
63-
let key_auth = Auth::from_bytes(random_digest.as_bytes()).unwrap();
63+
let mut random_digest = vec![0u8; 16];
64+
getrandom::getrandom(&mut random_digest).unwrap();
65+
let key_auth = Auth::from_bytes(random_digest.as_slice()).unwrap();
6466

6567
let ecc_parms = PublicEccParametersBuilder::new()
6668
.with_ecc_scheme(EccScheme::EcDh(HashScheme::new(HashingAlgorithm::Sha256)))

tss-esapi/tests/integration_tests/context_tests/tpm_commands/context_management_tests.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ mod test_ctx_save {
77
#[test]
88
fn test_ctx_save() {
99
let mut context = create_ctx_with_session();
10-
let random_digest = context.get_random(16).unwrap();
11-
let key_auth = Auth::from_bytes(random_digest.as_bytes()).unwrap();
10+
let mut random_digest = vec![0u8; 16];
11+
getrandom::getrandom(&mut random_digest).unwrap();
12+
let key_auth = Auth::from_bytes(random_digest.as_slice()).unwrap();
1213

1314
let key_handle = context
1415
.create_primary(
@@ -27,8 +28,9 @@ mod test_ctx_save {
2728
#[test]
2829
fn test_ctx_save_leaf() {
2930
let mut context = create_ctx_with_session();
30-
let random_digest = context.get_random(16).unwrap();
31-
let key_auth = Auth::from_bytes(random_digest.as_bytes()).unwrap();
31+
let mut random_digest = vec![0u8; 16];
32+
getrandom::getrandom(&mut random_digest).unwrap();
33+
let key_auth = Auth::from_bytes(random_digest.as_slice()).unwrap();
3234

3335
let prim_key_handle = context
3436
.create_primary(
@@ -70,13 +72,14 @@ mod test_ctx_load {
7072
#[test]
7173
fn test_ctx_load() {
7274
let mut context = create_ctx_with_session();
73-
let key_auth = context.get_random(16).unwrap();
75+
let mut random_digest = vec![0u8; 16];
76+
getrandom::getrandom(&mut random_digest).unwrap();
7477

7578
let prim_key_handle = context
7679
.create_primary(
7780
Hierarchy::Owner,
7881
decryption_key_pub(),
79-
Some(Auth::from_bytes(key_auth.as_bytes()).unwrap()),
82+
Some(Auth::from_bytes(random_digest.as_slice()).unwrap()),
8083
None,
8184
None,
8285
None,
@@ -88,7 +91,7 @@ mod test_ctx_load {
8891
.create(
8992
prim_key_handle,
9093
signing_key_pub(),
91-
Some(Auth::from_bytes(key_auth.as_bytes()).unwrap()),
94+
Some(Auth::from_bytes(random_digest.as_slice()).unwrap()),
9295
None,
9396
None,
9497
None,
@@ -112,8 +115,9 @@ mod test_flush_context {
112115
#[test]
113116
fn test_flush_ctx() {
114117
let mut context = create_ctx_with_session();
115-
let random_digest = context.get_random(16).unwrap();
116-
let key_auth = Auth::from_bytes(random_digest.as_bytes()).unwrap();
118+
let mut random_digest = vec![0u8; 16];
119+
getrandom::getrandom(&mut random_digest).unwrap();
120+
let key_auth = Auth::from_bytes(random_digest.as_slice()).unwrap();
117121

118122
let key_handle = context
119123
.create_primary(
@@ -133,8 +137,9 @@ mod test_flush_context {
133137
#[test]
134138
fn test_flush_parent_ctx() {
135139
let mut context = create_ctx_with_session();
136-
let random_digest = context.get_random(16).unwrap();
137-
let key_auth = Auth::from_bytes(random_digest.as_bytes()).unwrap();
140+
let mut random_digest = vec![0u8; 16];
141+
getrandom::getrandom(&mut random_digest).unwrap();
142+
let key_auth = Auth::from_bytes(random_digest.as_slice()).unwrap();
138143

139144
let prim_key_handle = context
140145
.create_primary(

tss-esapi/tests/integration_tests/context_tests/tpm_commands/enhanced_authorization_ea_commands_tests.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -536,8 +536,9 @@ mod test_policy_authorize {
536536
#[test]
537537
fn test_policy_authorize() {
538538
let mut context = create_ctx_with_session();
539-
let random_digest = context.get_random(16).unwrap();
540-
let key_auth = Auth::from_bytes(random_digest.as_bytes()).unwrap();
539+
let mut random_digest = vec![0u8; 16];
540+
getrandom::getrandom(&mut random_digest).unwrap();
541+
let key_auth = Auth::from_bytes(random_digest.as_slice()).unwrap();
541542

542543
let key_handle = context
543544
.create_primary(

0 commit comments

Comments
 (0)