Pierre-Clément Tosi | a59103d | 2023-02-02 14:46:55 +0000 | [diff] [blame] | 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 | |
Pierre-Clément Tosi | a59103d | 2023-02-02 14:46:55 +0000 | [diff] [blame] | 15 | use core::fmt; |
| 16 | use core::result; |
Pierre-Clément Tosi | a59103d | 2023-02-02 14:46:55 +0000 | [diff] [blame] | 17 | |
| 18 | /// Standard SMCCC TRNG error values as described in DEN 0098 1.0 REL0. |
| 19 | #[derive(Debug, Clone)] |
| 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 { |
| 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 Walbran | 9afab67 | 2023-04-17 14:26:23 +0000 | [diff] [blame] | 45 | impl 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 Tosi | a59103d | 2023-02-02 14:46:55 +0000 | [diff] [blame] | 57 | pub type Result<T> = result::Result<T, Error>; |