2018-08-22 04:40:30 +00:00
|
|
|
// sha3.cpp - modified by Wei Dai from Ronny Van Keer's public domain
|
|
|
|
// Keccak-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.
|
2013-01-19 02:20:00 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
The Keccak 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/
|
|
|
|
|
2018-08-22 04:40:30 +00:00
|
|
|
Implementation by Ronny Van Keer, hereby denoted as "the implementer".
|
2013-01-19 02:20:00 +00:00
|
|
|
|
|
|
|
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 "sha3.h"
|
2018-08-22 04:40:30 +00:00
|
|
|
#include "keccakc.h"
|
2013-01-19 02:20:00 +00:00
|
|
|
|
|
|
|
NAMESPACE_BEGIN(CryptoPP)
|
|
|
|
|
|
|
|
void SHA3::Update(const byte *input, size_t length)
|
|
|
|
{
|
2018-05-06 02:56:15 +00:00
|
|
|
CRYPTOPP_ASSERT(!(input == NULLPTR && length != 0));
|
|
|
|
if (length == 0) { return; }
|
2015-11-18 20:32:28 +00:00
|
|
|
|
2016-09-22 14:35:57 +00:00
|
|
|
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;
|
|
|
|
}
|
2013-01-19 02:20:00 +00:00
|
|
|
|
2016-09-22 14:35:57 +00:00
|
|
|
if (length)
|
|
|
|
xorbuf(m_state.BytePtr() + m_counter, input, length);
|
|
|
|
m_counter += (unsigned int)length;
|
2013-01-19 02:20:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SHA3::Restart()
|
|
|
|
{
|
2016-09-22 14:35:57 +00:00
|
|
|
memset(m_state, 0, m_state.SizeInBytes());
|
|
|
|
m_counter = 0;
|
2013-01-19 02:20:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SHA3::TruncatedFinal(byte *hash, size_t size)
|
|
|
|
{
|
2018-05-06 02:56:15 +00:00
|
|
|
CRYPTOPP_ASSERT(hash != NULLPTR);
|
2016-09-22 14:35:57 +00:00
|
|
|
ThrowIfInvalidTruncatedSize(size);
|
2016-04-23 22:47:23 +00:00
|
|
|
|
2016-09-22 14:35:57 +00:00
|
|
|
m_state.BytePtr()[m_counter] ^= 0x06;
|
|
|
|
m_state.BytePtr()[r()-1] ^= 0x80;
|
|
|
|
KeccakF1600(m_state);
|
|
|
|
memcpy(hash, m_state, size);
|
|
|
|
Restart();
|
2013-01-19 02:20:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NAMESPACE_END
|