Skip to content

Commit b828db2

Browse files
committed
Updated some dependencies to the latest version + implemented workaround to make test code compile with newer version of the "rand" package.
1 parent 5a40bb4 commit b828db2

File tree

7 files changed

+132
-33
lines changed

7 files changed

+132
-33
lines changed

Cargo.lock

Lines changed: 82 additions & 18 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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ function_name = "0.3.0"
3131
hex = "0.4.3"
3232
memory-stats = "1.2.0"
3333
p256 = "0.13.2"
34-
rand = "0.8.5"
35-
rand_chacha = "0.3.1"
34+
rand = "0.9.1"
35+
rand_chacha = "0.9.0"
3636
regex = "1.11.1"
3737
rsa = "0.9.8"
3838
serial_test = "3.2.0"

src/context.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ type QuoteResult = (JsonValue, Vec<u8>, Option<JsonValue>, Option<String>);
6767
///
6868
/// ### Thread Safety
6969
///
70-
/// In general, the FAPI is considered “thread-safe”, but individual instances of `FapiContext` are **not**.
70+
/// In general, the FAPI is considered “thread-safe”, but individual instances of `FapiContext` are **not** &#128680;
7171
///
72-
/// This means that an application may safely access the FAPI from multiple *concurrent* threads, provided that each of these threads uses its own separate `FapiContext` instance. Sharing the same `FapiContext` instance between *concurrent* threads is also possible, but this requires an explicit synchronization to ensure that *at most* **one** thread at a time will access the "shared" instance! Specifically, `FapiContext` implements the [`Send`] trait, so it may be transferred to another thread, but it does **not** implement the [`Sync`] trait. However, you can wrap the context in an `Arc<Mutex<T>>` in order to share it safely between multiple threads.
72+
/// This means that an application may safely access the FAPI from multiple *concurrent* threads <u>without</u> any synchronization (locking) at the application level, provided that each thread uses its own separate `FapiContext`. Meanwhile, sharing the same `FapiContext` between *concurrent* threads requires an explicit synchronization in the application code to ensure that *at most* **one** thread at a time will access the "shared" context! Consequently, `FapiContext` implements the [`Send`] trait, but it does **not** implement the [`Sync`] trait. You can wrap the context in an `Arc<Mutex<T>>` in order to share it safely between multiple threads.
7373
///
74-
/// By default, the `tss2-fapi-rs` library does **not** serialize FAPI calls from *concurrent* application threads, except for a few “critical” functions. The optional **`full_locking`** feature can be enabled to enforce the serialization of *all* FAPI calls.
74+
/// By default, the `tss2-fapi-rs` library does **not** serialize FAPI calls from *concurrent* contexts (threads), except for a few “critical” functions that need to be serialized. The optional **`full_locking`** feature can be enabled to enforce full serialization of *all* FAPI calls. Be aware, though, that the serialization of the FAPI calls is implemented *per-process*, **not** globally.
7575
///
7676
/// ### FAPI Library
7777
///
@@ -252,6 +252,8 @@ impl FapiContext {
252252

253253
/// Provisions a TSS with its TPM. This includes the setting of important passwords and policy settings as well as the readout of the EK and its certificate and the initialization of the system-wide keystore.
254254
///
255+
/// Invocations of this function are serialized between concurrent FAPI contexts (threads) by default.
256+
///
255257
/// *See also:* [`Fapi_Provision()`](https://tpm2-tss.readthedocs.io/en/latest/group___fapi___provision.html)
256258
pub fn provision(&mut self, auth_eh: Option<&str>, auth_sh: Option<&str>, auth_lo: Option<&str>) -> Result<(), ErrorCode> {
257259
let cstr_eh = CStringHolder::try_from(auth_eh)?;

tests/08_nv_test.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use common::{
1313
};
1414
use function_name::named;
1515
use log::{debug, trace};
16-
use rand::{thread_rng, RngCore, SeedableRng};
16+
use rand::{rng, RngCore, SeedableRng};
1717
use rand_chacha::ChaChaRng;
1818
use serial_test::serial;
1919
use tss2_fapi_rs::{FapiContext, NvFlags};
@@ -57,7 +57,7 @@ fn test_nv_write() {
5757
}
5858

5959
// Generate random data
60-
thread_rng().fill_bytes(&mut data[..]);
60+
rng().fill_bytes(&mut data[..]);
6161

6262
// Write data to NV index
6363
match context.nv_write(nv_path, &data[..]) {
@@ -66,7 +66,7 @@ fn test_nv_write() {
6666
}
6767

6868
// Generate random number
69-
let number = thread_rng().next_u64();
69+
let number = rng().next_u64();
7070

7171
// Write number to NV index
7272
match context.nv_write_u64(nv_path, number) {
@@ -103,7 +103,7 @@ fn test_nv_read() {
103103
}
104104

105105
// Generate random data
106-
thread_rng().fill_bytes(&mut data[..]);
106+
rng().fill_bytes(&mut data[..]);
107107

108108
// Write data to NV index
109109
match context.nv_write(nv_path, &data[..]) {

tests/common/crypto.rs

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@ use p256::{
1010
pkcs8::DecodePrivateKey,
1111
PublicKey as EccPublicKey, SecretKey as EccPrivateKey,
1212
};
13-
use rand::thread_rng;
13+
use rand::{rng, RngCore};
1414
use rsa::{
1515
pkcs8::DecodePublicKey,
1616
pss::{Signature as RsaSignature, SigningKey as RsaSigningKey},
17-
signature::{RandomizedDigestSigner as RsaRandomizedDigestSigner, SignatureEncoding},
17+
signature::{
18+
rand_core::{CryptoRng as LegacyCryptoRng, Error as LegacyRngError, RngCore as LegacyRngCore},
19+
RandomizedDigestSigner as RsaRandomizedDigestSigner, SignatureEncoding,
20+
},
1821
RsaPrivateKey, RsaPublicKey,
1922
};
2023
use sha2::{Sha256, Sha384, Sha512};
@@ -80,6 +83,36 @@ pub fn load_private_key(pem_data: &str, key_type: KeyType) -> Option<PrivateKey>
8083
}
8184
}
8285

86+
// ==========================================================================
87+
// RNG wrapper for signing
88+
// ==========================================================================
89+
90+
/// Workaround to implement the `RngCore` and `CryptoRng` traits from the **`rsa::signature::rand_core`** crate.
91+
///
92+
/// **TODO:** Remove when `rsa::signature::rand_core` is updated eventually!
93+
struct LegacyCompatRng;
94+
95+
impl LegacyRngCore for LegacyCompatRng {
96+
fn next_u32(&mut self) -> u32 {
97+
rng().next_u32()
98+
}
99+
100+
fn next_u64(&mut self) -> u64 {
101+
rng().next_u64()
102+
}
103+
104+
fn fill_bytes(&mut self, dest: &mut [u8]) {
105+
rng().fill_bytes(dest);
106+
}
107+
108+
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), LegacyRngError> {
109+
self.fill_bytes(dest);
110+
Ok(())
111+
}
112+
}
113+
114+
impl LegacyCryptoRng for LegacyCompatRng {}
115+
83116
// ==========================================================================
84117
// Signature computation
85118
// ==========================================================================
@@ -108,7 +141,7 @@ where
108141
D: Digest + FixedOutputReset,
109142
{
110143
let sign_key = RsaSigningKey::<D>::from(private_key.to_owned());
111-
RsaRandomizedDigestSigner::<D, RsaSignature>::sign_digest_with_rng(&sign_key, &mut thread_rng(), digest).to_vec()
144+
RsaRandomizedDigestSigner::<D, RsaSignature>::sign_digest_with_rng(&sign_key, &mut LegacyCompatRng, digest).to_vec()
112145
}
113146

114147
/// Compute signature using the ECDSA-scheme on NIST P-256 curve
@@ -122,7 +155,7 @@ fn create_signature_ecc(private_key: &EccPrivateKey, hash_algo: &HashAlgorithm,
122155
/// Compute signature using the ECDSA-scheme on NIST P-256 curve
123156
fn _create_signature_ecc(private_key: &EccPrivateKey, message: &[u8]) -> Vec<u8> {
124157
let sign_key = EccSigningKey::from(private_key);
125-
EccRandomizedDigestSigner::<Sha256, EccSignature>::sign_digest_with_rng(&sign_key, &mut thread_rng(), Sha256::new_with_prefix(message))
158+
EccRandomizedDigestSigner::<Sha256, EccSignature>::sign_digest_with_rng(&sign_key, &mut LegacyCompatRng, Sha256::new_with_prefix(message))
126159
.to_der()
127160
.to_vec()
128161
}

tests/common/random.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub fn generate_string<const N: usize>(rand_gen: &mut impl RngCore) -> String {
3939
];
4040
let mut rand_str = ['\0'; N];
4141
for i in 0..N {
42-
rand_str[i] = ASCII_PRINTABLE[rand_gen.gen_range(0..ASCII_PRINTABLE.len())];
42+
rand_str[i] = ASCII_PRINTABLE[rand_gen.random_range(0..ASCII_PRINTABLE.len())];
4343
}
4444
String::from_iter(rand_str)
4545
}

tests/common/tempfile.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ impl TempFile {
2121

2222
pub fn with_suffix(base_dir: &Path, suffix: &str) -> Option<TempFile> {
2323
assert!(!suffix.is_empty() && suffix.chars().all(|c| char::is_ascii_alphanumeric(&c)));
24-
let mut rng = rand::thread_rng();
24+
let mut rng = rand::rng();
2525

2626
for _i in 0..99 {
2727
let file_path = base_dir.join(format!("temp-{:16X}.{}", rng.next_u64(), suffix));

0 commit comments

Comments
 (0)