Slimmed down memory footprint of signing backends

This commit is contained in:
Patrick Fallberg
2015-03-16 20:24:42 +01:00
parent f295ef2961
commit 611ba0cf44
7 changed files with 92 additions and 147 deletions
+47 -89
View File
@@ -8,18 +8,17 @@
#include "MySigningAtsha204.h"
// Uncomment this to get some useful serial debug info (Serial.print and Serial.println expected)
//#define DEBUG_ATSHA_SIGNING
//#define DEBUG_SIGNING
#ifdef DEBUG_ATSHA_SIGNING
#define DEBUG_ATSHA_PRINTLN(args) Serial.println(args)
#ifdef DEBUG_SIGNING
#define DEBUG_SIGNING_PRINTLN(args) Serial.println(args)
#else
#define DEBUG_ATSHA_PRINTLN(args)
#define DEBUG_SIGNING_PRINTLN(args)
#endif
#ifdef DEBUG_ATSHA_SIGNING
static void DEBUG_ATSHA_PRINTBUF(char* str, uint8_t* buf, uint8_t sz)
#ifdef DEBUG_SIGNING
static void DEBUG_SIGNING_PRINTBUF(const __FlashStringHelper* str, uint8_t* buf, uint8_t sz)
{
int i;
Serial.println(str);
for (int i=0; i<sz; i++)
{
@@ -32,7 +31,7 @@ static void DEBUG_ATSHA_PRINTBUF(char* str, uint8_t* buf, uint8_t sz)
Serial.println();
}
#else
#define DEBUG_ATSHA_PRINTBUF(str, buf, sz)
#define DEBUG_SIGNING_PRINTBUF(str, buf, sz)
#endif
@@ -54,25 +53,17 @@ MySigningAtsha204::MySigningAtsha204(bool requestSignatures,
bool MySigningAtsha204::getNonce(MyMessage &msg) {
// Generate random number for use as nonce
uint8_t* pRnd = NULL;
// We used a basic whitening technique that takes the first byte of a new random value and builds up a 32-byte random value
// This 32-byte random value is then hashed (SHA256) to produce the resulting nonce
for (int i = 0; i < 32; i++) {
if (atsha204.sha204m_execute(SHA204_RANDOM, RANDOM_NO_SEED_UPDATE, 0, 0, NULL,
RANDOM_COUNT, tx_buffer, RANDOM_RSP_SIZE, rx_buffer) != SHA204_SUCCESS) {
DEBUG_ATSHA_PRINTLN("Failed to generate nonce");
DEBUG_SIGNING_PRINTLN(F("FTGN")); // FTGN = Failed to generate nonce
return false;
}
current_nonce[i] = rx_buffer[SHA204_BUFFER_POS_DATA];
}
pRnd = sha256(current_nonce, 32);
if (!pRnd) {
DEBUG_ATSHA_PRINTLN("Failed to hash random value");
return false;
}
memcpy(current_nonce, pRnd, MAX_PAYLOAD);
memcpy(current_nonce, sha256(current_nonce, 32), MAX_PAYLOAD);
// We set the part of the 32-byte nonce that does not fit into a message to 0xAA
memset(&current_nonce[MAX_PAYLOAD], 0xAA, sizeof(current_nonce)-MAX_PAYLOAD);
@@ -94,9 +85,9 @@ bool MySigningAtsha204::getNonce(MyMessage &msg) {
bool MySigningAtsha204::checkTimer() {
if (verification_ongoing) {
if (millis() < timestamp || millis() > timestamp + MY_VERIFICATION_TIMEOUT_MS) {
DEBUG_ATSHA_PRINTLN("Verification timeout");
DEBUG_SIGNING_PRINTLN(F("VT")); // VT = Verification timeout
// Purge nonce
memset(current_nonce, 0xAA, NONCE_NUMIN_SIZE_PASSTHROUGH);
memset(current_nonce, 0x00, NONCE_NUMIN_SIZE_PASSTHROUGH);
verification_ongoing = false;
return false;
}
@@ -105,44 +96,35 @@ bool MySigningAtsha204::checkTimer() {
}
bool MySigningAtsha204::putNonce(MyMessage &msg) {
if (mGetLength(msg) != MAX_PAYLOAD) {
DEBUG_ATSHA_PRINTLN("Incoming nonce with incorrect size");
return false; // We require as big nonce as possible
}
if (((uint8_t*)msg.getCustom())[0] != SIGNING_IDENTIFIER) {
DEBUG_ATSHA_PRINTLN("Incorrect signing identifier");
DEBUG_SIGNING_PRINTLN(F("ISI")); // ISI = Incorrect signing identifier
return false;
}
memcpy(current_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(&current_nonce[MAX_PAYLOAD], 0xAA, sizeof(current_nonce)-MAX_PAYLOAD);
return true;
}
bool MySigningAtsha204::signMsg(MyMessage &msg) {
// If we cannot fit any signature in the message, refuse to sign it
if (mGetLength(msg) > MAX_PAYLOAD-2) {
DEBUG_ATSHA_PRINTLN("Cannot fit any signature to this message");
DEBUG_SIGNING_PRINTLN(F("MTOL")); // Message too large for signature to fit
return false;
}
// Calculate signature of message
mSetSigned(msg, 1); // make sure signing flag is set before signature is calculated
DEBUG_ATSHA_PRINTBUF("Message to sign:", (uint8_t*)&msg.data[1-HEADER_SIZE], MAX_MESSAGE_LENGTH-1-(MAX_PAYLOAD-mGetLength(msg)));
if (!calculateSignature(msg)) {
DEBUG_ATSHA_PRINTLN("Failed to calculate signature");
mSetSigned(msg, 0); // make sure signing flag is truthful
return false;
}
calculateSignature(msg);
#ifdef MY_SECURE_NODE_WHITELISTING
// Salt the signature with the senders nodeId and the unique serial of the ATSHA device
memcpy(current_nonce, &rx_buffer[SHA204_BUFFER_POS_DATA], 32); // We can reuse the nonce buffer now since it is no longer needed
current_nonce[32] = msg.sender;
(void)atsha204.getSerialNumber(&current_nonce[33]);
atsha204.getSerialNumber(&current_nonce[33]);
(void)sha256(current_nonce, 32+1+SHA204_SERIAL_SZ); // we can 'void' sha256 because the hash is already put in the correct place
memset(current_nonce, 0xAA, NONCE_NUMIN_SIZE_PASSTHROUGH);
DEBUG_ATSHA_PRINTLN("Signature salted");
DEBUG_SIGNING_PRINTLN(F("SWS")); // SWS = Signature whitelist salted
#endif
// Overwrite the first byte in the signature with the signing identifier
@@ -156,7 +138,7 @@ bool MySigningAtsha204::signMsg(MyMessage &msg) {
bool MySigningAtsha204::verifyMsg(MyMessage &msg) {
if (!verification_ongoing) {
DEBUG_ATSHA_PRINTLN("No active verification session");
DEBUG_SIGNING_PRINTLN(F("NAVS")); // NAVS = No active verification session
return false;
} else {
// Make sure we have not expired
@@ -167,24 +149,18 @@ bool MySigningAtsha204::verifyMsg(MyMessage &msg) {
verification_ongoing = false;
if (msg.data[mGetLength(msg)] != SIGNING_IDENTIFIER) {
DEBUG_ATSHA_PRINTLN("Incorrect signing identifier");
DEBUG_SIGNING_PRINTLN(F("ISI")); // ISI = Incorrect signing identifier
return false;
}
DEBUG_ATSHA_PRINTBUF("Message to verify:", (uint8_t*)&msg.data[1-HEADER_SIZE], MAX_MESSAGE_LENGTH-1-(MAX_PAYLOAD-mGetLength(msg)));
DEBUG_ATSHA_PRINTBUF("Signature in message:", (uint8_t*)&msg.data[mGetLength(msg)], MAX_PAYLOAD-mGetLength(msg));
// Get signature of message
if (!calculateSignature(msg)) {
DEBUG_ATSHA_PRINTLN("Failed to calculate signature");
return false;
}
DEBUG_SIGNING_PRINTBUF(F("SIM:"), (uint8_t*)&msg.data[mGetLength(msg)], MAX_PAYLOAD-mGetLength(msg)); // SIM = Signature in message
calculateSignature(msg); // Get signature of message
#ifdef MY_SECURE_NODE_WHITELISTING
// Look up the senders nodeId in our whitelist and salt the signature with that data
for (int j=0; j < whitlist_sz; j++) {
if (whitelist[j].nodeId == msg.sender) {
DEBUG_ATSHA_PRINTLN("Sender found in whitelist");
DEBUG_SIGNING_PRINTLN(F("SIW")); // SIW = Sender found in whitelist
memcpy(current_nonce, &rx_buffer[SHA204_BUFFER_POS_DATA], 32); // We can reuse the nonce buffer now since it is no longer needed
current_nonce[32] = msg.sender;
memcpy(&current_nonce[33], whitelist[j].serial, SHA204_SERIAL_SZ);
@@ -199,71 +175,56 @@ bool MySigningAtsha204::verifyMsg(MyMessage &msg) {
// Compare the caluclated signature with the provided signature
if (memcmp(&msg.data[mGetLength(msg)], &rx_buffer[SHA204_BUFFER_POS_DATA], MAX_PAYLOAD-mGetLength(msg))) {
DEBUG_ATSHA_PRINTBUF("Signature bad. Calculated signature:", &rx_buffer[SHA204_BUFFER_POS_DATA], MAX_PAYLOAD-mGetLength(msg));
DEBUG_SIGNING_PRINTBUF(F("SNOK:"), &rx_buffer[SHA204_BUFFER_POS_DATA], MAX_PAYLOAD-mGetLength(msg)); // SNOK = Signature bad
#ifdef MY_SECURE_NODE_WHITELISTING
DEBUG_ATSHA_PRINTLN("Is the sender whitelisted?");
DEBUG_SIGNING_PRINTLN(F("W?")); // W? = Is the sender whitelisted?
#endif
return false;
} else {
DEBUG_ATSHA_PRINTLN("Signature ok");
DEBUG_SIGNING_PRINTLN(F("SOK")); // SOK = Signature OK
return true;
}
}
}
// Helper to calculate signature of msg (returned in rx_buffer[SHA204_BUFFER_POS_DATA])
bool MySigningAtsha204::calculateSignature(MyMessage &msg) {
void MySigningAtsha204::calculateSignature(MyMessage &msg) {
memset(temp_message, 0, 32);
memcpy(temp_message, (uint8_t*)&msg.data[1-HEADER_SIZE], MAX_MESSAGE_LENGTH-1-(MAX_PAYLOAD-mGetLength(msg)));
// Program the data to sign into the ATSHA204
if (atsha204.sha204m_execute(SHA204_WRITE, SHA204_ZONE_DATA | SHA204_ZONE_COUNT_FLAG, 8 << 3, 32, temp_message,
WRITE_COUNT_LONG, tx_buffer, WRITE_RSP_SIZE, rx_buffer) != SHA204_SUCCESS) {
DEBUG_ATSHA_PRINTLN("Failed to program data");
return false;
}
DEBUG_SIGNING_PRINTBUF(F("MSG:"), (uint8_t*)&msg.data[1-HEADER_SIZE], MAX_MESSAGE_LENGTH-1-(MAX_PAYLOAD-mGetLength(msg))); // MSG = Message to sign
DEBUG_SIGNING_PRINTBUF(F("CNC:"), current_nonce, 32); // CNC = Current nonce
(void)atsha204.sha204m_execute(SHA204_WRITE, SHA204_ZONE_DATA | SHA204_ZONE_COUNT_FLAG, 8 << 3, 32, temp_message,
WRITE_COUNT_LONG, tx_buffer, WRITE_RSP_SIZE, rx_buffer);
// Program the nonce to use for the signature (has to be done just before GENDIG due to chip limitations)
if (atsha204.sha204m_execute(SHA204_NONCE, NONCE_MODE_PASSTHROUGH, 0, NONCE_NUMIN_SIZE_PASSTHROUGH, current_nonce,
NONCE_COUNT_LONG, tx_buffer, NONCE_RSP_SIZE_SHORT, rx_buffer) != SHA204_SUCCESS) {
DEBUG_ATSHA_PRINTLN("Failed to program nonce");
// Purge nonce when used
memset(current_nonce, 0xAA, NONCE_NUMIN_SIZE_PASSTHROUGH);
return false;
}
(void)atsha204.sha204m_execute(SHA204_NONCE, NONCE_MODE_PASSTHROUGH, 0, NONCE_NUMIN_SIZE_PASSTHROUGH, current_nonce,
NONCE_COUNT_LONG, tx_buffer, NONCE_RSP_SIZE_SHORT, rx_buffer);
// Purge nonce when used
memset(current_nonce, 0xAA, NONCE_NUMIN_SIZE_PASSTHROUGH);
memset(current_nonce, 0x00, NONCE_NUMIN_SIZE_PASSTHROUGH);
// Generate digest of data and nonce
if (atsha204.sha204m_execute(SHA204_GENDIG, GENDIG_ZONE_DATA, 8, 0, NULL,
GENDIG_COUNT_DATA, tx_buffer, GENDIG_RSP_SIZE, rx_buffer) != SHA204_SUCCESS) {
DEBUG_ATSHA_PRINTLN("Failed to generate digest");
return false;
}
(void)atsha204.sha204m_execute(SHA204_GENDIG, GENDIG_ZONE_DATA, 8, 0, NULL,
GENDIG_COUNT_DATA, tx_buffer, GENDIG_RSP_SIZE, rx_buffer);
// Calculate HMAC of message+nonce digest and secret key
if (atsha204.sha204m_execute(SHA204_HMAC, HMAC_MODE_SOURCE_FLAG_MATCH, 0, 0, NULL,
HMAC_COUNT, tx_buffer, HMAC_RSP_SIZE, rx_buffer) != SHA204_SUCCESS) {
DEBUG_ATSHA_PRINTLN("Failed to generate HMAC");
return false;
}
(void)atsha204.sha204m_execute(SHA204_HMAC, HMAC_MODE_SOURCE_FLAG_MATCH, 0, 0, NULL,
HMAC_COUNT, tx_buffer, HMAC_RSP_SIZE, rx_buffer);
// Put device back to sleep
atsha204.sha204c_sleep();
DEBUG_ATSHA_PRINTBUF("HMAC:", &rx_buffer[SHA204_BUFFER_POS_DATA], 32);
return true; // We return with the signature in rx_buffer[SHA204_BUFFER_POS_DATA]
DEBUG_SIGNING_PRINTBUF(F("HMAC:"), &rx_buffer[SHA204_BUFFER_POS_DATA], 32);
}
// Helper to calculate a generic SHA256 digest of provided buffer (returned in rx_buffer[SHA204_BUFFER_POS_DATA]) (only supports one block)
// 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 rx_buffer[SHA204_BUFFER_POS_DATA])
uint8_t* MySigningAtsha204::sha256(const uint8_t* data, size_t sz) {
// Initiate SHA256 calculator
if (atsha204.sha204m_execute(SHA204_SHA, SHA_INIT, 0, 0, NULL,
SHA_COUNT_SHORT, tx_buffer, SHA_RSP_SIZE_SHORT, rx_buffer) != SHA204_SUCCESS) {
DEBUG_ATSHA_PRINTLN("Failed to initiate sha256");
return NULL;
}
(void)atsha204.sha204m_execute(SHA204_SHA, SHA_INIT, 0, 0, NULL,
SHA_COUNT_SHORT, tx_buffer, SHA_RSP_SIZE_SHORT, rx_buffer);
// Calculate a hash
memset(temp_message, 0x00, SHA_MSG_SIZE);
@@ -272,16 +233,13 @@ uint8_t* MySigningAtsha204::sha256(const uint8_t* data, size_t sz) {
// Write length data to the last bytes
temp_message[SHA_MSG_SIZE-2] = (sz >> 5);
temp_message[SHA_MSG_SIZE-1] = (sz << 3);
DEBUG_ATSHA_PRINTBUF("Data to hash:", temp_message, SHA_MSG_SIZE);
if (atsha204.sha204m_execute(SHA204_SHA, SHA_CALC, 0, SHA_MSG_SIZE, temp_message,
SHA_COUNT_LONG, tx_buffer, SHA_RSP_SIZE_LONG, rx_buffer) != SHA204_SUCCESS) {
DEBUG_ATSHA_PRINTLN("Failed to calculate sha256");
return NULL;
}
DEBUG_SIGNING_PRINTBUF(F("DTH:"), temp_message, SHA_MSG_SIZE); // DTH = Data to hash
(void)atsha204.sha204m_execute(SHA204_SHA, SHA_CALC, 0, SHA_MSG_SIZE, temp_message,
SHA_COUNT_LONG, tx_buffer, SHA_RSP_SIZE_LONG, rx_buffer);
// Put device back to sleep
atsha204.sha204c_sleep();
DEBUG_ATSHA_PRINTBUF("SHA:", &rx_buffer[SHA204_BUFFER_POS_DATA], 32);
DEBUG_SIGNING_PRINTBUF(F("SHA:"), &rx_buffer[SHA204_BUFFER_POS_DATA], 32);
return &rx_buffer[SHA204_BUFFER_POS_DATA];
}
+1 -1
View File
@@ -45,7 +45,7 @@ private:
uint8_t temp_message[SHA_MSG_SIZE];
uint8_t rx_buffer[SHA204_RSP_SIZE_MAX];
uint8_t tx_buffer[SHA204_CMD_SIZE_MAX];
bool calculateSignature(MyMessage &msg);
void calculateSignature(MyMessage &msg);
uint8_t* sha256(const uint8_t* data, size_t sz);
#ifdef MY_SECURE_NODE_WHITELISTING
uint8_t whitlist_sz;
+24 -38
View File
@@ -13,16 +13,16 @@
#include "utility/sha256.h"
// Uncomment this to get some useful serial debug info (Serial.print and Serial.println expected)
//#define DEBUG_ATSHASOFT_SIGNING
//#define DEBUG_SIGNING
#ifdef DEBUG_ATSHASOFT_SIGNING
#define DEBUG_ATSHASOFT_PRINTLN(args) Serial.println(args)
#ifdef DEBUG_SIGNING
#define DEBUG_SIGNING_PRINTLN(args) Serial.println(args)
#else
#define DEBUG_ATSHASOFT_PRINTLN(args)
#define DEBUG_SIGNING_PRINTLN(args)
#endif
#ifdef DEBUG_ATSHASOFT_SIGNING
static void DEBUG_ATSHASOFT_PRINTBUF(char* str, uint8_t* buf, uint8_t sz)
#ifdef DEBUG_SIGNING
static void DEBUG_SIGNING_PRINTBUF(const __FlashStringHelper* str, uint8_t* buf, uint8_t sz)
{
int i;
Serial.println(str);
@@ -37,7 +37,7 @@ static void DEBUG_ATSHASOFT_PRINTBUF(char* str, uint8_t* buf, uint8_t sz)
Serial.println();
}
#else
#define DEBUG_ATSHASOFT_PRINTBUF(str, buf, sz)
#define DEBUG_SIGNING_PRINTBUF(str, buf, sz)
#endif
MySigningAtsha204Soft::MySigningAtsha204Soft(bool requestSignatures,
@@ -93,7 +93,7 @@ bool MySigningAtsha204Soft::getNonce(MyMessage &msg) {
bool MySigningAtsha204Soft::checkTimer() {
if (verification_ongoing) {
if (millis() < timestamp || millis() > timestamp + MY_VERIFICATION_TIMEOUT_MS) {
DEBUG_ATSHASOFT_PRINTLN("Verification timeout");
DEBUG_SIGNING_PRINTLN(F("VT")); // VT = Verification timeout
// Purge nonce
memset(current_nonce, 0xAA, 32);
verification_ongoing = false;
@@ -104,13 +104,8 @@ bool MySigningAtsha204Soft::checkTimer() {
}
bool MySigningAtsha204Soft::putNonce(MyMessage &msg) {
if (mGetLength(msg) != MAX_PAYLOAD) {
DEBUG_ATSHASOFT_PRINTLN("Incoming nonce with incorrect size");
return false; // We require as big nonce as possible
}
if (((uint8_t*)msg.getCustom())[0] != SIGNING_IDENTIFIER) {
DEBUG_ATSHASOFT_PRINTLN("Incorrect signing identifier");
DEBUG_SIGNING_PRINTLN(F("ISI")); // ISI = Incorrect signing identifier
return false;
}
@@ -121,18 +116,13 @@ bool MySigningAtsha204Soft::putNonce(MyMessage &msg) {
bool MySigningAtsha204Soft::signMsg(MyMessage &msg) {
// If we cannot fit any signature in the message, refuse to sign it
if (mGetLength(msg) > MAX_PAYLOAD-2) {
DEBUG_ATSHASOFT_PRINTLN("Cannot fit any signature to this message");
DEBUG_SIGNING_PRINTLN(F("MTOL")); // Message too large for signature to fit
return false;
}
// Calculate signature of message
mSetSigned(msg, 1); // make sure signing flag is set before signature is calculated
DEBUG_ATSHASOFT_PRINTBUF("Message to sign:", (uint8_t*)&msg.data[1-HEADER_SIZE], MAX_MESSAGE_LENGTH-1-(MAX_PAYLOAD-mGetLength(msg)));
if (!calculateSignature(msg)) {
DEBUG_ATSHASOFT_PRINTLN("Failed to calculate signature");
mSetSigned(msg, 0); // make sure signing flag is truthful
return false;
}
calculateSignature(msg);
#ifdef MY_SECURE_NODE_WHITELISTING
// Salt the signature with the senders nodeId and the (hopefully) unique serial The Creator has provided
@@ -141,7 +131,7 @@ bool MySigningAtsha204Soft::signMsg(MyMessage &msg) {
Sha256.write(msg.sender);
for (int i=0; i<SHA204_SERIAL_SZ; i++) Sha256.write(node_serial_info[i]);
memcpy(hmac, Sha256.result(), 32);
DEBUG_ATSHASOFT_PRINTLN("Signature salted for whitelisting");
DEBUG_SIGNING_PRINTLN(F("SWS")); // SWS = Signature whitelist salted
#endif
// Overwrite the first byte in the signature with the signing identifier
@@ -155,7 +145,7 @@ bool MySigningAtsha204Soft::signMsg(MyMessage &msg) {
bool MySigningAtsha204Soft::verifyMsg(MyMessage &msg) {
if (!verification_ongoing) {
DEBUG_ATSHASOFT_PRINTLN("No active verification session");
DEBUG_SIGNING_PRINTLN(F("NAVS")); // NAVS = No active verification session
return false;
} else {
// Make sure we have not expired
@@ -166,24 +156,19 @@ bool MySigningAtsha204Soft::verifyMsg(MyMessage &msg) {
verification_ongoing = false;
if (msg.data[mGetLength(msg)] != SIGNING_IDENTIFIER) {
DEBUG_ATSHASOFT_PRINTLN("Incorrect signing identifier");
DEBUG_SIGNING_PRINTLN(F("ISI")); // ISI = Incorrect signing identifier
return false;
}
DEBUG_ATSHASOFT_PRINTBUF("Message to verify:", (uint8_t*)&msg.data[1-HEADER_SIZE], MAX_MESSAGE_LENGTH-1-(MAX_PAYLOAD-mGetLength(msg)));
DEBUG_ATSHASOFT_PRINTBUF("Signature in message:", (uint8_t*)&msg.data[mGetLength(msg)], MAX_PAYLOAD-mGetLength(msg));
// Get signature of message
if (!calculateSignature(msg)) {
DEBUG_ATSHASOFT_PRINTLN("Failed to calculate signature");
return false;
}
DEBUG_SIGNING_PRINTBUF(F("SIM:"), (uint8_t*)&msg.data[mGetLength(msg)], MAX_PAYLOAD-mGetLength(msg)); // SIM = Signature in message
calculateSignature(msg);
#ifdef MY_SECURE_NODE_WHITELISTING
// Look up the senders nodeId in our whitelist and salt the signature with that data
for (int j=0; j < whitlist_sz; j++) {
if (whitelist[j].nodeId == msg.sender) {
DEBUG_ATSHASOFT_PRINTLN("Sender found in whitelist");
DEBUG_SIGNING_PRINTLN(F("SIW")); // SIW = Sender found in whitelist
Sha256.init();
for (int i=0; i<32; i++) Sha256.write(hmac[i]);
Sha256.write(msg.sender);
@@ -199,22 +184,24 @@ bool MySigningAtsha204Soft::verifyMsg(MyMessage &msg) {
// Compare the caluclated signature with the provided signature
if (memcmp(&msg.data[mGetLength(msg)], hmac, MAX_PAYLOAD-mGetLength(msg))) {
DEBUG_ATSHASOFT_PRINTBUF("Signature bad. Calculated signature:", hmac, MAX_PAYLOAD-mGetLength(msg));
DEBUG_SIGNING_PRINTBUF(F("SNOK:"), hmac, MAX_PAYLOAD-mGetLength(msg)); // SNOK = Signature bad
#ifdef MY_SECURE_NODE_WHITELISTING
DEBUG_ATSHASOFT_PRINTLN("Is the sender whitelisted?");
DEBUG_SIGNING_PRINTLN(F("W?")); // W? = Is the sender whitelisted?
#endif
return false;
} else {
DEBUG_ATSHASOFT_PRINTLN("Signature ok");
DEBUG_SIGNING_PRINTLN(F("SOK")); // SOK = Signature OK
return true;
}
}
}
// Helper to calculate signature of msg (returned in hmac)
bool MySigningAtsha204Soft::calculateSignature(MyMessage &msg) {
void MySigningAtsha204Soft::calculateSignature(MyMessage &msg) {
memset(temp_message, 0, 32);
memcpy(temp_message, (uint8_t*)&msg.data[1-HEADER_SIZE], MAX_MESSAGE_LENGTH-1-(MAX_PAYLOAD-mGetLength(msg)));
DEBUG_SIGNING_PRINTBUF(F("MSG:"), (uint8_t*)&msg.data[1-HEADER_SIZE], MAX_MESSAGE_LENGTH-1-(MAX_PAYLOAD-mGetLength(msg))); // MSG = Message to sign
DEBUG_SIGNING_PRINTBUF(F("CNC:"), current_nonce, 32); // CNC = Current nonce
// ATSHA204 calculates the HMAC with a PSK and a SHA256 digest of the following data:
// 32 bytes zeroes
@@ -271,6 +258,5 @@ bool MySigningAtsha204Soft::calculateSignature(MyMessage &msg) {
memcpy(hmac, Sha256.resultHmac(), 32);
DEBUG_ATSHASOFT_PRINTBUF("HMAC:", hmac, 32);
return true;
DEBUG_SIGNING_PRINTBUF(F("HMAC:"), hmac, 32);
}
+1 -1
View File
@@ -55,7 +55,7 @@ private:
uint8_t hmacKey[32];
uint8_t rndPin;
uint8_t hmac[32];
bool calculateSignature(MyMessage &msg);
void calculateSignature(MyMessage &msg);
#ifdef MY_SECURE_NODE_WHITELISTING
uint8_t whitlist_sz;
const whitelist_entry_t* whitelist;
+8 -7
View File
@@ -3,20 +3,21 @@
Created by Patrick "Anticimex" Fallberg <patrick@fallberg.net>
*/
#include "MyConfig.h"
#include "MySigning.h"
#include "MySigningNone.h"
// Uncomment this to get some useful serial debug info (Serial.print and Serial.println expected)
//#define DEBUG_NONE_SIGNING
//#define DEBUG_SIGNING
#ifdef DEBUG_NONE_SIGNING
#define DEBUG_NONE_PRINTLN(args) Serial.println(args)
#ifdef DEBUG_SIGNING
#define DEBUG_SIGNING_PRINTLN(args) Serial.println(args)
#else
#define DEBUG_NONE_PRINTLN(args)
#define DEBUG_SIGNING_PRINTLN(args)
#endif
#ifdef DEBUG_NONE_SIGNING
static void DEBUG_NONE_PRINTBUF(char* str, uint8_t* buf, uint8_t sz)
#ifdef DEBUG_SIGNING
static void DEBUG_NONE_PRINTBUF(const __FlashStringHelper* str, uint8_t* buf, uint8_t sz)
{
int i;
Serial.println(str);
@@ -52,7 +53,7 @@ bool MySigningNone::putNonce(MyMessage &msg) {
bool MySigningNone::signMsg(MyMessage &msg) {
// If we cannot fit any signature in the message, refuse to sign it
if (mGetLength(msg) > MAX_PAYLOAD-2) {
DEBUG_NONE_PRINTLN("Cannot fit any signature to this message");
DEBUG_SIGNING_PRINTLN(F("MTOL")); // Message too large for signature to fit
return false;
}
mSetSigned(msg, 1); // make sure signing flag is set before signature is calculated
+6 -6
View File
@@ -18,14 +18,14 @@ atsha204Class::atsha204Class(uint8_t pin)
device_port_IN = portInputRegister(port);
}
uint8_t atsha204Class::getSerialNumber(uint8_t * response)
void atsha204Class::getSerialNumber(uint8_t * response)
{
uint8_t readCommand[READ_COUNT];
uint8_t readResponse[READ_4_RSP_SIZE];
/* read from bytes 0->3 of config zone */
uint8_t returnCode = sha204m_read(readCommand, readResponse, SHA204_ZONE_CONFIG, ADDRESS_SN03);
if (!returnCode) // should return 0 if successful
if (!returnCode)
{
for (int i=0; i<4; i++) // store bytes 0-3 into respones array
response[i] = readResponse[SHA204_BUFFER_POS_DATA+i];
@@ -43,7 +43,7 @@ uint8_t atsha204Class::getSerialNumber(uint8_t * response)
}
}
return returnCode;
return;
}
/* SWI bit bang functions */
@@ -212,9 +212,9 @@ uint8_t atsha204Class::swi_receive_bytes(uint8_t count, uint8_t *buffer)
/* Physical functions */
uint8_t atsha204Class::sha204c_sleep()
void atsha204Class::sha204c_sleep()
{
return swi_send_byte(SHA204_SWI_FLAG_SLEEP);
swi_send_byte(SHA204_SWI_FLAG_SLEEP);
}
uint8_t atsha204Class::sha204p_receive_response(uint8_t size, uint8_t *response)
@@ -289,7 +289,7 @@ uint8_t atsha204Class::sha204c_resync(uint8_t size, uint8_t *response)
// We lost communication. Send a Wake pulse and try
// to receive a response (steps 2 and 3 of the
// re-synchronization process).
(void) sha204c_sleep();
sha204c_sleep();
ret_code = sha204c_wakeup(response);
// Translate a return value of success into one
+5 -5
View File
@@ -233,15 +233,15 @@ private:
uint8_t swi_send_byte(uint8_t value);
uint8_t sha204p_receive_response(uint8_t size, uint8_t *response);
uint8_t sha204m_read(uint8_t *tx_buffer, uint8_t *rx_buffer, uint8_t zone, uint16_t address);
uint8_t sha204c_wakeup(uint8_t *response);
uint8_t sha204c_resync(uint8_t size, uint8_t *response);
uint8_t sha204c_send_and_receive(uint8_t *tx_buffer, uint8_t rx_size, uint8_t *rx_buffer, uint8_t execution_delay, uint8_t execution_timeout);
public:
atsha204Class(uint8_t pin); // Constructor
uint8_t sha204c_wakeup(uint8_t *response);
uint8_t sha204c_sleep();
uint8_t sha204c_send_and_receive(uint8_t *tx_buffer, uint8_t rx_size, uint8_t *rx_buffer, uint8_t execution_delay, uint8_t execution_timeout);
uint8_t sha204c_resync(uint8_t size, uint8_t *response);
void sha204c_sleep();
uint8_t sha204m_execute(uint8_t op_code, uint8_t param1, uint16_t param2,
uint8_t datalen1, uint8_t *data1, uint8_t tx_size, uint8_t *tx_buffer, uint8_t rx_size, uint8_t *rx_buffer);
uint8_t getSerialNumber(uint8_t *response);
void getSerialNumber(uint8_t *response);
};
#endif