4
4
5
5
6
6
import binascii
7
+ import mmap
7
8
import os
9
+ import sys
8
10
9
11
import pytest
10
12
26
28
from .utils import _load_all_params
27
29
28
30
29
- class FakeData (bytes ):
30
- def __len__ (self ):
31
- return 2 ** 31
32
-
33
-
34
31
def _aead_supported (cls ):
35
32
try :
36
33
cls (b"0" * 32 )
@@ -39,6 +36,10 @@ def _aead_supported(cls):
39
36
return False
40
37
41
38
39
+ def large_mmap ():
40
+ return mmap .mmap (- 1 , 2 ** 32 , prot = mmap .PROT_READ )
41
+
42
+
42
43
@pytest .mark .skipif (
43
44
_aead_supported (ChaCha20Poly1305 ),
44
45
reason = "Requires OpenSSL without ChaCha20Poly1305 support" ,
@@ -53,16 +54,21 @@ def test_chacha20poly1305_unsupported_on_older_openssl(backend):
53
54
reason = "Does not support ChaCha20Poly1305" ,
54
55
)
55
56
class TestChaCha20Poly1305 :
57
+ @pytest .mark .skipif (
58
+ sys .platform not in {"linux" , "darwin" }, reason = "mmap required"
59
+ )
56
60
def test_data_too_large (self ):
57
61
key = ChaCha20Poly1305 .generate_key ()
58
62
chacha = ChaCha20Poly1305 (key )
59
63
nonce = b"0" * 12
60
64
65
+ large_data = large_mmap ()
66
+
61
67
with pytest .raises (OverflowError ):
62
- chacha .encrypt (nonce , FakeData () , b"" )
68
+ chacha .encrypt (nonce , large_data , b"" )
63
69
64
70
with pytest .raises (OverflowError ):
65
- chacha .encrypt (nonce , b"" , FakeData () )
71
+ chacha .encrypt (nonce , b"" , large_data )
66
72
67
73
def test_generate_key (self ):
68
74
key = ChaCha20Poly1305 .generate_key ()
@@ -189,16 +195,21 @@ def test_buffer_protocol(self, backend):
189
195
reason = "Does not support AESCCM" ,
190
196
)
191
197
class TestAESCCM :
198
+ @pytest .mark .skipif (
199
+ sys .platform not in {"linux" , "darwin" }, reason = "mmap required"
200
+ )
192
201
def test_data_too_large (self ):
193
202
key = AESCCM .generate_key (128 )
194
203
aesccm = AESCCM (key )
195
204
nonce = b"0" * 12
196
205
206
+ large_data = large_mmap ()
207
+
197
208
with pytest .raises (OverflowError ):
198
- aesccm .encrypt (nonce , FakeData () , b"" )
209
+ aesccm .encrypt (nonce , large_data , b"" )
199
210
200
211
with pytest .raises (OverflowError ):
201
- aesccm .encrypt (nonce , b"" , FakeData () )
212
+ aesccm .encrypt (nonce , b"" , large_data )
202
213
203
214
def test_default_tag_length (self , backend ):
204
215
key = AESCCM .generate_key (128 )
@@ -362,16 +373,21 @@ def _load_gcm_vectors():
362
373
363
374
364
375
class TestAESGCM :
376
+ @pytest .mark .skipif (
377
+ sys .platform not in {"linux" , "darwin" }, reason = "mmap required"
378
+ )
365
379
def test_data_too_large (self ):
366
380
key = AESGCM .generate_key (128 )
367
381
aesgcm = AESGCM (key )
368
382
nonce = b"0" * 12
369
383
384
+ large_data = large_mmap ()
385
+
370
386
with pytest .raises (OverflowError ):
371
- aesgcm .encrypt (nonce , FakeData () , b"" )
387
+ aesgcm .encrypt (nonce , large_data , b"" )
372
388
373
389
with pytest .raises (OverflowError ):
374
- aesgcm .encrypt (nonce , b"" , FakeData () )
390
+ aesgcm .encrypt (nonce , b"" , large_data )
375
391
376
392
def test_vectors (self , backend , subtests ):
377
393
vectors = _load_gcm_vectors ()
@@ -496,16 +512,21 @@ def test_aesocb3_unsupported_on_older_openssl(backend):
496
512
reason = "Does not support AESOCB3" ,
497
513
)
498
514
class TestAESOCB3 :
515
+ @pytest .mark .skipif (
516
+ sys .platform not in {"linux" , "darwin" }, reason = "mmap required"
517
+ )
499
518
def test_data_too_large (self ):
500
519
key = AESOCB3 .generate_key (128 )
501
520
aesocb3 = AESOCB3 (key )
502
521
nonce = b"0" * 12
503
522
523
+ large_data = large_mmap ()
524
+
504
525
with pytest .raises (OverflowError ):
505
- aesocb3 .encrypt (nonce , FakeData () , b"" )
526
+ aesocb3 .encrypt (nonce , large_data , b"" )
506
527
507
528
with pytest .raises (OverflowError ):
508
- aesocb3 .encrypt (nonce , b"" , FakeData () )
529
+ aesocb3 .encrypt (nonce , b"" , large_data )
509
530
510
531
def test_vectors (self , backend , subtests ):
511
532
vectors = []
@@ -629,15 +650,20 @@ def test_buffer_protocol(self, backend):
629
650
reason = "Does not support AESSIV" ,
630
651
)
631
652
class TestAESSIV :
653
+ @pytest .mark .skipif (
654
+ sys .platform not in {"linux" , "darwin" }, reason = "mmap required"
655
+ )
632
656
def test_data_too_large (self ):
633
657
key = AESSIV .generate_key (256 )
634
658
aessiv = AESSIV (key )
635
659
660
+ large_data = large_mmap ()
661
+
636
662
with pytest .raises (OverflowError ):
637
- aessiv .encrypt (FakeData () , None )
663
+ aessiv .encrypt (large_data , None )
638
664
639
665
with pytest .raises (OverflowError ):
640
- aessiv .encrypt (b"irrelevant" , [FakeData () ])
666
+ aessiv .encrypt (b"irrelevant" , [large_data ])
641
667
642
668
def test_no_empty_encryption (self ):
643
669
key = AESSIV .generate_key (256 )
0 commit comments