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
//! Standard key format support (DER, JWK, PKCS#8).
//!
//! This module provides import/export functionality for standard cryptographic
//! key formats used in various protocols and applications.
//!
//! # Supported Formats
//!
//! - **DER**: Distinguished Encoding Rules (ASN.1 binary format)
//! - **JWK**: JSON Web Key (RFC 7517)
//! - **PKCS#8**: Public-Key Cryptography Standards #8
//!
//! # Example
//!
//! ```rust
//! use chie_crypto::key_formats::{JwkKey, DerKey};
//! use chie_crypto::signing::KeyPair;
//!
//! // Generate a keypair
//! let keypair = KeyPair::generate();
//!
//! // Export to JWK
//! let jwk = JwkKey::from_ed25519_keypair(&keypair);
//! let jwk_json = jwk.to_json().unwrap();
//!
//! // Import from JWK
//! let imported_jwk = JwkKey::from_json(&jwk_json).unwrap();
//! let imported_keypair = imported_jwk.to_ed25519_keypair().unwrap();
//! ```

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

/// Key format errors
#[derive(Debug, Error, Clone, PartialEq, Eq)]
pub enum KeyFormatError {
    /// Invalid DER encoding
    #[error("Invalid DER encoding: {0}")]
    InvalidDer(String),

    /// Invalid JWK format
    #[error("Invalid JWK format: {0}")]
    InvalidJwk(String),

    /// Unsupported key type
    #[error("Unsupported key type: {0}")]
    UnsupportedKeyType(String),

    /// Invalid key length
    #[error("Invalid key length: expected {expected}, got {actual}")]
    InvalidKeyLength { expected: usize, actual: usize },

    /// Serialization error
    #[error("Serialization error: {0}")]
    SerializationError(String),

    /// Missing required field
    #[error("Missing required field: {0}")]
    MissingField(String),
}

/// Result type for key format operations
pub type KeyFormatResult<T> = Result<T, KeyFormatError>;

/// JSON Web Key (JWK) representation
///
/// Implements RFC 7517 for Ed25519 keys (EdDSA curve).
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct JwkKey {
    /// Key type (always "OKP" for EdDSA)
    pub kty: String,

    /// Curve (always "Ed25519")
    #[serde(skip_serializing_if = "Option::is_none")]
    pub crv: Option<String>,

    /// Public key (base64url encoded)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub x: Option<String>,

    /// Private key (base64url encoded)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub d: Option<String>,

    /// Key usage
    #[serde(rename = "use", skip_serializing_if = "Option::is_none")]
    pub key_use: Option<String>,

    /// Key ID
    #[serde(skip_serializing_if = "Option::is_none")]
    pub kid: Option<String>,

    /// Algorithm
    #[serde(skip_serializing_if = "Option::is_none")]
    pub alg: Option<String>,
}

impl JwkKey {
    /// Create a JWK from an Ed25519 keypair
    pub fn from_ed25519_keypair(keypair: &KeyPair) -> Self {
        let public_key = keypair.public_key();
        let secret_key = keypair.secret_key();

        Self {
            kty: "OKP".to_string(),
            crv: Some("Ed25519".to_string()),
            x: Some(base64_url_encode(&public_key)),
            d: Some(base64_url_encode(&secret_key)),
            key_use: Some("sig".to_string()),
            kid: None,
            alg: Some("EdDSA".to_string()),
        }
    }

    /// Create a JWK from an Ed25519 public key
    pub fn from_ed25519_public_key(public_key: &PublicKey) -> Self {
        Self {
            kty: "OKP".to_string(),
            crv: Some("Ed25519".to_string()),
            x: Some(base64_url_encode(public_key)),
            d: None,
            key_use: Some("sig".to_string()),
            kid: None,
            alg: Some("EdDSA".to_string()),
        }
    }

    /// Convert JWK to Ed25519 keypair
    pub fn to_ed25519_keypair(&self) -> KeyFormatResult<KeyPair> {
        // Validate key type
        if self.kty != "OKP" {
            return Err(KeyFormatError::UnsupportedKeyType(self.kty.clone()));
        }

        // Validate curve
        if let Some(crv) = &self.crv {
            if crv != "Ed25519" {
                return Err(KeyFormatError::UnsupportedKeyType(crv.clone()));
            }
        }

        // Extract public key
        let x = self
            .x
            .as_ref()
            .ok_or_else(|| KeyFormatError::MissingField("x".to_string()))?;
        let public_bytes =
            base64_url_decode(x).map_err(|e| KeyFormatError::InvalidJwk(e.to_string()))?;

        if public_bytes.len() != 32 {
            return Err(KeyFormatError::InvalidKeyLength {
                expected: 32,
                actual: public_bytes.len(),
            });
        }

        // Extract private key
        let d = self
            .d
            .as_ref()
            .ok_or_else(|| KeyFormatError::MissingField("d".to_string()))?;
        let secret_bytes =
            base64_url_decode(d).map_err(|e| KeyFormatError::InvalidJwk(e.to_string()))?;

        if secret_bytes.len() != 32 {
            return Err(KeyFormatError::InvalidKeyLength {
                expected: 32,
                actual: secret_bytes.len(),
            });
        }

        // Create keypair
        let mut secret_key = [0u8; 32];
        secret_key.copy_from_slice(&secret_bytes);

        KeyPair::from_secret_key(&secret_key)
            .map_err(|_| KeyFormatError::InvalidJwk("Invalid secret key".to_string()))
    }

    /// Convert JWK to Ed25519 public key
    pub fn to_ed25519_public_key(&self) -> KeyFormatResult<PublicKey> {
        // Validate key type
        if self.kty != "OKP" {
            return Err(KeyFormatError::UnsupportedKeyType(self.kty.clone()));
        }

        // Extract public key
        let x = self
            .x
            .as_ref()
            .ok_or_else(|| KeyFormatError::MissingField("x".to_string()))?;
        let public_bytes =
            base64_url_decode(x).map_err(|e| KeyFormatError::InvalidJwk(e.to_string()))?;

        if public_bytes.len() != 32 {
            return Err(KeyFormatError::InvalidKeyLength {
                expected: 32,
                actual: public_bytes.len(),
            });
        }

        let mut public_key = [0u8; 32];
        public_key.copy_from_slice(&public_bytes);
        Ok(public_key)
    }

    /// Serialize JWK to JSON string
    pub fn to_json(&self) -> KeyFormatResult<String> {
        serde_json::to_string_pretty(self)
            .map_err(|e| KeyFormatError::SerializationError(e.to_string()))
    }

    /// Deserialize JWK from JSON string
    pub fn from_json(json: &str) -> KeyFormatResult<Self> {
        serde_json::from_str(json).map_err(|e| KeyFormatError::SerializationError(e.to_string()))
    }

    /// Set key ID
    pub fn with_kid(mut self, kid: impl Into<String>) -> Self {
        self.kid = Some(kid.into());
        self
    }
}

/// DER (Distinguished Encoding Rules) key representation
///
/// Simple DER encoding for Ed25519 keys.
/// For production use, consider using a full ASN.1 library.
pub struct DerKey;

impl DerKey {
    /// Encode Ed25519 public key to DER format (SubjectPublicKeyInfo)
    ///
    /// This creates a simple DER structure for Ed25519 public keys.
    pub fn encode_ed25519_public_key(public_key: &PublicKey) -> Vec<u8> {
        // Simple DER encoding for Ed25519 public key
        // This is a simplified version; production code should use proper ASN.1 library
        let mut der = Vec::with_capacity(44);

        // SEQUENCE
        der.push(0x30);
        der.push(42); // Length

        // AlgorithmIdentifier SEQUENCE
        der.push(0x30);
        der.push(5);

        // OID for Ed25519 (1.3.101.112)
        der.push(0x06);
        der.push(3);
        der.extend_from_slice(&[0x2B, 0x65, 0x70]);

        // BIT STRING for public key
        der.push(0x03);
        der.push(33); // Length (32 + 1 for unused bits)
        der.push(0x00); // No unused bits

        // Public key bytes
        der.extend_from_slice(public_key);

        der
    }

    /// Decode Ed25519 public key from DER format (SubjectPublicKeyInfo)
    pub fn decode_ed25519_public_key(der: &[u8]) -> KeyFormatResult<PublicKey> {
        // Simple DER decoder
        // This is a simplified version; production code should use proper ASN.1 library

        if der.len() < 44 {
            return Err(KeyFormatError::InvalidDer("DER data too short".to_string()));
        }

        // Verify SEQUENCE tag
        if der[0] != 0x30 {
            return Err(KeyFormatError::InvalidDer(
                "Expected SEQUENCE tag".to_string(),
            ));
        }

        // Find BIT STRING with public key
        // Skip to the public key part (simplified parsing)
        let key_start = der.len() - 32;
        if key_start >= der.len() {
            return Err(KeyFormatError::InvalidDer(
                "Invalid DER structure".to_string(),
            ));
        }

        let mut public_key = [0u8; 32];
        public_key.copy_from_slice(&der[key_start..]);

        Ok(public_key)
    }

    /// Encode Ed25519 private key to DER format (PKCS#8)
    ///
    /// Creates a PKCS#8 PrivateKeyInfo structure for Ed25519.
    pub fn encode_ed25519_private_key(secret_key: &SecretKey) -> Vec<u8> {
        // Simple PKCS#8 encoding for Ed25519 private key
        let mut der = Vec::with_capacity(48);

        // SEQUENCE
        der.push(0x30);
        der.push(46); // Length

        // Version (0)
        der.push(0x02);
        der.push(0x01);
        der.push(0x00);

        // AlgorithmIdentifier SEQUENCE
        der.push(0x30);
        der.push(5);

        // OID for Ed25519
        der.push(0x06);
        der.push(3);
        der.extend_from_slice(&[0x2B, 0x65, 0x70]);

        // OCTET STRING containing private key
        der.push(0x04);
        der.push(34); // Length

        // Inner OCTET STRING for the actual key
        der.push(0x04);
        der.push(32);
        der.extend_from_slice(secret_key);

        der
    }

    /// Decode Ed25519 private key from DER format (PKCS#8)
    pub fn decode_ed25519_private_key(der: &[u8]) -> KeyFormatResult<SecretKey> {
        if der.len() < 48 {
            return Err(KeyFormatError::InvalidDer(
                "DER data too short for private key".to_string(),
            ));
        }

        // Find the private key octets (simplified parsing)
        let key_start = der.len() - 32;
        if key_start >= der.len() {
            return Err(KeyFormatError::InvalidDer(
                "Invalid DER structure".to_string(),
            ));
        }

        let mut secret_key = [0u8; 32];
        secret_key.copy_from_slice(&der[key_start..]);

        Ok(secret_key)
    }
}

/// Base64url encoding (URL-safe, no padding)
fn base64_url_encode(data: &[u8]) -> String {
    use base64::{Engine as _, engine::general_purpose::URL_SAFE_NO_PAD};
    URL_SAFE_NO_PAD.encode(data)
}

/// Base64url decoding
fn base64_url_decode(data: &str) -> Result<Vec<u8>, String> {
    use base64::{Engine as _, engine::general_purpose::URL_SAFE_NO_PAD};
    URL_SAFE_NO_PAD.decode(data).map_err(|e| e.to_string())
}

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

    #[test]
    fn test_jwk_keypair_roundtrip() {
        let keypair = KeyPair::generate();

        // Convert to JWK and back
        let jwk = JwkKey::from_ed25519_keypair(&keypair);
        let restored = jwk.to_ed25519_keypair().unwrap();

        // Keys should match
        assert_eq!(keypair.public_key(), restored.public_key());
        assert_eq!(keypair.secret_key(), restored.secret_key());
    }

    #[test]
    fn test_jwk_public_key_roundtrip() {
        let keypair = KeyPair::generate();
        let public_key = keypair.public_key();

        // Convert to JWK and back
        let jwk = JwkKey::from_ed25519_public_key(&public_key);
        let restored = jwk.to_ed25519_public_key().unwrap();

        assert_eq!(public_key, restored);
    }

    #[test]
    fn test_jwk_json_serialization() {
        let keypair = KeyPair::generate();
        let jwk = JwkKey::from_ed25519_keypair(&keypair);

        // Serialize to JSON and back
        let json = jwk.to_json().unwrap();
        let restored = JwkKey::from_json(&json).unwrap();

        assert_eq!(jwk, restored);
    }

    #[test]
    fn test_jwk_with_kid() {
        let keypair = KeyPair::generate();
        let jwk = JwkKey::from_ed25519_keypair(&keypair).with_kid("my-key-id");

        assert_eq!(jwk.kid, Some("my-key-id".to_string()));
    }

    #[test]
    fn test_jwk_validation() {
        // Invalid key type
        let invalid_jwk = JwkKey {
            kty: "RSA".to_string(),
            crv: None,
            x: None,
            d: None,
            key_use: None,
            kid: None,
            alg: None,
        };

        assert!(invalid_jwk.to_ed25519_keypair().is_err());
    }

    #[test]
    fn test_der_public_key_roundtrip() {
        let keypair = KeyPair::generate();
        let public_key = keypair.public_key();

        // Encode to DER and decode back
        let der = DerKey::encode_ed25519_public_key(&public_key);
        let restored = DerKey::decode_ed25519_public_key(&der).unwrap();

        assert_eq!(public_key, restored);
    }

    #[test]
    fn test_der_private_key_roundtrip() {
        let keypair = KeyPair::generate();
        let secret_key = keypair.secret_key();

        // Encode to DER and decode back
        let der = DerKey::encode_ed25519_private_key(&secret_key);
        let restored = DerKey::decode_ed25519_private_key(&der).unwrap();

        assert_eq!(secret_key, restored);
    }

    #[test]
    fn test_der_public_key_structure() {
        let keypair = KeyPair::generate();
        let der = DerKey::encode_ed25519_public_key(&keypair.public_key());

        // Verify it starts with SEQUENCE tag
        assert_eq!(der[0], 0x30);

        // Verify it contains Ed25519 OID
        assert!(der.windows(3).any(|w| w == [0x2B, 0x65, 0x70]));
    }

    #[test]
    fn test_base64_url_encoding() {
        let data = b"Hello, World!";
        let encoded = base64_url_encode(data);
        let decoded = base64_url_decode(&encoded).unwrap();

        assert_eq!(data, &decoded[..]);

        // URL-safe encoding shouldn't contain +, /, or =
        assert!(!encoded.contains('+'));
        assert!(!encoded.contains('/'));
        assert!(!encoded.contains('='));
    }

    #[test]
    fn test_jwk_missing_fields() {
        let jwk = JwkKey {
            kty: "OKP".to_string(),
            crv: Some("Ed25519".to_string()),
            x: None, // Missing public key
            d: Some("test".to_string()),
            key_use: None,
            kid: None,
            alg: None,
        };

        assert!(jwk.to_ed25519_keypair().is_err());
    }
}