Skip to content

Commit 1fcc0ef

Browse files
msprotzgpsheaderlend-aasland
authored
gh-99108: Replace SHA2-224 & 256 with verified code from HACL* (#99109)
replacing hashlib primitives (for the non-OpenSSL case) with verified implementations from HACL*. This is the first PR in the series, and focuses specifically on SHA2-256 and SHA2-224. This PR imports Hacl_Streaming_SHA2 into the Python tree. This is the HACL* implementation of SHA2, which combines a core implementation of SHA2 along with a layer of buffer management that allows updating the digest with any number of bytes. This supersedes the previous implementation in the tree. @franziskuskiefer was kind enough to benchmark the changes: in addition to being verified (thus providing significant safety and security improvements), this implementation also provides a sizeable performance boost! ``` --------------------------------------------------------------- Benchmark Time CPU Iterations --------------------------------------------------------------- Sha2_256_Streaming 3163 ns 3160 ns 219353 // this PR LibTomCrypt_Sha2_256 5057 ns 5056 ns 136234 // library used by Python currently ``` The changes in this PR are as follows: - import the subset of HACL* that covers SHA2-256/224 into `Modules/_hacl` - rewire sha256module.c to use the HACL* implementation Co-authored-by: Gregory P. Smith [Google LLC] <[email protected]> Co-authored-by: Erlend E. Aasland <[email protected]>
1 parent 914f8fd commit 1fcc0ef

18 files changed

+1779
-350
lines changed

Lib/test/test_hashlib.py

+10
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from test.support import _4G, bigmemtest
2323
from test.support.import_helper import import_fresh_module
2424
from test.support import os_helper
25+
from test.support import requires_resource
2526
from test.support import threading_helper
2627
from test.support import warnings_helper
2728
from http.client import HTTPException
@@ -354,6 +355,15 @@ def test_large_update(self):
354355
self.assertEqual(m1.digest(*args), m4_copy.digest(*args))
355356
self.assertEqual(m4.digest(*args), m4_digest)
356357

358+
@requires_resource('cpu')
359+
def test_sha256_update_over_4gb(self):
360+
zero_1mb = b"\0" * 1024 * 1024
361+
h = hashlib.sha256()
362+
for i in range(0, 4096):
363+
h.update(zero_1mb)
364+
h.update(b"hello world")
365+
self.assertEqual(h.hexdigest(), "a5364f7a52ebe2e25f1838a4ca715a893b6fd7a23f2a0d9e9762120da8b1bf53")
366+
357367
def check(self, name, data, hexdigest, shake=False, **kwargs):
358368
length = len(hexdigest)//2
359369
hexdigest = hexdigest.lower()

Makefile.pre.in

+1-1
Original file line numberDiff line numberDiff line change
@@ -2612,7 +2612,7 @@ MODULE__HASHLIB_DEPS=$(srcdir)/Modules/hashlib.h
26122612
MODULE__IO_DEPS=$(srcdir)/Modules/_io/_iomodule.h
26132613
MODULE__MD5_DEPS=$(srcdir)/Modules/hashlib.h
26142614
MODULE__SHA1_DEPS=$(srcdir)/Modules/hashlib.h
2615-
MODULE__SHA256_DEPS=$(srcdir)/Modules/hashlib.h
2615+
MODULE__SHA256_DEPS=$(srcdir)/Modules/hashlib.h $(srcdir)/Modules/_hacl/include/krml/FStar_UInt_8_16_32_64.h $(srcdir)/Modules/_hacl/include/krml/lowstar_endianness.h $(srcdir)/Modules/_hacl/include/krml/internal/target.h $(srcdir)/Modules/_hacl/Hacl_Streaming_SHA2.h
26162616
MODULE__SHA3_DEPS=$(srcdir)/Modules/_sha3/sha3.c $(srcdir)/Modules/_sha3/sha3.h $(srcdir)/Modules/hashlib.h
26172617
MODULE__SHA512_DEPS=$(srcdir)/Modules/hashlib.h
26182618
MODULE__SOCKET_DEPS=$(srcdir)/Modules/socketmodule.h $(srcdir)/Modules/addrinfo.h $(srcdir)/Modules/getaddrinfo.c $(srcdir)/Modules/getnameinfo.c
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Replace the builtin :mod:`hashlib` implementations of SHA2-224 and SHA2-256
2+
originally from LibTomCrypt with formally verified, side-channel resistant
3+
code from the `HACL* <https://github.com/hacl-star/hacl-star/>`_ project. The
4+
builtins remain a fallback only used when OpenSSL does not provide them.

Modules/Setup.stdlib.in

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
# hashing builtins, can be disabled with --without-builtin-hashlib-hashes
8080
@MODULE__MD5_TRUE@_md5 md5module.c
8181
@MODULE__SHA1_TRUE@_sha1 sha1module.c
82-
@MODULE__SHA256_TRUE@_sha256 sha256module.c
82+
@MODULE__SHA256_TRUE@_sha256 sha256module.c _hacl/Hacl_Streaming_SHA2.c
8383
@MODULE__SHA512_TRUE@_sha512 sha512module.c
8484
@MODULE__SHA3_TRUE@_sha3 _sha3/sha3module.c
8585
@MODULE__BLAKE2_TRUE@_blake2 _blake2/blake2module.c _blake2/blake2b_impl.c _blake2/blake2s_impl.c

0 commit comments

Comments
 (0)