Skip to main content

libkeri/cesr/signing/
mod.rs

1mod cipher;
2mod decrypter;
3mod encrypter;
4mod salter;
5pub mod signer;
6
7pub use cipher::Cipher;
8pub use decrypter::Decrypter;
9pub use encrypter::Encrypter;
10pub use salter::Salter;
11pub use signer::Signer;
12
13use libsodium_sys;
14
15/// Represents different types of signatures that can be produced by signing
16#[derive(Debug, Clone)]
17pub enum Sigmat {
18    /// Indexed signature (Siger)
19    Indexed(Siger),
20    /// Non-indexed signature (Cigar)
21    NonIndexed(Cigar),
22}
23
24/// Module for X25519 cipher codes for variable-sized sniffable stream plaintext
25#[allow(dead_code)]
26pub mod cix_var_strm_dex {
27    use once_cell::sync::Lazy;
28    use std::collections::HashMap;
29
30    /// X25519 sealed box cipher bytes of sniffable stream plaintext lead size 0
31    pub const X25519_CIPHER_L0: &str = "4C";
32    /// X25519 sealed box cipher bytes of sniffable stream plaintext lead size 1
33    pub const X25519_CIPHER_L1: &str = "5C";
34    /// X25519 sealed box cipher bytes of sniffable stream plaintext lead size 2
35    pub const X25519_CIPHER_L2: &str = "6C";
36    /// X25519 sealed box cipher bytes of sniffable stream plaintext big lead size 0
37    pub const X25519_CIPHER_BIG_L0: &str = "7AAC";
38    /// X25519 sealed box cipher bytes of sniffable stream plaintext big lead size 1
39    pub const X25519_CIPHER_BIG_L1: &str = "8AAC";
40    /// X25519 sealed box cipher bytes of sniffable stream plaintext big lead size 2
41    pub const X25519_CIPHER_BIG_L2: &str = "9AAC";
42
43    pub static MAP: Lazy<HashMap<&'static str, &'static str>> = Lazy::new(|| {
44        let mut map = HashMap::new();
45        map.insert("X25519_CIPHER_L0", X25519_CIPHER_L0);
46        map.insert("X25519_CIPHER_L1", X25519_CIPHER_L1);
47        map.insert("X25519_CIPHER_L2", X25519_CIPHER_L2);
48        map.insert("X25519_CIPHER_BIG_L0", X25519_CIPHER_BIG_L0);
49        map.insert("X25519_CIPHER_BIG_L1", X25519_CIPHER_BIG_L1);
50        map.insert("X25519_CIPHER_BIG_L2", X25519_CIPHER_BIG_L2);
51        map
52    });
53
54    pub static TUPLE: [&'static str; 6] = [
55        X25519_CIPHER_L0,
56        X25519_CIPHER_L1,
57        X25519_CIPHER_L2,
58        X25519_CIPHER_BIG_L0,
59        X25519_CIPHER_BIG_L1,
60        X25519_CIPHER_BIG_L2,
61    ];
62}
63
64/// Module for X25519 cipher codes for variable-sized QB64 plaintext
65#[allow(dead_code)]
66pub mod cix_var_qb64_dex {
67    use once_cell::sync::Lazy;
68    use std::collections::HashMap;
69
70    /// X25519 sealed box cipher bytes of QB64 plaintext lead size 0
71    pub const X25519_CIPHER_QB64_L0: &str = "4D";
72    /// X25519 sealed box cipher bytes of QB64 plaintext lead size 1
73    pub const X25519_CIPHER_QB64_L1: &str = "5D";
74    /// X25519 sealed box cipher bytes of QB64 plaintext lead size 2
75    pub const X25519_CIPHER_QB64_L2: &str = "6D";
76    /// X25519 sealed box cipher bytes of QB64 plaintext big lead size 0
77    pub const X25519_CIPHER_QB64_BIG_L0: &str = "7AAD";
78    /// X25519 sealed box cipher bytes of QB64 plaintext big lead size 1
79    pub const X25519_CIPHER_QB64_BIG_L1: &str = "8AAD";
80    /// X25519 sealed box cipher bytes of QB64 plaintext big lead size 2
81    pub const X25519_CIPHER_QB64_BIG_L2: &str = "9AAD";
82
83    pub static MAP: Lazy<HashMap<&'static str, &'static str>> = Lazy::new(|| {
84        let mut map = HashMap::new();
85        map.insert("X25519_CIPHER_QB64_L0", X25519_CIPHER_QB64_L0);
86        map.insert("X25519_CIPHER_QB64_L1", X25519_CIPHER_QB64_L1);
87        map.insert("X25519_CIPHER_QB64_L2", X25519_CIPHER_QB64_L2);
88        map.insert("X25519_CIPHER_QB64_BIG_L0", X25519_CIPHER_QB64_BIG_L0);
89        map.insert("X25519_CIPHER_QB64_BIG_L1", X25519_CIPHER_QB64_BIG_L1);
90        map.insert("X25519_CIPHER_QB64_BIG_L2", X25519_CIPHER_QB64_BIG_L2);
91        map
92    });
93
94    pub static TUPLE: [&'static str; 6] = [
95        X25519_CIPHER_QB64_L0,
96        X25519_CIPHER_QB64_L1,
97        X25519_CIPHER_QB64_L2,
98        X25519_CIPHER_QB64_BIG_L0,
99        X25519_CIPHER_QB64_BIG_L1,
100        X25519_CIPHER_QB64_BIG_L2,
101    ];
102}
103
104/// Module for X25519 cipher codes for fixed-sized QB64 plaintext
105#[allow(dead_code)]
106pub mod cix_fix_qb64_dex {
107    use once_cell::sync::Lazy;
108    use std::collections::HashMap;
109
110    /// X25519 sealed box 124 char qb64 Cipher of 44 char qb64 Seed
111    pub const X25519_CIPHER_SEED: &str = "P";
112    /// X25519 sealed box 100 char qb64 Cipher of 24 char qb64 Salt
113    pub const X25519_CIPHER_SALT: &str = "1AAH";
114
115    pub static MAP: Lazy<HashMap<&'static str, &'static str>> = Lazy::new(|| {
116        let mut map = HashMap::new();
117        map.insert("X25519_CIPHER_SEED", X25519_CIPHER_SEED);
118        map.insert("X25519_CIPHER_SALT", X25519_CIPHER_SALT);
119        map
120    });
121
122    pub static TUPLE: [&'static str; 2] = [X25519_CIPHER_SEED, X25519_CIPHER_SALT];
123}
124
125/// Module for X25519 cipher codes for all (both fixed and variable) sizes of QB64 plaintext
126#[allow(dead_code)]
127pub mod cix_all_qb64_dex {
128    use super::{cix_fix_qb64_dex, cix_var_qb64_dex};
129    use once_cell::sync::Lazy;
130    use std::collections::HashMap;
131
132    /// X25519 sealed box 124 char qb64 Cipher of 44 char qb64 Seed
133    pub const X25519_CIPHER_SEED: &str = cix_fix_qb64_dex::X25519_CIPHER_SEED;
134    /// X25519 sealed box 100 char qb64 Cipher of 24 char qb64 Salt
135    pub const X25519_CIPHER_SALT: &str = cix_fix_qb64_dex::X25519_CIPHER_SALT;
136
137    /// X25519 sealed box cipher bytes of QB64 plaintext lead size 0
138    pub const X25519_CIPHER_QB64_L0: &str = cix_var_qb64_dex::X25519_CIPHER_QB64_L0;
139    /// X25519 sealed box cipher bytes of QB64 plaintext lead size 1
140    pub const X25519_CIPHER_QB64_L1: &str = cix_var_qb64_dex::X25519_CIPHER_QB64_L1;
141    /// X25519 sealed box cipher bytes of QB64 plaintext lead size 2
142    pub const X25519_CIPHER_QB64_L2: &str = cix_var_qb64_dex::X25519_CIPHER_QB64_L2;
143    /// X25519 sealed box cipher bytes of QB64 plaintext big lead size 0
144    pub const X25519_CIPHER_QB64_BIG_L0: &str = cix_var_qb64_dex::X25519_CIPHER_QB64_BIG_L0;
145    /// X25519 sealed box cipher bytes of QB64 plaintext big lead size 1
146    pub const X25519_CIPHER_QB64_BIG_L1: &str = cix_var_qb64_dex::X25519_CIPHER_QB64_BIG_L1;
147    /// X25519 sealed box cipher bytes of QB64 plaintext big lead size 2
148    pub const X25519_CIPHER_QB64_BIG_L2: &str = cix_var_qb64_dex::X25519_CIPHER_QB64_BIG_L2;
149
150    pub static MAP: Lazy<HashMap<&'static str, &'static str>> = Lazy::new(|| {
151        let mut map = HashMap::new();
152        map.insert("X25519_CIPHER_SEED", X25519_CIPHER_SEED);
153        map.insert("X25519_CIPHER_SALT", X25519_CIPHER_SALT);
154        map.insert("X25519_CIPHER_QB64_L0", X25519_CIPHER_QB64_L0);
155        map.insert("X25519_CIPHER_QB64_L1", X25519_CIPHER_QB64_L1);
156        map.insert("X25519_CIPHER_QB64_L2", X25519_CIPHER_QB64_L2);
157        map.insert("X25519_CIPHER_QB64_BIG_L0", X25519_CIPHER_QB64_BIG_L0);
158        map.insert("X25519_CIPHER_QB64_BIG_L1", X25519_CIPHER_QB64_BIG_L1);
159        map.insert("X25519_CIPHER_QB64_BIG_L2", X25519_CIPHER_QB64_BIG_L2);
160        map
161    });
162
163    pub static TUPLE: [&'static str; 8] = [
164        X25519_CIPHER_SEED,
165        X25519_CIPHER_SALT,
166        X25519_CIPHER_QB64_L0,
167        X25519_CIPHER_QB64_L1,
168        X25519_CIPHER_QB64_L2,
169        X25519_CIPHER_QB64_BIG_L0,
170        X25519_CIPHER_QB64_BIG_L1,
171        X25519_CIPHER_QB64_BIG_L2,
172    ];
173}
174
175/// Module for X25519 cipher codes for variable-sized QB2 plaintext
176#[allow(dead_code)]
177pub mod cix_var_qb2_dex {
178    use once_cell::sync::Lazy;
179    use std::collections::HashMap;
180
181    /// X25519 sealed box cipher bytes of QB2 plaintext lead size 0
182    pub const X25519_CIPHER_QB2_L0: &str = "4E";
183    /// X25519 sealed box cipher bytes of QB2 plaintext lead size 1
184    pub const X25519_CIPHER_QB2_L1: &str = "5E";
185    /// X25519 sealed box cipher bytes of QB2 plaintext lead size 2
186    pub const X25519_CIPHER_QB2_L2: &str = "6E";
187    /// X25519 sealed box cipher bytes of QB2 plaintext big lead size 0
188    pub const X25519_CIPHER_QB2_BIG_L0: &str = "7AAE";
189    /// X25519 sealed box cipher bytes of QB2 plaintext big lead size 1
190    pub const X25519_CIPHER_QB2_BIG_L1: &str = "8AAE";
191    /// X25519 sealed box cipher bytes of QB2 plaintext big lead size 2
192    pub const X25519_CIPHER_QB2_BIG_L2: &str = "9AAE";
193
194    pub static MAP: Lazy<HashMap<&'static str, &'static str>> = Lazy::new(|| {
195        let mut map = HashMap::new();
196        map.insert("X25519_CIPHER_QB2_L0", X25519_CIPHER_QB2_L0);
197        map.insert("X25519_CIPHER_QB2_L1", X25519_CIPHER_QB2_L1);
198        map.insert("X25519_CIPHER_QB2_L2", X25519_CIPHER_QB2_L2);
199        map.insert("X25519_CIPHER_QB2_BIG_L0", X25519_CIPHER_QB2_BIG_L0);
200        map.insert("X25519_CIPHER_QB2_BIG_L1", X25519_CIPHER_QB2_BIG_L1);
201        map.insert("X25519_CIPHER_QB2_BIG_L2", X25519_CIPHER_QB2_BIG_L2);
202        map
203    });
204
205    pub static TUPLE: [&'static str; 6] = [
206        X25519_CIPHER_QB2_L0,
207        X25519_CIPHER_QB2_L1,
208        X25519_CIPHER_QB2_L2,
209        X25519_CIPHER_QB2_BIG_L0,
210        X25519_CIPHER_QB2_BIG_L1,
211        X25519_CIPHER_QB2_BIG_L2,
212    ];
213}
214
215/// Module for X25519 cipher codes for all variable sizes and all types of plaintext
216#[allow(dead_code)]
217pub mod cix_var_dex {
218    use super::{cix_var_qb2_dex, cix_var_qb64_dex, cix_var_strm_dex};
219    use once_cell::sync::Lazy;
220    use std::collections::HashMap;
221
222    // Stream plaintext codes
223    /// X25519 sealed box cipher bytes of sniffable stream plaintext lead size 0
224    pub const X25519_CIPHER_L0: &str = cix_var_strm_dex::X25519_CIPHER_L0;
225    /// X25519 sealed box cipher bytes of sniffable stream plaintext lead size 1
226    pub const X25519_CIPHER_L1: &str = cix_var_strm_dex::X25519_CIPHER_L1;
227    /// X25519 sealed box cipher bytes of sniffable stream plaintext lead size 2
228    pub const X25519_CIPHER_L2: &str = cix_var_strm_dex::X25519_CIPHER_L2;
229    /// X25519 sealed box cipher bytes of sniffable stream plaintext big lead size 0
230    pub const X25519_CIPHER_BIG_L0: &str = cix_var_strm_dex::X25519_CIPHER_BIG_L0;
231    /// X25519 sealed box cipher bytes of sniffable stream plaintext big lead size 1
232    pub const X25519_CIPHER_BIG_L1: &str = cix_var_strm_dex::X25519_CIPHER_BIG_L1;
233    /// X25519 sealed box cipher bytes of sniffable stream plaintext big lead size 2
234    pub const X25519_CIPHER_BIG_L2: &str = cix_var_strm_dex::X25519_CIPHER_BIG_L2;
235
236    // QB64 plaintext codes
237    /// X25519 sealed box cipher bytes of QB64 plaintext lead size 0
238    pub const X25519_CIPHER_QB64_L0: &str = cix_var_qb64_dex::X25519_CIPHER_QB64_L0;
239    /// X25519 sealed box cipher bytes of QB64 plaintext lead size 1
240    pub const X25519_CIPHER_QB64_L1: &str = cix_var_qb64_dex::X25519_CIPHER_QB64_L1;
241    /// X25519 sealed box cipher bytes of QB64 plaintext lead size 2
242    pub const X25519_CIPHER_QB64_L2: &str = cix_var_qb64_dex::X25519_CIPHER_QB64_L2;
243    /// X25519 sealed box cipher bytes of QB64 plaintext big lead size 0
244    pub const X25519_CIPHER_QB64_BIG_L0: &str = cix_var_qb64_dex::X25519_CIPHER_QB64_BIG_L0;
245    /// X25519 sealed box cipher bytes of QB64 plaintext big lead size 1
246    pub const X25519_CIPHER_QB64_BIG_L1: &str = cix_var_qb64_dex::X25519_CIPHER_QB64_BIG_L1;
247    /// X25519 sealed box cipher bytes of QB64 plaintext big lead size 2
248    pub const X25519_CIPHER_QB64_BIG_L2: &str = cix_var_qb64_dex::X25519_CIPHER_QB64_BIG_L2;
249
250    // QB2 plaintext codes
251    /// X25519 sealed box cipher bytes of QB2 plaintext lead size 0
252    pub const X25519_CIPHER_QB2_L0: &str = cix_var_qb2_dex::X25519_CIPHER_QB2_L0;
253    /// X25519 sealed box cipher bytes of QB2 plaintext lead size 1
254    pub const X25519_CIPHER_QB2_L1: &str = cix_var_qb2_dex::X25519_CIPHER_QB2_L1;
255    /// X25519 sealed box cipher bytes of QB2 plaintext lead size 2
256    pub const X25519_CIPHER_QB2_L2: &str = cix_var_qb2_dex::X25519_CIPHER_QB2_L2;
257    /// X25519 sealed box cipher bytes of QB2 plaintext big lead size 0
258    pub const X25519_CIPHER_QB2_BIG_L0: &str = cix_var_qb2_dex::X25519_CIPHER_QB2_BIG_L0;
259    /// X25519 sealed box cipher bytes of QB2 plaintext big lead size 1
260    pub const X25519_CIPHER_QB2_BIG_L1: &str = cix_var_qb2_dex::X25519_CIPHER_QB2_BIG_L1;
261    /// X25519 sealed box cipher bytes of QB2 plaintext big lead size 2
262    pub const X25519_CIPHER_QB2_BIG_L2: &str = cix_var_qb2_dex::X25519_CIPHER_QB2_BIG_L2;
263
264    pub static MAP: Lazy<HashMap<&'static str, &'static str>> = Lazy::new(|| {
265        let mut map = HashMap::new();
266        map.insert(X25519_CIPHER_L0, "X25519_CIPHER_L0");
267        map.insert(X25519_CIPHER_L1, "X25519_CIPHER_L1");
268        map.insert(X25519_CIPHER_L2, "X25519_CIPHER_L2");
269        map.insert(X25519_CIPHER_BIG_L0, "X25519_CIPHER_BIG_L0");
270        map.insert(X25519_CIPHER_BIG_L1, "X25519_CIPHER_BIG_L1");
271        map.insert(X25519_CIPHER_BIG_L2, "X25519_CIPHER_BIG_L2");
272        map.insert(X25519_CIPHER_QB64_L0, "X25519_CIPHER_QB64_L0");
273        map.insert(X25519_CIPHER_QB64_L1, "X25519_CIPHER_QB64_L1");
274        map.insert(X25519_CIPHER_QB64_L2, "X25519_CIPHER_QB64_L2");
275        map.insert(X25519_CIPHER_QB64_BIG_L0, "X25519_CIPHER_QB64_BIG_L0");
276        map.insert(X25519_CIPHER_QB64_BIG_L1, "X25519_CIPHER_QB64_BIG_L1");
277        map.insert(X25519_CIPHER_QB64_BIG_L2, "X25519_CIPHER_QB64_BIG_L2");
278        map.insert(X25519_CIPHER_QB2_L0, "X25519_CIPHER_QB2_L0");
279        map.insert(X25519_CIPHER_QB2_L1, "X25519_CIPHER_QB2_L1");
280        map.insert(X25519_CIPHER_QB2_L2, "X25519_CIPHER_QB2_L2");
281        map.insert(X25519_CIPHER_QB2_BIG_L0, "X25519_CIPHER_QB2_BIG_L0");
282        map.insert(X25519_CIPHER_QB2_BIG_L1, "X25519_CIPHER_QB2_BIG_L1");
283        map.insert(X25519_CIPHER_QB2_BIG_L2, "X25519_CIPHER_QB2_BIG_L2");
284        map
285    });
286
287    pub static TUPLE: [&'static str; 18] = [
288        X25519_CIPHER_L0,
289        X25519_CIPHER_L1,
290        X25519_CIPHER_L2,
291        X25519_CIPHER_BIG_L0,
292        X25519_CIPHER_BIG_L1,
293        X25519_CIPHER_BIG_L2,
294        X25519_CIPHER_QB64_L0,
295        X25519_CIPHER_QB64_L1,
296        X25519_CIPHER_QB64_L2,
297        X25519_CIPHER_QB64_BIG_L0,
298        X25519_CIPHER_QB64_BIG_L1,
299        X25519_CIPHER_QB64_BIG_L2,
300        X25519_CIPHER_QB2_L0,
301        X25519_CIPHER_QB2_L1,
302        X25519_CIPHER_QB2_L2,
303        X25519_CIPHER_QB2_BIG_L0,
304        X25519_CIPHER_QB2_BIG_L1,
305        X25519_CIPHER_QB2_BIG_L2,
306    ];
307}
308
309/// Module for X25519 cipher codes for all sizes and all types of plaintext
310#[allow(dead_code)]
311pub mod cix_dex {
312    use super::{cix_fix_qb64_dex, cix_var_dex};
313    use once_cell::sync::Lazy;
314    use std::collections::HashMap;
315
316    // Variable size codes for stream plaintext
317    /// X25519 sealed box cipher bytes of sniffable stream plaintext lead size 0
318    pub const X25519_CIPHER_L0: &str = cix_var_dex::X25519_CIPHER_L0;
319    /// X25519 sealed box cipher bytes of sniffable stream plaintext lead size 1
320    pub const X25519_CIPHER_L1: &str = cix_var_dex::X25519_CIPHER_L1;
321    /// X25519 sealed box cipher bytes of sniffable stream plaintext lead size 2
322    pub const X25519_CIPHER_L2: &str = cix_var_dex::X25519_CIPHER_L2;
323    /// X25519 sealed box cipher bytes of sniffable stream plaintext big lead size 0
324    pub const X25519_CIPHER_BIG_L0: &str = cix_var_dex::X25519_CIPHER_BIG_L0;
325    /// X25519 sealed box cipher bytes of sniffable stream plaintext big lead size 1
326    pub const X25519_CIPHER_BIG_L1: &str = cix_var_dex::X25519_CIPHER_BIG_L1;
327    /// X25519 sealed box cipher bytes of sniffable stream plaintext big lead size 2
328    pub const X25519_CIPHER_BIG_L2: &str = cix_var_dex::X25519_CIPHER_BIG_L2;
329
330    // Fixed size codes
331    /// X25519 sealed box 124 char qb64 Cipher of 44 char qb64 Seed
332    pub const X25519_CIPHER_SEED: &str = cix_fix_qb64_dex::X25519_CIPHER_SEED;
333    /// X25519 sealed box 100 char qb64 Cipher of 24 char qb64 Salt
334    pub const X25519_CIPHER_SALT: &str = cix_fix_qb64_dex::X25519_CIPHER_SALT;
335
336    // Variable size codes for QB64 plaintext
337    /// X25519 sealed box cipher bytes of QB64 plaintext lead size 0
338    pub const X25519_CIPHER_QB64_L0: &str = cix_var_dex::X25519_CIPHER_QB64_L0;
339    /// X25519 sealed box cipher bytes of QB64 plaintext lead size 1
340    pub const X25519_CIPHER_QB64_L1: &str = cix_var_dex::X25519_CIPHER_QB64_L1;
341    /// X25519 sealed box cipher bytes of QB64 plaintext lead size 2
342    pub const X25519_CIPHER_QB64_L2: &str = cix_var_dex::X25519_CIPHER_QB64_L2;
343    /// X25519 sealed box cipher bytes of QB64 plaintext big lead size 0
344    pub const X25519_CIPHER_QB64_BIG_L0: &str = cix_var_dex::X25519_CIPHER_QB64_BIG_L0;
345    /// X25519 sealed box cipher bytes of QB64 plaintext big lead size 1
346    pub const X25519_CIPHER_QB64_BIG_L1: &str = cix_var_dex::X25519_CIPHER_QB64_BIG_L1;
347    /// X25519 sealed box cipher bytes of QB64 plaintext big lead size 2
348    pub const X25519_CIPHER_QB64_BIG_L2: &str = cix_var_dex::X25519_CIPHER_QB64_BIG_L2;
349
350    // Variable size codes for QB2 plaintext
351    /// X25519 sealed box cipher bytes of QB2 plaintext lead size 0
352    pub const X25519_CIPHER_QB2_L0: &str = cix_var_dex::X25519_CIPHER_QB2_L0;
353    /// X25519 sealed box cipher bytes of QB2 plaintext lead size 1
354    pub const X25519_CIPHER_QB2_L1: &str = cix_var_dex::X25519_CIPHER_QB2_L1;
355    /// X25519 sealed box cipher bytes of QB2 plaintext lead size 2
356    pub const X25519_CIPHER_QB2_L2: &str = cix_var_dex::X25519_CIPHER_QB2_L2;
357    /// X25519 sealed box cipher bytes of QB2 plaintext big lead size 0
358    pub const X25519_CIPHER_QB2_BIG_L0: &str = cix_var_dex::X25519_CIPHER_QB2_BIG_L0;
359    /// X25519 sealed box cipher bytes of QB2 plaintext big lead size 1
360    pub const X25519_CIPHER_QB2_BIG_L1: &str = cix_var_dex::X25519_CIPHER_QB2_BIG_L1;
361    /// X25519 sealed box cipher bytes of QB2 plaintext big lead size 2
362    pub const X25519_CIPHER_QB2_BIG_L2: &str = cix_var_dex::X25519_CIPHER_QB2_BIG_L2;
363
364    pub static MAP: Lazy<HashMap<&'static str, &'static str>> = Lazy::new(|| {
365        let mut map = HashMap::new();
366
367        map.insert(X25519_CIPHER_L0, "X25519_CIPHER_L0");
368        map.insert(X25519_CIPHER_L1, "X25519_CIPHER_L1");
369        map.insert(X25519_CIPHER_L2, "X25519_CIPHER_L2");
370        map.insert(X25519_CIPHER_BIG_L0, "X25519_CIPHER_BIG_L0");
371        map.insert(X25519_CIPHER_BIG_L1, "X25519_CIPHER_BIG_L1");
372        map.insert(X25519_CIPHER_BIG_L2, "X25519_CIPHER_BIG_L2");
373        map.insert(X25519_CIPHER_SEED, "X25519_CIPHER_SEED");
374        map.insert(X25519_CIPHER_SALT, "X25519_CIPHER_SALT");
375        map.insert(X25519_CIPHER_QB64_L0, "X25519_CIPHER_QB64_L0");
376        map.insert(X25519_CIPHER_QB64_L1, "X25519_CIPHER_QB64_L1");
377        map.insert(X25519_CIPHER_QB64_L2, "X25519_CIPHER_QB64_L2");
378        map.insert(X25519_CIPHER_QB64_BIG_L0, "X25519_CIPHER_QB64_BIG_L0");
379        map.insert(X25519_CIPHER_QB64_BIG_L1, "X25519_CIPHER_QB64_BIG_L1");
380        map.insert(X25519_CIPHER_QB64_BIG_L2, "X25519_CIPHER_QB64_BIG_L2");
381        map.insert(X25519_CIPHER_QB2_L0, "X25519_CIPHER_QB2_L0");
382        map.insert(X25519_CIPHER_QB2_L1, "X25519_CIPHER_QB2_L1");
383        map.insert(X25519_CIPHER_QB2_L2, "X25519_CIPHER_QB2_L2");
384        map.insert(X25519_CIPHER_QB2_BIG_L0, "X25519_CIPHER_QB2_BIG_L0");
385        map.insert(X25519_CIPHER_QB2_BIG_L1, "X25519_CIPHER_QB2_BIG_L1");
386        map.insert(X25519_CIPHER_QB2_BIG_L2, "X25519_CIPHER_QB2_BIG_L2");
387        map
388    });
389
390    pub static TUPLE: [&'static str; 20] = [
391        X25519_CIPHER_L0,
392        X25519_CIPHER_L1,
393        X25519_CIPHER_L2,
394        X25519_CIPHER_BIG_L0,
395        X25519_CIPHER_BIG_L1,
396        X25519_CIPHER_BIG_L2,
397        X25519_CIPHER_SEED,
398        X25519_CIPHER_SALT,
399        X25519_CIPHER_QB64_L0,
400        X25519_CIPHER_QB64_L1,
401        X25519_CIPHER_QB64_L2,
402        X25519_CIPHER_QB64_BIG_L0,
403        X25519_CIPHER_QB64_BIG_L1,
404        X25519_CIPHER_QB64_BIG_L2,
405        X25519_CIPHER_QB2_L0,
406        X25519_CIPHER_QB2_L1,
407        X25519_CIPHER_QB2_L2,
408        X25519_CIPHER_QB2_BIG_L0,
409        X25519_CIPHER_QB2_BIG_L1,
410        X25519_CIPHER_QB2_BIG_L2,
411    ];
412}
413
414use crate::cesr::cigar::Cigar;
415use crate::cesr::indexing::siger::Siger;
416use crate::errors::MatterError;
417use sodiumoxide::crypto::box_::curve25519xsalsa20poly1305 as crypto_box;
418use sodiumoxide::crypto::sign::ed25519;
419
420/// Convert an Ed25519 public key to an X25519 public key
421pub fn ed25519_pk_to_x25519_pk(
422    ed_pk: &ed25519::PublicKey,
423) -> Result<crypto_box::PublicKey, MatterError> {
424    // In libsodium, there's a crypto_sign_ed25519_pk_to_curve25519 function
425    // We need to implement this conversion for sodiumoxide
426
427    // The first step is to extract the raw bytes from the Ed25519 public key
428    let ed_pk_bytes = ed_pk.as_ref();
429
430    // We need access to the underlying curve25519 conversion
431    // You'll need to use the libsodium-sys crate directly for this
432
433    let mut curve_pk = [0u8; 32];
434
435    // SAFETY: This is a FFI call to libsodium
436    let result = unsafe {
437        libsodium_sys::crypto_sign_ed25519_pk_to_curve25519(
438            curve_pk.as_mut_ptr(),
439            ed_pk_bytes.as_ptr(),
440        )
441    };
442
443    if result != 0 {
444        return Err(MatterError::CryptoError(
445            "Failed to convert Ed25519 public key to X25519".to_string(),
446        ));
447    }
448
449    // Convert the raw bytes to a sodiumoxide X25519 public key
450    crypto_box::PublicKey::from_slice(&curve_pk).ok_or(MatterError::CryptoError(
451        "Failed to convert Ed25519 public key to X25519".to_string(),
452    ))
453}
454
455/// Convert an Ed25519 secret key to an X25519 secret key
456pub fn ed25519_sk_to_x25519_sk(
457    ed_sk: &ed25519::SecretKey,
458) -> Result<crypto_box::SecretKey, MatterError> {
459    // In libsodium, there's a crypto_sign_ed25519_sk_to_curve25519 function
460
461    // Extract the raw bytes from the Ed25519 secret key
462    let ed_sk_bytes = ed_sk.as_ref();
463
464    let mut curve_sk = [0u8; 32];
465
466    // SAFETY: This is a FFI call to libsodium
467    let result = unsafe {
468        libsodium_sys::crypto_sign_ed25519_sk_to_curve25519(
469            curve_sk.as_mut_ptr(),
470            ed_sk_bytes.as_ptr(),
471        )
472    };
473
474    if result != 0 {
475        return Err(MatterError::CryptoError(
476            "Failed to convert Ed25519 secret key to X25519".to_string(),
477        ));
478    }
479
480    // Convert the raw bytes to a sodiumoxide X25519 secret key
481    crypto_box::SecretKey::from_slice(&curve_sk).ok_or(MatterError::CryptoError(
482        "Failed to convert Ed25519 secret key to X25519".to_string(),
483    ))
484}