pqc-binary-format 1.0.14

Standardized binary format for post-quantum cryptography encrypted data interchange
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
467
468
469
470
471
472
473
//! WebAssembly bindings for PQC Binary Format
//!
//! This module provides WASM bindings for use in JavaScript/TypeScript environments.

use std::collections::HashMap;
use wasm_bindgen::prelude::*;

use crate::{
    Algorithm, CompressionParameters, EncParameters, FormatFlags, KemParameters, PqcBinaryFormat,
    PqcMetadata, SigParameters,
};

/// WebAssembly wrapper for Algorithm
#[wasm_bindgen]
pub struct WasmAlgorithm {
    inner: Algorithm,
}

#[wasm_bindgen]
impl WasmAlgorithm {
    /// Create a new algorithm from name
    #[wasm_bindgen(constructor)]
    pub fn new(name: &str) -> Result<WasmAlgorithm, JsValue> {
        let inner = match name.to_lowercase().as_str() {
            "classical" => Algorithm::Classical,
            "hybrid" => Algorithm::Hybrid,
            "post-quantum" | "postquantum" => Algorithm::PostQuantum,
            "ml-kem-1024" | "mlkem1024" => Algorithm::MlKem1024,
            "multi-algorithm" | "multialgorithm" => Algorithm::MultiAlgorithm,
            "multi-kem" | "multikem" => Algorithm::MultiKem,
            "multi-kem-triple" | "multikemtriple" => Algorithm::MultiKemTriple,
            "quad-layer" | "quadlayer" => Algorithm::QuadLayer,
            "pq3-stack" | "pq3stack" => Algorithm::Pq3Stack,
            "lattice-code-hybrid" | "latticecodehybrid" => Algorithm::LatticeCodeHybrid,
            _ => {
                return Err(JsValue::from_str(&format!("Unknown algorithm: {}", name)));
            }
        };
        Ok(Self { inner })
    }

    /// Get algorithm name
    #[wasm_bindgen(getter)]
    pub fn name(&self) -> String {
        self.inner.name().to_string()
    }

    /// Get algorithm ID
    #[wasm_bindgen(getter)]
    pub fn id(&self) -> u16 {
        self.inner.as_id()
    }

    /// Get all supported algorithm names
    #[wasm_bindgen(js_name = supportedAlgorithms)]
    pub fn supported_algorithms() -> Vec<JsValue> {
        vec![
            "classical",
            "hybrid",
            "post-quantum",
            "ml-kem-1024",
            "multi-algorithm",
            "multi-kem",
            "multi-kem-triple",
            "quad-layer",
            "pq3-stack",
            "lattice-code-hybrid",
        ]
        .into_iter()
        .map(JsValue::from_str)
        .collect()
    }
}

/// WebAssembly wrapper for EncParameters
#[wasm_bindgen]
pub struct WasmEncParameters {
    inner: EncParameters,
}

#[wasm_bindgen]
impl WasmEncParameters {
    /// Create new encryption parameters
    #[wasm_bindgen(constructor)]
    pub fn new(iv: Vec<u8>, tag: Vec<u8>) -> Self {
        Self {
            inner: EncParameters {
                iv,
                tag,
                params: HashMap::new(),
            },
        }
    }

    /// Get IV/nonce
    #[wasm_bindgen(getter)]
    pub fn iv(&self) -> Vec<u8> {
        self.inner.iv.clone()
    }

    /// Set IV/nonce
    #[wasm_bindgen(setter)]
    pub fn set_iv(&mut self, iv: Vec<u8>) {
        self.inner.iv = iv;
    }

    /// Get authentication tag
    #[wasm_bindgen(getter)]
    pub fn tag(&self) -> Vec<u8> {
        self.inner.tag.clone()
    }

    /// Set authentication tag
    #[wasm_bindgen(setter)]
    pub fn set_tag(&mut self, tag: Vec<u8>) {
        self.inner.tag = tag;
    }
}

/// WebAssembly wrapper for KemParameters
#[wasm_bindgen]
pub struct WasmKemParameters {
    inner: KemParameters,
}

#[wasm_bindgen]
impl WasmKemParameters {
    /// Create new KEM parameters
    #[wasm_bindgen(constructor)]
    pub fn new(public_key: Vec<u8>, ciphertext: Vec<u8>) -> Self {
        Self {
            inner: KemParameters {
                public_key,
                ciphertext,
                params: HashMap::new(),
            },
        }
    }

    /// Get public key
    #[wasm_bindgen(getter)]
    pub fn public_key(&self) -> Vec<u8> {
        self.inner.public_key.clone()
    }

    /// Get ciphertext
    #[wasm_bindgen(getter)]
    pub fn ciphertext(&self) -> Vec<u8> {
        self.inner.ciphertext.clone()
    }
}

/// WebAssembly wrapper for SigParameters
#[wasm_bindgen]
pub struct WasmSigParameters {
    inner: SigParameters,
}

#[wasm_bindgen]
impl WasmSigParameters {
    /// Create new signature parameters
    #[wasm_bindgen(constructor)]
    pub fn new(public_key: Vec<u8>, signature: Vec<u8>) -> Self {
        Self {
            inner: SigParameters {
                public_key,
                signature,
                params: HashMap::new(),
            },
        }
    }

    /// Get public key
    #[wasm_bindgen(getter)]
    pub fn public_key(&self) -> Vec<u8> {
        self.inner.public_key.clone()
    }

    /// Get signature
    #[wasm_bindgen(getter)]
    pub fn signature(&self) -> Vec<u8> {
        self.inner.signature.clone()
    }
}

/// WebAssembly wrapper for CompressionParameters
#[wasm_bindgen]
pub struct WasmCompressionParameters {
    inner: CompressionParameters,
}

#[wasm_bindgen]
impl WasmCompressionParameters {
    /// Create new compression parameters
    #[wasm_bindgen(constructor)]
    pub fn new(algorithm: String, level: u8, original_size: u64) -> Self {
        Self {
            inner: CompressionParameters {
                algorithm,
                level,
                original_size,
                params: HashMap::new(),
            },
        }
    }

    /// Get compression algorithm
    #[wasm_bindgen(getter)]
    pub fn algorithm(&self) -> String {
        self.inner.algorithm.clone()
    }

    /// Get compression level
    #[wasm_bindgen(getter)]
    pub fn level(&self) -> u8 {
        self.inner.level
    }

    /// Get original size
    #[wasm_bindgen(getter)]
    pub fn original_size(&self) -> u64 {
        self.inner.original_size
    }
}

/// WebAssembly wrapper for PqcMetadata
#[wasm_bindgen]
pub struct WasmPqcMetadata {
    enc_params: WasmEncParameters,
    kem_params: Option<WasmKemParameters>,
    sig_params: Option<WasmSigParameters>,
    compression_params: Option<WasmCompressionParameters>,
}

#[wasm_bindgen]
impl WasmPqcMetadata {
    /// Create new metadata
    #[wasm_bindgen(constructor)]
    pub fn new(enc_params: WasmEncParameters) -> Self {
        Self {
            enc_params,
            kem_params: None,
            sig_params: None,
            compression_params: None,
        }
    }

    /// Set KEM parameters
    #[wasm_bindgen(js_name = setKemParams)]
    pub fn set_kem_params(&mut self, kem_params: WasmKemParameters) {
        self.kem_params = Some(kem_params);
    }

    /// Set signature parameters
    #[wasm_bindgen(js_name = setSigParams)]
    pub fn set_sig_params(&mut self, sig_params: WasmSigParameters) {
        self.sig_params = Some(sig_params);
    }

    /// Set compression parameters
    #[wasm_bindgen(js_name = setCompressionParams)]
    pub fn set_compression_params(&mut self, compression_params: WasmCompressionParameters) {
        self.compression_params = Some(compression_params);
    }

    fn to_rust(&self) -> PqcMetadata {
        PqcMetadata {
            kem_params: self.kem_params.as_ref().map(|k| k.inner.clone()),
            sig_params: self.sig_params.as_ref().map(|s| s.inner.clone()),
            enc_params: self.enc_params.inner.clone(),
            compression_params: self.compression_params.as_ref().map(|c| c.inner.clone()),
            custom: HashMap::new(),
        }
    }
}

/// WebAssembly wrapper for FormatFlags
#[wasm_bindgen]
pub struct WasmFormatFlags {
    inner: FormatFlags,
}

#[wasm_bindgen]
impl WasmFormatFlags {
    /// Create new empty flags
    #[wasm_bindgen(constructor)]
    pub fn new() -> Self {
        Self {
            inner: FormatFlags::new(),
        }
    }

    /// Enable compression flag
    #[wasm_bindgen(js_name = withCompression)]
    pub fn with_compression(&self) -> WasmFormatFlags {
        Self {
            inner: self.inner.with_compression(),
        }
    }

    /// Enable streaming flag
    #[wasm_bindgen(js_name = withStreaming)]
    pub fn with_streaming(&self) -> WasmFormatFlags {
        Self {
            inner: self.inner.with_streaming(),
        }
    }

    /// Enable additional auth flag
    #[wasm_bindgen(js_name = withAdditionalAuth)]
    pub fn with_additional_auth(&self) -> WasmFormatFlags {
        Self {
            inner: self.inner.with_additional_auth(),
        }
    }

    /// Enable experimental features flag
    #[wasm_bindgen(js_name = withExperimental)]
    pub fn with_experimental(&self) -> WasmFormatFlags {
        Self {
            inner: self.inner.with_experimental(),
        }
    }

    /// Check if compression is enabled
    #[wasm_bindgen(js_name = hasCompression)]
    pub fn has_compression(&self) -> bool {
        self.inner.has_compression()
    }

    /// Check if streaming is enabled
    #[wasm_bindgen(js_name = hasStreaming)]
    pub fn has_streaming(&self) -> bool {
        self.inner.has_streaming()
    }

    /// Check if additional auth is enabled
    #[wasm_bindgen(js_name = hasAdditionalAuth)]
    pub fn has_additional_auth(&self) -> bool {
        self.inner.has_additional_auth()
    }

    /// Check if experimental features are enabled
    #[wasm_bindgen(js_name = hasExperimental)]
    pub fn has_experimental(&self) -> bool {
        self.inner.has_experimental()
    }
}

/// WebAssembly wrapper for PqcBinaryFormat
#[wasm_bindgen]
pub struct WasmPqcBinaryFormat {
    inner: PqcBinaryFormat,
}

#[wasm_bindgen]
impl WasmPqcBinaryFormat {
    /// Create a new PQC Binary Format structure
    ///
    /// @param {WasmAlgorithm} algorithm - Algorithm to use
    /// @param {WasmPqcMetadata} metadata - Metadata container
    /// @param {Uint8Array} data - Encrypted data bytes
    /// @returns {WasmPqcBinaryFormat} New instance
    #[wasm_bindgen(constructor)]
    pub fn new(
        algorithm: WasmAlgorithm,
        metadata: WasmPqcMetadata,
        data: Vec<u8>,
    ) -> WasmPqcBinaryFormat {
        let rust_metadata = metadata.to_rust();
        let inner = PqcBinaryFormat::new(algorithm.inner, rust_metadata, data);
        Self { inner }
    }

    /// Create with specific flags
    ///
    /// @param {WasmAlgorithm} algorithm - Algorithm to use
    /// @param {WasmFormatFlags} flags - Format flags
    /// @param {WasmPqcMetadata} metadata - Metadata container
    /// @param {Uint8Array} data - Encrypted data bytes
    /// @returns {WasmPqcBinaryFormat} New instance
    #[wasm_bindgen(js_name = withFlags)]
    pub fn with_flags(
        algorithm: WasmAlgorithm,
        flags: WasmFormatFlags,
        metadata: WasmPqcMetadata,
        data: Vec<u8>,
    ) -> WasmPqcBinaryFormat {
        let rust_metadata = metadata.to_rust();
        let inner = PqcBinaryFormat::with_flags(algorithm.inner, flags.inner, rust_metadata, data);
        Self { inner }
    }

    /// Serialize to bytes
    ///
    /// @returns {Uint8Array} Serialized bytes
    /// @throws {Error} If serialization fails
    #[wasm_bindgen(js_name = toBytes)]
    pub fn to_bytes(&self) -> Result<Vec<u8>, JsValue> {
        self.inner
            .to_bytes()
            .map_err(|e| JsValue::from_str(&e.to_string()))
    }

    /// Deserialize from bytes
    ///
    /// @param {Uint8Array} data - Bytes to deserialize
    /// @returns {WasmPqcBinaryFormat} Deserialized instance
    /// @throws {Error} If deserialization fails
    #[wasm_bindgen(js_name = fromBytes)]
    pub fn from_bytes(data: &[u8]) -> Result<WasmPqcBinaryFormat, JsValue> {
        let inner =
            PqcBinaryFormat::from_bytes(data).map_err(|e| JsValue::from_str(&e.to_string()))?;
        Ok(Self { inner })
    }

    /// Validate the format structure
    ///
    /// @throws {Error} If validation fails
    pub fn validate(&self) -> Result<(), JsValue> {
        self.inner
            .validate()
            .map_err(|e| JsValue::from_str(&e.to_string()))
    }

    /// Get algorithm
    ///
    /// @returns {WasmAlgorithm} Algorithm used
    #[wasm_bindgen(getter)]
    pub fn algorithm(&self) -> WasmAlgorithm {
        WasmAlgorithm {
            inner: self.inner.algorithm(),
        }
    }

    /// Get encrypted data
    ///
    /// @returns {Uint8Array} Encrypted data
    #[wasm_bindgen(getter)]
    pub fn data(&self) -> Vec<u8> {
        self.inner.data().to_vec()
    }

    /// Get format flags
    ///
    /// @returns {WasmFormatFlags} Format flags
    #[wasm_bindgen(getter)]
    pub fn flags(&self) -> WasmFormatFlags {
        WasmFormatFlags {
            inner: self.inner.flags(),
        }
    }

    /// Get total serialized size
    ///
    /// @returns {number} Size in bytes
    #[wasm_bindgen(js_name = totalSize)]
    pub fn total_size(&self) -> usize {
        self.inner.total_size()
    }
}

/// Get the PQC Binary Format version
#[wasm_bindgen(js_name = getVersion)]
pub fn get_version() -> String {
    crate::VERSION.to_string()
}

/// Get the PQC Binary Format spec version
#[wasm_bindgen(js_name = getBinaryVersion)]
pub fn get_binary_version() -> u8 {
    crate::PQC_BINARY_VERSION
}