libpep 0.12.0

Library for polymorphic encryption and pseudonymization
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
//! WASM bindings for distributed client.

use crate::client::Client;
use crate::client::Distributed;
#[cfg(feature = "json")]
use crate::data::wasm::json::{WASMEncryptedPEPJSONValue, WASMPEPJSONValue};
#[cfg(feature = "long")]
use crate::data::wasm::long::{
    WASMLongAttribute, WASMLongEncryptedAttribute, WASMLongEncryptedPseudonym, WASMLongPseudonym,
};
#[cfg(feature = "long")]
use crate::data::wasm::records::{WASMLongRecord, WASMLongRecordEncrypted};
use crate::data::wasm::records::{WASMRecord, WASMRecordEncrypted};
use crate::data::wasm::simple::{
    WASMAttribute, WASMEncryptedAttribute, WASMEncryptedPseudonym, WASMPseudonym,
};
use crate::keys::distribution::{
    AttributeSessionKeyShare, BlindedGlobalKeys, PseudonymSessionKeyShare, SessionKeyShares,
};
use crate::keys::wasm::distribution::WASMBlindedGlobalKeys;
use crate::keys::wasm::types::WASMSessionKeys;
use crate::keys::wasm::{
    WASMAttributeSessionKeyShare, WASMPseudonymSessionKeyShare, WASMSessionKeyShares,
};
use derive_more::{Deref, From, Into};
use wasm_bindgen::prelude::*;

/// A PEP client.
#[derive(Clone, From, Into, Deref)]
#[wasm_bindgen(js_name = Client)]
pub struct WASMClient(Client);

#[wasm_bindgen(js_class = Client)]
impl WASMClient {
    #[wasm_bindgen(constructor)]
    pub fn new(
        blinded_global_keys: &WASMBlindedGlobalKeys,
        session_key_shares: Vec<WASMSessionKeyShares>,
    ) -> Self {
        let shares: Vec<SessionKeyShares> = session_key_shares
            .into_iter()
            .map(|x| SessionKeyShares {
                pseudonym: PseudonymSessionKeyShare(x.0.pseudonym.0),
                attribute: AttributeSessionKeyShare(x.0.attribute.0),
            })
            .collect();
        let blinded_keys = BlindedGlobalKeys {
            pseudonym: blinded_global_keys.0.pseudonym,
            attribute: blinded_global_keys.0.attribute,
        };
        Self(Client::from_shares(blinded_keys, &shares))
    }

    #[wasm_bindgen(js_name = restore)]
    pub fn wasm_restore(keys: &WASMSessionKeys) -> Self {
        Self(Client::restore((*keys).into()))
    }

    #[wasm_bindgen(js_name = dump)]
    pub fn wasm_dump(&self) -> WASMSessionKeys {
        (*self.dump()).into()
    }

    #[wasm_bindgen(js_name = updatePseudonymSessionSecretKey)]
    pub fn wasm_update_pseudonym_session_secret_key(
        &mut self,
        old_key_share: WASMPseudonymSessionKeyShare,
        new_key_share: WASMPseudonymSessionKeyShare,
    ) {
        self.0
            .update_session_secret_key(old_key_share.0, new_key_share.0);
    }

    #[wasm_bindgen(js_name = updateAttributeSessionSecretKey)]
    pub fn wasm_update_attribute_session_secret_key(
        &mut self,
        old_key_share: WASMAttributeSessionKeyShare,
        new_key_share: WASMAttributeSessionKeyShare,
    ) {
        self.0
            .update_session_secret_key(old_key_share.0, new_key_share.0);
    }

    #[wasm_bindgen(js_name = updateSessionSecretKeys)]
    pub fn wasm_update_session_secret_keys(
        &mut self,
        old_key_shares: WASMSessionKeyShares,
        new_key_shares: WASMSessionKeyShares,
    ) {
        self.0
            .update_session_secret_keys(old_key_shares.0, new_key_shares.0);
    }

    #[wasm_bindgen(js_name = decryptPseudonym)]
    #[cfg(feature = "elgamal3")]
    pub fn wasm_decrypt_pseudonym(
        &self,
        encrypted: &WASMEncryptedPseudonym,
    ) -> Option<WASMPseudonym> {
        self.decrypt(&encrypted.0).map(WASMPseudonym::from)
    }

    #[wasm_bindgen(js_name = decryptPseudonym)]
    #[cfg(not(feature = "elgamal3"))]
    pub fn wasm_decrypt_pseudonym(&self, encrypted: &WASMEncryptedPseudonym) -> WASMPseudonym {
        WASMPseudonym::from(self.decrypt(&encrypted.0))
    }

    #[wasm_bindgen(js_name = decryptData)]
    #[cfg(feature = "elgamal3")]
    pub fn wasm_decrypt_data(&self, encrypted: &WASMEncryptedAttribute) -> Option<WASMAttribute> {
        self.decrypt(&encrypted.0).map(WASMAttribute::from)
    }

    #[wasm_bindgen(js_name = decryptData)]
    #[cfg(not(feature = "elgamal3"))]
    pub fn wasm_decrypt_data(&self, encrypted: &WASMEncryptedAttribute) -> WASMAttribute {
        WASMAttribute::from(self.decrypt(&encrypted.0))
    }

    #[wasm_bindgen(js_name = encryptData)]
    pub fn wasm_encrypt_data(&self, message: &WASMAttribute) -> WASMEncryptedAttribute {
        let mut rng = rand::rng();
        WASMEncryptedAttribute::from(self.encrypt(&message.0, &mut rng))
    }

    #[wasm_bindgen(js_name = encryptPseudonym)]
    pub fn wasm_encrypt_pseudonym(&self, message: &WASMPseudonym) -> WASMEncryptedPseudonym {
        let mut rng = rand::rng();
        WASMEncryptedPseudonym(self.encrypt(&message.0, &mut rng))
    }

    #[cfg(feature = "long")]
    #[wasm_bindgen(js_name = encryptLongPseudonym)]
    pub fn wasm_encrypt_long_pseudonym(
        &self,
        message: &WASMLongPseudonym,
    ) -> WASMLongEncryptedPseudonym {
        let mut rng = rand::rng();
        WASMLongEncryptedPseudonym::from(self.encrypt(&message.0, &mut rng))
    }

    #[cfg(all(feature = "long", feature = "elgamal3"))]
    #[wasm_bindgen(js_name = decryptLongPseudonym)]
    pub fn wasm_decrypt_long_pseudonym(
        &self,
        encrypted: &WASMLongEncryptedPseudonym,
    ) -> Option<WASMLongPseudonym> {
        self.decrypt(&encrypted.0).map(WASMLongPseudonym::from)
    }

    #[cfg(all(feature = "long", not(feature = "elgamal3")))]
    #[wasm_bindgen(js_name = decryptLongPseudonym)]
    pub fn wasm_decrypt_long_pseudonym(
        &self,
        encrypted: &WASMLongEncryptedPseudonym,
    ) -> WASMLongPseudonym {
        WASMLongPseudonym::from(self.decrypt(&encrypted.0))
    }

    #[cfg(feature = "long")]
    #[wasm_bindgen(js_name = encryptLongData)]
    pub fn wasm_encrypt_long_data(
        &self,
        message: &WASMLongAttribute,
    ) -> WASMLongEncryptedAttribute {
        let mut rng = rand::rng();
        WASMLongEncryptedAttribute::from(self.encrypt(&message.0, &mut rng))
    }

    #[cfg(all(feature = "long", feature = "elgamal3"))]
    #[wasm_bindgen(js_name = decryptLongData)]
    pub fn wasm_decrypt_long_data(
        &self,
        encrypted: &WASMLongEncryptedAttribute,
    ) -> Option<WASMLongAttribute> {
        self.decrypt(&encrypted.0).map(WASMLongAttribute::from)
    }

    #[cfg(all(feature = "long", not(feature = "elgamal3")))]
    #[wasm_bindgen(js_name = decryptLongData)]
    pub fn wasm_decrypt_long_data(
        &self,
        encrypted: &WASMLongEncryptedAttribute,
    ) -> WASMLongAttribute {
        WASMLongAttribute::from(self.decrypt(&encrypted.0))
    }

    /// Encrypt a batch of attributes.
    #[cfg(feature = "batch")]
    #[wasm_bindgen(js_name = encryptDataBatch)]
    pub fn wasm_encrypt_data_batch(
        &self,
        messages: Vec<WASMAttribute>,
    ) -> Result<Vec<WASMEncryptedAttribute>, wasm_bindgen::JsValue> {
        let mut rng = rand::rng();
        let rust_messages: Vec<_> = messages.into_iter().map(|m| m.0).collect();
        let encrypted = self
            .0
            .encrypt_batch(&rust_messages, &mut rng)
            .map_err(|e| wasm_bindgen::JsValue::from_str(&format!("{}", e)))?;
        Ok(encrypted
            .into_iter()
            .map(WASMEncryptedAttribute::from)
            .collect())
    }

    /// Encrypt a batch of pseudonyms.
    #[cfg(feature = "batch")]
    #[wasm_bindgen(js_name = encryptPseudonymBatch)]
    pub fn wasm_encrypt_pseudonym_batch(
        &self,
        messages: Vec<WASMPseudonym>,
    ) -> Result<Vec<WASMEncryptedPseudonym>, wasm_bindgen::JsValue> {
        let mut rng = rand::rng();
        let rust_messages: Vec<_> = messages.into_iter().map(|m| m.0).collect();
        let encrypted = self
            .0
            .encrypt_batch(&rust_messages, &mut rng)
            .map_err(|e| wasm_bindgen::JsValue::from_str(&format!("{}", e)))?;
        Ok(encrypted.into_iter().map(WASMEncryptedPseudonym).collect())
    }

    /// Decrypt a batch of encrypted attributes.
    #[cfg(feature = "batch")]
    #[wasm_bindgen(js_name = decryptDataBatch)]
    pub fn wasm_decrypt_data_batch(
        &self,
        encrypted: Vec<WASMEncryptedAttribute>,
    ) -> Result<Vec<WASMAttribute>, wasm_bindgen::JsValue> {
        let rust_encrypted: Vec<_> = encrypted.into_iter().map(|e| e.0).collect();
        let decrypted = self
            .0
            .decrypt_batch(&rust_encrypted)
            .map_err(|e| wasm_bindgen::JsValue::from_str(&format!("{}", e)))?;
        Ok(decrypted.into_iter().map(WASMAttribute::from).collect())
    }

    /// Decrypt a batch of encrypted pseudonyms.
    #[cfg(feature = "batch")]
    #[wasm_bindgen(js_name = decryptPseudonymBatch)]
    pub fn wasm_decrypt_pseudonym_batch(
        &self,
        encrypted: Vec<WASMEncryptedPseudonym>,
    ) -> Result<Vec<WASMPseudonym>, wasm_bindgen::JsValue> {
        let rust_encrypted: Vec<_> = encrypted.into_iter().map(|e| e.0).collect();
        let decrypted = self
            .0
            .decrypt_batch(&rust_encrypted)
            .map_err(|e| wasm_bindgen::JsValue::from_str(&format!("{}", e)))?;
        Ok(decrypted.into_iter().map(WASMPseudonym::from).collect())
    }

    /// Encrypt a batch of long attributes.
    #[cfg(all(feature = "batch", feature = "long"))]
    #[wasm_bindgen(js_name = encryptLongDataBatch)]
    pub fn wasm_encrypt_long_data_batch(
        &self,
        messages: Vec<WASMLongAttribute>,
    ) -> Result<Vec<WASMLongEncryptedAttribute>, wasm_bindgen::JsValue> {
        let mut rng = rand::rng();
        let rust_messages: Vec<_> = messages.into_iter().map(|m| m.0).collect();
        let encrypted = self
            .0
            .encrypt_batch(&rust_messages, &mut rng)
            .map_err(|e| wasm_bindgen::JsValue::from_str(&format!("{}", e)))?;
        Ok(encrypted
            .into_iter()
            .map(WASMLongEncryptedAttribute::from)
            .collect())
    }

    /// Encrypt a batch of long pseudonyms.
    #[cfg(all(feature = "batch", feature = "long"))]
    #[wasm_bindgen(js_name = encryptLongPseudonymBatch)]
    pub fn wasm_encrypt_long_pseudonym_batch(
        &self,
        messages: Vec<WASMLongPseudonym>,
    ) -> Result<Vec<WASMLongEncryptedPseudonym>, wasm_bindgen::JsValue> {
        let mut rng = rand::rng();
        let rust_messages: Vec<_> = messages.into_iter().map(|m| m.0).collect();
        let encrypted = self
            .0
            .encrypt_batch(&rust_messages, &mut rng)
            .map_err(|e| wasm_bindgen::JsValue::from_str(&format!("{}", e)))?;
        Ok(encrypted
            .into_iter()
            .map(WASMLongEncryptedPseudonym::from)
            .collect())
    }

    /// Decrypt a batch of long encrypted attributes.
    #[cfg(all(feature = "batch", feature = "long"))]
    #[wasm_bindgen(js_name = decryptLongDataBatch)]
    pub fn wasm_decrypt_long_data_batch(
        &self,
        encrypted: Vec<WASMLongEncryptedAttribute>,
    ) -> Result<Vec<WASMLongAttribute>, wasm_bindgen::JsValue> {
        let rust_encrypted: Vec<_> = encrypted.into_iter().map(|e| e.0).collect();
        let decrypted = self
            .0
            .decrypt_batch(&rust_encrypted)
            .map_err(|e| wasm_bindgen::JsValue::from_str(&format!("{}", e)))?;
        Ok(decrypted.into_iter().map(WASMLongAttribute::from).collect())
    }

    /// Decrypt a batch of long encrypted pseudonyms.
    #[cfg(all(feature = "batch", feature = "long"))]
    #[wasm_bindgen(js_name = decryptLongPseudonymBatch)]
    pub fn wasm_decrypt_long_pseudonym_batch(
        &self,
        encrypted: Vec<WASMLongEncryptedPseudonym>,
    ) -> Result<Vec<WASMLongPseudonym>, wasm_bindgen::JsValue> {
        let rust_encrypted: Vec<_> = encrypted.into_iter().map(|e| e.0).collect();
        let decrypted = self
            .0
            .decrypt_batch(&rust_encrypted)
            .map_err(|e| wasm_bindgen::JsValue::from_str(&format!("{}", e)))?;
        Ok(decrypted.into_iter().map(WASMLongPseudonym::from).collect())
    }

    /// Encrypt a Record using session keys.
    #[wasm_bindgen(js_name = encryptRecord)]
    pub fn wasm_encrypt_record(&self, record: WASMRecord) -> WASMRecordEncrypted {
        let mut rng = rand::rng();
        use crate::data::records::Record;
        use crate::data::traits::Encryptable;
        let rust_record: Record = record.into();
        let encrypted = rust_record.encrypt(&self.0.keys, &mut rng);
        encrypted.into()
    }

    /// Decrypt an encrypted Record using session keys.
    #[cfg(feature = "elgamal3")]
    #[wasm_bindgen(js_name = decryptRecord)]
    pub fn wasm_decrypt_record(&self, encrypted: WASMRecordEncrypted) -> Option<WASMRecord> {
        use crate::data::records::EncryptedRecord;
        use crate::data::traits::Encrypted;
        let rust_encrypted: EncryptedRecord = encrypted.into();
        rust_encrypted.decrypt(&self.0.keys).map(|r| r.into())
    }

    /// Decrypt an encrypted Record using session keys.
    #[cfg(not(feature = "elgamal3"))]
    #[wasm_bindgen(js_name = decryptRecord)]
    pub fn wasm_decrypt_record(&self, encrypted: WASMRecordEncrypted) -> WASMRecord {
        use crate::data::records::EncryptedRecord;
        use crate::data::traits::Encrypted;
        let rust_encrypted: EncryptedRecord = encrypted.into();
        rust_encrypted.decrypt(&self.0.keys).into()
    }

    /// Encrypt a LongRecord using session keys.
    #[cfg(feature = "long")]
    #[wasm_bindgen(js_name = encryptLongRecord)]
    pub fn wasm_encrypt_long_record(&self, record: WASMLongRecord) -> WASMLongRecordEncrypted {
        let mut rng = rand::rng();
        use crate::data::records::LongRecord;
        use crate::data::traits::Encryptable;
        let rust_record: LongRecord = record.into();
        let encrypted = rust_record.encrypt(&self.0.keys, &mut rng);
        encrypted.into()
    }

    /// Decrypt an encrypted LongRecord using session keys.
    #[cfg(all(feature = "long", feature = "elgamal3"))]
    #[wasm_bindgen(js_name = decryptLongRecord)]
    pub fn wasm_decrypt_long_record(
        &self,
        encrypted: WASMLongRecordEncrypted,
    ) -> Option<WASMLongRecord> {
        use crate::data::records::LongEncryptedRecord;
        use crate::data::traits::Encrypted;
        let rust_encrypted: LongEncryptedRecord = encrypted.into();
        rust_encrypted.decrypt(&self.0.keys).map(|r| r.into())
    }

    /// Decrypt an encrypted LongRecord using session keys.
    #[cfg(all(feature = "long", not(feature = "elgamal3")))]
    #[wasm_bindgen(js_name = decryptLongRecord)]
    pub fn wasm_decrypt_long_record(&self, encrypted: WASMLongRecordEncrypted) -> WASMLongRecord {
        use crate::data::records::LongEncryptedRecord;
        use crate::data::traits::Encrypted;
        let rust_encrypted: LongEncryptedRecord = encrypted.into();
        rust_encrypted.decrypt(&self.0.keys).into()
    }

    /// Encrypt a PEPJSONValue using session keys.
    #[cfg(feature = "json")]
    #[wasm_bindgen(js_name = encryptJSON)]
    pub fn wasm_encrypt_json(&self, value: WASMPEPJSONValue) -> WASMEncryptedPEPJSONValue {
        let mut rng = rand::rng();
        use crate::data::traits::Encryptable;
        let rust_value = value.0;
        let encrypted = rust_value.encrypt(&self.0.keys, &mut rng);
        WASMEncryptedPEPJSONValue(encrypted)
    }

    /// Encrypt a PEPJSONValue using session keys.
    #[cfg(all(feature = "json", feature = "batch"))]
    #[wasm_bindgen(js_name = encryptJSONBatch)]
    pub fn wasm_encrypt_json_batch(
        &self,
        values: Vec<WASMPEPJSONValue>,
    ) -> Result<Vec<WASMEncryptedPEPJSONValue>, wasm_bindgen::JsValue> {
        let mut rng = rand::rng();
        let rust_values: Vec<_> = values.into_iter().map(|m| m.0).collect();
        let encrypted = self
            .0
            .encrypt_batch(&rust_values, &mut rng)
            .map_err(|e| wasm_bindgen::JsValue::from_str(&format!("{}", e)))?;
        Ok(encrypted
            .into_iter()
            .map(WASMEncryptedPEPJSONValue)
            .collect())
    }

    /// Decrypt an encrypted PEPJSONValue using session keys.
    #[cfg(all(feature = "json", feature = "elgamal3"))]
    #[wasm_bindgen(js_name = decryptJSON)]
    pub fn wasm_decrypt_json(
        &self,
        encrypted: WASMEncryptedPEPJSONValue,
    ) -> Option<WASMPEPJSONValue> {
        use crate::data::traits::Encrypted;
        encrypted.0.decrypt(&self.0.keys).map(WASMPEPJSONValue)
    }

    /// Decrypt an encrypted PEPJSONValue using session keys.
    #[cfg(all(feature = "json", not(feature = "elgamal3")))]
    #[wasm_bindgen(js_name = decryptJSON)]
    pub fn wasm_decrypt_json(&self, encrypted: WASMEncryptedPEPJSONValue) -> WASMPEPJSONValue {
        use crate::data::traits::Encrypted;
        WASMPEPJSONValue(encrypted.0.decrypt(&self.0.keys))
    }

    /// Decrypt a batch of encrypted PEPJSONValues using session keys.
    #[cfg(all(feature = "json", feature = "batch", feature = "elgamal3"))]
    #[wasm_bindgen(js_name = decryptJSONBatch)]
    pub fn wasm_decrypt_json_batch(
        &self,
        encrypted: Vec<WASMEncryptedPEPJSONValue>,
    ) -> Result<Vec<WASMPEPJSONValue>, wasm_bindgen::JsValue> {
        let rust_encrypted: Vec<_> = encrypted.into_iter().map(|e| e.0).collect();
        let decrypted = self
            .0
            .decrypt_batch(&rust_encrypted)
            .map_err(|e| wasm_bindgen::JsValue::from_str(&format!("{}", e)))?;
        Ok(decrypted.into_iter().map(WASMPEPJSONValue).collect())
    }

    /// Decrypt a batch of encrypted PEPJSONValues using session keys.
    #[cfg(all(feature = "json", feature = "batch", not(feature = "elgamal3")))]
    #[wasm_bindgen(js_name = decryptJSONBatch)]
    pub fn wasm_decrypt_json_batch(
        &self,
        encrypted: Vec<WASMEncryptedPEPJSONValue>,
    ) -> Result<Vec<WASMPEPJSONValue>, wasm_bindgen::JsValue> {
        let rust_encrypted: Vec<_> = encrypted.into_iter().map(|e| e.0).collect();
        let decrypted = self
            .0
            .decrypt_batch(&rust_encrypted)
            .map_err(|e| wasm_bindgen::JsValue::from_str(&format!("{}", e)))?;
        Ok(decrypted.into_iter().map(WASMPEPJSONValue).collect())
    }
}