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
17 /// TRNG ARM specific module
18 pub mod trng;
19 use self::trng::Error;
20 use smccc::{
21 error::{positive_or_error_64, success_or_error_64},
22 hvc64,
23 };
24
25 /// ARM HVC call number that will return TRNG version
26 const ARM_SMCCC_TRNG_VERSION: u32 = 0x8400_0050;
27 /// ARM TRNG feature hypercall number
28 const ARM_SMCCC_TRNG_FEATURES: u32 = 0x8400_0051;
29 #[allow(dead_code)]
30 /// ARM SMCC TRNG get uuid hypercall number
31 const ARM_SMCCC_TRNG_GET_UUID: u32 = 0x8400_0052;
32 #[allow(dead_code)]
33 /// ARM SMCC TRNG 32BIT random hypercall number
34 const ARM_SMCCC_TRNG_RND32: u32 = 0x8400_0053;
35
36 /// ARM SMCCC 64BIT random hypercall number
37 pub const ARM_SMCCC_TRNG_RND64: u32 = 0xc400_0053;
38
39 /// Returns the (major, minor) version tuple, as defined by the SMCCC TRNG.
trng_version() -> trng::Result<trng::Version>40 pub fn trng_version() -> trng::Result<trng::Version> {
41 let args = [0u64; 17];
42
43 let version = positive_or_error_64::<Error>(hvc64(ARM_SMCCC_TRNG_VERSION, args)[0])?;
44 (version as u32 as i32).try_into()
45 }
46
47 /// Buffer for random number
48 pub type TrngRng64Entropy = [u64; 3];
49
50 /// Return hardware backed entropy from TRNG
trng_rnd64(nbits: u64) -> trng::Result<TrngRng64Entropy>51 pub fn trng_rnd64(nbits: u64) -> trng::Result<TrngRng64Entropy> {
52 let mut args = [0u64; 17];
53 args[0] = nbits;
54
55 let regs = hvc64(ARM_SMCCC_TRNG_RND64, args);
56 success_or_error_64::<Error>(regs[0])?;
57
58 Ok([regs[1], regs[2], regs[3]])
59 }
60
61 /// Return TRNG feature
trng_features(fid: u32) -> trng::Result<u64>62 pub fn trng_features(fid: u32) -> trng::Result<u64> {
63 let mut args = [0u64; 17];
64 args[0] = fid as u64;
65
66 positive_or_error_64::<Error>(hvc64(ARM_SMCCC_TRNG_FEATURES, args)[0])
67 }
68