Skip to content

Commit e84e76f

Browse files
authored
Merge pull request #778 from nicholasbishop/bishop-raw-first-proto
Add RngProtocol to `uefi-raw` and use it from `uefi`
2 parents 1ab4840 + 9d7bdeb commit e84e76f

File tree

6 files changed

+91
-60
lines changed

6 files changed

+91
-60
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

uefi-raw/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ rust-version = "1.68"
1515
bitflags = "1.3.1"
1616
ptr_meta = { version = "0.2.0", default-features = false }
1717
uefi-macros = "0.11.0"
18+
uguid = "2.0.0"
1819

1920
[package.metadata.docs.rs]
2021
rustdoc-args = ["--cfg", "docsrs"]

uefi-raw/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
#[macro_use]
1818
mod enums;
1919

20+
pub mod protocol;
21+
2022
mod status;
2123

2224
pub use status::Status;
25+
pub use uguid::{guid, Guid};

uefi-raw/src/protocol/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//! Protocol definitions.
2+
//!
3+
//! Protocols are sets of related functionality identified by a unique
4+
//! ID. They can be implemented by a UEFI driver or occasionally by a
5+
//! UEFI application.
6+
7+
pub mod rng;

uefi-raw/src/protocol/rng.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//! `Rng` protocol.
2+
3+
use crate::{guid, Guid, Status};
4+
5+
newtype_enum! {
6+
/// The algorithms listed are optional, not meant to be exhaustive
7+
/// and may be augmented by vendors or other industry standards.
8+
pub enum RngAlgorithmType: Guid => {
9+
/// Indicates a empty algorithm, used to instantiate a buffer
10+
/// for `get_info`
11+
EMPTY_ALGORITHM = guid!("00000000-0000-0000-0000-000000000000"),
12+
13+
/// The “raw” algorithm, when supported, is intended to provide
14+
/// entropy directly from the source, without it going through
15+
/// some deterministic random bit generator.
16+
ALGORITHM_RAW = guid!("e43176d7-b6e8-4827-b784-7ffdc4b68561"),
17+
18+
/// ALGORITHM_SP800_90_HASH_256
19+
ALGORITHM_SP800_90_HASH_256 = guid!("a7af67cb-603b-4d42-ba21-70bfb6293f96"),
20+
21+
/// ALGORITHM_SP800_90_HMAC_256
22+
ALGORITHM_SP800_90_HMAC_256 = guid!("c5149b43-ae85-4f53-9982-b94335d3a9e7"),
23+
24+
/// ALGORITHM_SP800_90_CTR_256
25+
ALGORITHM_SP800_90_CTR_256 = guid!("44f0de6e-4d8c-4045-a8c7-4dd168856b9e"),
26+
27+
/// ALGORITHM_X9_31_3DES
28+
ALGORITHM_X9_31_3DES = guid!("63c4785a-ca34-4012-a3c8-0b6a324f5546"),
29+
30+
/// ALGORITHM_X9_31_AES
31+
ALGORITHM_X9_31_AES = guid!("acd03321-777e-4d3d-b1c8-20cfd88820c9"),
32+
}
33+
}
34+
35+
/// Rng protocol.
36+
#[repr(C)]
37+
pub struct RngProtocol {
38+
pub get_info: unsafe extern "efiapi" fn(
39+
this: *mut RngProtocol,
40+
algorithm_list_size: *mut usize,
41+
algorithm_list: *mut RngAlgorithmType,
42+
) -> Status,
43+
44+
pub get_rng: unsafe extern "efiapi" fn(
45+
this: *mut RngProtocol,
46+
algorithm: *const RngAlgorithmType,
47+
value_length: usize,
48+
value: *mut u8,
49+
) -> Status,
50+
}
51+
52+
impl RngProtocol {
53+
pub const GUID: Guid = guid!("3152bca5-eade-433d-862e-c01cdc291f44");
54+
}

uefi/src/proto/rng.rs

Lines changed: 25 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,15 @@
11
//! `Rng` protocol.
22
3-
use crate::data_types::Guid;
43
use crate::proto::unsafe_protocol;
5-
use crate::{guid, Result, Status, StatusExt};
4+
use crate::{Result, Status, StatusExt};
65
use core::{mem, ptr};
76

8-
newtype_enum! {
9-
/// The algorithms listed are optional, not meant to be exhaustive
10-
/// and may be augmented by vendors or other industry standards.
11-
pub enum RngAlgorithmType: Guid => {
12-
/// Indicates a empty algorithm, used to instantiate a buffer
13-
/// for `get_info`
14-
EMPTY_ALGORITHM = guid!("00000000-0000-0000-0000-000000000000"),
15-
16-
/// The “raw” algorithm, when supported, is intended to provide
17-
/// entropy directly from the source, without it going through
18-
/// some deterministic random bit generator.
19-
ALGORITHM_RAW = guid!("e43176d7-b6e8-4827-b784-7ffdc4b68561"),
20-
21-
/// ALGORITHM_SP800_90_HASH_256
22-
ALGORITHM_SP800_90_HASH_256 = guid!("a7af67cb-603b-4d42-ba21-70bfb6293f96"),
23-
24-
/// ALGORITHM_SP800_90_HMAC_256
25-
ALGORITHM_SP800_90_HMAC_256 = guid!("c5149b43-ae85-4f53-9982-b94335d3a9e7"),
26-
27-
/// ALGORITHM_SP800_90_CTR_256
28-
ALGORITHM_SP800_90_CTR_256 = guid!("44f0de6e-4d8c-4045-a8c7-4dd168856b9e"),
29-
30-
/// ALGORITHM_X9_31_3DES
31-
ALGORITHM_X9_31_3DES = guid!("63c4785a-ca34-4012-a3c8-0b6a324f5546"),
32-
33-
/// ALGORITHM_X9_31_AES
34-
ALGORITHM_X9_31_AES = guid!("acd03321-777e-4d3d-b1c8-20cfd88820c9"),
35-
}
36-
}
7+
pub use uefi_raw::protocol::rng::RngAlgorithmType;
378

389
/// Rng protocol
3910
#[repr(C)]
40-
#[unsafe_protocol("3152bca5-eade-433d-862e-c01cdc291f44")]
41-
pub struct Rng {
42-
get_info: unsafe extern "efiapi" fn(
43-
this: &Rng,
44-
algorithm_list_size: *mut usize,
45-
algorithm_list: *mut RngAlgorithmType,
46-
) -> Status,
47-
get_rng: unsafe extern "efiapi" fn(
48-
this: &Rng,
49-
algorithm: *const RngAlgorithmType,
50-
value_length: usize,
51-
value: *mut u8,
52-
) -> Status,
53-
}
11+
#[unsafe_protocol(uefi_raw::protocol::rng::RngProtocol::GUID)]
12+
pub struct Rng(uefi_raw::protocol::rng::RngProtocol);
5413

5514
impl Rng {
5615
/// Returns information about the random number generation implementation.
@@ -61,20 +20,24 @@ impl Rng {
6120
let mut algorithm_list_size = algorithm_list.len() * mem::size_of::<RngAlgorithmType>();
6221

6322
unsafe {
64-
(self.get_info)(self, &mut algorithm_list_size, algorithm_list.as_mut_ptr())
65-
.to_result_with(
66-
|| {
67-
let len = algorithm_list_size / mem::size_of::<RngAlgorithmType>();
68-
&algorithm_list[..len]
69-
},
70-
|status| {
71-
if status == Status::BUFFER_TOO_SMALL {
72-
Some(algorithm_list_size)
73-
} else {
74-
None
75-
}
76-
},
77-
)
23+
(self.0.get_info)(
24+
&mut self.0,
25+
&mut algorithm_list_size,
26+
algorithm_list.as_mut_ptr(),
27+
)
28+
.to_result_with(
29+
|| {
30+
let len = algorithm_list_size / mem::size_of::<RngAlgorithmType>();
31+
&algorithm_list[..len]
32+
},
33+
|status| {
34+
if status == Status::BUFFER_TOO_SMALL {
35+
Some(algorithm_list_size)
36+
} else {
37+
None
38+
}
39+
},
40+
)
7841
}
7942
}
8043

@@ -87,6 +50,8 @@ impl Rng {
8750
Some(algo) => algo as *const RngAlgorithmType,
8851
};
8952

90-
unsafe { (self.get_rng)(self, algo, buffer_length, buffer.as_mut_ptr()).to_result() }
53+
unsafe {
54+
(self.0.get_rng)(&mut self.0, algo, buffer_length, buffer.as_mut_ptr()).to_result()
55+
}
9156
}
9257
}

0 commit comments

Comments
 (0)