Skip to content

Commit 9d7bdeb

Browse files
uefi: Use uefi-raw's RngProtocol
This replaces much of the `Rng` implementation with stuff from uefi-raw.
1 parent 28d863f commit 9d7bdeb

File tree

1 file changed

+25
-60
lines changed

1 file changed

+25
-60
lines changed

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)