1
0
mirror of https://github.com/taigrr/arduinolibs synced 2025-01-18 04:33:12 -08:00

237 lines
5.5 KiB
C++

/*
* Copyright (C) 2015 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 "SHA3.h"
#include "Crypto.h"
/**
* \class SHA3_256 SHA3.h <SHA3.h>
* \brief SHA3-256 hash algorithm.
*
* Reference: http://en.wikipedia.org/wiki/SHA-3
*
* \sa SHA3_512
*/
/**
* \brief Constructs a new SHA3-256 hash object.
*/
SHA3_256::SHA3_256()
{
core.setCapacity(512);
}
/**
* \brief Destroys this hash object after clearing sensitive information.
*/
SHA3_256::~SHA3_256()
{
// The destructor for the KeccakCore object will do most of the work.
}
size_t SHA3_256::hashSize() const
{
return 32;
}
size_t SHA3_256::blockSize() const
{
return core.blockSize();
}
void SHA3_256::reset()
{
core.reset();
}
void SHA3_256::update(const void *data, size_t len)
{
core.update(data, len);
}
void SHA3_256::finalize(void *hash, size_t len)
{
// Pad the final block and then extract the hash value.
core.pad(0x06);
core.extract(hash, len);
}
void SHA3_256::clear()
{
core.clear();
}
void SHA3_256::resetHMAC(const void *key, size_t keyLen)
{
core.setHMACKey(key, keyLen, 0x36, 32);
}
void SHA3_256::finalizeHMAC(const void *key, size_t keyLen, void *hash, size_t hashLen)
{
uint8_t temp[32];
finalize(temp, sizeof(temp));
core.setHMACKey(key, keyLen, 0x5C, 32);
core.update(temp, sizeof(temp));
finalize(hash, hashLen);
clean(temp);
}
/**
* \brief Copies the entire hash state from another object.
*
* \param other The other object to copy the state from.
*
* This function is intended for scenarios where the application needs to
* finalize the state to get an intermediate hash value, but must then
* continue hashing new data into the original state.
*
* In the following example, h1 will be the hash over data1 and h2 will
* be the hash over data1 concatenated with data2:
*
* \code
* // Hash the initial data.
* SHA3_256 hash1;
* hash1.update(data1, sizeof(data1));
*
* // Copy the hash state and finalize to create h1.
* SHA3_256 hash2;
* hash2.copyFrom(hash1);
* hash2.finalize(h1, sizeof(h1));
*
* // Continue adding data to the original unfinalized hash.
* hash1.update(data2, sizeof(data2));
*
* // Get the final hash value h2.
* hash1.finalize(h2, sizeof(h2));
* \endcode
*/
void SHA3_256::copyFrom(const SHA3_256 &other)
{
core.copyFrom(other.core);
}
/**
* \class SHA3_512 SHA3.h <SHA3.h>
* \brief SHA3-512 hash algorithm.
*
* Reference: http://en.wikipedia.org/wiki/SHA-3
*
* \sa SHA3_256
*/
/**
* \brief Constructs a new SHA3-512 hash object.
*/
SHA3_512::SHA3_512()
{
core.setCapacity(1024);
}
/**
* \brief Destroys this hash object after clearing sensitive information.
*/
SHA3_512::~SHA3_512()
{
// The destructor for the KeccakCore object will do most of the work.
}
size_t SHA3_512::hashSize() const
{
return 64;
}
size_t SHA3_512::blockSize() const
{
return core.blockSize();
}
void SHA3_512::reset()
{
core.reset();
}
void SHA3_512::update(const void *data, size_t len)
{
core.update(data, len);
}
void SHA3_512::finalize(void *hash, size_t len)
{
// Pad the final block and then extract the hash value.
core.pad(0x06);
core.extract(hash, len);
}
void SHA3_512::clear()
{
core.clear();
}
void SHA3_512::resetHMAC(const void *key, size_t keyLen)
{
core.setHMACKey(key, keyLen, 0x36, 64);
}
void SHA3_512::finalizeHMAC(const void *key, size_t keyLen, void *hash, size_t hashLen)
{
uint8_t temp[64];
finalize(temp, sizeof(temp));
core.setHMACKey(key, keyLen, 0x5C, 64);
core.update(temp, sizeof(temp));
finalize(hash, hashLen);
clean(temp);
}
/**
* \brief Copies the entire hash state from another object.
*
* \param other The other object to copy the state from.
*
* This function is intended for scenarios where the application needs to
* finalize the state to get an intermediate hash value, but must then
* continue hashing new data into the original state.
*
* In the following example, h1 will be the hash over data1 and h2 will
* be the hash over data1 concatenated with data2:
*
* \code
* // Hash the initial data.
* SHA3_512 hash1;
* hash1.update(data1, sizeof(data1));
*
* // Copy the hash state and finalize to create h1.
* SHA3_512 hash2;
* hash2.copyFrom(hash1);
* hash2.finalize(h1, sizeof(h1));
*
* // Continue adding data to the original unfinalized hash.
* hash1.update(data2, sizeof(data2));
*
* // Get the final hash value h2.
* hash1.finalize(h2, sizeof(h2));
* \endcode
*/
void SHA3_512::copyFrom(const SHA3_512 &other)
{
core.copyFrom(other.core);
}