-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Duc Tri Nguyen <[email protected]>
- Loading branch information
Showing
6 changed files
with
311 additions
and
25 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,177 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
#include <string.h> | ||
#include "fips202x.h" | ||
#include "fips202.h" | ||
#include "keccakf1600.h" | ||
|
||
#define KECCAK_CTX 25 | ||
|
||
static void keccak_absorb_x4(uint64_t *s, uint32_t r, | ||
const uint8_t *in0, | ||
const uint8_t *in1, | ||
const uint8_t *in2, | ||
const uint8_t *in3, | ||
size_t inlen, | ||
uint8_t p) | ||
{ | ||
|
||
while (inlen >= r) | ||
{ | ||
|
||
KeccakF1600_StateXORBytes(s + KECCAK_CTX * 0, in0, 0, r); | ||
KeccakF1600_StateXORBytes(s + KECCAK_CTX * 1, in1, 0, r); | ||
KeccakF1600_StateXORBytes(s + KECCAK_CTX * 2, in2, 0, r); | ||
KeccakF1600_StateXORBytes(s + KECCAK_CTX * 3, in3, 0, r); | ||
|
||
KeccakF1600_StatePermute(s + KECCAK_CTX * 0); | ||
KeccakF1600_StatePermute(s + KECCAK_CTX * 1); | ||
KeccakF1600_StatePermute(s + KECCAK_CTX * 2); | ||
KeccakF1600_StatePermute(s + KECCAK_CTX * 3); | ||
|
||
in0 += r; | ||
in1 += r; | ||
in2 += r; | ||
in3 += r; | ||
inlen -= r; | ||
} | ||
|
||
if (inlen > 0) | ||
{ | ||
KeccakF1600_StateXORBytes(s + KECCAK_CTX * 0, in0, 0, inlen); | ||
KeccakF1600_StateXORBytes(s + KECCAK_CTX * 1, in1, 0, inlen); | ||
KeccakF1600_StateXORBytes(s + KECCAK_CTX * 2, in2, 0, inlen); | ||
KeccakF1600_StateXORBytes(s + KECCAK_CTX * 3, in3, 0, inlen); | ||
} | ||
|
||
if (inlen == r - 1) | ||
{ | ||
p |= 128; | ||
KeccakF1600_StateXORBytes(s + KECCAK_CTX * 0, &p, inlen, 1); | ||
KeccakF1600_StateXORBytes(s + KECCAK_CTX * 1, &p, inlen, 1); | ||
KeccakF1600_StateXORBytes(s + KECCAK_CTX * 2, &p, inlen, 1); | ||
KeccakF1600_StateXORBytes(s + KECCAK_CTX * 3, &p, inlen, 1); | ||
} | ||
else | ||
{ | ||
KeccakF1600_StateXORBytes(s + KECCAK_CTX * 0, &p, inlen, 1); | ||
KeccakF1600_StateXORBytes(s + KECCAK_CTX * 1, &p, inlen, 1); | ||
KeccakF1600_StateXORBytes(s + KECCAK_CTX * 2, &p, inlen, 1); | ||
KeccakF1600_StateXORBytes(s + KECCAK_CTX * 3, &p, inlen, 1); | ||
p = 128; | ||
KeccakF1600_StateXORBytes(s + KECCAK_CTX * 0, &p, r - 1, 1); | ||
KeccakF1600_StateXORBytes(s + KECCAK_CTX * 1, &p, r - 1, 1); | ||
KeccakF1600_StateXORBytes(s + KECCAK_CTX * 2, &p, r - 1, 1); | ||
KeccakF1600_StateXORBytes(s + KECCAK_CTX * 3, &p, r - 1, 1); | ||
} | ||
} | ||
|
||
static void keccak_squeezeblocks_x4(uint8_t *out0, | ||
uint8_t *out1, | ||
uint8_t *out2, | ||
uint8_t *out3, | ||
size_t nblocks, | ||
uint64_t *s, | ||
uint32_t r) | ||
{ | ||
|
||
while (nblocks > 0) | ||
{ | ||
KeccakF1600_StatePermute(s + KECCAK_CTX * 0); | ||
KeccakF1600_StatePermute(s + KECCAK_CTX * 1); | ||
KeccakF1600_StatePermute(s + KECCAK_CTX * 2); | ||
KeccakF1600_StatePermute(s + KECCAK_CTX * 3); | ||
|
||
KeccakF1600_StateExtractBytes(s + KECCAK_CTX * 0, out0, 0, r); | ||
KeccakF1600_StateExtractBytes(s + KECCAK_CTX * 1, out1, 0, r); | ||
KeccakF1600_StateExtractBytes(s + KECCAK_CTX * 2, out2, 0, r); | ||
KeccakF1600_StateExtractBytes(s + KECCAK_CTX * 3, out3, 0, r); | ||
|
||
out0 += r; | ||
out1 += r; | ||
out2 += r; | ||
out3 += r; | ||
nblocks--; | ||
} | ||
} | ||
|
||
uint64_t *keccakx_get_lane_state(keccakx4_state *state, size_t index) | ||
{ | ||
if (index >= KECCAK_WAY) | ||
{ | ||
return NULL; | ||
} | ||
|
||
return state->ctx + index *KECCAK_CTX; | ||
} | ||
|
||
int shake128x4_absorb(keccakx4_state *state, | ||
const uint8_t *in0, | ||
const uint8_t *in1, | ||
const uint8_t *in2, | ||
const uint8_t *in3, | ||
size_t inlen) | ||
{ | ||
if (state == NULL || in0 == NULL || in1 == NULL || in2 == NULL || in3 == NULL) | ||
{ | ||
return 1; | ||
} | ||
|
||
memset(state->ctx, 0, sizeof(state->ctx)); | ||
|
||
keccak_absorb_x4(state->ctx, SHAKE128_RATE, in0, in1, in2, in3, inlen, 0x1F); | ||
|
||
return 0; | ||
} | ||
|
||
int shake256x4_absorb(keccakx4_state *state, | ||
const uint8_t *in0, | ||
const uint8_t *in1, | ||
const uint8_t *in2, | ||
const uint8_t *in3, | ||
size_t inlen) | ||
{ | ||
if (state == NULL || in0 == NULL || in1 == NULL || in2 == NULL || in3 == NULL) | ||
{ | ||
return 1; | ||
} | ||
|
||
memset(state->ctx, 0, sizeof(state->ctx)); | ||
|
||
keccak_absorb_x4(state->ctx, SHAKE256_RATE, in0, in1, in2, in3, inlen, 0x1F); | ||
|
||
return 0; | ||
} | ||
|
||
|
||
int shake128x4_squeezeblocks(uint8_t *out0, | ||
uint8_t *out1, | ||
uint8_t *out2, | ||
uint8_t *out3, | ||
size_t nblocks, | ||
keccakx4_state *state) | ||
{ | ||
if (state == NULL || out0 == NULL || out1 == NULL || out2 == NULL || out3 == NULL) | ||
{ | ||
return 1; | ||
} | ||
keccak_squeezeblocks_x4(out0, out1, out2, out3, nblocks, state->ctx, SHAKE128_RATE); | ||
|
||
return 0; | ||
} | ||
|
||
int shake256x4_squeezeblocks(uint8_t *out0, | ||
uint8_t *out1, | ||
uint8_t *out2, | ||
uint8_t *out3, | ||
size_t nblocks, | ||
keccakx4_state *state) | ||
{ | ||
|
||
if (state == NULL || out0 == NULL || out1 == NULL || out2 == NULL || out3 == NULL) | ||
{ | ||
return 1; | ||
} | ||
keccak_squeezeblocks_x4(out0, out1, out2, out3, nblocks, state->ctx, SHAKE256_RATE); | ||
|
||
return 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
#ifndef FIPS_202X_H | ||
#define FIPS_202X_H | ||
|
||
#ifndef KECCAK_WAY | ||
#define KECCAK_WAY 4 | ||
#endif | ||
|
||
#include <stdint.h> | ||
|
||
typedef struct | ||
{ | ||
uint64_t ctx[25 * KECCAK_WAY]; | ||
} keccakx4_state; | ||
|
||
uint64_t *keccakx_get_lane_state(keccakx4_state *state, size_t index); | ||
|
||
int shake128x4_absorb(keccakx4_state *state, | ||
const uint8_t *in0, | ||
const uint8_t *in1, | ||
const uint8_t *in2, | ||
const uint8_t *in3, | ||
size_t inlen); | ||
|
||
int shake256x4_absorb(keccakx4_state *state, | ||
const uint8_t *in0, | ||
const uint8_t *in1, | ||
const uint8_t *in2, | ||
const uint8_t *in3, | ||
size_t inlen); | ||
|
||
|
||
int shake128x4_squeezeblocks(uint8_t *out0, | ||
uint8_t *out1, | ||
uint8_t *out2, | ||
uint8_t *out3, | ||
size_t nblocks, | ||
keccakx4_state *state); | ||
|
||
int shake256x4_squeezeblocks(uint8_t *out0, | ||
uint8_t *out1, | ||
uint8_t *out2, | ||
uint8_t *out3, | ||
size_t nblocks, | ||
keccakx4_state *state); | ||
#endif |
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