10000 gh-99108: Import SHA2-224 and SHA2-256 from HACL* by msprotz · Pull Request #99109 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-99108: Import SHA2-224 and SHA2-256 from HACL* #99109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 36 commits into from
Feb 7, 2023
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
Use newly-implemented copy operation
  • Loading branch information
msprotz committed Dec 20, 2022
commit a632c84b6c09e87b51378821f171f1ddce8df6aa
30 changes: 28 additions & 2 deletions Modules/_hacl/Hacl_Streaming_SHA2.c
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ static inline void sha224_finish(uint32_t *st, uint8_t *h)
Allocate initial state for the SHA2_256 hash. The state is to be freed by
calling `free_256`.
*/
Hacl_Streaming_SHA2_state_sha2_224 *Hacl_Streaming_SHA2_create_in_256()
Hacl_Streaming_SHA2_state_sha2_224 *Hacl_Streaming_SHA2_create_in_256(void)
{
uint8_t *buf = (uint8_t *)KRML_HOST_CALLOC((uint32_t)64U, sizeof (uint8_t));
uint32_t *block_state = (uint32_t *)KRML_HOST_CALLOC((uint32_t)8U, sizeof (uint32_t));
Expand All @@ -269,6 +269,32 @@ Hacl_Streaming_SHA2_state_sha2_224 *Hacl_Streaming_SHA2_create_in_256()
return p;
}

/**
Copies the state passed as argument into a newly allocated state. The state
is to be freed by calling `free_256`
*/
Hacl_Streaming_SHA2_state_sha2_224
*Hacl_Streaming_SHA2_copy_256(Hacl_Streaming_SHA2_state_sha2_224 *s0)
{
Hacl_Streaming_SHA2_state_sha2_224 scrut = *s0;
uint32_t *block_state0 = scrut.block_state;
uint8_t *buf0 = scrut.buf;
uint64_t total_len0 = scrut.total_len;
uint8_t *buf = (uint8_t *)KRML_HOST_CALLOC((uint32_t)64U, sizeof (uint8_t));
memcpy(buf, buf0, (uint32_t)64U * sizeof (uint8_t));
uint32_t *block_state = (uint32_t *)KRML_HOST_CALLOC((uint32_t)8U, sizeof (uint32_t));
memcpy(block_state, block_state0, (uint32_t)8U * sizeof (uint32_t));
Hacl_Streaming_SHA2_state_sha2_224
s = { .block_state = block_state, .buf = buf, .total_len = total_len0 };
Hacl_Streaming_SHA2_state_sha2_224
*p =
(Hacl_Streaming_SHA2_state_sha2_224 *)KRML_HOST_MALLOC(sizeof (
Hacl_Streaming_SHA2_state_sha2_224
));
p[0U] = s;
return p;
}

/**
Reset an existing state to the initial hash state with empty data.
*/
Expand Down Expand Up @@ -550,7 +576,7 @@ void Hacl_Streaming_SHA2_sha256(uint8_t *input, uint32_t input_len, uint8_t *dst
sha256_finish(st, rb);
}

Hacl_Streaming_SHA2_state_sha2_224 *Hacl_Streaming_SHA2_create_in_224()
Hacl_Streaming_SHA2_state_sha2_224 *Hacl_Streaming_SHA2_create_in_224(void)
{
uint8_t *buf = (uint8_t *)KRML_HOST_CALLOC((uint32_t)64U, sizeof (uint8_t));
uint32_t *block_state = (uint32_t *)KRML_HOST_CALLOC((uint32_t)8U, sizeof (uint32_t));
Expand Down
11 changes: 9 additions & 2 deletions Modules/_hacl/Hacl_Streaming_SHA2.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,14 @@ typedef Hacl_Streaming_SHA2_state_sha2_224 Hacl_Streaming_SHA2_state_sha2_256;
Allocate initial state for the SHA2_256 hash. The state is to be freed by
calling `free_256`.
*/
Hacl_Streaming_SHA2_state_sha2_224 *Hacl_Streaming_SHA2_create_in_256();
Hacl_Streaming_SHA2_state_sha2_224 *Hacl_Streaming_SHA2_create_in_256(void);

/**
Copies the state passed as argument into a newly allocated state. The state
is to be freed by calling `free_256`
*/
Hacl_Streaming_SHA2_state_sha2_224
*Hacl_Streaming_SHA2_copy_256(Hacl_Streaming_SHA2_state_sha2_224 *s0);

/**
Reset an existing state to the initial hash state with empty data.
Expand Down Expand Up @@ -92,7 +99,7 @@ Hash `input`, of len `input_len`, into `dst`, an array of 32 bytes.
*/
void Hacl_Streaming_SHA2_sha256(uint8_t *input, uint32_t input_len, uint8_t *dst);

Hacl_Streaming_SHA2_state_sha2_224 *Hacl_Streaming_SHA2_create_in_224();
Hacl_Streaming_SHA2_state_sha2_224 *Hacl_Streaming_SHA2_create_in_224(void);

void Hacl_Streaming_SHA2_init_224(Hacl_Streaming_SHA2_state_sha2_224 *s);

Expand Down
8 changes: 4 additions & 4 deletions Modules/_hacl/refresh.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ if [[ $1 == "" ]]; then
fi

hacl_dir=$1
expected_rev=b0f29a4bd3a9408a9453b97de9cefffb11d89391
expected_rev=34b8a9fcd91859460b021dabc54deb961e02a675
actual_rev=$(cd "$hacl_dir" && git rev-parse HEAD)

if [[ $actual_rev != $expected_rev ]]; then
Expand Down Expand Up @@ -54,9 +54,9 @@ fi

all_files=$(find . -name '*.h' -or -name '*.c')

# types.h is a simple wrapper that defines the uint128 then proceeds to include
# FStar_UInt_8_16_32_64.h; we jump the types.h step since our current selection
# of algorithms does not necessitate the use of uint128
# types.h is a simple wrapper that defines the uint128 type then proceeds to
# include FStar_UInt_8_16_32_64.h; we jump the types.h step since our current
# selection of algorithms does not necessitate the use of uint128
$sed -i 's!#include.*types.h"!#include "krml/FStar_UInt_8_16_32_64.h"!g' $all_files
$sed -i 's!#include.*compat.h"!!g' $all_files

Expand Down
9 changes: 1 addition & 8 deletions Modules/sha256module.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,7 @@ _sha256_get_state(PyObject *module)
static void SHAcopy(SHAobject *src, SHAobject *dest)
{
dest->digestsize = src->digestsize;
// TODO: implement this in HACL
if (dest->digestsize == 28)
dest->state = Hacl_Streaming_SHA2_create_in_224();
else
dest->state = Hacl_Streaming_SHA2_create_in_256();
dest->state->total_len = src->state->total_len;
memcpy(dest->state->block_state, src->state->block_state, SHA_DIGESTSIZE);
memcpy(dest->state->buf, src->state->buf, SHA_BLOCKSIZE);
dest->state = Hacl_Streaming_SHA2_copy_256(src->state);
}

static SHAobject *
Expand Down
0