anya-core 1.2.0

Enterprise-grade Bitcoin Infrastructure Platform
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
// Symmetric Encryption Module
// [AIR-2][AIS-2][BPC-2][AIT-2][RES-2]
//
// This module provides symmetric encryption utilities using modern algorithms.
use aes_gcm::{aead::Aead as AesAead, Aes256Gcm};
/// Supports AES-256 (GCM, CBC, CTR modes) and ChaCha20-Poly1305.
use chacha20poly1305::{
    aead::{KeyInit, Payload},
    ChaCha20Poly1305, Key,
};
use thiserror::Error;

use crate::security::crypto::random;

/// Symmetric encryption error type
#[derive(Debug, Error)]
pub enum SymmetricError {
    #[error("Encryption error: {0}")]
    EncryptionError(String),

    #[error("Decryption error: {0}")]
    DecryptionError(String),

    #[error("Invalid key error: {0}")]
    InvalidKeyError(String),

    #[error("Invalid data error: {0}")]
    InvalidDataError(String),

    #[error("Invalid nonce error: {0}")]
    InvalidNonceError(String),

    #[error("Other error: {0}")]
    OtherError(String),
}

/// Symmetric encryption algorithm type
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SymmetricAlgorithm {
    /// AES-256 in GCM mode
    Aes256Gcm,
    /// AES-256 in CBC mode
    Aes256Cbc,
    /// AES-256 in CTR mode
    Aes256Ctr,
    /// ChaCha20-Poly1305
    ChaCha20Poly1305,
}

/// Symmetric encryption/decryption handler
#[derive(Debug)]
pub struct SymmetricCrypto {
    /// Algorithm to use
    algorithm: SymmetricAlgorithm,
}

impl SymmetricCrypto {
    /// Create a new symmetric crypto handler
    pub fn new(algorithm: SymmetricAlgorithm) -> Self {
        Self { algorithm }
    }

    /// Generate a random key suitable for the selected algorithm
    pub fn generate_key(&self) -> Vec<u8> {
        match self.algorithm {
            SymmetricAlgorithm::Aes256Gcm
            | SymmetricAlgorithm::Aes256Cbc
            | SymmetricAlgorithm::Aes256Ctr => {
                // For AES-256, we need a 32-byte key
                random::random_bytes(32)
            }
            SymmetricAlgorithm::ChaCha20Poly1305 => {
                // For ChaCha20-Poly1305, we need a 32-byte key
                random::random_bytes(32)
            }
        }
    }

    /// Generate a random nonce/IV suitable for the selected algorithm
    pub fn generate_nonce(&self) -> Vec<u8> {
        match self.algorithm {
            SymmetricAlgorithm::Aes256Gcm => {
                // For AES-GCM, standard nonce size is 12 bytes
                random::random_bytes(12)
            }
            SymmetricAlgorithm::Aes256Cbc => {
                // For AES-CBC, IV size is 16 bytes (block size)
                random::random_bytes(16)
            }
            SymmetricAlgorithm::Aes256Ctr => {
                // For AES-CTR, nonce size is 16 bytes (block size)
                random::random_bytes(16)
            }
            SymmetricAlgorithm::ChaCha20Poly1305 => {
                // For ChaCha20-Poly1305, nonce size is 12 bytes
                random::random_bytes(12)
            }
        }
    }

    /// Encrypt data using the selected algorithm
    pub fn encrypt(
        &self,
        key: &[u8],
        nonce: &[u8],
        plaintext: &[u8],
        aad: Option<&[u8]>,
    ) -> Result<Vec<u8>, SymmetricError> {
        match self.algorithm {
            SymmetricAlgorithm::Aes256Gcm => self.encrypt_aes_gcm(key, nonce, plaintext, aad),
            SymmetricAlgorithm::ChaCha20Poly1305 => {
                self.encrypt_chacha20_poly1305(key, nonce, plaintext, aad)
            }
            _ => Err(SymmetricError::EncryptionError(format!(
                "Algorithm {:?} not yet implemented",
                self.algorithm
            ))),
        }
    }

    /// Decrypt data using the selected algorithm
    pub fn decrypt(
        &self,
        key: &[u8],
        nonce: &[u8],
        ciphertext: &[u8],
        aad: Option<&[u8]>,
    ) -> Result<Vec<u8>, SymmetricError> {
        match self.algorithm {
            SymmetricAlgorithm::Aes256Gcm => self.decrypt_aes_gcm(key, nonce, ciphertext, aad),
            SymmetricAlgorithm::ChaCha20Poly1305 => {
                self.decrypt_chacha20_poly1305(key, nonce, ciphertext, aad)
            }
            _ => Err(SymmetricError::DecryptionError(format!(
                "Algorithm {:?} not yet implemented",
                self.algorithm
            ))),
        }
    }

    /// Encrypt data using AES-GCM
    fn encrypt_aes_gcm(
        &self,
        key: &[u8],
        nonce: &[u8],
        plaintext: &[u8],
        aad: Option<&[u8]>,
    ) -> Result<Vec<u8>, SymmetricError> {
        // Validate key and nonce
        if key.len() != 32 {
            return Err(SymmetricError::InvalidKeyError(format!(
                "AES-256-GCM requires a 32-byte key, got {}",
                key.len()
            )));
        }

        if nonce.len() != 12 {
            return Err(SymmetricError::InvalidNonceError(format!(
                "AES-256-GCM requires a 12-byte nonce, got {}",
                nonce.len()
            )));
        }

        // Create cipher
        let cipher = Aes256Gcm::new_from_slice(key)
            .map_err(|e| SymmetricError::EncryptionError(e.to_string()))?;

        // Create nonce
        let nonce = aes_gcm::Nonce::from_slice(nonce);

        // Encrypt
        let payload = if let Some(aad_data) = aad {
            aes_gcm::aead::Payload {
                msg: plaintext,
                aad: aad_data,
            }
        } else {
            aes_gcm::aead::Payload {
                msg: plaintext,
                aad: &[],
            }
        };

        cipher
            .encrypt(nonce, payload)
            .map_err(|e| SymmetricError::EncryptionError(e.to_string()))
    }

    /// Decrypt data using AES-GCM
    fn decrypt_aes_gcm(
        &self,
        key: &[u8],
        nonce: &[u8],
        ciphertext: &[u8],
        aad: Option<&[u8]>,
    ) -> Result<Vec<u8>, SymmetricError> {
        // Validate key and nonce
        if key.len() != 32 {
            return Err(SymmetricError::InvalidKeyError(format!(
                "AES-256-GCM requires a 32-byte key, got {}",
                key.len()
            )));
        }

        if nonce.len() != 12 {
            return Err(SymmetricError::InvalidNonceError(format!(
                "AES-256-GCM requires a 12-byte nonce, got {}",
                nonce.len()
            )));
        }

        // Create cipher
        let cipher = Aes256Gcm::new_from_slice(key)
            .map_err(|e| SymmetricError::DecryptionError(e.to_string()))?;

        // Create nonce
        let nonce = aes_gcm::Nonce::from_slice(nonce);

        // Decrypt
        let payload = if let Some(aad_data) = aad {
            aes_gcm::aead::Payload {
                msg: ciphertext,
                aad: aad_data,
            }
        } else {
            aes_gcm::aead::Payload {
                msg: ciphertext,
                aad: &[],
            }
        };

        cipher
            .decrypt(nonce, payload)
            .map_err(|e| SymmetricError::DecryptionError(e.to_string()))
    }

    /// Encrypt data using ChaCha20-Poly1305
    fn encrypt_chacha20_poly1305(
        &self,
        key: &[u8],
        nonce: &[u8],
        plaintext: &[u8],
        aad: Option<&[u8]>,
    ) -> Result<Vec<u8>, SymmetricError> {
        // Validate key and nonce
        if key.len() != 32 {
            return Err(SymmetricError::InvalidKeyError(format!(
                "ChaCha20-Poly1305 requires a 32-byte key, got {}",
                key.len()
            )));
        }

        if nonce.len() != 12 {
            return Err(SymmetricError::InvalidNonceError(format!(
                "ChaCha20-Poly1305 requires a 12-byte nonce, got {}",
                nonce.len()
            )));
        }

        // Create cipher
        let key = Key::from_slice(key);
        let cipher = ChaCha20Poly1305::new(key);

        // Create nonce
        let nonce = chacha20poly1305::Nonce::from_slice(nonce);

        // Encrypt with payload
        let payload = if let Some(aad_data) = aad {
            Payload {
                msg: plaintext,
                aad: aad_data,
            }
        } else {
            Payload {
                msg: plaintext,
                aad: &[],
            }
        };

        cipher
            .encrypt(nonce, payload)
            .map_err(|e| SymmetricError::EncryptionError(e.to_string()))
    }

    /// Decrypt data using ChaCha20-Poly1305
    fn decrypt_chacha20_poly1305(
        &self,
        key: &[u8],
        nonce: &[u8],
        ciphertext: &[u8],
        aad: Option<&[u8]>,
    ) -> Result<Vec<u8>, SymmetricError> {
        // Validate key and nonce
        if key.len() != 32 {
            return Err(SymmetricError::InvalidKeyError(format!(
                "ChaCha20-Poly1305 requires a 32-byte key, got {}",
                key.len()
            )));
        }

        if nonce.len() != 12 {
            return Err(SymmetricError::InvalidNonceError(format!(
                "ChaCha20-Poly1305 requires a 12-byte nonce, got {}",
                nonce.len()
            )));
        }

        // Create cipher
        let key = Key::from_slice(key);
        let cipher = ChaCha20Poly1305::new(key);

        // Create nonce
        let nonce = chacha20poly1305::Nonce::from_slice(nonce);

        // Decrypt with payload
        let payload = if let Some(aad_data) = aad {
            Payload {
                msg: ciphertext,
                aad: aad_data,
            }
        } else {
            Payload {
                msg: ciphertext,
                aad: &[],
            }
        };

        cipher
            .decrypt(nonce, payload)
            .map_err(|e| SymmetricError::DecryptionError(e.to_string()))
    }
}

/// Helper function to create an AES-256-GCM cipher
pub fn create_aes_256_gcm() -> SymmetricCrypto {
    SymmetricCrypto::new(SymmetricAlgorithm::Aes256Gcm)
}

/// Helper function to create a ChaCha20-Poly1305 cipher
pub fn create_chacha20_poly1305() -> SymmetricCrypto {
    SymmetricCrypto::new(SymmetricAlgorithm::ChaCha20Poly1305)
}

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

    #[test]
    fn test_aes_gcm() -> Result<(), Box<dyn std::error::Error>> {
        let crypto = SymmetricCrypto::new(SymmetricAlgorithm::Aes256Gcm);

        // Generate key and nonce
        let key = crypto.generate_key();
        let nonce = crypto.generate_nonce();

        // Test data
        let plaintext = b"This is a test message";
        let aad = b"Additional authenticated data";

        // Encrypt
        let ciphertext = crypto.encrypt(&key, &nonce, plaintext, Some(aad))?;

        // Verify not the same as plaintext
        assert_ne!(&ciphertext, plaintext);

        // Decrypt
        let decrypted = crypto.decrypt(&key, &nonce, &ciphertext, Some(aad))?;

        // Verify decrypted matches original
        assert_eq!(&decrypted, plaintext);

        // Verify decryption fails with wrong AAD
        let wrong_aad = b"Wrong additional data";
        let result = crypto.decrypt(&key, &nonce, &ciphertext, Some(wrong_aad));
        assert!(result.is_err());

        Ok(())
    }

    #[test]
    fn test_chacha20_poly1305() -> Result<(), Box<dyn std::error::Error>> {
        let crypto = SymmetricCrypto::new(SymmetricAlgorithm::ChaCha20Poly1305);

        // Generate key and nonce
        let key = crypto.generate_key();
        let nonce = crypto.generate_nonce();

        // Test data
        let plaintext = b"This is a test message for ChaCha20-Poly1305";

        // Encrypt
        let ciphertext = crypto.encrypt(&key, &nonce, plaintext, None)?;

        // Verify not the same as plaintext
        assert_ne!(&ciphertext, plaintext);

        // Decrypt
        let decrypted = crypto.decrypt(&key, &nonce, &ciphertext, None)?;

        // Verify decrypted matches original
        assert_eq!(&decrypted, plaintext);

        Ok(())
    }

    #[test]
    fn test_helper_functions() {
        let aes_crypto = create_aes_256_gcm();
        let chacha_crypto = create_chacha20_poly1305();

        assert_eq!(aes_crypto.algorithm, SymmetricAlgorithm::Aes256Gcm);
        assert_eq!(
            chacha_crypto.algorithm,
            SymmetricAlgorithm::ChaCha20Poly1305
        );
    }
}