Sha256 calculator for library use
The signing infrastructure now supports calculating sha256. This feature does not require signing support to be activated. Fixes #315
This commit is contained in:
@@ -98,6 +98,7 @@
|
|||||||
#define MY_SIGNING_FEATURE
|
#define MY_SIGNING_FEATURE
|
||||||
#endif
|
#endif
|
||||||
#include "core/MySigning.cpp"
|
#include "core/MySigning.cpp"
|
||||||
|
#include "drivers/ATSHA204/sha256.cpp"
|
||||||
#if defined(MY_SIGNING_FEATURE)
|
#if defined(MY_SIGNING_FEATURE)
|
||||||
// SIGNING COMMON FUNCTIONS
|
// SIGNING COMMON FUNCTIONS
|
||||||
#if defined(MY_SIGNING_ATSHA204) && defined(MY_SIGNING_SOFT)
|
#if defined(MY_SIGNING_ATSHA204) && defined(MY_SIGNING_SOFT)
|
||||||
@@ -109,7 +110,6 @@
|
|||||||
#include "drivers/ATSHA204/ATSHA204.cpp"
|
#include "drivers/ATSHA204/ATSHA204.cpp"
|
||||||
#elif defined(MY_SIGNING_SOFT)
|
#elif defined(MY_SIGNING_SOFT)
|
||||||
#include "core/MySigningAtsha204Soft.cpp"
|
#include "core/MySigningAtsha204Soft.cpp"
|
||||||
#include "drivers/ATSHA204/sha256.cpp"
|
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
@@ -18,6 +18,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "MySigning.h"
|
#include "MySigning.h"
|
||||||
|
#include "drivers/ATSHA204/sha256.h"
|
||||||
|
|
||||||
#if defined(MY_DEBUG_VERBOSE)
|
#if defined(MY_DEBUG_VERBOSE)
|
||||||
#define SIGN_DEBUG(x,...) debug(x, ##__VA_ARGS__)
|
#define SIGN_DEBUG(x,...) debug(x, ##__VA_ARGS__)
|
||||||
@@ -308,3 +309,22 @@ bool signerVerifyMsg(MyMessage &msg) {
|
|||||||
#endif // MY_SIGNING_REQUEST_SIGNATURES
|
#endif // MY_SIGNING_REQUEST_SIGNATURES
|
||||||
return verificationResult;
|
return verificationResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static uint8_t sha256_hash[32];
|
||||||
|
Sha256Class _soft_sha256;
|
||||||
|
|
||||||
|
void signerSha256Init(void) {
|
||||||
|
memset(sha256_hash, 0, 32);
|
||||||
|
_soft_sha256.init();
|
||||||
|
}
|
||||||
|
|
||||||
|
void signerSha256Update(const uint8_t* data, size_t sz) {
|
||||||
|
for (size_t i = 0; i < sz; i++) {
|
||||||
|
_soft_sha256.write(data[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t* signerSha256Final(void) {
|
||||||
|
memcpy(sha256_hash, _soft_sha256.result(), 32);
|
||||||
|
return sha256_hash;
|
||||||
|
}
|
||||||
|
|||||||
@@ -581,5 +581,32 @@ bool signerSignMsg(MyMessage &msg);
|
|||||||
*/
|
*/
|
||||||
bool signerVerifyMsg(MyMessage &msg);
|
bool signerVerifyMsg(MyMessage &msg);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Initialize a hash calculation session.
|
||||||
|
*
|
||||||
|
* Any ongoing calculation session will be terminated/reset.
|
||||||
|
*/
|
||||||
|
void signerSha256Init(void);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Add data to an ongoing hash calculation session.
|
||||||
|
*
|
||||||
|
* More data can be added by repeated calls to this function.
|
||||||
|
*
|
||||||
|
* @param data Buffer with data to add.
|
||||||
|
* @param sz Size of data buffer.
|
||||||
|
*/
|
||||||
|
void signerSha256Update(const uint8_t* data, size_t sz);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Finalize an ongong hash calculation session and return the hash.
|
||||||
|
*
|
||||||
|
* The returned hash size is always 32 bytes. Buffer can be assumed to be valid
|
||||||
|
* until @ref signerSha256Init() is called again.
|
||||||
|
*
|
||||||
|
* @returns Buffer with 32-byte hash.
|
||||||
|
*/
|
||||||
|
uint8_t* signerSha256Final(void);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
/** @}*/
|
/** @}*/
|
||||||
@@ -23,7 +23,7 @@ class Sha256Class
|
|||||||
void initHmac(const uint8_t* secret, int secretLength);
|
void initHmac(const uint8_t* secret, int secretLength);
|
||||||
uint8_t* result(void);
|
uint8_t* result(void);
|
||||||
uint8_t* resultHmac(void);
|
uint8_t* resultHmac(void);
|
||||||
virtual void write(uint8_t);
|
void write(uint8_t);
|
||||||
private:
|
private:
|
||||||
void pad();
|
void pad();
|
||||||
void addUncounted(uint8_t data);
|
void addUncounted(uint8_t data);
|
||||||
|
|||||||
Reference in New Issue
Block a user