Skip to content

Commit

Permalink
#57 hpke decrypt
Browse files Browse the repository at this point in the history
  • Loading branch information
WingZer0o committed Oct 13, 2024
1 parent 7183e54 commit 4a69e12
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/hpke/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::ffi::c_uchar;

use cas_lib::hybrid::{cas_hybrid::CASHybrid, hpke::CASHPKE};
use types::{HpkeEncrypt, HpkeKeyPair};
use types::{HpkeDecrypt, HpkeEncrypt, HpkeKeyPair};

mod types;

Expand Down Expand Up @@ -59,4 +59,33 @@ pub extern "C" fn hpke_encrypt(
std::mem::forget(ciphertext);
std::mem::forget(tag);
return_result
}

#[no_mangle]
pub extern "C" fn hpke_decrypt(
ciphertext: *const c_uchar,
ciphertext_length: usize,
private_key: *const c_uchar,
private_keylength: usize,
encapped_key: *const c_uchar,
encapped_key_length: usize,
tag: *const c_uchar,
tag_length: usize,
info_str: *const c_uchar,
info_str_length: usize,
) -> HpkeDecrypt {
let ciphertext = unsafe { std::slice::from_raw_parts(ciphertext, ciphertext_length) }.to_vec();
let private_key = unsafe { std::slice::from_raw_parts(private_key, private_keylength) }.to_vec();
let encapped_key = unsafe { std::slice::from_raw_parts(encapped_key, encapped_key_length) }.to_vec();
let tag = unsafe { std::slice::from_raw_parts(tag, tag_length)}.to_vec();
let info_str = unsafe { std::slice::from_raw_parts(info_str, info_str_length) }.to_vec();
let mut plaintext = <CASHPKE as CASHybrid>::decrypt(ciphertext, private_key, encapped_key, tag, info_str);
let plaintext_capacity = plaintext.capacity();
plaintext.reserve_exact(plaintext_capacity);
let return_result = HpkeDecrypt {
plaintext_ptr: plaintext.as_mut_ptr(),
plaintext_ptr_length: plaintext.len()
};
std::mem::forget(plaintext);
return_result
}
6 changes: 6 additions & 0 deletions src/hpke/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,10 @@ pub struct HpkeEncrypt {
pub ciphertext_ptr_length: usize,
pub tag_ptr: *mut c_uchar,
pub tag_ptr_length: usize
}

#[repr(C)]
pub struct HpkeDecrypt {
pub plaintext_ptr: *mut c_uchar,
pub plaintext_ptr_length: usize
}

0 comments on commit 4a69e12

Please sign in to comment.