blob: 6c5017f359dc43133c35948ebac96bd7c888a524 [file] [log] [blame]
Andrew Walbranba47d1d2022-12-14 15:21:44 +00001// 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 Tosia59103d2023-02-02 14:46:55 +000017pub mod trng;
18
Alice Wang0bdc3f62023-03-15 10:46:12 +000019// TODO(b/272226230): Move all the trng functions to trng module
Pierre-Clément Tosia59103d2023-02-02 14:46:55 +000020const ARM_SMCCC_TRNG_VERSION: u32 = 0x8400_0050;
21#[allow(dead_code)]
22const ARM_SMCCC_TRNG_FEATURES: u32 = 0x8400_0051;
23#[allow(dead_code)]
24const ARM_SMCCC_TRNG_GET_UUID: u32 = 0x8400_0052;
25#[allow(dead_code)]
26const ARM_SMCCC_TRNG_RND32: u32 = 0x8400_0053;
27const ARM_SMCCC_TRNG_RND64: u32 = 0xc400_0053;
Pierre-Clément Tosia59103d2023-02-02 14:46:55 +000028
29/// Returns the (major, minor) version tuple, as defined by the SMCCC TRNG.
30pub fn trng_version() -> trng::Result<(u16, u16)> {
31 let args = [0u64; 17];
32
33 let version = trng::hvc64(ARM_SMCCC_TRNG_VERSION, args)?[0];
34 Ok(((version >> 16) as u16, version as u16))
35}
36
37pub type TrngRng64Entropy = (u64, u64, u64);
38
39pub fn trng_rnd64(nbits: u64) -> trng::Result<TrngRng64Entropy> {
40 let mut args = [0u64; 17];
41 args[0] = nbits;
42
43 let regs = trng::hvc64_expect_zero(ARM_SMCCC_TRNG_RND64, args)?;
44
45 Ok((regs[1], regs[2], regs[3]))
46}