Skip to content

Commit

Permalink
* Move ffi type container to boring-additions
Browse files Browse the repository at this point in the history
* Use boring::derive for EC and ED instead of own implementation
  • Loading branch information
Jan Rüth committed Nov 24, 2023
1 parent 319029f commit aa74b45
Show file tree
Hide file tree
Showing 15 changed files with 417 additions and 410 deletions.
26 changes: 10 additions & 16 deletions boring-additions/src/aead/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
use std::ptr;

use boring::error::ErrorStack;
use foreign_types::ForeignType;

mod types;

use crate::helper::{cvt, cvt_p};

pub use self::types::*;

pub struct Algorithm(*const boring_sys::EVP_AEAD);

impl Algorithm {
Expand Down Expand Up @@ -55,27 +60,24 @@ impl Algorithm {
}

pub struct Crypter {
ctx: *mut boring_sys::EVP_AEAD_CTX,
ctx: EvpAeadCtx,
max_overhead: usize,
nonce_len: usize,
}

unsafe impl Send for Crypter {}
unsafe impl Sync for Crypter {}

impl Crypter {
pub fn new(aead_alg: Algorithm, key: &[u8]) -> Result<Self, ErrorStack> {
assert_eq!(aead_alg.key_length(), key.len());
boring_sys::init();

let this = unsafe {
Self {
ctx: cvt_p(boring_sys::EVP_AEAD_CTX_new(
ctx: EvpAeadCtx::from_ptr(cvt_p(boring_sys::EVP_AEAD_CTX_new(
aead_alg.0,
key.as_ptr(),
key.len(),
boring_sys::EVP_AEAD_DEFAULT_TAG_LENGTH as usize,
))?,
))?),
max_overhead: aead_alg.max_overhead(),
nonce_len: aead_alg.nonce_len(),
}
Expand Down Expand Up @@ -104,7 +106,7 @@ impl Crypter {
let mut tag_len = tag.len();
unsafe {
cvt(boring_sys::EVP_AEAD_CTX_seal_scatter(
self.ctx,
self.ctx.as_ptr(),
buffer.as_mut_ptr(),
tag.as_mut_ptr(),
&mut tag_len,
Expand Down Expand Up @@ -133,7 +135,7 @@ impl Crypter {

unsafe {
cvt(boring_sys::EVP_AEAD_CTX_open_gather(
self.ctx,
self.ctx.as_ptr(),
buffer.as_mut_ptr(),
nonce.as_ptr(),
nonce.len(),
Expand All @@ -149,14 +151,6 @@ impl Crypter {
}
}

impl Drop for Crypter {
fn drop(&mut self) {
unsafe {
boring_sys::EVP_AEAD_CTX_free(self.ctx);
}
}
}

#[cfg(test)]
mod tests {
use super::Crypter;
Expand Down
56 changes: 56 additions & 0 deletions boring-additions/src/aead/types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use std::{
ops::{Deref, DerefMut},
ptr::NonNull,
};

use foreign_types::{ForeignType, ForeignTypeRef, Opaque};

pub struct EvpAeadCtxRef(Opaque);

unsafe impl ForeignTypeRef for EvpAeadCtxRef {
type CType = boring_sys::EVP_AEAD_CTX;
}

unsafe impl Sync for EvpAeadCtxRef {}
unsafe impl Send for EvpAeadCtxRef {}

pub struct EvpAeadCtx(NonNull<boring_sys::EVP_AEAD_CTX>);

unsafe impl Sync for EvpAeadCtx {}
unsafe impl Send for EvpAeadCtx {}

unsafe impl ForeignType for EvpAeadCtx {
type CType = boring_sys::EVP_AEAD_CTX;

type Ref = EvpAeadCtxRef;

unsafe fn from_ptr(ptr: *mut Self::CType) -> Self {
Self(NonNull::new_unchecked(ptr))
}

fn as_ptr(&self) -> *mut Self::CType {
self.0.as_ptr()
}
}

impl Drop for EvpAeadCtx {
fn drop(&mut self) {
unsafe {
boring_sys::EVP_AEAD_CTX_free(self.0.as_ptr());
}
}
}

impl Deref for EvpAeadCtx {
type Target = EvpAeadCtxRef;

fn deref(&self) -> &EvpAeadCtxRef {
unsafe { EvpAeadCtxRef::from_ptr(self.as_ptr()) }
}
}

impl DerefMut for EvpAeadCtx {
fn deref_mut(&mut self) -> &mut EvpAeadCtxRef {
unsafe { EvpAeadCtxRef::from_ptr_mut(self.as_ptr()) }
}
}
3 changes: 3 additions & 0 deletions boring-additions/src/evp/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod types;

pub use types::*;
60 changes: 60 additions & 0 deletions boring-additions/src/evp/types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use std::{
ops::{Deref, DerefMut},
ptr::NonNull,
};

use foreign_types::{ForeignType, ForeignTypeRef, Opaque};

pub struct EvpPkeyCtxRef(Opaque);

unsafe impl ForeignTypeRef for EvpPkeyCtxRef {
type CType = boring_sys::EVP_PKEY_CTX;
}

unsafe impl Sync for EvpPkeyCtxRef {}
unsafe impl Send for EvpPkeyCtxRef {}

unsafe impl Sync for EvpPkeyCtx {}
unsafe impl Send for EvpPkeyCtx {}

pub struct EvpPkeyCtx(NonNull<boring_sys::EVP_PKEY_CTX>);
unsafe impl ForeignType for EvpPkeyCtx {
type CType = boring_sys::EVP_PKEY_CTX;

type Ref = EvpPkeyCtxRef;

unsafe fn from_ptr(ptr: *mut Self::CType) -> Self {
Self(NonNull::new_unchecked(ptr))
}

fn as_ptr(&self) -> *mut Self::CType {
self.0.as_ptr()
}
}
impl Drop for EvpPkeyCtx {
fn drop(&mut self) {
unsafe {
boring_sys::EVP_PKEY_CTX_free(self.0.as_ptr());
}
}
}

impl core::fmt::Debug for EvpPkeyCtx {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("EvpPkeyCtx").field(&self.0).finish()
}
}

impl Deref for EvpPkeyCtx {
type Target = EvpPkeyCtxRef;

fn deref(&self) -> &EvpPkeyCtxRef {
unsafe { EvpPkeyCtxRef::from_ptr(self.as_ptr()) }
}
}

impl DerefMut for EvpPkeyCtx {
fn deref_mut(&mut self) -> &mut EvpPkeyCtxRef {
unsafe { EvpPkeyCtxRef::from_ptr_mut(self.as_ptr()) }
}
}
3 changes: 3 additions & 0 deletions boring-additions/src/hmac/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod types;

pub use types::*;
69 changes: 69 additions & 0 deletions boring-additions/src/hmac/types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use std::{
ops::{Deref, DerefMut},
ptr::NonNull,
};

use foreign_types::{ForeignType, ForeignTypeRef, Opaque};

use crate::helper::{cvt, cvt_p};

pub struct HmacCtxRef(Opaque);

unsafe impl ForeignTypeRef for HmacCtxRef {
type CType = boring_sys::HMAC_CTX;
}

unsafe impl Sync for HmacCtxRef {}
unsafe impl Send for HmacCtxRef {}

pub struct HmacCtx(NonNull<boring_sys::HMAC_CTX>);

unsafe impl Sync for HmacCtx {}
unsafe impl Send for HmacCtx {}

unsafe impl ForeignType for HmacCtx {
type CType = boring_sys::HMAC_CTX;

type Ref = HmacCtxRef;

unsafe fn from_ptr(ptr: *mut Self::CType) -> Self {
Self(NonNull::new_unchecked(ptr))
}

fn as_ptr(&self) -> *mut Self::CType {
self.0.as_ptr()
}
}

impl Clone for HmacCtx {
fn clone(&self) -> Self {
unsafe {
let ctx = HmacCtx::from_ptr(cvt_p(boring_sys::HMAC_CTX_new()).unwrap());

cvt(boring_sys::HMAC_CTX_copy(ctx.as_ptr(), self.0.as_ptr())).unwrap();
ctx
}
}
}

impl Drop for HmacCtx {
fn drop(&mut self) {
unsafe {
boring_sys::HMAC_CTX_free(self.0.as_ptr());
}
}
}

impl Deref for HmacCtx {
type Target = HmacCtxRef;

fn deref(&self) -> &HmacCtxRef {
unsafe { Self::Target::from_ptr(self.as_ptr()) }
}
}

impl DerefMut for HmacCtx {
fn deref_mut(&mut self) -> &mut HmacCtxRef {
unsafe { HmacCtxRef::from_ptr_mut(self.as_ptr()) }
}
}
2 changes: 2 additions & 0 deletions boring-additions/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
pub mod aead;
pub mod evp;
pub(crate) mod helper;
pub mod hmac;
6 changes: 0 additions & 6 deletions boring-rustls-provider/src/aead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ pub(crate) struct BoringAeadCrypter<T: BoringAead> {
phantom: PhantomData<T>,
}

unsafe impl<T: BoringAead> Sync for BoringAeadCrypter<T> {}
unsafe impl<T: BoringAead> Send for BoringAeadCrypter<T> {}

impl<T: BoringAead> AeadCore for BoringAeadCrypter<T> {
// inherit all properties from the Algorithm

Expand Down Expand Up @@ -185,9 +182,6 @@ where

pub(crate) struct Aead<T: BoringCipher>(PhantomData<T>);

unsafe impl<T: BoringCipher> Sync for Aead<T> {}
unsafe impl<T: BoringCipher> Send for Aead<T> {}

impl<T: BoringCipher> Aead<T> {
pub const DEFAULT: Self = Self(PhantomData);
}
Expand Down
Loading

0 comments on commit aa74b45

Please sign in to comment.