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 | |
| 15 | //! Wrapper around BoringSSL/OpenSSL symbols. |
| 16 | |
| 17 | use core::convert::AsRef; |
| 18 | use core::ffi::{c_char, c_int, CStr}; |
| 19 | use core::fmt; |
| 20 | use core::mem::MaybeUninit; |
| 21 | use core::num::NonZeroU32; |
| 22 | use core::ptr; |
| 23 | |
Pierre-Clément Tosi | 41748ed | 2023-03-31 18:20:40 +0100 | [diff] [blame] | 24 | use bssl_ffi::CRYPTO_library_init; |
Pierre-Clément Tosi | 90cd4f1 | 2023-02-17 11:19:56 +0000 | [diff] [blame] | 25 | use bssl_ffi::ERR_get_error_line; |
| 26 | use bssl_ffi::ERR_lib_error_string; |
| 27 | use bssl_ffi::ERR_reason_error_string; |
| 28 | use bssl_ffi::EVP_AEAD_CTX_aead; |
| 29 | use bssl_ffi::EVP_AEAD_CTX_init; |
| 30 | use bssl_ffi::EVP_AEAD_CTX_open; |
| 31 | use bssl_ffi::EVP_AEAD_CTX_seal; |
| 32 | use bssl_ffi::EVP_AEAD_max_overhead; |
| 33 | use bssl_ffi::EVP_aead_aes_256_gcm_randnonce; |
| 34 | use bssl_ffi::EVP_sha512; |
| 35 | use bssl_ffi::EVP_AEAD; |
| 36 | use bssl_ffi::EVP_AEAD_CTX; |
| 37 | use bssl_ffi::HKDF; |
Alice Wang | a397106 | 2023-06-13 11:48:53 +0000 | [diff] [blame] | 38 | use vmbase::cstr; |
Pierre-Clément Tosi | 90cd4f1 | 2023-02-17 11:19:56 +0000 | [diff] [blame] | 39 | |
| 40 | #[derive(Debug)] |
| 41 | pub struct Error { |
| 42 | packed: NonZeroU32, |
| 43 | file: Option<&'static CStr>, |
| 44 | line: c_int, |
| 45 | } |
| 46 | |
| 47 | impl Error { |
| 48 | fn get() -> Option<Self> { |
Andrew Walbran | 20bb4e4 | 2023-07-07 13:55:55 +0100 | [diff] [blame] | 49 | let mut file = ptr::null(); |
| 50 | let mut line = 0; |
| 51 | // SAFETY: The function writes to the provided pointers, which are valid because they come |
| 52 | // from references. It doesn't retain them after it returns. |
| 53 | let packed = unsafe { ERR_get_error_line(&mut file, &mut line) }; |
Pierre-Clément Tosi | 90cd4f1 | 2023-02-17 11:19:56 +0000 | [diff] [blame] | 54 | |
| 55 | let packed = packed.try_into().ok()?; |
Andrew Walbran | 20bb4e4 | 2023-07-07 13:55:55 +0100 | [diff] [blame] | 56 | // SAFETY: Any non-NULL result is expected to point to a global const C string. |
Pierre-Clément Tosi | 90cd4f1 | 2023-02-17 11:19:56 +0000 | [diff] [blame] | 57 | let file = unsafe { as_static_cstr(file) }; |
| 58 | |
| 59 | Some(Self { packed, file, line }) |
| 60 | } |
| 61 | |
| 62 | fn packed_value(&self) -> u32 { |
| 63 | self.packed.get() |
| 64 | } |
| 65 | |
| 66 | fn library_name(&self) -> Option<&'static CStr> { |
Andrew Walbran | 20bb4e4 | 2023-07-07 13:55:55 +0100 | [diff] [blame] | 67 | // SAFETY: Call to a pure function. |
Pierre-Clément Tosi | 90cd4f1 | 2023-02-17 11:19:56 +0000 | [diff] [blame] | 68 | let name = unsafe { ERR_lib_error_string(self.packed_value()) }; |
Andrew Walbran | 20bb4e4 | 2023-07-07 13:55:55 +0100 | [diff] [blame] | 69 | // SAFETY: Any non-NULL result is expected to point to a global const C string. |
Pierre-Clément Tosi | 90cd4f1 | 2023-02-17 11:19:56 +0000 | [diff] [blame] | 70 | unsafe { as_static_cstr(name) } |
| 71 | } |
| 72 | |
| 73 | fn reason(&self) -> Option<&'static CStr> { |
Andrew Walbran | 20bb4e4 | 2023-07-07 13:55:55 +0100 | [diff] [blame] | 74 | // SAFETY: Call to a pure function. |
Pierre-Clément Tosi | 90cd4f1 | 2023-02-17 11:19:56 +0000 | [diff] [blame] | 75 | let reason = unsafe { ERR_reason_error_string(self.packed_value()) }; |
Andrew Walbran | 20bb4e4 | 2023-07-07 13:55:55 +0100 | [diff] [blame] | 76 | // SAFETY: Any non-NULL result is expected to point to a global const C string. |
Pierre-Clément Tosi | 90cd4f1 | 2023-02-17 11:19:56 +0000 | [diff] [blame] | 77 | unsafe { as_static_cstr(reason) } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | impl fmt::Display for Error { |
| 82 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Pierre-Clément Tosi | 90cd4f1 | 2023-02-17 11:19:56 +0000 | [diff] [blame] | 83 | let packed = self.packed_value(); |
Jiyong Park | b87f330 | 2023-03-21 10:03:11 +0900 | [diff] [blame] | 84 | let library = self.library_name().unwrap_or(cstr!("{unknown library}")).to_str().unwrap(); |
| 85 | let reason = self.reason().unwrap_or(cstr!("{unknown reason}")).to_str().unwrap(); |
| 86 | let file = self.file.unwrap_or(cstr!("??")).to_str().unwrap(); |
Pierre-Clément Tosi | 90cd4f1 | 2023-02-17 11:19:56 +0000 | [diff] [blame] | 87 | let line = self.line; |
| 88 | |
| 89 | write!(f, "{file}:{line}: {library}: {reason} ({packed:#x})") |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | #[derive(Copy, Clone)] |
| 94 | pub struct ErrorIterator {} |
| 95 | |
| 96 | impl Iterator for ErrorIterator { |
| 97 | type Item = Error; |
| 98 | |
| 99 | fn next(&mut self) -> Option<Self::Item> { |
| 100 | Self::Item::get() |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | pub type Result<T> = core::result::Result<T, ErrorIterator>; |
| 105 | |
| 106 | #[repr(transparent)] |
| 107 | pub struct Aead(EVP_AEAD); |
| 108 | |
| 109 | impl Aead { |
| 110 | pub fn aes_256_gcm_randnonce() -> Option<&'static Self> { |
Andrew Walbran | 20bb4e4 | 2023-07-07 13:55:55 +0100 | [diff] [blame] | 111 | // SAFETY: Returned pointer is checked below. |
Pierre-Clément Tosi | 90cd4f1 | 2023-02-17 11:19:56 +0000 | [diff] [blame] | 112 | let aead = unsafe { EVP_aead_aes_256_gcm_randnonce() }; |
| 113 | if aead.is_null() { |
| 114 | None |
| 115 | } else { |
Andrew Walbran | 20bb4e4 | 2023-07-07 13:55:55 +0100 | [diff] [blame] | 116 | // SAFETY: We assume that the non-NULL value points to a valid and static EVP_AEAD. |
Pierre-Clément Tosi | 90cd4f1 | 2023-02-17 11:19:56 +0000 | [diff] [blame] | 117 | Some(unsafe { &*(aead as *const _) }) |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | pub fn max_overhead(&self) -> usize { |
Andrew Walbran | 20bb4e4 | 2023-07-07 13:55:55 +0100 | [diff] [blame] | 122 | // SAFETY: Function should only read from self. |
Pierre-Clément Tosi | 90cd4f1 | 2023-02-17 11:19:56 +0000 | [diff] [blame] | 123 | unsafe { EVP_AEAD_max_overhead(self.as_ref() as *const _) } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | #[repr(transparent)] |
| 128 | pub struct AeadCtx(EVP_AEAD_CTX); |
| 129 | |
| 130 | impl AeadCtx { |
| 131 | pub fn new_aes_256_gcm_randnonce(key: &[u8]) -> Result<Self> { |
| 132 | let aead = Aead::aes_256_gcm_randnonce().unwrap(); |
| 133 | |
| 134 | Self::new(aead, key) |
| 135 | } |
| 136 | |
| 137 | fn new(aead: &'static Aead, key: &[u8]) -> Result<Self> { |
| 138 | const DEFAULT_TAG_LENGTH: usize = 0; |
| 139 | let engine = ptr::null_mut(); // Use default implementation. |
| 140 | let mut ctx = MaybeUninit::zeroed(); |
Andrew Walbran | 20bb4e4 | 2023-07-07 13:55:55 +0100 | [diff] [blame] | 141 | // SAFETY: Initialize the EVP_AEAD_CTX with const pointers to the AEAD and key. |
Pierre-Clément Tosi | 90cd4f1 | 2023-02-17 11:19:56 +0000 | [diff] [blame] | 142 | let result = unsafe { |
| 143 | EVP_AEAD_CTX_init( |
| 144 | ctx.as_mut_ptr(), |
| 145 | aead.as_ref() as *const _, |
| 146 | key.as_ptr(), |
| 147 | key.len(), |
| 148 | DEFAULT_TAG_LENGTH, |
| 149 | engine, |
| 150 | ) |
| 151 | }; |
| 152 | |
| 153 | if result == 1 { |
Andrew Walbran | 20bb4e4 | 2023-07-07 13:55:55 +0100 | [diff] [blame] | 154 | // SAFETY: We assume that the non-NULL value points to a valid and static EVP_AEAD. |
Pierre-Clément Tosi | 90cd4f1 | 2023-02-17 11:19:56 +0000 | [diff] [blame] | 155 | Ok(Self(unsafe { ctx.assume_init() })) |
| 156 | } else { |
| 157 | Err(ErrorIterator {}) |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | pub fn aead(&self) -> Option<&'static Aead> { |
Andrew Walbran | 20bb4e4 | 2023-07-07 13:55:55 +0100 | [diff] [blame] | 162 | // SAFETY: The function should only read from self. |
Pierre-Clément Tosi | 90cd4f1 | 2023-02-17 11:19:56 +0000 | [diff] [blame] | 163 | let aead = unsafe { EVP_AEAD_CTX_aead(self.as_ref() as *const _) }; |
| 164 | if aead.is_null() { |
| 165 | None |
| 166 | } else { |
Andrew Walbran | 20bb4e4 | 2023-07-07 13:55:55 +0100 | [diff] [blame] | 167 | // SAFETY: We assume that the non-NULL value points to a valid and static EVP_AEAD. |
Pierre-Clément Tosi | 90cd4f1 | 2023-02-17 11:19:56 +0000 | [diff] [blame] | 168 | Some(unsafe { &*(aead as *const _) }) |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | pub fn open<'b>(&self, out: &'b mut [u8], data: &[u8]) -> Result<&'b mut [u8]> { |
| 173 | let nonce = ptr::null_mut(); |
| 174 | let nonce_len = 0; |
| 175 | let ad = ptr::null_mut(); |
| 176 | let ad_len = 0; |
| 177 | let mut out_len = MaybeUninit::uninit(); |
Andrew Walbran | 20bb4e4 | 2023-07-07 13:55:55 +0100 | [diff] [blame] | 178 | // SAFETY: The function should only read from self and write to out (at most the provided |
Pierre-Clément Tosi | 90cd4f1 | 2023-02-17 11:19:56 +0000 | [diff] [blame] | 179 | // number of bytes) and out_len while reading from data (at most the provided number of |
| 180 | // bytes), ignoring any NULL input. |
| 181 | let result = unsafe { |
| 182 | EVP_AEAD_CTX_open( |
| 183 | self.as_ref() as *const _, |
| 184 | out.as_mut_ptr(), |
| 185 | out_len.as_mut_ptr(), |
| 186 | out.len(), |
| 187 | nonce, |
| 188 | nonce_len, |
| 189 | data.as_ptr(), |
| 190 | data.len(), |
| 191 | ad, |
| 192 | ad_len, |
| 193 | ) |
| 194 | }; |
| 195 | |
| 196 | if result == 1 { |
Andrew Walbran | 20bb4e4 | 2023-07-07 13:55:55 +0100 | [diff] [blame] | 197 | // SAFETY: Any value written to out_len could be a valid usize. The value itself is |
Pierre-Clément Tosi | 90cd4f1 | 2023-02-17 11:19:56 +0000 | [diff] [blame] | 198 | // validated as being a proper slice length by panicking in the following indexing |
| 199 | // otherwise. |
| 200 | let out_len = unsafe { out_len.assume_init() }; |
| 201 | Ok(&mut out[..out_len]) |
| 202 | } else { |
| 203 | Err(ErrorIterator {}) |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | pub fn seal<'b>(&self, out: &'b mut [u8], data: &[u8]) -> Result<&'b mut [u8]> { |
| 208 | let nonce = ptr::null_mut(); |
| 209 | let nonce_len = 0; |
| 210 | let ad = ptr::null_mut(); |
| 211 | let ad_len = 0; |
| 212 | let mut out_len = MaybeUninit::uninit(); |
Andrew Walbran | 20bb4e4 | 2023-07-07 13:55:55 +0100 | [diff] [blame] | 213 | // SAFETY: The function should only read from self and write to out (at most the provided |
Pierre-Clément Tosi | 90cd4f1 | 2023-02-17 11:19:56 +0000 | [diff] [blame] | 214 | // number of bytes) while reading from data (at most the provided number of bytes), |
| 215 | // ignoring any NULL input. |
| 216 | let result = unsafe { |
| 217 | EVP_AEAD_CTX_seal( |
| 218 | self.as_ref() as *const _, |
| 219 | out.as_mut_ptr(), |
| 220 | out_len.as_mut_ptr(), |
| 221 | out.len(), |
| 222 | nonce, |
| 223 | nonce_len, |
| 224 | data.as_ptr(), |
| 225 | data.len(), |
| 226 | ad, |
| 227 | ad_len, |
| 228 | ) |
| 229 | }; |
| 230 | |
| 231 | if result == 1 { |
Andrew Walbran | 20bb4e4 | 2023-07-07 13:55:55 +0100 | [diff] [blame] | 232 | // SAFETY: Any value written to out_len could be a valid usize. The value itself is |
Pierre-Clément Tosi | 90cd4f1 | 2023-02-17 11:19:56 +0000 | [diff] [blame] | 233 | // validated as being a proper slice length by panicking in the following indexing |
| 234 | // otherwise. |
| 235 | let out_len = unsafe { out_len.assume_init() }; |
| 236 | Ok(&mut out[..out_len]) |
| 237 | } else { |
| 238 | Err(ErrorIterator {}) |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | /// Cast a C string pointer to a static non-mutable reference. |
| 244 | /// |
| 245 | /// # Safety |
| 246 | /// |
Alan Stokes | a0e4296 | 2023-04-14 17:59:50 +0100 | [diff] [blame] | 247 | /// The caller needs to ensure that the pointer is null or points to a valid C string and that the |
| 248 | /// C lifetime of the string is compatible with a static Rust lifetime. |
Pierre-Clément Tosi | 90cd4f1 | 2023-02-17 11:19:56 +0000 | [diff] [blame] | 249 | unsafe fn as_static_cstr(p: *const c_char) -> Option<&'static CStr> { |
| 250 | if p.is_null() { |
| 251 | None |
| 252 | } else { |
Alan Stokes | a0e4296 | 2023-04-14 17:59:50 +0100 | [diff] [blame] | 253 | // Safety: Safe given the requirements of this function. |
| 254 | Some(unsafe { CStr::from_ptr(p) }) |
Pierre-Clément Tosi | 90cd4f1 | 2023-02-17 11:19:56 +0000 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | |
| 258 | impl AsRef<EVP_AEAD> for Aead { |
| 259 | fn as_ref(&self) -> &EVP_AEAD { |
| 260 | &self.0 |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | impl AsRef<EVP_AEAD_CTX> for AeadCtx { |
| 265 | fn as_ref(&self) -> &EVP_AEAD_CTX { |
| 266 | &self.0 |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | pub fn hkdf_sh512<const N: usize>(secret: &[u8], salt: &[u8], info: &[u8]) -> Result<[u8; N]> { |
| 271 | let mut key = [0; N]; |
Andrew Walbran | 20bb4e4 | 2023-07-07 13:55:55 +0100 | [diff] [blame] | 272 | // SAFETY: The function shouldn't access any Rust variable and the returned value is accepted |
Pierre-Clément Tosi | 90cd4f1 | 2023-02-17 11:19:56 +0000 | [diff] [blame] | 273 | // as a potentially NULL pointer. |
| 274 | let digest = unsafe { EVP_sha512() }; |
| 275 | |
| 276 | assert!(!digest.is_null()); |
Andrew Walbran | 20bb4e4 | 2023-07-07 13:55:55 +0100 | [diff] [blame] | 277 | // SAFETY: Only reads from/writes to the provided slices and supports digest was checked not |
Pierre-Clément Tosi | 90cd4f1 | 2023-02-17 11:19:56 +0000 | [diff] [blame] | 278 | // be NULL. |
| 279 | let result = unsafe { |
| 280 | HKDF( |
| 281 | key.as_mut_ptr(), |
| 282 | key.len(), |
| 283 | digest, |
| 284 | secret.as_ptr(), |
| 285 | secret.len(), |
| 286 | salt.as_ptr(), |
| 287 | salt.len(), |
| 288 | info.as_ptr(), |
| 289 | info.len(), |
| 290 | ) |
| 291 | }; |
| 292 | |
| 293 | if result == 1 { |
| 294 | Ok(key) |
| 295 | } else { |
| 296 | Err(ErrorIterator {}) |
| 297 | } |
| 298 | } |
Pierre-Clément Tosi | 41748ed | 2023-03-31 18:20:40 +0100 | [diff] [blame] | 299 | |
| 300 | pub fn init() { |
Andrew Walbran | 20bb4e4 | 2023-07-07 13:55:55 +0100 | [diff] [blame] | 301 | // SAFETY: Configures the internal state of the library - may be called multiple times. |
Pierre-Clément Tosi | 41748ed | 2023-03-31 18:20:40 +0100 | [diff] [blame] | 302 | unsafe { CRYPTO_library_init() } |
| 303 | } |