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 /// Local result alias 58 pub type Result<T> = result::Result<T, Error>; 59 60 /// A version of the SMCCC TRNG interface. 61 #[derive(Copy, Clone, Eq, Ord, PartialEq, PartialOrd)] 62 pub struct Version { 63 /// Version majon number 64 pub major: u16, 65 /// Version minor number 66 pub minor: u16, 67 } 68 69 impl fmt::Display for Version { fmt(&self, f: &mut fmt::Formatter) -> fmt::Result70 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 71 write!(f, "{}.{}", self.major, self.minor) 72 } 73 } 74 75 impl fmt::Debug for Version { fmt(&self, f: &mut fmt::Formatter) -> fmt::Result76 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 77 fmt::Display::fmt(self, f) 78 } 79 } 80 81 impl TryFrom<i32> for Version { 82 type Error = Error; 83 try_from(value: i32) -> core::result::Result<Self, Error>84 fn try_from(value: i32) -> core::result::Result<Self, Error> { 85 if value < 0 { 86 Err((value as i64).into()) 87 } else { 88 Ok(Self { major: (value >> 16) as u16, minor: value as u16 }) 89 } 90 } 91 } 92 93 impl From<Version> for u32 { from(version: Version) -> Self94 fn from(version: Version) -> Self { 95 (u32::from(version.major) << 16) | u32::from(version.minor) 96 } 97 } 98