chie-crypto 0.2.0

Cryptographic primitives for CHIE Protocol
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
//! WebCrypto API Compatibility Layer
//!
//! This module provides compatibility with the W3C WebCrypto API standard,
//! enabling interoperability between Rust cryptographic operations and
//! browser-based WebCrypto operations.
//!
//! # Examples
//!
//! ```
//! use chie_crypto::webcrypto::{WebCryptoKey, Algorithm, KeyUsage};
//! use chie_crypto::signing::KeyPair;
//!
//! // Create a WebCrypto-compatible key
//! let keypair = KeyPair::generate();
//! let web_key = WebCryptoKey::from_ed25519_keypair(&keypair, &[KeyUsage::Sign]);
//!
//! // Export to JWK (WebCrypto standard format)
//! let jwk = web_key.to_jwk().unwrap();
//! ```

use crate::key_formats::JwkKey;
use crate::signing::{KeyPair, PublicKey};
use serde::{Deserialize, Serialize};
use thiserror::Error;

/// WebCrypto API errors
#[derive(Debug, Error, Clone, PartialEq, Eq)]
pub enum WebCryptoError {
    /// Unsupported algorithm
    #[error("Unsupported algorithm: {0}")]
    UnsupportedAlgorithm(String),

    /// Invalid key usage
    #[error("Invalid key usage: {0}")]
    InvalidKeyUsage(String),

    /// Key format error
    #[error("Key format error: {0}")]
    KeyFormatError(String),

    /// Operation not permitted
    #[error("Operation not permitted with current key usages")]
    OperationNotPermitted,
}

/// Result type for WebCrypto operations
pub type WebCryptoResult<T> = Result<T, WebCryptoError>;

/// WebCrypto algorithm identifiers
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "name")]
#[allow(non_snake_case)]
pub enum Algorithm {
    /// EdDSA (Ed25519) - Digital signatures
    #[serde(rename = "EdDSA")]
    EdDSA { namedCurve: String },

    /// ECDH (X25519) - Key agreement
    #[serde(rename = "ECDH")]
    ECDH { namedCurve: String },

    /// AES-GCM - Authenticated encryption
    #[serde(rename = "AES-GCM")]
    AesGcm { length: u32 },

    /// HMAC - Message authentication
    #[serde(rename = "HMAC")]
    Hmac { hash: String },

    /// HKDF - Key derivation
    #[serde(rename = "HKDF")]
    Hkdf { hash: String },

    /// PBKDF2 - Password-based key derivation
    #[serde(rename = "PBKDF2")]
    Pbkdf2 {
        hash: String,
        iterations: u32,
        salt: Vec<u8>,
    },
}

impl Algorithm {
    /// Create EdDSA algorithm with Ed25519 curve
    pub fn ed_dsa() -> Self {
        Self::EdDSA {
            namedCurve: "Ed25519".to_string(),
        }
    }

    /// Create ECDH algorithm with X25519 curve
    pub fn ecdh() -> Self {
        Self::ECDH {
            namedCurve: "X25519".to_string(),
        }
    }

    /// Create AES-GCM algorithm with specified key length
    pub fn aes_gcm(length: u32) -> Self {
        Self::AesGcm { length }
    }

    /// Create HMAC algorithm with specified hash
    pub fn hmac(hash: impl Into<String>) -> Self {
        Self::Hmac { hash: hash.into() }
    }

    /// Create HKDF algorithm with specified hash
    pub fn hkdf(hash: impl Into<String>) -> Self {
        Self::Hkdf { hash: hash.into() }
    }

    /// Create PBKDF2 algorithm
    pub fn pbkdf2(hash: impl Into<String>, iterations: u32, salt: Vec<u8>) -> Self {
        Self::Pbkdf2 {
            hash: hash.into(),
            iterations,
            salt,
        }
    }
}

/// WebCrypto key usage flags
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum KeyUsage {
    /// Key can be used for encryption
    Encrypt,
    /// Key can be used for decryption
    Decrypt,
    /// Key can be used for signing
    Sign,
    /// Key can be used for verification
    Verify,
    /// Key can be used for key derivation
    DeriveKey,
    /// Key can be used to derive bits
    DeriveBits,
    /// Key can be wrapped
    WrapKey,
    /// Key can be unwrapped
    UnwrapKey,
}

impl KeyUsage {
    /// Check if usage is a signing operation
    pub fn is_signing(&self) -> bool {
        matches!(self, Self::Sign | Self::Verify)
    }

    /// Check if usage is an encryption operation
    pub fn is_encryption(&self) -> bool {
        matches!(self, Self::Encrypt | Self::Decrypt)
    }

    /// Check if usage is a key derivation operation
    pub fn is_derivation(&self) -> bool {
        matches!(self, Self::DeriveKey | Self::DeriveBits)
    }
}

/// WebCrypto-compatible key
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WebCryptoKey {
    /// Algorithm
    pub algorithm: Algorithm,
    /// Key type (public, private, secret)
    #[serde(rename = "type")]
    pub key_type: KeyType,
    /// Extractable flag
    pub extractable: bool,
    /// Key usages
    pub usages: Vec<KeyUsage>,
    /// Internal key data (not exposed in serialization)
    #[serde(skip)]
    key_data: Vec<u8>,
}

/// WebCrypto key type
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum KeyType {
    /// Public key
    Public,
    /// Private key
    Private,
    /// Secret (symmetric) key
    Secret,
}

impl WebCryptoKey {
    /// Create WebCrypto key from Ed25519 keypair
    pub fn from_ed25519_keypair(keypair: &KeyPair, usages: &[KeyUsage]) -> Self {
        Self {
            algorithm: Algorithm::ed_dsa(),
            key_type: KeyType::Private,
            extractable: true,
            usages: usages.to_vec(),
            key_data: keypair.secret_key().to_vec(),
        }
    }

    /// Create WebCrypto key from Ed25519 public key
    pub fn from_ed25519_public_key(public_key: &PublicKey, usages: &[KeyUsage]) -> Self {
        Self {
            algorithm: Algorithm::ed_dsa(),
            key_type: KeyType::Public,
            extractable: true,
            usages: usages.to_vec(),
            key_data: public_key.to_vec(),
        }
    }

    /// Create WebCrypto key from symmetric key
    pub fn from_symmetric_key(key: &[u8], algorithm: Algorithm, usages: &[KeyUsage]) -> Self {
        Self {
            algorithm,
            key_type: KeyType::Secret,
            extractable: true,
            usages: usages.to_vec(),
            key_data: key.to_vec(),
        }
    }

    /// Set extractable flag
    pub fn with_extractable(mut self, extractable: bool) -> Self {
        self.extractable = extractable;
        self
    }

    /// Check if key can be used for operation
    pub fn can_use_for(&self, usage: KeyUsage) -> bool {
        self.usages.contains(&usage)
    }

    /// Export key to JWK format (WebCrypto standard)
    pub fn to_jwk(&self) -> WebCryptoResult<JwkKey> {
        if !self.extractable {
            return Err(WebCryptoError::OperationNotPermitted);
        }

        match (&self.algorithm, self.key_type) {
            (Algorithm::EdDSA { .. }, KeyType::Private) => {
                let mut secret = [0u8; 32];
                secret.copy_from_slice(&self.key_data);
                let keypair = KeyPair::from_secret_key(&secret).map_err(|_| {
                    WebCryptoError::KeyFormatError("Invalid secret key".to_string())
                })?;
                Ok(JwkKey::from_ed25519_keypair(&keypair))
            }
            (Algorithm::EdDSA { .. }, KeyType::Public) => {
                let mut public = [0u8; 32];
                public.copy_from_slice(&self.key_data);
                Ok(JwkKey::from_ed25519_public_key(&public))
            }
            _ => Err(WebCryptoError::UnsupportedAlgorithm(
                "Only EdDSA keys can be exported to JWK".to_string(),
            )),
        }
    }

    /// Import key from JWK format
    pub fn from_jwk(jwk: &JwkKey, usages: &[KeyUsage]) -> WebCryptoResult<Self> {
        if jwk.kty != "OKP" {
            return Err(WebCryptoError::UnsupportedAlgorithm(format!(
                "Unsupported key type: {}",
                jwk.kty
            )));
        }

        let crv = jwk
            .crv
            .as_ref()
            .ok_or_else(|| WebCryptoError::KeyFormatError("Missing curve parameter".to_string()))?;

        if crv != "Ed25519" {
            return Err(WebCryptoError::UnsupportedAlgorithm(format!(
                "Unsupported curve: {}",
                crv
            )));
        }

        let algorithm = Algorithm::ed_dsa();

        // Check if it's a private key
        if jwk.d.is_some() {
            let keypair = jwk.to_ed25519_keypair().map_err(|e| {
                WebCryptoError::KeyFormatError(format!("Failed to import keypair: {}", e))
            })?;

            Ok(Self {
                algorithm,
                key_type: KeyType::Private,
                extractable: true,
                usages: usages.to_vec(),
                key_data: keypair.secret_key().to_vec(),
            })
        } else {
            let public_key = jwk.to_ed25519_public_key().map_err(|e| {
                WebCryptoError::KeyFormatError(format!("Failed to import public key: {}", e))
            })?;

            Ok(Self {
                algorithm,
                key_type: KeyType::Public,
                extractable: true,
                usages: usages.to_vec(),
                key_data: public_key.to_vec(),
            })
        }
    }

    /// Get key data (for internal use)
    pub fn key_data(&self) -> &[u8] {
        &self.key_data
    }
}

/// WebCrypto key pair (for asymmetric algorithms)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WebCryptoKeyPair {
    /// Public key
    #[serde(rename = "publicKey")]
    pub public_key: WebCryptoKey,
    /// Private key
    #[serde(rename = "privateKey")]
    pub private_key: WebCryptoKey,
}

impl WebCryptoKeyPair {
    /// Create from Ed25519 keypair
    pub fn from_ed25519(keypair: &KeyPair, usages: &[KeyUsage]) -> Self {
        let (sign_usages, verify_usages): (Vec<_>, Vec<_>) =
            usages.iter().partition(|u| **u == KeyUsage::Sign);

        Self {
            public_key: WebCryptoKey::from_ed25519_public_key(
                &keypair.public_key(),
                &verify_usages,
            ),
            private_key: WebCryptoKey::from_ed25519_keypair(keypair, &sign_usages),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_algorithm_creation() {
        let algo = Algorithm::ed_dsa();
        match algo {
            Algorithm::EdDSA { namedCurve } => {
                assert_eq!(namedCurve, "Ed25519");
            }
            _ => panic!("Wrong algorithm type"),
        }
    }

    #[test]
    fn test_key_usage_checks() {
        assert!(KeyUsage::Sign.is_signing());
        assert!(KeyUsage::Verify.is_signing());
        assert!(KeyUsage::Encrypt.is_encryption());
        assert!(KeyUsage::Decrypt.is_encryption());
        assert!(KeyUsage::DeriveKey.is_derivation());
        assert!(KeyUsage::DeriveBits.is_derivation());
    }

    #[test]
    fn test_webcrypto_key_from_ed25519() {
        let keypair = KeyPair::generate();
        let usages = vec![KeyUsage::Sign];

        let web_key = WebCryptoKey::from_ed25519_keypair(&keypair, &usages);

        assert_eq!(web_key.key_type, KeyType::Private);
        assert!(web_key.extractable);
        assert_eq!(web_key.usages, usages);
        assert!(web_key.can_use_for(KeyUsage::Sign));
        assert!(!web_key.can_use_for(KeyUsage::Verify));
    }

    #[test]
    fn test_webcrypto_public_key() {
        let keypair = KeyPair::generate();
        let public_key = keypair.public_key();
        let usages = vec![KeyUsage::Verify];

        let web_key = WebCryptoKey::from_ed25519_public_key(&public_key, &usages);

        assert_eq!(web_key.key_type, KeyType::Public);
        assert!(web_key.can_use_for(KeyUsage::Verify));
    }

    #[test]
    fn test_jwk_export() {
        let keypair = KeyPair::generate();
        let usages = vec![KeyUsage::Sign];

        let web_key = WebCryptoKey::from_ed25519_keypair(&keypair, &usages);
        let jwk = web_key.to_jwk().unwrap();

        assert_eq!(jwk.kty, "OKP");
        assert_eq!(jwk.crv, Some("Ed25519".to_string()));
        assert!(jwk.d.is_some()); // Private key
    }

    #[test]
    fn test_jwk_import_private() {
        let keypair = KeyPair::generate();
        let jwk = JwkKey::from_ed25519_keypair(&keypair);
        let usages = vec![KeyUsage::Sign];

        let web_key = WebCryptoKey::from_jwk(&jwk, &usages).unwrap();

        assert_eq!(web_key.key_type, KeyType::Private);
        assert_eq!(web_key.usages, usages);
    }

    #[test]
    fn test_jwk_import_public() {
        let keypair = KeyPair::generate();
        let public_key = keypair.public_key();
        let jwk = JwkKey::from_ed25519_public_key(&public_key);
        let usages = vec![KeyUsage::Verify];

        let web_key = WebCryptoKey::from_jwk(&jwk, &usages).unwrap();

        assert_eq!(web_key.key_type, KeyType::Public);
        assert_eq!(web_key.usages, usages);
    }

    #[test]
    fn test_extractable_flag() {
        let keypair = KeyPair::generate();
        let web_key =
            WebCryptoKey::from_ed25519_keypair(&keypair, &[KeyUsage::Sign]).with_extractable(false);

        assert!(!web_key.extractable);
        assert!(web_key.to_jwk().is_err());
    }

    #[test]
    fn test_keypair_creation() {
        let keypair = KeyPair::generate();
        let usages = vec![KeyUsage::Sign, KeyUsage::Verify];

        let web_keypair = WebCryptoKeyPair::from_ed25519(&keypair, &usages);

        assert_eq!(web_keypair.public_key.key_type, KeyType::Public);
        assert_eq!(web_keypair.private_key.key_type, KeyType::Private);
    }

    #[test]
    fn test_algorithm_serialization() {
        let algo = Algorithm::ed_dsa();
        let json = serde_json::to_string(&algo).unwrap();
        assert!(json.contains("EdDSA"));
        assert!(json.contains("Ed25519"));

        let deserialized: Algorithm = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized, algo);
    }

    #[test]
    fn test_key_usage_serialization() {
        let usage = KeyUsage::Sign;
        let json = serde_json::to_string(&usage).unwrap();
        assert_eq!(json, "\"sign\"");

        let deserialized: KeyUsage = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized, usage);
    }

    #[test]
    fn test_webcrypto_key_serialization() {
        let keypair = KeyPair::generate();
        let web_key = WebCryptoKey::from_ed25519_keypair(&keypair, &[KeyUsage::Sign]);

        // Use JSON serialization (more appropriate for WebCrypto)
        let serialized = serde_json::to_string(&web_key).unwrap();
        let deserialized: WebCryptoKey = serde_json::from_str(&serialized).unwrap();

        assert_eq!(deserialized.key_type, web_key.key_type);
        assert_eq!(deserialized.extractable, web_key.extractable);
        assert_eq!(deserialized.usages, web_key.usages);
    }

    #[test]
    fn test_symmetric_key() {
        let key = [0x42u8; 32];
        let algo = Algorithm::aes_gcm(256);
        let usages = vec![KeyUsage::Encrypt, KeyUsage::Decrypt];

        let web_key = WebCryptoKey::from_symmetric_key(&key, algo.clone(), &usages);

        assert_eq!(web_key.key_type, KeyType::Secret);
        assert_eq!(web_key.algorithm, algo);
        assert!(web_key.can_use_for(KeyUsage::Encrypt));
        assert!(web_key.can_use_for(KeyUsage::Decrypt));
    }

    #[test]
    fn test_hmac_algorithm() {
        let algo = Algorithm::hmac("SHA-256");
        match algo {
            Algorithm::Hmac { hash } => {
                assert_eq!(hash, "SHA-256");
            }
            _ => panic!("Wrong algorithm type"),
        }
    }

    #[test]
    fn test_hkdf_algorithm() {
        let algo = Algorithm::hkdf("SHA-256");
        match algo {
            Algorithm::Hkdf { hash } => {
                assert_eq!(hash, "SHA-256");
            }
            _ => panic!("Wrong algorithm type"),
        }
    }

    #[test]
    fn test_pbkdf2_algorithm() {
        let salt = vec![1, 2, 3, 4];
        let algo = Algorithm::pbkdf2("SHA-256", 100000, salt.clone());
        match algo {
            Algorithm::Pbkdf2 {
                hash,
                iterations,
                salt: s,
            } => {
                assert_eq!(hash, "SHA-256");
                assert_eq!(iterations, 100000);
                assert_eq!(s, salt);
            }
            _ => panic!("Wrong algorithm type"),
        }
    }
}