Skip to content

Commit 78f72dd

Browse files
aryanputtaleofang
andauthored
Use FIPS-safe hashes for program cache keys (#2087)
* Use FIPS-safe hashes for program cache keys Signed-off-by: Aryan <aryansputta@gmail.com> * Fix FIPS cache test linting Signed-off-by: Aryan <aryansputta@gmail.com> * Use benchmarked FIPS-safe cache hashing Signed-off-by: Aryan <aryansputta@gmail.com> * Clarify SHA-384 cache digest comment Signed-off-by: Aryan <aryansputta@gmail.com> * Switch program cache hashing back to sha256 * Remove benchmark helper script --------- Signed-off-by: Aryan <aryansputta@gmail.com> Co-authored-by: Leo Fang <leof@nvidia.com>
1 parent dc4dfe8 commit 78f72dd

3 files changed

Lines changed: 17 additions & 8 deletions

File tree

cuda_core/cuda/core/utils/_program_cache/_file_stream.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -422,11 +422,17 @@ def _path_for_key(self, key: object) -> Path:
422422
k = _as_key_bytes(key)
423423
# Hash the key to a fixed-length identifier so arbitrary-length user
424424
# keys never exceed per-component filename limits (typically 255 on
425-
# ext4 / NTFS). With a 256-bit blake2b digest, the cache relies on
426-
# cryptographic collision resistance for key uniqueness -- two
427-
# distinct keys hashing to the same path is astronomically unlikely
428-
# (~2^-128 with the 32-byte digest in use here).
429-
digest = hashlib.blake2b(k, digest_size=32).hexdigest()
425+
# ext4 / NTFS).
426+
#
427+
# FIPS: must use a FIPS-approved hash algorithm. FIPS-enforcing
428+
# systems can disable non-approved hashlib algorithms (for example
429+
# blake2b) at the OpenSSL level. See #2043.
430+
#
431+
# With a 256-bit SHA-256 digest, the cache relies on collision
432+
# resistance for key uniqueness -- two distinct keys hashing to the
433+
# same path is astronomically unlikely (~2^128 practical collision
434+
# work).
435+
digest = hashlib.sha256(k, usedforsecurity=False).hexdigest()
430436
return self._entries / digest[:2] / digest[2:]
431437

432438
# -- mapping API ---------------------------------------------------------

cuda_core/cuda/core/utils/_program_cache/_keys.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
)
3636

3737
# Bump when the key schema changes in a way that invalidates existing caches.
38-
_KEY_SCHEMA_VERSION = 1
38+
_KEY_SCHEMA_VERSION = 2
3939

4040
_VALID_CODE_TYPES = frozenset({"c++", "ptx", "nvvm"})
4141
_VALID_TARGET_TYPES = frozenset({"ptx", "cubin", "ltoir"})
@@ -768,7 +768,10 @@ def make_program_cache_key(
768768
option_bytes = backend.option_fingerprint(options, target_type)
769769
name_tags = backend.encode_name_expressions(name_expressions)
770770

771-
hasher = hashlib.blake2b(digest_size=32)
771+
# IMPORTANT: Must use a FIPS-approved hash algorithm (SHA-2 family).
772+
# FIPS-enforcing systems can disable non-approved hashlib algorithms
773+
# (for example blake2b) at the OpenSSL level. See #2043.
774+
hasher = hashlib.sha256(usedforsecurity=False)
772775

773776
def _update(label: str, payload: bytes) -> None:
774777
hasher.update(label.encode("ascii"))

cuda_core/tests/test_program_cache.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1773,7 +1773,7 @@ def test_filestream_cache_size_cap_counts_tmp_files(tmp_path):
17731773

17741774
def test_filestream_cache_handles_long_keys(tmp_path):
17751775
"""Arbitrary-length keys must not overflow per-component filename limits.
1776-
The filename is a fixed-length 256-bit blake2b digest; key uniqueness
1776+
The filename is a fixed-length 256-bit digest; key uniqueness
17771777
relies on the digest's collision resistance."""
17781778
from cuda.core.utils import FileStreamProgramCache
17791779

0 commit comments

Comments
 (0)