diff --git a/doc/crypto.dox b/doc/crypto.dox
index 156e827f..d2546451 100644
--- a/doc/crypto.dox
+++ b/doc/crypto.dox
@@ -31,6 +31,7 @@
\li Stream ciphers: ChaCha
\li Authenticated encryption with associated data (AEAD): ChaChaPoly, EAX, GCM
\li Hash algorithms: SHA256, SHA512, SHA3_256, SHA3_512, BLAKE2s, BLAKE2b (regular and HMAC modes)
+\li Extendable output functions (XOF's): SHAKE128, SHAKE256
\li Message authenticators: Poly1305, GHASH, OMAC
\li Public key algorithms: Curve25519, Ed25519
\li Random number generation: \link RNGClass RNG\endlink, TransistorNoiseSource, RingOscillatorNoiseSource
@@ -113,6 +114,10 @@ Ardunino Mega 2560 running at 16 MHz are similar:
Poly1305 | 26.26us | 489.11us | 17.06us | 53 |
GHASH | 74.59us | 15.91us | 14.79us | 33 |
|
+XOF Algorithm | Hashing (per byte) | Extending (per byte) | Encryption (per byte) | State Size (bytes) |
+SHAKE128 | 49.43us | 49.02us | 49.59us | 206 |
+SHAKE256 | 60.77us | 60.37us | 60.93us | 206 |
+ |
Public Key Operation | Time (per operation) | Comment |
Curve25519::eval() | 2716ms | Raw curve evaluation |
Curve25519::dh1() | 2718ms | First half of Diffie-Hellman key agreement |
@@ -174,6 +179,10 @@ All figures are for the Arduino Due running at 84 MHz:
Poly1305 | 0.81us | 19.01us | 2.57us | 60 |
GHASH | 4.47us | 1.52us | 2.60us | 36 |
|
+XOF Algorithm | Hashing (per byte) | Extending (per byte) | Encryption (per byte) | State Size (bytes) |
+SHAKE128 | 4.60us | 4.45us | 4.59us | 232 |
+SHAKE256 | 5.64us | 5.49us | 5.63us | 232 |
+ |
Public Key Operation | Time (per operation) | Comment |
Curve25519::eval() | 103ms | Raw curve evaluation |
Curve25519::dh1() | 103ms | First half of Diffie-Hellman key agreement |
diff --git a/doc/mainpage.dox b/doc/mainpage.dox
index 892772a9..27c319a0 100644
--- a/doc/mainpage.dox
+++ b/doc/mainpage.dox
@@ -96,6 +96,7 @@ realtime clock and the LCD library to implement an alarm clock.
\li Stream ciphers: ChaCha
\li Authenticated encryption with associated data (AEAD): ChaChaPoly, EAX, GCM
\li Hash algorithms: SHA256, SHA512, SHA3_256, SHA3_512, BLAKE2s, BLAKE2b (regular and HMAC modes)
+\li Extendable output functions (XOF's): SHAKE128, SHAKE256
\li Message authenticators: Poly1305, GHASH, OMAC
\li Public key algorithms: Curve25519, Ed25519
\li Random number generation: \link RNGClass RNG\endlink, TransistorNoiseSource, RingOscillatorNoiseSource
diff --git a/libraries/Crypto/KeccakCore.cpp b/libraries/Crypto/KeccakCore.cpp
index 1abfb604..4cc108af 100644
--- a/libraries/Crypto/KeccakCore.cpp
+++ b/libraries/Crypto/KeccakCore.cpp
@@ -32,11 +32,11 @@
* \brief Keccak core sponge function.
*
* KeccakCore provides the core sponge function for different capacities.
- * It is used to implement Hash algorithms such as SHA3.
+ * It is used to implement algorithms such as SHA3 and SHAKE.
*
* References: http://en.wikipedia.org/wiki/SHA-3
*
- * \sa SHA3
+ * \sa SHA3_256, SHAKE256
*/
#if !defined(CRYPTO_LITTLE_ENDIAN)
@@ -189,7 +189,7 @@ void KeccakCore::pad(uint8_t tag)
* If more than blockSize() bytes are required, the sponge function will
* be invoked to generate additional data.
*
- * \sa update(), reset(), extractHash()
+ * \sa update(), reset(), encrypt()
*/
void KeccakCore::extract(void *data, size_t size)
{
@@ -219,6 +219,56 @@ void KeccakCore::extract(void *data, size_t size)
}
}
+/**
+ * \brief Extracts data from the Keccak sponge function and uses it to
+ * encrypt a buffer.
+ *
+ * \param output The output buffer to write to, which may be the same
+ * buffer as \a input. The \a output buffer must have at least as many
+ * bytes as the \a input buffer.
+ * \param input The input buffer to read from.
+ * \param size The number of bytes to encrypt.
+ *
+ * This function extracts data from the sponge function and then XOR's
+ * it with \a input to generate the \a output.
+ *
+ * If more than blockSize() bytes are required, the sponge function will
+ * be invoked to generate additional data.
+ *
+ * \sa update(), reset(), extract()
+ */
+void KeccakCore::encrypt(void *output, const void *input, size_t size)
+{
+ // Stop accepting input while we are generating output.
+ state.inputSize = 0;
+
+ // Copy the output data into the caller's return buffer.
+ uint8_t *out = (uint8_t *)output;
+ const uint8_t *in = (const uint8_t *)input;
+ uint8_t tempSize;
+ while (size > 0) {
+ // Generate another output block if the current one has been exhausted.
+ if (state.outputSize >= _blockSize) {
+ keccakp();
+ state.outputSize = 0;
+ }
+
+ // How many bytes can we extract this time around?
+ tempSize = _blockSize - state.outputSize;
+ if (tempSize > size)
+ tempSize = size;
+
+ // XOR the partial output data into the caller's return buffer.
+ const uint8_t *d = ((const uint8_t *)(state.A)) + state.outputSize;
+ for (uint8_t index = 0; index < tempSize; ++index)
+ out[index] = in[index] ^ d[index];
+ state.outputSize += tempSize;
+ size -= tempSize;
+ out += tempSize;
+ in += tempSize;
+ }
+}
+
/**
* \brief Clears all sensitive data from this object.
*/
diff --git a/libraries/Crypto/KeccakCore.h b/libraries/Crypto/KeccakCore.h
index 12a5402b..ca2447ee 100644
--- a/libraries/Crypto/KeccakCore.h
+++ b/libraries/Crypto/KeccakCore.h
@@ -43,6 +43,7 @@ public:
void pad(uint8_t tag);
void extract(void *data, size_t size);
+ void encrypt(void *output, const void *input, size_t size);
void clear();
diff --git a/libraries/Crypto/SHAKE.cpp b/libraries/Crypto/SHAKE.cpp
new file mode 100644
index 00000000..b813675c
--- /dev/null
+++ b/libraries/Crypto/SHAKE.cpp
@@ -0,0 +1,137 @@
+/*
+ * Copyright (C) 2016 Southern Storm Software, Pty Ltd.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included
+ * in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#include "SHAKE.h"
+
+/**
+ * \class SHAKE SHAKE.h
+ * \brief Abstract base class for the SHAKE Extendable-Output Functions (XOFs).
+ *
+ * Reference: http://en.wikipedia.org/wiki/SHA-3
+ *
+ * \sa SHAKE256, SHAKE128, SHA3_256
+ */
+
+/**
+ * \brief Constructs a SHAKE object.
+ *
+ * \param capacity The capacity of the Keccak sponge function in bits which
+ * should be a multiple of 64 and between 64 and 1536.
+ */
+SHAKE::SHAKE(size_t capacity)
+ : finalized(false)
+{
+ core.setCapacity(capacity);
+}
+
+/**
+ * \brief Destroys this SHAKE object after clearing all sensitive information.
+ */
+SHAKE::~SHAKE()
+{
+}
+
+size_t SHAKE::blockSize() const
+{
+ return core.blockSize();
+}
+
+void SHAKE::reset()
+{
+ core.reset();
+ finalized = false;
+}
+
+void SHAKE::update(const void *data, size_t len)
+{
+ if (finalized)
+ reset();
+ core.update(data, len);
+}
+
+void SHAKE::extend(uint8_t *data, size_t len)
+{
+ if (!finalized) {
+ core.pad(0x1F);
+ finalized = true;
+ }
+ core.extract(data, len);
+}
+
+void SHAKE::encrypt(uint8_t *output, const uint8_t *input, size_t len)
+{
+ if (!finalized) {
+ core.pad(0x1F);
+ finalized = true;
+ }
+ core.encrypt(output, input, len);
+}
+
+void SHAKE::clear()
+{
+ core.clear();
+ finalized = false;
+}
+
+/**
+ * \class SHAKE128 SHAKE.h
+ * \brief SHAKE Extendable-Output Function (XOF) with 128-bit security.
+ *
+ * Reference: http://en.wikipedia.org/wiki/SHA-3
+ *
+ * \sa SHAKE256, SHAKE, SHA3_256
+ */
+
+/**
+ * \fn SHAKE128::SHAKE128()
+ * \brief Constructs a SHAKE object with 128-bit security.
+ */
+
+/**
+ * \brief Destroys this SHAKE128 object after clearing all sensitive
+ * information.
+ */
+SHAKE128::~SHAKE128()
+{
+}
+
+/**
+ * \class SHAKE256 SHAKE.h
+ * \brief SHAKE Extendable-Output Function (XOF) with 256-bit security.
+ *
+ * Reference: http://en.wikipedia.org/wiki/SHA-3
+ *
+ * \sa SHAKE128, SHAKE, SHA3_256
+ */
+
+/**
+ * \fn SHAKE256::SHAKE256()
+ * \brief Constructs a SHAKE object with 256-bit security.
+ */
+
+/**
+ * \brief Destroys this SHAKE256 object after clearing all sensitive
+ * information.
+ */
+SHAKE256::~SHAKE256()
+{
+}
diff --git a/libraries/Crypto/SHAKE.h b/libraries/Crypto/SHAKE.h
new file mode 100644
index 00000000..83826e6d
--- /dev/null
+++ b/libraries/Crypto/SHAKE.h
@@ -0,0 +1,66 @@
+/*
+ * Copyright (C) 2016 Southern Storm Software, Pty Ltd.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included
+ * in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef CRYPTO_SHAKE_h
+#define CRYPTO_SHAKE_h
+
+#include "XOF.h"
+#include "KeccakCore.h"
+
+class SHAKE : public XOF
+{
+public:
+ virtual ~SHAKE();
+
+ size_t blockSize() const;
+
+ void reset();
+ void update(const void *data, size_t len);
+
+ void extend(uint8_t *data, size_t len);
+ void encrypt(uint8_t *output, const uint8_t *input, size_t len);
+
+ void clear();
+
+protected:
+ SHAKE(size_t capacity);
+
+private:
+ KeccakCore core;
+ bool finalized;
+};
+
+class SHAKE128 : public SHAKE
+{
+public:
+ SHAKE128() : SHAKE(256) {}
+ virtual ~SHAKE128();
+};
+
+class SHAKE256 : public SHAKE
+{
+public:
+ SHAKE256() : SHAKE(512) {}
+ virtual ~SHAKE256();
+};
+
+#endif
diff --git a/libraries/Crypto/XOF.cpp b/libraries/Crypto/XOF.cpp
new file mode 100644
index 00000000..2462a819
--- /dev/null
+++ b/libraries/Crypto/XOF.cpp
@@ -0,0 +1,216 @@
+/*
+ * Copyright (C) 2016 Southern Storm Software, Pty Ltd.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included
+ * in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#include "XOF.h"
+
+/**
+ * \class XOF XOF.h
+ * \brief Abstract base class for Extendable-Output Functions (XOFs).
+ *
+ * Extendable-Output Functions, or XOFs, are a new class of cryptographic
+ * primitive that was defined by NIST during the SHA-3 standardization
+ * process. Essentially an XOF is a hash algorithm that has an
+ * arbitrary-length output instead of a fixed-length digest.
+ *
+ * XOFs can be used for a variety of cryptographic tasks:
+ *
+ * \li Mask generation functions for RSA OAEP style padding.
+ * \li Key derivation functions for expanding key seed material into
+ * arbitrary amounts of keying material for a secure session.
+ * \li Stream ciphers based on a key and IV.
+ *
+ * To use an XOF, it is first reset() and then data is added via multiple
+ * calls to update():
+ *
+ * \code
+ * SHAKE256 xof;
+ * xof.reset();
+ * xof.update(data1, sizeof(data1));
+ * xof.update(data2, sizeof(data2));
+ * ...
+ * \endcode
+ *
+ * Once all input data has been added, the XOF switches into extend mode
+ * to generate the arbitrary-length output data:
+ *
+ * \code
+ * xof.extend(output1, sizeof(output1));
+ * xof.extend(output2, sizeof(output2));
+ * ...
+ * \endcode
+ *
+ * Mask generation and key derivation is achieved as follows, where the
+ * key is unique for each invocation:
+ *
+ * \code
+ * SHAKE256 xof;
+ * xof.reset();
+ * xof.update(key, sizeof(key));
+ * xof.extend(output, sizeof(output));
+ * \endcode
+ *
+ * Stream ciphers can be constructed as follows, using the special
+ * encrypt() function that XOR's the output of extend() with the
+ * input plaintext to generate the output ciphertext (or alternatively
+ * XOR's the output of extend() with the ciphertext to recover the
+ * plaintext):
+ *
+ * \code
+ * SHAKE256 xof;
+ * xof.reset();
+ * xof.update(key, sizeof(key));
+ * xof.update(iv, sizeof(iv));
+ * xof.encrypt(output1, input1, sizeof(input1));
+ * xof.encrypt(output2, input2, sizeof(input2));
+ * ...
+ * \endcode
+ *
+ * If the key is reused, then the IV must be different for each session
+ * or the encryption scheme can be easily broken. It is better to
+ * generate a new key and IV combination for every session.
+ *
+ * It may also be a good idea to include some tag information with the input
+ * data to distinguish different uses of the XOF. For example:
+ *
+ * \code
+ * SHAKE256 xof;
+ * xof.reset();
+ * xof.update(key, sizeof(key));
+ * xof.update(iv, sizeof(iv));
+ * xof.update("MyCrypt", 7);
+ * xof.encrypt(output, input, sizeof(input));
+ * \endcode
+ *
+ * If the same key and IV was used with a different package, then it would
+ * not generate the same output as "MyCrypt".
+ *
+ * NIST warns that XOFs should not be used in place of hash functions.
+ * This is because of related outputs: if the same input is provided to
+ * an XOF with different output lengths, then the shorter output will
+ * be a prefix of the larger. This breaks the expected collision-resistance
+ * of regular hash functions. There is typically no need to use an XOF
+ * for hashing because NIST has already defined SHA3_256 and SHA3_512
+ * for that purpose.
+ *
+ * Reference: http://en.wikipedia.org/wiki/SHA-3
+ *
+ * \sa SHAKE256, SHAKE128, SHA3_256
+ */
+
+/**
+ * \brief Constructs a new XOF object.
+ */
+XOF::XOF()
+{
+}
+
+/**
+ * \brief Destroys this XOF object.
+ *
+ * \note Subclasses are responsible for clearing any sensitive data
+ * that remains in the XOF object when it is destroyed.
+ *
+ * \sa clear()
+ */
+XOF::~XOF()
+{
+}
+
+/**
+ * \fn size_t XOF::blockSize() const
+ * \brief Size of the internal block used by the XOF algorithm, in bytes.
+ *
+ * \sa update()
+ */
+
+/**
+ * \fn void XOF::reset()
+ * \brief Resets the XOF ready for a new session.
+ *
+ * \sa update(), extend(), encrypt()
+ */
+
+/**
+ * \fn void XOF::update(const void *data, size_t len)
+ * \brief Updates the XOF with more data.
+ *
+ * \param data Data to be hashed.
+ * \param len Number of bytes of data to be added to the XOF.
+ *
+ * If extend() or encrypt() has already been called, then the behavior of
+ * update() will be undefined. Call reset() first to start a new session.
+ *
+ * \sa reset(), extend(), encrypt()
+ */
+
+/**
+ * \fn void XOF::extend(uint8_t *data, size_t len)
+ * \brief Generates extendable output from this XOF.
+ *
+ * \param data The data buffer to be filled.
+ * \param len The number of bytes to write to \a data.
+ *
+ * \sa reset(), update(), encrypt()
+ */
+
+/**
+ * \fn void XOF::encrypt(uint8_t *output, const uint8_t *input, size_t len)
+ * \brief Encrypts an input buffer with extendable output from this XOF.
+ *
+ * \param output The output buffer to write to, which may be the same
+ * buffer as \a input. The \a output buffer must have at least as many
+ * bytes as the \a input buffer.
+ * \param input The input buffer to read from.
+ * \param len The number of bytes to encrypt.
+ *
+ * This function is a convenience that generates data with extend() and
+ * then XOR's it with the contents of \a input to generate the \a output.
+ * This function can also be used to decrypt.
+ *
+ * The encrypt() function can be called multiple times with different
+ * regions of the plaintext data.
+ *
+ * \sa reset(), update(), extend(), decrypt()
+ */
+
+/**
+ * \fn void XOF::decrypt(uint8_t *output, const uint8_t *input, size_t len)
+ * \brief Decrypts an input buffer with extendable output from this XOF.
+ *
+ * \param output The output buffer to write to, which may be the same
+ * buffer as \a input. The \a output buffer must have at least as many
+ * bytes as the \a input buffer.
+ * \param input The input buffer to read from.
+ * \param len The number of bytes to encrypt.
+ *
+ * This is a convenience function that merely calls encrypt().
+ *
+ * \sa reset(), update(), extend(), encrypt()
+ */
+
+/**
+ * \fn void XOF::clear()
+ * \brief Clears the hash state, removing all sensitive data, and then
+ * resets the XOF ready for a new session.
+ *
+ * \sa reset()
+ */
diff --git a/libraries/Crypto/XOF.h b/libraries/Crypto/XOF.h
new file mode 100644
index 00000000..fc728563
--- /dev/null
+++ b/libraries/Crypto/XOF.h
@@ -0,0 +1,51 @@
+/*
+ * Copyright (C) 2016 Southern Storm Software, Pty Ltd.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included
+ * in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef CRYPTO_XOF_h
+#define CRYPTO_XOF_h
+
+#include
+#include
+
+class XOF
+{
+public:
+ XOF();
+ virtual ~XOF();
+
+ virtual size_t blockSize() const = 0;
+
+ virtual void reset() = 0;
+ virtual void update(const void *data, size_t len) = 0;
+
+ virtual void extend(uint8_t *data, size_t len) = 0;
+ virtual void encrypt(uint8_t *output, const uint8_t *input, size_t len) = 0;
+
+ inline void decrypt(uint8_t *output, const uint8_t *input, size_t len)
+ {
+ encrypt(output, input, len);
+ }
+
+ virtual void clear() = 0;
+};
+
+#endif
diff --git a/libraries/Crypto/examples/TestSHAKE128/TestSHAKE128.ino b/libraries/Crypto/examples/TestSHAKE128/TestSHAKE128.ino
new file mode 100644
index 00000000..5d1acc1c
--- /dev/null
+++ b/libraries/Crypto/examples/TestSHAKE128/TestSHAKE128.ino
@@ -0,0 +1,371 @@
+/*
+ * Copyright (C) 2016 Southern Storm Software, Pty Ltd.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included
+ * in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+/*
+This example runs tests on the SHAKE128 implementation to verify
+correct behaviour.
+*/
+
+#include
+#include
+#include
+#if defined(__AVR__)
+#include
+#else
+#define PROGMEM
+#define memcpy_P(d, s, l) memcpy((d), (s), (l))
+#endif
+
+#define MAX_HASH_DATA_SIZE 167
+#define MAX_SHAKE_OUTPUT 256
+
+struct TestHashVectorSHAKE
+{
+ const char *name;
+ uint8_t data[MAX_HASH_DATA_SIZE];
+ size_t dataLen;
+ uint8_t hash[MAX_SHAKE_OUTPUT];
+};
+
+// Some test vectors from https://github.com/gvanas/KeccakCodePackage
+static TestHashVectorSHAKE const testVectorSHAKE128_1 PROGMEM = {
+ "SHAKE128 #1",
+ {0},
+ 0,
+ {0x7F, 0x9C, 0x2B, 0xA4, 0xE8, 0x8F, 0x82, 0x7D,
+ 0x61, 0x60, 0x45, 0x50, 0x76, 0x05, 0x85, 0x3E,
+ 0xD7, 0x3B, 0x80, 0x93, 0xF6, 0xEF, 0xBC, 0x88,
+ 0xEB, 0x1A, 0x6E, 0xAC, 0xFA, 0x66, 0xEF, 0x26,
+ 0x3C, 0xB1, 0xEE, 0xA9, 0x88, 0x00, 0x4B, 0x93,
+ 0x10, 0x3C, 0xFB,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3, 0xC5, 0xBA,
+ 0x7D, 0x2C, 0xDF, 0x3F, 0x1D, 0x39, 0x37, 0x6A,
+ 0x67, 0xC2, 0x3E, 0x55, 0x5F, 0x5A, 0xCF, 0x25,
+ 0xBC, 0xE1, 0xE5, 0x5F, 0x30, 0x72, 0x52, 0xB9,
+ 0xAA, 0xC2, 0xC0, 0xA3, 0x9C, 0x88, 0x5C, 0x7E,
+ 0x44, 0xF2, 0x04, 0xCB, 0x82, 0x1C, 0x0D, 0x37,
+ 0xA2, 0x2D, 0xE3, 0xA7, 0x1F, 0x3A, 0x19, 0x09,
+ 0xB1, 0x1B, 0x71, 0x81, 0xC4, 0x2B, 0xE9, 0xB7}
+};
+
+SHAKE128 shake128;
+TestHashVectorSHAKE tst;
+uint8_t output[MAX_SHAKE_OUTPUT];
+
+bool testSHAKE_N(SHAKE *shake, const struct TestHashVectorSHAKE *test, size_t inc, bool printName = false)
+{
+ size_t size;
+ size_t posn, len;
+
+ // Copy the test case out of program memory.
+ memcpy_P(&tst, test, sizeof(tst));
+ test = &tst;
+
+ // Print the test name if necessary.
+ if (printName) {
+ Serial.print(test->name);
+ Serial.print(" ... ");
+ }
+
+ // Hash the input data.
+ if (!inc)
+ inc = 1;
+ size = test->dataLen;
+ shake->reset();
+ for (posn = 0; posn < size; posn += inc) {
+ len = size - posn;
+ if (len > inc)
+ len = inc;
+ shake->update(test->data + posn, len);
+ }
+
+ // Generate data using extend() and check it.
+ size = MAX_SHAKE_OUTPUT;
+ for (posn = 0; posn < size; posn += inc) {
+ len = size - posn;
+ if (len > inc)
+ len = inc;
+ shake->extend(output + posn, len);
+ }
+ if (memcmp(output, test->hash, size) != 0)
+ return false;
+
+ // Restart and use encrypt() this time.
+ shake->reset();
+ shake->update(test->data, test->dataLen);
+ for (posn = 0; posn < size; ++posn)
+ output[posn] = (uint8_t)(posn + 0xAA);
+ for (posn = 0; posn < size; posn += inc) {
+ len = size - posn;
+ if (len > inc)
+ len = inc;
+ shake->encrypt(output + posn, output + posn, len);
+ }
+ for (posn = 0; posn < size; ++posn)
+ output[posn] ^= (uint8_t)(posn + 0xAA);
+ if (memcmp(output, test->hash, size) != 0)
+ return false;
+
+ return true;
+}
+
+void testSHAKE(SHAKE *shake, const struct TestHashVectorSHAKE *test)
+{
+ bool ok;
+ const char *str;
+ uint8_t ch;
+ size_t dataLen;
+
+ memcpy_P(&dataLen, &(test->dataLen), sizeof(size_t));
+
+ ok = testSHAKE_N(shake, test, dataLen, true);
+ ok &= testSHAKE_N(shake, test, 1);
+ ok &= testSHAKE_N(shake, test, 2);
+ ok &= testSHAKE_N(shake, test, 5);
+ ok &= testSHAKE_N(shake, test, 8);
+ ok &= testSHAKE_N(shake, test, 13);
+ ok &= testSHAKE_N(shake, test, 16);
+ ok &= testSHAKE_N(shake, test, 24);
+ ok &= testSHAKE_N(shake, test, 63);
+ ok &= testSHAKE_N(shake, test, 64);
+
+ if (ok)
+ Serial.println("Passed");
+ else
+ Serial.println("Failed");
+}
+
+void perfUpdate(SHAKE *shake)
+{
+ unsigned long start;
+ unsigned long elapsed;
+ int count;
+
+ Serial.print("Updating ... ");
+
+ for (size_t posn = 0; posn < MAX_SHAKE_OUTPUT; ++posn)
+ output[posn] = (uint8_t)posn;
+
+ shake->reset();
+ start = micros();
+ for (count = 0; count < 300; ++count) {
+ shake->update(output, MAX_SHAKE_OUTPUT);
+ }
+ shake->extend(output, 0); // Force a finalize after the update.
+ elapsed = micros() - start;
+
+ Serial.print(elapsed / (MAX_SHAKE_OUTPUT * 300.0));
+ Serial.print("us per byte, ");
+ Serial.print((MAX_SHAKE_OUTPUT * 300.0 * 1000000.0) / elapsed);
+ Serial.println(" bytes per second");
+}
+
+void perfExtend(SHAKE *shake)
+{
+ unsigned long start;
+ unsigned long elapsed;
+ int count;
+
+ Serial.print("Extending ... ");
+
+ for (size_t posn = 0; posn < MAX_SHAKE_OUTPUT; ++posn)
+ output[posn] = (uint8_t)posn;
+
+ shake->reset();
+ shake->update(output, MAX_SHAKE_OUTPUT);
+ shake->extend(output, 0); // Force a finalize after the update.
+ start = micros();
+ for (count = 0; count < 300; ++count) {
+ shake->extend(output, MAX_SHAKE_OUTPUT);
+ }
+ elapsed = micros() - start;
+
+ Serial.print(elapsed / (MAX_SHAKE_OUTPUT * 300.0));
+ Serial.print("us per byte, ");
+ Serial.print((MAX_SHAKE_OUTPUT * 300.0 * 1000000.0) / elapsed);
+ Serial.println(" bytes per second");
+}
+
+void perfEncrypt(SHAKE *shake)
+{
+ unsigned long start;
+ unsigned long elapsed;
+ int count;
+
+ Serial.print("Encrypting ... ");
+
+ for (size_t posn = 0; posn < MAX_SHAKE_OUTPUT; ++posn)
+ output[posn] = (uint8_t)posn;
+
+ shake->reset();
+ shake->update(output, MAX_SHAKE_OUTPUT);
+ shake->extend(output, 0); // Force a finalize after the update.
+ start = micros();
+ for (count = 0; count < 300; ++count) {
+ shake->encrypt(output, output, MAX_SHAKE_OUTPUT);
+ }
+ elapsed = micros() - start;
+
+ Serial.print(elapsed / (MAX_SHAKE_OUTPUT * 300.0));
+ Serial.print("us per byte, ");
+ Serial.print((MAX_SHAKE_OUTPUT * 300.0 * 1000000.0) / elapsed);
+ Serial.println(" bytes per second");
+}
+
+void setup()
+{
+ Serial.begin(9600);
+
+ Serial.println();
+
+ Serial.print("State Size ...");
+ Serial.println(sizeof(SHAKE128));
+ Serial.println();
+
+ Serial.println("Test Vectors:");
+ testSHAKE(&shake128, &testVectorSHAKE128_1);
+ testSHAKE(&shake128, &testVectorSHAKE128_2);
+ testSHAKE(&shake128, &testVectorSHAKE128_3);
+
+ Serial.println();
+
+ Serial.println("Performance Tests:");
+ perfUpdate(&shake128);
+ perfExtend(&shake128);
+ perfEncrypt(&shake128);
+}
+
+void loop()
+{
+}
diff --git a/libraries/Crypto/examples/TestSHAKE256/TestSHAKE256.ino b/libraries/Crypto/examples/TestSHAKE256/TestSHAKE256.ino
new file mode 100644
index 00000000..074bd48c
--- /dev/null
+++ b/libraries/Crypto/examples/TestSHAKE256/TestSHAKE256.ino
@@ -0,0 +1,367 @@
+/*
+ * Copyright (C) 2016 Southern Storm Software, Pty Ltd.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included
+ * in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+/*
+This example runs tests on the SHAKE256 implementation to verify
+correct behaviour.
+*/
+
+#include
+#include
+#include
+#if defined(__AVR__)
+#include
+#else
+#define PROGMEM
+#define memcpy_P(d, s, l) memcpy((d), (s), (l))
+#endif
+
+#define MAX_HASH_DATA_SIZE 135
+#define MAX_SHAKE_OUTPUT 256
+
+struct TestHashVectorSHAKE
+{
+ const char *name;
+ uint8_t data[MAX_HASH_DATA_SIZE];
+ size_t dataLen;
+ uint8_t hash[MAX_SHAKE_OUTPUT];
+};
+
+// Some test vectors from https://github.com/gvanas/KeccakCodePackage
+static TestHashVectorSHAKE const testVectorSHAKE256_1 PROGMEM = {
+ "SHAKE256 #1",
+ {0},
+ 0,
+ {0x46, 0xB9, 0xDD, 0x2B, 0x0B, 0xA8, 0x8D, 0x13,
+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 0xB3, 0x54, 0xCE, 0x3E, 0x6D, 0x1A, 0xCF,
+ 0x98, 0x44, 0x38, 0x79, 0xA5, 0x54, 0xEC, 0xA6,
+ 0xB9, 0x6D, 0xF0, 0x61, 0xD0, 0x4A, 0x11, 0x7C,
+ 0x98, 0xAE, 0xEC, 0x1C, 0xDE, 0x1A, 0xFA, 0x9C,
+ 0xEF, 0x62, 0xDD, 0x68, 0x6D, 0xA9, 0x1B, 0xB2,
+ 0xB1, 0xF1, 0x23, 0x79, 0xBB, 0xDC, 0x9F, 0xA3,
+ 0x2A, 0x6B, 0x69, 0x98, 0xB7, 0x7E, 0x8E, 0xB0,
+ 0xB5, 0x05, 0x07, 0x86, 0x2A, 0xFA, 0x77, 0x99}
+};
+
+SHAKE256 shake256;
+TestHashVectorSHAKE tst;
+uint8_t output[MAX_SHAKE_OUTPUT];
+
+bool testSHAKE_N(SHAKE *shake, const struct TestHashVectorSHAKE *test, size_t inc, bool printName = false)
+{
+ size_t size;
+ size_t posn, len;
+
+ // Copy the test case out of program memory.
+ memcpy_P(&tst, test, sizeof(tst));
+ test = &tst;
+
+ // Print the test name if necessary.
+ if (printName) {
+ Serial.print(test->name);
+ Serial.print(" ... ");
+ }
+
+ // Hash the input data.
+ if (!inc)
+ inc = 1;
+ size = test->dataLen;
+ shake->reset();
+ for (posn = 0; posn < size; posn += inc) {
+ len = size - posn;
+ if (len > inc)
+ len = inc;
+ shake->update(test->data + posn, len);
+ }
+
+ // Generate data using extend() and check it.
+ size = MAX_SHAKE_OUTPUT;
+ for (posn = 0; posn < size; posn += inc) {
+ len = size - posn;
+ if (len > inc)
+ len = inc;
+ shake->extend(output + posn, len);
+ }
+ if (memcmp(output, test->hash, size) != 0)
+ return false;
+
+ // Restart and use encrypt() this time.
+ shake->reset();
+ shake->update(test->data, test->dataLen);
+ for (posn = 0; posn < size; ++posn)
+ output[posn] = (uint8_t)(posn + 0xAA);
+ for (posn = 0; posn < size; posn += inc) {
+ len = size - posn;
+ if (len > inc)
+ len = inc;
+ shake->encrypt(output + posn, output + posn, len);
+ }
+ for (posn = 0; posn < size; ++posn)
+ output[posn] ^= (uint8_t)(posn + 0xAA);
+ if (memcmp(output, test->hash, size) != 0)
+ return false;
+
+ return true;
+}
+
+void testSHAKE(SHAKE *shake, const struct TestHashVectorSHAKE *test)
+{
+ bool ok;
+ const char *str;
+ uint8_t ch;
+ size_t dataLen;
+
+ memcpy_P(&dataLen, &(test->dataLen), sizeof(size_t));
+
+ ok = testSHAKE_N(shake, test, dataLen, true);
+ ok &= testSHAKE_N(shake, test, 1);
+ ok &= testSHAKE_N(shake, test, 2);
+ ok &= testSHAKE_N(shake, test, 5);
+ ok &= testSHAKE_N(shake, test, 8);
+ ok &= testSHAKE_N(shake, test, 13);
+ ok &= testSHAKE_N(shake, test, 16);
+ ok &= testSHAKE_N(shake, test, 24);
+ ok &= testSHAKE_N(shake, test, 63);
+ ok &= testSHAKE_N(shake, test, 64);
+
+ if (ok)
+ Serial.println("Passed");
+ else
+ Serial.println("Failed");
+}
+
+void perfUpdate(SHAKE *shake)
+{
+ unsigned long start;
+ unsigned long elapsed;
+ int count;
+
+ Serial.print("Updating ... ");
+
+ for (size_t posn = 0; posn < MAX_SHAKE_OUTPUT; ++posn)
+ output[posn] = (uint8_t)posn;
+
+ shake->reset();
+ start = micros();
+ for (count = 0; count < 300; ++count) {
+ shake->update(output, MAX_SHAKE_OUTPUT);
+ }
+ shake->extend(output, 0); // Force a finalize after the update.
+ elapsed = micros() - start;
+
+ Serial.print(elapsed / (MAX_SHAKE_OUTPUT * 300.0));
+ Serial.print("us per byte, ");
+ Serial.print((MAX_SHAKE_OUTPUT * 300.0 * 1000000.0) / elapsed);
+ Serial.println(" bytes per second");
+}
+
+void perfExtend(SHAKE *shake)
+{
+ unsigned long start;
+ unsigned long elapsed;
+ int count;
+
+ Serial.print("Extending ... ");
+
+ for (size_t posn = 0; posn < MAX_SHAKE_OUTPUT; ++posn)
+ output[posn] = (uint8_t)posn;
+
+ shake->reset();
+ shake->update(output, MAX_SHAKE_OUTPUT);
+ shake->extend(output, 0); // Force a finalize after the update.
+ start = micros();
+ for (count = 0; count < 300; ++count) {
+ shake->extend(output, MAX_SHAKE_OUTPUT);
+ }
+ elapsed = micros() - start;
+
+ Serial.print(elapsed / (MAX_SHAKE_OUTPUT * 300.0));
+ Serial.print("us per byte, ");
+ Serial.print((MAX_SHAKE_OUTPUT * 300.0 * 1000000.0) / elapsed);
+ Serial.println(" bytes per second");
+}
+
+void perfEncrypt(SHAKE *shake)
+{
+ unsigned long start;
+ unsigned long elapsed;
+ int count;
+
+ Serial.print("Encrypting ... ");
+
+ for (size_t posn = 0; posn < MAX_SHAKE_OUTPUT; ++posn)
+ output[posn] = (uint8_t)posn;
+
+ shake->reset();
+ shake->update(output, MAX_SHAKE_OUTPUT);
+ shake->extend(output, 0); // Force a finalize after the update.
+ start = micros();
+ for (count = 0; count < 300; ++count) {
+ shake->encrypt(output, output, MAX_SHAKE_OUTPUT);
+ }
+ elapsed = micros() - start;
+
+ Serial.print(elapsed / (MAX_SHAKE_OUTPUT * 300.0));
+ Serial.print("us per byte, ");
+ Serial.print((MAX_SHAKE_OUTPUT * 300.0 * 1000000.0) / elapsed);
+ Serial.println(" bytes per second");
+}
+
+void setup()
+{
+ Serial.begin(9600);
+
+ Serial.println();
+
+ Serial.print("State Size ...");
+ Serial.println(sizeof(SHAKE256));
+ Serial.println();
+
+ Serial.println("Test Vectors:");
+ testSHAKE(&shake256, &testVectorSHAKE256_1);
+ testSHAKE(&shake256, &testVectorSHAKE256_2);
+ testSHAKE(&shake256, &testVectorSHAKE256_3);
+
+ Serial.println();
+
+ Serial.println("Performance Tests:");
+ perfUpdate(&shake256);
+ perfExtend(&shake256);
+ perfEncrypt(&shake256);
+}
+
+void loop()
+{
+}