| 1 | const std = @import("std"); |
| 2 | const uefi = std.os.uefi; |
| 3 | const Guid = uefi.Guid; |
| 4 | const Status = uefi.Status; |
| 5 | const cc = uefi.cc; |
| 6 | |
| 7 | /// Random Number Generator protocol |
| 8 | pub const Rng = extern struct { |
| 9 | _get_info: *const fn (*const Rng, *usize, [*]align(8) Guid) callconv(cc) Status, |
| 10 | _get_rng: *const fn (*const Rng, ?*align(8) const Guid, usize, [*]u8) callconv(cc) Status, |
| 11 | |
| 12 | pub const GetInfoError = uefi.UnexpectedError || error{ |
| 13 | Unsupported, |
| 14 | DeviceError, |
| 15 | BufferTooSmall, |
| 16 | }; |
| 17 | pub const GetRNGError = uefi.UnexpectedError || error{ |
| 18 | Unsupported, |
| 19 | DeviceError, |
| 20 | NotReady, |
| 21 | InvalidParameter, |
| 22 | }; |
| 23 | |
| 24 | /// Returns information about the random number generation implementation. |
| 25 | pub fn getInfo(self: *const Rng, list: []align(8) Guid) GetInfoError![]align(8) Guid { |
| 26 | var len: usize = list.len; |
| 27 | switch (self._get_info(self, &len, list.ptr)) { |
| 28 | .success => return list[0..len], |
| 29 | .unsupported => return error.Unsupported, |
| 30 | .device_error => return error.DeviceError, |
| 31 | .buffer_too_small => return error.BufferTooSmall, |
| 32 | else => |status| return uefi.unexpectedStatus(status), |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | /// Produces and returns an RNG value using either the default or specified RNG algorithm. |
| 37 | pub fn getRNG(self: *const Rng, algo: ?*align(8) const Guid, value: []u8) GetRNGError!void { |
| 38 | switch (self._get_rng(self, algo, value.len, value.ptr)) { |
| 39 | .success => {}, |
| 40 | .unsupported => return error.Unsupported, |
| 41 | .device_error => return error.DeviceError, |
| 42 | .not_ready => return error.NotReady, |
| 43 | .invalid_parameter => return error.InvalidParameter, |
| 44 | else => |status| return uefi.unexpectedStatus(status), |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | pub const guid align(8) = Guid{ |
| 49 | .time_low = 0x3152bca5, |
| 50 | .time_mid = 0xeade, |
| 51 | .time_high_and_version = 0x433d, |
| 52 | .clock_seq_high_and_reserved = 0x86, |
| 53 | .clock_seq_low = 0x2e, |
| 54 | .node = [_]u8{ 0xc0, 0x1c, 0xdc, 0x29, 0x1f, 0x44 }, |
| 55 | }; |
| 56 | pub const algorithm_sp800_90_hash_256 align(8) = Guid{ |
| 57 | .time_low = 0xa7af67cb, |
| 58 | .time_mid = 0x603b, |
| 59 | .time_high_and_version = 0x4d42, |
| 60 | .clock_seq_high_and_reserved = 0xba, |
| 61 | .clock_seq_low = 0x21, |
| 62 | .node = [_]u8{ 0x70, 0xbf, 0xb6, 0x29, 0x3f, 0x96 }, |
| 63 | }; |
| 64 | pub const algorithm_sp800_90_hmac_256 align(8) = Guid{ |
| 65 | .time_low = 0xc5149b43, |
| 66 | .time_mid = 0xae85, |
| 67 | .time_high_and_version = 0x4f53, |
| 68 | .clock_seq_high_and_reserved = 0x99, |
| 69 | .clock_seq_low = 0x82, |
| 70 | .node = [_]u8{ 0xb9, 0x43, 0x35, 0xd3, 0xa9, 0xe7 }, |
| 71 | }; |
| 72 | pub const algorithm_sp800_90_ctr_256 align(8) = Guid{ |
| 73 | .time_low = 0x44f0de6e, |
| 74 | .time_mid = 0x4d8c, |
| 75 | .time_high_and_version = 0x4045, |
| 76 | .clock_seq_high_and_reserved = 0xa8, |
| 77 | .clock_seq_low = 0xc7, |
| 78 | .node = [_]u8{ 0x4d, 0xd1, 0x68, 0x85, 0x6b, 0x9e }, |
| 79 | }; |
| 80 | pub const algorithm_x9_31_3des align(8) = Guid{ |
| 81 | .time_low = 0x63c4785a, |
| 82 | .time_mid = 0xca34, |
| 83 | .time_high_and_version = 0x4012, |
| 84 | .clock_seq_high_and_reserved = 0xa3, |
| 85 | .clock_seq_low = 0xc8, |
| 86 | .node = [_]u8{ 0x0b, 0x6a, 0x32, 0x4f, 0x55, 0x46 }, |
| 87 | }; |
| 88 | pub const algorithm_x9_31_aes align(8) = Guid{ |
| 89 | .time_low = 0xacd03321, |
| 90 | .time_mid = 0x777e, |
| 91 | .time_high_and_version = 0x4d3d, |
| 92 | .clock_seq_high_and_reserved = 0xb1, |
| 93 | .clock_seq_low = 0xc8, |
| 94 | .node = [_]u8{ 0x20, 0xcf, 0xd8, 0x88, 0x20, 0xc9 }, |
| 95 | }; |
| 96 | pub const algorithm_raw align(8) = Guid{ |
| 97 | .time_low = 0xe43176d7, |
| 98 | .time_mid = 0xb6e8, |
| 99 | .time_high_and_version = 0x4827, |
| 100 | .clock_seq_high_and_reserved = 0xb7, |
| 101 | .clock_seq_low = 0x84, |
| 102 | .node = [_]u8{ 0x7f, 0xfd, 0xc4, 0xb6, 0x85, 0x61 }, |
| 103 | }; |
| 104 | }; |