* Sha256 driver cosmetics cosmetic cleanups: - use actual define that was used to allocate the array - remove unused file globals * Save memory by eliminating sha256_hash array eliminated sha256_hash array to save memory. Instead pass the Sha256Class result pointer. There seems to be no use for the extra sha256_hash array as it was zeroed exactly at the same time, the Sha256Class result was invalidated (by calling signerSha256Init) If the result needs to stay around while another sha is calculated, it's better to have the user taking care of this. * Moved hmac methods and buffers to separate class This allows saving the buffer memory if no hmac is needed ... eg. with the public Signing API.
49 lines
941 B
C++
49 lines
941 B
C++
// This code is taken from https://github.com/Cathedrow/Cryptosuite (Peter Knight) and is lightly modified for MySensors use
|
|
#ifndef Sha256_h
|
|
#define Sha256_h
|
|
#if !DOXYGEN
|
|
#include <inttypes.h>
|
|
|
|
#define HASH_LENGTH 32
|
|
#define BLOCK_LENGTH 64
|
|
|
|
union _buffer {
|
|
uint8_t b[BLOCK_LENGTH];
|
|
uint32_t w[BLOCK_LENGTH/4];
|
|
};
|
|
union _state {
|
|
uint8_t b[HASH_LENGTH];
|
|
uint32_t w[HASH_LENGTH/4];
|
|
};
|
|
|
|
class Sha256Class
|
|
{
|
|
public:
|
|
Sha256Class(); // Constructor
|
|
void init(void);
|
|
uint8_t* result(void);
|
|
void write(uint8_t);
|
|
private:
|
|
void pad();
|
|
void addUncounted(uint8_t data);
|
|
void hashBlock();
|
|
uint32_t ror32(uint32_t number, uint8_t bits);
|
|
_buffer buffer;
|
|
uint8_t bufferOffset;
|
|
_state state;
|
|
uint32_t byteCount;
|
|
};
|
|
|
|
class HmacClass : public Sha256Class
|
|
{
|
|
public:
|
|
void initHmac(const uint8_t* secret, int secretLength);
|
|
uint8_t* resultHmac(void);
|
|
private:
|
|
uint8_t keyBuffer[BLOCK_LENGTH];
|
|
uint8_t innerHash[HASH_LENGTH];
|
|
};
|
|
|
|
#endif
|
|
#endif
|