blob: d34769318d88c1e6e8e127bdb4c4ca57b3484a71 [file] [log] [blame]
Pierre-Clément Tosia59103d2023-02-02 14:46:55 +00001// Copyright 2023, 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
15use crate::smccc;
16use core::fmt;
17use core::result;
18
19/// Standard SMCCC TRNG error values as described in DEN 0098 1.0 REL0.
20#[derive(Debug, Clone)]
21pub enum Error {
22 /// The call is not supported by the implementation.
23 NotSupported,
24 /// One of the call parameters has a non-supported value.
25 InvalidParameter,
26 /// Call returned without the requested entropy bits.
27 NoEntropy,
28 /// Negative values indicate error.
29 Unknown(i64),
30 /// The call returned a positive value when 0 was expected.
31 Unexpected(u64),
32}
33
34impl fmt::Display for Error {
35 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
36 match self {
37 Self::NotSupported => write!(f, "SMCCC TRNG call not supported"),
38 Self::InvalidParameter => write!(f, "SMCCC TRNG call received non-supported value"),
39 Self::NoEntropy => write!(f, "SMCCC TRNG call returned no entropy"),
40 Self::Unexpected(v) => write!(f, "Unexpected SMCCC TRNG return value {} ({0:#x})", v),
41 Self::Unknown(e) => write!(f, "Unknown SMCCC TRNG return value {} ({0:#x})", e),
42 }
43 }
44}
45
46pub type Result<T> = result::Result<T, Error>;
47
48pub fn hvc64(function: u32, args: [u64; 17]) -> Result<[u64; 18]> {
49 let res = smccc::hvc64(function, args);
50 match res[0] as i64 {
51 ret if ret >= 0 => Ok(res),
52 -1 => Err(Error::NotSupported),
53 -2 => Err(Error::InvalidParameter),
54 -3 => Err(Error::NoEntropy),
55 ret => Err(Error::Unknown(ret)),
56 }
57}
58
59pub fn hvc64_expect_zero(function: u32, args: [u64; 17]) -> Result<[u64; 18]> {
60 let res = hvc64(function, args)?;
61 match res[0] {
62 0 => Ok(res),
63 v => Err(Error::Unexpected(v)),
64 }
65}