@@ -64,6 +64,10 @@ def get_fips_mode():
64
64
65
65
requires_blake2 = unittest .skipUnless (_blake2 , 'requires _blake2' )
66
66
67
+ # bpo-46913: Don't test the _sha3 extension on a Python UBSAN build
68
+ SKIP_SHA3 = support .check_sanitizer (ub = True )
69
+ requires_sha3 = unittest .skipUnless (not SKIP_SHA3 , 'requires _sha3' )
70
+
67
71
68
72
def hexstr (s ):
69
73
assert isinstance (s , bytes ), repr (s )
@@ -125,6 +129,8 @@ def __init__(self, *args, **kwargs):
125
129
126
130
self .constructors_to_test = {}
127
131
for algorithm in algorithms :
132
+ if SKIP_SHA3 and algorithm .startswith ('sha3_' ):
133
+ continue
128
134
self .constructors_to_test [algorithm ] = set ()
129
135
130
136
# For each algorithm, test the direct constructor and the use
@@ -177,14 +183,15 @@ def add_builtin_constructor(name):
177
183
add_builtin_constructor ('blake2s' )
178
184
add_builtin_constructor ('blake2b' )
179
185
180
- _sha3 = self ._conditional_import_module ('_sha3' )
181
- if _sha3 :
182
- add_builtin_constructor ('sha3_224' )
183
- add_builtin_constructor ('sha3_256' )
184
- add_builtin_constructor ('sha3_384' )
185
- add_builtin_constructor ('sha3_512' )
186
- add_builtin_constructor ('shake_128' )
187
- add_builtin_constructor ('shake_256' )
186
+ if not SKIP_SHA3 :
187
+ _sha3 = self ._conditional_import_module ('_sha3' )
188
+ if _sha3 :
189
+ add_builtin_constructor ('sha3_224' )
190
+ add_builtin_constructor ('sha3_256' )
191
+ add_builtin_constructor ('sha3_384' )
192
+ add_builtin_constructor ('sha3_512' )
193
+ add_builtin_constructor ('shake_128' )
194
+ add_builtin_constructor ('shake_256' )
188
195
189
196
super (HashLibTestCase , self ).__init__ (* args , ** kwargs )
190
197
@@ -383,6 +390,7 @@ def test_no_unicode_blake2(self):
383
390
self .check_no_unicode ('blake2b' )
384
391
self .check_no_unicode ('blake2s' )
385
392
393
+ @requires_sha3
386
394
def test_no_unicode_sha3 (self ):
387
395
self .check_no_unicode ('sha3_224' )
388
396
self .check_no_unicode ('sha3_256' )
@@ -418,6 +426,7 @@ def test_blocksize_name(self):
418
426
self .check_blocksize_name ('sha384' , 128 , 48 )
419
427
self .check_blocksize_name ('sha512' , 128 , 64 )
420
428
429
+ @requires_sha3
421
430
def test_blocksize_name_sha3 (self ):
422
431
self .check_blocksize_name ('sha3_224' , 144 , 28 )
423
432
self .check_blocksize_name ('sha3_256' , 136 , 32 )
@@ -438,6 +447,7 @@ def check_sha3(self, name, capacity, rate, suffix):
438
447
self .assertEqual (m ._rate_bits , rate )
439
448
self .assertEqual (m ._suffix , suffix )
440
449
450
+ @requires_sha3
441
451
def test_extra_sha3 (self ):
442
452
self .check_sha3 ('sha3_224' , 448 , 1152 , b'\x06 ' )
443
453
self .check_sha3 ('sha3_256' , 512 , 1088 , b'\x06 ' )
@@ -777,36 +787,44 @@ def test_blake2s_vectors(self):
777
787
key = bytes .fromhex (key )
778
788
self .check ('blake2s' , msg , md , key = key )
779
789
790
+ @requires_sha3
780
791
def test_case_sha3_224_0 (self ):
781
792
self .check ('sha3_224' , b"" ,
782
793
"6b4e03423667dbb73b6e15454f0eb1abd4597f9a1b078e3f5b5a6bc7" )
783
794
795
+ @requires_sha3
784
796
def test_case_sha3_224_vector (self ):
785
797
for msg , md in read_vectors ('sha3_224' ):
786
798
self .check ('sha3_224' , msg , md )
787
799
800
+ @requires_sha3
788
801
def test_case_sha3_256_0 (self ):
789
802
self .check ('sha3_256' , b"" ,
790
803
"a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a" )
791
804
805
+ @requires_sha3
792
806
def test_case_sha3_256_vector (self ):
793
807
for msg , md in read_vectors ('sha3_256' ):
794
808
self .check ('sha3_256' , msg , md )
795
809
810
+ @requires_sha3
796
811
def test_case_sha3_384_0 (self ):
797
812
self .check ('sha3_384' , b"" ,
798
813
"0c63a75b845e4f7d01107d852e4c2485c51a50aaaa94fc61995e71bbee983a2a" +
799
814
"c3713831264adb47fb6bd1e058d5f004" )
800
815
816
+ @requires_sha3
801
817
def test_case_sha3_384_vector (self ):
802
818
for msg , md in read_vectors ('sha3_384' ):
803
819
self .check ('sha3_384' , msg , md )
804
820
821
+ @requires_sha3
805
822
def test_case_sha3_512_0 (self ):
806
823
self .check ('sha3_512' , b"" ,
807
824
"a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a6" +
808
825
"15b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26" )
809
826
827
+ @requires_sha3
810
828
def test_case_sha3_512_vector (self ):
811
829
for msg , md in read_vectors ('sha3_512' ):
812
830
self .check ('sha3_512' , msg , md )
0 commit comments