shake.cpp
// shake.cpp - modified by Wei Dai from Ronny Van Keer's public domain
// sha3-simple.c. All modifications here are placed in the
// public domain by Wei Dai.
// Keccack core function moved to keccakc.cpp in AUG 2018
// by Jeffrey Walton. Separating the core file allows both
// SHA3 and Keccack to share the core implementation.
/*
The SHAKE sponge function, designed by Guido Bertoni, Joan Daemen,
Michael Peeters and Gilles Van Assche. For more information, feedback or
questions, please refer to our website: http://keccak.noekeon.org/
Implementation by Ronny Van Keer, hereby denoted as "the implementer".
To the extent possible under law, the implementer has waived all copyright
and related or neighboring rights to the source code in this file.
http://creativecommons.org/publicdomain/zero/1.0/
*/
#include "pch.h"
#include "shake.h"
NAMESPACE_BEGIN(CryptoPP)
// The Keccak core function
extern void KeccakF1600(word64 *state);
void SHAKE::Update(const byte *input, size_t length)
{
CRYPTOPP_ASSERT(!(input == NULLPTR && length != 0));
if (length == 0) { return; }
size_t spaceLeft;
while (length >= (spaceLeft = r() - m_counter))
{
if (spaceLeft)
xorbuf(m_state.BytePtr() + m_counter, input, spaceLeft);
KeccakF1600(m_state);
input += spaceLeft;
length -= spaceLeft;
m_counter = 0;
}
if (length)
xorbuf(m_state.BytePtr() + m_counter, input, length);
m_counter += (unsigned int)length;
}
void SHAKE::Restart()
{
memset(m_state, 0, m_state.SizeInBytes());
m_counter = 0;
}
void SHAKE::ThrowIfInvalidTruncatedSize(size_t size) const
{
if (size > UINT_MAX)
throw InvalidArgument(std::string("HashTransformation: can't truncate a ") +
IntToString(UINT_MAX) + " byte digest to " + IntToString(size) + " bytes");
}
void SHAKE::TruncatedFinal(byte *hash, size_t size)
{
CRYPTOPP_ASSERT(hash != NULLPTR);
ThrowIfInvalidTruncatedSize(size);
m_state.BytePtr()[m_counter] ^= 0x1F;
m_state.BytePtr()[r()-1] ^= 0x80;
// FIPS 202, Algorithm 8, pp 18-19.
while (size > 0)
{
KeccakF1600(m_state);
const size_t segmentLen = STDMIN(size, (size_t)BlockSize());
std::memcpy(hash, m_state, segmentLen);
hash += segmentLen;
size -= segmentLen;
}
Restart();
}
NAMESPACE_END
HashTransformation::BlockSize
virtual unsigned int BlockSize() const
Provides the block size of the compression function.
Definition: cryptlib.h:1165
InvalidArgument
An invalid argument was detected.
Definition: cryptlib.h:203
SHAKE::Restart
void Restart()
Restart the hash.
Definition: shake.cpp:49
SHAKE::TruncatedFinal
void TruncatedFinal(byte *hash, size_t size)
Computes the hash of the current message.
Definition: shake.cpp:62
SHAKE::Update
void Update(const byte *input, size_t length)
Updates a hash with additional input.
Definition: shake.cpp:28
SecBlock::BytePtr
byte * BytePtr()
Provides a byte pointer to the first element in the memory block.
Definition: secblock.h:876
SecBlock::SizeInBytes
size_type SizeInBytes() const
Provides the number of bytes in the SecBlock.
Definition: secblock.h:885
word64
unsigned long long word64
64-bit unsigned datatype
Definition: config_int.h:91
IntToString
std::string IntToString(T value, unsigned int base=10)
Converts a value to a string.
Definition: misc.h:724
STDMIN
const T & STDMIN(const T &a, const T &b)
Replacement function for std::min.
Definition: misc.h:655
xorbuf
CRYPTOPP_DLL void xorbuf(byte *buf, const byte *mask, size_t count)
Performs an XOR of a buffer with a mask.
CryptoPP
Crypto++ library namespace.
pch.h
Precompiled header file.
shake.h
Classes for SHAKE message digests.
CRYPTOPP_ASSERT
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.
Definition: trap.h:68