Skip to content

Commit c0c13f2

Browse files
miss-islingtonAnsuelgpshead
authored
[3.13] gh-141907: Better handle support for SHA3 for test_hashlib (GH-141908) (#141919)
gh-141907: Better handle support for SHA3 for test_hashlib (GH-141908) * test_hashlib: better handle support for SHA3 It's possible that the SSL library supports only SHA3 algo and doesn't have SHAKE one. The current test wrongly detect this and set both HASH and HASHXOF to None expecting to have the extra SHA3 attributes present but this should only be true for SHAKE algo. To better handle this, move the HASH condition to a dedicated try-expect condition and check if HASHXOF is None in the relevant code effectively checking if SHA3 is supported by the SSL library but SHAKE algo needs to use the sha3module one. --------- (cherry picked from commit fee7782) Signed-off-by: Christian Marangi <ansuelsmth@gmail.com> Co-authored-by: Christian Marangi <ansuelsmth@gmail.com> Co-authored-by: Gregory P. Smith <greg@krypto.org>
1 parent 9127013 commit c0c13f2

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

Lib/test/test_hashlib.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,15 @@
4040
openssl_hashlib = import_fresh_module('hashlib', fresh=['_hashlib'])
4141

4242
try:
43-
from _hashlib import HASH, HASHXOF, openssl_md_meth_names, get_fips_mode
43+
import _hashlib
4444
except ImportError:
45-
HASH = None
46-
HASHXOF = None
47-
openssl_md_meth_names = frozenset()
48-
45+
_hashlib = None
46+
# The extension module may exist but only define some of these. gh-141907
47+
HASH = getattr(_hashlib, 'HASH', None)
48+
HASHXOF = getattr(_hashlib, 'HASHXOF', None)
49+
openssl_md_meth_names = getattr(_hashlib, 'openssl_md_meth_names', frozenset())
50+
get_fips_mode = getattr(_hashlib, 'get_fips_mode', None)
51+
if not get_fips_mode:
4952
def get_fips_mode():
5053
return 0
5154

@@ -558,9 +561,14 @@ def check_sha3(self, name, capacity, rate, suffix):
558561
constructors = self.constructors_to_test[name]
559562
for hash_object_constructor in constructors:
560563
m = hash_object_constructor()
561-
if HASH is not None and isinstance(m, HASH):
562-
# _hashopenssl's variant does not have extra SHA3 attributes
563-
continue
564+
if name.startswith('shake_'):
565+
if HASHXOF is not None and isinstance(m, HASHXOF):
566+
# _hashopenssl's variant does not have extra SHA3 attributes
567+
continue
568+
else:
569+
if HASH is not None and isinstance(m, HASH):
570+
# _hashopenssl's variant does not have extra SHA3 attributes
571+
continue
564572
self.assertEqual(capacity + rate, 1600)
565573
self.assertEqual(m._capacity_bits, capacity)
566574
self.assertEqual(m._rate_bits, rate)
@@ -1057,7 +1065,8 @@ def test_disallow_instantiation(self):
10571065
def test_hash_disallow_instantiation(self):
10581066
# internal types like _hashlib.HASH are not constructable
10591067
support.check_disallow_instantiation(self, HASH)
1060-
support.check_disallow_instantiation(self, HASHXOF)
1068+
if HASHXOF is not None:
1069+
support.check_disallow_instantiation(self, HASHXOF)
10611070

10621071
def test_readonly_types(self):
10631072
for algorithm, constructors in self.constructors_to_test.items():

0 commit comments

Comments
 (0)