From 13e3b849298534f0e90350cf9bda0fef0a5777d3 Mon Sep 17 00:00:00 2001 From: Avril Date: Wed, 23 Jun 2021 18:09:53 +0100 Subject: [PATCH] added C FFI header --- build.rs | 1 + include/cc20.h | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/ffi.rs | 17 ++++++++++++----- 3 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 include/cc20.h diff --git a/build.rs b/build.rs index 6399463..6cb813f 100644 --- a/build.rs +++ b/build.rs @@ -1,5 +1,6 @@ extern crate rustc_version; + use rustc_version::{version, version_meta, Channel}; fn main() { diff --git a/include/cc20.h b/include/cc20.h new file mode 100644 index 0000000..c706c49 --- /dev/null +++ b/include/cc20.h @@ -0,0 +1,44 @@ +#ifndef _CC20_H +#define _CC20_H + +#define KEY_SIZE 32 +#define IV_SIZE 12 + +enum cc20_mode { + CC20_ENCRYPT, + CC20_DECRYPT, +}; + +typedef uint8_t cc20_key_t[KEY_SIZE]; +typedef uint8_t cc20_iv_t[IV_SIZE]; + +struct cc20_metadata { + FILE* backing; + cc20_key_t key; + cc20_iv_t iv; + enum cc20_mode mode; +}; + +typedef struct cc20_sink cc20_sink_t; + +int cc20_gen_meta(FILE* file, + const cc20_key_t* key, + const cc20_iv_t* iv, + enum cc20_mode mode, + struct cc20_metadata* restrict output); + +cc20_sink_t* cc20_gen_sink(const struct cc20_metadata* meta); +cc20_sink_t* cc20_gen_sink_full(FILE* file, + const cc20_key_t* key, + const cc20_iv_t* iv, + enum cc20_mode mode); +FILE* cc20_wrap(FILE* file, + const cc20_key_t* key, + const cc20_iv_t* iv, + enum cc20_mode mode); +int cc20_close_sink(cc20_sink_t* sink, + struct cc20_metadata* restrict meta); + +FILE* cc20_wrap_sink(cc20_sink_t* sink); + +#endif /* _CC20_H */ diff --git a/src/ffi.rs b/src/ffi.rs index 5ea18d1..1b697e1 100644 --- a/src/ffi.rs +++ b/src/ffi.rs @@ -8,6 +8,12 @@ use std::io::{ self, Write, }; +use key::{ + Key, + IV, +}; + +/* #[derive(Debug, Clone, PartialEq, Eq, Hash, Copy, PartialOrd, Ord, Default)] #[repr(C)] pub struct CKey([u8; key::KEY_SIZE]); @@ -15,6 +21,7 @@ pub struct CKey([u8; key::KEY_SIZE]); #[derive(Debug, Clone, PartialEq, Eq, Hash, Copy, PartialOrd, Ord, Default)] #[repr(C)] pub struct CIv([u8; key::IV_SIZE]); + */ /// Non-encrypted wrapper #[derive(Debug, Clone, PartialEq, Eq, Hash)] @@ -49,7 +56,7 @@ mod interop; mod error; pub use error::*; -#[no_mangle] pub unsafe extern "C" fn cc20_gen_meta(file: *mut libc::FILE, key: *const CKey, iv: *const CIv, mode: CMode, output: *mut CPassthrough) -> i32 +#[no_mangle] pub unsafe extern "C" fn cc20_gen_meta(file: *mut libc::FILE, key: *const Key, iv: *const IV, mode: CMode, output: *mut CPassthrough) -> i32 { no_unwind!({ if file.is_null() { @@ -59,8 +66,8 @@ pub use error::*; let iv = nullchk!(move iv); let write = CPassthrough { backing: file, - key: key::Key::from_bytes(key.0), - iv: key::IV::from_bytes(iv.0), + key, + iv, mode, }; nullchk!(output); @@ -86,7 +93,7 @@ pub use error::*; }).unwrap_or(ptr::null_mut()) } /// Create an encrypting `Sink` over a `FILE*` with these options. -#[no_mangle] pub unsafe extern "C" fn cc20_gen_sink_full(file: *mut libc::FILE, key: *const CKey, iv: *const CIv, mode: CMode) -> *mut CSink +#[no_mangle] pub unsafe extern "C" fn cc20_gen_sink_full(file: *mut libc::FILE, key: *const Key, iv: *const IV, mode: CMode) -> *mut CSink { let meta = { // No need to `no_unwind` this, `cc20_gen_meta` already does it, and nothing else here can panic. @@ -99,7 +106,7 @@ pub use error::*; cc20_gen_sink(&meta as *const _) } /// Create a wrapper `FILE*` that acts as a `Sink` when written to. -#[no_mangle] pub unsafe extern "C" fn cc20_wrap(file: *mut libc::FILE, key: *const CKey, iv: *const CIv, mode: CMode) -> *mut libc::FILE +#[no_mangle] pub unsafe extern "C" fn cc20_wrap(file: *mut libc::FILE, key: *const Key, iv: *const IV, mode: CMode) -> *mut libc::FILE { // No need to `no_unwind` this, nothing here can panic. let csink = cc20_gen_sink_full(file, key, iv, mode);