|
| 1 | +// SPDX-License-Identifier: Apache-2.0 OR MIT |
| 2 | +// |
| 3 | +// Copyright (c) 2022 Amazon.com, Inc. or its affiliates. |
| 4 | +// |
| 5 | +// Author(s): |
| 6 | +// - Ali Saidi <[email protected]> |
| 7 | + |
| 8 | + |
| 9 | +/// Implement an interface for accessing Arm v8.5 RNG instructions. |
| 10 | +/// An empty struct is used to confirm that the system has the |
| 11 | +/// instructions available. |
| 12 | +/// # Example: |
| 13 | +/// ```no_run |
| 14 | +/// use cortex_a::asm::random::ArmRng; |
| 15 | +/// if let Some(rng) = ArmRng::new() { |
| 16 | +/// let rand_num = rng.rndr(); |
| 17 | +/// } |
| 18 | +/// ``` |
| 19 | +#[derive(Copy, Clone, Debug)] |
| 20 | +pub struct ArmRng; |
| 21 | + |
| 22 | +use crate::registers::ID_AA64ISAR0_EL1; |
| 23 | +use core::arch::asm; |
| 24 | +use tock_registers::interfaces::Readable; |
| 25 | + |
| 26 | +impl ArmRng { |
| 27 | + /// Return an empty object that is used to gate calling |
| 28 | + /// rndr and rndrss on discovery of the feature so each |
| 29 | + /// call doesn't need to confirm it. |
| 30 | + #[inline] |
| 31 | + pub fn new() -> Option<Self> { |
| 32 | + #[cfg(not(target_arch = "aarch64"))] |
| 33 | + return None; |
| 34 | + |
| 35 | + #[cfg(target_arch = "aarch64")] |
| 36 | + if ID_AA64ISAR0_EL1.is_set(ID_AA64ISAR0_EL1::RNDR) { |
| 37 | + Some(ArmRng) |
| 38 | + } else { |
| 39 | + None |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + /// Return an random number from the Arm v8.5 RNG. |
| 44 | + /// This returns an option because the instruction can fail |
| 45 | + /// (e.g. the entropy is exhausted or the RNG has failed.) |
| 46 | + #[inline] |
| 47 | + pub fn rndr(&self) -> Option<u64> { |
| 48 | + let mut flags: u64; |
| 49 | + let mut data: u64; |
| 50 | + |
| 51 | + #[cfg(target_arch = "aarch64")] |
| 52 | + unsafe { |
| 53 | + asm!( |
| 54 | + "mrs {o}, s3_3_c2_c4_0", |
| 55 | + "mrs {f}, nzcv", |
| 56 | + o = out(reg) data, |
| 57 | + f = out(reg) flags, |
| 58 | + options(nomem, nostack)); |
| 59 | + } |
| 60 | + if cfg!(not(target_arch = "aarch64")) || flags != 0 { |
| 61 | + None |
| 62 | + } else { |
| 63 | + Some(data) |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + /// Return an random number from the Arm v8.5 RNG after reseeding it |
| 68 | + /// This returns an option because the instruction can fail |
| 69 | + /// (e.g. the entropy is exhausted or the RNG has failed.) |
| 70 | + #[inline] |
| 71 | + pub fn rndrss(&self) -> Option<u64> { |
| 72 | + let mut flags: u64; |
| 73 | + let mut data: u64; |
| 74 | + |
| 75 | + #[cfg(target_arch = "aarch64")] |
| 76 | + unsafe { |
| 77 | + asm!( |
| 78 | + "mrs {o}, s3_3_c2_c4_1", |
| 79 | + "mrs {f}, nzcv", |
| 80 | + o = out(reg) data, |
| 81 | + f = out(reg) flags, |
| 82 | + options(nomem, nostack)); |
| 83 | + } |
| 84 | + |
| 85 | + if cfg!(not(target_arch = "aarch64")) || flags != 0 { |
| 86 | + None |
| 87 | + } else { |
| 88 | + Some(data) |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + |
| 93 | +} |
| 94 | + |
| 95 | + |
| 96 | +#[cfg(all(test, target_os = "linux"))] |
| 97 | +mod tests { |
| 98 | + use super::*; |
| 99 | + |
| 100 | + #[test] |
| 101 | + pub fn test_rndr() { |
| 102 | + // This works on Linux from userspace since Linux emulatates |
| 103 | + // the Arm ID registers on the userspace undef. |
| 104 | + if let Some(rand) = ArmRng::new() { |
| 105 | + assert!(rand.rndr().unwrap() != 0); |
| 106 | + assert!(rand.rndrss().unwrap() != 0); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | +} |
| 111 | + |
0 commit comments