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

Copying hash states to allow obtaining intermediate hash values

This commit is contained in:
Rhys Weatherley 2018-06-20 19:41:47 +10:00
parent e4b90184fd
commit 3e3e90b19e
14 changed files with 269 additions and 0 deletions

View File

@ -255,6 +255,40 @@ void BLAKE2b::finalizeHMAC(const void *key, size_t keyLen, void *hash, size_t ha
clean(temp); 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.
* BLAKE2b hash1;
* hash1.update(data1, sizeof(data1));
*
* // Copy the hash state and finalize to create h1.
* BLAKE2b 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 BLAKE2b::copyFrom(const BLAKE2b &other)
{
state = other.state;
}
// Permutation on the message input state for BLAKE2b. // Permutation on the message input state for BLAKE2b.
static const uint8_t sigma[12][16] PROGMEM = { static const uint8_t sigma[12][16] PROGMEM = {
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},

View File

@ -46,6 +46,8 @@ public:
void resetHMAC(const void *key, size_t keyLen); void resetHMAC(const void *key, size_t keyLen);
void finalizeHMAC(const void *key, size_t keyLen, void *hash, size_t hashLen); void finalizeHMAC(const void *key, size_t keyLen, void *hash, size_t hashLen);
void copyFrom(const BLAKE2b &other);
private: private:
struct { struct {
uint64_t h[8]; uint64_t h[8];

View File

@ -249,6 +249,40 @@ void BLAKE2s::finalizeHMAC(const void *key, size_t keyLen, void *hash, size_t ha
clean(temp); 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.
* BLAKE2s hash1;
* hash1.update(data1, sizeof(data1));
*
* // Copy the hash state and finalize to create h1.
* BLAKE2s 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 BLAKE2s::copyFrom(const BLAKE2s &other)
{
state = other.state;
}
// Permutation on the message input state for BLAKE2s. // Permutation on the message input state for BLAKE2s.
static const uint8_t sigma[10][16] PROGMEM = { static const uint8_t sigma[10][16] PROGMEM = {
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},

View File

@ -46,6 +46,8 @@ public:
void resetHMAC(const void *key, size_t keyLen); void resetHMAC(const void *key, size_t keyLen);
void finalizeHMAC(const void *key, size_t keyLen, void *hash, size_t hashLen); void finalizeHMAC(const void *key, size_t keyLen, void *hash, size_t hashLen);
void copyFrom(const BLAKE2s &other);
private: private:
struct { struct {
uint32_t h[8]; uint32_t h[8];

View File

@ -318,6 +318,21 @@ void KeccakCore::setHMACKey(const void *key, size_t len, uint8_t pad, size_t has
keccakp(); keccakp();
} }
/**
* \brief Copies the entire Keccak 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.
*/
void KeccakCore::copyFrom(const KeccakCore &other)
{
state = other.state;
_blockSize = other._blockSize;
}
/** /**
* \brief Transform the state with the KECCAK-p sponge function with b = 1600. * \brief Transform the state with the KECCAK-p sponge function with b = 1600.
*/ */

View File

@ -49,6 +49,8 @@ public:
void setHMACKey(const void *key, size_t len, uint8_t pad, size_t hashSize); void setHMACKey(const void *key, size_t len, uint8_t pad, size_t hashSize);
void copyFrom(const KeccakCore &other);
private: private:
struct { struct {
uint64_t A[5][5]; uint64_t A[5][5];

View File

@ -155,6 +155,40 @@ void SHA256::finalizeHMAC(const void *key, size_t keyLen, void *hash, size_t has
clean(temp); 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.
* SHA256 hash1;
* hash1.update(data1, sizeof(data1));
*
* // Copy the hash state and finalize to create h1.
* SHA256 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 SHA256::copyFrom(const SHA256 &other)
{
state = other.state;
}
/** /**
* \brief Processes a single 512-bit chunk with the core SHA-256 algorithm. * \brief Processes a single 512-bit chunk with the core SHA-256 algorithm.
* *

View File

@ -43,6 +43,8 @@ public:
void resetHMAC(const void *key, size_t keyLen); void resetHMAC(const void *key, size_t keyLen);
void finalizeHMAC(const void *key, size_t keyLen, void *hash, size_t hashLen); void finalizeHMAC(const void *key, size_t keyLen, void *hash, size_t hashLen);
void copyFrom(const SHA256 &other);
private: private:
struct { struct {
uint32_t h[8]; uint32_t h[8];

View File

@ -95,6 +95,40 @@ void SHA3_256::finalizeHMAC(const void *key, size_t keyLen, void *hash, size_t h
clean(temp); 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> * \class SHA3_512 SHA3.h <SHA3.h>
* \brief SHA3-512 hash algorithm. * \brief SHA3-512 hash algorithm.
@ -166,3 +200,37 @@ void SHA3_512::finalizeHMAC(const void *key, size_t keyLen, void *hash, size_t h
finalize(hash, hashLen); finalize(hash, hashLen);
clean(temp); 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);
}

View File

@ -44,6 +44,8 @@ public:
void resetHMAC(const void *key, size_t keyLen); void resetHMAC(const void *key, size_t keyLen);
void finalizeHMAC(const void *key, size_t keyLen, void *hash, size_t hashLen); void finalizeHMAC(const void *key, size_t keyLen, void *hash, size_t hashLen);
void copyFrom(const SHA3_256 &other);
private: private:
KeccakCore core; KeccakCore core;
}; };
@ -66,6 +68,8 @@ public:
void resetHMAC(const void *key, size_t keyLen); void resetHMAC(const void *key, size_t keyLen);
void finalizeHMAC(const void *key, size_t keyLen, void *hash, size_t hashLen); void finalizeHMAC(const void *key, size_t keyLen, void *hash, size_t hashLen);
void copyFrom(const SHA3_512 &other);
private: private:
KeccakCore core; KeccakCore core;
}; };

View File

@ -158,6 +158,40 @@ void SHA512::finalizeHMAC(const void *key, size_t keyLen, void *hash, size_t has
clean(temp); 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.
* SHA512 hash1;
* hash1.update(data1, sizeof(data1));
*
* // Copy the hash state and finalize to create h1.
* SHA512 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 SHA512::copyFrom(const SHA512 &other)
{
state = other.state;
}
/** /**
* \brief Processes a single 1024-bit chunk with the core SHA-512 algorithm. * \brief Processes a single 1024-bit chunk with the core SHA-512 algorithm.
* *

View File

@ -45,6 +45,8 @@ public:
void resetHMAC(const void *key, size_t keyLen); void resetHMAC(const void *key, size_t keyLen);
void finalizeHMAC(const void *key, size_t keyLen, void *hash, size_t hashLen); void finalizeHMAC(const void *key, size_t keyLen, void *hash, size_t hashLen);
void copyFrom(const SHA512 &other);
private: private:
struct { struct {
uint64_t h[8]; uint64_t h[8];

View File

@ -150,6 +150,40 @@ void SHA1::finalizeHMAC(const void *key, size_t keyLen, void *hash, size_t hashL
clean(temp); 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.
* SHA1 hash1;
* hash1.update(data1, sizeof(data1));
*
* // Copy the hash state and finalize to create h1.
* SHA1 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 SHA1::copyFrom(const SHA1 &other)
{
state = other.state;
}
/** /**
* \brief Processes a single 512-bit chunk with the core SHA-1 algorithm. * \brief Processes a single 512-bit chunk with the core SHA-1 algorithm.
* *

View File

@ -43,6 +43,8 @@ public:
void resetHMAC(const void *key, size_t keyLen); void resetHMAC(const void *key, size_t keyLen);
void finalizeHMAC(const void *key, size_t keyLen, void *hash, size_t hashLen); void finalizeHMAC(const void *key, size_t keyLen, void *hash, size_t hashLen);
void copyFrom(const SHA1 &other);
private: private:
struct { struct {
uint32_t h[5]; uint32_t h[5];