1#![allow(non_snake_case)]
5extern crate alloc;
6
7use super::*;
8use crate::params::*;
9use alloc::boxed::Box;
10use rand::rngs::OsRng;
11use wasm_bindgen::prelude::*;
12
13#[wasm_bindgen]
19pub fn keypair() -> Result<Keys, JsError> {
20 let mut rng = OsRng {};
21 match api::keypair(&mut rng) {
22 Ok(keys) => Ok(Keys {
23 pubkey: Box::new(keys.public),
24 secret: Box::new(keys.secret),
25 }),
26 Err(KyberLibError::RandomBytesGeneration) => {
27 Err(JsError::new("Error trying to fill random bytes"))
28 }
29 _ => Err(JsError::new("The keypair could not be generated")),
30 }
31}
32
33#[wasm_bindgen]
43pub fn encapsulate(pk: Box<[u8]>) -> Result<Kex, JsValue> {
44 if pk.len() != KYBER_PUBLIC_KEY_BYTES {
45 return Err(JsValue::null());
46 }
47
48 let mut rng = OsRng {};
49 match api::encapsulate(&pk, &mut rng) {
50 Ok(kex) => Ok(Kex {
51 ciphertext: Box::new(kex.0),
52 sharedSecret: Box::new(kex.1),
53 }),
54 Err(_) => Err(JsValue::null()),
55 }
56}
57
58#[wasm_bindgen]
69pub fn decapsulate(
70 ct: Box<[u8]>,
71 sk: Box<[u8]>,
72) -> Result<Box<[u8]>, JsValue> {
73 if ct.len() != KYBER_CIPHERTEXT_BYTES
74 || sk.len() != KYBER_SECRET_KEY_BYTES
75 {
76 return Err(JsValue::null());
77 }
78
79 match api::decapsulate(&ct, &sk) {
80 Ok(ss) => Ok(Box::new(ss)),
81 Err(_) => Err(JsValue::null()),
82 }
83}
84
85#[wasm_bindgen]
87#[derive(Debug)]
88pub struct Keys {
89 pubkey: Box<[u8]>,
90 secret: Box<[u8]>,
91}
92
93#[wasm_bindgen]
95#[derive(Debug)]
96pub struct Kex {
97 ciphertext: Box<[u8]>,
98 sharedSecret: Box<[u8]>,
99}
100
101#[wasm_bindgen]
102impl Keys {
103 #[wasm_bindgen(constructor)]
111 pub fn new() -> Result<Keys, JsError> {
112 keypair()
113 }
114
115 #[wasm_bindgen(getter)]
119 pub fn pubkey(&self) -> Box<[u8]> {
120 self.pubkey.clone()
121 }
122
123 #[wasm_bindgen(getter)]
127 pub fn secret(&self) -> Box<[u8]> {
128 self.secret.clone()
129 }
130}
131
132#[wasm_bindgen]
133impl Kex {
134 #[wasm_bindgen(constructor)]
144 pub fn new(public_key: Box<[u8]>) -> Self {
145 encapsulate(public_key).expect("Invalid Public Key Size")
146 }
147
148 #[wasm_bindgen(getter)]
152 pub fn ciphertext(&self) -> Box<[u8]> {
153 self.ciphertext.clone()
154 }
155
156 #[wasm_bindgen(getter)]
160 pub fn sharedSecret(&self) -> Box<[u8]> {
161 self.sharedSecret.clone()
162 }
163
164 #[wasm_bindgen(setter)]
170 pub fn set_ciphertext(&mut self, ciphertext: Box<[u8]>) {
171 self.ciphertext = ciphertext;
172 }
173
174 #[wasm_bindgen(setter)]
180 pub fn set_sharedSecret(&mut self, sharedSecret: Box<[u8]>) {
181 self.sharedSecret = sharedSecret;
182 }
183}
184
185#[wasm_bindgen]
187#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
188pub struct Params {
189 #[wasm_bindgen(readonly)]
191 pub publicKeyBytes: usize,
192 #[wasm_bindgen(readonly)]
194 pub secretKeyBytes: usize,
195 #[wasm_bindgen(readonly)]
197 pub ciphertextBytes: usize,
198 #[wasm_bindgen(readonly)]
200 pub sharedSecretBytes: usize,
201}
202
203#[wasm_bindgen]
204impl Params {
205 #[wasm_bindgen(getter)]
207 pub fn publicKeyBytes() -> usize {
208 KYBER_PUBLIC_KEY_BYTES
209 }
210
211 #[wasm_bindgen(getter)]
213 pub fn secretKeyBytes() -> usize {
214 KYBER_SECRET_KEY_BYTES
215 }
216
217 #[wasm_bindgen(getter)]
219 pub fn ciphertextBytes() -> usize {
220 KYBER_CIPHERTEXT_BYTES
221 }
222
223 #[wasm_bindgen(getter)]
225 pub fn sharedSecretBytes() -> usize {
226 KYBER_SHARED_SECRET_BYTES
227 }
228}