Skip to content

Commit 31a992b

Browse files
committed
use wasm_bindgen(getter_with_clone)
1 parent ad599d5 commit 31a992b

File tree

2 files changed

+32
-82
lines changed

2 files changed

+32
-82
lines changed

src/ciphertext.rs

+11-40
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,18 @@
11
#[cfg(target_arch = "wasm32")]
22
use wasm_bindgen::prelude::*;
33

4-
cfg_if::cfg_if! {
5-
if #[cfg(target_arch = "wasm32")] {
6-
/**
7-
a simple struct to return the combined encapsulated key
8-
and ciphertext from seal
9-
*/
10-
#[wasm_bindgen]
11-
#[derive(Debug, Clone, PartialEq, Eq)]
12-
pub struct EncappedKeyAndCiphertext {
13-
pub(crate) encapped_key: Vec<u8>,
14-
pub(crate) ciphertext: Vec<u8>
15-
}
4+
/**
5+
a simple struct to return the combined encapsulated key
6+
and ciphertext from seal
7+
*/
8+
#[cfg_attr(target_arch = "wasm32", wasm_bindgen(getter_with_clone))]
9+
#[derive(Debug, Clone, PartialEq, Eq)]
10+
pub struct EncappedKeyAndCiphertext {
11+
/// the encapsulated encryption key
12+
pub encapped_key: Vec<u8>,
1613

17-
#[wasm_bindgen]
18-
impl EncappedKeyAndCiphertext {
19-
/// getter for encapped_key
20-
#[wasm_bindgen(getter)]
21-
pub fn encapped_key(&self) -> Vec<u8> {
22-
self.encapped_key.clone()
23-
}
24-
25-
/// getter for ciphertext
26-
#[wasm_bindgen(getter)]
27-
pub fn ciphertext(&self) -> Vec<u8> {
28-
self.ciphertext.clone()
29-
}
30-
}
31-
32-
} else {
33-
/**
34-
a simple open struct to return the combined encapsulated key
35-
and ciphertext from seal
36-
*/
37-
#[derive(Debug, Clone, PartialEq, Eq)]
38-
pub struct EncappedKeyAndCiphertext {
39-
/// the encapsulated encryption key
40-
pub encapped_key: Vec<u8>,
41-
/// the ciphertext, encrypted with the encapsulated key
42-
pub ciphertext: Vec<u8>
43-
}
44-
}
14+
/// the ciphertext, encrypted with the key
15+
pub ciphertext: Vec<u8>,
4516
}
4617

4718
impl EncappedKeyAndCiphertext {

src/keypair.rs

+21-42
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,25 @@ use wasm_bindgen::prelude::*;
44
use crate::Kem;
55
use hpke::Serializable;
66

7-
cfg_if::cfg_if! {
8-
if #[cfg(target_arch = "wasm32")] {
9-
/// An encoded keypair
10-
#[derive(Debug, Clone, Eq, PartialEq)]
11-
#[wasm_bindgen]
12-
pub struct Keypair {
13-
public_key: Vec<u8>,
14-
private_key: Vec<u8>,
15-
}
16-
} else {
17-
/// An encoded keypair
18-
#[derive(Debug, Clone, Eq, PartialEq, zeroize::Zeroize)]
19-
pub struct Keypair {
20-
/// the public key for this keypair
21-
pub public_key: Vec<u8>,
22-
/// the private key for this keypair,
23-
pub private_key: Vec<u8>,
24-
}
25-
}
7+
/// An encoded keypair
8+
#[derive(Debug, Clone, Eq, PartialEq, zeroize::Zeroize)]
9+
#[cfg_attr(target_arch = "wasm32", wasm_bindgen(getter_with_clone))]
10+
pub struct Keypair {
11+
/// the public key for this keypair
12+
pub public_key: Vec<u8>,
13+
14+
/// the private key for this keypair,
15+
pub private_key: Vec<u8>,
2616
}
2717

2818
#[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
2919
impl Keypair {
3020
/// generate a keypair from a [`Kem`]
3121
#[cfg_attr(target_arch = "wasm32", wasm_bindgen(constructor))]
3222
#[must_use]
33-
pub fn generate(kem: Kem) -> Keypair {
23+
pub fn new(kem: Kem) -> Keypair {
3424
gen_keypair(kem)
3525
}
36-
37-
/// getter for `public_key`
38-
#[cfg_attr(target_arch = "wasm32", wasm_bindgen(getter))]
39-
pub fn public_key(&self) -> Vec<u8> {
40-
self.public_key.clone()
41-
}
42-
43-
/// getter for `private_key`
44-
#[cfg_attr(target_arch = "wasm32", wasm_bindgen(getter))]
45-
pub fn private_key(&self) -> Vec<u8> {
46-
self.private_key.clone()
47-
}
4826
}
4927

5028
impl Keypair {
@@ -58,16 +36,6 @@ impl Keypair {
5836
/// generate a Keypair for the provided asymmetric key encapsulation mechanism ([`Kem`])
5937
#[must_use]
6038
pub fn gen_keypair(kem: Kem) -> Keypair {
61-
fn gen_kp<KemT: hpke::kem::Kem>() -> Keypair {
62-
let (private_key, public_key) = KemT::gen_keypair(&mut rand::thread_rng());
63-
let public_key = public_key.to_bytes().to_vec();
64-
let private_key = private_key.to_bytes().to_vec();
65-
Keypair {
66-
public_key,
67-
private_key,
68-
}
69-
}
70-
7139
match kem {
7240
#[cfg(feature = "kem-dh-p256-hkdf-sha256")]
7341
Kem::DhP256HkdfSha256 => gen_kp::<hpke::kem::DhP256HkdfSha256>(),
@@ -76,3 +44,14 @@ pub fn gen_keypair(kem: Kem) -> Keypair {
7644
Kem::X25519HkdfSha256 => gen_kp::<hpke::kem::X25519HkdfSha256>(),
7745
}
7846
}
47+
48+
fn gen_kp<KemT: hpke::kem::Kem>() -> Keypair {
49+
let (private_key, public_key) = KemT::gen_keypair(&mut rand::thread_rng());
50+
let public_key = public_key.to_bytes().to_vec();
51+
let private_key = private_key.to_bytes().to_vec();
52+
53+
Keypair {
54+
public_key,
55+
private_key,
56+
}
57+
}

0 commit comments

Comments
 (0)