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:
Patrick Fallberg
2016-01-12 08:41:54 +01:00
parent 02fbe536d4
commit 5ecf146f15
4 changed files with 49 additions and 2 deletions
+20
View File
@@ -18,6 +18,7 @@
*/
#include "MySigning.h"
#include "drivers/ATSHA204/sha256.h"
#if defined(MY_DEBUG_VERBOSE)
#define SIGN_DEBUG(x,...) debug(x, ##__VA_ARGS__)
@@ -308,3 +309,22 @@ bool signerVerifyMsg(MyMessage &msg) {
#endif // MY_SIGNING_REQUEST_SIGNATURES
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;
}
+27
View File
@@ -581,5 +581,32 @@ bool signerSignMsg(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
/** @}*/