Andrew Walbran | ba47d1d | 2022-12-14 15:21:44 +0000 | [diff] [blame] | 1 | // Copyright 2022, The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | //! Wrappers around calls to the hypervisor. |
| 16 | |
Pierre-Clément Tosi | a59103d | 2023-02-02 14:46:55 +0000 | [diff] [blame] | 17 | pub mod trng; |
Andrew Walbran | 9afab67 | 2023-04-17 14:26:23 +0000 | [diff] [blame] | 18 | use self::trng::Error; |
Andrew Walbran | f44f160 | 2023-05-30 14:59:19 +0000 | [diff] [blame] | 19 | use smccc::{ |
Andrew Walbran | 9afab67 | 2023-04-17 14:26:23 +0000 | [diff] [blame] | 20 | error::{positive_or_error_64, success_or_error_64}, |
| 21 | hvc64, |
| 22 | }; |
Pierre-Clément Tosi | a59103d | 2023-02-02 14:46:55 +0000 | [diff] [blame] | 23 | |
Pierre-Clément Tosi | a59103d | 2023-02-02 14:46:55 +0000 | [diff] [blame] | 24 | const ARM_SMCCC_TRNG_VERSION: u32 = 0x8400_0050; |
Pierre-Clément Tosi | a59103d | 2023-02-02 14:46:55 +0000 | [diff] [blame] | 25 | const ARM_SMCCC_TRNG_FEATURES: u32 = 0x8400_0051; |
| 26 | #[allow(dead_code)] |
| 27 | const ARM_SMCCC_TRNG_GET_UUID: u32 = 0x8400_0052; |
| 28 | #[allow(dead_code)] |
| 29 | const ARM_SMCCC_TRNG_RND32: u32 = 0x8400_0053; |
Pierre-Clément Tosi | 62ffc0d | 2023-06-30 09:31:56 +0000 | [diff] [blame] | 30 | pub const ARM_SMCCC_TRNG_RND64: u32 = 0xc400_0053; |
Pierre-Clément Tosi | a59103d | 2023-02-02 14:46:55 +0000 | [diff] [blame] | 31 | |
| 32 | /// Returns the (major, minor) version tuple, as defined by the SMCCC TRNG. |
Pierre-Clément Tosi | 628c438 | 2023-06-30 18:15:37 +0000 | [diff] [blame] | 33 | pub fn trng_version() -> trng::Result<trng::Version> { |
Pierre-Clément Tosi | a59103d | 2023-02-02 14:46:55 +0000 | [diff] [blame] | 34 | let args = [0u64; 17]; |
| 35 | |
Andrew Walbran | 9afab67 | 2023-04-17 14:26:23 +0000 | [diff] [blame] | 36 | let version = positive_or_error_64::<Error>(hvc64(ARM_SMCCC_TRNG_VERSION, args)[0])?; |
Pierre-Clément Tosi | 628c438 | 2023-06-30 18:15:37 +0000 | [diff] [blame] | 37 | (version as u32 as i32).try_into() |
Pierre-Clément Tosi | a59103d | 2023-02-02 14:46:55 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Pierre-Clément Tosi | 9320e0e | 2023-07-11 15:51:55 +0000 | [diff] [blame] | 40 | pub type TrngRng64Entropy = [u64; 3]; |
Pierre-Clément Tosi | a59103d | 2023-02-02 14:46:55 +0000 | [diff] [blame] | 41 | |
| 42 | pub fn trng_rnd64(nbits: u64) -> trng::Result<TrngRng64Entropy> { |
| 43 | let mut args = [0u64; 17]; |
| 44 | args[0] = nbits; |
| 45 | |
Andrew Walbran | 9afab67 | 2023-04-17 14:26:23 +0000 | [diff] [blame] | 46 | let regs = hvc64(ARM_SMCCC_TRNG_RND64, args); |
| 47 | success_or_error_64::<Error>(regs[0])?; |
Pierre-Clément Tosi | a59103d | 2023-02-02 14:46:55 +0000 | [diff] [blame] | 48 | |
Pierre-Clément Tosi | 9320e0e | 2023-07-11 15:51:55 +0000 | [diff] [blame] | 49 | Ok([regs[1], regs[2], regs[3]]) |
Pierre-Clément Tosi | a59103d | 2023-02-02 14:46:55 +0000 | [diff] [blame] | 50 | } |
Pierre-Clément Tosi | 62ffc0d | 2023-06-30 09:31:56 +0000 | [diff] [blame] | 51 | |
| 52 | pub fn trng_features(fid: u32) -> trng::Result<u64> { |
| 53 | let mut args = [0u64; 17]; |
| 54 | args[0] = fid as u64; |
| 55 | |
| 56 | positive_or_error_64::<Error>(hvc64(ARM_SMCCC_TRNG_FEATURES, args)[0]) |
| 57 | } |