Security personalizer rewritten (#794)

* Removed ability to lock data section
* Introduced "guided mode" to simplify process
* Refactored the sketch to be less monolithic
* Print details and instructions in tables
* Use device unique ID as serial when
* Calculate and store personalization checksum
* Print serial in a copy+paste friendly manner (fixes #804)
* Clarify meaning of UART confirmation (fixes #805)
This commit is contained in:
Patrick Fallberg
2017-04-02 15:37:21 +02:00
committed by GitHub
parent 3350d79fe6
commit 7e6db413c7
14 changed files with 1794 additions and 954 deletions
+5 -2
View File
@@ -36,7 +36,8 @@
#define SIZE_PARENT_NODE_ID (1u) //!< Size parent node ID
#define SIZE_DISTANCE (1u) //!< Size GW distance
#define SIZE_ROUTES (256u) //!< Size routing table
#define SIZE_CONTROLLER_CONFIG (24u) //!< Size controller config
#define SIZE_CONTROLLER_CONFIG (23u) //!< Size controller config
#define SIZE_PERSONALIZATION_CHECKSUM (1u) //!< Size personalization checksum
#define SIZE_FIRMWARE_TYPE (2u) //!< Size firmware type
#define SIZE_FIRMWARE_VERSION (2u) //!< Size firmware version
#define SIZE_FIRMWARE_BLOCKS (2u) //!< Size firmware blocks
@@ -61,8 +62,10 @@
#define EEPROM_ROUTES_ADDRESS (EEPROM_DISTANCE_ADDRESS + SIZE_DISTANCE)
/** @brief Address configuration bytes sent by controller */
#define EEPROM_CONTROLLER_CONFIG_ADDRESS (EEPROM_ROUTES_ADDRESS + SIZE_ROUTES)
/** @brief Personalization checksum (set by SecurityPersonalizer.ino) */
#define EEPROM_PERSONALIZATION_CHECKSUM_ADDRESS (EEPROM_CONTROLLER_CONFIG_ADDRESS + SIZE_CONTROLLER_CONFIG)
/** @brief Address firmware type */
#define EEPROM_FIRMWARE_TYPE_ADDRESS (EEPROM_CONTROLLER_CONFIG_ADDRESS + SIZE_CONTROLLER_CONFIG)
#define EEPROM_FIRMWARE_TYPE_ADDRESS (EEPROM_PERSONALIZATION_CHECKSUM_ADDRESS + SIZE_PERSONALIZATION_CHECKSUM)
/** @brief Address firmware version */
#define EEPROM_FIRMWARE_VERSION_ADDRESS (EEPROM_FIRMWARE_TYPE_ADDRESS + SIZE_FIRMWARE_TYPE)
/** @brief Address firmware blocks */
+5
View File
@@ -59,6 +59,11 @@ uint8_t hwReadConfig(int adr);
typedef uint8_t unique_id_t[16];
/**
* Sleep for a defined time, using minimum power.
* @param ms Time to sleep, in [ms].
* @return -1.
*/
int8_t hwSleep(unsigned long ms);
/**
+1
View File
@@ -118,6 +118,7 @@ void _begin(void)
ledsInit();
#endif
(void)signerValidatePersonalization();
signerInit();
// Read latest received controller configuration from EEPROM
+27 -2
View File
@@ -62,7 +62,7 @@ enum { SIGN_WAITING_FOR_NONCE = 0, SIGN_OK = 1 };
#define CLEAR_WHITELIST(node) (_doWhitelist[node>>3]|=(1<<node%8))
#if defined(MY_SIGNING_SOFT)
extern void signerAtsha204SoftInit(void);
extern bool signerAtsha204SoftInit(void);
extern bool signerAtsha204SoftCheckTimer(void);
extern bool signerAtsha204SoftGetNonce(MyMessage &msg);
extern void signerAtsha204SoftPutNonce(MyMessage &msg);
@@ -75,7 +75,7 @@ extern bool signerAtsha204SoftSignMsg(MyMessage &msg);
#define signerBackendVerifyMsg signerAtsha204SoftVerifyMsg
#define signerBackendSignMsg signerAtsha204SoftSignMsg
#elif defined(MY_SIGNING_ATSHA204)
extern void signerAtsha204Init(void);
extern bool signerAtsha204Init(void);
extern bool signerAtsha204CheckTimer(void);
extern bool signerAtsha204GetNonce(MyMessage &msg);
extern void signerAtsha204PutNonce(MyMessage &msg);
@@ -112,6 +112,31 @@ void signerInit(void)
#endif
}
bool signerValidatePersonalization(void)
{
uint8_t buffer[32];
uint8_t* hash;
uint8_t checksum;
signerSha256Init();
hwReadConfigBlock((void*)buffer, (void*)EEPROM_SIGNING_SOFT_HMAC_KEY_ADDRESS, 32);
signerSha256Update(buffer, 32);
hwReadConfigBlock((void*)buffer, (void*)EEPROM_RF_ENCRYPTION_AES_KEY_ADDRESS, 16);
signerSha256Update(buffer, 16);
hwReadConfigBlock((void*)buffer, (void*)EEPROM_SIGNING_SOFT_SERIAL_ADDRESS, 9);
signerSha256Update(buffer, 9);
hash = signerSha256Final();
hwReadConfigBlock((void*)&checksum, (void*)EEPROM_PERSONALIZATION_CHECKSUM_ADDRESS, 1);
if (checksum != hash[0]) {
SIGN_DEBUG(PSTR("WARNING! Personalization data in EEPROM is tampered!\n"));
SIGN_DEBUG(PSTR("If you are sure that the personalization data in EEPROM is ok,\n"));
SIGN_DEBUG(
PSTR("execute SecurityPersonalizer.ino unmodified to get a new checksum stored.\n"));
return false;
} else {
return true;
}
}
void signerPresentation(MyMessage &msg, uint8_t destination)
{
prepareSigningPresentation(msg, destination);
+39 -66
View File
@@ -195,76 +195,39 @@
* @anchor personalization If you use the “real” ATSHA204A, before any signing operations can be done, the device needs to be <i>personalized</i>.
* This can be a daunting process as it involves irreversibly writing configurations to the device, which cannot be undone. I have however tried
* to simplify the process as much as possibly by creating a helper-sketch specifically for this purpose in @ref SecurityPersonalizer.ino
* Note that you also need to do personalization for software signing, but then the values are stored in EEPROM (described below).
* Note that you also need to do personalization for software signing, but then the values are stored in EEPROM.
*
* The process of personalizing the ATSHA204A involves
* - Writing and locking chip configuration
* - (optionally) Generating and (mandatory) writing HMAC key
* - (optionally) Locking data sections
* To personalize a ATSHA204A do the following procedure:
* 1. Enable @ref GENERATE_KEYS_ATSHA204A<br>
* This will lock the ATSHA204A and generate random keys for HMAC (signing) and AES (encryption).
* Copy the keys generated and replace the corresponding definitions under
* "User defined key data", specifically @ref MY_HMAC_KEY and @ref MY_AES_KEY.
* 2. Disable @ref GENERATE_KEYS_ATSHA204A and enable @ref PERSONALIZE_ATSHA204A<br>
* This will store the HMAC key to the ATSHA204A and the AES key to EEPROM. It will also write
* a checksum of the personalization data in EEPROM to be able to detect if the data is
* altered.<br>
* Personalization is now complete.
*
* First execute the sketch without modifications to make sure communications with the device can be established. It is highly recommended that
* the first time this is done, a device with serial debug possibilities is used.<br>
* When this has been confirmed, it is time to decide what type of personalization is desired.<br>
* There are a few options here.<br>
* Firstly, enable @ref LOCK_CONFIGURATION to allow the sketch to lock the chip configuration. The sketch will write the default settings to the
* chip because these are fine for our purposes. This also enables the RNG which is required to allow the sketch to automatically generate a PSK
* if this is desired.<br>
* Now it is possible to execute the sketch to lock the configuration and enable the RNG.<br>
* Next step is to decide if a new key should be generated or an existing key should be stored to the device. This is determined using
* @ref USER_KEY, which, if defined, will use the data in the variable user_key_data.<br>
* If @ref USER_KEY is disabled, the RNG will be used to generate a key. This key obviously need to be made available to you so you can use
* it in other devices in the network, and this key is therefore also printed on the serial console when it has been generated.<br>
* The key (generated or provided) will be written to the device unless @ref SKIP_KEY_STORAGE is set. As long as the data zone is kept unlocked
* the key can be replaced at any time. However, Atmel suggests the data region to be locked for maximum security. On the other hand, they also
* claim that the key is not readable from the device even if the data zone remains unlocked so the need for locking the data region is optional
* for MySensors usage.<br>
* For devices that does not have serial debug possibilities, it is possible to set @ref SKIP_UART_CONFIRMATION, but it is required to set
* @ref USER_KEY if this option is enabled since a generated and potentially unknown key could be written to the device and thus rendering
* it useless (if the data zone is also locked).<br>
* For devices with serial debug possibilities it is recommended to not use @ref SKIP_UART_CONFIRMATION as the sketch without that setting will
* ask user to send a ‘space’ character on the serial terminal before any locking operations are executed as an additional confirmation that
* this irreversible operation is done. However, if a number of nodes are to undergo personalization, this option can be enabled to streamline
* the personalization.<br>
* This is a condensed description of settings to fully personalize and lock down a set of sensors (and gateways):
* - Pick a “master” device with serial debug port.
* - Set the following sketch configuration of the personalizer:
* -# Enable @ref LOCK_CONFIGURATION
* -# Disable @ref LOCK_DATA
* -# Enable @ref SKIP_KEY_STORAGE
* -# Disable @ref SKIP_UART_CONFIRMATION
* -# Disable @ref USER_KEY
* - Execute the sketch on the “master” device to obtain a randomized key. Save this key to a secure location and keep it confidential so that\n
you can retrieve it if you need to personalize more devices later on.
* - Now reconfigure the sketch with these settings:
* -# Enable @ref LOCK_CONFIGURATION
* -# Enable @ref LOCK_DATA (if you are sure you do not need to replace/revoke the key, this is the most secure option to protect from key readout\n
* according to Atmel, but they also claim that key is not readable even if data region remains unlocked from the slot we are using)
* -# Disable @ref SKIP_KEY_STORAGE
* -# Enable @ref SKIP_UART_CONFIRMATION
* -# Enable @ref USER_KEY
* - Put the saved key in the user_key_data variable.
* - Now execute the sketch on all devices you want to personalize with this secret key.
* To personalize for software signing do the following procedure:
* 1. Enable @ref GENERATE_KEYS_SOFT<br>
* This will generate random keys for HMAC (signing) and AES (encryption).
* Copy the keys generated and replace the corresponding definitions under
* "User defined key data", specifically @ref MY_HMAC_KEY and @ref MY_AES_KEY.
* 2. Disable @ref GENERATE_KEYS_SOFT and enable @ref PERSONALIZE_SOFT<br>
* This will store the HMAC key and the AES key to EEPROM. It will also write
* a checksum of the personalization data in EEPROM to be able to detect if the data is
* altered.<br>
* Personalization is now complete.
*
* For software signing the personalization procedure looks slightly different.<br>
* - If you do not have the ATSHA204A device and need to generate random keys:
* -# Enable @ref USE_SOFT_SIGNING
* - If you do have the ATSHA204A and want to use it to generate random keys for soft signing:
* -# Disable @ref USE_SOFT_SIGNING
* - If you want to review existing EEPROM configuration to determine if anything needs to be updated:
* -# Make sure to disable any ATSHA204A update features if you use it (enable @ref SKIP_KEY_STORAGE, disable @ref LOCK_CONFIGURATION and @ref LOCK_DATA)
* -# Disable @ref STORE_SOFT_KEY
* -# Disable @ref STORE_SOFT_SERIAL
* -# Disable @ref STORE_AES_KEY
* - Execute the sketch and the EEPROM configuration will be printed on the serial console.
* - Decide what you want to update and enable the corresponding flags (@ref STORE_SOFT_KEY, @ref STORE_SOFT_SERIAL, @ref STORE_AES_KEY).
* - If you have existing keys and want to use them instead of having the values randomized:
* - Enable the corresponding flag (@ref USER_SOFT_KEY, @ref USER_SOFT_SERIAL, @ref USER_AES_KEY) and also set the appropriate values
* (@ref user_soft_key_data, @ref user_soft_serial, @ref user_aes_key)
* If you want to use soft signing and you want to use whitelisting (the ability to revoke/ban
* compromised nodes in the network) and your target does not provide a unique device ID, you have
* to generate a unique serial and store it in EEPROM. This can be done by replacing
* @ref PERSONALIZE_SOFT in step 2 above with @ref PERSONALIZE_SOFT_RANDOM_SERIAL. See the output
* under "Hardware security peripherals" to determine if this is necessary.
*
* The personalizer also takes care of generation and storage of the AES key used for RF traffic encryption.
* See the software signing personalization flow for how to manage it.
*
* That’s it. Personalization is done and the device is ready to execute signing operations which are valid only on your personal network.
* When you have personalized your first device after step 2 above, you can run the same sketch on
* all devices in your network that needs to be personalized in a compatible manner. Pick
* @ref PERSONALIZE_ATSHA204A or @ref PERSONALIZE_SOFT as needed by the hardware.
*
* If a node does require signing, any unsigned message sent to the node will be rejected.<br>
* This also applies to the gateway. However, the difference is that the gateway will only require signed messages from nodes it knows in turn
@@ -499,6 +462,16 @@ typedef struct {
*/
void signerInit(void);
/**
* @brief Validates personalization data
*
* This function checks that the personalization details are in order.
* \n@b Usage: This fuction should be called before any signing related operations take place.
*
* @returns @c true if personalization data is valid.
*/
bool signerValidatePersonalization(void);
/**
* @brief Does signing specific presentation for a node.
*
+158 -67
View File
@@ -27,7 +27,13 @@
#include "MySigning.h"
#define SIGNING_IDENTIFIER (1)
#define SIGNING_IDENTIFIER (1) //HMAC-SHA256
#if defined(MY_DEBUG_VERBOSE_SIGNING)
#define SIGN_DEBUG(x,...) hwDebugPrint(x, ##__VA_ARGS__)
#else
#define SIGN_DEBUG(x,...)
#endif
// Define MY_DEBUG_VERBOSE_SIGNING in your sketch to enable signing backend debugprints
@@ -47,59 +53,36 @@ const whitelist_entry_t _signing_whitelist[] = MY_SIGNING_NODE_WHITELISTING;
static void signerCalculateSignature(MyMessage &msg, bool signing);
static uint8_t* signerSha256(const uint8_t* data, size_t sz);
static bool init_ok = false;
#ifdef MY_DEBUG_VERBOSE_SIGNING
static char i2h(uint8_t i)
{
uint8_t k = i & 0x0F;
if (k <= 9) {
return '0' + k;
} else {
return 'A' + k - 10;
}
}
static void DEBUG_SIGNING_PRINTBUF(const __FlashStringHelper* str, uint8_t* buf, uint8_t sz)
{
static char printBuffer[300];
if (NULL == buf) {
return;
}
#ifdef MY_GATEWAY_FEATURE
// prepend debug message to be handled correctly by controller (C_INTERNAL, I_LOG_MESSAGE)
snprintf_P(printBuffer, 299, PSTR("0;255;%d;0;%d;"), C_INTERNAL, I_LOG_MESSAGE);
MY_SERIALDEVICE.print(printBuffer);
#endif
for (int i=0; i<sz; i++) {
printBuffer[i * 2] = i2h(buf[i] >> 4);
printBuffer[(i * 2) + 1] = i2h(buf[i]);
}
printBuffer[sz * 2] = '\0';
#ifdef MY_GATEWAY_FEATURE
// Truncate message if this is gateway node
printBuffer[MY_GATEWAY_MAX_SEND_LENGTH-1-strlen_P((const char*)str)] = '\0';
#endif
MY_SERIALDEVICE.print(str);
if (sz > 0) {
MY_SERIALDEVICE.print(printBuffer);
}
MY_SERIALDEVICE.println("");
}
#else
#define DEBUG_SIGNING_PRINTBUF(str, buf, sz)
#endif
void signerAtsha204Init(void)
bool signerAtsha204Init(void)
{
init_ok = true;
atsha204_init(MY_SIGNING_ATSHA204_PIN);
(void)atsha204_wakeup(_signing_temp_message);
// 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"));
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"));
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) {
DEBUG_SIGNING_PRINTBUF(F("Verification timeout"), NULL, 0);
SIGN_DEBUG(PSTR("Verification timeout\n"));
// Purge nonce
memset(_signing_signing_nonce, 0x00, NONCE_NUMIN_SIZE_PASSTHROUGH);
memset(_signing_verifying_nonce, 0x00, NONCE_NUMIN_SIZE_PASSTHROUGH);
@@ -112,14 +95,18 @@ bool signerAtsha204CheckTimer(void)
bool signerAtsha204GetNonce(MyMessage &msg)
{
DEBUG_SIGNING_PRINTBUF(F("Signing backend: ATSHA204"), NULL, 0);
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
(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) {
DEBUG_SIGNING_PRINTBUF(F("Failed to generate nonce"), NULL, 0);
SIGN_DEBUG(PSTR("Failed to generate nonce\n"));
return false;
}
for (int i = 0; i < 32; i++) {
@@ -147,7 +134,11 @@ bool signerAtsha204GetNonce(MyMessage &msg)
void signerAtsha204PutNonce(MyMessage &msg)
{
DEBUG_SIGNING_PRINTBUF(F("Signing backend: ATSHA204"), NULL, 0);
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
@@ -158,7 +149,7 @@ bool signerAtsha204SignMsg(MyMessage &msg)
{
// If we cannot fit any signature in the message, refuse to sign it
if (mGetLength(msg) > MAX_PAYLOAD-2) {
DEBUG_SIGNING_PRINTBUF(F("Message too large"), NULL, 0);
SIGN_DEBUG(PSTR("Message too large\n"));
return false;
}
@@ -174,7 +165,10 @@ bool signerAtsha204SignMsg(MyMessage &msg)
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
DEBUG_SIGNING_PRINTBUF(F("Signature salted with serial"), NULL, 0);
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]);
}
// Put device back to sleep
@@ -186,8 +180,6 @@ bool signerAtsha204SignMsg(MyMessage &msg)
// 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));
DEBUG_SIGNING_PRINTBUF(F("Signature in message: "), (uint8_t*)&msg.data[mGetLength(msg)],
MAX_PAYLOAD-mGetLength(msg));
return true;
}
@@ -195,7 +187,7 @@ bool signerAtsha204SignMsg(MyMessage &msg)
bool signerAtsha204VerifyMsg(MyMessage &msg)
{
if (!_signing_verification_ongoing) {
DEBUG_SIGNING_PRINTBUF(F("No active verification session"), NULL, 0);
SIGN_DEBUG(PSTR("No active verification session\n"));
return false;
} else {
// Make sure we have not expired
@@ -206,12 +198,10 @@ bool signerAtsha204VerifyMsg(MyMessage &msg)
_signing_verification_ongoing = false;
if (msg.data[mGetLength(msg)] != SIGNING_IDENTIFIER) {
DEBUG_SIGNING_PRINTBUF(F("Incorrect signing identifier"), NULL, 0);
SIGN_DEBUG(PSTR("Incorrect signing identifier\n"));
return false;
}
DEBUG_SIGNING_PRINTBUF(F("Signature in message: "), (uint8_t*)&msg.data[mGetLength(msg)],
MAX_PAYLOAD-mGetLength(msg));
signerCalculateSignature(msg, false); // Get signature of message
#ifdef MY_SIGNING_NODE_WHITELISTING
@@ -219,7 +209,7 @@ bool signerAtsha204VerifyMsg(MyMessage &msg)
size_t j;
for (j=0; j < NUM_OF(_signing_whitelist); j++) {
if (_signing_whitelist[j].nodeId == msg.sender) {
DEBUG_SIGNING_PRINTBUF(F("Sender found in whitelist"), NULL, 0);
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
_signing_verifying_nonce[32] = msg.sender;
@@ -230,7 +220,7 @@ bool signerAtsha204VerifyMsg(MyMessage &msg)
}
}
if (j == NUM_OF(_signing_whitelist)) {
DEBUG_SIGNING_PRINTBUF(F("Sender not found in whitelist, message rejected!"), NULL, 0);
SIGN_DEBUG(PSTR("Sender not found in whitelist, message rejected!\n"));
// Put device back to sleep
atsha204_sleep();
return false;
@@ -246,14 +236,13 @@ bool signerAtsha204VerifyMsg(MyMessage &msg)
// 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))) {
DEBUG_SIGNING_PRINTBUF(F("Signature bad: "), &_signing_rx_buffer[SHA204_BUFFER_POS_DATA],
MAX_PAYLOAD-mGetLength(msg));
SIGN_DEBUG(PSTR("Signature bad\n"));
#ifdef MY_SIGNING_NODE_WHITELISTING
DEBUG_SIGNING_PRINTBUF(F("Is the sender whitelisted and serial correct?"), NULL, 0);
SIGN_DEBUG(PSTR("Is the sender whitelisted and serial correct?\n"));
#endif
return false;
} else {
DEBUG_SIGNING_PRINTBUF(F("Signature OK"), NULL, 0);
SIGN_DEBUG(PSTR("Signature OK\n"));
return true;
}
}
@@ -268,10 +257,42 @@ static void signerCalculateSignature(MyMessage &msg, bool signing)
MAX_MESSAGE_LENGTH-1-(MAX_PAYLOAD-mGetLength(msg)));
// Program the data to sign into the ATSHA204
DEBUG_SIGNING_PRINTBUF(F("Message to process: "), (uint8_t*)&msg.data[1-HEADER_SIZE],
MAX_MESSAGE_LENGTH-1-(MAX_PAYLOAD-mGetLength(msg)));
DEBUG_SIGNING_PRINTBUF(F("Current nonce: "),
signing ? _signing_signing_nonce : _signing_verifying_nonce, 32);
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,
WRITE_COUNT_LONG, _signing_tx_buffer, WRITE_RSP_SIZE, _signing_rx_buffer);
@@ -293,7 +314,42 @@ static void signerCalculateSignature(MyMessage &msg, bool signing)
(void)atsha204_execute(SHA204_HMAC, HMAC_MODE_SOURCE_FLAG_MATCH, 0, 0, NULL,
HMAC_COUNT, _signing_tx_buffer, HMAC_RSP_SIZE, _signing_rx_buffer);
DEBUG_SIGNING_PRINTBUF(F("HMAC: "), &_signing_rx_buffer[SHA204_BUFFER_POS_DATA], 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_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]
);
}
// Helper to calculate a generic SHA256 digest of provided buffer (only supports one block)
@@ -314,6 +370,41 @@ static uint8_t* signerSha256(const uint8_t* data, size_t sz)
(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);
DEBUG_SIGNING_PRINTBUF(F("SHA256: "), &_signing_rx_buffer[SHA204_BUFFER_POS_DATA], 32);
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];
}
+94 -74
View File
@@ -31,7 +31,13 @@
#include "MySigning.h"
#include "drivers/ATSHA204/sha256.h"
#define SIGNING_IDENTIFIER (1)
#define SIGNING_IDENTIFIER (1) //HMAC-SHA256
#if defined(MY_DEBUG_VERBOSE_SIGNING)
#define SIGN_DEBUG(x,...) hwDebugPrint(x, ##__VA_ARGS__)
#else
#define SIGN_DEBUG(x,...)
#endif
// Define MY_DEBUG_VERBOSE_SIGNING in your sketch to enable signing backend debugprints
@@ -52,69 +58,39 @@ const whitelist_entry_t _signing_whitelist[] = MY_SIGNING_NODE_WHITELISTING;
static void signerCalculateSignature(MyMessage &msg, bool signing);
#ifdef MY_DEBUG_VERBOSE_SIGNING
static char i2h(uint8_t i)
{
uint8_t k = i & 0x0F;
if (k <= 9) {
return '0' + k;
} else {
return 'A' + k - 10;
}
}
static bool init_ok = false;
static const uint8_t reset_serial[9] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
#ifdef __linux__
#define __FlashStringHelper char
#define MY_SERIALDEVICE.print debug
#endif
static void DEBUG_SIGNING_PRINTBUF(const __FlashStringHelper* str, uint8_t* buf, uint8_t sz)
bool signerAtsha204SoftInit(void)
{
static char printBuffer[300];
if (NULL == buf) {
return;
}
#if defined(MY_GATEWAY_FEATURE) && !defined(__linux__)
// prepend debug message to be handled correctly by controller (C_INTERNAL, I_LOG_MESSAGE)
snprintf_P(printBuffer, 299, PSTR("0;255;%d;0;%d;"), C_INTERNAL, I_LOG_MESSAGE);
MY_SERIALDEVICE.print(printBuffer);
#endif
for (int i=0; i<sz; i++) {
printBuffer[i * 2] = i2h(buf[i] >> 4);
printBuffer[(i * 2) + 1] = i2h(buf[i]);
}
printBuffer[sz * 2] = '\0';
#if defined(MY_GATEWAY_FEATURE) && !defined(__linux__)
// Truncate message if this is gateway node
printBuffer[MY_GATEWAY_MAX_SEND_LENGTH-1-strlen_P((const char*)str)] = '\0';
#endif
MY_SERIALDEVICE.print(str);
if (sz > 0) {
MY_SERIALDEVICE.print(printBuffer);
}
#if !defined(__linux__)
MY_SERIALDEVICE.println("");
#endif
}
#else
#define DEBUG_SIGNING_PRINTBUF(str, buf, sz)
#endif
init_ok = true;
void signerAtsha204SoftInit(void)
{
// initialize pseudo-RNG
hwRandomNumberInit();
// Set secrets
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);
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
if (hwUniqueID(&uniqueID)) {
// There is a unique serial, use that
memcpy(_signing_node_serial_info, uniqueID, 9);
}
}
return init_ok;
}
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) {
DEBUG_SIGNING_PRINTBUF(F("Verification timeout"), NULL, 0);
SIGN_DEBUG(PSTR("Verification timeout\n"));
// Purge nonce
memset(_signing_signing_nonce, 0xAA, 32);
memset(_signing_verifying_nonce, 0xAA, 32);
@@ -127,7 +103,11 @@ bool signerAtsha204SoftCheckTimer(void)
bool signerAtsha204SoftGetNonce(MyMessage &msg)
{
DEBUG_SIGNING_PRINTBUF(F("Signing backend: ATSHA204Soft"), NULL, 0);
if (!init_ok) {
SIGN_DEBUG(PSTR("Backend not initialized\n"));
return false;
}
SIGN_DEBUG(PSTR("Signing backend: ATSHA204Soft\n"));
// 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
@@ -136,7 +116,6 @@ bool signerAtsha204SoftGetNonce(MyMessage &msg)
_signing_sha256.write(random(256) ^ (hwMillis()&0xFF));
}
memcpy(_signing_verifying_nonce, _signing_sha256.result(), MAX_PAYLOAD);
DEBUG_SIGNING_PRINTBUF(F("SHA256: "), _signing_verifying_nonce, 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, sizeof(_signing_verifying_nonce)-MAX_PAYLOAD);
@@ -156,7 +135,10 @@ bool signerAtsha204SoftGetNonce(MyMessage &msg)
void signerAtsha204SoftPutNonce(MyMessage &msg)
{
DEBUG_SIGNING_PRINTBUF(F("Signing backend: ATSHA204Soft"), NULL, 0);
if (!init_ok) {
SIGN_DEBUG(PSTR("Backend not initialized\n"));
}
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
@@ -167,7 +149,7 @@ bool signerAtsha204SoftSignMsg(MyMessage &msg)
{
// If we cannot fit any signature in the message, refuse to sign it
if (mGetLength(msg) > MAX_PAYLOAD-2) {
DEBUG_SIGNING_PRINTBUF(F("Message too large"), NULL, 0);
SIGN_DEBUG(PSTR("Message too large\n"));
return false;
}
@@ -186,8 +168,10 @@ bool signerAtsha204SoftSignMsg(MyMessage &msg)
_signing_sha256.write(_signing_node_serial_info[i]);
}
memcpy(_signing_hmac, _signing_sha256.result(), 32);
DEBUG_SIGNING_PRINTBUF(F("SHA256: "), _signing_hmac, 32);
DEBUG_SIGNING_PRINTBUF(F("Signature salted with serial"), NULL, 0);
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]);
}
// Overwrite the first byte in the signature with the signing identifier
@@ -195,8 +179,6 @@ bool signerAtsha204SoftSignMsg(MyMessage &msg)
// Transfer as much signature data as the remaining space in the message permits
memcpy(&msg.data[mGetLength(msg)], _signing_hmac, MAX_PAYLOAD-mGetLength(msg));
DEBUG_SIGNING_PRINTBUF(F("Signature in message: "), (uint8_t*)&msg.data[mGetLength(msg)],
MAX_PAYLOAD-mGetLength(msg));
return true;
}
@@ -204,7 +186,7 @@ bool signerAtsha204SoftSignMsg(MyMessage &msg)
bool signerAtsha204SoftVerifyMsg(MyMessage &msg)
{
if (!_signing_verification_ongoing) {
DEBUG_SIGNING_PRINTBUF(F("No active verification session"), NULL, 0);
SIGN_DEBUG(PSTR("No active verification session\n"));
return false;
} else {
// Make sure we have not expired
@@ -215,21 +197,18 @@ bool signerAtsha204SoftVerifyMsg(MyMessage &msg)
_signing_verification_ongoing = false;
if (msg.data[mGetLength(msg)] != SIGNING_IDENTIFIER) {
DEBUG_SIGNING_PRINTBUF(F("Incorrect signing identifier"), NULL, 0);
SIGN_DEBUG(PSTR("Incorrect signing identifier\n"));
return false;
}
// Get signature of message
DEBUG_SIGNING_PRINTBUF(F("Signature in message: "), (uint8_t*)&msg.data[mGetLength(msg)],
MAX_PAYLOAD-mGetLength(msg));
signerCalculateSignature(msg, false);
signerCalculateSignature(msg, false); // Get signature of message
#ifdef MY_SIGNING_NODE_WHITELISTING
// Look up the senders nodeId in our whitelist and salt the signature with that data
size_t j;
for (j=0; j < NUM_OF(_signing_whitelist); j++) {
if (_signing_whitelist[j].nodeId == msg.sender) {
DEBUG_SIGNING_PRINTBUF(F("Sender found in whitelist"), NULL, 0);
SIGN_DEBUG(PSTR("Sender found in whitelist\n"));
_signing_sha256.init();
for (int i=0; i<32; i++) {
_signing_sha256.write(_signing_hmac[i]);
@@ -239,12 +218,11 @@ bool signerAtsha204SoftVerifyMsg(MyMessage &msg)
_signing_sha256.write(_signing_whitelist[j].serial[i]);
}
memcpy(_signing_hmac, _signing_sha256.result(), 32);
DEBUG_SIGNING_PRINTBUF(F("SHA256: "), _signing_hmac, 32);
break;
}
}
if (j == NUM_OF(_signing_whitelist)) {
DEBUG_SIGNING_PRINTBUF(F("Sender not found in whitelist, message rejected!"), NULL, 0);
SIGN_DEBUG(PSTR("Sender not found in whitelist, message rejected!\n"));
return false;
}
#endif
@@ -254,13 +232,13 @@ bool signerAtsha204SoftVerifyMsg(MyMessage &msg)
// Compare the caluclated signature with the provided signature
if (signerMemcmp(&msg.data[mGetLength(msg)], _signing_hmac, MAX_PAYLOAD-mGetLength(msg))) {
DEBUG_SIGNING_PRINTBUF(F("Signature bad: "), _signing_hmac, MAX_PAYLOAD-mGetLength(msg));
SIGN_DEBUG(PSTR("Signature bad\n"));
#ifdef MY_SIGNING_NODE_WHITELISTING
DEBUG_SIGNING_PRINTBUF(F("Is the sender whitelisted and serial correct?"), NULL, 0);
SIGN_DEBUG(PSTR("Is the sender whitelisted and serial correct?\n"));
#endif
return false;
} else {
DEBUG_SIGNING_PRINTBUF(F("Signature OK"), NULL, 0);
SIGN_DEBUG(PSTR("Signature OK\n"));
return true;
}
}
@@ -272,10 +250,42 @@ 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)));
DEBUG_SIGNING_PRINTBUF(F("Message to process: "), (uint8_t*)&msg.data[1-HEADER_SIZE],
MAX_MESSAGE_LENGTH-1-(MAX_PAYLOAD-mGetLength(msg)));
DEBUG_SIGNING_PRINTBUF(F("Current nonce: "),
signing ? _signing_signing_nonce : _signing_verifying_nonce, 32);
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]
);
// ATSHA204 calculates the HMAC with a PSK and a SHA256 digest of the following data:
// 32 bytes zeroes
@@ -348,5 +358,15 @@ static void signerCalculateSignature(MyMessage &msg, bool signing)
memcpy(_signing_hmac, _signing_sha256.resultHmac(), 32);
DEBUG_SIGNING_PRINTBUF(F("HMAC: "), _signing_hmac, 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]);
}
+21 -25
View File
@@ -14,7 +14,6 @@ static uint8_t swi_receive_bytes(uint8_t count, uint8_t *buffer);
static uint8_t swi_send_bytes(uint8_t count, uint8_t *buffer);
static uint8_t swi_send_byte(uint8_t value);
static uint8_t sha204p_receive_response(uint8_t size, uint8_t *response);
static uint8_t sha204m_read(uint8_t *tx_buffer, uint8_t *rx_buffer, uint8_t zone, uint16_t address);
static uint8_t sha204c_resync(uint8_t size, uint8_t *response);
static 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);
@@ -373,27 +372,6 @@ static uint8_t sha204c_send_and_receive(uint8_t *tx_buffer, uint8_t rx_size, uin
return ret_code;
}
/* Marshaling functions */
static uint8_t sha204m_read(uint8_t *tx_buffer, uint8_t *rx_buffer, uint8_t zone, uint16_t address)
{
uint8_t rx_size;
address >>= 2;
tx_buffer[SHA204_COUNT_IDX] = READ_COUNT;
tx_buffer[SHA204_OPCODE_IDX] = SHA204_READ;
tx_buffer[READ_ZONE_IDX] = zone;
tx_buffer[READ_ADDR_IDX] = (uint8_t) (address & SHA204_ADDRESS_MASK);
tx_buffer[READ_ADDR_IDX + 1] = 0;
rx_size = (zone & SHA204_ZONE_COUNT_FLAG) ? READ_32_RSP_SIZE : READ_4_RSP_SIZE;
return sha204c_send_and_receive(&tx_buffer[0], rx_size, &rx_buffer[0], READ_DELAY,
READ_EXEC_MAX - READ_DELAY);
}
/* CRC Calculator and Checker */
static void sha204c_calculate_crc(uint8_t length, uint8_t *data, uint8_t *crc)
@@ -574,14 +552,14 @@ uint8_t atsha204_getSerialNumber(uint8_t * response)
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);
uint8_t returnCode = atsha204_read(readCommand, readResponse, SHA204_ZONE_CONFIG, ADDRESS_SN03);
if (!returnCode) {
for (int i=0; i<4; i++) {// store bytes 0-3 into respones array
response[i] = readResponse[SHA204_BUFFER_POS_DATA+i];
}
/* read from bytes 8->11 of config zone */
returnCode = sha204m_read(readCommand, readResponse, SHA204_ZONE_CONFIG, ADDRESS_SN47);
returnCode = atsha204_read(readCommand, readResponse, SHA204_ZONE_CONFIG, ADDRESS_SN47);
for (int i=4; i<8; i++) {// store bytes 4-7 of SN into response array
response[i] = readResponse[SHA204_BUFFER_POS_DATA+(i-4)];
@@ -589,10 +567,28 @@ uint8_t atsha204_getSerialNumber(uint8_t * response)
if (!returnCode) {
/* Finally if last two reads were successful, read byte 8 of the SN */
returnCode = sha204m_read(readCommand, readResponse, SHA204_ZONE_CONFIG, ADDRESS_SN8);
returnCode = atsha204_read(readCommand, readResponse, SHA204_ZONE_CONFIG, ADDRESS_SN8);
response[8] = readResponse[SHA204_BUFFER_POS_DATA]; // Byte 8 of SN should always be 0xEE
}
}
return returnCode;
}
uint8_t atsha204_read(uint8_t *tx_buffer, uint8_t *rx_buffer, uint8_t zone, uint16_t address)
{
uint8_t rx_size;
address >>= 2;
tx_buffer[SHA204_COUNT_IDX] = READ_COUNT;
tx_buffer[SHA204_OPCODE_IDX] = SHA204_READ;
tx_buffer[READ_ZONE_IDX] = zone;
tx_buffer[READ_ADDR_IDX] = (uint8_t) (address & SHA204_ADDRESS_MASK);
tx_buffer[READ_ADDR_IDX + 1] = 0;
rx_size = (zone & SHA204_ZONE_COUNT_FLAG) ? READ_32_RSP_SIZE : READ_4_RSP_SIZE;
return sha204c_send_and_receive(&tx_buffer[0], rx_size, &rx_buffer[0], READ_DELAY,
READ_EXEC_MAX - READ_DELAY);
}
+1
View File
@@ -247,6 +247,7 @@ uint8_t atsha204_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 atsha204_getSerialNumber(uint8_t *response);
uint8_t atsha204_read(uint8_t *tx_buffer, uint8_t *rx_buffer, uint8_t zone, uint16_t address);
#endif
#endif
+1 -2
View File
@@ -290,11 +290,10 @@ void SPIFlash::writeBytes(uint32_t addr, const void* buf, uint16_t len)
command(SPIFLASH_WRITEDISABLE); //end AAI programming
unselect();
#else
uint16_t n;
uint16_t maxBytes = 256-(addr%256); // force the first set of bytes to stay within the first page
uint16_t offset = 0;
while (len>0) {
n = (len<=maxBytes) ? len : maxBytes;
uint16_t n = (len<=maxBytes) ? len : maxBytes;
command(SPIFLASH_BYTEPAGEPROGRAM, true); // Byte/Page Program
SPI.transfer(addr >> 16);
SPI.transfer(addr >> 8);
+7 -8
View File
@@ -122,16 +122,16 @@ byte extEEPROM::begin(twiClockFreq_t twiFreq)
//from the Arduino Wire library is passed back through to the caller.
byte extEEPROM::write(unsigned long addr, byte *values, unsigned int nBytes)
{
uint8_t ctrlByte; //control byte (I2C device address & chip/block select bits)
uint8_t txStatus = 0; //transmit status
uint16_t nWrite; //number of bytes to write
uint16_t nPage; //number of bytes remaining on current page, starting at addr
if (addr + nBytes > _totalCapacity) { //will this write go past the top of the EEPROM?
return EEPROM_ADDR_ERR; //yes, tell the caller
}
while (nBytes > 0) {
uint8_t ctrlByte; //control byte (I2C device address & chip/block select bits)
uint16_t nWrite; //number of bytes to write
uint16_t nPage; //number of bytes remaining on current page, starting at addr
nPage = _pageSize - ( addr & (_pageSize - 1) );
//find min(nBytes, nPage, BUFFER_LENGTH) -- BUFFER_LENGTH is defined in the Wire library.
nWrite = nBytes < nPage ? nBytes : nPage;
@@ -178,16 +178,15 @@ byte extEEPROM::write(unsigned long addr, byte *values, unsigned int nBytes)
//from the Arduino Wire library is passed back through to the caller.
byte extEEPROM::read(unsigned long addr, byte *values, unsigned int nBytes)
{
byte ctrlByte;
byte rxStatus;
uint16_t nRead; //number of bytes to read
uint16_t nPage; //number of bytes remaining on current page, starting at addr
if (addr + nBytes > _totalCapacity) { //will this read take us past the top of the EEPROM?
return EEPROM_ADDR_ERR; //yes, tell the caller
}
while (nBytes > 0) {
byte ctrlByte;
byte rxStatus;
uint16_t nRead; //number of bytes to read
uint16_t nPage; //number of bytes remaining on current page, starting at addr
nPage = _pageSize - ( addr & (_pageSize - 1) );
nRead = nBytes < nPage ? nBytes : nPage;
nRead = BUFFER_LENGTH < nRead ? BUFFER_LENGTH : nRead;
+1 -1
View File
@@ -92,7 +92,7 @@ public:
* @param pageSize
* @param eepromAddr
*/
extEEPROM(eeprom_size_t deviceCapacity, byte nDevice, unsigned int pageSize,
extEEPROM(eeprom_size_t deviceCapacity, byte nDevice, unsigned int pageSize, //!< extEEPROM()
byte eepromAddr = 0x50);
byte begin(twiClockFreq_t twiFreq = twiClock100kHz); //!< begin()
byte write(unsigned long addr, byte *values, unsigned int nBytes); //!< write()
File diff suppressed because it is too large Load Diff
@@ -41,6 +41,7 @@
"use_only_additional_options": true
},
},
"rulers": [100],
},
// MySensors core library uses cppcheck to do static code analysis.
// The below settings enable cppcheck as a linter for automatic code analysis during development.