Skip to content

SHA-3

Header: #include <cryptopp/sha3.h> | Namespace: CryptoPP Since: Crypto++ 5.6.2 as Keccak; FIPS 202 padding since Crypto++ 5.6.4 Thread Safety: Not thread-safe per instance; use separate instances per thread

SHA-3 is a hash function family standardised by NIST in FIPS 202, built from the KECCAK sponge construction rather than the SHA-2 design. FIPS 202 describes the SHA-3 hash functions as alternatives to the SHA-2 functions.

Quick Example

#include <cryptopp/sha3.h>
#include <cryptopp/hex.h>
#include <cryptopp/filters.h>
#include <iostream>

int main() {
    using namespace CryptoPP;

    SHA3_256 hash;
    std::string message = "Hello, World!";
    std::string digest, hexOutput;

    StringSource(message, true,
        new HashFilter(hash,
            new StringSink(digest)
        )
    );

    StringSource(digest, true,
        new HexEncoder(new StringSink(hexOutput))
    );

    std::cout << "SHA3-256: " << hexOutput << std::endl;

    return 0;
}

Usage Guidelines

Do:

  • Use SHA-3 for algorithmic diversity alongside SHA-2
  • Use when a standard or protocol specifically requires SHA-3
  • Use the extendable-output functions SHAKE128 and SHAKE256 (#include <cryptopp/shake.h>) when you need variable-length output

Avoid:

  • Mixing up SHA-3 with SHA-2 (different constructions)
  • Using Keccak (pre-FIPS padding) when you need SHA-3 (FIPS 202)

Available Variants

The constants below come from SHA3_Final, where BLOCKSIZE is the rate, 200 - 2 x DIGESTSIZE bytes.

SHA3_256

SHA3_256 hash;
static const int DIGESTSIZE = 32;  // 256 bits
static const int BLOCKSIZE = 136;  // Rate: 1088 bits

SHA3_512

SHA3_512 hash;
static const int DIGESTSIZE = 64;  // 512 bits
static const int BLOCKSIZE = 72;   // Rate: 576 bits

SHA3_224

SHA3_224 hash;
static const int DIGESTSIZE = 28;  // 224 bits
static const int BLOCKSIZE = 144;  // Rate: 1152 bits

SHA3_384

SHA3_384 hash;
static const int DIGESTSIZE = 48;  // 384 bits
static const int BLOCKSIZE = 104;  // Rate: 832 bits

Methods

Update()

void Update(const byte* input, size_t length);

Add data to the hash computation.

Final()

void Final(byte* digest);

Finalize and get digest.

TruncatedFinal()

void TruncatedFinal(byte* digest, size_t size);

Get truncated hash. A size larger than the digest size throws InvalidArgument.

Restart()

void Restart();

Reset to initial state. Final() and TruncatedFinal() also restart the object.

CalculateDigest()

void CalculateDigest(byte* digest,
                     const byte* input,
                     size_t length);

One-shot hashing: Update() followed by Final().

VerifyDigest()

bool VerifyDigest(const byte* digest,
                  const byte* input,
                  size_t length);

Verify hash. The comparison uses VerifyBufsEqual (constant time).

Complete Example: File Integrity with SHA3-256

#include <cryptopp/sha3.h>
#include <cryptopp/files.h>
#include <cryptopp/hex.h>
#include <iostream>

using namespace CryptoPP;

std::string sha3File(const std::string& filename) {
    SHA3_256 hash;
    std::string digest, hexDigest;

    FileSource(filename.c_str(), true,
        new HashFilter(hash,
            new StringSink(digest)
        )
    );

    StringSource(digest, true,
        new HexEncoder(new StringSink(hexDigest))
    );

    return hexDigest;
}

int main() {
    try {
        std::string hash = sha3File("firmware.bin");
        std::cout << "SHA3-256: " << hash << std::endl;
    } catch (const CryptoPP::Exception& e) {
        std::cerr << "Error: " << e.what() << std::endl;
        return 1;
    }
    return 0;
}

Performance

The SHA-3 classes have no AlgorithmProvider() override, so they report the default "C++". Each permutation call absorbs one block of the rate, which is 136 bytes for SHA3-256 and 72 bytes for SHA3-512.

Security

Security Properties

VariantOutputCollisionPreimageCapacity
SHA3-224224-bit112-bit224-bit448-bit
SHA3-256256-bit128-bit256-bit512-bit
SHA3-384384-bit192-bit384-bit768-bit
SHA3-512512-bit256-bit512-bit1024-bit

1 and table 4. Second-preimage strength equals the preimage strength for all four.

Sponge Construction

SHA-3 uses a sponge construction with:

  • Rate (r): Bits absorbed per call of the permutation, 1600 - c
  • Capacity (c): Twice the digest length, c = 2d
  • Permutation: KECCAK-p[1600, 24]

FIPS 202 also states that SHA-3 is designed to resist length-extension attacks.

Test Vectors

The empty-string digest is the Len = 0 case of the NIST CAVP SHA-3 byte-oriented test vectors.

// SHA3-256(""): empty string
// a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a

SHA3_256 hash;
byte digest[32];
hash.Final(digest);

byte expected[] = {
    0xa7, 0xff, 0xc6, 0xf8, 0xbf, 0x1e, 0xd7, 0x66,
    0x51, 0xc1, 0x47, 0x56, 0xa0, 0x61, 0xd6, 0x62,
    0xf5, 0x80, 0xff, 0x4d, 0xe4, 0x3b, 0x49, 0xfa,
    0x82, 0xd8, 0x0a, 0x4b, 0x80, 0xf8, 0x43, 0x4a
};

assert(std::memcmp(digest, expected, 32) == 0);

SHA-3 vs SHA-2

FeatureSHA-2SHA-3
DesignMessage processed in 512- or 1024-bit blocks in sequenceSponge
Accelerated paths in this librarySHA extensions, ARMv8, Power8 (SHA-256)None; reports “C++”
StandardFIPS 180-4FIPS 202
Use CaseGeneral purposeDiversity

When to use SHA-3:

  • A standard or protocol requires SHA-3
  • Algorithmic diversity alongside SHA-2

When to use SHA-2/BLAKE3:

  • You want the library’s accelerated code paths
  • A protocol specifies SHA-2 or BLAKE3

SHA-3 vs Keccak

Important: SHA-3 (FIPS 202) and Keccak (original) are different:

// SHA-3 (FIPS 202) - Use this for standards compliance
SHA3_256 sha3;  // Domain separation: 0x06

// Keccak (original) - Pre-standardization version
Keccak_256 keccak;  // Domain separation: 0x01

The two differ only in the padding byte the library XORs into the final block: 0x06 for SHA-3 and 0x01 for Keccak. The 0x06 byte is the FIPS 202 padding for the SHA-3 hash functions.

Use SHA-3 unless you specifically need Keccak for compatibility with pre-FIPS implementations.

Thread Safety

Not thread-safe. Use separate instances per thread.

When to Use SHA-3

Use SHA-3 for:

  1. Algorithmic Diversity - A second hash family alongside SHA-2
  2. Compliance - FIPS 202 requirements

Don’t use SHA-3 for:

  1. Pre-FIPS Keccak interoperability - Use the Keccak_* classes instead

Exceptions

TruncatedFinal(), TruncatedVerify() and VerifyTruncatedDigest() throw InvalidArgument when the requested size is larger than the digest size.

See Also

References

  1. FIPS 202: SHA-3 Standard: Permutation-Based Hash and Extendable-Output Functions
  2. NIST CAVP: Secure Hashing