Skip to content

Commit a95f73a

Browse files
authored
Merge pull request #59 from Cryptographic-API-Services/#57-hpke-hybrid-encryption
#57 hpke hybrid encryption
2 parents 6d0409e + 4a69e12 commit a95f73a

File tree

4 files changed

+120
-1
lines changed

4 files changed

+120
-1
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ crate-type = ["dylib"]
1414

1515
[dependencies]
1616
libc = "0.2.146"
17-
cas-lib = "0.2.0"
17+
cas-lib = {path = "../cas-lib"}

src/hpke/mod.rs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
use std::ffi::c_uchar;
2+
3+
use cas_lib::hybrid::{cas_hybrid::CASHybrid, hpke::CASHPKE};
4+
use types::{HpkeDecrypt, HpkeEncrypt, HpkeKeyPair};
5+
6+
mod types;
7+
8+
#[no_mangle]
9+
pub extern "C" fn hpke_generate_keypair() -> HpkeKeyPair {
10+
let (mut private_key, mut public_key, mut info_str) = <CASHPKE as CASHybrid>::generate_key_pair();
11+
let private_key_capacity = private_key.capacity();
12+
private_key.reserve_exact(private_key_capacity);
13+
let public_key_capacity = public_key.capacity();
14+
public_key.reserve_exact(public_key_capacity);
15+
let info_str_capacity = info_str.capacity();
16+
info_str.reserve_exact(info_str_capacity);
17+
let return_result = HpkeKeyPair {
18+
private_key_ptr: private_key.as_mut_ptr(),
19+
private_key_ptr_length: private_key.len(),
20+
public_key_ptr: public_key.as_mut_ptr(),
21+
public_key_ptr_length: public_key.len(),
22+
info_str_ptr: info_str.as_mut_ptr(),
23+
info_str_ptr_length: info_str.len()
24+
};
25+
std::mem::forget(private_key);
26+
std::mem::forget(public_key);
27+
std::mem::forget(info_str);
28+
return_result
29+
}
30+
31+
#[no_mangle]
32+
pub extern "C" fn hpke_encrypt(
33+
plaintext: *const c_uchar,
34+
plaintext_length: usize,
35+
public_key: *const c_uchar,
36+
public_keylength: usize,
37+
info_str: *const c_uchar,
38+
info_str_length: usize,
39+
) -> HpkeEncrypt {
40+
let plaintext = unsafe { std::slice::from_raw_parts(plaintext, plaintext_length) }.to_vec();
41+
let public_key = unsafe { std::slice::from_raw_parts(public_key, public_keylength) }.to_vec();
42+
let info_str = unsafe { std::slice::from_raw_parts(info_str, info_str_length) }.to_vec();
43+
let (mut encapped_key, mut ciphertext, mut tag) = <CASHPKE as CASHybrid>::encrypt(plaintext, public_key, info_str);
44+
let encapped_key_capacity = encapped_key.capacity();
45+
encapped_key.reserve_exact(encapped_key_capacity);
46+
let ciphertext_capacity = ciphertext.capacity();
47+
ciphertext.reserve_exact(ciphertext_capacity);
48+
let tag_capacity = tag.capacity();
49+
tag.reserve_exact(tag_capacity);
50+
let return_result = HpkeEncrypt {
51+
encapped_key_ptr: encapped_key.as_mut_ptr(),
52+
encapped_key_ptr_length: encapped_key.len(),
53+
ciphertext_ptr: ciphertext.as_mut_ptr(),
54+
ciphertext_ptr_length: ciphertext.len(),
55+
tag_ptr: tag.as_mut_ptr(),
56+
tag_ptr_length: tag.len()
57+
};
58+
std::mem::forget(encapped_key);
59+
std::mem::forget(ciphertext);
60+
std::mem::forget(tag);
61+
return_result
62+
}
63+
64+
#[no_mangle]
65+
pub extern "C" fn hpke_decrypt(
66+
ciphertext: *const c_uchar,
67+
ciphertext_length: usize,
68+
private_key: *const c_uchar,
69+
private_keylength: usize,
70+
encapped_key: *const c_uchar,
71+
encapped_key_length: usize,
72+
tag: *const c_uchar,
73+
tag_length: usize,
74+
info_str: *const c_uchar,
75+
info_str_length: usize,
76+
) -> HpkeDecrypt {
77+
let ciphertext = unsafe { std::slice::from_raw_parts(ciphertext, ciphertext_length) }.to_vec();
78+
let private_key = unsafe { std::slice::from_raw_parts(private_key, private_keylength) }.to_vec();
79+
let encapped_key = unsafe { std::slice::from_raw_parts(encapped_key, encapped_key_length) }.to_vec();
80+
let tag = unsafe { std::slice::from_raw_parts(tag, tag_length)}.to_vec();
81+
let info_str = unsafe { std::slice::from_raw_parts(info_str, info_str_length) }.to_vec();
82+
let mut plaintext = <CASHPKE as CASHybrid>::decrypt(ciphertext, private_key, encapped_key, tag, info_str);
83+
let plaintext_capacity = plaintext.capacity();
84+
plaintext.reserve_exact(plaintext_capacity);
85+
let return_result = HpkeDecrypt {
86+
plaintext_ptr: plaintext.as_mut_ptr(),
87+
plaintext_ptr_length: plaintext.len()
88+
};
89+
std::mem::forget(plaintext);
90+
return_result
91+
}

src/hpke/types.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use std::ffi::c_uchar;
2+
3+
#[repr(C)]
4+
pub struct HpkeKeyPair {
5+
pub private_key_ptr: *mut c_uchar,
6+
pub private_key_ptr_length: usize,
7+
pub public_key_ptr: *mut c_uchar,
8+
pub public_key_ptr_length: usize,
9+
pub info_str_ptr: *mut c_uchar,
10+
pub info_str_ptr_length: usize
11+
}
12+
13+
#[repr(C)]
14+
pub struct HpkeEncrypt {
15+
pub encapped_key_ptr: *mut c_uchar,
16+
pub encapped_key_ptr_length: usize,
17+
pub ciphertext_ptr: *mut c_uchar,
18+
pub ciphertext_ptr_length: usize,
19+
pub tag_ptr: *mut c_uchar,
20+
pub tag_ptr_length: usize
21+
}
22+
23+
#[repr(C)]
24+
pub struct HpkeDecrypt {
25+
pub plaintext_ptr: *mut c_uchar,
26+
pub plaintext_ptr_length: usize
27+
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ mod sha;
99
mod x25519;
1010
mod ascon_aead;
1111
mod zstd;
12+
mod hpke;
1213

1314
pub mod password_hashers {
1415
pub mod argon2;

0 commit comments

Comments
 (0)