1 // 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 15 use core::fmt; 16 use core::result; 17 18 /// Standard SMCCC TRNG error values as described in DEN 0098 1.0 REL0. 19 #[derive(Debug, Clone, PartialEq)] 20 pub 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 33 impl fmt::Display for Error { fmt(&self, f: &mut fmt::Formatter) -> fmt::Result34 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 45 impl From<i64> for Error { from(value: i64) -> Self46 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 57 pub type Result<T> = result::Result<T, Error>; 58 59 /// A version of the SMCCC TRNG interface. 60 #[derive(Copy, Clone, Eq, Ord, PartialEq, PartialOrd)] 61 pub struct Version { 62 pub major: u16, 63 pub minor: u16, 64 } 65 66 impl fmt::Display for Version { fmt(&self, f: &mut fmt::Formatter) -> fmt::Result67 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 68 write!(f, "{}.{}", self.major, self.minor) 69 } 70 } 71 72 impl fmt::Debug for Version { fmt(&self, f: &mut fmt::Formatter) -> fmt::Result73 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 74 fmt::Display::fmt(self, f) 75 } 76 } 77 78 impl TryFrom<i32> for Version { 79 type Error = Error; 80 try_from(value: i32) -> core::result::Result<Self, Error>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 90 impl From<Version> for u32 { from(version: Version) -> Self91 fn from(version: Version) -> Self { 92 (u32::from(version.major) << 16) | u32::from(version.minor) 93 } 94 } 95