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
//! HMAC-based authentication for message integrity.
//!
//! This module provides HMAC (Hash-based Message Authentication Code) functionality
//! for ensuring message integrity and authenticity in the CHIE protocol.
//!
//! # Features
//!
//! - HMAC-SHA256 and HMAC-BLAKE3 support
//! - Constant-time MAC verification
//! - Key derivation for HMAC keys
//! - Tagged authentication for different message types
//!
//! # Examples
//!
//! ```
//! use chie_crypto::hmac::{HmacKey, HmacTag, compute_hmac, verify_hmac};
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // Generate a random HMAC key
//! let key = HmacKey::generate();
//!
//! // Compute HMAC for a message
//! let message = b"Hello, CHIE!";
//! let tag = compute_hmac(&key, message);
//!
//! // Verify the HMAC tag
//! assert!(verify_hmac(&key, message, &tag));
//!
//! // Verification fails for wrong message
//! assert!(!verify_hmac(&key, b"Wrong message", &tag));
//! # Ok(())
//! # }
//! ```
//!
//! # Security Considerations
//!
//! - HMAC keys should be at least 32 bytes (256 bits)
//! - Use constant-time comparison for MAC verification
//! - Never reuse HMAC keys across different protocols
//! - Rotate HMAC keys periodically

use blake3;
use serde::{Deserialize, Serialize};
use sha2::Sha256;
use zeroize::{Zeroize, ZeroizeOnDrop};

use crate::ct::ct_eq;

// Serde helper for Vec<u8>
mod serde_bytes {
    use serde::{Deserialize, Deserializer, Serializer};

    pub fn serialize<S>(bytes: &[u8], serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_bytes(bytes)
    }

    pub fn deserialize<'de, D>(deserializer: D) -> Result<Vec<u8>, D::Error>
    where
        D: Deserializer<'de>,
    {
        <Vec<u8>>::deserialize(deserializer)
    }
}

/// HMAC errors.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum HmacError {
    /// Invalid key size
    InvalidKeySize,
    /// Invalid tag size
    InvalidTagSize,
    /// Verification failed
    VerificationFailed,
    /// Serialization error
    SerializationError(String),
}

impl std::fmt::Display for HmacError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::InvalidKeySize => write!(f, "Invalid HMAC key size"),
            Self::InvalidTagSize => write!(f, "Invalid HMAC tag size"),
            Self::VerificationFailed => write!(f, "HMAC verification failed"),
            Self::SerializationError(e) => write!(f, "Serialization error: {}", e),
        }
    }
}

impl std::error::Error for HmacError {}

/// HMAC result type.
pub type HmacResult<T> = Result<T, HmacError>;

/// HMAC key size in bytes.
pub const HMAC_KEY_SIZE: usize = 32;

/// HMAC tag size for SHA256 in bytes.
pub const HMAC_SHA256_TAG_SIZE: usize = 32;

/// HMAC tag size for BLAKE3 in bytes.
pub const HMAC_BLAKE3_TAG_SIZE: usize = 32;

/// HMAC key for message authentication.
#[derive(Clone, Zeroize, ZeroizeOnDrop, Serialize, Deserialize)]
pub struct HmacKey {
    #[serde(with = "serde_bytes")]
    key: Vec<u8>,
}

impl HmacKey {
    /// Generate a random HMAC key.
    pub fn generate() -> Self {
        use rand::Rng as _;
        let mut rng = rand::rng();
        let mut key = vec![0u8; HMAC_KEY_SIZE];
        rng.fill_bytes(&mut key[..]);
        Self { key }
    }

    /// Create an HMAC key from bytes.
    ///
    /// # Errors
    ///
    /// Returns `HmacError::InvalidKeySize` if the key is not exactly 32 bytes.
    pub fn from_bytes(bytes: &[u8]) -> HmacResult<Self> {
        if bytes.len() != HMAC_KEY_SIZE {
            return Err(HmacError::InvalidKeySize);
        }
        Ok(Self {
            key: bytes.to_vec(),
        })
    }

    /// Get the key bytes.
    pub fn as_bytes(&self) -> &[u8] {
        &self.key
    }

    /// Convert to bytes.
    pub fn to_bytes(&self) -> Vec<u8> {
        self.key.clone()
    }

    /// Derive an HMAC key from a password using PBKDF2.
    pub fn derive_from_password(password: &[u8], salt: &[u8], iterations: u32) -> Self {
        use pbkdf2::pbkdf2_hmac;
        let mut key = vec![0u8; HMAC_KEY_SIZE];
        pbkdf2_hmac::<Sha256>(password, salt, iterations, &mut key);
        Self { key }
    }
}

impl std::fmt::Debug for HmacKey {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("HmacKey")
            .field("key", &"[REDACTED]")
            .finish()
    }
}

/// HMAC tag (authentication code).
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct HmacTag {
    #[serde(with = "serde_bytes")]
    tag: Vec<u8>,
}

impl HmacTag {
    /// Create an HMAC tag from bytes.
    pub fn from_bytes(bytes: &[u8]) -> Self {
        Self {
            tag: bytes.to_vec(),
        }
    }

    /// Get the tag bytes.
    pub fn as_bytes(&self) -> &[u8] {
        &self.tag
    }

    /// Convert to bytes.
    pub fn to_bytes(&self) -> Vec<u8> {
        self.tag.clone()
    }

    /// Verify this tag against another tag in constant time.
    pub fn verify(&self, other: &Self) -> bool {
        ct_eq(&self.tag, &other.tag)
    }
}

/// Compute HMAC-SHA256 for a message.
pub fn compute_hmac_sha256(key: &HmacKey, message: &[u8]) -> HmacTag {
    use hmac::digest::KeyInit;
    use hmac::{Hmac, Mac};
    type HmacSha256 = Hmac<Sha256>;

    let mut mac = <HmacSha256 as KeyInit>::new_from_slice(key.as_bytes())
        .expect("HMAC can take key of any size");
    mac.update(message);
    let result = mac.finalize();
    HmacTag::from_bytes(&result.into_bytes())
}

/// Compute HMAC-BLAKE3 for a message (using keyed BLAKE3).
pub fn compute_hmac_blake3(key: &HmacKey, message: &[u8]) -> HmacTag {
    // BLAKE3 has native keyed hashing support
    let key_array: [u8; 32] = key.as_bytes().try_into().expect("key is 32 bytes");
    let hash = blake3::keyed_hash(&key_array, message);
    HmacTag::from_bytes(hash.as_bytes())
}

/// Compute HMAC for a message (defaults to BLAKE3 for performance).
pub fn compute_hmac(key: &HmacKey, message: &[u8]) -> HmacTag {
    compute_hmac_blake3(key, message)
}

/// Verify HMAC tag in constant time.
pub fn verify_hmac(key: &HmacKey, message: &[u8], tag: &HmacTag) -> bool {
    let computed = compute_hmac(key, message);
    computed.verify(tag)
}

/// Verify HMAC-SHA256 tag in constant time.
pub fn verify_hmac_sha256(key: &HmacKey, message: &[u8], tag: &HmacTag) -> bool {
    let computed = compute_hmac_sha256(key, message);
    computed.verify(tag)
}

/// Verify HMAC-BLAKE3 tag in constant time.
pub fn verify_hmac_blake3(key: &HmacKey, message: &[u8], tag: &HmacTag) -> bool {
    let computed = compute_hmac_blake3(key, message);
    computed.verify(tag)
}

/// Tagged HMAC for domain separation.
///
/// This adds a context tag to the message before computing HMAC,
/// preventing HMAC values from being reused across different contexts.
pub fn compute_tagged_hmac(key: &HmacKey, context: &[u8], message: &[u8]) -> HmacTag {
    let key_array: [u8; 32] = key.as_bytes().try_into().expect("key is 32 bytes");
    let mut hasher = blake3::Hasher::new_keyed(&key_array);
    hasher.update(context);
    hasher.update(message);
    HmacTag::from_bytes(hasher.finalize().as_bytes())
}

/// Verify tagged HMAC in constant time.
pub fn verify_tagged_hmac(key: &HmacKey, context: &[u8], message: &[u8], tag: &HmacTag) -> bool {
    let computed = compute_tagged_hmac(key, context, message);
    computed.verify(tag)
}

/// Authenticated message containing both data and HMAC tag.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct AuthenticatedMessage {
    #[serde(with = "serde_bytes")]
    data: Vec<u8>,
    tag: HmacTag,
}

impl AuthenticatedMessage {
    /// Create a new authenticated message.
    pub fn new(key: &HmacKey, data: Vec<u8>) -> Self {
        let tag = compute_hmac(key, &data);
        Self { data, tag }
    }

    /// Create a new tagged authenticated message.
    pub fn new_tagged(key: &HmacKey, context: &[u8], data: Vec<u8>) -> Self {
        let tag = compute_tagged_hmac(key, context, &data);
        Self { data, tag }
    }

    /// Verify and extract the data.
    ///
    /// # Errors
    ///
    /// Returns `HmacError::VerificationFailed` if the HMAC tag is invalid.
    pub fn verify(self, key: &HmacKey) -> HmacResult<Vec<u8>> {
        if verify_hmac(key, &self.data, &self.tag) {
            Ok(self.data)
        } else {
            Err(HmacError::VerificationFailed)
        }
    }

    /// Verify and extract the data (tagged version).
    ///
    /// # Errors
    ///
    /// Returns `HmacError::VerificationFailed` if the HMAC tag is invalid.
    pub fn verify_tagged(self, key: &HmacKey, context: &[u8]) -> HmacResult<Vec<u8>> {
        if verify_tagged_hmac(key, context, &self.data, &self.tag) {
            Ok(self.data)
        } else {
            Err(HmacError::VerificationFailed)
        }
    }

    /// Get the data without verification (unsafe).
    pub fn data(&self) -> &[u8] {
        &self.data
    }

    /// Get the HMAC tag.
    pub fn tag(&self) -> &HmacTag {
        &self.tag
    }

    /// Serialize to bytes.
    pub fn to_bytes(&self) -> HmacResult<Vec<u8>> {
        crate::codec::encode(self).map_err(|e| HmacError::SerializationError(e.to_string()))
    }

    /// Deserialize from bytes.
    pub fn from_bytes(bytes: &[u8]) -> HmacResult<Self> {
        crate::codec::decode(bytes).map_err(|e| HmacError::SerializationError(e.to_string()))
    }
}

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

    #[test]
    fn test_hmac_basic() {
        let key = HmacKey::generate();
        let message = b"Hello, CHIE!";

        let tag = compute_hmac(&key, message);
        assert!(verify_hmac(&key, message, &tag));

        // Wrong message should fail
        assert!(!verify_hmac(&key, b"Wrong message", &tag));
    }

    #[test]
    fn test_hmac_sha256() {
        let key = HmacKey::generate();
        let message = b"Test message";

        let tag = compute_hmac_sha256(&key, message);
        assert!(verify_hmac_sha256(&key, message, &tag));
        assert_eq!(tag.as_bytes().len(), HMAC_SHA256_TAG_SIZE);
    }

    #[test]
    fn test_hmac_blake3() {
        let key = HmacKey::generate();
        let message = b"Test message";

        let tag = compute_hmac_blake3(&key, message);
        assert!(verify_hmac_blake3(&key, message, &tag));
        assert_eq!(tag.as_bytes().len(), HMAC_BLAKE3_TAG_SIZE);
    }

    #[test]
    fn test_tagged_hmac() {
        let key = HmacKey::generate();
        let context = b"CHIE:BandwidthProof";
        let message = b"1234567890";

        let tag = compute_tagged_hmac(&key, context, message);
        assert!(verify_tagged_hmac(&key, context, message, &tag));

        // Wrong context should fail
        assert!(!verify_tagged_hmac(&key, b"wrong", message, &tag));
    }

    #[test]
    fn test_authenticated_message() {
        let key = HmacKey::generate();
        let data = b"Secret data".to_vec();

        let msg = AuthenticatedMessage::new(&key, data.clone());
        let verified = msg.verify(&key).unwrap();
        assert_eq!(verified, data);
    }

    #[test]
    fn test_authenticated_message_fails() {
        let key1 = HmacKey::generate();
        let key2 = HmacKey::generate();
        let data = b"Secret data".to_vec();

        let msg = AuthenticatedMessage::new(&key1, data);
        assert!(msg.verify(&key2).is_err());
    }

    #[test]
    fn test_tagged_authenticated_message() {
        let key = HmacKey::generate();
        let context = b"CHIE:Chunk";
        let data = b"Chunk data".to_vec();

        let msg = AuthenticatedMessage::new_tagged(&key, context, data.clone());
        let verified = msg.verify_tagged(&key, context).unwrap();
        assert_eq!(verified, data);
    }

    #[test]
    fn test_hmac_key_from_bytes() {
        let bytes = [42u8; HMAC_KEY_SIZE];
        let key = HmacKey::from_bytes(&bytes).unwrap();
        assert_eq!(key.as_bytes(), &bytes);
    }

    #[test]
    fn test_hmac_key_invalid_size() {
        let bytes = [42u8; 16]; // Wrong size
        assert!(HmacKey::from_bytes(&bytes).is_err());
    }

    #[test]
    fn test_hmac_key_derive_from_password() {
        let password = b"my secret password";
        let salt = b"unique salt";
        let key1 = HmacKey::derive_from_password(password, salt, 10000);
        let key2 = HmacKey::derive_from_password(password, salt, 10000);

        // Same password and salt should give same key
        assert_eq!(key1.as_bytes(), key2.as_bytes());

        // Different salt should give different key
        let key3 = HmacKey::derive_from_password(password, b"different salt", 10000);
        assert_ne!(key1.as_bytes(), key3.as_bytes());
    }

    #[test]
    fn test_serialization() {
        let key = HmacKey::generate();
        let data = b"Test data".to_vec();
        let msg = AuthenticatedMessage::new(&key, data);

        let bytes = msg.to_bytes().unwrap();
        let deserialized = AuthenticatedMessage::from_bytes(&bytes).unwrap();

        assert_eq!(msg.data, deserialized.data);
        assert_eq!(msg.tag, deserialized.tag);
    }

    #[test]
    fn test_constant_time_verification() {
        let key = HmacKey::generate();
        let message = b"Test message";
        let tag1 = compute_hmac(&key, message);
        let tag2 = compute_hmac(&key, message);

        assert!(tag1.verify(&tag2));

        // Create a modified tag
        let mut wrong_tag = tag1.clone();
        wrong_tag.tag[0] ^= 1;

        assert!(!tag1.verify(&wrong_tag));
    }
}