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

eXtendable Output Functions (XOF) with both fixed-length and 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 (const uint8_t *msg, size_t len, uint8_t dst[static 16])
 Hash data with SHAKE128. More...
 
void shake256 (const uint8_t *msg, size_t len, uint8_t dst[static 32])
 Hash data with SHAKE256. More...
 
void shake128_xof_init (sha3_xof_t *const xof)
 Initialize SHAKE128 extendable-output function (XOF) context. More...
 
_Bool shake128_xof_absorb (sha3_xof_t *xof, const uint8_t *msg, const size_t len)
 Absorb data into SHAKE128 XOF context. More...
 
void shake128_xof_squeeze (sha3_xof_t *xof, uint8_t *dst, const size_t len)
 Squeeze bytes from SHAKE128 XOF context. More...
 
void shake128_xof_once (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_xof_init (sha3_xof_t *xof)
 Initialize SHAKE256 extendable-output function (XOF) context. More...
 
_Bool shake256_xof_absorb (sha3_xof_t *xof, const uint8_t *msg, const size_t len)
 Absorb data into SHAKE256 XOF context. More...
 
void shake256_xof_squeeze (sha3_xof_t *xof, uint8_t *dst, const size_t len)
 Squeeze bytes from SHAKE256 XOF context. More...
 
void shake256_xof_once (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

eXtendable Output Functions (XOF) with both fixed-length and arbitrary length output, as defined in section 6.2 of FIPS 202.

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

Function Documentation

◆ shake128()

void shake128 ( const uint8_t *  msg,
size_t  len,
uint8_t  dst[static 16] 
)

Hash data with SHAKE128.

Hash input message in buffer msg of length len bytes with SHAKE128 (FIPS 202, section 6.2) and write 16 bytes of output to destination buffer dst.

Parameters
[in]msgInput message.
[in]lenInput message length, in bytes.
[out]dstDestination buffer. Must be at least 16 bytes in length.

Example:

// get 1024 random bytes
uint8_t buf[1024] = { 0 };
rand_bytes(buf, sizeof(buf));
// absorb into shake128, write 16 byte result to `out`
uint8_t hash[16] = { 0 };
shake128(buf, sizeof(buf), hash);
void shake128(const uint8_t *msg, size_t len, uint8_t dst[static 16])
Hash data with SHAKE128.

◆ shake128_xof_absorb()

_Bool shake128_xof_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 xof context with arbitrary length output
sha3_xof_t ctx = { 0 };
// absorb `buf` contents in 32 byte chunks
for (size_t i = 0; i < sizeof(buf); i += 32) {
shake128_xof_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_xof_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_xof_squeeze(sha3_xof_t *xof, uint8_t *dst, const size_t len)
Squeeze bytes from SHAKE128 XOF context.
void shake128_xof_init(sha3_xof_t *const xof)
Initialize SHAKE128 extendable-output function (XOF) context.
_Bool shake128_xof_absorb(sha3_xof_t *xof, const uint8_t *msg, const size_t len)
Absorb data into SHAKE128 XOF context.
Iterative XOF context (all members are private).
Definition: sha3.h:93

◆ shake128_xof_init()

void shake128_xof_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 xof context with arbitrary length output
sha3_xof_t ctx = { 0 };
// absorb `buf` contents in 32 byte chunks
for (size_t i = 0; i < sizeof(buf); i += 32) {
shake128_xof_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_xof_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_xof_once()

void shake128_xof_once ( 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.

Note
This function will produce different output than shake128(), because shake128() produces fixed-length output and this function produces arbitrary-length output.
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 xof context with 15 byte output, write result to `out`
uint8_t out[15] = { 0 };
shake128_xof_once(buf, sizeof(buf), out, sizeof(out));
void shake128_xof_once(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_xof_squeeze()

void shake128_xof_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 xof context with arbitrary length output
sha3_xof_t ctx = { 0 };
// absorb `buf` contents in 32 byte chunks
for (size_t i = 0; i < sizeof(buf); i += 32) {
shake128_xof_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_xof_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 *  msg,
size_t  len,
uint8_t  dst[static 32] 
)

Hash data with SHAKE256.

Hash input message in buffer msg of length len bytes with SHAKE256 (FIPS 202, section 6.2) and write 32 bytes of output to destination buffer dst.

Parameters
[in]msgInput message.
[in]lenInput message length, in bytes.
[out]dstDestination buffer. Must be at least 32 bytes in length.

Example:

// get 1024 random bytes
uint8_t buf[1024] = { 0 };
rand_bytes(buf, sizeof(buf));
// absorb into shake256, write 32 byte result to `out`
uint8_t hash[32] = { 0 };
shake256(buf, sizeof(buf), hash);
void shake256(const uint8_t *msg, size_t len, uint8_t dst[static 32])
Hash data with SHAKE256.

◆ shake256_xof_absorb()

_Bool shake256_xof_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 xof context with arbitrary length output
sha3_xof_t ctx = { 0 };
// absorb `buf` contents in 32 byte chunks
for (size_t i = 0; i < sizeof(buf); i += 32) {
shake256_xof_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_xof_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_xof_absorb(sha3_xof_t *xof, const uint8_t *msg, const size_t len)
Absorb data into SHAKE256 XOF context.
void shake256_xof_init(sha3_xof_t *xof)
Initialize SHAKE256 extendable-output function (XOF) context.
void shake256_xof_squeeze(sha3_xof_t *xof, uint8_t *dst, const size_t len)
Squeeze bytes from SHAKE256 XOF context.

◆ shake256_xof_init()

void shake256_xof_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 xof context with arbitrary length output
sha3_xof_t ctx = { 0 };
// absorb `buf` contents in 32 byte chunks
for (size_t i = 0; i < sizeof(buf); i += 32) {
shake256_xof_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_xof_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_xof_once()

void shake256_xof_once ( 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.

Note
This function will produce different output than shake256(), because shake256() produces fixed-length output and this function produces arbitrary-length output.
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 xof with 15 byte output, write result to `out`
uint8_t out[15] = { 0 };
shake256_xof_once(buf, sizeof(buf), out, sizeof(out));
void shake256_xof_once(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_xof_squeeze()

void shake256_xof_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 xof context with arbitrary length output
sha3_xof_t ctx = { 0 };
// absorb `buf` contents in 32 byte chunks
for (size_t i = 0; i < sizeof(buf); i += 32) {
shake256_xof_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_xof_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);