Pierre-Clément Tosi | 90cd4f1 | 2023-02-17 11:19:56 +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 | cb38e6d | 2023-06-22 10:51:00 +0000 | [diff] [blame] | 15 | //! Functions and drivers for obtaining true entropy. |
| 16 | |
Bartłomiej Grzesik | ee0a5c6 | 2025-02-04 09:06:17 +0000 | [diff] [blame] | 17 | use crate::arch::rand::{platform_entropy, PlatformError, MAX_BYTES_PER_CALL}; |
Pierre-Clément Tosi | a59103d | 2023-02-02 14:46:55 +0000 | [diff] [blame] | 18 | use core::fmt; |
Pierre-Clément Tosi | 9320e0e | 2023-07-11 15:51:55 +0000 | [diff] [blame] | 19 | |
Bartłomiej Grzesik | ee0a5c6 | 2025-02-04 09:06:17 +0000 | [diff] [blame] | 20 | pub(crate) type Entropy = [u8; MAX_BYTES_PER_CALL]; |
Pierre-Clément Tosi | a59103d | 2023-02-02 14:46:55 +0000 | [diff] [blame] | 21 | |
Pierre-Clément Tosi | cb38e6d | 2023-06-22 10:51:00 +0000 | [diff] [blame] | 22 | /// Error type for rand operations. |
Pierre-Clément Tosi | a59103d | 2023-02-02 14:46:55 +0000 | [diff] [blame] | 23 | pub enum Error { |
Pierre-Clément Tosi | 62ffc0d | 2023-06-30 09:31:56 +0000 | [diff] [blame] | 24 | /// No source of entropy found. |
| 25 | NoEntropySource, |
Bartłomiej Grzesik | ee0a5c6 | 2025-02-04 09:06:17 +0000 | [diff] [blame] | 26 | |
| 27 | /// Platform specific error |
| 28 | Platform(PlatformError), |
Pierre-Clément Tosi | a59103d | 2023-02-02 14:46:55 +0000 | [diff] [blame] | 29 | } |
| 30 | |
Bartłomiej Grzesik | ee0a5c6 | 2025-02-04 09:06:17 +0000 | [diff] [blame] | 31 | impl From<PlatformError> for Error { |
| 32 | fn from(e: PlatformError) -> Self { |
| 33 | Error::Platform(e) |
Pierre-Clément Tosi | a59103d | 2023-02-02 14:46:55 +0000 | [diff] [blame] | 34 | } |
| 35 | } |
| 36 | |
Pierre-Clément Tosi | cb38e6d | 2023-06-22 10:51:00 +0000 | [diff] [blame] | 37 | /// Result type for rand operations. |
Pierre-Clément Tosi | a59103d | 2023-02-02 14:46:55 +0000 | [diff] [blame] | 38 | pub type Result<T> = core::result::Result<T, Error>; |
| 39 | |
| 40 | impl fmt::Display for Error { |
| 41 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 42 | match self { |
Pierre-Clément Tosi | 62ffc0d | 2023-06-30 09:31:56 +0000 | [diff] [blame] | 43 | Self::NoEntropySource => write!(f, "No source of entropy available"), |
Bartłomiej Grzesik | ee0a5c6 | 2025-02-04 09:06:17 +0000 | [diff] [blame] | 44 | Self::Platform(e) => write!(f, "Platform error: {e}"), |
Pierre-Clément Tosi | a59103d | 2023-02-02 14:46:55 +0000 | [diff] [blame] | 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
Pierre-Clément Tosi | 78b6851 | 2023-06-22 09:40:16 +0000 | [diff] [blame] | 49 | impl fmt::Debug for Error { |
| 50 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 51 | write!(f, "{self}") |
| 52 | } |
| 53 | } |
| 54 | |
Pierre-Clément Tosi | 62ffc0d | 2023-06-30 09:31:56 +0000 | [diff] [blame] | 55 | /// Fills a slice of bytes with true entropy. |
| 56 | pub fn fill_with_entropy(s: &mut [u8]) -> Result<()> { |
Pierre-Clément Tosi | 9320e0e | 2023-07-11 15:51:55 +0000 | [diff] [blame] | 57 | for chunk in s.chunks_mut(MAX_BYTES_PER_CALL) { |
Bartłomiej Grzesik | ee0a5c6 | 2025-02-04 09:06:17 +0000 | [diff] [blame] | 58 | let entropy = platform_entropy(chunk.len())?; |
Pierre-Clément Tosi | 9320e0e | 2023-07-11 15:51:55 +0000 | [diff] [blame] | 59 | chunk.clone_from_slice(&entropy[..chunk.len()]); |
Pierre-Clément Tosi | a59103d | 2023-02-02 14:46:55 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | Ok(()) |
| 63 | } |
| 64 | |
Pierre-Clément Tosi | cb38e6d | 2023-06-22 10:51:00 +0000 | [diff] [blame] | 65 | /// Generate an array of fixed-size initialized with true-random bytes. |
Pierre-Clément Tosi | a59103d | 2023-02-02 14:46:55 +0000 | [diff] [blame] | 66 | pub fn random_array<const N: usize>() -> Result<[u8; N]> { |
| 67 | let mut arr = [0; N]; |
| 68 | fill_with_entropy(&mut arr)?; |
| 69 | Ok(arr) |
| 70 | } |