Skip to content
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

Added SHA3/SHAKE XOF functionality #1839

Merged
merged 28 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
ee1c120
added SHA3/SHAKE XOF functionality
jakemas Sep 9, 2024
2505ce9
Update crypto/fipsmodule/sha/internal.h
jakemas Sep 9, 2024
c8cb84d
Update crypto/fipsmodule/sha/internal.h
jakemas Sep 9, 2024
b8e5a09
Update crypto/fipsmodule/sha/keccak1600.c
jakemas Sep 9, 2024
d9e1c25
Update crypto/fipsmodule/sha/keccak1600.c
jakemas Sep 9, 2024
f1a425d
Update crypto/fipsmodule/sha/internal.h
jakemas Sep 9, 2024
ea632ea
Update crypto/fipsmodule/sha/keccak1600.c
jakemas Sep 9, 2024
3c41a92
Update crypto/fipsmodule/sha/keccak1600.c
jakemas Sep 9, 2024
cd8a406
Update crypto/fipsmodule/sha/keccak1600.c
jakemas Sep 9, 2024
26c3e1b
typo
jakemas Sep 9, 2024
5860219
Merge branch 'main' into sha3-xof
jakemas Sep 9, 2024
aaf756e
Merge branch 'main' into sha3-xof
jakemas Sep 9, 2024
ab12f35
added autogen build files
jakemas Sep 9, 2024
379ff03
added asm functions for sha3_squeeze
jakemas Sep 10, 2024
a8c5c5d
added gen-src files for keccak arm
jakemas Sep 10, 2024
ef1949c
more consistency with openssl for ASM
jakemas Sep 12, 2024
dc5cb36
Merge branch 'main' into sha3-xof
jakemas Sep 12, 2024
8950123
description change
jakemas Sep 12, 2024
edac785
padded flag use consistant with openssl
jakemas Sep 12, 2024
74b9df6
Merge branch 'main' into sha3-xof
jakemas Sep 12, 2024
3240910
variable re-name and addressing CR comments
jakemas Sep 12, 2024
cfcf293
Merge branch 'main' into sha3-xof
jakemas Sep 12, 2024
ec4218f
improved description
jakemas Sep 12, 2024
582a63a
Merge branch 'sha3-xof' of github.com:jakemas/aws-lc into sha3-xof
jakemas Sep 12, 2024
7b3368b
documentation edit
jakemas Sep 13, 2024
f843fb5
Merge branch 'main' into sha3-xof
jakemas Sep 13, 2024
f6dd916
updated gen-src
jakemas Sep 13, 2024
5f1933d
Merge branch 'main' into sha3-xof
jakemas Sep 13, 2024
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
3 changes: 3 additions & 0 deletions crypto/fipsmodule/sha/asm/keccak1600-armv8.pl
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,8 @@
mov $out,x1
mov $len,x2
mov $bsz,x3
cmp x4, #0 // x4 = 'next' argument
jakemas marked this conversation as resolved.
Show resolved Hide resolved
bne .Lnext_block
.Loop_squeeze:
ldr x4,[x0],#8
cmp $len,#8
Expand All @@ -470,6 +472,7 @@
beq .Lsqueeze_done
subs x3,x3,#8
bhi .Loop_squeeze
.Lnext_block:
mov x0,$A_flat
bl KeccakF1600
mov x0,$A_flat
Expand Down
10 changes: 7 additions & 3 deletions crypto/fipsmodule/sha/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ struct keccak_st {
size_t md_size; // output length, variable in XOF (SHAKE)
size_t buf_load; // used bytes in below buffer
uint8_t buf[SHA3_MAX_BLOCKSIZE]; // should have at least the max data block size bytes
uint8_t pad;
uint8_t pad; // padding character
uint8_t padded; // denotes if padding has been performed (used to facilitate multiple squeezes)
jakemas marked this conversation as resolved.
Show resolved Hide resolved
};
// Define SHA{n}[_{variant}]_ASM if sha{n}_block_data_order[_{variant}] is
// defined in assembly.
Expand Down Expand Up @@ -377,9 +378,12 @@ OPENSSL_EXPORT int SHA3_Final(uint8_t *md, KECCAK1600_CTX *ctx);
OPENSSL_EXPORT size_t SHA3_Absorb(uint64_t A[SHA3_ROWS][SHA3_ROWS],
const uint8_t *data, size_t len, size_t r);

// SHA3_Squeeze generate |out| hash value of |len| bytes.
// SHA3_Squeeze generates |out| value of |len| bytes (per call). It can be called
// multiple times when used as eXtendable Output Function. |first| indicates weather it is
jakemas marked this conversation as resolved.
Show resolved Hide resolved
// the first call to SHA3_Squeeze; i.e., if the current block has been already processed
// right after the last call to SHA3_Absorb.
OPENSSL_EXPORT void SHA3_Squeeze(uint64_t A[SHA3_ROWS][SHA3_ROWS],
uint8_t *out, size_t len, size_t r);
uint8_t *out, size_t len, size_t r, int first);
jakemas marked this conversation as resolved.
Show resolved Hide resolved
jakemas marked this conversation as resolved.
Show resolved Hide resolved

#if defined(__cplusplus)
} // extern "C"
Expand Down
20 changes: 11 additions & 9 deletions crypto/fipsmodule/sha/keccak1600.c
Original file line number Diff line number Diff line change
Expand Up @@ -356,16 +356,21 @@ size_t SHA3_Absorb(uint64_t A[SHA3_ROWS][SHA3_ROWS], const uint8_t *inp, size_t
return len;
}

// SHA3_Squeeze is called once at the end to generate |out| hash value
// of |len| bytes.
void SHA3_Squeeze(uint64_t A[SHA3_ROWS][SHA3_ROWS], uint8_t *out, size_t len, size_t r)
// SHA3_Squeeze can be called multiple times to incrementally
jakemas marked this conversation as resolved.
Show resolved Hide resolved
// generate |out| value of |len| bytes. |first| indicates weather
// it is the first call to SHA3_Squeeze.
void SHA3_Squeeze(uint64_t A[SHA3_ROWS][SHA3_ROWS], uint8_t *out, size_t len, size_t r, int padded)
torben-hansen marked this conversation as resolved.
Show resolved Hide resolved
{
uint64_t *A_flat = (uint64_t *)A;
size_t i, w = r / 8;

assert(r < (25 * sizeof(A[0][0])) && (r % 8) == 0);

while (len != 0) {
if (padded) {
KeccakF1600(A);
}
padded = 1;
for (i = 0; i < w && len != 0; i++) {
uint64_t Ai = BitDeinterleave(A_flat[i]);

Expand All @@ -388,9 +393,6 @@ void SHA3_Squeeze(uint64_t A[SHA3_ROWS][SHA3_ROWS], uint8_t *out, size_t len, si
out += 8;
len -= 8;
}
if (len != 0) {
KeccakF1600(A);
}
}
}

Expand All @@ -405,10 +407,10 @@ size_t SHA3_Absorb(uint64_t A[SHA3_ROWS][SHA3_ROWS], const uint8_t *inp, size_t
}

size_t SHA3_Squeeze_hw(uint64_t A[SHA3_ROWS][SHA3_ROWS], const uint8_t *out, size_t len,
size_t r);
size_t r, int first);
jakemas marked this conversation as resolved.
Show resolved Hide resolved

void SHA3_Squeeze(uint64_t A[SHA3_ROWS][SHA3_ROWS], uint8_t *out, size_t len, size_t r) {
SHA3_Squeeze_hw(A, out, len, r);
void SHA3_Squeeze(uint64_t A[SHA3_ROWS][SHA3_ROWS], uint8_t *out, size_t len, size_t r, int first) {
SHA3_Squeeze_hw(A, out, len, r, first);
jakemas marked this conversation as resolved.
Show resolved Hide resolved
jakemas marked this conversation as resolved.
Show resolved Hide resolved
}

#endif // !KECCAK1600_ASM
26 changes: 15 additions & 11 deletions crypto/fipsmodule/sha/sha3.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include "internal.h"
#include <string.h>


uint8_t *SHA3_224(const uint8_t *data, size_t len,
uint8_t out[SHA3_224_DIGEST_LENGTH]) {
FIPS_service_indicator_lock_state();
Expand Down Expand Up @@ -126,6 +125,7 @@ int SHAKE_Final(uint8_t *md, KECCAK1600_CTX *ctx, size_t len) {
void SHA3_Reset(KECCAK1600_CTX *ctx) {
memset(ctx->A, 0, sizeof(ctx->A));
ctx->buf_load = 0;
ctx->padded = 0;
}

int SHA3_Init(KECCAK1600_CTX *ctx, uint8_t pad, size_t bit_len) {
Expand All @@ -144,7 +144,8 @@ int SHA3_Init(KECCAK1600_CTX *ctx, uint8_t pad, size_t bit_len) {
} else {
return 0;
}

ctx->padded = 0;

if (block_size <= sizeof(ctx->buf)) {
SHA3_Reset(ctx);
ctx->block_size = block_size;
Expand Down Expand Up @@ -209,18 +210,21 @@ int SHA3_Final(uint8_t *md, KECCAK1600_CTX *ctx) {
return 1;
}

// Pad the data with 10*1. Note that |num| can be |block_size - 1|
// in which case both byte operations below are performed on
// the same byte.
memset(ctx->buf + num, 0, block_size - num);
ctx->buf[num] = ctx->pad;
ctx->buf[block_size - 1] |= 0x80;
if (ctx->padded == 0) {
// Pad the data with 10*1. Note that |num| can be |block_size - 1|
// in which case both byte operations below are performed on
// the same byte.
memset(ctx->buf + num, 0, block_size - num);
ctx->buf[num] = ctx->pad;
ctx->buf[block_size - 1] |= 0x80;

if (SHA3_Absorb(ctx->A, ctx->buf, block_size, block_size) != 0) {
return 0;
if (SHA3_Absorb(ctx->A, ctx->buf, block_size, block_size) != 0) {
return 0;
}
}

SHA3_Squeeze(ctx->A, md, ctx->md_size, block_size);
SHA3_Squeeze(ctx->A, md, ctx->md_size, block_size, ctx->padded);
ctx->padded = 1;

FIPS_service_indicator_update_state();

Expand Down
32 changes: 31 additions & 1 deletion crypto/fipsmodule/sha/sha3_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,38 @@ TEST(SHA3Test, NISTTestVectors) {
const EVP_MD *algorithm = EVP_sha3_512();
test_vec.NISTTestVectors(algorithm);
});

FileTestGTest("crypto/fipsmodule/sha/testvectors/SHA3_224LongMsg.txt",
[](FileTest *t) {
SHA3TestVector test_vec;
EXPECT_TRUE(test_vec.ReadFromFileTest(t));
const EVP_MD *algorithm = EVP_sha3_224();
test_vec.NISTTestVectors(algorithm);
});
FileTestGTest("crypto/fipsmodule/sha/testvectors/SHA3_256LongMsg.txt",
[](FileTest *t) {
SHA3TestVector test_vec;
EXPECT_TRUE(test_vec.ReadFromFileTest(t));
const EVP_MD *algorithm = EVP_sha3_256();
test_vec.NISTTestVectors(algorithm);
});
FileTestGTest("crypto/fipsmodule/sha/testvectors/SHA3_384LongMsg.txt",
[](FileTest *t) {
SHA3TestVector test_vec;
EXPECT_TRUE(test_vec.ReadFromFileTest(t));
const EVP_MD *algorithm = EVP_sha3_384();
test_vec.NISTTestVectors(algorithm);
});
FileTestGTest("crypto/fipsmodule/sha/testvectors/SHA3_512LongMsg.txt",
[](FileTest *t) {
SHA3TestVector test_vec;
EXPECT_TRUE(test_vec.ReadFromFileTest(t));
const EVP_MD *algorithm = EVP_sha3_512();
test_vec.NISTTestVectors(algorithm);
});
}


TEST(SHA3Test, NISTTestVectors_SingleShot) {
FileTestGTest("crypto/fipsmodule/sha/testvectors/SHA3_224ShortMsg.txt",
[](FileTest *t) {
Expand Down Expand Up @@ -181,7 +211,7 @@ TEST(KeccakInternalTest, SqueezeOutputBufferOverflow) {
EXPECT_TRUE(SHA3_Init(&ctx, SHA3_PAD_CHAR, SHA3_384_DIGEST_BITLENGTH));
out.resize(out_len + canary.size());
std::copy(canary.begin(), canary.end(), out.end() - canary.size());
SHA3_Squeeze(ctx.A, out.data(), out_len, ctx.block_size);
SHA3_Squeeze(ctx.A, out.data(), out_len, ctx.block_size, 1);
torben-hansen marked this conversation as resolved.
Show resolved Hide resolved
EXPECT_TRUE(std::equal(out.end() - canary.size(), out.end(),
canary.begin()) == true);
}
Expand Down
Loading
Loading