Signing improvements package (#871)
* Signing support with MAX_PAYLOAD>32 Support in the signing backends to handle configurations where MAX_PAYLOAD is more than 32 bytes. Fixes #748 * Support big message signatures Support for signing messages larger than 32 bytes. This is not fully testable with the current version of the library but the change would add theoretical support for signing any sized messages. Fixes #749 * Have gw properly indicate whitelisting preferences If the gateway hold a signing whitelist, inform all nodes of this requirement and not just nodes that hold whitelists themselves. Fixes #806 * Harden security The flag MY_SIGNING_GW_REQUEST_SIGNATURES_FROM_ALL has been removed. Gateway will now request signatures from everyone by default. In addition to this, no nodes in the network will allow clearing of signing/whitelisting preferences by OTA commands by default. If the old behaviour is needed (suitable for gradual signing roll out and development purposes) a new flag, MY_SIGNING_WEAK_SECURITY can be set. Fixes #807 * Signing debug messages rewritten All signing related debug has been rewritten to better match other core debug printouts. Also, backends are updated to have a smaller implementation delta. * Simplified signing option implemented Enable by MY_SIGNING_SIMPLE_PASSWD. Signing, and signing requirements will be enabled, as will encryption. Whitelisting is optional. The value provided to MY_SIGNING_SIMPLE_PASSWD is used as HMAC and AES key. Whitelists use the first 8 bytes of the password and the ninth byte from the node ID as serial. Password is required to be at least 8 characters wide. It is zero padded if it is not long enough to cover the AES (16 bytes) and HMAC (32 bytes) keys. It is not recommended to have a password shorter than 32 bytes, and there is no need for a password longer than 32 bytes. * Add documentation section for signing debug * Add nRF5 encryption to simple signing option * Restructure MySigning documentation * Minor fixes for Linux
This commit is contained in:
committed by
Patrick Fallberg
parent
4c0661e410
commit
28c4f3f19e
+165
-130
@@ -1,4 +1,4 @@
|
||||
/**
|
||||
/*
|
||||
* The MySensors Arduino library handles the wireless radio link and protocol
|
||||
* between your home built sensors/actuators and HA controller of choice.
|
||||
* The sensors forms a self healing radio network with optional repeaters. Each
|
||||
@@ -23,44 +23,66 @@
|
||||
* ATSHA204 emulated signing backend. The emulated ATSHA204 implementation offers pseudo random
|
||||
* number generation and HMAC-SHA256 authentication compatible with a "physical" ATSHA204.
|
||||
* NOTE: Key is stored in clear text in the Arduino firmware. Therefore, the use of this back-end
|
||||
* could compromise the key used in the signed message infrastructure if device is lost and its memory
|
||||
* dumped.
|
||||
* could compromise the key used in the signed message infrastructure if device is lost and its
|
||||
* memory dumped.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "MySigning.h"
|
||||
#include "drivers/ATSHA204/sha256.h"
|
||||
|
||||
#ifdef MY_SIGNING_SOFT
|
||||
#include "drivers/ATSHA204/sha256.h"
|
||||
#define SIGNING_IDENTIFIER (1) //HMAC-SHA256
|
||||
|
||||
#if defined(MY_DEBUG_VERBOSE_SIGNING)
|
||||
#define SIGN_DEBUG(x,...) hwDebugPrint(x, ##__VA_ARGS__)
|
||||
static char printStr[65];
|
||||
static char i2h(uint8_t i)
|
||||
{
|
||||
uint8_t k = i & 0x0F;
|
||||
if (k <= 9) {
|
||||
return '0' + k;
|
||||
} else {
|
||||
return 'A' + k - 10;
|
||||
}
|
||||
}
|
||||
|
||||
static void buf2str(const uint8_t* buf, size_t sz)
|
||||
{
|
||||
uint8_t i;
|
||||
if (sz > 32) {
|
||||
sz = 32; //clamp to 32 bytes
|
||||
}
|
||||
for (i = 0; i < sz; i++) {
|
||||
printStr[i * 2] = i2h(buf[i] >> 4);
|
||||
printStr[(i * 2) + 1] = i2h(buf[i]);
|
||||
}
|
||||
printStr[sz * 2] = '\0';
|
||||
}
|
||||
#else
|
||||
#define SIGN_DEBUG(x,...)
|
||||
#endif
|
||||
|
||||
// Define MY_DEBUG_VERBOSE_SIGNING in your sketch to enable signing backend debugprints
|
||||
|
||||
Sha256Class _signing_sha256;
|
||||
unsigned long _signing_timestamp;
|
||||
bool _signing_verification_ongoing = false;
|
||||
uint8_t _signing_verifying_nonce[32];
|
||||
uint8_t _signing_signing_nonce[32];
|
||||
uint8_t _signing_temp_message[32];
|
||||
static unsigned long _signing_timestamp;
|
||||
static bool _signing_verification_ongoing = false;
|
||||
static uint8_t _signing_verifying_nonce[32+9+1];
|
||||
static uint8_t _signing_signing_nonce[32+9+1];
|
||||
static uint8_t _signing_temp_message[32];
|
||||
static uint8_t _signing_hmac_key[32];
|
||||
uint8_t _signing_hmac[32];
|
||||
extern uint8_t _doWhitelist[32];
|
||||
|
||||
static uint8_t _signing_hmac[32];
|
||||
static uint8_t _signing_node_serial_info[9];
|
||||
#ifdef MY_SIGNING_NODE_WHITELISTING
|
||||
const whitelist_entry_t _signing_whitelist[] = MY_SIGNING_NODE_WHITELISTING;
|
||||
static const whitelist_entry_t _signing_whitelist[] = MY_SIGNING_NODE_WHITELISTING;
|
||||
#endif
|
||||
|
||||
static void signerCalculateSignature(MyMessage &msg, bool signing);
|
||||
|
||||
static bool init_ok = false;
|
||||
static const uint8_t reset_serial[9] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
|
||||
|
||||
static void signerCalculateSignature(MyMessage &msg, bool signing);
|
||||
static uint8_t* signerAtsha204AHmac(const uint8_t* nonce, const uint8_t* data);
|
||||
static uint8_t* signerSha256(const uint8_t* data, size_t sz);
|
||||
|
||||
bool signerAtsha204SoftInit(void)
|
||||
{
|
||||
init_ok = true;
|
||||
@@ -68,8 +90,22 @@ bool signerAtsha204SoftInit(void)
|
||||
// initialize pseudo-RNG
|
||||
hwRandomNumberInit();
|
||||
// Set secrets
|
||||
#ifdef MY_SIGNING_SIMPLE_PASSWD
|
||||
if (strnlen(MY_SIGNING_SIMPLE_PASSWD, 32) < 8) {
|
||||
SIGN_DEBUG(PSTR("!SGN:BND:PWD<8\n")); //Password is too short to be acceptable
|
||||
init_ok = false;
|
||||
} else {
|
||||
memset(_signing_hmac_key, 0, 32);
|
||||
memcpy(_signing_hmac_key, MY_SIGNING_SIMPLE_PASSWD, strnlen(MY_SIGNING_SIMPLE_PASSWD, 32));
|
||||
memset(_signing_node_serial_info, 0, 9);
|
||||
memcpy(_signing_node_serial_info, MY_SIGNING_SIMPLE_PASSWD,
|
||||
strnlen(MY_SIGNING_SIMPLE_PASSWD, 8));
|
||||
_signing_node_serial_info[8] = getNodeId();
|
||||
}
|
||||
#else
|
||||
hwReadConfigBlock((void*)_signing_hmac_key, (void*)EEPROM_SIGNING_SOFT_HMAC_KEY_ADDRESS, 32);
|
||||
hwReadConfigBlock((void*)_signing_node_serial_info, (void*)EEPROM_SIGNING_SOFT_SERIAL_ADDRESS, 9);
|
||||
#endif
|
||||
if (!memcmp(_signing_node_serial_info, reset_serial, 9)) {
|
||||
unique_id_t uniqueID;
|
||||
// There is no serial, attempt to get unique ID for serial instead
|
||||
@@ -84,13 +120,19 @@ bool signerAtsha204SoftInit(void)
|
||||
bool signerAtsha204SoftCheckTimer(void)
|
||||
{
|
||||
if (!init_ok) {
|
||||
SIGN_DEBUG(PSTR("Backend not initialized\n"));
|
||||
return false;
|
||||
}
|
||||
if (_signing_verification_ongoing) {
|
||||
if (hwMillis() < _signing_timestamp ||
|
||||
hwMillis() > _signing_timestamp + MY_VERIFICATION_TIMEOUT_MS) {
|
||||
SIGN_DEBUG(PSTR("Verification timeout\n"));
|
||||
unsigned long time_now = hwMillis();
|
||||
// If timestamp is taken so late a rollover can take place during the timeout,
|
||||
// offset both timestamp and current time to make sure no rollover takes place during the
|
||||
// timeout
|
||||
if (_signing_timestamp + MY_VERIFICATION_TIMEOUT_MS < _signing_timestamp) {
|
||||
_signing_timestamp += MY_VERIFICATION_TIMEOUT_MS;
|
||||
time_now += MY_VERIFICATION_TIMEOUT_MS;
|
||||
}
|
||||
if (time_now > _signing_timestamp + MY_VERIFICATION_TIMEOUT_MS) {
|
||||
SIGN_DEBUG(PSTR("!SGN:BND:TMR\n")); //Verification timeout
|
||||
// Purge nonce
|
||||
memset(_signing_signing_nonce, 0xAA, 32);
|
||||
memset(_signing_verifying_nonce, 0xAA, 32);
|
||||
@@ -104,29 +146,29 @@ bool signerAtsha204SoftCheckTimer(void)
|
||||
bool signerAtsha204SoftGetNonce(MyMessage &msg)
|
||||
{
|
||||
if (!init_ok) {
|
||||
SIGN_DEBUG(PSTR("Backend not initialized\n"));
|
||||
return false;
|
||||
}
|
||||
SIGN_DEBUG(PSTR("Signing backend: ATSHA204Soft\n"));
|
||||
|
||||
#ifdef MY_HW_HAS_GETRANDOM
|
||||
// Try to get MAX_PAYLOAD random bytes
|
||||
while (hwGetentropy(&_signing_verifying_nonce, MAX_PAYLOAD) != MAX_PAYLOAD);
|
||||
// Try to get MAX_PAYLOAD (or 32) random bytes
|
||||
while (hwGetentropy(&_signing_verifying_nonce, MIN(MAX_PAYLOAD, 32)) != MIN(MAX_PAYLOAD, 32));
|
||||
#else
|
||||
// We used a basic whitening technique that XORs a random byte with the current hwMillis() counter and then the byte is
|
||||
// hashed (SHA256) to produce the resulting nonce
|
||||
// We used a basic whitening technique that XORs a random byte with the current hwMillis() counter
|
||||
// and then the byte is hashed (SHA256) to produce the resulting nonce
|
||||
_signing_sha256.init();
|
||||
for (int i = 0; i < 32; i++) {
|
||||
_signing_sha256.write(random(256) ^ (hwMillis()&0xFF));
|
||||
}
|
||||
memcpy(_signing_verifying_nonce, _signing_sha256.result(), MAX_PAYLOAD);
|
||||
memcpy(_signing_verifying_nonce, _signing_sha256.result(), MIN(MAX_PAYLOAD, 32));
|
||||
#endif
|
||||
|
||||
// We set the part of the 32-byte nonce that does not fit into a message to 0xAA
|
||||
memset(&_signing_verifying_nonce[MAX_PAYLOAD], 0xAA, sizeof(_signing_verifying_nonce)-MAX_PAYLOAD);
|
||||
if (MAX_PAYLOAD < 32) {
|
||||
// We set the part of the 32-byte nonce that does not fit into a message to 0xAA
|
||||
memset(&_signing_verifying_nonce[MAX_PAYLOAD], 0xAA, 32-MAX_PAYLOAD);
|
||||
}
|
||||
|
||||
// Transfer the first part of the nonce to the message
|
||||
msg.set(_signing_verifying_nonce, MAX_PAYLOAD);
|
||||
msg.set(_signing_verifying_nonce, MIN(MAX_PAYLOAD, 32));
|
||||
_signing_verification_ongoing = true;
|
||||
_signing_timestamp = hwMillis(); // Set timestamp to determine when to purge nonce
|
||||
// Be a little fancy to handle turnover (prolong the time allowed to timeout after turnover)
|
||||
@@ -141,20 +183,21 @@ bool signerAtsha204SoftGetNonce(MyMessage &msg)
|
||||
void signerAtsha204SoftPutNonce(MyMessage &msg)
|
||||
{
|
||||
if (!init_ok) {
|
||||
SIGN_DEBUG(PSTR("Backend not initialized\n"));
|
||||
return;
|
||||
}
|
||||
SIGN_DEBUG(PSTR("Signing backend: ATSHA204Soft\n"));
|
||||
|
||||
memcpy(_signing_signing_nonce, (uint8_t*)msg.getCustom(), MAX_PAYLOAD);
|
||||
// We set the part of the 32-byte nonce that does not fit into a message to 0xAA
|
||||
memset(&_signing_signing_nonce[MAX_PAYLOAD], 0xAA, sizeof(_signing_signing_nonce)-MAX_PAYLOAD);
|
||||
memcpy(_signing_signing_nonce, (uint8_t*)msg.getCustom(), MIN(MAX_PAYLOAD, 32));
|
||||
if (MAX_PAYLOAD < 32) {
|
||||
// We set the part of the 32-byte nonce that does not fit into a message to 0xAA
|
||||
memset(&_signing_signing_nonce[MAX_PAYLOAD], 0xAA, 32-MAX_PAYLOAD);
|
||||
}
|
||||
}
|
||||
|
||||
bool signerAtsha204SoftSignMsg(MyMessage &msg)
|
||||
{
|
||||
// If we cannot fit any signature in the message, refuse to sign it
|
||||
if (mGetLength(msg) > MAX_PAYLOAD-2) {
|
||||
SIGN_DEBUG(PSTR("Message too large\n"));
|
||||
SIGN_DEBUG(PSTR("!SGN:BND:SIG,SIZE,%d>%d\n"), mGetLength(msg), MAX_PAYLOAD-2); //Message too large
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -163,27 +206,24 @@ bool signerAtsha204SoftSignMsg(MyMessage &msg)
|
||||
signerCalculateSignature(msg, true);
|
||||
|
||||
if (DO_WHITELIST(msg.destination)) {
|
||||
// Salt the signature with the senders nodeId and the (hopefully) unique serial The Creator has provided
|
||||
_signing_sha256.init();
|
||||
for (int i=0; i<32; i++) {
|
||||
_signing_sha256.write(_signing_hmac[i]);
|
||||
}
|
||||
_signing_sha256.write(msg.sender);
|
||||
for (int i=0; i<SHA204_SERIAL_SZ; i++) {
|
||||
_signing_sha256.write(_signing_node_serial_info[i]);
|
||||
}
|
||||
memcpy(_signing_hmac, _signing_sha256.result(), 32);
|
||||
SIGN_DEBUG(PSTR("Signature salted with serial: %02X%02X%02X%02X%02X%02X%02X%02X%02X\n"),
|
||||
_signing_node_serial_info[0], _signing_node_serial_info[1], _signing_node_serial_info[2],
|
||||
_signing_node_serial_info[3], _signing_node_serial_info[4], _signing_node_serial_info[5],
|
||||
_signing_node_serial_info[6], _signing_node_serial_info[7], _signing_node_serial_info[8]);
|
||||
// Salt the signature with the senders nodeId and the (hopefully) unique serial The Creator has
|
||||
// provided. We can reuse the nonce buffer now since it is no longer needed
|
||||
memcpy(_signing_signing_nonce, _signing_hmac, 32);
|
||||
_signing_signing_nonce[32] = msg.sender;
|
||||
memcpy(&_signing_signing_nonce[33], _signing_node_serial_info, 9);
|
||||
memcpy(_signing_hmac, signerSha256(_signing_signing_nonce, 32+1+9), 32);
|
||||
SIGN_DEBUG(PSTR("SGN:BND:SIG WHI,ID=%d\n"), msg.sender);
|
||||
#ifdef MY_DEBUG_VERBOSE_SIGNING
|
||||
buf2str(_signing_node_serial_info, 9);
|
||||
SIGN_DEBUG(PSTR("SGN:BND:SIG WHI,SERIAL=%s\n"), printStr);
|
||||
#endif
|
||||
}
|
||||
|
||||
// Overwrite the first byte in the signature with the signing identifier
|
||||
_signing_hmac[0] = SIGNING_IDENTIFIER;
|
||||
|
||||
// Transfer as much signature data as the remaining space in the message permits
|
||||
memcpy(&msg.data[mGetLength(msg)], _signing_hmac, MAX_PAYLOAD-mGetLength(msg));
|
||||
memcpy(&msg.data[mGetLength(msg)], _signing_hmac, MIN(MAX_PAYLOAD-mGetLength(msg), 32));
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -191,7 +231,7 @@ bool signerAtsha204SoftSignMsg(MyMessage &msg)
|
||||
bool signerAtsha204SoftVerifyMsg(MyMessage &msg)
|
||||
{
|
||||
if (!_signing_verification_ongoing) {
|
||||
SIGN_DEBUG(PSTR("No active verification session\n"));
|
||||
SIGN_DEBUG(PSTR("!SGN:BND:VER ONGOING\n"));
|
||||
return false;
|
||||
} else {
|
||||
// Make sure we have not expired
|
||||
@@ -202,7 +242,7 @@ bool signerAtsha204SoftVerifyMsg(MyMessage &msg)
|
||||
_signing_verification_ongoing = false;
|
||||
|
||||
if (msg.data[mGetLength(msg)] != SIGNING_IDENTIFIER) {
|
||||
SIGN_DEBUG(PSTR("Incorrect signing identifier\n"));
|
||||
SIGN_DEBUG(PSTR("!SGN:BND:VER,IDENT=%d\n"), msg.data[mGetLength(msg)]);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -213,21 +253,21 @@ bool signerAtsha204SoftVerifyMsg(MyMessage &msg)
|
||||
size_t j;
|
||||
for (j=0; j < NUM_OF(_signing_whitelist); j++) {
|
||||
if (_signing_whitelist[j].nodeId == msg.sender) {
|
||||
SIGN_DEBUG(PSTR("Sender found in whitelist\n"));
|
||||
_signing_sha256.init();
|
||||
for (int i=0; i<32; i++) {
|
||||
_signing_sha256.write(_signing_hmac[i]);
|
||||
}
|
||||
_signing_sha256.write(msg.sender);
|
||||
for (int i=0; i<SHA204_SERIAL_SZ; i++) {
|
||||
_signing_sha256.write(_signing_whitelist[j].serial[i]);
|
||||
}
|
||||
memcpy(_signing_hmac, _signing_sha256.result(), 32);
|
||||
// We can reuse the nonce buffer now since it is no longer needed
|
||||
memcpy(_signing_verifying_nonce, _signing_hmac, 32);
|
||||
_signing_verifying_nonce[32] = msg.sender;
|
||||
memcpy(&_signing_verifying_nonce[33], _signing_whitelist[j].serial, 9);
|
||||
memcpy(_signing_hmac, signerSha256(_signing_verifying_nonce, 32+1+9), 32);
|
||||
SIGN_DEBUG(PSTR("SGN:BND:VER WHI,ID=%d\n"), msg.sender);
|
||||
#ifdef MY_DEBUG_VERBOSE_SIGNING
|
||||
buf2str(_signing_whitelist[j].serial, 9);
|
||||
SIGN_DEBUG(PSTR("SGN:BND:VER WHI,SERIAL=%s\n"), printStr);
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (j == NUM_OF(_signing_whitelist)) {
|
||||
SIGN_DEBUG(PSTR("Sender not found in whitelist, message rejected!\n"));
|
||||
SIGN_DEBUG(PSTR("!SGN:BND:VER WHI,ID=%d MISSING\n"), msg.sender);
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
@@ -236,62 +276,58 @@ bool signerAtsha204SoftVerifyMsg(MyMessage &msg)
|
||||
_signing_hmac[0] = SIGNING_IDENTIFIER;
|
||||
|
||||
// Compare the caluclated signature with the provided signature
|
||||
if (signerMemcmp(&msg.data[mGetLength(msg)], _signing_hmac, MAX_PAYLOAD-mGetLength(msg))) {
|
||||
SIGN_DEBUG(PSTR("Signature bad\n"));
|
||||
#ifdef MY_SIGNING_NODE_WHITELISTING
|
||||
SIGN_DEBUG(PSTR("Is the sender whitelisted and serial correct?\n"));
|
||||
#endif
|
||||
if (signerMemcmp(&msg.data[mGetLength(msg)], _signing_hmac,
|
||||
MIN(MAX_PAYLOAD-mGetLength(msg), 32))) {
|
||||
return false;
|
||||
} else {
|
||||
SIGN_DEBUG(PSTR("Signature OK\n"));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Helper to calculate signature of msg (returned in hmac)
|
||||
// Helper to calculate signature of msg (returned in _signing_hmac)
|
||||
static void signerCalculateSignature(MyMessage &msg, bool signing)
|
||||
{
|
||||
memset(_signing_temp_message, 0, 32);
|
||||
memcpy(_signing_temp_message, (uint8_t*)&msg.data[1-HEADER_SIZE],
|
||||
MAX_MESSAGE_LENGTH-1-(MAX_PAYLOAD-mGetLength(msg)));
|
||||
SIGN_DEBUG(PSTR("Current nonce: "
|
||||
"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X"
|
||||
"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X\n"),
|
||||
signing ? _signing_signing_nonce[0] : _signing_verifying_nonce[0],
|
||||
signing ? _signing_signing_nonce[1] : _signing_verifying_nonce[1],
|
||||
signing ? _signing_signing_nonce[2] : _signing_verifying_nonce[2],
|
||||
signing ? _signing_signing_nonce[3] : _signing_verifying_nonce[3],
|
||||
signing ? _signing_signing_nonce[4] : _signing_verifying_nonce[4],
|
||||
signing ? _signing_signing_nonce[5] : _signing_verifying_nonce[5],
|
||||
signing ? _signing_signing_nonce[6] : _signing_verifying_nonce[6],
|
||||
signing ? _signing_signing_nonce[7] : _signing_verifying_nonce[7],
|
||||
signing ? _signing_signing_nonce[8] : _signing_verifying_nonce[8],
|
||||
signing ? _signing_signing_nonce[9] : _signing_verifying_nonce[9],
|
||||
signing ? _signing_signing_nonce[10] : _signing_verifying_nonce[10],
|
||||
signing ? _signing_signing_nonce[11] : _signing_verifying_nonce[11],
|
||||
signing ? _signing_signing_nonce[12] : _signing_verifying_nonce[12],
|
||||
signing ? _signing_signing_nonce[13] : _signing_verifying_nonce[13],
|
||||
signing ? _signing_signing_nonce[14] : _signing_verifying_nonce[14],
|
||||
signing ? _signing_signing_nonce[15] : _signing_verifying_nonce[15],
|
||||
signing ? _signing_signing_nonce[16] : _signing_verifying_nonce[16],
|
||||
signing ? _signing_signing_nonce[17] : _signing_verifying_nonce[17],
|
||||
signing ? _signing_signing_nonce[18] : _signing_verifying_nonce[18],
|
||||
signing ? _signing_signing_nonce[19] : _signing_verifying_nonce[19],
|
||||
signing ? _signing_signing_nonce[20] : _signing_verifying_nonce[20],
|
||||
signing ? _signing_signing_nonce[21] : _signing_verifying_nonce[21],
|
||||
signing ? _signing_signing_nonce[22] : _signing_verifying_nonce[22],
|
||||
signing ? _signing_signing_nonce[23] : _signing_verifying_nonce[23],
|
||||
signing ? _signing_signing_nonce[24] : _signing_verifying_nonce[24],
|
||||
signing ? _signing_signing_nonce[25] : _signing_verifying_nonce[25],
|
||||
signing ? _signing_signing_nonce[26] : _signing_verifying_nonce[26],
|
||||
signing ? _signing_signing_nonce[27] : _signing_verifying_nonce[27],
|
||||
signing ? _signing_signing_nonce[28] : _signing_verifying_nonce[28],
|
||||
signing ? _signing_signing_nonce[29] : _signing_verifying_nonce[29],
|
||||
signing ? _signing_signing_nonce[30] : _signing_verifying_nonce[30],
|
||||
signing ? _signing_signing_nonce[31] : _signing_verifying_nonce[31]
|
||||
);
|
||||
// Signature is calculated on everything expect the first byte in the header
|
||||
uint16_t bytes_left = mGetLength(msg)+HEADER_SIZE-1;
|
||||
int16_t current_pos = 1-(int16_t)HEADER_SIZE; // Start at the second byte in the header
|
||||
uint8_t* nonce = signing ? _signing_signing_nonce : _signing_verifying_nonce;
|
||||
|
||||
#ifdef MY_DEBUG_VERBOSE_SIGNING
|
||||
buf2str(nonce, 32);
|
||||
SIGN_DEBUG(PSTR("SGN:BND:NONCE=%s\n"), printStr);
|
||||
#endif
|
||||
|
||||
while (bytes_left) {
|
||||
uint16_t bytes_to_include = MIN(bytes_left, 32);
|
||||
|
||||
memset(_signing_temp_message, 0, 32);
|
||||
memcpy(_signing_temp_message, (uint8_t*)&msg.data[current_pos], bytes_to_include);
|
||||
|
||||
// We can 'void' signerAtsha204AHmac because the HMAC is already put in the correct place
|
||||
(void)signerAtsha204AHmac(nonce, _signing_temp_message);
|
||||
// Purge nonce when used
|
||||
memset(nonce, 0xAA, 32);
|
||||
|
||||
bytes_left -= bytes_to_include;
|
||||
current_pos += bytes_to_include;
|
||||
|
||||
if (bytes_left > 0) {
|
||||
// We will do another pass, use current HMAC as nonce for the next HMAC
|
||||
memcpy(nonce, _signing_hmac, 32);
|
||||
}
|
||||
}
|
||||
#ifdef MY_DEBUG_VERBOSE_SIGNING
|
||||
buf2str(_signing_hmac, 32);
|
||||
SIGN_DEBUG(PSTR("SGN:BND:HMAC=%s\n"), printStr);
|
||||
#endif
|
||||
}
|
||||
|
||||
// Helper to calculate a ATSHA204A specific HMAC-SHA256 using provided 32 byte nonce and data
|
||||
// (zero padded to 32 bytes)
|
||||
// The pointer to the HMAC is returned, but the HMAC is also stored in _signing_hmac
|
||||
static uint8_t* signerAtsha204AHmac(const uint8_t* nonce, const uint8_t* data)
|
||||
{
|
||||
// ATSHA204 calculates the HMAC with a PSK and a SHA256 digest of the following data:
|
||||
// 32 bytes zeroes
|
||||
// 32 bytes digest,
|
||||
@@ -317,7 +353,7 @@ static void signerCalculateSignature(MyMessage &msg, bool signing)
|
||||
// Calculate message digest first
|
||||
_signing_sha256.init();
|
||||
for (int i=0; i<32; i++) {
|
||||
_signing_sha256.write(_signing_temp_message[i]);
|
||||
_signing_sha256.write(data[i]);
|
||||
}
|
||||
_signing_sha256.write(0x15); // OPCODE
|
||||
_signing_sha256.write(0x02); // param1
|
||||
@@ -330,19 +366,17 @@ static void signerCalculateSignature(MyMessage &msg, bool signing)
|
||||
_signing_sha256.write(0x00);
|
||||
}
|
||||
for (int i=0; i<32; i++) {
|
||||
_signing_sha256.write(signing ? _signing_signing_nonce[i] : _signing_verifying_nonce[i]);
|
||||
_signing_sha256.write(nonce[i]);
|
||||
}
|
||||
// Purge nonce when used
|
||||
memset(signing ? _signing_signing_nonce : _signing_verifying_nonce, 0xAA, 32);
|
||||
memcpy(_signing_temp_message, _signing_sha256.result(), 32);
|
||||
memcpy(_signing_hmac, _signing_sha256.result(), 32);
|
||||
|
||||
// Feed "message" to HMAC calculator
|
||||
_signing_sha256.initHmac(_signing_hmac_key,32); // Set the key to use
|
||||
_signing_sha256.initHmac(_signing_hmac_key, 32); // Set the key to use
|
||||
for (int i=0; i<32; i++) {
|
||||
_signing_sha256.write(0x00); // 32 bytes zeroes
|
||||
}
|
||||
for (int i=0; i<32; i++) {
|
||||
_signing_sha256.write(_signing_temp_message[i]); // 32 bytes digest
|
||||
_signing_sha256.write(_signing_hmac[i]); // 32 bytes digest
|
||||
}
|
||||
_signing_sha256.write(0x11); // OPCODE
|
||||
_signing_sha256.write(0x04); // Mode
|
||||
@@ -362,16 +396,17 @@ static void signerCalculateSignature(MyMessage &msg, bool signing)
|
||||
}
|
||||
|
||||
memcpy(_signing_hmac, _signing_sha256.resultHmac(), 32);
|
||||
|
||||
SIGN_DEBUG(PSTR("HMAC: "
|
||||
"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X"
|
||||
"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X\n"),
|
||||
_signing_hmac[0], _signing_hmac[1], _signing_hmac[2], _signing_hmac[3],
|
||||
_signing_hmac[4], _signing_hmac[5], _signing_hmac[6], _signing_hmac[7],
|
||||
_signing_hmac[8], _signing_hmac[9], _signing_hmac[10], _signing_hmac[11],
|
||||
_signing_hmac[12], _signing_hmac[13], _signing_hmac[14], _signing_hmac[15],
|
||||
_signing_hmac[16], _signing_hmac[17], _signing_hmac[18], _signing_hmac[19],
|
||||
_signing_hmac[20], _signing_hmac[21], _signing_hmac[22], _signing_hmac[23],
|
||||
_signing_hmac[24], _signing_hmac[25], _signing_hmac[26], _signing_hmac[27],
|
||||
_signing_hmac[28], _signing_hmac[29], _signing_hmac[30], _signing_hmac[31]);
|
||||
return _signing_hmac;
|
||||
}
|
||||
|
||||
// Helper to calculate a generic SHA256 digest of provided buffer
|
||||
// The pointer to the hash is returned
|
||||
static uint8_t* signerSha256(const uint8_t* data, size_t sz)
|
||||
{
|
||||
_signing_sha256.init();
|
||||
for (size_t i=0; i<sz; i++) {
|
||||
_signing_sha256.write(data[i]);
|
||||
}
|
||||
return _signing_sha256.result();
|
||||
}
|
||||
#endif //MY_SIGNING_SOFT
|
||||
|
||||
Reference in New Issue
Block a user