Skip to content

Commit 17c9011

Browse files
committed
Replace RngAlgorithm struct by RngAlgorithmType Enum + Better test log
1 parent 620ed9d commit 17c9011

File tree

2 files changed

+80
-20
lines changed

2 files changed

+80
-20
lines changed

src/proto/rng.rs

Lines changed: 76 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,75 @@
33
use crate::{data_types::Guid, proto::Protocol, unsafe_guid, Result, Status};
44
use core::{mem, ptr};
55

6-
/// Contain a Rng algorithm Guid
7-
#[repr(C)]
8-
#[derive(Clone, Copy, Debug)]
9-
pub struct RngAlgorithm(pub Guid);
6+
newtype_enum! {
7+
/// The algorithms listed are optional, not meant to be exhaustive
8+
/// and may be augmented by vendors or other industry standards.
9+
pub enum RngAlgorithmType: Guid => {
10+
/// Indicates a empty algorithm, used to instantiate a buffer
11+
/// for `get_info`
12+
EMPTY_ALGORITHM = Guid::from_values(
13+
0x00000000,
14+
0x0000,
15+
0x0000,
16+
0x0000,
17+
0x000000000000,
18+
),
19+
20+
/// The “raw” algorithm, when supported, is intended to provide
21+
/// entropy directly from the source, without it going through
22+
/// some deterministic random bit generator.
23+
ALGORITHM_RAW = Guid::from_values(
24+
0xe43176d7,
25+
0xb6e8,
26+
0x4827,
27+
0xb784,
28+
0x7ffdc4b68561,
29+
),
30+
31+
/// ALGORITHM_SP800_90_HASH_256
32+
ALGORITHM_SP800_90_HASH_256 = Guid::from_values(
33+
0xa7af67cb,
34+
0x603b,
35+
0x4d42,
36+
0xba21,
37+
0x70bfb6293f96,
38+
),
39+
40+
/// ALGORITHM_SP800_90_HMAC_256
41+
ALGORITHM_SP800_90_HMAC_256 = Guid::from_values(
42+
0xc5149b43,
43+
0xae85,
44+
0x4f53,
45+
0x9982,
46+
0xb94335d3a9e7,
47+
),
48+
49+
/// ALGORITHM_SP800_90_CTR_256
50+
ALGORITHM_SP800_90_CTR_256 = Guid::from_values(
51+
0x44f0de6e,
52+
0x4d8c,
53+
0x4045,
54+
0xa8c7,
55+
0x4dd168856b9e,
56+
),
57+
58+
/// ALGORITHM_X9_31_3DES
59+
ALGORITHM_X9_31_3DES = Guid::from_values(
60+
0x63c4785a,
61+
0xca34,
62+
0x4012,
63+
0xa3c8,
64+
0x0b6a324f5546,
65+
),
1066

11-
impl RngAlgorithm {
12-
/// Get an empty `RngAlgorithm`
13-
///
14-
/// Used provide a buffer to `Rng.get_info`
15-
pub fn default() -> Self {
16-
Self(Guid::default())
67+
/// ALGORITHM_X9_31_AES
68+
ALGORITHM_X9_31_AES = Guid::from_values(
69+
0xacd03321,
70+
0x777e,
71+
0x4d3d,
72+
0xb1c8,
73+
0x20cfd88820c9,
74+
),
1775
}
1876
}
1977

@@ -25,11 +83,11 @@ pub struct Rng {
2583
get_info: unsafe extern "efiapi" fn(
2684
this: &Rng,
2785
algorithm_list_size: *mut usize,
28-
algorithm_list: *mut RngAlgorithm,
86+
algorithm_list: *mut RngAlgorithmType,
2987
) -> Status,
3088
get_rng: unsafe extern "efiapi" fn(
3189
this: &Rng,
32-
algorithm: *const RngAlgorithm,
90+
algorithm: *const RngAlgorithmType,
3391
value_length: usize,
3492
value: *mut u8,
3593
) -> Status,
@@ -39,14 +97,14 @@ impl Rng {
3997
/// Returns information about the random number generation implementation.
4098
pub fn get_info<'buf>(
4199
&mut self,
42-
algorithm_list: &'buf mut [RngAlgorithm],
43-
) -> Result<&'buf [RngAlgorithm], Option<usize>> {
44-
let mut algorithm_list_size = algorithm_list.len() * mem::size_of::<RngAlgorithm>();
100+
algorithm_list: &'buf mut [RngAlgorithmType],
101+
) -> Result<&'buf [RngAlgorithmType], Option<usize>> {
102+
let mut algorithm_list_size = algorithm_list.len() * mem::size_of::<RngAlgorithmType>();
45103

46104
unsafe {
47105
(self.get_info)(self, &mut algorithm_list_size, algorithm_list.as_mut_ptr()).into_with(
48106
|| {
49-
let len = algorithm_list_size / mem::size_of::<RngAlgorithm>();
107+
let len = algorithm_list_size / mem::size_of::<RngAlgorithmType>();
50108
&algorithm_list[..len]
51109
},
52110
|status| {
@@ -61,12 +119,12 @@ impl Rng {
61119
}
62120

63121
/// Returns the next set of random numbers
64-
pub fn get_rng(&mut self, algorithm: Option<RngAlgorithm>, buffer: &mut [u8]) -> Result {
122+
pub fn get_rng(&mut self, algorithm: Option<RngAlgorithmType>, buffer: &mut [u8]) -> Result {
65123
let buffer_length = buffer.len();
66124

67125
let algo = match algorithm {
68126
None => ptr::null(),
69-
Some(algo) => &algo as *const RngAlgorithm,
127+
Some(algo) => &algo as *const RngAlgorithmType,
70128
};
71129

72130
unsafe { (self.get_rng)(self, algo, buffer_length, buffer.as_mut_ptr()).into() }

uefi-test-runner/src/proto/rng.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use uefi::prelude::*;
2-
use uefi::proto::rng::{Rng, RngAlgorithm};
2+
use uefi::proto::rng::{Rng, RngAlgorithmType};
33
use uefi::table::boot::{BootServices, OpenProtocolAttributes, OpenProtocolParams};
44

55
pub fn test(image: Handle, bt: &BootServices) {
@@ -23,13 +23,15 @@ pub fn test(image: Handle, bt: &BootServices) {
2323
.expect_success("Failed to open Rng protocol");
2424
let rng = unsafe { &mut *rng.interface.get() };
2525

26-
let mut list = [RngAlgorithm::default(); 4];
26+
let mut list = [RngAlgorithmType::EMPTY_ALGORITHM; 4];
2727

2828
rng.get_info(&mut list).unwrap_success();
29+
info!("Supported rng algorithms : {:?}", list);
2930

3031
let mut buf = [0u8; 4];
3132

3233
rng.get_rng(Some(list[0]), &mut buf).unwrap_success();
3334

3435
assert_ne!([0u8; 4], buf);
36+
info!("Random buffer : {:?}", buf);
3537
}

0 commit comments

Comments
 (0)