Skip to content

Commit

Permalink
feat: added selection of entropy crate
Browse files Browse the repository at this point in the history
Added ability to select between `rand` and
`aws-lc-rs` crates for entropy device.

Signed-off-by: Egor Lazarchuk <yegorlz@amazon.co.uk>
  • Loading branch information
ShadowCurse committed May 28, 2023
1 parent 48a3e87 commit 318f08a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion src/firecracker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ description = "Firecracker enables you to deploy workloads in lightweight virtua
homepage = "https://firecracker-microvm.github.io/"
license = "Apache-2.0"

[features]
default = ["aws-lc-rs"]
aws-lc-rs = ["vmm/aws-lc-rs"]
rand = ["vmm/rand"]

[dependencies]
event-manager = "0.3.0"
libc = "0.2.117"
Expand All @@ -20,7 +25,7 @@ mmds = { path = "../mmds" }
seccompiler = { path = "../seccompiler" }
snapshot = { path = "../snapshot"}
utils = { path = "../utils" }
vmm = { path = "../vmm" }
vmm = { path = "../vmm", default-features = false }

[dev-dependencies]
cargo_toml = "0.13.0"
Expand Down
8 changes: 7 additions & 1 deletion src/vmm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@ authors = ["Amazon Firecracker team <firecracker-devel@amazon.com>"]
edition = "2021"
license = "Apache-2.0"

[features]
default = []
aws-lc-rs = ["dep:aws-lc-rs"]
rand = ["dep:rand"]

[dependencies]
aws-lc-rs = "1.0.2"
aws-lc-rs = { version = "1.0.2", optional = true }
rand = { version = "0.8.5", optional = true }
bitflags = "2.0.2"
derive_more = { version = "0.99.17", default-features = false, features = ["from", "display"] }
event-manager = "0.3.0"
Expand Down
15 changes: 13 additions & 2 deletions src/vmm/src/devices/virtio/rng/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ use std::io;
use std::sync::atomic::AtomicUsize;
use std::sync::Arc;

use aws_lc_rs::rand;
#[cfg(feature = "aws-lc-rs")]
use aws_lc_rs::{error::Unspecified as RandomError, rand};
#[cfg(feature = "rand")]
use rand::{rngs::OsRng, Error as RandomError, RngCore};

use logger::{debug, error, IncMetric, METRICS};
use rate_limiter::{RateLimiter, TokenType};
use utils::eventfd::EventFd;
Expand All @@ -27,7 +31,7 @@ pub enum Error {
#[error("Bad guest memory buffer: {0}")]
GuestMemory(#[from] GuestMemoryError),
#[error("Could not get random bytes: {0}")]
Random(#[from] aws_lc_rs::error::Unspecified),
Random(#[from] RandomError),
}

type Result<T> = std::result::Result<T, Error>;
Expand Down Expand Up @@ -109,10 +113,17 @@ impl Entropy {
}

let mut rand_bytes = vec![0; iovec.len()];

#[cfg(feature = "aws-lc-rs")]
rand::fill(&mut rand_bytes).map_err(|err| {
METRICS.entropy.host_rng_fails.inc();
err
})?;
#[cfg(feature = "rand")]
OsRng.try_fill_bytes(&mut rand_bytes).map_err(|err| {
METRICS.entropy.host_rng_fails.inc();
err
})?;

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

0 comments on commit 318f08a

Please sign in to comment.