authentik.root.tests.test_fips

FIPS mode tests

 1"""FIPS mode tests"""
 2
 3import hashlib
 4from unittest import skipUnless
 5
 6from cryptography.exceptions import InternalError, UnsupportedAlgorithm
 7from cryptography.hazmat.backends.openssl.backend import backend
 8from cryptography.hazmat.primitives import hashes, hmac
 9from cryptography.hazmat.primitives.ciphers.aead import ChaCha20Poly1305
10from cryptography.hazmat.primitives.kdf.scrypt import Scrypt
11from django.test import SimpleTestCase
12
13
14@skipUnless(backend._fips_enabled, "FIPS mode is off")
15class TestFIPS(SimpleTestCase):
16    """FIPS mode refuses algorithms that no FIPS standard approves. Ed25519 and ML-DSA aren't
17    checked: they're approved, the current FIPS provider just doesn't have them."""
18
19    def test_hashlib_md5(self):
20        """MD5 for security use"""
21        with self.assertRaises(ValueError):
22            hashlib.md5(b"")  # nosec
23
24    def test_hashlib_new(self):
25        """hashlib.new() falls back to Python's builtin MD5, so these have no builtin"""
26        for name in ("ripemd160", "sm3", "md5-sha1"):
27            with self.subTest(name=name), self.assertRaises(ValueError):
28                hashlib.new(name, b"")
29
30    def test_hashlib_pbkdf2_md5(self):
31        """PBKDF2 with MD5"""
32        with self.assertRaises(ValueError):
33            hashlib.pbkdf2_hmac("md5", b"", b"salt", 1)
34
35    def test_hashlib_scrypt(self):
36        """scrypt"""
37        with self.assertRaises(ValueError):
38            hashlib.scrypt(b"", salt=b"salt", n=2, r=1, p=1)
39
40    def test_cryptography_md5(self):
41        """MD5, which only fails once OpenSSL initializes the digest"""
42        with self.assertRaises((InternalError, UnsupportedAlgorithm)):
43            hashes.Hash(hashes.MD5())  # nosec
44
45    def test_cryptography_hmac_md5(self):
46        """HMAC with MD5"""
47        with self.assertRaises(UnsupportedAlgorithm):
48            hmac.HMAC(b"key", hashes.MD5())  # nosec
49
50    def test_cryptography_scrypt(self):
51        """scrypt"""
52        with self.assertRaises(UnsupportedAlgorithm):
53            Scrypt(salt=b"salt", length=32, n=2, r=1, p=1).derive(b"")
54
55    def test_cryptography_chacha20_poly1305(self):
56        """ChaCha20-Poly1305"""
57        with self.assertRaises(UnsupportedAlgorithm):
58            ChaCha20Poly1305(b"k" * 32).encrypt(b"n" * 12, b"", None)
@skipUnless(backend._fips_enabled, 'FIPS mode is off')
class TestFIPS(django.test.testcases.SimpleTestCase):
15@skipUnless(backend._fips_enabled, "FIPS mode is off")
16class TestFIPS(SimpleTestCase):
17    """FIPS mode refuses algorithms that no FIPS standard approves. Ed25519 and ML-DSA aren't
18    checked: they're approved, the current FIPS provider just doesn't have them."""
19
20    def test_hashlib_md5(self):
21        """MD5 for security use"""
22        with self.assertRaises(ValueError):
23            hashlib.md5(b"")  # nosec
24
25    def test_hashlib_new(self):
26        """hashlib.new() falls back to Python's builtin MD5, so these have no builtin"""
27        for name in ("ripemd160", "sm3", "md5-sha1"):
28            with self.subTest(name=name), self.assertRaises(ValueError):
29                hashlib.new(name, b"")
30
31    def test_hashlib_pbkdf2_md5(self):
32        """PBKDF2 with MD5"""
33        with self.assertRaises(ValueError):
34            hashlib.pbkdf2_hmac("md5", b"", b"salt", 1)
35
36    def test_hashlib_scrypt(self):
37        """scrypt"""
38        with self.assertRaises(ValueError):
39            hashlib.scrypt(b"", salt=b"salt", n=2, r=1, p=1)
40
41    def test_cryptography_md5(self):
42        """MD5, which only fails once OpenSSL initializes the digest"""
43        with self.assertRaises((InternalError, UnsupportedAlgorithm)):
44            hashes.Hash(hashes.MD5())  # nosec
45
46    def test_cryptography_hmac_md5(self):
47        """HMAC with MD5"""
48        with self.assertRaises(UnsupportedAlgorithm):
49            hmac.HMAC(b"key", hashes.MD5())  # nosec
50
51    def test_cryptography_scrypt(self):
52        """scrypt"""
53        with self.assertRaises(UnsupportedAlgorithm):
54            Scrypt(salt=b"salt", length=32, n=2, r=1, p=1).derive(b"")
55
56    def test_cryptography_chacha20_poly1305(self):
57        """ChaCha20-Poly1305"""
58        with self.assertRaises(UnsupportedAlgorithm):
59            ChaCha20Poly1305(b"k" * 32).encrypt(b"n" * 12, b"", None)

FIPS mode refuses algorithms that no FIPS standard approves. Ed25519 and ML-DSA aren't checked: they're approved, the current FIPS provider just doesn't have them.

def test_hashlib_md5(self):
20    def test_hashlib_md5(self):
21        """MD5 for security use"""
22        with self.assertRaises(ValueError):
23            hashlib.md5(b"")  # nosec

MD5 for security use

def test_hashlib_new(self):
25    def test_hashlib_new(self):
26        """hashlib.new() falls back to Python's builtin MD5, so these have no builtin"""
27        for name in ("ripemd160", "sm3", "md5-sha1"):
28            with self.subTest(name=name), self.assertRaises(ValueError):
29                hashlib.new(name, b"")

hashlib.new() falls back to Python's builtin MD5, so these have no builtin

def test_hashlib_pbkdf2_md5(self):
31    def test_hashlib_pbkdf2_md5(self):
32        """PBKDF2 with MD5"""
33        with self.assertRaises(ValueError):
34            hashlib.pbkdf2_hmac("md5", b"", b"salt", 1)

PBKDF2 with MD5

def test_hashlib_scrypt(self):
36    def test_hashlib_scrypt(self):
37        """scrypt"""
38        with self.assertRaises(ValueError):
39            hashlib.scrypt(b"", salt=b"salt", n=2, r=1, p=1)

scrypt

def test_cryptography_md5(self):
41    def test_cryptography_md5(self):
42        """MD5, which only fails once OpenSSL initializes the digest"""
43        with self.assertRaises((InternalError, UnsupportedAlgorithm)):
44            hashes.Hash(hashes.MD5())  # nosec

MD5, which only fails once OpenSSL initializes the digest

def test_cryptography_hmac_md5(self):
46    def test_cryptography_hmac_md5(self):
47        """HMAC with MD5"""
48        with self.assertRaises(UnsupportedAlgorithm):
49            hmac.HMAC(b"key", hashes.MD5())  # nosec

HMAC with MD5

def test_cryptography_scrypt(self):
51    def test_cryptography_scrypt(self):
52        """scrypt"""
53        with self.assertRaises(UnsupportedAlgorithm):
54            Scrypt(salt=b"salt", length=32, n=2, r=1, p=1).derive(b"")

scrypt

def test_cryptography_chacha20_poly1305(self):
56    def test_cryptography_chacha20_poly1305(self):
57        """ChaCha20-Poly1305"""
58        with self.assertRaises(UnsupportedAlgorithm):
59            ChaCha20Poly1305(b"k" * 32).encrypt(b"n" * 12, b"", None)

ChaCha20-Poly1305