arcanum-core 0.1.2

Core traits, types, and primitives for the Arcanum cryptographic engine
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
//! Secure buffer types with automatic zeroization.
//!
//! These types provide secure memory handling for sensitive data,
//! ensuring that secrets are properly zeroized when no longer needed.

use crate::error::{Error, Result};
use crate::random::OsRng;
use rand::RngCore;
use std::ops::{Deref, DerefMut};
use zeroize::{Zeroize, ZeroizeOnDrop};

// ═══════════════════════════════════════════════════════════════════════════════
// SECRET BUFFER (Fixed Size)
// ═══════════════════════════════════════════════════════════════════════════════

/// A fixed-size buffer that is zeroized on drop.
///
/// Use this for secrets with known sizes at compile time.
#[derive(Clone, ZeroizeOnDrop)]
pub struct SecretBuffer<const N: usize> {
    data: [u8; N],
}

impl<const N: usize> SecretBuffer<N> {
    /// Create a new zeroed buffer.
    pub fn new() -> Self {
        Self { data: [0u8; N] }
    }

    /// Create a buffer filled with cryptographically secure random bytes.
    ///
    /// This is the recommended way to generate secret keys.
    pub fn random() -> Self {
        let mut data = [0u8; N];
        OsRng.fill_bytes(&mut data);
        Self { data }
    }

    /// Create from an array.
    pub fn from_array(data: [u8; N]) -> Self {
        Self { data }
    }

    /// Create from a slice, returning error if length doesn't match.
    pub fn from_slice(slice: &[u8]) -> Result<Self> {
        if slice.len() != N {
            return Err(Error::InvalidParameter(format!(
                "expected {} bytes, got {}",
                N,
                slice.len()
            )));
        }
        let mut data = [0u8; N];
        data.copy_from_slice(slice);
        Ok(Self { data })
    }

    /// Get the buffer size.
    pub const fn len() -> usize {
        N
    }

    /// Access the underlying array.
    pub fn as_array(&self) -> &[u8; N] {
        &self.data
    }

    /// Access the underlying array mutably.
    pub fn as_array_mut(&mut self) -> &mut [u8; N] {
        &mut self.data
    }

    /// Fill with a repeated byte value.
    pub fn fill(&mut self, value: u8) {
        self.data.fill(value);
    }

    /// Copy from another buffer of the same size.
    pub fn copy_from(&mut self, other: &Self) {
        self.data.copy_from_slice(&other.data);
    }
}

impl<const N: usize> Default for SecretBuffer<N> {
    fn default() -> Self {
        Self::new()
    }
}

impl<const N: usize> Deref for SecretBuffer<N> {
    type Target = [u8; N];

    fn deref(&self) -> &Self::Target {
        &self.data
    }
}

impl<const N: usize> DerefMut for SecretBuffer<N> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.data
    }
}

impl<const N: usize> AsRef<[u8]> for SecretBuffer<N> {
    fn as_ref(&self) -> &[u8] {
        &self.data
    }
}

impl<const N: usize> AsMut<[u8]> for SecretBuffer<N> {
    fn as_mut(&mut self) -> &mut [u8] {
        &mut self.data
    }
}

impl<const N: usize> std::fmt::Debug for SecretBuffer<N> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "SecretBuffer<{}>[REDACTED]", N)
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// SECURE VEC (Dynamic Size)
// ═══════════════════════════════════════════════════════════════════════════════

/// A dynamically-sized buffer that is zeroized on drop.
///
/// Use this for secrets with sizes known only at runtime.
#[derive(Clone)]
pub struct SecureVec {
    data: Vec<u8>,
}

impl SecureVec {
    /// Create a new empty buffer.
    pub fn new() -> Self {
        Self { data: Vec::new() }
    }

    /// Create a buffer with specified capacity.
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            data: Vec::with_capacity(capacity),
        }
    }

    /// Create a buffer of given size filled with zeros.
    pub fn zeroed(len: usize) -> Self {
        Self {
            data: vec![0u8; len],
        }
    }

    /// Create a buffer filled with cryptographically secure random bytes.
    ///
    /// This is the recommended way to generate secret keys of arbitrary length.
    pub fn random(len: usize) -> Self {
        let mut data = vec![0u8; len];
        OsRng.fill_bytes(&mut data);
        Self { data }
    }

    /// Create from a vector, taking ownership.
    pub fn from_vec(data: Vec<u8>) -> Self {
        Self { data }
    }

    /// Create from a slice, copying the data.
    pub fn from_slice(slice: &[u8]) -> Self {
        Self {
            data: slice.to_vec(),
        }
    }

    /// Get the buffer length.
    pub fn len(&self) -> usize {
        self.data.len()
    }

    /// Check if the buffer is empty.
    pub fn is_empty(&self) -> bool {
        self.data.is_empty()
    }

    /// Get the buffer capacity.
    pub fn capacity(&self) -> usize {
        self.data.capacity()
    }

    /// Reserve additional capacity.
    pub fn reserve(&mut self, additional: usize) {
        self.data.reserve(additional);
    }

    /// Push a byte.
    pub fn push(&mut self, byte: u8) {
        self.data.push(byte);
    }

    /// Extend from a slice.
    pub fn extend_from_slice(&mut self, slice: &[u8]) {
        self.data.extend_from_slice(slice);
    }

    /// Clear the buffer (zeroizes first).
    pub fn clear(&mut self) {
        self.data.zeroize();
        self.data.clear();
    }

    /// Resize the buffer.
    pub fn resize(&mut self, new_len: usize, value: u8) {
        if new_len < self.data.len() {
            // Zeroize the portion being removed
            self.data[new_len..].zeroize();
        }
        self.data.resize(new_len, value);
    }

    /// Truncate the buffer.
    pub fn truncate(&mut self, len: usize) {
        if len < self.data.len() {
            self.data[len..].zeroize();
        }
        self.data.truncate(len);
    }

    /// Convert to a regular Vec, consuming self.
    ///
    /// **Warning**: The returned Vec will NOT be automatically zeroized.
    /// Use with caution.
    pub fn into_vec(mut self) -> Vec<u8> {
        std::mem::take(&mut self.data)
    }

    /// Split at a position, returning the second half.
    pub fn split_off(&mut self, at: usize) -> Self {
        Self {
            data: self.data.split_off(at),
        }
    }

    /// Expose the secret bytes (read-only).
    ///
    /// Use this when passing the secret to cryptographic functions.
    pub fn expose(&self) -> &[u8] {
        &self.data
    }

    /// Expose the secret bytes (mutable).
    ///
    /// Use with caution - modifying secret data directly.
    pub fn expose_mut(&mut self) -> &mut [u8] {
        &mut self.data
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// TYPE ALIASES
// ═══════════════════════════════════════════════════════════════════════════════

/// A securely-managed byte buffer for cryptographic secrets.
///
/// This is the recommended type for storing secret keys, as it:
/// - Automatically zeroizes memory on drop
/// - Provides explicit `expose()` methods to access the secret
/// - Prevents accidental logging via redacted Debug impl
///
/// # Example
///
/// ```ignore
/// use arcanum_core::buffer::SecretBytes;
///
/// // Generate a random 32-byte secret key
/// let key = SecretBytes::random(32);
///
/// // Access the key for encryption
/// cipher.encrypt(key.expose(), &nonce, &plaintext);
///
/// // Key is automatically zeroized when dropped
/// ```
pub type SecretBytes = SecureVec;

impl Default for SecureVec {
    fn default() -> Self {
        Self::new()
    }
}

impl Drop for SecureVec {
    fn drop(&mut self) {
        self.data.zeroize();
    }
}

impl Deref for SecureVec {
    type Target = [u8];

    fn deref(&self) -> &Self::Target {
        &self.data
    }
}

impl DerefMut for SecureVec {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.data
    }
}

impl AsRef<[u8]> for SecureVec {
    fn as_ref(&self) -> &[u8] {
        &self.data
    }
}

impl AsMut<[u8]> for SecureVec {
    fn as_mut(&mut self) -> &mut [u8] {
        &mut self.data
    }
}

impl std::fmt::Debug for SecureVec {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "SecureVec[{} bytes, REDACTED]", self.data.len())
    }
}

impl From<Vec<u8>> for SecureVec {
    fn from(data: Vec<u8>) -> Self {
        Self::from_vec(data)
    }
}

impl From<&[u8]> for SecureVec {
    fn from(slice: &[u8]) -> Self {
        Self::from_slice(slice)
    }
}

impl<const N: usize> From<[u8; N]> for SecureVec {
    fn from(array: [u8; N]) -> Self {
        Self::from_slice(&array)
    }
}

impl FromIterator<u8> for SecureVec {
    fn from_iter<T: IntoIterator<Item = u8>>(iter: T) -> Self {
        Self {
            data: iter.into_iter().collect(),
        }
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// GUARDED ALLOCATION
// ═══════════════════════════════════════════════════════════════════════════════

/// A buffer with guard pages for additional security.
///
/// This wraps memory with guard pages to detect buffer overflows/underflows.
/// Useful for extremely sensitive data.
#[cfg(feature = "std")]
pub struct GuardedBuffer {
    data: SecureVec,
    // In a full implementation, this would use mprotect/VirtualProtect
    // to set up actual guard pages around the allocation
}

#[cfg(feature = "std")]
impl GuardedBuffer {
    /// Create a new guarded buffer of the specified size.
    pub fn new(size: usize) -> Self {
        // In a production implementation, this would:
        // 1. Allocate size + 2*PAGE_SIZE bytes
        // 2. mprotect the first and last pages as PROT_NONE
        // 3. Return a pointer to the middle region
        Self {
            data: SecureVec::zeroed(size),
        }
    }

    /// Access the protected data.
    pub fn as_slice(&self) -> &[u8] {
        &self.data
    }

    /// Access the protected data mutably.
    pub fn as_mut_slice(&mut self) -> &mut [u8] {
        &mut self.data
    }
}

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

    #[test]
    fn test_secret_buffer() {
        let mut buf = SecretBuffer::<32>::new();
        buf.fill(0xAB);
        assert_eq!(buf.as_array(), &[0xAB; 32]);
    }

    #[test]
    fn test_secret_buffer_from_slice() {
        let data = [1u8; 16];
        let buf = SecretBuffer::<16>::from_slice(&data).unwrap();
        assert_eq!(buf.as_array(), &data);

        // Wrong size should fail
        assert!(SecretBuffer::<32>::from_slice(&data).is_err());
    }

    #[test]
    fn test_secure_vec() {
        let mut vec = SecureVec::new();
        vec.extend_from_slice(&[1, 2, 3, 4]);
        assert_eq!(vec.len(), 4);
        assert_eq!(&*vec, &[1, 2, 3, 4]);

        vec.clear();
        assert!(vec.is_empty());
    }

    #[test]
    fn test_secure_vec_truncate() {
        let mut vec = SecureVec::from_slice(&[1, 2, 3, 4, 5]);
        vec.truncate(3);
        assert_eq!(vec.len(), 3);
        assert_eq!(&*vec, &[1, 2, 3]);
    }

    #[test]
    fn test_secret_buffer_random() {
        let buf1 = SecretBuffer::<32>::random();
        let buf2 = SecretBuffer::<32>::random();
        // Random buffers should be different (with overwhelming probability)
        assert_ne!(buf1.as_array(), buf2.as_array());
    }

    #[test]
    fn test_secure_vec_random() {
        let vec1 = SecureVec::random(32);
        let vec2 = SecureVec::random(32);
        // Random vecs should be different
        assert_ne!(vec1.expose(), vec2.expose());
        assert_eq!(vec1.len(), 32);
    }

    #[test]
    fn test_secret_bytes_alias() {
        // SecretBytes is an alias for SecureVec
        let key: SecretBytes = SecretBytes::random(32);
        assert_eq!(key.len(), 32);
        assert_eq!(key.expose().len(), 32);
    }
}