sha3
Embeddable C11 SHA-3 implementation.
Data Structures | Functions
SHAKE

SHA-3 Extendable-output functions (XOFs) with arbitrary length output, as defined in section 6.2 of FIPS 202. More...

Data Structures

struct  sha3_xof_t
 Iterative XOF context (all members are private). More...
 

Functions

void shake128_init (sha3_xof_t *const xof)
 Initialize SHAKE128 extendable-output function (XOF) context. More...
 
_Bool shake128_absorb (sha3_xof_t *xof, const uint8_t *msg, const size_t len)
 Absorb data into SHAKE128 XOF context. More...
 
void shake128_squeeze (sha3_xof_t *xof, uint8_t *dst, const size_t len)
 Squeeze bytes from SHAKE128 XOF context. More...
 
void shake128 (const uint8_t *src, const size_t src_len, uint8_t *dst, const size_t dst_len)
 Absorb data into SHAKE128 XOF, then squeeze bytes out. More...
 
void shake256_init (sha3_xof_t *xof)
 Initialize SHAKE256 extendable-output function (XOF) context. More...
 
_Bool shake256_absorb (sha3_xof_t *xof, const uint8_t *msg, const size_t len)
 Absorb data into SHAKE256 XOF context. More...
 
void shake256_squeeze (sha3_xof_t *xof, uint8_t *dst, const size_t len)
 Squeeze bytes from SHAKE256 XOF context. More...
 
void shake256 (const uint8_t *src, const size_t src_len, uint8_t *dst, const size_t dst_len)
 Absorb data into SHAKE256 XOF, then squeeze bytes out. More...
 

Detailed Description

SHA-3 Extendable-output functions (XOFs) with arbitrary length output, as defined in section 6.2 of FIPS 202.

Function Documentation

◆ shake128()

void shake128 ( const uint8_t *  src,
const size_t  src_len,
uint8_t *  dst,
const size_t  dst_len 
)

Absorb data into SHAKE128 XOF, then squeeze bytes out.

Absorb data in buffer src of length src_len bytes into SHAKE128 XOF context, then squeeze dst_len bytes of output into destination buffer dst.

Parameters
[in]srcSource buffer.
[in]src_lenSource buffer length, in bytes.
[out]dstDestination buffer.
[in]dst_lenDestination buffer length, in bytes.

Example:

// get 1024 random bytes
uint8_t buf[1024] = { 0 };
rand_bytes(buf, sizeof(buf));
// absorb into shake128 context with 15 byte output, write result to `out`
uint8_t out[15] = { 0 };
shake128(buf, sizeof(buf), out, sizeof(out));
void shake128(const uint8_t *src, const size_t src_len, uint8_t *dst, const size_t dst_len)
Absorb data into SHAKE128 XOF, then squeeze bytes out.

◆ shake128_absorb()

_Bool shake128_absorb ( sha3_xof_t xof,
const uint8_t *  msg,
const size_t  len 
)

Absorb data into SHAKE128 XOF context.

Absorb input data in msg of length len bytes into SHAKE128 XOF context xof. Can be called iteratively to absorb input data in chunks.

Parameters
[in,out]xofSHAKE128 XOF context.
[in]msgInput data.
[in]lenInput data length, in bytes.
Returns
True if data was absorbed, and false otherwise (e.g., if context has already been squeezed).

Example:

// get 1024 random bytes
uint8_t buf[1024] = { 0 };
rand_bytes(buf, sizeof(buf));
// create shake128 context
sha3_xof_t ctx = { 0 };
// absorb `buf` contents in 32 byte chunks
for (size_t i = 0; i < sizeof(buf); i += 32) {
shake128_absorb(&ctx, buf + i, 32);
}
// print result prefix to stdout
printf("%s: ", __func__);
// squeeze first 128 bytes of of result in 32 byte chunks
for (size_t i = 0; i < 128; i += 32) {
// squeeze 32 bytes of result into `out`
uint8_t out[32] = { 0 };
shake128_squeeze(&ctx, out, sizeof(out));
// encode as hex, print to stdout
hex_write(stdout, out, sizeof(out));
}
// print result suffix to stdout
fputs("\n", stdout);
void shake128_init(sha3_xof_t *const xof)
Initialize SHAKE128 extendable-output function (XOF) context.
_Bool shake128_absorb(sha3_xof_t *xof, const uint8_t *msg, const size_t len)
Absorb data into SHAKE128 XOF context.
void shake128_squeeze(sha3_xof_t *xof, uint8_t *dst, const size_t len)
Squeeze bytes from SHAKE128 XOF context.
Iterative XOF context (all members are private).
Definition: sha3.h:346

◆ shake128_init()

void shake128_init ( sha3_xof_t *const  xof)

Initialize SHAKE128 extendable-output function (XOF) context.

Parameters
[out]xofSHAKE128 XOF context.

Example:

// get 1024 random bytes
uint8_t buf[1024] = { 0 };
rand_bytes(buf, sizeof(buf));
// create shake128 context
sha3_xof_t ctx = { 0 };
// absorb `buf` contents in 32 byte chunks
for (size_t i = 0; i < sizeof(buf); i += 32) {
shake128_absorb(&ctx, buf + i, 32);
}
// print result prefix to stdout
printf("%s: ", __func__);
// squeeze first 128 bytes of of result in 32 byte chunks
for (size_t i = 0; i < 128; i += 32) {
// squeeze 32 bytes of result into `out`
uint8_t out[32] = { 0 };
shake128_squeeze(&ctx, out, sizeof(out));
// encode as hex, print to stdout
hex_write(stdout, out, sizeof(out));
}
// print result suffix to stdout
fputs("\n", stdout);

◆ shake128_squeeze()

void shake128_squeeze ( sha3_xof_t xof,
uint8_t *  dst,
const size_t  len 
)

Squeeze bytes from SHAKE128 XOF context.

Squeeze len bytes of output into destination buffer dst from SHAKE128 XOF context xof. Can be called iteratively to squeeze output data in chunks.

Parameters
[in,out]xofSHAKE128 XOF context.
[out]dstDestination buffer.
[in]lenDestination buffer length, in bytes.

Example:

// get 1024 random bytes
uint8_t buf[1024] = { 0 };
rand_bytes(buf, sizeof(buf));
// create shake128 context
sha3_xof_t ctx = { 0 };
// absorb `buf` contents in 32 byte chunks
for (size_t i = 0; i < sizeof(buf); i += 32) {
shake128_absorb(&ctx, buf + i, 32);
}
// print result prefix to stdout
printf("%s: ", __func__);
// squeeze first 128 bytes of of result in 32 byte chunks
for (size_t i = 0; i < 128; i += 32) {
// squeeze 32 bytes of result into `out`
uint8_t out[32] = { 0 };
shake128_squeeze(&ctx, out, sizeof(out));
// encode as hex, print to stdout
hex_write(stdout, out, sizeof(out));
}
// print result suffix to stdout
fputs("\n", stdout);

◆ shake256()

void shake256 ( const uint8_t *  src,
const size_t  src_len,
uint8_t *  dst,
const size_t  dst_len 
)

Absorb data into SHAKE256 XOF, then squeeze bytes out.

Absorb data in buffer src of length src_len bytes into SHAKE256 XOF context, then squeeze dst_len bytes of output into destination buffer dst.

Parameters
[in]srcSource buffer.
[in]src_lenSource buffer length, in bytes.
[out]dstDestination buffer.
[in]dst_lenDestination buffer length, in bytes.

Example:

// get 1024 random bytes
uint8_t buf[1024] = { 0 };
rand_bytes(buf, sizeof(buf));
// absorb `buf` into shake256 context with 15 byte output, write result to `out`
uint8_t out[15] = { 0 };
shake256(buf, sizeof(buf), out, sizeof(out));
void shake256(const uint8_t *src, const size_t src_len, uint8_t *dst, const size_t dst_len)
Absorb data into SHAKE256 XOF, then squeeze bytes out.

◆ shake256_absorb()

_Bool shake256_absorb ( sha3_xof_t xof,
const uint8_t *  msg,
const size_t  len 
)

Absorb data into SHAKE256 XOF context.

Absorb input data in msg of length len bytes into SHAKE256 XOF context xof. Can be called iteratively to absorb input data in chunks.

Parameters
[in,out]xofSHAKE256 XOF context.
[in]msgInput data.
[in]lenInput data length, in bytes.
Returns
True if data was absorbed, and false otherwise (e.g., if context has already been squeezed).

Example:

// get 1024 random bytes
uint8_t buf[1024] = { 0 };
rand_bytes(buf, sizeof(buf));
// create shake256 context
sha3_xof_t ctx = { 0 };
// absorb `buf` contents in 32 byte chunks
for (size_t i = 0; i < sizeof(buf); i += 32) {
shake256_absorb(&ctx, buf + i, 32);
}
// print result prefix to stdout
printf("%s: ", __func__);
// squeeze first 128 bytes of of result in 32 byte chunks
for (size_t i = 0; i < 128; i += 32) {
// squeeze 32 bytes of result into `out`
uint8_t out[32] = { 0 };
shake256_squeeze(&ctx, out, sizeof(out));
// encode as hex, print to stdout
hex_write(stdout, out, sizeof(out));
}
// print result suffix to stdout
fputs("\n", stdout);
_Bool shake256_absorb(sha3_xof_t *xof, const uint8_t *msg, const size_t len)
Absorb data into SHAKE256 XOF context.
void shake256_squeeze(sha3_xof_t *xof, uint8_t *dst, const size_t len)
Squeeze bytes from SHAKE256 XOF context.
void shake256_init(sha3_xof_t *xof)
Initialize SHAKE256 extendable-output function (XOF) context.

◆ shake256_init()

void shake256_init ( sha3_xof_t xof)

Initialize SHAKE256 extendable-output function (XOF) context.

Parameters
[out]xofSHAKE256 XOF context.

Example:

// get 1024 random bytes
uint8_t buf[1024] = { 0 };
rand_bytes(buf, sizeof(buf));
// create shake256 context
sha3_xof_t ctx = { 0 };
// absorb `buf` contents in 32 byte chunks
for (size_t i = 0; i < sizeof(buf); i += 32) {
shake256_absorb(&ctx, buf + i, 32);
}
// print result prefix to stdout
printf("%s: ", __func__);
// squeeze first 128 bytes of of result in 32 byte chunks
for (size_t i = 0; i < 128; i += 32) {
// squeeze 32 bytes of result into `out`
uint8_t out[32] = { 0 };
shake256_squeeze(&ctx, out, sizeof(out));
// encode as hex, print to stdout
hex_write(stdout, out, sizeof(out));
}
// print result suffix to stdout
fputs("\n", stdout);

◆ shake256_squeeze()

void shake256_squeeze ( sha3_xof_t xof,
uint8_t *  dst,
const size_t  len 
)

Squeeze bytes from SHAKE256 XOF context.

Squeeze len bytes of output into destination buffer dst from SHAKE256 XOF context xof. Can be called iteratively to squeeze output data in chunks.

Parameters
[in,out]xofSHAKE256 XOF context.
[out]dstDestination buffer.
[in]lenDestination buffer length, in bytes.

Example:

// get 1024 random bytes
uint8_t buf[1024] = { 0 };
rand_bytes(buf, sizeof(buf));
// create shake256 context
sha3_xof_t ctx = { 0 };
// absorb `buf` contents in 32 byte chunks
for (size_t i = 0; i < sizeof(buf); i += 32) {
shake256_absorb(&ctx, buf + i, 32);
}
// print result prefix to stdout
printf("%s: ", __func__);
// squeeze first 128 bytes of of result in 32 byte chunks
for (size_t i = 0; i < 128; i += 32) {
// squeeze 32 bytes of result into `out`
uint8_t out[32] = { 0 };
shake256_squeeze(&ctx, out, sizeof(out));
// encode as hex, print to stdout
hex_write(stdout, out, sizeof(out));
}
// print result suffix to stdout
fputs("\n", stdout);