Skip to content

Commit 69435b0

Browse files
committed
SHAKE: in case update is called right after squeezing, permute
Calling update after squeezing is undocumented and non standard, but if an application still decides to do it, permute the state before absorbing so that it's still safe to do so. We can easily do it since we keep track of the state. Still return an error as this is not the expected usage of SHAKE, and zeroing the state is another thing we could do.
1 parent 012bab9 commit 69435b0

File tree

4 files changed

+16
-4
lines changed

4 files changed

+16
-4
lines changed

src/libsodium/crypto_xof/shake128/ref/shake128_ref.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,13 @@ shake128_ref_update(shake128_state_internal *state, const unsigned char *in, siz
2828
{
2929
size_t consumed = 0;
3030
size_t chunk_size;
31+
int ret = 0;
3132

3233
if (state->phase != SHAKE128_PHASE_ABSORBING) {
34+
crypto_core_keccak1600_permute_24(&state->state);
3335
state->phase = SHAKE128_PHASE_ABSORBING;
3436
state->offset = 0;
37+
ret = -1;
3538
}
3639

3740
while (consumed < inlen) {
@@ -48,7 +51,7 @@ shake128_ref_update(shake128_state_internal *state, const unsigned char *in, siz
4851
consumed += chunk_size;
4952
}
5053

51-
return 0;
54+
return ret;
5255
}
5356

5457
static void

src/libsodium/crypto_xof/shake256/ref/shake256_ref.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,13 @@ shake256_ref_update(shake256_state_internal *state, const unsigned char *in, siz
2828
{
2929
size_t consumed = 0;
3030
size_t chunk_size;
31+
int ret = 0;
3132

3233
if (state->phase != SHAKE256_PHASE_ABSORBING) {
34+
crypto_core_keccak1600_permute_24(&state->state);
3335
state->phase = SHAKE256_PHASE_ABSORBING;
3436
state->offset = 0;
37+
ret = -1;
3538
}
3639

3740
while (consumed < inlen) {
@@ -48,7 +51,7 @@ shake256_ref_update(shake256_state_internal *state, const unsigned char *in, siz
4851
consumed += chunk_size;
4952
}
5053

51-
return 0;
54+
return ret;
5255
}
5356

5457
static void

src/libsodium/crypto_xof/turboshake128/ref/turboshake128_ref.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,13 @@ turboshake128_ref_update(turboshake128_state_internal *state, const unsigned cha
2828
{
2929
size_t consumed = 0;
3030
size_t chunk_size;
31+
int ret = 0;
3132

3233
if (state->phase != TURBOSHAKE128_PHASE_ABSORBING) {
34+
crypto_core_keccak1600_permute_12(&state->state);
3335
state->phase = TURBOSHAKE128_PHASE_ABSORBING;
3436
state->offset = 0;
37+
ret = -1;
3538
}
3639

3740
while (consumed < inlen) {
@@ -48,7 +51,7 @@ turboshake128_ref_update(turboshake128_state_internal *state, const unsigned cha
4851
consumed += chunk_size;
4952
}
5053

51-
return 0;
54+
return ret;
5255
}
5356

5457
static void

src/libsodium/crypto_xof/turboshake256/ref/turboshake256_ref.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,13 @@ turboshake256_ref_update(turboshake256_state_internal *state, const unsigned cha
2828
{
2929
size_t consumed = 0;
3030
size_t chunk_size;
31+
int ret = 0;
3132

3233
if (state->phase != TURBOSHAKE256_PHASE_ABSORBING) {
34+
crypto_core_keccak1600_permute_12(&state->state);
3335
state->phase = TURBOSHAKE256_PHASE_ABSORBING;
3436
state->offset = 0;
37+
ret = -1;
3538
}
3639

3740
while (consumed < inlen) {
@@ -48,7 +51,7 @@ turboshake256_ref_update(turboshake256_state_internal *state, const unsigned cha
4851
consumed += chunk_size;
4952
}
5053

51-
return 0;
54+
return ret;
5255
}
5356

5457
static void

0 commit comments

Comments
 (0)