exochain-identity 0.2.0-beta

EXOCHAIN constitutional trust fabric — privacy-preserving identity adjudication
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
// Copyright 2026 Exochain Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0

//! Vault encryption module for EXOCHAIN identity-bound AEAD.
//!
//! Provides XChaCha20-Poly1305 authenticated encryption with associated data
//! (AEAD), where the associated data is typically DID bytes so that ciphertext
//! is cryptographically bound to a specific identity.
//!
//! Key derivation uses HKDF-SHA256 from an Ed25519 secret key with a
//! protocol-domain salt.
//!
//! Ciphertext format: `[24-byte nonce][encrypted payload][16-byte Poly1305 tag]`.

use chacha20poly1305::{
    XChaCha20Poly1305,
    aead::{Aead, KeyInit, Payload},
};
use exo_core::SecretKey;
use hkdf::Hkdf;
use sha2::Sha256;
use zeroize::{Zeroize, Zeroizing};

use crate::error::IdentityError;

/// Size of the XChaCha20-Poly1305 nonce in bytes.
pub const VAULT_NONCE_SIZE: usize = 24;

/// Size of the XChaCha20-Poly1305 nonce in bytes.
const NONCE_SIZE: usize = VAULT_NONCE_SIZE;

/// Size of the Poly1305 authentication tag in bytes.
const TAG_SIZE: usize = 16;

/// Minimum ciphertext length: nonce + tag (payload can be empty).
const MIN_CIPHERTEXT_LEN: usize = NONCE_SIZE + TAG_SIZE;

/// HKDF extraction salt for identity vault keys.
const VAULT_HKDF_SALT_DOMAIN: &[u8] = b"exo.identity.vault.hkdf.salt.v1";

/// AEAD vault encryptor using XChaCha20-Poly1305.
///
/// Wraps a 256-bit symmetric key derived from an Ed25519 secret key via
/// HKDF-SHA256.  The key material is zeroized on drop.
pub struct VaultEncryptor {
    key: [u8; 32],
}

impl VaultEncryptor {
    /// Create a `VaultEncryptor` from raw 256-bit key material.
    #[must_use]
    pub fn from_key(key: [u8; 32]) -> Self {
        Self { key }
    }

    /// Derive a vault encryption key from an Ed25519 secret key using
    /// HKDF-SHA256.
    ///
    /// The `context` bytes are used as the HKDF info parameter, allowing
    /// the same secret key to produce different vault keys for different
    /// purposes.
    ///
    /// # Errors
    ///
    /// Returns `IdentityError::VaultKeyDerivationFailed` if HKDF expand
    /// fails (in practice this cannot occur for a 32-byte output).
    pub fn derive_key(secret: &SecretKey, context: &[u8]) -> Result<Self, IdentityError> {
        let hk = Hkdf::<Sha256>::new(Some(VAULT_HKDF_SALT_DOMAIN), secret.as_bytes());
        let mut okm = Zeroizing::new([0u8; 32]);
        hk.expand(context, &mut *okm)
            .map_err(|e| IdentityError::VaultKeyDerivationFailed(e.to_string()))?;
        Ok(Self { key: *okm })
    }

    /// Legacy encryption entrypoint retained for API compatibility.
    ///
    /// Vault encryption must receive an explicit nonce from the caller so the
    /// runtime path remains deterministic and the nonce provenance is auditable.
    /// Use [`encrypt_with_nonce`](Self::encrypt_with_nonce) for supported
    /// encryption.
    ///
    /// # Errors
    ///
    /// Always returns `IdentityError::VaultNonceRequired`.
    pub fn encrypt(
        &self,
        _plaintext: &[u8],
        _associated_data: &[u8],
    ) -> Result<Vec<u8>, IdentityError> {
        Err(IdentityError::VaultNonceRequired)
    }

    /// Decrypt a ciphertext produced by [`encrypt_with_nonce`](Self::encrypt_with_nonce).
    ///
    /// `associated_data` must match the value used during encryption.
    ///
    /// # Errors
    ///
    /// Returns `IdentityError::VaultCiphertextTooShort` if the ciphertext
    /// is shorter than `NONCE_SIZE + TAG_SIZE`.
    ///
    /// Returns `IdentityError::VaultDecryptionFailed` if authentication
    /// fails (wrong key, tampered ciphertext, or wrong associated data).
    pub fn decrypt(
        &self,
        ciphertext: &[u8],
        associated_data: &[u8],
    ) -> Result<Vec<u8>, IdentityError> {
        if ciphertext.len() < MIN_CIPHERTEXT_LEN {
            return Err(IdentityError::VaultCiphertextTooShort);
        }

        let (nonce_bytes, encrypted) = ciphertext.split_at(NONCE_SIZE);
        let nonce = chacha20poly1305::XNonce::from_slice(nonce_bytes);

        let cipher = XChaCha20Poly1305::new(self.cipher_key());

        cipher
            .decrypt(
                nonce,
                Payload {
                    msg: encrypted,
                    aad: associated_data,
                },
            )
            .map_err(|_| IdentityError::VaultDecryptionFailed)
    }

    /// Return a reference to the raw key bytes (for testing/inspection).
    #[must_use]
    pub fn key_bytes(&self) -> &[u8; 32] {
        &self.key
    }

    /// Encrypt with an explicit caller-supplied nonce.
    ///
    /// `associated_data` should be the DID bytes so the ciphertext is bound
    /// to the identity. The nonce must be unique for the derived key and must
    /// be supplied by the caller from an auditable deterministic runtime input.
    ///
    /// Returns `[24-byte nonce][ciphertext][16-byte tag]`.
    ///
    /// # Errors
    ///
    /// Returns `IdentityError::InvalidVaultNonce` when the nonce fails local
    /// validation.
    ///
    /// Returns `IdentityError::VaultEncryptionFailed` on cipher failure.
    pub fn encrypt_with_nonce(
        &self,
        plaintext: &[u8],
        associated_data: &[u8],
        nonce: &[u8; VAULT_NONCE_SIZE],
    ) -> Result<Vec<u8>, IdentityError> {
        Self::validate_nonce(nonce)?;

        let cipher = XChaCha20Poly1305::new(self.cipher_key());
        let xcnonce = chacha20poly1305::XNonce::from_slice(nonce);

        let encrypted = cipher
            .encrypt(
                xcnonce,
                Payload {
                    msg: plaintext,
                    aad: associated_data,
                },
            )
            .map_err(|e| IdentityError::VaultEncryptionFailed(e.to_string()))?;

        // Format: [nonce][encrypted payload with appended tag]
        let mut out = Vec::with_capacity(NONCE_SIZE + encrypted.len());
        out.extend_from_slice(nonce);
        out.extend_from_slice(&encrypted);
        Ok(out)
    }

    // ---- internal helpers ----

    fn validate_nonce(nonce: &[u8; VAULT_NONCE_SIZE]) -> Result<(), IdentityError> {
        if nonce.iter().all(|byte| *byte == 0) {
            return Err(IdentityError::InvalidVaultNonce {
                reason: "nonce must not be all zero".into(),
            });
        }

        Ok(())
    }

    /// Build the `chacha20poly1305` key type from our raw bytes.
    fn cipher_key(&self) -> &chacha20poly1305::Key {
        chacha20poly1305::Key::from_slice(&self.key)
    }
}

impl Drop for VaultEncryptor {
    fn drop(&mut self) {
        self.key.zeroize();
    }
}

impl core::fmt::Debug for VaultEncryptor {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("VaultEncryptor")
            .field("key", &"***")
            .finish()
    }
}

// ===========================================================================
// Tests
// ===========================================================================

#[cfg(test)]
mod tests {
    use exo_core::crypto::generate_keypair;

    use super::*;

    /// Helper: create a VaultEncryptor from deterministic test key material.
    fn test_encryptor() -> VaultEncryptor {
        VaultEncryptor::from_key([0x42; 32])
    }

    fn test_nonce(tag: u8) -> [u8; NONCE_SIZE] {
        [tag; NONCE_SIZE]
    }

    fn encrypt_for_test(
        enc: &VaultEncryptor,
        plaintext: &[u8],
        associated_data: &[u8],
        nonce_tag: u8,
    ) -> Vec<u8> {
        enc.encrypt_with_nonce(plaintext, associated_data, &test_nonce(nonce_tag))
            .expect("encrypt_with_nonce")
    }

    #[test]
    fn encrypt_decrypt_round_trip() {
        let enc = test_encryptor();
        let plaintext = b"secret identity data";
        let ad = b"did:exo:alice";

        let ct = encrypt_for_test(&enc, plaintext, ad, 1);
        let pt = enc.decrypt(&ct, ad).expect("decrypt");
        assert_eq!(pt, plaintext);
    }

    #[test]
    fn encrypt_requires_caller_supplied_nonce() {
        let enc = test_encryptor();
        let result = enc.encrypt(b"secret identity data", b"did:exo:alice");

        assert!(
            matches!(result, Err(IdentityError::VaultNonceRequired)),
            "implicit nonce generation must fail closed, got {result:?}"
        );
    }

    #[test]
    fn encrypt_source_does_not_generate_internal_nonce() {
        let source = include_str!("vault.rs");
        let production = match source.split("#[cfg(test)]").next() {
            Some(production) => production,
            None => panic!("test boundary marker must be present"),
        };

        assert!(
            !production.contains("random_nonce"),
            "vault encryption must not hide nonce generation inside production logic"
        );
        assert!(
            !production.contains("OsRng"),
            "vault encryption must not use OS randomness in production logic"
        );
        assert!(
            !production.contains("fill_bytes"),
            "vault encryption must not fill nonce bytes from hidden randomness"
        );
    }

    #[test]
    fn encrypt_with_nonce_rejects_all_zero_nonce() {
        let enc = test_encryptor();
        let result = enc.encrypt_with_nonce(b"secret identity data", b"did:exo:alice", &[0u8; 24]);

        assert!(
            matches!(result, Err(IdentityError::InvalidVaultNonce { .. })),
            "zero nonce must be rejected, got {result:?}"
        );
    }

    #[test]
    fn tampered_ciphertext_fails() {
        let enc = test_encryptor();
        let plaintext = b"do not tamper";
        let ad = b"did:exo:bob";

        let mut ct = encrypt_for_test(&enc, plaintext, ad, 2);
        // Flip a byte in the encrypted payload (after nonce)
        let idx = NONCE_SIZE + 1;
        ct[idx] ^= 0xff;

        let result = enc.decrypt(&ct, ad);
        assert!(
            matches!(result, Err(IdentityError::VaultDecryptionFailed)),
            "expected VaultDecryptionFailed, got {result:?}"
        );
    }

    #[test]
    fn wrong_key_fails() {
        let enc1 = VaultEncryptor::from_key([0x11; 32]);
        let enc2 = VaultEncryptor::from_key([0x22; 32]);
        let plaintext = b"key mismatch";
        let ad = b"did:exo:carol";

        let ct = encrypt_for_test(&enc1, plaintext, ad, 3);
        let result = enc2.decrypt(&ct, ad);
        assert!(
            matches!(result, Err(IdentityError::VaultDecryptionFailed)),
            "expected VaultDecryptionFailed, got {result:?}"
        );
    }

    #[test]
    fn wrong_associated_data_fails() {
        let enc = test_encryptor();
        let plaintext = b"bound to identity";
        let ad_a = b"did:exo:alice";
        let ad_b = b"did:exo:eve";

        let ct = encrypt_for_test(&enc, plaintext, ad_a, 4);
        let result = enc.decrypt(&ct, ad_b);
        assert!(
            matches!(result, Err(IdentityError::VaultDecryptionFailed)),
            "expected VaultDecryptionFailed, got {result:?}"
        );
    }

    #[test]
    fn empty_plaintext() {
        let enc = test_encryptor();
        let ad = b"did:exo:empty";

        let ct = encrypt_for_test(&enc, b"", ad, 5);
        assert_eq!(
            ct.len(),
            NONCE_SIZE + TAG_SIZE,
            "empty plaintext produces nonce + tag only"
        );

        let pt = enc.decrypt(&ct, ad).expect("decrypt");
        assert!(pt.is_empty());
    }

    #[test]
    fn large_plaintext() {
        let enc = test_encryptor();
        let plaintext = vec![0xab_u8; 1_000_000]; // 1 MB
        let ad = b"did:exo:large";

        let ct = encrypt_for_test(&enc, &plaintext, ad, 6);
        let pt = enc.decrypt(&ct, ad).expect("decrypt");
        assert_eq!(pt, plaintext);
    }

    #[test]
    fn derive_key_deterministic() {
        let (_, sk) = generate_keypair();
        let context = b"vault-test-context";

        let enc1 = VaultEncryptor::derive_key(&sk, context).expect("derive_key");
        let enc2 = VaultEncryptor::derive_key(&sk, context).expect("derive_key");

        assert_eq!(enc1.key_bytes(), enc2.key_bytes());
    }

    #[test]
    fn derive_key_different_contexts() {
        let (_, sk) = generate_keypair();

        let enc1 = VaultEncryptor::derive_key(&sk, b"context-alpha").expect("derive_key");
        let enc2 = VaultEncryptor::derive_key(&sk, b"context-beta").expect("derive_key");

        assert_ne!(enc1.key_bytes(), enc2.key_bytes());
    }

    #[test]
    fn derive_key_uses_protocol_bound_hkdf_salt() {
        let (_, sk) = generate_keypair();
        let context = b"vault-test-context";
        let enc = VaultEncryptor::derive_key(&sk, context).expect("derive_key");

        let unsalted_hkdf = Hkdf::<Sha256>::new(None, sk.as_bytes());
        let mut unsalted = [0u8; 32];
        unsalted_hkdf
            .expand(context, &mut unsalted)
            .expect("unsalted HKDF expansion");

        assert_ne!(
            enc.key_bytes(),
            &unsalted,
            "vault key derivation must not match HKDF extraction with an absent salt"
        );

        let source = include_str!("vault.rs");
        let production = match source.split("#[cfg(test)]").next() {
            Some(production) => production,
            None => panic!("test boundary marker must be present"),
        };

        assert!(
            production.contains("VAULT_HKDF_SALT_DOMAIN"),
            "derive_key must use an explicit protocol-domain HKDF salt"
        );
        assert!(
            !production.contains("Hkdf::<Sha256>::new(None"),
            "derive_key must not use HKDF extraction with an absent salt"
        );
    }

    #[test]
    fn derive_key_source_zeroizes_hkdf_output_buffer() {
        let source = include_str!("vault.rs");
        let production = match source.split("#[cfg(test)]").next() {
            Some(production) => production,
            None => panic!("test boundary marker must be present"),
        };

        assert!(
            production.contains("Zeroizing::new([0u8; 32])"),
            "derive_key must hold HKDF output in an auto-zeroizing buffer"
        );
        assert!(
            !production.contains("let mut okm = [0u8; 32]"),
            "derive_key must not leave a plain stack copy of derived key material"
        );
    }

    #[test]
    fn zeroize_on_drop() {
        // Verify that VaultEncryptor implements the Zeroize trait via Drop.
        // We construct, read the key, drop, and verify via a copy of the pointer
        // that the type system enforces Zeroize.
        fn assert_zeroize_impl<T: Zeroize>() {}
        assert_zeroize_impl::<[u8; 32]>(); // underlying storage implements Zeroize

        // Functional check: create an encryptor, do work, drop it.
        let enc = test_encryptor();
        let key_copy = *enc.key_bytes();
        // Key was non-zero before drop
        assert_ne!(key_copy, [0u8; 32]);
        // After drop the struct's Drop impl calls zeroize.
        // We can't safely read freed memory, but we verify the Drop impl
        // compiles and the Zeroize trait is used on [u8; 32].
        drop(enc);
    }
}