Alice Wang | 709cce9 | 2023-09-26 10:30:21 +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 | //! Wrappers of the digest functions in BoringSSL digest.h. |
| 16 | |
Alice Wang | 7468ae4 | 2023-11-30 10:20:36 +0000 | [diff] [blame^] | 17 | use crate::util::to_call_failed_error; |
| 18 | use bssl_avf_error::{ApiName, Result}; |
| 19 | use bssl_ffi::{ |
| 20 | EVP_MD_CTX_free, EVP_MD_CTX_new, EVP_MD_size, EVP_sha256, EVP_sha512, EVP_MD, EVP_MD_CTX, |
| 21 | }; |
| 22 | use core::ptr::NonNull; |
Alice Wang | 709cce9 | 2023-09-26 10:30:21 +0000 | [diff] [blame] | 23 | |
| 24 | /// Message digester wrapping `EVP_MD`. |
| 25 | #[derive(Clone, Debug)] |
| 26 | pub struct Digester(pub(crate) &'static EVP_MD); |
| 27 | |
| 28 | impl Digester { |
| 29 | /// Returns a `Digester` implementing `SHA-256` algorithm. |
| 30 | pub fn sha256() -> Self { |
| 31 | // SAFETY: This function does not access any Rust variables and simply returns |
| 32 | // a pointer to the static variable in BoringSSL. |
| 33 | let p = unsafe { EVP_sha256() }; |
| 34 | // SAFETY: The returned pointer should always be valid and points to a static |
| 35 | // `EVP_MD`. |
| 36 | Self(unsafe { &*p }) |
| 37 | } |
| 38 | |
| 39 | /// Returns a `Digester` implementing `SHA-512` algorithm. |
Alice Wang | 709cce9 | 2023-09-26 10:30:21 +0000 | [diff] [blame] | 40 | pub fn sha512() -> Self { |
| 41 | // SAFETY: This function does not access any Rust variables and simply returns |
| 42 | // a pointer to the static variable in BoringSSL. |
| 43 | let p = unsafe { EVP_sha512() }; |
| 44 | // SAFETY: The returned pointer should always be valid and points to a static |
| 45 | // `EVP_MD`. |
| 46 | Self(unsafe { &*p }) |
| 47 | } |
| 48 | |
| 49 | /// Returns the digest size in bytes. |
| 50 | pub fn size(&self) -> usize { |
| 51 | // SAFETY: The inner pointer is fetched from EVP_* hash functions in BoringSSL digest.h |
| 52 | unsafe { EVP_MD_size(self.0) } |
| 53 | } |
| 54 | } |
Alice Wang | 7468ae4 | 2023-11-30 10:20:36 +0000 | [diff] [blame^] | 55 | |
| 56 | /// Message digester context wrapping `EVP_MD_CTX`. |
| 57 | #[derive(Clone, Debug)] |
| 58 | pub struct DigesterContext(NonNull<EVP_MD_CTX>); |
| 59 | |
| 60 | impl Drop for DigesterContext { |
| 61 | fn drop(&mut self) { |
| 62 | // SAFETY: This function frees any resources owned by `EVP_MD_CTX` and resets it to a |
| 63 | // freshly initialised state and then frees the context. |
| 64 | // It is safe because `EVP_MD_CTX` has been allocated by BoringSSL and isn't used after |
| 65 | // this. |
| 66 | unsafe { EVP_MD_CTX_free(self.0.as_ptr()) } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | impl DigesterContext { |
| 71 | /// Creates a new `DigesterContext` wrapping a freshly allocated and initialised `EVP_MD_CTX`. |
| 72 | pub fn new() -> Result<Self> { |
| 73 | // SAFETY: The returned pointer is checked below. |
| 74 | let ctx = unsafe { EVP_MD_CTX_new() }; |
| 75 | NonNull::new(ctx).map(Self).ok_or(to_call_failed_error(ApiName::EVP_MD_CTX_new)) |
| 76 | } |
| 77 | |
| 78 | pub(crate) fn as_mut_ptr(&mut self) -> *mut EVP_MD_CTX { |
| 79 | self.0.as_ptr() |
| 80 | } |
| 81 | } |