Skip to content

Commit 6b74d9e

Browse files
stratakisMarcel Plch
authored andcommitted
Add initial tests for various hashes under FIPS mode
1 parent a7996ce commit 6b74d9e

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

Lib/test/test_fips.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import unittest
2+
import hmac, _hmacopenssl
3+
import hashlib, _hashlib
4+
5+
6+
7+
class HashlibFipsTests(unittest.TestCase):
8+
9+
@unittest.skipUnless(hashlib.get_fips_mode(), "Test only when FIPS is enabled")
10+
def test_fips_imports(self):
11+
"""blake2s and blake2b should fail to import in FIPS mode
12+
"""
13+
with self.assertRaises(ValueError, msg='blake2s not available in FIPS'):
14+
m = hashlib.blake2s()
15+
with self.assertRaises(ValueError, msg='blake2b not available in FIPS'):
16+
m = hashlib.blake2b()
17+
18+
def compare_hashes(self, python_hash, openssl_hash):
19+
"""
20+
Compare between the python implementation and the openssl one that the digests
21+
are the same
22+
"""
23+
if python_hash.name.startswith('shake_128'):
24+
m = python_hash.hexdigest(16)
25+
elif python_hash.name.startswith('shake_256'):
26+
m = python_hash.hexdigest(32)
27+
else:
28+
m = python_hash.hexdigest()
29+
h = openssl_hash.hexdigest()
30+
31+
self.assertEqual(m, h)
32+
33+
@unittest.skipIf(hashlib.get_fips_mode(), "blake2 hashes are not available under FIPS")
34+
def test_blake2_hashes(self):
35+
self.compare_hashes(hashlib.blake2b(b'abc'), _hashlib.openssl_blake2b(b'abc'))
36+
self.compare_hashes(hashlib.blake2s(b'abc'), _hashlib.openssl_blake2s(b'abc'))
37+
38+
def test_sha3_hashes(self):
39+
self.compare_hashes(hashlib.sha3_224(b'abc'), _hashlib.openssl_sha3_224(b'abc'))
40+
self.compare_hashes(hashlib.sha3_256(b'abc'), _hashlib.openssl_sha3_256(b'abc'))
41+
self.compare_hashes(hashlib.sha3_384(b'abc'), _hashlib.openssl_sha3_384(b'abc'))
42+
self.compare_hashes(hashlib.sha3_512(b'abc'), _hashlib.openssl_sha3_512(b'abc'))
43+
44+
@unittest.skipIf(hashlib.get_fips_mode(), "shake hashes are not available under FIPS")
45+
def test_shake_hashes(self):
46+
self.compare_hashes(hashlib.shake_128(b'abc'), _hashlib.openssl_shake_128(b'abc'))
47+
self.compare_hashes(hashlib.shake_256(b'abc'), _hashlib.openssl_shake_256(b'abc'))
48+
49+
def test_sha(self):
50+
self.compare_hashes(hashlib.sha1(b'abc'), _hashlib.openssl_sha1(b'abc'))
51+
self.compare_hashes(hashlib.sha224(b'abc'), _hashlib.openssl_sha224(b'abc'))
52+
self.compare_hashes(hashlib.sha256(b'abc'), _hashlib.openssl_sha256(b'abc'))
53+
self.compare_hashes(hashlib.sha384(b'abc'), _hashlib.openssl_sha384(b'abc'))
54+
self.compare_hashes(hashlib.sha512(b'abc'), _hashlib.openssl_sha512(b'abc'))
55+
56+
def test_hmac_digests(self):
57+
self.compare_hashes(_hmacopenssl.new(b'My hovercraft is full of eels', digestmod='sha384'),
58+
hmac.new(b'My hovercraft is full of eels', digestmod='sha384'))
59+
60+
61+
62+
63+
if __name__ == "__main__":
64+
unittest.main()

0 commit comments

Comments
 (0)