Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
f941537
Replace SHA2 implementation with verified code from HACL*
msprotz Nov 4, 2022
89712f6
Fixup some casts
msprotz Nov 4, 2022
21fddf1
And proper error handling for maximum hashing lengths exceeded
msprotz Nov 4, 2022
6c5498e
Remove extra file
msprotz Nov 4, 2022
77c682c
See if using srcdir helps
msprotz Nov 4, 2022
1586188
Delete 11 un-needed files
msprotz Nov 7, 2022
621ef0d
Wrong direction for error checking
msprotz Nov 7, 2022
81cac0f
Remove another four files
msprotz Nov 7, 2022
5134c6a
better code quality
msprotz Nov 7, 2022
f212447
Fixup memory management mistake
msprotz Nov 7, 2022
6543fdd
Fix Linux build
msprotz Nov 7, 2022
a52e9ce
Address review comments from @tiran; regenerate configure
msprotz Nov 8, 2022
de35332
Merge remote-tracking branch 'origin/sha2_hacl' into sha2_hacl
msprotz Nov 8, 2022
7f997a9
Move Linux & BSD CFLAGS into configure
msprotz Nov 8, 2022
ec36acb
Add NEWS entry.
msprotz Nov 8, 2022
c5d5e67
SRCDIRS
msprotz Nov 8, 2022
adc0b8b
Properly regenerate configure
msprotz Nov 8, 2022
ca1e02e
Fix paths in Windows build
msprotz Nov 8, 2022
47be718
Add include directory
msprotz Nov 8, 2022
b508177
Fixup include paths
msprotz Nov 8, 2022
e893889
Fix patchcheck
msprotz Nov 9, 2022
1c82dc2
Automate import of HACL* into the tree with a script
msprotz Dec 20, 2022
a632c84
Use newly-implemented copy operation
msprotz Dec 20, 2022
60d87fe
Merge remote-tracking branch 'upstream/main' into sha2_hacl
msprotz Dec 20, 2022
f4b267e
Add a suitable test for data > 4GB
msprotz Dec 20, 2022
537831e
Make more robust & follow shellcheck.
gpshead Jan 31, 2023
f33e2ef
Update to the latest HACL* revision.
gpshead Jan 31, 2023
ce292e7
Add a README to the _hacl directory.
gpshead Jan 31, 2023
aa24fd4
reword and ReSTify the news entry.
gpshead Jan 31, 2023
29fd3c9
Dynamically rename the Hacl_ C symbols.
gpshead Jan 31, 2023
0362c1a
missing file add & readme update.
gpshead Jan 31, 2023
0f33a6a
_
gpshead Jan 31, 2023
d7c1240
remove wrongly placed defines.
gpshead Jan 31, 2023
9ec37a6
Merge branch 'main' into sha2_hacl
gpshead Jan 31, 2023
92033ad
Stylistic fix in Modules/sha256module.c
msprotz Jan 31, 2023
c670530
Fix a warning on Windows builders
msprotz Jan 31, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Dynamically rename the Hacl_ C symbols.
We do this with other third party code as well, it avoids linking and
dynamic linking ODR violations when extension module or embedding
applications also use their own version of the library code in question.

+ Minor annotation on the test, use `python -m test -u cpu`
+ Add a link to the readme.
  • Loading branch information
gpshead committed Jan 31, 2023
commit 29fd3c94f4de81800314f3704def039e76bfe06f
4 changes: 3 additions & 1 deletion Lib/test/test_hashlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from test.support import _4G, bigmemtest
from test.support.import_helper import import_fresh_module
from test.support import os_helper
from test.support import requires_resource
from test.support import threading_helper
from test.support import warnings_helper
from http.client import HTTPException
Expand Down Expand Up @@ -354,7 +355,8 @@ def test_large_update(self):
self.assertEqual(m1.digest(*args), m4_copy.digest(*args))
self.assertEqual(m4.digest(*args), m4_digest)

def test_sha256_over_4gb(self):
@requires_resource('cpu')
def test_sha256_update_over_4gb(self):
zero_1mb = b"\0" * 1024 * 1024
h = hashlib.sha256()
for i in range(0, 4096):
Expand Down
1 change: 1 addition & 0 deletions Modules/_hacl/Hacl_Streaming_SHA2.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ extern "C" {
#endif

#include <string.h>
#include "python_hacl_namespaces.h"
#include "krml/FStar_UInt_8_16_32_64.h"
#include "krml/lowstar_endianness.h"
#include "krml/internal/target.h"
Expand Down
3 changes: 3 additions & 0 deletions Modules/_hacl/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Algorithm implementations used by the `hashlib` module.

This code comes from the
[HACL\*](https://github.com/hacl-star/hacl-star/) project.

HACL\* is a cryptographic library that has been formally verified for memory
safety, functional correctness, and secret independence.

Expand Down
3 changes: 3 additions & 0 deletions Modules/_hacl/refresh.sh
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ $sed -i -z 's/\n\n\([^\n]\+\n\)*[^\n]*\(384\|512\)[^{]*{\n\?\( [^\n]*\n\)*}//g'
# Same thing with function prototypes
$sed -i -z 's/\n\n\([^\n]\+\n\)*[^\n]*\(384\|512\)[^;]*;//g' Hacl_Streaming_SHA2.h

# Use globally unique names for the Hacl_ C APIs to avoid linkage conflicts.
$sed -i -z 's!#include <string.h>\n!#include <string.h>\n#include "python_hacl_namespaces.h"\n!' Hacl_Streaming_SHA2.h

# Finally, we remove a bunch of ifdefs from target.h that are, again, useful in
# the general case, but not exercised by the subset of HACL* that we vendor.
$sed -z -i 's!#ifndef KRML_\(HOST_PRINTF\|HOST_EXIT\|PRE_ALIGN\|POST_ALIGN\|ALIGNED_MALLOC\|ALIGNED_FREE\|HOST_TIME\)\n\(\n\|# [^\n]*\n\|[^#][^\n]*\n\)*#endif\n\n!!g' include/krml/internal/target.h
Expand Down
25 changes: 23 additions & 2 deletions Modules/sha256module.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,26 @@ class SHA256Type "SHAobject *" "&PyType_Type"
/* The SHA2-224 and SHA2-256 implementations defer to the HACL* verified
* library. */

/* C's excuse for namespaces: Use unique names to avoid linkage conflicts
* with builds linking or dynamically loading other code potentially using
* HACL* libraries. */
#define Hacl_Streaming_SHA2_state_sha2_224_s python_hashlib_Hacl_Streaming_SHA2_state_sha2_224_s
#define Hacl_Streaming_SHA2_state_sha2_224 python_hashlib_Hacl_Streaming_SHA2_state_sha2_224
#define Hacl_Streaming_SHA2_state_sha2_256 python_hashlib_Hacl_Streaming_SHA2_state_sha2_256
#define Hacl_Streaming_SHA2_create_in_256 python_hashlib_Hacl_Streaming_SHA2_create_in_256
#define Hacl_Streaming_SHA2_create_in_224 python_hashlib_Hacl_Streaming_SHA2_create_in_224
#define Hacl_Streaming_SHA2_copy_256 python_hashlib_Hacl_Streaming_SHA2_copy_256
#define Hacl_Streaming_SHA2_copy_224 python_hashlib_Hacl_Streaming_SHA2_copy_224
#define Hacl_Streaming_SHA2_init_256 python_hashlib_Hacl_Streaming_SHA2_init_256
#define Hacl_Streaming_SHA2_init_224 python_hashlib_Hacl_Streaming_SHA2_init_224
#define Hacl_Streaming_SHA2_update_256 python_hashlib_Hacl_Streaming_SHA2_update_256
#define Hacl_Streaming_SHA2_update_224 python_hashlib_Hacl_Streaming_SHA2_update_224
#define Hacl_Streaming_SHA2_finish_256 python_hashlib_Hacl_Streaming_SHA2_finish_256
#define Hacl_Streaming_SHA2_finish_224 python_hashlib_Hacl_Streaming_SHA2_finish_224
#define Hacl_Streaming_SHA2_free_256 python_hashlib_Hacl_Streaming_SHA2_free_256
#define Hacl_Streaming_SHA2_free_224 python_hashlib_Hacl_Streaming_SHA2_free_224
#define Hacl_Streaming_SHA2_sha256 python_hashlib_Hacl_Streaming_SHA2_sha256
#define Hacl_Streaming_SHA2_sha224 python_hashlib_Hacl_Streaming_SHA2_sha224
#include "_hacl/Hacl_Streaming_SHA2.h"

typedef struct {
Expand Down Expand Up @@ -230,10 +250,11 @@ SHA256_get_block_size(PyObject *self, void *closure)
static PyObject *
SHA256_get_name(SHAobject *self, void *closure)
{
if (self->digestsize == 28)
if (self->digestsize == 28) {
return PyUnicode_FromStringAndSize("sha224", 6);
else
} else {
return PyUnicode_FromStringAndSize("sha256", 6);
}
}

static PyGetSetDef SHA_getseters[] = {
Expand Down