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
+166
-203
@@ -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
|
||||
@@ -27,34 +27,57 @@
|
||||
|
||||
#include "MySigning.h"
|
||||
|
||||
#ifdef MY_SIGNING_ATSHA204
|
||||
#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
|
||||
|
||||
unsigned long _signing_timestamp;
|
||||
bool _signing_verification_ongoing = false;
|
||||
uint8_t _signing_verifying_nonce[NONCE_NUMIN_SIZE_PASSTHROUGH+SHA204_SERIAL_SZ+1];
|
||||
uint8_t _signing_signing_nonce[NONCE_NUMIN_SIZE_PASSTHROUGH+SHA204_SERIAL_SZ+1];
|
||||
uint8_t _signing_temp_message[SHA_MSG_SIZE];
|
||||
uint8_t _signing_rx_buffer[SHA204_RSP_SIZE_MAX];
|
||||
uint8_t _signing_tx_buffer[SHA204_CMD_SIZE_MAX];
|
||||
extern uint8_t _doWhitelist[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[SHA_MSG_SIZE];
|
||||
static uint8_t _signing_rx_buffer[SHA204_RSP_SIZE_MAX];
|
||||
static uint8_t _signing_tx_buffer[SHA204_CMD_SIZE_MAX];
|
||||
static uint8_t* const _signing_hmac = &_signing_rx_buffer[SHA204_BUFFER_POS_DATA];
|
||||
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 uint8_t* signerSha256(const uint8_t* data, size_t sz);
|
||||
|
||||
static bool init_ok = false;
|
||||
|
||||
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 signerAtsha204Init(void)
|
||||
{
|
||||
init_ok = true;
|
||||
@@ -64,28 +87,41 @@ bool signerAtsha204Init(void)
|
||||
// Read the configuration lock flag to determine if device is personalized or not
|
||||
if (atsha204_read(_signing_tx_buffer, _signing_rx_buffer,
|
||||
SHA204_ZONE_CONFIG, 0x15<<2) != SHA204_SUCCESS) {
|
||||
SIGN_DEBUG(PSTR("Could not read ATSHA204A lock config, refusing to use backend\n"));
|
||||
SIGN_DEBUG(PSTR("!SGN:BND:INIT FAIL\n")); //Could not read ATSHA204A lock config
|
||||
init_ok = false;
|
||||
} else if (_signing_rx_buffer[SHA204_BUFFER_POS_DATA+3] != 0x00) {
|
||||
SIGN_DEBUG(PSTR("ATSHA204A not personalized, refusing to use backend\n"));
|
||||
SIGN_DEBUG(PSTR("!SGN:BND:PER\n")); //ATSHA204A not personalized
|
||||
init_ok = false;
|
||||
}
|
||||
if (init_ok) {
|
||||
// Get and cache the serial of the ATSHA204A
|
||||
if (atsha204_getSerialNumber(_signing_node_serial_info) != SHA204_SUCCESS) {
|
||||
SIGN_DEBUG(PSTR("!SGN:BND:SER\n")); //Could not get ATSHA204A serial
|
||||
init_ok = false;
|
||||
}
|
||||
}
|
||||
return init_ok;
|
||||
}
|
||||
|
||||
bool signerAtsha204CheckTimer(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, 0x00, NONCE_NUMIN_SIZE_PASSTHROUGH);
|
||||
memset(_signing_verifying_nonce, 0x00, NONCE_NUMIN_SIZE_PASSTHROUGH);
|
||||
memset(_signing_signing_nonce, 0xAA, 32);
|
||||
memset(_signing_verifying_nonce, 0xAA, 32);
|
||||
_signing_verification_ongoing = false;
|
||||
return false;
|
||||
}
|
||||
@@ -96,60 +132,57 @@ bool signerAtsha204CheckTimer(void)
|
||||
bool signerAtsha204GetNonce(MyMessage &msg)
|
||||
{
|
||||
if (!init_ok) {
|
||||
SIGN_DEBUG(PSTR("Backend not initialized\n"));
|
||||
return false;
|
||||
}
|
||||
SIGN_DEBUG(PSTR("Signing backend: ATSHA204\n"));
|
||||
// Generate random number for use as nonce
|
||||
// We used a basic whitening technique that XORs each byte in a 32byte random value with current hwMillis() counter
|
||||
// This 32-byte random value is then hashed (SHA256) to produce the resulting nonce
|
||||
|
||||
// We used a basic whitening technique that XORs each byte in a 32byte random value with current
|
||||
// hwMillis() counter. This 32-byte random value is then hashed (SHA256) to produce the resulting
|
||||
// nonce
|
||||
(void)atsha204_wakeup(_signing_temp_message);
|
||||
if (atsha204_execute(SHA204_RANDOM, RANDOM_SEED_UPDATE, 0, 0, NULL,
|
||||
RANDOM_COUNT, _signing_tx_buffer, RANDOM_RSP_SIZE, _signing_rx_buffer) != SHA204_SUCCESS) {
|
||||
SIGN_DEBUG(PSTR("Failed to generate nonce\n"));
|
||||
RANDOM_COUNT, _signing_tx_buffer, RANDOM_RSP_SIZE, _signing_rx_buffer) !=
|
||||
SHA204_SUCCESS) {
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i < 32; i++) {
|
||||
_signing_verifying_nonce[i] = _signing_rx_buffer[SHA204_BUFFER_POS_DATA+i] ^ (hwMillis()&0xFF);
|
||||
}
|
||||
memcpy(_signing_verifying_nonce, signerSha256(_signing_verifying_nonce, 32), MAX_PAYLOAD);
|
||||
memcpy(_signing_verifying_nonce, signerSha256(_signing_verifying_nonce, 32),
|
||||
min(MAX_PAYLOAD, 32));
|
||||
|
||||
atsha204_idle(); // We just idle the chip now since we expect to use it soon when the signed message arrives
|
||||
// We just idle the chip now since we expect to use it soon when the signed message arrives
|
||||
atsha204_idle();
|
||||
|
||||
// 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)
|
||||
// Note that if message is "too" quick, and arrives before turnover, it will be rejected
|
||||
// but this is consider such a rare case that it is accepted and rejects are 'safe'
|
||||
if (_signing_timestamp + MY_VERIFICATION_TIMEOUT_MS < hwMillis()) {
|
||||
_signing_timestamp = 0;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void signerAtsha204PutNonce(MyMessage &msg)
|
||||
{
|
||||
if (!init_ok) {
|
||||
SIGN_DEBUG(PSTR("Backend not initialized\n"));
|
||||
return;
|
||||
}
|
||||
SIGN_DEBUG(PSTR("Signing backend: ATSHA204\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 signerAtsha204SignMsg(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;
|
||||
}
|
||||
|
||||
@@ -159,27 +192,27 @@ bool signerAtsha204SignMsg(MyMessage &msg)
|
||||
|
||||
if (DO_WHITELIST(msg.destination)) {
|
||||
// Salt the signature with the senders nodeId and the unique serial of the ATSHA device
|
||||
memcpy(_signing_signing_nonce, &_signing_rx_buffer[SHA204_BUFFER_POS_DATA],
|
||||
32); // We can reuse the nonce buffer now since it is no longer needed
|
||||
// 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;
|
||||
atsha204_getSerialNumber(&_signing_signing_nonce[33]);
|
||||
(void)signerSha256(_signing_signing_nonce,
|
||||
32+1+SHA204_SERIAL_SZ); // we can 'void' sha256 because the hash is already put in the correct place
|
||||
SIGN_DEBUG(PSTR("Signature salted with serial: %02X%02X%02X%02X%02X%02X%02X%02X%02X\n"),
|
||||
_signing_signing_nonce[33], _signing_signing_nonce[34], _signing_signing_nonce[35],
|
||||
_signing_signing_nonce[36], _signing_signing_nonce[37], _signing_signing_nonce[38],
|
||||
_signing_signing_nonce[39], _signing_signing_nonce[40], _signing_signing_nonce[41]);
|
||||
memcpy(&_signing_signing_nonce[33], _signing_node_serial_info, 9);
|
||||
// We can 'void' sha256 because the hash is already put in the correct place
|
||||
(void)signerSha256(_signing_signing_nonce, 32+1+9);
|
||||
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
|
||||
}
|
||||
|
||||
// Put device back to sleep
|
||||
atsha204_sleep();
|
||||
|
||||
// Overwrite the first byte in the signature with the signing identifier
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA] = 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_rx_buffer[SHA204_BUFFER_POS_DATA],
|
||||
MAX_PAYLOAD-mGetLength(msg));
|
||||
memcpy(&msg.data[mGetLength(msg)], _signing_hmac, min(MAX_PAYLOAD-mGetLength(msg), 32));
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -187,7 +220,7 @@ bool signerAtsha204SignMsg(MyMessage &msg)
|
||||
bool signerAtsha204VerifyMsg(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
|
||||
@@ -198,7 +231,7 @@ bool signerAtsha204VerifyMsg(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;
|
||||
}
|
||||
|
||||
@@ -209,18 +242,22 @@ bool signerAtsha204VerifyMsg(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"));
|
||||
memcpy(_signing_verifying_nonce, &_signing_rx_buffer[SHA204_BUFFER_POS_DATA],
|
||||
32); // We can reuse the nonce buffer now since it is no longer needed
|
||||
// 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, SHA204_SERIAL_SZ);
|
||||
(void)signerSha256(_signing_verifying_nonce,
|
||||
32+1+SHA204_SERIAL_SZ); // we can 'void' sha256 because the hash is already put in the correct place
|
||||
memcpy(&_signing_verifying_nonce[33], _signing_whitelist[j].serial, 9);
|
||||
// We can 'void' sha256 because the hash is already put in the correct place
|
||||
(void)signerSha256(_signing_verifying_nonce, 32+1+9);
|
||||
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);
|
||||
// Put device back to sleep
|
||||
atsha204_sleep();
|
||||
return false;
|
||||
@@ -231,134 +268,96 @@ bool signerAtsha204VerifyMsg(MyMessage &msg)
|
||||
atsha204_sleep();
|
||||
|
||||
// Overwrite the first byte in the signature with the signing identifier
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA] = SIGNING_IDENTIFIER;
|
||||
_signing_hmac[0] = SIGNING_IDENTIFIER;
|
||||
|
||||
// Compare the caluclated signature with the provided signature
|
||||
if (signerMemcmp(&msg.data[mGetLength(msg)], &_signing_rx_buffer[SHA204_BUFFER_POS_DATA],
|
||||
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 _signing_rx_buffer[SHA204_BUFFER_POS_DATA])
|
||||
// (=_signing_hmac)
|
||||
static void signerCalculateSignature(MyMessage &msg, bool signing)
|
||||
{
|
||||
(void)atsha204_wakeup(_signing_temp_message);
|
||||
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)));
|
||||
// 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);
|
||||
|
||||
(void)atsha204_wakeup(_signing_temp_message); // Issue wakeup to reset watchdog
|
||||
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);
|
||||
atsha204_idle(); // Idle the chip to allow the wakeup call to reset the watchdog
|
||||
}
|
||||
}
|
||||
#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_rx_buffer[SHA204_BUFFER_POS_DATA] (=_signing_hmac)
|
||||
static uint8_t* signerAtsha204AHmac(const uint8_t* nonce, const uint8_t* data)
|
||||
{
|
||||
// Program the data to sign into the ATSHA204
|
||||
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]
|
||||
);
|
||||
(void)atsha204_execute(SHA204_WRITE, SHA204_ZONE_DATA | SHA204_ZONE_COUNT_FLAG, 8 << 3, 32,
|
||||
_signing_temp_message,
|
||||
(uint8_t*)data,
|
||||
WRITE_COUNT_LONG, _signing_tx_buffer, WRITE_RSP_SIZE, _signing_rx_buffer);
|
||||
|
||||
// Program the nonce to use for the signature (has to be done just before GENDIG due to chip limitations)
|
||||
(void)atsha204_execute(SHA204_NONCE, NONCE_MODE_PASSTHROUGH, 0, NONCE_NUMIN_SIZE_PASSTHROUGH,
|
||||
signing ? _signing_signing_nonce : _signing_verifying_nonce,
|
||||
NONCE_COUNT_LONG, _signing_tx_buffer, NONCE_RSP_SIZE_SHORT, _signing_rx_buffer);
|
||||
|
||||
// Purge nonce when used
|
||||
memset(signing ? _signing_signing_nonce : _signing_verifying_nonce, 0x00,
|
||||
NONCE_NUMIN_SIZE_PASSTHROUGH);
|
||||
// Program the nonce to use for the signature (has to be done just before GENDIG
|
||||
// due to chip limitations)
|
||||
(void)atsha204_execute(SHA204_NONCE, NONCE_MODE_PASSTHROUGH, 0, 32, (uint8_t*)nonce,
|
||||
NONCE_COUNT_LONG, _signing_tx_buffer, NONCE_RSP_SIZE_SHORT,
|
||||
_signing_rx_buffer);
|
||||
|
||||
// Generate digest of data and nonce
|
||||
(void)atsha204_execute(SHA204_GENDIG, GENDIG_ZONE_DATA, 8, 0, NULL,
|
||||
GENDIG_COUNT_DATA, _signing_tx_buffer, GENDIG_RSP_SIZE, _signing_rx_buffer);
|
||||
GENDIG_COUNT_DATA, _signing_tx_buffer, GENDIG_RSP_SIZE,
|
||||
_signing_rx_buffer);
|
||||
|
||||
// Calculate HMAC of message+nonce digest and secret key
|
||||
(void)atsha204_execute(SHA204_HMAC, HMAC_MODE_SOURCE_FLAG_MATCH, 0, 0, NULL,
|
||||
HMAC_COUNT, _signing_tx_buffer, HMAC_RSP_SIZE, _signing_rx_buffer);
|
||||
|
||||
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_rx_buffer[SHA204_BUFFER_POS_DATA+0],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+1],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+2],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+3],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+4],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+5],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+6],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+7],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+8],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+9],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+10],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+11],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+12],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+13],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+14],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+15],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+16],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+17],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+18],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+19],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+20],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+21],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+22],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+23],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+24],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+25],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+26],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+27],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+28],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+29],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+30],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+31]
|
||||
);
|
||||
return &_signing_rx_buffer[SHA204_BUFFER_POS_DATA];
|
||||
}
|
||||
|
||||
// Helper to calculate a generic SHA256 digest of provided buffer (only supports one block)
|
||||
// The pointer to the hash is returned, but the hash is also stored in _signing_rx_buffer[SHA204_BUFFER_POS_DATA])
|
||||
// The pointer to the hash is returned, but the hash is also stored in
|
||||
// _signing_rx_buffer[SHA204_BUFFER_POS_DATA])
|
||||
static uint8_t* signerSha256(const uint8_t* data, size_t sz)
|
||||
{
|
||||
// Initiate SHA256 calculator
|
||||
(void)atsha204_execute(SHA204_SHA, SHA_INIT, 0, 0, NULL,
|
||||
SHA_COUNT_SHORT, _signing_tx_buffer, SHA_RSP_SIZE_SHORT, _signing_rx_buffer);
|
||||
SHA_COUNT_SHORT, _signing_tx_buffer, SHA_RSP_SIZE_SHORT,
|
||||
_signing_rx_buffer);
|
||||
|
||||
// Calculate a hash
|
||||
memset(_signing_temp_message, 0x00, SHA_MSG_SIZE);
|
||||
@@ -369,42 +368,6 @@ static uint8_t* signerSha256(const uint8_t* data, size_t sz)
|
||||
_signing_temp_message[SHA_MSG_SIZE-1] = (sz << 3);
|
||||
(void)atsha204_execute(SHA204_SHA, SHA_CALC, 0, SHA_MSG_SIZE, _signing_temp_message,
|
||||
SHA_COUNT_LONG, _signing_tx_buffer, SHA_RSP_SIZE_LONG, _signing_rx_buffer);
|
||||
|
||||
SIGN_DEBUG(PSTR("SHA256: "
|
||||
"%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_rx_buffer[SHA204_BUFFER_POS_DATA+0],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+1],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+2],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+3],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+4],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+5],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+6],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+7],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+8],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+9],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+10],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+11],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+12],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+13],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+14],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+15],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+16],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+17],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+18],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+19],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+20],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+21],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+22],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+23],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+24],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+25],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+26],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+27],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+28],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+29],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+30],
|
||||
_signing_rx_buffer[SHA204_BUFFER_POS_DATA+31]
|
||||
);
|
||||
return &_signing_rx_buffer[SHA204_BUFFER_POS_DATA];
|
||||
}
|
||||
#endif //MY_SIGNING_ATSHA204
|
||||
|
||||
Reference in New Issue
Block a user