blob: efb86f678d16337c01550cb3e293711769df1d5a [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
Pierre-Clément Tosia59103d2023-02-02 14:46:55 +000015use core::fmt;
16use core::result;
Pierre-Clément Tosia59103d2023-02-02 14:46:55 +000017
18/// Standard SMCCC TRNG error values as described in DEN 0098 1.0 REL0.
Pierre-Clément Tosi62ffc0d2023-06-30 09:31:56 +000019#[derive(Debug, Clone, PartialEq)]
Pierre-Clément Tosia59103d2023-02-02 14:46:55 +000020pub enum Error {
21 /// The call is not supported by the implementation.
22 NotSupported,
23 /// One of the call parameters has a non-supported value.
24 InvalidParameter,
25 /// Call returned without the requested entropy bits.
26 NoEntropy,
27 /// Negative values indicate error.
28 Unknown(i64),
29 /// The call returned a positive value when 0 was expected.
30 Unexpected(u64),
31}
32
33impl fmt::Display for Error {
34 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
35 match self {
36 Self::NotSupported => write!(f, "SMCCC TRNG call not supported"),
37 Self::InvalidParameter => write!(f, "SMCCC TRNG call received non-supported value"),
38 Self::NoEntropy => write!(f, "SMCCC TRNG call returned no entropy"),
39 Self::Unexpected(v) => write!(f, "Unexpected SMCCC TRNG return value {} ({0:#x})", v),
40 Self::Unknown(e) => write!(f, "Unknown SMCCC TRNG return value {} ({0:#x})", e),
41 }
42 }
43}
44
Andrew Walbran9afab672023-04-17 14:26:23 +000045impl From<i64> for Error {
46 fn from(value: i64) -> Self {
47 match value {
48 -1 => Error::NotSupported,
49 -2 => Error::InvalidParameter,
50 -3 => Error::NoEntropy,
51 _ if value < 0 => Error::Unknown(value),
52 _ => Error::Unexpected(value as u64),
53 }
54 }
55}
56
Pierre-Clément Tosia59103d2023-02-02 14:46:55 +000057pub type Result<T> = result::Result<T, Error>;
Pierre-Clément Tosi628c4382023-06-30 18:15:37 +000058
59/// A version of the SMCCC TRNG interface.
60#[derive(Copy, Clone, Eq, Ord, PartialEq, PartialOrd)]
61pub struct Version {
62 pub major: u16,
63 pub minor: u16,
64}
65
66impl fmt::Display for Version {
67 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
68 write!(f, "{}.{}", self.major, self.minor)
69 }
70}
71
72impl fmt::Debug for Version {
73 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
74 fmt::Display::fmt(self, f)
75 }
76}
77
78impl TryFrom<i32> for Version {
79 type Error = Error;
80
81 fn try_from(value: i32) -> core::result::Result<Self, Error> {
82 if value < 0 {
83 Err((value as i64).into())
84 } else {
85 Ok(Self { major: (value >> 16) as u16, minor: value as u16 })
86 }
87 }
88}
89
90impl From<Version> for u32 {
91 fn from(version: Version) -> Self {
92 (u32::from(version.major) << 16) | u32::from(version.minor)
93 }
94}