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

Cryptographic hash functions with fixed-length output, as defined in section 6.1 of FIPS 202. More...

Data Structures

union  sha3_state_t
 Internal SHA-3 state (all members are private). More...
 
struct  sha3_t
 Iterative SHA-3 context (all members are private). More...
 

Functions

void sha3_224 (const uint8_t *src, size_t len, uint8_t dst[static 28])
 Calculate SHA3-224 hash of input data. More...
 
void sha3_224_init (sha3_t *hash)
 Initialize SHA3-224 hash context. More...
 
_Bool sha3_224_absorb (sha3_t *hash, const uint8_t *src, const size_t len)
 Initialize SHA3-224 hash context. More...
 
void sha3_224_final (sha3_t *hash, uint8_t dst[28])
 Finalize SHA3-224 hash context and write 28 bytes of output to destination buffer dst. More...
 
void sha3_256 (const uint8_t *src, size_t len, uint8_t dst[static 32])
 Calculate SHA3-256 hash of input data. More...
 
void sha3_256_init (sha3_t *hash)
 Initialize SHA3-256 hash context. More...
 
_Bool sha3_256_absorb (sha3_t *hash, const uint8_t *src, const size_t len)
 Absorb input data into SHA3-256 hash context. More...
 
void sha3_256_final (sha3_t *hash, uint8_t dst[32])
 Finalize SHA3-256 hash context and write 32 bytes of output to destination buffer dst. More...
 
void sha3_384 (const uint8_t *src, size_t len, uint8_t dst[static 48])
 Calculate SHA3-384 hash of input data. More...
 
void sha3_384_init (sha3_t *hash)
 Initialize SHA3-384 hash context. More...
 
_Bool sha3_384_absorb (sha3_t *hash, const uint8_t *src, const size_t len)
 Absorb input data into SHA3-384 hash context. More...
 
void sha3_384_final (sha3_t *hash, uint8_t dst[48])
 Finalize SHA3-384 hash context and write 48 bytes of output to destination buffer dst. More...
 
void sha3_512 (const uint8_t *src, size_t len, uint8_t dst[static 64])
 Calculate SHA3-512 hash of input data. More...
 
void sha3_512_init (sha3_t *hash)
 Initialize SHA3-512 hash context. More...
 
_Bool sha3_512_absorb (sha3_t *hash, const uint8_t *src, const size_t len)
 Absorb input data into SHA3-512 hash context. More...
 
void sha3_512_final (sha3_t *hash, uint8_t dst[64])
 Finalize SHA3-512 hash context and write 64 bytes of output to destination buffer dst. More...
 

Detailed Description

Cryptographic hash functions with fixed-length output, as defined in section 6.1 of FIPS 202.

Function Documentation

◆ sha3_224()

void sha3_224 ( const uint8_t *  src,
size_t  len,
uint8_t  dst[static 28] 
)

Calculate SHA3-224 hash of input data.

Hash len bytes of input data from source buffer src with SHA3-224 (FIPS 202, section 6.1), then write 28 bytes of output to destination buffer dst.

Parameters
[in]srcSource buffer.
[in]lenSource buffer length, in bytes.
[out]dstDestination buffer. Must be at least 28 bytes in length.

Example:

// get 1024 random bytes
uint8_t buf[1024] = { 0 };
rand_bytes(buf, sizeof(buf));
// calculate sha3-224 hash of `buf`, write results to `hash`
uint8_t hash[28] = { 0 };
sha3_224(buf, sizeof(buf), hash);
void sha3_224(const uint8_t *src, size_t len, uint8_t dst[static 28])
Calculate SHA3-224 hash of input data.

◆ sha3_224_absorb()

_Bool sha3_224_absorb ( sha3_t hash,
const uint8_t *  src,
const size_t  len 
)

Initialize SHA3-224 hash context.

Absorb len bytes of input data from source buffer src into SHA3-224 hash context hash. Can be called iteratively to absorb input data in chunks.

Parameters
[in,out]hashSHA3-224 hash context.
[in]srcSource buffer.
[in]lenSource buffer length, in bytes.
Returns
True if data was absorbed, and false otherwise (e.g., if context has already been finalized).

Example:

// get 1024 random bytes
uint8_t buf[1024] = { 0 };
rand_bytes(buf, sizeof(buf));
// create sha3-224 context
sha3_t ctx = { 0 };
// absorb `buf` in 32 byte chunks
for (size_t i = 0; i < sizeof(buf); i += 32) {
sha3_224_absorb(&ctx, buf + i, 32);
}
// finalize context, write result to `hash`
uint8_t hash[28] = { 0 };
sha3_224_final(&ctx, hash);
void sha3_224_final(sha3_t *hash, uint8_t dst[28])
Finalize SHA3-224 hash context and write 28 bytes of output to destination buffer dst.
void sha3_224_init(sha3_t *hash)
Initialize SHA3-224 hash context.
_Bool sha3_224_absorb(sha3_t *hash, const uint8_t *src, const size_t len)
Initialize SHA3-224 hash context.
Iterative SHA-3 context (all members are private).
Definition: sha3.h:67

◆ sha3_224_final()

void sha3_224_final ( sha3_t hash,
uint8_t  dst[28] 
)

Finalize SHA3-224 hash context and write 28 bytes of output to destination buffer dst.

Parameters
[in,out]hashSHA3-224 hash context.
[out]dstDestination buffer. Must be at least 28 bytes in length.

Example:

// get 1024 random bytes
uint8_t buf[1024] = { 0 };
rand_bytes(buf, sizeof(buf));
// create sha3-224 context
sha3_t ctx = { 0 };
// absorb `buf` in 32 byte chunks
for (size_t i = 0; i < sizeof(buf); i += 32) {
sha3_224_absorb(&ctx, buf + i, 32);
}
// finalize context, write result to `hash`
uint8_t hash[28] = { 0 };
sha3_224_final(&ctx, hash);

◆ sha3_224_init()

void sha3_224_init ( sha3_t hash)

Initialize SHA3-224 hash context.

Parameters
[out]hashSHA3-224 hash context.

Example:

// get 1024 random bytes
uint8_t buf[1024] = { 0 };
rand_bytes(buf, sizeof(buf));
// create sha3-224 context
sha3_t ctx = { 0 };
// absorb `buf` in 32 byte chunks
for (size_t i = 0; i < sizeof(buf); i += 32) {
sha3_224_absorb(&ctx, buf + i, 32);
}
// finalize context, write result to `hash`
uint8_t hash[28] = { 0 };
sha3_224_final(&ctx, hash);

◆ sha3_256()

void sha3_256 ( const uint8_t *  src,
size_t  len,
uint8_t  dst[static 32] 
)

Calculate SHA3-256 hash of input data.

Hash len bytes of input data from source buffer src with SHA3-256 (FIPS 202, section 6.1), then write 32 bytes of output to destination buffer dst.

Parameters
[in]srcSource buffer.
[in]lenSource buffer 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));
// calculate sha3-256 hash of `buf`, write results to `hash`
uint8_t hash[32] = { 0 };
sha3_256(buf, sizeof(buf), hash);
void sha3_256(const uint8_t *src, size_t len, uint8_t dst[static 32])
Calculate SHA3-256 hash of input data.

◆ sha3_256_absorb()

_Bool sha3_256_absorb ( sha3_t hash,
const uint8_t *  src,
const size_t  len 
)

Absorb input data into SHA3-256 hash context.

Absorb len bytes of input data from source buffer src into SHA3-256 hash context hash. Can be called iteratively to absorb input data in chunks.

Parameters
[in,out]hashSHA3-256 hash context.
[in]srcSource buffer.
[in]lenSource buffer length, in bytes.
Returns
True if data was absorbed, and false otherwise (e.g., if context has already been finalized).

Example:

// get 1024 random bytes
uint8_t buf[1024] = { 0 };
rand_bytes(buf, sizeof(buf));
// create sha3-256 context
sha3_t ctx = { 0 };
// absorb `buf` in 32 byte chunks
for (size_t i = 0; i < sizeof(buf); i += 32) {
sha3_256_absorb(&ctx, buf + i, 32);
}
// finalize context, write result to `hash`
uint8_t hash[32] = { 0 };
sha3_256_final(&ctx, hash);
void sha3_256_final(sha3_t *hash, uint8_t dst[32])
Finalize SHA3-256 hash context and write 32 bytes of output to destination buffer dst.
void sha3_256_init(sha3_t *hash)
Initialize SHA3-256 hash context.
_Bool sha3_256_absorb(sha3_t *hash, const uint8_t *src, const size_t len)
Absorb input data into SHA3-256 hash context.

◆ sha3_256_final()

void sha3_256_final ( sha3_t hash,
uint8_t  dst[32] 
)

Finalize SHA3-256 hash context and write 32 bytes of output to destination buffer dst.

Parameters
[in,out]hashSHA3-256 hash context.
[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));
// create sha3-256 context
sha3_t ctx = { 0 };
// absorb `buf` in 32 byte chunks
for (size_t i = 0; i < sizeof(buf); i += 32) {
sha3_256_absorb(&ctx, buf + i, 32);
}
// finalize context, write result to `hash`
uint8_t hash[32] = { 0 };
sha3_256_final(&ctx, hash);

◆ sha3_256_init()

void sha3_256_init ( sha3_t hash)

Initialize SHA3-256 hash context.

Parameters
[out]hashSHA3-256 hash context.

Example:

// get 1024 random bytes
uint8_t buf[1024] = { 0 };
rand_bytes(buf, sizeof(buf));
// create sha3-256 context
sha3_t ctx = { 0 };
// absorb `buf` in 32 byte chunks
for (size_t i = 0; i < sizeof(buf); i += 32) {
sha3_256_absorb(&ctx, buf + i, 32);
}
// finalize context, write result to `hash`
uint8_t hash[32] = { 0 };
sha3_256_final(&ctx, hash);

◆ sha3_384()

void sha3_384 ( const uint8_t *  src,
size_t  len,
uint8_t  dst[static 48] 
)

Calculate SHA3-384 hash of input data.

Hash len bytes of input data from source buffer src with SHA3-384 (FIPS 202, section 6.1), then write 48 bytes of output to destination buffer dst.

Parameters
[in]srcSource buffer.
[in]lenSource buffer length, in bytes.
[out]dstDestination buffer. Must be at least 48 bytes in length.

Example:

// get 1024 random bytes
uint8_t buf[1024] = { 0 };
rand_bytes(buf, sizeof(buf));
// calculate sha3-384 hash of `buf`, write results to `hash`
uint8_t hash[48] = { 0 };
sha3_384(buf, sizeof(buf), hash);
void sha3_384(const uint8_t *src, size_t len, uint8_t dst[static 48])
Calculate SHA3-384 hash of input data.

◆ sha3_384_absorb()

_Bool sha3_384_absorb ( sha3_t hash,
const uint8_t *  src,
const size_t  len 
)

Absorb input data into SHA3-384 hash context.

Absorb len bytes of input data from source buffer src into SHA3-384 hash context hash. Can be called iteratively to absorb input data in chunks.

Parameters
[in,out]hashSHA3-384 hash context.
[in]srcSource buffer.
[in]lenSource buffer length, in bytes.
Returns
True if data was absorbed, and false otherwise (e.g., if context has already been finalized).

Example:

// get 1024 random bytes
uint8_t buf[1024] = { 0 };
rand_bytes(buf, sizeof(buf));
// create sha3-384 context
sha3_t ctx = { 0 };
// absorb `buf` in 32 byte chunks
for (size_t i = 0; i < sizeof(buf); i += 32) {
sha3_384_absorb(&ctx, buf + i, 32);
}
// finalize context, write result to `hash`
uint8_t hash[48] = { 0 };
sha3_384_final(&ctx, hash);
void sha3_384_final(sha3_t *hash, uint8_t dst[48])
Finalize SHA3-384 hash context and write 48 bytes of output to destination buffer dst.
_Bool sha3_384_absorb(sha3_t *hash, const uint8_t *src, const size_t len)
Absorb input data into SHA3-384 hash context.
void sha3_384_init(sha3_t *hash)
Initialize SHA3-384 hash context.

◆ sha3_384_final()

void sha3_384_final ( sha3_t hash,
uint8_t  dst[48] 
)

Finalize SHA3-384 hash context and write 48 bytes of output to destination buffer dst.

Parameters
[in,out]hashSHA3-384 hash context.
[out]dstDestination buffer. Must be at least 48 bytes in length.

Example:

// get 1024 random bytes
uint8_t buf[1024] = { 0 };
rand_bytes(buf, sizeof(buf));
// create sha3-384 context
sha3_t ctx = { 0 };
// absorb `buf` in 32 byte chunks
for (size_t i = 0; i < sizeof(buf); i += 32) {
sha3_384_absorb(&ctx, buf + i, 32);
}
// finalize context, write result to `hash`
uint8_t hash[48] = { 0 };
sha3_384_final(&ctx, hash);

◆ sha3_384_init()

void sha3_384_init ( sha3_t hash)

Initialize SHA3-384 hash context.

Parameters
[out]hashSHA3-384 hash context.

Example:

// get 1024 random bytes
uint8_t buf[1024] = { 0 };
rand_bytes(buf, sizeof(buf));
// create sha3-384 context
sha3_t ctx = { 0 };
// absorb `buf` in 32 byte chunks
for (size_t i = 0; i < sizeof(buf); i += 32) {
sha3_384_absorb(&ctx, buf + i, 32);
}
// finalize context, write result to `hash`
uint8_t hash[48] = { 0 };
sha3_384_final(&ctx, hash);

◆ sha3_512()

void sha3_512 ( const uint8_t *  src,
size_t  len,
uint8_t  dst[static 64] 
)

Calculate SHA3-512 hash of input data.

Hash len bytes of input data from source buffer src with SHA3-512 (FIPS 202, section 6.1), then write 64 bytes of output to destination buffer dst.

Parameters
[in]srcSource buffer.
[in]lenSource buffer length, in bytes.
[out]dstDestination buffer. Must be at least 64 bytes in length.

Example:

// get 1024 random bytes
uint8_t buf[1024] = { 0 };
rand_bytes(buf, sizeof(buf));
// calculate sha3-512 hash of `buf`, write results to `hash`
uint8_t hash[64] = { 0 };
sha3_512(buf, sizeof(buf), hash);
void sha3_512(const uint8_t *src, size_t len, uint8_t dst[static 64])
Calculate SHA3-512 hash of input data.

◆ sha3_512_absorb()

_Bool sha3_512_absorb ( sha3_t hash,
const uint8_t *  src,
const size_t  len 
)

Absorb input data into SHA3-512 hash context.

Absorb len bytes of input data from source buffer src into SHA3-512 hash context hash. Can be called iteratively to absorb input data in chunks.

Parameters
[in,out]hashSHA3-512 hash context.
[in]srcSource buffer.
[in]lenSource buffer length, in bytes.
Returns
True if data was absorbed, and false otherwise (e.g., if context has already been finalized).

Example:

// get 1024 random bytes
uint8_t buf[1024] = { 0 };
rand_bytes(buf, sizeof(buf));
// create sha3-512 context
sha3_t ctx = { 0 };
// absorb `buf` in 32 byte chunks
for (size_t i = 0; i < sizeof(buf); i += 32) {
sha3_512_absorb(&ctx, buf + i, 32);
}
// finalize context, write result to `hash`
uint8_t hash[64] = { 0 };
sha3_512_final(&ctx, hash);
_Bool sha3_512_absorb(sha3_t *hash, const uint8_t *src, const size_t len)
Absorb input data into SHA3-512 hash context.
void sha3_512_final(sha3_t *hash, uint8_t dst[64])
Finalize SHA3-512 hash context and write 64 bytes of output to destination buffer dst.
void sha3_512_init(sha3_t *hash)
Initialize SHA3-512 hash context.

◆ sha3_512_final()

void sha3_512_final ( sha3_t hash,
uint8_t  dst[64] 
)

Finalize SHA3-512 hash context and write 64 bytes of output to destination buffer dst.

Parameters
[in,out]hashSHA3-512 hash context.
[out]dstDestination buffer. Must be at least 64 bytes in length.

Example:

// get 1024 random bytes
uint8_t buf[1024] = { 0 };
rand_bytes(buf, sizeof(buf));
// create sha3-512 context
sha3_t ctx = { 0 };
// absorb `buf` in 32 byte chunks
for (size_t i = 0; i < sizeof(buf); i += 32) {
sha3_512_absorb(&ctx, buf + i, 32);
}
// finalize context, write result to `hash`
uint8_t hash[64] = { 0 };
sha3_512_final(&ctx, hash);

◆ sha3_512_init()

void sha3_512_init ( sha3_t hash)

Initialize SHA3-512 hash context.

Parameters
[out]hashSHA3-512 hash context.

Example:

// get 1024 random bytes
uint8_t buf[1024] = { 0 };
rand_bytes(buf, sizeof(buf));
// create sha3-512 context
sha3_t ctx = { 0 };
// absorb `buf` in 32 byte chunks
for (size_t i = 0; i < sizeof(buf); i += 32) {
sha3_512_absorb(&ctx, buf + i, 32);
}
// finalize context, write result to `hash`
uint8_t hash[64] = { 0 };
sha3_512_final(&ctx, hash);