Crypto++  8.6
Free C++ class library of cryptographic schemes
sha3.cpp
1 // sha3.cpp - modified by Wei Dai from Ronny Van Keer's public domain
2 // Keccak-simple.c. All modifications here are placed in the
3 // public domain by Wei Dai.
4 // Keccack core function moved to keccakc.cpp in AUG 2018
5 // by Jeffrey Walton. Separating the core file allows both
6 // SHA3 and Keccack to share the core implementation.
7 
8 /*
9 The Keccak sponge function, designed by Guido Bertoni, Joan Daemen,
10 Michael Peeters and Gilles Van Assche. For more information, feedback or
11 questions, please refer to our website: http://keccak.noekeon.org/
12 
13 Implementation by Ronny Van Keer, hereby denoted as "the implementer".
14 
15 To the extent possible under law, the implementer has waived all copyright
16 and related or neighboring rights to the source code in this file.
17 http://creativecommons.org/publicdomain/zero/1.0/
18 */
19 
20 #include "pch.h"
21 #include "sha3.h"
22 
23 NAMESPACE_BEGIN(CryptoPP)
24 
25 // The Keccak core function
26 extern void KeccakF1600(word64 *state);
27 
28 NAMESPACE_END
29 
30 NAMESPACE_BEGIN(CryptoPP)
31 
32 void SHA3::Update(const byte *input, size_t length)
33 {
34  CRYPTOPP_ASSERT(!(input == NULLPTR && length != 0));
35  if (length == 0) { return; }
36 
37  size_t spaceLeft;
38  while (length >= (spaceLeft = r() - m_counter))
39  {
40  if (spaceLeft)
41  xorbuf(m_state.BytePtr() + m_counter, input, spaceLeft);
42  KeccakF1600(m_state);
43  input += spaceLeft;
44  length -= spaceLeft;
45  m_counter = 0;
46  }
47 
48  if (length)
49  xorbuf(m_state.BytePtr() + m_counter, input, length);
50  m_counter += (unsigned int)length;
51 }
52 
54 {
55  memset(m_state, 0, m_state.SizeInBytes());
56  m_counter = 0;
57 }
58 
59 void SHA3::TruncatedFinal(byte *hash, size_t size)
60 {
61  CRYPTOPP_ASSERT(hash != NULLPTR);
62  ThrowIfInvalidTruncatedSize(size);
63 
64  m_state.BytePtr()[m_counter] ^= 0x06;
65  m_state.BytePtr()[r()-1] ^= 0x80;
66  KeccakF1600(m_state);
67  std::memcpy(hash, m_state, size);
68  Restart();
69 }
70 
71 NAMESPACE_END
SHA3::Restart
void Restart()
Restart the hash.
Definition: sha3.cpp:53
CRYPTOPP_ASSERT
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.
Definition: trap.h:68
word64
unsigned long long word64
64-bit unsigned datatype
Definition: config_int.h:91
pch.h
Precompiled header file.
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
SHA3::TruncatedFinal
void TruncatedFinal(byte *hash, size_t size)
Computes the hash of the current message.
Definition: sha3.cpp:59
CryptoPP
Crypto++ library namespace.
SHA3
SHA3 message digest base class.
Definition: sha3.h:28
xorbuf
CRYPTOPP_DLL void xorbuf(byte *buf, const byte *mask, size_t count)
Performs an XOR of a buffer with a mask.
sha3.h
Classes for SHA3 message digests.