summaryrefslogtreecommitdiff
path: root/shake.cpp
diff options
context:
space:
mode:
authorJeffrey Walton <noloader@gmail.com>2019-02-12 19:51:37 -0500
committerGitHub <noreply@github.com>2019-02-12 19:51:37 -0500
commitc6e8a61b8b7dac8ac33bf12a4b9a0b510232da83 (patch)
treee17f8e69cefe48982a36f44e7c062b894e23ad9a /shake.cpp
parente499131ea6ba23ce57f6a7e50d0c2ff3fb8eff62 (diff)
downloadcryptopp-git-c6e8a61b8b7dac8ac33bf12a4b9a0b510232da83.tar.gz
Add SHAKE-128 and SHAKE-256 (GH #805, PR #806)
Diffstat (limited to 'shake.cpp')
-rw-r--r--shake.cpp67
1 files changed, 67 insertions, 0 deletions
diff --git a/shake.cpp b/shake.cpp
new file mode 100644
index 00000000..aa880389
--- /dev/null
+++ b/shake.cpp
@@ -0,0 +1,67 @@
+// 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::TruncatedFinal(byte *hash, size_t size)
+{
+ CRYPTOPP_ASSERT(hash != NULLPTR);
+ ThrowIfInvalidTruncatedSize(size);
+
+ m_state.BytePtr()[m_counter] ^= 0x1F;
+ m_state.BytePtr()[r()-1] ^= 0x80;
+ KeccakF1600(m_state);
+ std::memcpy(hash, m_state, size);
+ Restart();
+}
+
+NAMESPACE_END