Skip to content

Commit

Permalink
pythongh-99108: Refresh HACL* (python#104808)
Browse files Browse the repository at this point in the history
Refresh HACL* from upstream to improve SHA2 performance and fix a 32-bit issue in SHA3.
  • Loading branch information
msprotz committed May 24, 2023
1 parent fe77a99 commit 160321e
Show file tree
Hide file tree
Showing 18 changed files with 225 additions and 149 deletions.
14 changes: 7 additions & 7 deletions Makefile.pre.in
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ ENSUREPIP= @ENSUREPIP@
# Internal static libraries
LIBMPDEC_A= Modules/_decimal/libmpdec/libmpdec.a
LIBEXPAT_A= Modules/expat/libexpat.a
LIBHACL_SHA2_A= Modules/_hacl/libHacl_Streaming_SHA2.a
LIBHACL_SHA2_A= Modules/_hacl/libHacl_Hash_SHA2.a

# Module state, compiler flags and linker flags
# Empty CFLAGS and LDFLAGS are omitted.
Expand Down Expand Up @@ -583,7 +583,7 @@ LIBEXPAT_HEADERS= \
# hashlib's HACL* library

LIBHACL_SHA2_OBJS= \
Modules/_hacl/Hacl_Streaming_SHA2.o
Modules/_hacl/Hacl_Hash_SHA2.o

LIBHACL_HEADERS= \
Modules/_hacl/include/krml/FStar_UInt128_Verified.h \
Expand All @@ -596,8 +596,8 @@ LIBHACL_HEADERS= \
Modules/_hacl/python_hacl_namespaces.h

LIBHACL_SHA2_HEADERS= \
Modules/_hacl/Hacl_Streaming_SHA2.h \
Modules/_hacl/internal/Hacl_SHA2_Generic.h \
Modules/_hacl/Hacl_Hash_SHA2.h \
Modules/_hacl/internal/Hacl_Hash_SHA2.h \
$(LIBHACL_HEADERS)

#########################################################################
Expand Down Expand Up @@ -964,11 +964,11 @@ $(LIBEXPAT_A): $(LIBEXPAT_OBJS)
$(AR) $(ARFLAGS) $@ $(LIBEXPAT_OBJS)

##########################################################################
# Build HACL* static libraries for hashlib: libHacl_Streaming_SHA2.a
# Build HACL* static libraries for hashlib: libHacl_Hash_SHA2.a
LIBHACL_CFLAGS=-I$(srcdir)/Modules/_hacl/include -D_BSD_SOURCE -D_DEFAULT_SOURCE $(PY_STDMODULE_CFLAGS) $(CCSHARED)

Modules/_hacl/Hacl_Streaming_SHA2.o: $(srcdir)/Modules/_hacl/Hacl_Streaming_SHA2.c $(LIBHACL_SHA2_HEADERS)
$(CC) -c $(LIBHACL_CFLAGS) -o $@ $(srcdir)/Modules/_hacl/Hacl_Streaming_SHA2.c
Modules/_hacl/Hacl_Hash_SHA2.o: $(srcdir)/Modules/_hacl/Hacl_Hash_SHA2.c $(LIBHACL_SHA2_HEADERS)
$(CC) -c $(LIBHACL_CFLAGS) -o $@ $(srcdir)/Modules/_hacl/Hacl_Hash_SHA2.c

$(LIBHACL_SHA2_A): $(LIBHACL_SHA2_OBJS)
-rm -f $@
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Refresh our new HACL* built-in :mod:`hashlib` code from upstream. Built-in
SHA2 should be faster and an issue with SHA3 on 32-bit platforms is fixed.
2 changes: 1 addition & 1 deletion Modules/Setup
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ PYTHONPATH=$(COREPYTHONPATH)
#_blake2 _blake2/blake2module.c _blake2/blake2b_impl.c _blake2/blake2s_impl.c
#_md5 md5module.c -I$(srcdir)/Modules/_hacl/include _hacl/Hacl_Hash_MD5.c -D_BSD_SOURCE -D_DEFAULT_SOURCE
#_sha1 sha1module.c -I$(srcdir)/Modules/_hacl/include _hacl/Hacl_Hash_SHA1.c -D_BSD_SOURCE -D_DEFAULT_SOURCE
#_sha2 sha2module.c -I$(srcdir)/Modules/_hacl/include Modules/_hacl/libHacl_Streaming_SHA2.a
#_sha2 sha2module.c -I$(srcdir)/Modules/_hacl/include Modules/_hacl/libHacl_Hash_SHA2.a
#_sha3 sha3module.c -I$(srcdir)/Modules/_hacl/include _hacl/Hacl_Hash_SHA3.c -D_BSD_SOURCE -D_DEFAULT_SOURCE

# text encodings and unicode
Expand Down
2 changes: 1 addition & 1 deletion Modules/Setup.stdlib.in
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
# hashing builtins, can be disabled with --without-builtin-hashlib-hashes
@MODULE__MD5_TRUE@_md5 md5module.c -I$(srcdir)/Modules/_hacl/include _hacl/Hacl_Hash_MD5.c -D_BSD_SOURCE -D_DEFAULT_SOURCE
@MODULE__SHA1_TRUE@_sha1 sha1module.c -I$(srcdir)/Modules/_hacl/include _hacl/Hacl_Hash_SHA1.c -D_BSD_SOURCE -D_DEFAULT_SOURCE
@MODULE__SHA2_TRUE@_sha2 sha2module.c -I$(srcdir)/Modules/_hacl/include Modules/_hacl/libHacl_Streaming_SHA2.a
@MODULE__SHA2_TRUE@_sha2 sha2module.c -I$(srcdir)/Modules/_hacl/include Modules/_hacl/libHacl_Hash_SHA2.a
@MODULE__SHA3_TRUE@_sha3 sha3module.c -I$(srcdir)/Modules/_hacl/include _hacl/Hacl_Hash_SHA3.c -D_BSD_SOURCE -D_DEFAULT_SOURCE
@MODULE__BLAKE2_TRUE@_blake2 _blake2/blake2module.c _blake2/blake2b_impl.c _blake2/blake2s_impl.c

Expand Down
6 changes: 3 additions & 3 deletions Modules/_hacl/Hacl_Hash_MD5.c
Original file line number Diff line number Diff line change
Expand Up @@ -1227,14 +1227,14 @@ void Hacl_Streaming_MD5_legacy_init(Hacl_Streaming_MD_state_32 *s)
/**
0 = success, 1 = max length exceeded
*/
uint32_t
Hacl_Streaming_Types_error_code
Hacl_Streaming_MD5_legacy_update(Hacl_Streaming_MD_state_32 *p, uint8_t *data, uint32_t len)
{
Hacl_Streaming_MD_state_32 s = *p;
uint64_t total_len = s.total_len;
if ((uint64_t)len > (uint64_t)2305843009213693951U - total_len)
{
return (uint32_t)1U;
return Hacl_Streaming_Types_MaximumLengthExceeded;
}
uint32_t sz;
if (total_len % (uint64_t)(uint32_t)64U == (uint64_t)0U && total_len > (uint64_t)0U)
Expand Down Expand Up @@ -1399,7 +1399,7 @@ Hacl_Streaming_MD5_legacy_update(Hacl_Streaming_MD_state_32 *p, uint8_t *data, u
}
);
}
return (uint32_t)0U;
return Hacl_Streaming_Types_Success;
}

void Hacl_Streaming_MD5_legacy_finish(Hacl_Streaming_MD_state_32 *p, uint8_t *dst)
Expand Down
2 changes: 1 addition & 1 deletion Modules/_hacl/Hacl_Hash_MD5.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void Hacl_Streaming_MD5_legacy_init(Hacl_Streaming_MD_state_32 *s);
/**
0 = success, 1 = max length exceeded
*/
uint32_t
Hacl_Streaming_Types_error_code
Hacl_Streaming_MD5_legacy_update(Hacl_Streaming_MD_state_32 *p, uint8_t *data, uint32_t len);

void Hacl_Streaming_MD5_legacy_finish(Hacl_Streaming_MD_state_32 *p, uint8_t *dst);
Expand Down
6 changes: 3 additions & 3 deletions Modules/_hacl/Hacl_Hash_SHA1.c
Original file line number Diff line number Diff line change
Expand Up @@ -263,14 +263,14 @@ void Hacl_Streaming_SHA1_legacy_init(Hacl_Streaming_MD_state_32 *s)
/**
0 = success, 1 = max length exceeded
*/
uint32_t
Hacl_Streaming_Types_error_code
Hacl_Streaming_SHA1_legacy_update(Hacl_Streaming_MD_state_32 *p, uint8_t *data, uint32_t len)
{
Hacl_Streaming_MD_state_32 s = *p;
uint64_t total_len = s.total_len;
if ((uint64_t)len > (uint64_t)2305843009213693951U - total_len)
{
return (uint32_t)1U;
return Hacl_Streaming_Types_MaximumLengthExceeded;
}
uint32_t sz;
if (total_len % (uint64_t)(uint32_t)64U == (uint64_t)0U && total_len > (uint64_t)0U)
Expand Down Expand Up @@ -435,7 +435,7 @@ Hacl_Streaming_SHA1_legacy_update(Hacl_Streaming_MD_state_32 *p, uint8_t *data,
}
);
}
return (uint32_t)0U;
return Hacl_Streaming_Types_Success;
}

void Hacl_Streaming_SHA1_legacy_finish(Hacl_Streaming_MD_state_32 *p, uint8_t *dst)
Expand Down
2 changes: 1 addition & 1 deletion Modules/_hacl/Hacl_Hash_SHA1.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void Hacl_Streaming_SHA1_legacy_init(Hacl_Streaming_MD_state_32 *s);
/**
0 = success, 1 = max length exceeded
*/
uint32_t
Hacl_Streaming_Types_error_code
Hacl_Streaming_SHA1_legacy_update(Hacl_Streaming_MD_state_32 *p, uint8_t *data, uint32_t len);

void Hacl_Streaming_SHA1_legacy_finish(Hacl_Streaming_MD_state_32 *p, uint8_t *dst);
Expand Down

0 comments on commit 160321e

Please sign in to comment.