Skip to content

Commit 318f08a

Browse files
committed
feat: added selection of entropy crate
Added ability to select between `rand` and `aws-lc-rs` crates for entropy device. Signed-off-by: Egor Lazarchuk <[email protected]>
1 parent 48a3e87 commit 318f08a

File tree

4 files changed

+27
-4
lines changed

4 files changed

+27
-4
lines changed

Diff for: Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: src/firecracker/Cargo.toml

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ description = "Firecracker enables you to deploy workloads in lightweight virtua
88
homepage = "https://firecracker-microvm.github.io/"
99
license = "Apache-2.0"
1010

11+
[features]
12+
default = ["aws-lc-rs"]
13+
aws-lc-rs = ["vmm/aws-lc-rs"]
14+
rand = ["vmm/rand"]
15+
1116
[dependencies]
1217
event-manager = "0.3.0"
1318
libc = "0.2.117"
@@ -20,7 +25,7 @@ mmds = { path = "../mmds" }
2025
seccompiler = { path = "../seccompiler" }
2126
snapshot = { path = "../snapshot"}
2227
utils = { path = "../utils" }
23-
vmm = { path = "../vmm" }
28+
vmm = { path = "../vmm", default-features = false }
2429

2530
[dev-dependencies]
2631
cargo_toml = "0.13.0"

Diff for: src/vmm/Cargo.toml

+7-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,14 @@ authors = ["Amazon Firecracker team <[email protected]>"]
55
edition = "2021"
66
license = "Apache-2.0"
77

8+
[features]
9+
default = []
10+
aws-lc-rs = ["dep:aws-lc-rs"]
11+
rand = ["dep:rand"]
12+
813
[dependencies]
9-
aws-lc-rs = "1.0.2"
14+
aws-lc-rs = { version = "1.0.2", optional = true }
15+
rand = { version = "0.8.5", optional = true }
1016
bitflags = "2.0.2"
1117
derive_more = { version = "0.99.17", default-features = false, features = ["from", "display"] }
1218
event-manager = "0.3.0"

Diff for: src/vmm/src/devices/virtio/rng/device.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ use std::io;
55
use std::sync::atomic::AtomicUsize;
66
use std::sync::Arc;
77

8-
use aws_lc_rs::rand;
8+
#[cfg(feature = "aws-lc-rs")]
9+
use aws_lc_rs::{error::Unspecified as RandomError, rand};
10+
#[cfg(feature = "rand")]
11+
use rand::{rngs::OsRng, Error as RandomError, RngCore};
12+
913
use logger::{debug, error, IncMetric, METRICS};
1014
use rate_limiter::{RateLimiter, TokenType};
1115
use utils::eventfd::EventFd;
@@ -27,7 +31,7 @@ pub enum Error {
2731
#[error("Bad guest memory buffer: {0}")]
2832
GuestMemory(#[from] GuestMemoryError),
2933
#[error("Could not get random bytes: {0}")]
30-
Random(#[from] aws_lc_rs::error::Unspecified),
34+
Random(#[from] RandomError),
3135
}
3236

3337
type Result<T> = std::result::Result<T, Error>;
@@ -109,10 +113,17 @@ impl Entropy {
109113
}
110114

111115
let mut rand_bytes = vec![0; iovec.len()];
116+
117+
#[cfg(feature = "aws-lc-rs")]
112118
rand::fill(&mut rand_bytes).map_err(|err| {
113119
METRICS.entropy.host_rng_fails.inc();
114120
err
115121
})?;
122+
#[cfg(feature = "rand")]
123+
OsRng.try_fill_bytes(&mut rand_bytes).map_err(|err| {
124+
METRICS.entropy.host_rng_fails.inc();
125+
err
126+
})?;
116127

117128
// It is ok to unwrap here. We are writing `iovec.len()` bytes at offset 0.
118129
Ok(iovec.write_at(&rand_bytes, 0).unwrap().try_into().unwrap())

0 commit comments

Comments
 (0)