-
Notifications
You must be signed in to change notification settings - Fork 122
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
SHA3 and SHAKE - New API Design #2084
Closed
+337
−237
Closed
Changes from 13 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
c6ed451
Introduce SHA3/SHAKE layered API design; Only SHA3/SHAKE files updates
manastasova a05d255
Add changes to ML-KEM based on SHA3/SHAKE new API Design
manastasova 50cf7fa
Add changes to ML-DSA based on SHA3/SHAKE new API Design
manastasova 4b0b92e
Update build files in generated-src
manastasova eb992ea
Update service indicator in SHA3_Final
manastasova d40fbec
Initialize |ctx->padded| to 0 for SHAKE inside SHAKE_Init
manastasova adb910d
Update service indicator at the end of SHAKE_Finalize; The XOF functi…
manastasova 02b8085
Fix conflicts with MLDSA parameters renaming
manastasova e61be0d
Merge branch 'main' into sha3_absorb_squeeze
manastasova 3008821
Merge branch 'aws:main' into sha3_absorb_squeeze
manastasova 2a1622f
Update SHAKE single-shot and streaming APIs
manastasova c5d0afd
Update incremental block-wise SHAKE squeezes in MLKEM
manastasova b6a5590
Update incremental block-wise SHAKE squeezes in MLDSA
manastasova 7ccaeba
Replace |keccak_st->padded| flag with |keccak_st->state| flag
manastasova 7edb6c7
Update MLKEM and MLDSA
manastasova 7386c1b
Update Keccak state flag in SHA3 functions
manastasova e424771
Address code review comments
manastasova 6597af1
Add export macro to functions in the tests
manastasova File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -301,24 +301,24 @@ static unsigned int ml_dsa_rej_uniform(int32_t *a, | |||||
* - const uint8_t seed[]: byte array with seed of length SEEDBYTES | ||||||
* - uint16_t nonce: 2-byte nonce | ||||||
**************************************************/ | ||||||
#define POLY_UNIFORM_NBLOCKS ((768 + SHAKE128_RATE - 1)/ SHAKE128_RATE) | ||||||
#define POLY_UNIFORM_NBLOCKS ((768 + SHAKE128_BLOCKSIZE - 1)/ SHAKE128_BLOCKSIZE) | ||||||
void ml_dsa_poly_uniform(ml_dsa_poly *a, | ||||||
const uint8_t seed[ML_DSA_SEEDBYTES], | ||||||
uint16_t nonce) | ||||||
{ | ||||||
unsigned int i, ctr, off; | ||||||
unsigned int buflen = POLY_UNIFORM_NBLOCKS*SHAKE128_RATE; | ||||||
uint8_t buf[POLY_UNIFORM_NBLOCKS*SHAKE128_RATE + 2]; | ||||||
unsigned int buflen = POLY_UNIFORM_NBLOCKS*SHAKE128_BLOCKSIZE; | ||||||
uint8_t buf[POLY_UNIFORM_NBLOCKS*SHAKE128_BLOCKSIZE + 2]; | ||||||
KECCAK1600_CTX state; | ||||||
|
||||||
uint8_t t[2]; | ||||||
t[0] = nonce & 0xff; | ||||||
t[1] = nonce >> 8; | ||||||
|
||||||
SHAKE_Init(&state, SHAKE128_BLOCKSIZE); | ||||||
SHA3_Update(&state, seed, ML_DSA_SEEDBYTES); | ||||||
SHA3_Update(&state, t, 2); | ||||||
SHAKE_Final(buf, &state, POLY_UNIFORM_NBLOCKS * SHAKE128_BLOCKSIZE); | ||||||
SHAKE_Absorb(&state, seed, ML_DSA_SEEDBYTES); | ||||||
SHAKE_Absorb(&state, t, 2); | ||||||
SHAKE_Squeeze(buf, &state, POLY_UNIFORM_NBLOCKS * SHAKE128_BLOCKSIZE); | ||||||
|
||||||
ctr = ml_dsa_rej_uniform(a->coeffs, ML_DSA_N, buf, buflen); | ||||||
|
||||||
|
@@ -327,8 +327,8 @@ void ml_dsa_poly_uniform(ml_dsa_poly *a, | |||||
for(i = 0; i < off; ++i) | ||||||
buf[i] = buf[buflen - off + i]; | ||||||
|
||||||
SHAKE_Final(buf + off, &state, POLY_UNIFORM_NBLOCKS * SHAKE128_BLOCKSIZE); | ||||||
buflen = SHAKE128_RATE + off; | ||||||
SHAKE_Squeeze(buf + off, &state, POLY_UNIFORM_NBLOCKS * SHAKE128_BLOCKSIZE); | ||||||
buflen = SHAKE128_BLOCKSIZE + off; | ||||||
ctr += ml_dsa_rej_uniform(a->coeffs + ctr, ML_DSA_N - ctr, buf, buflen); | ||||||
} | ||||||
/* FIPS 204. Section 3.6.3 Destruction of intermediate values. */ | ||||||
|
@@ -409,25 +409,26 @@ void ml_dsa_poly_uniform_eta(ml_dsa_params *params, | |||||
uint16_t nonce) | ||||||
{ | ||||||
unsigned int ctr; | ||||||
unsigned int buflen = ML_DSA_POLY_UNIFORM_ETA_NBLOCKS_MAX * SHAKE256_RATE; | ||||||
uint8_t buf[ML_DSA_POLY_UNIFORM_ETA_NBLOCKS_MAX * SHAKE256_RATE]; | ||||||
unsigned int buflen = ML_DSA_POLY_UNIFORM_ETA_NBLOCKS_MAX * SHAKE256_BLOCKSIZE; | ||||||
uint8_t buf[ML_DSA_POLY_UNIFORM_ETA_NBLOCKS_MAX * SHAKE256_BLOCKSIZE]; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Assuming this buf and buflen are related:
Suggested change
|
||||||
KECCAK1600_CTX state; | ||||||
|
||||||
uint8_t t[2]; | ||||||
t[0] = nonce & 0xff; | ||||||
t[1] = nonce >> 8; | ||||||
|
||||||
SHAKE_Init(&state, SHAKE256_BLOCKSIZE); | ||||||
SHA3_Update(&state, seed, ML_DSA_CRHBYTES); | ||||||
SHA3_Update(&state, t, 2); | ||||||
SHAKE_Final(buf, &state, ML_DSA_POLY_UNIFORM_ETA_NBLOCKS_MAX * SHAKE256_BLOCKSIZE); | ||||||
SHAKE_Absorb(&state, seed, ML_DSA_CRHBYTES); | ||||||
SHAKE_Absorb(&state, t, 2); | ||||||
SHAKE_Squeeze(buf, &state, ML_DSA_POLY_UNIFORM_ETA_NBLOCKS_MAX * SHAKE256_BLOCKSIZE); | ||||||
|
||||||
ctr = rej_eta(params, a->coeffs, ML_DSA_N, buf, buflen); | ||||||
|
||||||
while(ctr < ML_DSA_N) { | ||||||
SHAKE_Final(buf, &state, SHAKE256_BLOCKSIZE); | ||||||
ctr += rej_eta(params, a->coeffs + ctr, ML_DSA_N - ctr, buf, SHAKE256_RATE); | ||||||
SHAKE_Squeeze(buf, &state, SHAKE256_BLOCKSIZE); | ||||||
ctr += rej_eta(params, a->coeffs + ctr, ML_DSA_N - ctr, buf, SHAKE256_BLOCKSIZE); | ||||||
} | ||||||
|
||||||
/* FIPS 204. Section 3.6.3 Destruction of intermediate values. */ | ||||||
OPENSSL_cleanse(buf, sizeof(buf)); | ||||||
OPENSSL_cleanse(&state, sizeof(state)); | ||||||
|
@@ -445,23 +446,22 @@ void ml_dsa_poly_uniform_eta(ml_dsa_params *params, | |||||
* - const uint8_t seed[]: byte array with seed of length CRHBYTES | ||||||
* - uint16_t nonce: 16-bit nonce | ||||||
**************************************************/ | ||||||
#define POLY_UNIFORM_GAMMA1_NBLOCKS ((ML_DSA_POLYZ_PACKEDBYTES_MAX + SHAKE256_RATE - 1) / SHAKE256_RATE) | ||||||
#define POLY_UNIFORM_GAMMA1_NBLOCKS ((ML_DSA_POLYZ_PACKEDBYTES_MAX + SHAKE256_BLOCKSIZE - 1) / SHAKE256_BLOCKSIZE) | ||||||
void ml_dsa_poly_uniform_gamma1(ml_dsa_params *params, | ||||||
ml_dsa_poly *a, | ||||||
const uint8_t seed[ML_DSA_CRHBYTES], | ||||||
uint16_t nonce) | ||||||
{ | ||||||
uint8_t buf[POLY_UNIFORM_GAMMA1_NBLOCKS * SHAKE256_RATE]; | ||||||
uint8_t buf[POLY_UNIFORM_GAMMA1_NBLOCKS * SHAKE256_BLOCKSIZE]; | ||||||
KECCAK1600_CTX state; | ||||||
|
||||||
uint8_t t[2]; | ||||||
t[0] = nonce & 0xff; | ||||||
t[1] = nonce >> 8; | ||||||
|
||||||
SHAKE_Init(&state, SHAKE256_BLOCKSIZE); | ||||||
SHA3_Update(&state, seed, ML_DSA_CRHBYTES); | ||||||
SHA3_Update(&state, t, 2); | ||||||
|
||||||
SHAKE_Absorb(&state, seed, ML_DSA_CRHBYTES); | ||||||
SHAKE_Absorb(&state, t, 2); | ||||||
SHAKE_Final(buf, &state, POLY_UNIFORM_GAMMA1_NBLOCKS * SHAKE256_BLOCKSIZE); | ||||||
ml_dsa_polyz_unpack(params, a, buf); | ||||||
/* FIPS 204. Section 3.6.3 Destruction of intermediate values. */ | ||||||
|
@@ -483,12 +483,12 @@ void ml_dsa_poly_uniform_gamma1(ml_dsa_params *params, | |||||
void ml_dsa_poly_challenge(ml_dsa_params *params, ml_dsa_poly *c, const uint8_t *seed) { | ||||||
unsigned int i, b, pos; | ||||||
uint64_t signs; | ||||||
uint8_t buf[SHAKE256_RATE]; | ||||||
uint8_t buf[SHAKE256_BLOCKSIZE]; | ||||||
KECCAK1600_CTX state; | ||||||
|
||||||
SHAKE_Init(&state, SHAKE256_BLOCKSIZE); | ||||||
SHA3_Update(&state, seed, params->c_tilde_bytes); | ||||||
SHAKE_Final(buf, &state, SHAKE256_BLOCKSIZE); | ||||||
SHAKE_Absorb(&state, seed, params->c_tilde_bytes); | ||||||
SHAKE_Squeeze(buf, &state, SHAKE256_BLOCKSIZE); | ||||||
|
||||||
signs = 0; | ||||||
for(i = 0; i < 8; ++i) { | ||||||
|
@@ -501,8 +501,8 @@ void ml_dsa_poly_challenge(ml_dsa_params *params, ml_dsa_poly *c, const uint8_t | |||||
} | ||||||
for(i = ML_DSA_N-params->tau; i < ML_DSA_N; ++i) { | ||||||
do { | ||||||
if(pos >= SHAKE256_RATE) { | ||||||
SHAKE_Final(buf, &state, SHAKE256_BLOCKSIZE); | ||||||
if(pos >= SHAKE256_BLOCKSIZE) { | ||||||
SHAKE_Squeeze(buf, &state, SHAKE256_BLOCKSIZE); | ||||||
pos = 0; | ||||||
} | ||||||
|
||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the
+ 2
? Should that be reflected inbuflen
?