Skip to content

Commit 5098dc2

Browse files
committed
Make target-specific errors private
1 parent 30a7f1f commit 5098dc2

8 files changed

Lines changed: 84 additions & 43 deletions

File tree

src/backends/apple_other.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
1010
if ret == libc::kCCSuccess {
1111
Ok(())
1212
} else {
13-
Err(Error::IOS_SEC_RANDOM)
13+
Err(Error::IOS_RANDOM_GEN)
1414
}
1515
}
16+
17+
impl Error {
18+
/// Call to `CCRandomGenerateBytes` failed.
19+
pub(crate) const IOS_RANDOM_GEN: Error = Self::new_internal(10);
20+
}

src/backends/rdrand.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,10 @@ pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
170170
// SAFETY: After this point, we know rdrand is supported.
171171
unsafe { rdrand_exact(dest) }.ok_or(Error::FAILED_RDRAND)
172172
}
173+
174+
impl Error {
175+
/// RDRAND instruction failed due to a hardware issue.
176+
pub(crate) const FAILED_RDRAND: Error = Self::new_internal(10);
177+
/// RDRAND instruction unsupported on this target.
178+
pub(crate) const NO_RDRAND: Error = Self::new_internal(11);
179+
}

src/backends/rndr.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,10 @@ pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
132132
Err(Error::RNDR_NOT_AVAILABLE)
133133
}
134134
}
135+
136+
impl Error {
137+
/// RNDR register read failed due to a hardware issue.
138+
pub(crate) const RNDR_FAILURE: Error = Self::new_internal(10);
139+
/// RNDR register is not supported on this target.
140+
pub(crate) const RNDR_NOT_AVAILABLE: Error = Self::new_internal(11);
141+
}

src/backends/vxworks.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,8 @@ pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
4242
}
4343
Ok(())
4444
}
45+
46+
impl Error {
47+
/// On VxWorks, call to `randSecure` failed (random number generator is not yet initialized).
48+
pub(crate) const VXWORKS_RAND_SECURE: Error = Self::new_internal(10);
49+
}

src/backends/wasm_js.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,8 @@ extern "C" {
6565
#[wasm_bindgen(js_namespace = ["globalThis", "crypto"], js_name = getRandomValues, catch)]
6666
fn get_random_values(buf: &js_sys::Uint8Array) -> Result<(), JsValue>;
6767
}
68+
69+
impl Error {
70+
/// The environment does not support the Web Crypto API.
71+
pub(crate) const WEB_CRYPTO: Error = Self::new_internal(10);
72+
}

src/backends/windows.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,8 @@ pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
4040
_ => Err(Error::WINDOWS_PROCESS_PRNG),
4141
}
4242
}
43+
44+
impl Error {
45+
/// Calling Windows ProcessPrng failed.
46+
pub(crate) const WINDOWS_PROCESS_PRNG: Error = Self::new_internal(10);
47+
}

src/backends/windows7.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,8 @@ pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
3737
}
3838
Ok(())
3939
}
40+
41+
impl Error {
42+
/// Call to Windows [`RtlGenRandom`](https://docs.microsoft.com/en-us/windows/win32/api/ntsecapi/nf-ntsecapi-rtlgenrandom) failed.
43+
pub(crate) const WINDOWS_RTL_GEN_RANDOM: Error = Self::new_internal(10);
44+
}

src/error.rs

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,6 @@ impl Error {
2727
pub const ERRNO_NOT_POSITIVE: Error = Self::new_internal(1);
2828
/// Encountered an unexpected situation which should not happen in practice.
2929
pub const UNEXPECTED: Error = Self::new_internal(2);
30-
/// Call to [`CCRandomGenerateBytes`](https://opensource.apple.com/source/CommonCrypto/CommonCrypto-60074/include/CommonRandom.h.auto.html) failed
31-
/// on iOS, tvOS, or waatchOS.
32-
// TODO: Update this constant name in the next breaking release.
33-
pub const IOS_SEC_RANDOM: Error = Self::new_internal(3);
34-
/// Call to Windows [`RtlGenRandom`](https://docs.microsoft.com/en-us/windows/win32/api/ntsecapi/nf-ntsecapi-rtlgenrandom) failed.
35-
pub const WINDOWS_RTL_GEN_RANDOM: Error = Self::new_internal(4);
36-
/// RDRAND instruction failed due to a hardware issue.
37-
pub const FAILED_RDRAND: Error = Self::new_internal(5);
38-
/// RDRAND instruction unsupported on this target.
39-
pub const NO_RDRAND: Error = Self::new_internal(6);
40-
/// The environment does not support the Web Crypto API.
41-
pub const WEB_CRYPTO: Error = Self::new_internal(7);
42-
/// On VxWorks, call to `randSecure` failed (random number generator is not yet initialized).
43-
pub const VXWORKS_RAND_SECURE: Error = Self::new_internal(11);
44-
/// Calling Windows ProcessPrng failed.
45-
pub const WINDOWS_PROCESS_PRNG: Error = Self::new_internal(12);
46-
/// RNDR register read failed due to a hardware issue.
47-
pub const RNDR_FAILURE: Error = Self::new_internal(13);
48-
/// RNDR register is not supported on this target.
49-
pub const RNDR_NOT_AVAILABLE: Error = Self::new_internal(14);
5030

5131
/// Codes below this point represent OS Errors (i.e. positive i32 values).
5232
/// Codes at or above this point, but below [`Error::CUSTOM_START`] are
@@ -101,11 +81,52 @@ impl Error {
10181
}
10282

10383
/// Creates a new instance of an `Error` from a particular internal error code.
104-
const fn new_internal(n: u16) -> Error {
84+
pub(crate) const fn new_internal(n: u16) -> Error {
10585
// SAFETY: code > 0 as INTERNAL_START > 0 and adding n won't overflow a u32.
10686
let code = Error::INTERNAL_START + (n as u32);
10787
Error(unsafe { NonZeroU32::new_unchecked(code) })
10888
}
89+
90+
fn internal_desc(&self) -> Option<&'static str> {
91+
let desc = match *self {
92+
Error::UNSUPPORTED => "getrandom: this target is not supported",
93+
Error::ERRNO_NOT_POSITIVE => "errno: did not return a positive value",
94+
Error::UNEXPECTED => "unexpected situation",
95+
#[cfg(any(
96+
target_os = "ios",
97+
target_os = "visionos",
98+
target_os = "watchos",
99+
target_os = "tvos",
100+
))]
101+
Error::IOS_RANDOM_GEN => "SecRandomCopyBytes: iOS Security framework failure",
102+
#[cfg(all(windows, not(target_vendor = "win7")))]
103+
Error::WINDOWS_PROCESS_PRNG => "ProcessPrng: Windows system function failure",
104+
#[cfg(all(windows, target_vendor = "win7"))]
105+
Error::WINDOWS_RTL_GEN_RANDOM => "RtlGenRandom: Windows system function failure",
106+
#[cfg(getrandom_backend = "wasm_js")]
107+
Error::WEB_CRYPTO => "Web Crypto API is unavailable",
108+
#[cfg(target_os = "vxworks")]
109+
Error::VXWORKS_RAND_SECURE => "randSecure: VxWorks RNG module is not initialized",
110+
111+
#[cfg(any(
112+
getrandom_backend = "rdrand",
113+
all(target_arch = "x86_64", target_env = "sgx")
114+
))]
115+
Error::FAILED_RDRAND => "RDRAND: failed multiple times: CPU issue likely",
116+
#[cfg(any(
117+
getrandom_backend = "rdrand",
118+
all(target_arch = "x86_64", target_env = "sgx")
119+
))]
120+
Error::NO_RDRAND => "RDRAND: instruction not supported",
121+
122+
#[cfg(getrandom_backend = "rndr")]
123+
Error::RNDR_FAILURE => "RNDR: Could not generate a random number",
124+
#[cfg(getrandom_backend = "rndr")]
125+
Error::RNDR_NOT_AVAILABLE => "RNDR: Register not supported",
126+
_ => return None,
127+
};
128+
Some(desc)
129+
}
109130
}
110131

111132
impl fmt::Debug for Error {
@@ -115,7 +136,7 @@ impl fmt::Debug for Error {
115136
dbg.field("os_error", &errno);
116137
#[cfg(feature = "std")]
117138
dbg.field("description", &std::io::Error::from_raw_os_error(errno));
118-
} else if let Some(desc) = internal_desc(*self) {
139+
} else if let Some(desc) = self.internal_desc() {
119140
dbg.field("internal_code", &self.0.get());
120141
dbg.field("description", &desc);
121142
} else {
@@ -135,33 +156,14 @@ impl fmt::Display for Error {
135156
write!(f, "OS Error: {}", errno)
136157
}
137158
}
138-
} else if let Some(desc) = internal_desc(*self) {
159+
} else if let Some(desc) = self.internal_desc() {
139160
f.write_str(desc)
140161
} else {
141162
write!(f, "Unknown Error: {}", self.0.get())
142163
}
143164
}
144165
}
145166

146-
fn internal_desc(error: Error) -> Option<&'static str> {
147-
let desc = match error {
148-
Error::UNSUPPORTED => "getrandom: this target is not supported",
149-
Error::ERRNO_NOT_POSITIVE => "errno: did not return a positive value",
150-
Error::UNEXPECTED => "unexpected situation",
151-
Error::IOS_SEC_RANDOM => "SecRandomCopyBytes: iOS Security framework failure",
152-
Error::WINDOWS_RTL_GEN_RANDOM => "RtlGenRandom: Windows system function failure",
153-
Error::FAILED_RDRAND => "RDRAND: failed multiple times: CPU issue likely",
154-
Error::NO_RDRAND => "RDRAND: instruction not supported",
155-
Error::WEB_CRYPTO => "Web Crypto API is unavailable",
156-
Error::VXWORKS_RAND_SECURE => "randSecure: VxWorks RNG module is not initialized",
157-
Error::WINDOWS_PROCESS_PRNG => "ProcessPrng: Windows system function failure",
158-
Error::RNDR_FAILURE => "RNDR: Could not generate a random number",
159-
Error::RNDR_NOT_AVAILABLE => "RNDR: Register not supported",
160-
_ => return None,
161-
};
162-
Some(desc)
163-
}
164-
165167
#[cfg(test)]
166168
mod tests {
167169
use super::Error;

0 commit comments

Comments
 (0)