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

Hash function and XOF, as defined in section 6 of SP 800-185. More...

Data Structures

struct  parallelhash_params_t
 ParallelHash configuration parameters. More...
 
struct  parallelhash_t
 ParallelHash context (all members are private). More...
 

Functions

void parallelhash128 (const parallelhash_params_t params, const uint8_t *src, const size_t src_len, uint8_t *dst, const size_t dst_len)
 Absorb data into ParallelHash128, then squeeze bytes out. More...
 
void parallelhash256 (const parallelhash_params_t params, const uint8_t *src, const size_t src_len, uint8_t *dst, const size_t dst_len)
 Absorb data into ParallelHash256, then squeeze bytes out. More...
 
void parallelhash128_xof_init (parallelhash_t *xof, const parallelhash_params_t params)
 Initialize a ParallelHash128 XOF context. More...
 
void parallelhash128_xof_absorb (parallelhash_t *hash, const uint8_t *src, const size_t len)
 Absorb data into a ParallelHash128 XOF context. More...
 
void parallelhash128_xof_squeeze (parallelhash_t *xof, uint8_t *dst, const size_t len)
 Squeeze bytes from a ParallelHash128 XOF context. More...
 
void parallelhash128_xof_once (const parallelhash_params_t params, const uint8_t *src, const size_t src_len, uint8_t *dst, const size_t dst_len)
 Absorb bytes into ParallelHash128 XOF, then squeeze bytes out. More...
 
void parallelhash256_xof_init (parallelhash_t *xof, const parallelhash_params_t params)
 Initialize a ParallelHash256 XOF context. More...
 
void parallelhash256_xof_absorb (parallelhash_t *xof, const uint8_t *src, const size_t len)
 Absorb data into a ParallelHash256 XOF context. More...
 
void parallelhash256_xof_squeeze (parallelhash_t *xof, uint8_t *dst, const size_t len)
 Squeeze bytes from a ParallelHash256 XOF context. More...
 
void parallelhash256_xof_once (const parallelhash_params_t params, const uint8_t *src, const size_t src_len, uint8_t *dst, const size_t dst_len)
 Absorb bytes into ParallelHash256 XOF, then squeeze bytes out. More...
 

Detailed Description

Hash function and XOF, as defined in section 6 of SP 800-185.

Function Documentation

◆ parallelhash128()

void parallelhash128 ( const parallelhash_params_t  params,
const uint8_t *  src,
const size_t  src_len,
uint8_t *  dst,
const size_t  dst_len 
)

Absorb data into ParallelHash128, then squeeze bytes out.

Initialize internal ParallelHash128 (NIST SP 800-185, section 6) context with configuration parameters params, then squeeze dst_len bytes of output from internal context into destination buffer dst.

Note
This ParallelHash implementation is sequential, not parallel.
Parameters
[in]paramsParallelHash configuration parameters.
[in]srcSource buffer.
[in]src_lenSource buffer length, in bytes.
[out]dstDestination buffer.
[in]dst_lenDestination buffer length, in bytes.

Example:

const size_t block_len = 200; // block size, in bytes
const uint8_t custom[] = "hello"; // customization string
// parallelhash parameters
const parallelhash_params_t params = {
.block_len = block_len,
.custom = custom, // customization string
.custom_len = sizeof(custom) - 1, // customization string length, in bytes (w/o trailing NUL)
};
// get 1024 random bytes
uint8_t buf[1024] = { 0 };
rand_bytes(buf, sizeof(buf));
// absorb into ParallelHash128 with fixed-length output, write 32 bytes to `out`
uint8_t out[32] = { 0 };
parallelhash128(params, buf, sizeof(buf), out, sizeof(out));
void parallelhash128(const parallelhash_params_t params, const uint8_t *src, const size_t src_len, uint8_t *dst, const size_t dst_len)
Absorb data into ParallelHash128, then squeeze bytes out.
ParallelHash configuration parameters.
Definition: sha3.h:1677
const size_t block_len
Definition: sha3.h:1678

◆ parallelhash128_xof_absorb()

void parallelhash128_xof_absorb ( parallelhash_t hash,
const uint8_t *  src,
const size_t  len 
)

Absorb data into a ParallelHash128 XOF context.

Absorb data in buffer src of length len bytes into ParallelHash128 XOF context. Can be called iteratively to absorb input data in chunks.

Note
This ParallelHash implementation is sequential, not parallel.
Parameters
[in,out]hashParallelHash128 XOF context.
[in]srcSource buffer.
[in]lenSource buffer length, in bytes.

Example:

const size_t block_len = 200; // block size, in bytes
const uint8_t custom[] = "hello"; // customization string
// parallelhash parameters
const parallelhash_params_t params = {
.block_len = block_len,
.custom = custom, // customization string
.custom_len = sizeof(custom) - 1, // customization string length, in bytes (w/o trailing NUL)
};
// get 1024 random bytes
uint8_t buf[1024] = { 0 };
rand_bytes(buf, sizeof(buf));
// create ParallelHash128 XOF context from parameters
parallelhash_t ctx = { 0 };
parallelhash128_xof_init(&ctx, params);
// absorb `buf` in 32 byte chunks
for (size_t i = 0; i < sizeof(buf); i += 32) {
parallelhash128_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 };
parallelhash128_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 parallelhash128_xof_squeeze(parallelhash_t *xof, uint8_t *dst, const size_t len)
Squeeze bytes from a ParallelHash128 XOF context.
void parallelhash128_xof_absorb(parallelhash_t *hash, const uint8_t *src, const size_t len)
Absorb data into a ParallelHash128 XOF context.
void parallelhash128_xof_init(parallelhash_t *xof, const parallelhash_params_t params)
Initialize a ParallelHash128 XOF context.
ParallelHash context (all members are private).
Definition: sha3.h:1687

◆ parallelhash128_xof_init()

void parallelhash128_xof_init ( parallelhash_t xof,
const parallelhash_params_t  params 
)

Initialize a ParallelHash128 XOF context.

Initialize ParallelHash128 XOF (ParallelHash eXtendable Output Function, as defined in section 6.3.1 of NIST SP 800-185) context with configuration parameters params.

Note
ParallelHash128 and ParallelHash128 XOF produce different output, because ParallelHash128 encodes the fixed output size as part of the input while ParallelHash128 XOF does not. See section 6.3.1 of NIST SP 800-185 for details.
This ParallelHash implementation is sequential, not parallel.
Parameters
[out]xofParallelHash128 XOF context.
[in]paramsParallelHash configuration parameters.

Example:

const size_t block_len = 200; // block size, in bytes
const uint8_t custom[] = "hello"; // customization string
// parallelhash parameters
const parallelhash_params_t params = {
.block_len = block_len,
.custom = custom, // customization string
.custom_len = sizeof(custom) - 1, // customization string length, in bytes (w/o trailing NUL)
};
// get 1024 random bytes
uint8_t buf[1024] = { 0 };
rand_bytes(buf, sizeof(buf));
// create ParallelHash128 XOF context from parameters
parallelhash_t ctx = { 0 };
parallelhash128_xof_init(&ctx, params);
// absorb `buf` in 32 byte chunks
for (size_t i = 0; i < sizeof(buf); i += 32) {
parallelhash128_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 };
parallelhash128_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);

◆ parallelhash128_xof_once()

void parallelhash128_xof_once ( const parallelhash_params_t  params,
const uint8_t *  src,
const size_t  src_len,
uint8_t *  dst,
const size_t  dst_len 
)

Absorb bytes into ParallelHash128 XOF, then squeeze bytes out.

Initialize internal ParallelHash128 XOF (ParallelHash eXtendable Output Function, as defined in section 6.3.1 of NIST SP 800-185) context with configuration parameters params, absorb data in buffer src of length src_len bytes into context, then squeeze dst_len bytes of output into destination buffer dst.

Note
ParallelHash128 and ParallelHash128 XOF produce different output, because ParallelHash128 encodes the fixed output size as part of the input while ParallelHash128 XOF does not. See section 6.3.1 of NIST SP 800-185 for details.
This ParallelHash implementation is sequential, not parallel.
Parameters
[in]paramsParallelHash configuration parameters.
[in]srcSource buffer.
[in]src_lenSource buffer length, in bytes.
[out]dstDestination buffer.
[in]dst_lenDestination buffer length, in bytes.

Example:

const size_t block_len = 200; // block size, in bytes
const uint8_t custom[] = "hello"; // customization string
// parallelhash parameters
const parallelhash_params_t params = {
.block_len = block_len,
.custom = custom, // customization string
.custom_len = sizeof(custom) - 1, // customization string length, in bytes (w/o trailing NUL)
};
// get 1024 random bytes
uint8_t buf[1024] = { 0 };
rand_bytes(buf, sizeof(buf));
// absorb into ParallelHash128 XOF, squeeze into `out`
uint8_t out[128] = { 0 }; // output buffer
parallelhash128_xof_once(params, buf, sizeof(buf), out, sizeof(out));
void parallelhash128_xof_once(const parallelhash_params_t params, const uint8_t *src, const size_t src_len, uint8_t *dst, const size_t dst_len)
Absorb bytes into ParallelHash128 XOF, then squeeze bytes out.

◆ parallelhash128_xof_squeeze()

void parallelhash128_xof_squeeze ( parallelhash_t xof,
uint8_t *  dst,
const size_t  len 
)

Squeeze bytes from a ParallelHash128 XOF context.

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

Note
ParallelHash128 and ParallelHash128 XOF produce different output, because ParallelHash128 encodes the fixed output size as part of the input while ParallelHash128 XOF does not. See section 6.3.1 of NIST SP 800-185 for details.
This ParallelHash implementation is sequential, not parallel.
Parameters
[in,out]xofParallelHash128 XOF context.
[out]dstDestination buffer.
[in]lenDestination buffer length, in bytes.

Example:

const size_t block_len = 200; // block size, in bytes
const uint8_t custom[] = "hello"; // customization string
// parallelhash parameters
const parallelhash_params_t params = {
.block_len = block_len,
.custom = custom, // customization string
.custom_len = sizeof(custom) - 1, // customization string length, in bytes (w/o trailing NUL)
};
// get 1024 random bytes
uint8_t buf[1024] = { 0 };
rand_bytes(buf, sizeof(buf));
// create ParallelHash128 XOF context from parameters
parallelhash_t ctx = { 0 };
parallelhash128_xof_init(&ctx, params);
// absorb `buf` in 32 byte chunks
for (size_t i = 0; i < sizeof(buf); i += 32) {
parallelhash128_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 };
parallelhash128_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);

◆ parallelhash256()

void parallelhash256 ( const parallelhash_params_t  params,
const uint8_t *  src,
const size_t  src_len,
uint8_t *  dst,
const size_t  dst_len 
)

Absorb data into ParallelHash256, then squeeze bytes out.

Initialize internal ParallelHash256 (NIST SP 800-185, section 6) context with configuration parameters params, then squeeze dst_len bytes of output from internal context into destination buffer dst.

Note
This ParallelHash implementation is sequential, not parallel.
Parameters
[in]paramsParallelHash configuration parameters.
[in]srcSource buffer.
[in]src_lenSource buffer length, in bytes.
[out]dstDestination buffer.
[in]dst_lenDestination buffer length, in bytes.

Example:

const size_t block_len = 200; // block size, in bytes
const uint8_t custom[] = "hello"; // customization string
// parallelhash parameters
const parallelhash_params_t params = {
.block_len = block_len,
.custom = custom, // customization string
.custom_len = sizeof(custom) - 1, // customization string length, in bytes (w/o trailing NUL)
};
// get 1024 random bytes
uint8_t buf[1024] = { 0 };
rand_bytes(buf, sizeof(buf));
// absorb into ParallelHash256 with fixed-length output, write 32 bytes to `out`
uint8_t out[32] = { 0 };
parallelhash256(params, buf, sizeof(buf), out, sizeof(out));
void parallelhash256(const parallelhash_params_t params, const uint8_t *src, const size_t src_len, uint8_t *dst, const size_t dst_len)
Absorb data into ParallelHash256, then squeeze bytes out.

◆ parallelhash256_xof_absorb()

void parallelhash256_xof_absorb ( parallelhash_t xof,
const uint8_t *  src,
const size_t  len 
)

Absorb data into a ParallelHash256 XOF context.

Absorb data in buffer src of length len bytes into ParallelHash256 XOF context. Can be called iteratively to absorb input data in chunks.

Note
This ParallelHash implementation is sequential, not parallel.
Parameters
[in,out]xofParallelHash256 XOF context.
[in]srcSource buffer.
[in]lenSource buffer length, in bytes.

Example:

const size_t block_len = 200; // block size, in bytes
const uint8_t custom[] = "hello"; // customization string
// parallelhash parameters
const parallelhash_params_t params = {
.block_len = block_len,
.custom = custom, // customization string
.custom_len = sizeof(custom) - 1, // customization string length, in bytes (w/o trailing NUL)
};
// get 1024 random bytes
uint8_t buf[1024] = { 0 };
rand_bytes(buf, sizeof(buf));
// create ParallelHash256 XOF context from parameters
parallelhash_t ctx = { 0 };
parallelhash256_xof_init(&ctx, params);
// absorb `buf` in 32 byte chunks
for (size_t i = 0; i < sizeof(buf); i += 32) {
parallelhash256_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 };
parallelhash256_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 parallelhash256_xof_absorb(parallelhash_t *xof, const uint8_t *src, const size_t len)
Absorb data into a ParallelHash256 XOF context.
void parallelhash256_xof_init(parallelhash_t *xof, const parallelhash_params_t params)
Initialize a ParallelHash256 XOF context.
void parallelhash256_xof_squeeze(parallelhash_t *xof, uint8_t *dst, const size_t len)
Squeeze bytes from a ParallelHash256 XOF context.

◆ parallelhash256_xof_init()

void parallelhash256_xof_init ( parallelhash_t xof,
const parallelhash_params_t  params 
)

Initialize a ParallelHash256 XOF context.

Initialize ParallelHash256 XOF (ParallelHash eXtendable Output Function, as defined in section 6.3.1 of NIST SP 800-185) context with configuration parameters params.

Note
ParallelHash256 and ParallelHash256 XOF produce different output, because ParallelHash256 encodes the fixed output size as part of the input while ParallelHash256 XOF does not. See section 6.3.1 of NIST SP 800-185 for details.

Note: This ParallelHash implementation is sequential, not parallel.

Parameters
[out]xofParallelHash256 XOF context.
[in]paramsParallelHash configuration parameters.

Example:

const size_t block_len = 200; // block size, in bytes
const uint8_t custom[] = "hello"; // customization string
// parallelhash parameters
const parallelhash_params_t params = {
.block_len = block_len,
.custom = custom, // customization string
.custom_len = sizeof(custom) - 1, // customization string length, in bytes (w/o trailing NUL)
};
// get 1024 random bytes
uint8_t buf[1024] = { 0 };
rand_bytes(buf, sizeof(buf));
// create ParallelHash256 XOF context from parameters
parallelhash_t ctx = { 0 };
parallelhash256_xof_init(&ctx, params);
// absorb `buf` in 32 byte chunks
for (size_t i = 0; i < sizeof(buf); i += 32) {
parallelhash256_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 };
parallelhash256_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);

◆ parallelhash256_xof_once()

void parallelhash256_xof_once ( const parallelhash_params_t  params,
const uint8_t *  src,
const size_t  src_len,
uint8_t *  dst,
const size_t  dst_len 
)

Absorb bytes into ParallelHash256 XOF, then squeeze bytes out.

Initialize internal ParallelHash256 XOF (ParallelHash eXtendable Output Function, as defined in section 6.3.1 of NIST SP 800-185) context with configuration parameters params, absorb src_len bytes if input from source buffer src, then squeeze dst_len bytes of output into destination buffer dst.

Note
ParallelHash256 and ParallelHash256 XOF produce different output, because ParallelHash256 encodes the fixed output size as part of the input while ParallelHash256 XOF does not. See section 6.3.1 of NIST SP 800-185 for details.

Note: This ParallelHash implementation is sequential, not parallel.

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

Example:

const size_t block_len = 200; // block size, in bytes
const uint8_t custom[] = "hello"; // customization string
// parallelhash parameters
const parallelhash_params_t params = {
.block_len = block_len,
.custom = custom, // customization string
.custom_len = sizeof(custom) - 1, // customization string length, in bytes (w/o trailing NUL)
};
// get 1024 random bytes
uint8_t buf[1024] = { 0 };
rand_bytes(buf, sizeof(buf));
// absorb into ParallelHash256 XOF, squeeze into `out`
uint8_t out[128] = { 0 }; // output buffer
parallelhash256_xof_once(params, buf, sizeof(buf), out, sizeof(out));
void parallelhash256_xof_once(const parallelhash_params_t params, const uint8_t *src, const size_t src_len, uint8_t *dst, const size_t dst_len)
Absorb bytes into ParallelHash256 XOF, then squeeze bytes out.

◆ parallelhash256_xof_squeeze()

void parallelhash256_xof_squeeze ( parallelhash_t xof,
uint8_t *  dst,
const size_t  len 
)

Squeeze bytes from a ParallelHash256 XOF context.

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

Note
ParallelHash256 and ParallelHash256 XOF produce different output, because ParallelHash256 encodes the fixed output size as part of the input while ParallelHash256 XOF does not. See section 6.3.1 of NIST SP 800-185 for details.

Note: This ParallelHash implementation is sequential, not parallel.

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

Example:

const size_t block_len = 200; // block size, in bytes
const uint8_t custom[] = "hello"; // customization string
// parallelhash parameters
const parallelhash_params_t params = {
.block_len = block_len,
.custom = custom, // customization string
.custom_len = sizeof(custom) - 1, // customization string length, in bytes (w/o trailing NUL)
};
// get 1024 random bytes
uint8_t buf[1024] = { 0 };
rand_bytes(buf, sizeof(buf));
// create ParallelHash256 XOF context from parameters
parallelhash_t ctx = { 0 };
parallelhash256_xof_init(&ctx, params);
// absorb `buf` in 32 byte chunks
for (size_t i = 0; i < sizeof(buf); i += 32) {
parallelhash256_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 };
parallelhash256_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);