secure-types 0.5.0

Secure data types that protect sensitive data in memory via locking and zeroization.
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
//! `SecureArray`, exercised through the public API.

#![cfg(feature = "use_os")]

#[cfg(feature = "serde")]
mod common;

use std::fmt::Debug;
use std::process::{Command, Stdio};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};

use secure_types::{Error, SecureArray, SecureVec, Zeroize};

#[test]
fn test_creation() {
   let exposed_mut = &mut [1, 2, 3];
   let array: SecureArray<u8, 3> = SecureArray::from_slice_mut(exposed_mut).unwrap();
   assert_eq!(array.len(), 3);

   array.unlock(|slice| {
      assert_eq!(slice, &[1, 2, 3]);
   });

   assert_eq!(exposed_mut, &[0u8; 3]);

   let exposed = &[1, 2, 3];
   let array: SecureArray<u8, 3> = SecureArray::from_slice(exposed).unwrap();
   assert_eq!(array.len(), 3);

   array.unlock(|slice| {
      assert_eq!(slice, &[1, 2, 3]);
   });

   assert_eq!(exposed, &[1, 2, 3]);
}

#[test]
fn test_from_secure_vec() {
   let vec: SecureVec<u8> = SecureVec::from_slice(&[1, 2, 3]).unwrap();
   let array: SecureArray<u8, 3> = vec.try_into().unwrap();
   assert_eq!(array.len(), 3);
   array.unlock(|slice| {
      assert_eq!(slice, &[1, 2, 3]);
   });
}

#[test]
fn test_from_vec() {
   let vec = vec![1u8, 2, 3];
   let array: SecureArray<u8, 3> = SecureArray::try_from(vec).unwrap();
   assert_eq!(array.len(), 3);
   array.unlock(|slice| {
      assert_eq!(slice, &[1, 2, 3]);
   });
}

#[test]
fn test_from_secure_vec_generic() {
   let vec: SecureVec<u64> = SecureVec::from_slice(&[100u64, 200]).unwrap();
   let array: SecureArray<u64, 2> = vec.try_into().unwrap();
   array.unlock(|slice| {
      assert_eq!(slice, &[100u64, 200]);
   });
}

#[test]
fn test_try_from_length_mismatch() {
   let vec: SecureVec<u8> = SecureVec::from_slice(&[1, 2, 3]).unwrap();
   let result: Result<SecureArray<u8, 4>, _> = SecureArray::try_from(vec);
   assert!(matches!(result, Err(Error::LengthMismatch)));

   let result: Result<SecureArray<u8, 2>, _> = SecureArray::try_from(vec![1u8, 2, 3]);
   assert!(matches!(result, Err(Error::LengthMismatch)));
}

/// Regression test: dropping an array that was never initialized must not
/// interpret the allocator's poison bytes as a `T`.
#[test]
fn test_drop_without_initialization_is_sound() {
   let arg = "CRASH_TEST_ARRAY_EMPTY_DROP";

   if std::env::args().any(|a| a == arg) {
      // `String` owns a pointer, so zeroizing an uninitialized slot as a
      // `String` would dereference the allocator's poison bytes.
      let array: SecureArray<String, 4> = SecureArray::empty().unwrap();
      drop(array);
      std::process::exit(0);
   }

   let child = Command::new(std::env::current_exe().unwrap())
      .arg("test_drop_without_initialization_is_sound")
      .arg(arg)
      .arg("--nocapture")
      .stdout(Stdio::piped())
      .stderr(Stdio::piped())
      .spawn()
      .expect("Failed to spawn child process");

   let output = child.wait_with_output().expect("Failed to wait on child");

   assert!(
      output.status.success(),
      "Dropping a never-initialized SecureArray must be sound, but the child terminated with {:?}",
      output.status
   );
}

/// Regression test: a panic while filling the array must not crash. `drop` must
/// never interpret the unwritten slots as a `T`.
///
/// Note what is *not* asserted here: because `initialized` is committed only
/// after the whole loop, the elements cloned before the panic are leaked rather
/// than dropped or zeroized (the same trade-off `SecureVec` documents). This
/// pins the soundness (no SIGSEGV/SIGABRT), not the wipe.
#[test]
fn test_panic_during_partial_init_is_sound() {
   let arg = "CRASH_TEST_ARRAY_PANIC_INIT";

   if std::env::args().any(|a| a == arg) {
      let panicked = std::panic::catch_unwind(|| {
         let content: [PanicOnClone; 3] = [
            PanicOnClone::new("a"),
            PanicOnClone::new("b"),
            PanicOnClone::new("c"),
         ];
         let _array = SecureArray::<PanicOnClone, 3>::from_slice(&content).unwrap();
      })
      .is_err();

      std::process::exit(if panicked { 0 } else { 1 });
   }

   let child = Command::new(std::env::current_exe().unwrap())
      .arg("test_panic_during_partial_init_is_sound")
      .arg(arg)
      .arg("--nocapture")
      .stdout(Stdio::piped())
      .stderr(Stdio::piped())
      .spawn()
      .expect("Failed to spawn child process");

   let output = child.wait_with_output().expect("Failed to wait on child");

   assert!(
      output.status.success(),
      "A panic during partial initialization must be sound, but the child terminated with {:?}",
      output.status
   );
}

#[test]
fn test_erase() {
   let exposed: &mut [u8; 3] = &mut [1, 2, 3];
   let mut array: SecureArray<u8, 3> = SecureArray::from_slice_mut(exposed).unwrap();
   array.erase();
   array.unlock(|slice| {
      assert_eq!(slice, &[0u8; 3]);
   });
}

/// A zero-length array is rejected with `LengthCannotBeZero` (a `SecureArray`
/// cannot hold a zero-sized allocation), asserted on the variant rather than on
/// an incidental `unwrap` panic.
#[test]
fn test_length_cannot_be_zero() {
   let secure_vec = SecureVec::new().unwrap();
   let result: Result<SecureArray<u8, 0>, _> = SecureArray::try_from(secure_vec);

   assert!(matches!(result, Err(Error::LengthCannotBeZero)));
}

#[test]
fn test_clone() {
   let mut array1: SecureArray<u8, 3> = SecureArray::empty().unwrap();
   array1.unlock_mut(|slice| {
      slice[0] = 1;
      slice[1] = 2;
      slice[2] = 3;
   });

   let array2 = array1.clone();

   array2.unlock(|slice| {
      assert_eq!(slice, &[1, 2, 3]);
   });

   array1.unlock(|slice| {
      assert_eq!(slice, &[1, 2, 3]);
   });
}

#[test]
fn test_thread_safety() {
   let exposed: &mut [u8; 3] = &mut [1, 2, 3];
   let array: SecureArray<u8, 3> = SecureArray::from_slice_mut(exposed).unwrap();
   let arc_array = Arc::new(Mutex::new(array));
   let mut handles = Vec::new();

   for _ in 0..5u8 {
      let array_clone = Arc::clone(&arc_array);
      let handle = std::thread::spawn(move || {
         let mut guard = array_clone.lock().unwrap();
         guard.unlock_mut(|slice| {
            slice[0] += 1;
         });
      });
      handles.push(handle);
   }

   for handle in handles {
      handle.join().unwrap();
   }

   let final_array = arc_array.lock().unwrap();
   final_array.unlock(|slice| {
      assert_eq!(slice[0], 6);
      assert_eq!(slice[1], 2);
      assert_eq!(slice[2], 3);
   });
}

#[test]
fn test_unlock_mut() {
   let exposed: &mut [u8; 3] = &mut [1, 2, 3];
   let mut array: SecureArray<u8, 3> = SecureArray::from_slice_mut(exposed).unwrap();

   array.unlock_mut(|slice| {
      slice[1] = 100;
   });

   array.unlock(|slice| {
      assert_eq!(slice, &[1, 100, 3]);
   });
}

#[cfg(feature = "serde")]
#[test]
fn test_serde() {
   let exposed: &mut [u8; 3] = &mut [1, 2, 3];
   let array: SecureArray<u8, 3> = SecureArray::from_slice_mut(exposed).unwrap();
   let json_string = serde_json::to_string(&array).expect("Serialization failed");
   let json_bytes = serde_json::to_vec(&array).expect("Serialization failed");

   let deserialized_string: SecureArray<u8, 3> =
      serde_json::from_str(&json_string).expect("Deserialization failed");

   let deserialized_bytes: SecureArray<u8, 3> =
      serde_json::from_slice(&json_bytes).expect("Deserialization failed");

   deserialized_string.unlock(|slice| {
      assert_eq!(slice, &[1, 2, 3]);
   });

   deserialized_bytes.unlock(|slice| {
      assert_eq!(slice, &[1, 2, 3]);
   });
}

#[cfg(feature = "serde")]
#[test]
fn test_deserialize_from_owned_byte_buf() {
   use common::OwnedBytes;
   use serde::Deserialize;

   // `visit_byte_buf`: copied into locked memory, then the owned buffer is wiped.
   let array = SecureArray::<u8, 3>::deserialize(OwnedBytes(vec![1, 2, 3])).unwrap();

   array.unlock(|slice| assert_eq!(slice, &[1, 2, 3]));

   // Wrong length still has to return an error. The owned buffer is wiped on
   // this path too; that wipe is not observable without reading freed memory.
   assert!(SecureArray::<u8, 3>::deserialize(OwnedBytes(vec![1, 2])).is_err());
}

#[cfg(feature = "serde")]
#[test]
fn test_deserialize_from_json_string_as_bytes() {
   // `deserialize_bytes` accepts a JSON string, which reaches `visit_bytes`.
   let array: SecureArray<u8, 3> = serde_json::from_str(r#""abc""#).unwrap();

   array.unlock(|slice| assert_eq!(slice, b"abc"));
}

#[cfg(feature = "serde")]
#[test]
fn test_deserialize_rejects_wrong_length() {
   let from_bytes: Result<SecureArray<u8, 3>, _> = serde_json::from_str(r#""abcd""#);
   assert!(from_bytes.is_err());

   let from_short_seq: Result<SecureArray<u8, 3>, _> = serde_json::from_str("[1,2]");
   assert!(from_short_seq.is_err());
}

#[cfg(feature = "serde")]
#[test]
fn test_deserialize_rejects_overlong_seq_early() {
   // Rejected as soon as it exceeds `LENGTH`, so the locked buffer never grows.
   let result: Result<SecureArray<u8, 2>, _> = serde_json::from_str("[1,2,3]");

   assert!(result.is_err());
}

// === Test helpers for variety of types (bigger than u8, complex) ===
#[derive(Clone, Debug, PartialEq)]
struct SmallStruct {
   a: u8,
   b: u16,
}
impl Zeroize for SmallStruct {
   fn zeroize(&mut self) {
      self.a.zeroize();
      self.b.zeroize();
   }
}

#[derive(Clone, Debug, PartialEq)]
struct LargeStruct {
   data: [u64; 4],
   flag: bool,
}
impl Zeroize for LargeStruct {
   fn zeroize(&mut self) {
      self.data.zeroize();
      self.flag.zeroize();
   }
}

#[derive(Clone, Debug, PartialEq)]
#[repr(align(64))]
struct AlignedStruct {
   value: u64,
}
impl Zeroize for AlignedStruct {
   fn zeroize(&mut self) {
      self.value.zeroize();
   }
}

#[derive(Clone, Debug, PartialEq)]
struct Person {
   name: String,
   age: u32,
   notes: String,
}
impl Person {
   fn new(name: impl Into<String>, age: u32, notes: impl Into<String>) -> Self {
      Self {
         name: name.into(),
         age,
         notes: notes.into(),
      }
   }
}
impl Zeroize for Person {
   fn zeroize(&mut self) {
      self.name.zeroize();
      self.age.zeroize();
      self.notes.zeroize();
   }
}
fn create_test_person(id: usize) -> Person {
   Person::new(
      format!("Person{}", id),
      (id % 100) as u32,
      format!("Notes #{}", id),
   )
}

/// Counts every `clone` call, so `PanicOnClone` can blow up mid-way through
/// an initialization loop.
static CLONE_BOMB: AtomicUsize = AtomicUsize::new(0);

/// Owns a `String` like `Person` does, but panics on its second clone.
#[derive(Debug)]
struct PanicOnClone {
   data: String,
}

impl PanicOnClone {
   fn new(data: impl Into<String>) -> Self {
      Self { data: data.into() }
   }
}

impl Clone for PanicOnClone {
   fn clone(&self) -> Self {
      if CLONE_BOMB.fetch_add(1, Ordering::SeqCst) == 1 {
         panic!("clone bomb");
      }

      Self::new(self.data.clone())
   }
}

impl Zeroize for PanicOnClone {
   fn zeroize(&mut self) {
      self.data.zeroize();
   }
}

fn test_array_generic_basics<T: Zeroize + Clone + PartialEq + Debug, const N: usize>(
   initial: &[T; N],
) {
   let secure: SecureArray<T, N> = SecureArray::from_slice(initial).unwrap();
   assert_eq!(secure.len(), N);
   secure.unlock(|slice| {
      assert_eq!(slice, initial);
   });
   let cloned = secure.clone();
   cloned.unlock(|slice| {
      assert_eq!(slice, initial);
   });
   let mut er = SecureArray::from_slice(initial).unwrap();
   er.erase();
   er.unlock(|slice| {
      assert_eq!(slice.len(), N);
      let mut expected = initial.clone();
      for item in expected.iter_mut() {
         item.zeroize();
      }
      assert_eq!(slice, expected.as_slice());
   });
}

#[test]
fn test_array_u8_variety() {
   let data: [u8; 3] = [1, 2, 3];
   test_array_generic_basics(&data);
}

#[test]
fn test_array_u64() {
   let data: [u64; 2] = [100u64, 200];
   test_array_generic_basics(&data);
}

#[test]
fn test_array_byte_array() {
   let data: [[u8; 16]; 2] = [[1u8; 16], [2u8; 16]];
   test_array_generic_basics(&data);
}

#[test]
fn test_array_small_struct() {
   let data: [SmallStruct; 2] = [SmallStruct { a: 1, b: 2 }, SmallStruct { a: 3, b: 4 }];
   test_array_generic_basics(&data);
}

#[test]
fn test_array_large_struct() {
   let data = [
      LargeStruct {
         data: [1, 2, 3, 4],
         flag: true,
      },
      LargeStruct {
         data: [5, 6, 7, 8],
         flag: false,
      },
   ];
   test_array_generic_basics(&data);
}

#[test]
fn test_array_person() {
   let data = [create_test_person(42), create_test_person(43)];
   test_array_generic_basics(&data);
}

#[test]
fn test_array_aligned() {
   let data = [
      AlignedStruct { value: 0xDEAD_BEEF },
      AlignedStruct { value: 0xCAFE_BABE },
   ];
   test_array_generic_basics(&data);
}