* Move ffi type container to boring-additions

* Use boring::derive for EC and ED instead of own implementation
This commit is contained in:
Jan Rüth 2023-11-23 21:11:25 +01:00
commit aa74b45a0f
15 changed files with 417 additions and 410 deletions

View file

@ -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 {
@ -55,14 +60,11 @@ 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());
@ -70,12 +72,12 @@ impl Crypter {
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(),
}
@ -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,
@ -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(),
@ -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;

View file

@ -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()) }
}
}

View file

@ -0,0 +1,3 @@
mod types;
pub use types::*;

View file

@ -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()) }
}
}

View file

@ -0,0 +1,3 @@
mod types;
pub use types::*;

View file

@ -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()) }
}
}

View file

@ -1,2 +1,4 @@
pub mod aead;
pub mod evp;
pub(crate) mod helper;
pub mod hmac;