added C FFI header

ffi
Avril 3 years ago
parent bf63998e73
commit 13e3b84929
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -1,5 +1,6 @@
extern crate rustc_version; extern crate rustc_version;
use rustc_version::{version, version_meta, Channel}; use rustc_version::{version, version_meta, Channel};
fn main() { fn main() {

@ -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 */

@ -8,6 +8,12 @@ use std::io::{
self, Write, self, Write,
}; };
use key::{
Key,
IV,
};
/*
#[derive(Debug, Clone, PartialEq, Eq, Hash, Copy, PartialOrd, Ord, Default)] #[derive(Debug, Clone, PartialEq, Eq, Hash, Copy, PartialOrd, Ord, Default)]
#[repr(C)] #[repr(C)]
pub struct CKey([u8; key::KEY_SIZE]); 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)] #[derive(Debug, Clone, PartialEq, Eq, Hash, Copy, PartialOrd, Ord, Default)]
#[repr(C)] #[repr(C)]
pub struct CIv([u8; key::IV_SIZE]); pub struct CIv([u8; key::IV_SIZE]);
*/
/// Non-encrypted wrapper /// Non-encrypted wrapper
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
@ -49,7 +56,7 @@ mod interop;
mod error; mod error;
pub use 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!({ no_unwind!({
if file.is_null() { if file.is_null() {
@ -59,8 +66,8 @@ pub use error::*;
let iv = nullchk!(move iv); let iv = nullchk!(move iv);
let write = CPassthrough { let write = CPassthrough {
backing: file, backing: file,
key: key::Key::from_bytes(key.0), key,
iv: key::IV::from_bytes(iv.0), iv,
mode, mode,
}; };
nullchk!(output); nullchk!(output);
@ -86,7 +93,7 @@ pub use error::*;
}).unwrap_or(ptr::null_mut()) }).unwrap_or(ptr::null_mut())
} }
/// Create an encrypting `Sink` over a `FILE*` with these options. /// 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 = { let meta = {
// No need to `no_unwind` this, `cc20_gen_meta` already does it, and nothing else here can panic. // 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 _) cc20_gen_sink(&meta as *const _)
} }
/// Create a wrapper `FILE*` that acts as a `Sink` when written to. /// 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. // No need to `no_unwind` this, nothing here can panic.
let csink = cc20_gen_sink_full(file, key, iv, mode); let csink = cc20_gen_sink_full(file, key, iv, mode);

Loading…
Cancel
Save