-
Notifications
You must be signed in to change notification settings - Fork 142
/
secure_cell.rs
848 lines (672 loc) · 29.7 KB
/
secure_cell.rs
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
// Copyright 2018 (c) rust-themis developers
//
// 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
//
// http://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.
use themis::keys::SymmetricKey;
use themis::secure_cell::SecureCell;
mod context_imprint {
use super::*;
#[test]
fn initialization() {
assert!(SecureCell::with_key(SymmetricKey::new()).is_ok());
assert!(SecureCell::with_key(&[]).is_err());
}
#[test]
fn roundtrip() {
let cell = SecureCell::with_key(SymmetricKey::new())
.unwrap()
.context_imprint();
let message = b"Colorless green ideas sleep furiously".as_ref();
let context = b"...and a toilet seat cover!".as_ref();
let encrypted = cell.encrypt_with_context(&message, &context).unwrap();
let decrypted = cell.decrypt_with_context(&encrypted, &context).unwrap();
assert_eq!(decrypted, message);
}
#[test]
fn data_length_preservation() {
let cell = SecureCell::with_key(SymmetricKey::new())
.unwrap()
.context_imprint();
let message = b"Colorless green ideas sleep furiously".as_ref();
let context = b"...and a toilet seat cover!".as_ref();
let encrypted = cell.encrypt_with_context(&message, &context).unwrap();
assert_eq!(encrypted.len(), message.len());
}
#[test]
fn context_not_included() {
let cell = SecureCell::with_key(SymmetricKey::new())
.unwrap()
.context_imprint();
let message = b"Colorless green ideas sleep furiously".as_ref();
let context_short = b"Shteko budlanula bokra".as_ref();
let context_long =
b"Buffalo buffalo Buffalo buffalo buffalo buffalo Buffalo buffalo".as_ref();
let encrypted_short = cell.encrypt_with_context(&message, &context_short).unwrap();
let encrypted_long = cell.encrypt_with_context(&message, &context_long).unwrap();
// Context is not (directly) included into encrypted message.
assert_eq!(encrypted_short.len(), encrypted_long.len());
}
#[test]
fn key_must_match() {
let cell_a = SecureCell::with_key(SymmetricKey::new())
.unwrap()
.context_imprint();
let cell_b = SecureCell::with_key(SymmetricKey::new())
.unwrap()
.context_imprint();
let message = b"Colorless green ideas sleep furiously".as_ref();
let context = b"...and a toilet seat cover!".as_ref();
let encrypted = cell_a.encrypt_with_context(&message, &context).unwrap();
// Context Imprint mode does not validate message data so using an incorrect key
// will successfully return garbage output.
let decrypted_incorrect = cell_b.decrypt_with_context(&encrypted, &context).unwrap();
assert_ne!(decrypted_incorrect, message);
assert_ne!(decrypted_incorrect, encrypted);
// Only the correct key will work.
let decrypted_correct = cell_a.decrypt_with_context(&encrypted, &context).unwrap();
assert_eq!(decrypted_correct, message);
}
#[test]
fn context_must_match() {
let cell = SecureCell::with_key(SymmetricKey::new())
.unwrap()
.context_imprint();
let message = b"Colorless green ideas sleep furiously".as_ref();
let context_a = b"The jaws that bite, the claws that catch!".as_ref();
let context_b = b"One, two! One, two! And through and through".as_ref();
let encrypted = cell.encrypt_with_context(&message, &context_a).unwrap();
// Context Imprint mode does not validate message data so using an incorrect context
// will successfully return garbage output.
let decrypted_incorrect = cell.decrypt_with_context(&encrypted, &context_b).unwrap();
assert_ne!(decrypted_incorrect, message);
assert_ne!(decrypted_incorrect, encrypted);
// Only the correct context will work.
let decrypted_correct = cell.decrypt_with_context(&encrypted, &context_a).unwrap();
assert_eq!(decrypted_correct, message);
}
#[test]
fn not_detects_corrupted_data() {
let cell = SecureCell::with_key(SymmetricKey::new())
.unwrap()
.context_imprint();
let message = b"Colorless green ideas sleep furiously".as_ref();
let context = b"...and a toilet seat cover!".as_ref();
let encrypted = cell.encrypt_with_context(&message, &context).unwrap();
// Invert every odd byte, this will surely break the message.
let mut corrupted = encrypted;
for (i, b) in corrupted.iter_mut().enumerate() {
if i % 2 == 1 {
*b = !*b
}
}
// Decrypts successfully but the content is garbage.
let decrypted = cell.decrypt_with_context(&corrupted, &context).unwrap();
assert_ne!(decrypted, message);
}
#[test]
fn not_detects_truncated_data() {
let cell = SecureCell::with_key(SymmetricKey::new())
.unwrap()
.context_imprint();
let message = b"Colorless green ideas sleep furiously".as_ref();
let context = b"...and a toilet seat cover!".as_ref();
let encrypted = cell.encrypt_with_context(&message, &context).unwrap();
let truncated = &encrypted[..encrypted.len() - 1];
// Decrypts successfully but the content is garbage.
let decrypted = cell.decrypt_with_context(&truncated, &context).unwrap();
assert_ne!(decrypted, message);
}
#[test]
fn detects_extended_data() {
let cell = SecureCell::with_key(SymmetricKey::new())
.unwrap()
.context_imprint();
let message = b"Colorless green ideas sleep furiously".as_ref();
let context = b"...and a toilet seat cover!".as_ref();
let encrypted = cell.encrypt_with_context(&message, &context).unwrap();
let mut extended = encrypted;
extended.push(0);
// Decrypts successfully but the content is garbage.
let decrypted = cell.decrypt_with_context(&extended, &context).unwrap();
assert_ne!(decrypted, message);
}
#[test]
fn empty_input_not_allowed() {
let cell = SecureCell::with_key(SymmetricKey::new())
.unwrap()
.context_imprint();
let message = b"Colorless green ideas sleep furiously".as_ref();
let context = b"...and a toilet seat cover!".as_ref();
// With Context Imprint the context cannot be empty.
assert!(cell.encrypt_with_context(&message, &[]).is_err());
assert!(cell.encrypt_with_context(&[], &context).is_err());
assert!(cell.decrypt_with_context(&message, &[]).is_err());
assert!(cell.decrypt_with_context(&[], &context).is_err());
}
}
mod seal {
use super::*;
#[test]
fn initialization() {
assert!(SecureCell::with_key(SymmetricKey::new()).is_ok());
assert!(SecureCell::with_key(&[]).is_err());
}
#[test]
fn roundtrip() {
let cell = SecureCell::with_key(SymmetricKey::new()).unwrap().seal();
let message = b"Colorless green ideas sleep furiously".as_ref();
let context = b"...and a toilet seat cover!".as_ref();
let encrypted = cell.encrypt_with_context(&message, &context).unwrap();
let decrypted = cell.decrypt_with_context(&encrypted, &context).unwrap();
assert_eq!(decrypted, message);
}
#[test]
fn data_length_extension() {
let cell = SecureCell::with_key(SymmetricKey::new()).unwrap().seal();
let message = b"Colorless green ideas sleep furiously".as_ref();
let encrypted = cell.encrypt(&message).unwrap();
assert!(encrypted.len() > message.len());
}
#[test]
fn context_not_included() {
let cell = SecureCell::with_key(SymmetricKey::new()).unwrap().seal();
let message = b"Colorless green ideas sleep furiously".as_ref();
let context_short = b"Shteko budlanula bokra".as_ref();
let context_long =
b"Buffalo buffalo Buffalo buffalo buffalo buffalo Buffalo buffalo".as_ref();
let encrypted_short = cell.encrypt_with_context(&message, &context_short).unwrap();
let encrypted_long = cell.encrypt_with_context(&message, &context_long).unwrap();
// Context is not (directly) included into encrypted message.
assert_eq!(encrypted_short.len(), encrypted_long.len());
}
#[test]
fn empty_context_handling() {
let cell = SecureCell::with_key(SymmetricKey::new()).unwrap().seal();
let message = b"Colorless green ideas sleep furiously".as_ref();
// encrypt(...) is encrypt_with_context(..., &[])
let encrypted_1 = cell.encrypt(&message).unwrap();
let encrypted_2 = cell.encrypt_with_context(&message, &[]).unwrap();
assert_eq!(cell.decrypt(&encrypted_1), Ok(message.to_vec()));
assert_eq!(cell.decrypt(&encrypted_2), Ok(message.to_vec()));
assert_eq!(
cell.decrypt_with_context(&encrypted_1, &[]),
Ok(message.to_vec())
);
assert_eq!(
cell.decrypt_with_context(&encrypted_2, &[]),
Ok(message.to_vec())
);
}
#[test]
fn key_must_match() {
let cell_a = SecureCell::with_key(SymmetricKey::new()).unwrap().seal();
let cell_b = SecureCell::with_key(SymmetricKey::new()).unwrap().seal();
let message = b"Colorless green ideas sleep furiously".as_ref();
let encrypted = cell_a.encrypt(&message).unwrap();
// You cannot use a different key to decrypt data.
assert!(cell_b.decrypt(&encrypted).is_err());
// Only the correct key will work.
let decrypted = cell_a.decrypt(&encrypted).unwrap();
assert_eq!(decrypted, message);
}
#[test]
fn context_must_match() {
let cell = SecureCell::with_key(SymmetricKey::new()).unwrap().seal();
let message = b"Colorless green ideas sleep furiously".as_ref();
let context_a = b"The jaws that bite, the claws that catch!".as_ref();
let context_b = b"One, two! One, two! And through and through".as_ref();
let encrypted = cell.encrypt_with_context(&message, &context_a).unwrap();
// You cannot use a different context to decrypt data.
assert!(cell.decrypt_with_context(&encrypted, &context_b).is_err());
// Only the correct context will work.
let decrypted = cell.decrypt_with_context(&encrypted, &context_a).unwrap();
assert_eq!(decrypted, message);
}
#[test]
fn detects_corrupted_data() {
let cell = SecureCell::with_key(SymmetricKey::new()).unwrap().seal();
let message = b"Colorless green ideas sleep furiously".as_ref();
let encrypted = cell.encrypt(&message).unwrap();
// Invert every odd byte, this will surely break the message.
let mut corrupted = encrypted;
for (i, b) in corrupted.iter_mut().enumerate() {
if i % 2 == 1 {
*b = !*b
}
}
assert!(cell.decrypt(&corrupted).is_err());
}
#[test]
fn detects_truncated_data() {
let cell = SecureCell::with_key(SymmetricKey::new()).unwrap().seal();
let message = b"Colorless green ideas sleep furiously".as_ref();
let encrypted = cell.encrypt(&message).unwrap();
let truncated = &encrypted[..encrypted.len() - 1];
assert!(cell.decrypt(truncated).is_err());
}
#[test]
fn detects_extended_data() {
let cell = SecureCell::with_key(SymmetricKey::new()).unwrap().seal();
let message = b"Colorless green ideas sleep furiously".as_ref();
let encrypted = cell.encrypt(&message).unwrap();
let mut extended = encrypted;
extended.push(0);
assert!(cell.decrypt(&extended).is_err());
}
#[test]
fn empty_messages_not_allowed() {
let cell = SecureCell::with_key(SymmetricKey::new()).unwrap().seal();
assert!(cell.encrypt(&[]).is_err());
assert!(cell.decrypt(&[]).is_err());
}
}
mod seal_with_passphrase {
use super::*;
#[test]
fn initialization() {
assert!(SecureCell::with_passphrase("non-empty passphrase").is_ok());
assert!(SecureCell::with_passphrase("").is_err());
}
#[test]
fn roundtrip() {
let cell = SecureCell::with_passphrase("sesquipedalian")
.unwrap()
.seal();
let message = b"Colorless green ideas sleep furiously".as_ref();
let context = b"...and a toilet seat cover!".as_ref();
let encrypted = cell.encrypt_with_context(&message, &context).unwrap();
let decrypted = cell.decrypt_with_context(&encrypted, &context).unwrap();
assert_eq!(decrypted, message);
}
#[test]
fn data_length_extension() {
let cell = SecureCell::with_passphrase("sesquipedalian")
.unwrap()
.seal();
let message = b"Colorless green ideas sleep furiously".as_ref();
let encrypted = cell.encrypt(&message).unwrap();
assert!(encrypted.len() > message.len());
}
#[test]
fn context_not_included() {
let cell = SecureCell::with_passphrase("sesquipedalian")
.unwrap()
.seal();
let message = b"Colorless green ideas sleep furiously".as_ref();
let context_short = b"Shteko budlanula bokra".as_ref();
let context_long =
b"Buffalo buffalo Buffalo buffalo buffalo buffalo Buffalo buffalo".as_ref();
let encrypted_short = cell.encrypt_with_context(&message, &context_short).unwrap();
let encrypted_long = cell.encrypt_with_context(&message, &context_long).unwrap();
// Context is not (directly) included into encrypted message.
assert_eq!(encrypted_short.len(), encrypted_long.len());
}
#[test]
fn empty_context_handling() {
let cell = SecureCell::with_passphrase("sesquipedalian")
.unwrap()
.seal();
let message = b"Colorless green ideas sleep furiously".as_ref();
// encrypt is encrypt_with_context(..., )
let encrypted_1 = cell.encrypt(&message).unwrap();
let encrypted_2 = cell.encrypt_with_context(&message, &[]).unwrap();
assert_eq!(cell.decrypt(&encrypted_1), Ok(message.to_vec()));
assert_eq!(cell.decrypt(&encrypted_2), Ok(message.to_vec()));
assert_eq!(
cell.decrypt_with_context(&encrypted_1, &[]),
Ok(message.to_vec())
);
assert_eq!(
cell.decrypt_with_context(&encrypted_2, &[]),
Ok(message.to_vec())
);
}
#[test]
fn passphrase_must_match() {
let cell_a = SecureCell::with_passphrase("sesquipedalian")
.unwrap()
.seal();
let cell_b = SecureCell::with_passphrase("loquaciousness")
.unwrap()
.seal();
let message = b"Colorless green ideas sleep furiously".as_ref();
let encrypted = cell_a.encrypt(&message).unwrap();
// You cannot use a different passphrase to decrypt data.
assert!(cell_b.decrypt(&encrypted).is_err());
// Only the correct passphrase will work.
let decrypted = cell_a.decrypt(&encrypted).unwrap();
assert_eq!(decrypted, message);
}
#[test]
fn context_must_match() {
let cell = SecureCell::with_passphrase("sesquipedalian")
.unwrap()
.seal();
let message = b"Colorless green ideas sleep furiously".as_ref();
let context_a = b"The jaws that bite, the claws that catch!".as_ref();
let context_b = b"One, two! One, two! And through and through".as_ref();
let encrypted = cell.encrypt_with_context(&message, &context_a).unwrap();
// You cannot use a different context to decrypt data.
assert!(cell.decrypt_with_context(&encrypted, &context_b).is_err());
// Only the correct context will work.
let decrypted = cell.decrypt_with_context(&encrypted, &context_a).unwrap();
assert_eq!(decrypted, message);
}
#[test]
fn detects_corrupted_data() {
let cell = SecureCell::with_passphrase("sesquipedalian")
.unwrap()
.seal();
let message = b"Colorless green ideas sleep furiously".as_ref();
let encrypted = cell.encrypt(&message).unwrap();
// Invert every odd byte, this will surely break the message.
let mut corrupted = encrypted;
for (i, b) in corrupted.iter_mut().enumerate() {
if i % 2 == 1 {
*b = !*b
}
}
assert!(cell.decrypt(&corrupted).is_err());
}
#[test]
fn detects_truncated_data() {
let cell = SecureCell::with_passphrase("sesquipedalian")
.unwrap()
.seal();
let message = b"Colorless green ideas sleep furiously".as_ref();
let encrypted = cell.encrypt(&message).unwrap();
let truncated = &encrypted[..encrypted.len() - 1];
assert!(cell.decrypt(truncated).is_err());
}
#[test]
fn detects_extended_data() {
let cell = SecureCell::with_passphrase("sesquipedalian")
.unwrap()
.seal();
let message = b"Colorless green ideas sleep furiously".as_ref();
let encrypted = cell.encrypt(&message).unwrap();
let mut extended = encrypted;
extended.push(0);
assert!(cell.decrypt(&extended).is_err());
}
#[test]
fn empty_messages_not_allowed() {
let cell = SecureCell::with_passphrase("sesquipedalian")
.unwrap()
.seal();
assert!(cell.encrypt(&[]).is_err());
assert!(cell.decrypt(&[]).is_err());
}
#[test]
fn passphrases_not_compatible_with_keys() {
let key = SymmetricKey::new();
let cell_mk = SecureCell::with_key(&key).unwrap().seal();
let cell_pw = SecureCell::with_passphrase(&key).unwrap().seal();
let message = b"Colorless green ideas sleep furiously".as_ref();
// Passphrases are not keys, keys are not passphrases.
let encrypted_mk = cell_mk.encrypt(&message).unwrap();
assert!(cell_pw.decrypt(&encrypted_mk).is_err());
let encrypted_pw = cell_pw.encrypt(&message).unwrap();
assert!(cell_mk.decrypt(&encrypted_pw).is_err());
}
#[test]
fn default_encoding_is_utf8() {
// It's a key. -- No. Much more better! It is a drawing of a key.
let cell_a = SecureCell::with_passphrase("🔑").unwrap().seal();
let cell_b = SecureCell::with_passphrase(b"\xF0\x9F\x94\x91")
.unwrap()
.seal();
let message = b"Colorless green ideas sleep furiously".as_ref();
let encrypted = cell_a.encrypt(&message).unwrap();
let decrypted = cell_b.decrypt(&encrypted).unwrap();
assert_eq!(decrypted, message);
}
#[test]
fn supports_other_encodings() {
// 🔑 in UTF-16BE
let cell = SecureCell::with_passphrase(b"\xD8\x3D\xDD\x11")
.unwrap()
.seal();
let message = b"Colorless green ideas sleep furiously".as_ref();
// Message encrypted by PyThemis
let encrypted = b"\x00\x01\x01\x41\x0C\x00\x00\x00\x10\x00\x00\x00\x25\x00\x00\x00\x16\x00\x00\x00\x78\x98\x93\x12\xC9\x60\x1E\x22\xD7\xCB\x47\x06\xC9\xEC\x46\xB5\xB5\x9A\xFC\xC8\x3F\x06\x8F\x5B\xBE\x9F\x66\xA6\x40\x0D\x03\x00\x10\x00\x1C\x6D\x16\xFF\x39\xB9\x13\xDF\xC4\x41\x56\x31\x17\xF3\xC4\x05\x28\x15\x13\xA6\x74\x29\x0B\xF0\x5A\xFB\xAC\xD9\x79\x35\x7E\xBA\xD0\x8F\x8C\xA6\x9E\x4E\x83\x2A\x12\xBA\xC7\x59\xAA\xF6\xDF\x62\x8E\xCE\x31\x7C\xCF".as_ref();
let decrypted = cell.decrypt(&encrypted).unwrap();
assert_eq!(decrypted, message);
}
}
mod token_protect {
use super::*;
#[test]
fn initialization() {
assert!(SecureCell::with_key(SymmetricKey::new()).is_ok());
assert!(SecureCell::with_key(&[]).is_err());
}
#[test]
fn roundtrip() {
let cell = SecureCell::with_key(SymmetricKey::new())
.unwrap()
.token_protect();
let message = b"Colorless green ideas sleep furiously".as_ref();
let context = b"...and a toilet seat cover!".as_ref();
let (encrypted, token) = cell.encrypt_with_context(&message, &context).unwrap();
let decrypted = cell
.decrypt_with_context(&encrypted, &token, &context)
.unwrap();
assert_eq!(decrypted, message);
}
#[test]
fn data_length_preservation() {
let cell = SecureCell::with_key(SymmetricKey::new())
.unwrap()
.token_protect();
let message = b"Colorless green ideas sleep furiously".as_ref();
let (encrypted, token) = cell.encrypt(&message).unwrap();
assert_eq!(encrypted.len(), message.len());
assert!(!token.is_empty());
}
#[test]
fn context_not_included() {
let cell = SecureCell::with_key(SymmetricKey::new())
.unwrap()
.token_protect();
let message = b"Colorless green ideas sleep furiously".as_ref();
let context_short = b"Shteko budlanula bokra".as_ref();
let context_long =
b"Buffalo buffalo Buffalo buffalo buffalo buffalo Buffalo buffalo".as_ref();
let (encrypted_short, token_short) =
cell.encrypt_with_context(&message, &context_short).unwrap();
let (encrypted_long, token_long) =
cell.encrypt_with_context(&message, &context_long).unwrap();
// Context is not (directly) included into encrypted message.
assert_eq!(encrypted_short.len(), encrypted_long.len());
assert_eq!(token_short.len(), token_long.len());
}
#[test]
fn empty_context_handling() {
let cell = SecureCell::with_key(SymmetricKey::new())
.unwrap()
.token_protect();
let message = b"Colorless green ideas sleep furiously".as_ref();
// encrypt(...) is encrypt_with_context(..., &[])
let (encrypted_1, token_1) = cell.encrypt(&message).unwrap();
let (encrypted_2, token_2) = cell.encrypt_with_context(&message, &[]).unwrap();
assert_eq!(cell.decrypt(&encrypted_1, &token_1), Ok(message.to_vec()));
assert_eq!(cell.decrypt(&encrypted_2, &token_2), Ok(message.to_vec()));
assert_eq!(
cell.decrypt_with_context(&encrypted_1, &token_1, &[]),
Ok(message.to_vec())
);
assert_eq!(
cell.decrypt_with_context(&encrypted_2, &token_2, &[]),
Ok(message.to_vec())
);
}
#[test]
fn key_must_match() {
let cell_a = SecureCell::with_key(SymmetricKey::new())
.unwrap()
.token_protect();
let cell_b = SecureCell::with_key(SymmetricKey::new())
.unwrap()
.token_protect();
let message = b"Colorless green ideas sleep furiously".as_ref();
let (encrypted, token) = cell_a.encrypt(&message).unwrap();
// You cannot use a different key to decrypt data.
assert!(cell_b.decrypt(&encrypted, &token).is_err());
// Only the correct key will work.
let decrypted = cell_a.decrypt(&encrypted, &token).unwrap();
assert_eq!(decrypted, message);
}
#[test]
fn token_must_match() {
let cell = SecureCell::with_key(SymmetricKey::new())
.unwrap()
.token_protect();
let message = b"Colorless green ideas sleep furiously".as_ref();
let (encrypted_1, token_1) = cell.encrypt(&message).unwrap();
let (encrypted_2, token_2) = cell.encrypt(&message).unwrap();
// You cannot use a different token to decrypt data, even the same original data.
assert!(cell.decrypt(&encrypted_1, &token_2).is_err());
assert!(cell.decrypt(&encrypted_2, &token_1).is_err());
// Only the matching token will work.
let decrypted_1 = cell.decrypt(&encrypted_1, &token_1).unwrap();
let decrypted_2 = cell.decrypt(&encrypted_2, &token_2).unwrap();
assert_eq!(decrypted_1, message);
assert_eq!(decrypted_2, message);
}
#[test]
fn context_must_match() {
let cell = SecureCell::with_key(SymmetricKey::new())
.unwrap()
.token_protect();
let message = b"Colorless green ideas sleep furiously".as_ref();
let context_a = b"The jaws that bite, the claws that catch!".as_ref();
let context_b = b"One, two! One, two! And through and through".as_ref();
let (encrypted, token) = cell.encrypt_with_context(&message, &context_a).unwrap();
// You cannot use a different context to decrypt data.
assert!(cell
.decrypt_with_context(&encrypted, &token, &context_b)
.is_err());
// Only the correct context will work.
let decrypted = cell
.decrypt_with_context(&encrypted, &token, &context_a)
.unwrap();
assert_eq!(decrypted, message);
}
#[test]
fn detects_corrupted_data() {
let cell = SecureCell::with_key(SymmetricKey::new())
.unwrap()
.token_protect();
let message = b"Colorless green ideas sleep furiously".as_ref();
let (encrypted, token) = cell.encrypt(&message).unwrap();
// Invert every odd byte, this will surely break the message.
let mut corrupted_data = encrypted;
for (i, b) in corrupted_data.iter_mut().enumerate() {
if i % 2 == 1 {
*b = !*b
}
}
assert!(cell.decrypt(&corrupted_data, &token).is_err());
}
#[test]
fn detects_truncated_data() {
let cell = SecureCell::with_key(SymmetricKey::new())
.unwrap()
.token_protect();
let message = b"Colorless green ideas sleep furiously".as_ref();
let (encrypted, token) = cell.encrypt(&message).unwrap();
let truncated_data = &encrypted[..encrypted.len() - 1];
assert!(cell.decrypt(&truncated_data, &token).is_err());
}
#[test]
fn detects_extended_data() {
let cell = SecureCell::with_key(SymmetricKey::new())
.unwrap()
.token_protect();
let message = b"Colorless green ideas sleep furiously".as_ref();
let (encrypted, token) = cell.encrypt(&message).unwrap();
let mut extended_data = encrypted;
extended_data.push(0);
assert!(cell.decrypt(&extended_data, &token).is_err());
}
#[test]
// FIXME(ilammy, 2020-05-25): avoid capacity allocation panics (T1649)
// This tests panics on 32-bit architectures due to size overflow.
// The implementation needs to use Vec::try_reserve instead of Vec::reserve
// when it becomes available in stable Rust.
#[cfg_attr(target_pointer_width = "32", ignore)]
fn detects_corrupted_token() {
let cell = SecureCell::with_key(SymmetricKey::new())
.unwrap()
.token_protect();
let message = b"Colorless green ideas sleep furiously".as_ref();
let (encrypted, token) = cell.encrypt(&message).unwrap();
// Invert every odd byte, this will surely break the token.
let mut corrupted_token = token;
for (i, b) in corrupted_token.iter_mut().enumerate() {
if i % 2 == 1 {
*b = !*b
}
}
assert!(cell.decrypt(&encrypted, &corrupted_token).is_err());
}
#[test]
fn detects_truncated_token() {
let cell = SecureCell::with_key(SymmetricKey::new())
.unwrap()
.token_protect();
let message = b"Colorless green ideas sleep furiously".as_ref();
let (encrypted, token) = cell.encrypt(&message).unwrap();
let truncated_token = &token[..token.len() - 1];
assert!(cell.decrypt(&encrypted, &truncated_token).is_err());
}
#[test]
fn detects_extended_token() {
let cell = SecureCell::with_key(SymmetricKey::new())
.unwrap()
.token_protect();
let message = b"Colorless green ideas sleep furiously".as_ref();
let (encrypted, token) = cell.encrypt(&message).unwrap();
let mut extended_token = token;
extended_token.push(0);
// Current implementation of Secure Cell allows the token to be overlong.
// Extra data is simply ignored.
let decrypted = cell.decrypt(&encrypted, &extended_token).unwrap();
assert_eq!(decrypted, message);
}
#[test]
// FIXME(ilammy, 2020-05-25): avoid capacity allocation panics (T1649)
// This tests panics on 32-bit architectures due to size overflow.
// The implementation needs to use Vec::try_reserve instead of Vec::reserve
// when it becomes available in stable Rust.
#[cfg_attr(target_pointer_width = "32", ignore)]
fn detects_data_token_swap() {
let cell = SecureCell::with_key(SymmetricKey::new())
.unwrap()
.token_protect();
let message = b"Colorless green ideas sleep furiously".as_ref();
let (encrypted, token) = cell.encrypt(&message).unwrap();
assert!(cell.decrypt(&token, &encrypted).is_err());
}
#[test]
fn empty_input_not_allowed() {
let cell = SecureCell::with_key(SymmetricKey::new())
.unwrap()
.token_protect();
assert!(cell.encrypt(&[]).is_err());
let message = b"Colorless green ideas sleep furiously".as_ref();
let (encrypted, token) = cell.encrypt(&message).unwrap();
assert!(cell.decrypt(&encrypted, &[]).is_err());
assert!(cell.decrypt(&[], &token).is_err());
}
}