diff --git a/libraries/MySensors/core/MySigningAtsha204.cpp b/libraries/MySensors/core/MySigningAtsha204.cpp index a056770..eeff1f6 100644 --- a/libraries/MySensors/core/MySigningAtsha204.cpp +++ b/libraries/MySensors/core/MySigningAtsha204.cpp @@ -31,7 +31,6 @@ // Define MY_DEBUG_VERBOSE_SIGNING in your sketch to enable signing backend debugprints -ATSHA204Class atsha204(MY_SIGNING_ATSHA204_PIN); unsigned long _signing_timestamp; bool _signing_verification_ongoing = false; uint8_t _signing_current_nonce[NONCE_NUMIN_SIZE_PASSTHROUGH+SHA204_SERIAL_SZ+1]; @@ -87,6 +86,7 @@ static void DEBUG_SIGNING_PRINTBUF(const __FlashStringHelper* str, uint8_t* buf, #endif void signerAtsha204Init(void) { + atsha204_init(MY_SIGNING_ATSHA204_PIN); } bool signerAtsha204CheckTimer(void) { @@ -107,8 +107,8 @@ bool signerAtsha204GetNonce(MyMessage &msg) { // 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 - if (atsha204.sha204m_execute(SHA204_RANDOM, RANDOM_SEED_UPDATE, 0, 0, NULL, - RANDOM_COUNT, _singning_tx_buffer, RANDOM_RSP_SIZE, _singning_rx_buffer) != SHA204_SUCCESS) { + if (atsha204_execute(SHA204_RANDOM, RANDOM_SEED_UPDATE, 0, 0, NULL, + RANDOM_COUNT, _singning_tx_buffer, RANDOM_RSP_SIZE, _singning_rx_buffer) != SHA204_SUCCESS) { DEBUG_SIGNING_PRINTBUF(F("Failed to generate nonce"), NULL, 0); return false; } @@ -154,7 +154,7 @@ bool signerAtsha204SignMsg(MyMessage &msg) { // Salt the signature with the senders nodeId and the unique serial of the ATSHA device memcpy(_signing_current_nonce, &_singning_rx_buffer[SHA204_BUFFER_POS_DATA], 32); // We can reuse the nonce buffer now since it is no longer needed _signing_current_nonce[32] = msg.sender; - atsha204.getSerialNumber(&_signing_current_nonce[33]); + atsha204_getSerialNumber(&_signing_current_nonce[33]); (void)signerSha256(_signing_current_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); } @@ -233,26 +233,26 @@ static void signerCalculateSignature(MyMessage &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_current_nonce, 32); - (void)atsha204.sha204m_execute(SHA204_WRITE, SHA204_ZONE_DATA | SHA204_ZONE_COUNT_FLAG, 8 << 3, 32, _signing_temp_message, + (void)atsha204_execute(SHA204_WRITE, SHA204_ZONE_DATA | SHA204_ZONE_COUNT_FLAG, 8 << 3, 32, _signing_temp_message, WRITE_COUNT_LONG, _singning_tx_buffer, WRITE_RSP_SIZE, _singning_rx_buffer); // Program the nonce to use for the signature (has to be done just before GENDIG due to chip limitations) - (void)atsha204.sha204m_execute(SHA204_NONCE, NONCE_MODE_PASSTHROUGH, 0, NONCE_NUMIN_SIZE_PASSTHROUGH, _signing_current_nonce, + (void)atsha204_execute(SHA204_NONCE, NONCE_MODE_PASSTHROUGH, 0, NONCE_NUMIN_SIZE_PASSTHROUGH, _signing_current_nonce, NONCE_COUNT_LONG, _singning_tx_buffer, NONCE_RSP_SIZE_SHORT, _singning_rx_buffer); // Purge nonce when used memset(_signing_current_nonce, 0x00, NONCE_NUMIN_SIZE_PASSTHROUGH); // Generate digest of data and nonce - (void)atsha204.sha204m_execute(SHA204_GENDIG, GENDIG_ZONE_DATA, 8, 0, NULL, + (void)atsha204_execute(SHA204_GENDIG, GENDIG_ZONE_DATA, 8, 0, NULL, GENDIG_COUNT_DATA, _singning_tx_buffer, GENDIG_RSP_SIZE, _singning_rx_buffer); // Calculate HMAC of message+nonce digest and secret key - (void)atsha204.sha204m_execute(SHA204_HMAC, HMAC_MODE_SOURCE_FLAG_MATCH, 0, 0, NULL, + (void)atsha204_execute(SHA204_HMAC, HMAC_MODE_SOURCE_FLAG_MATCH, 0, 0, NULL, HMAC_COUNT, _singning_tx_buffer, HMAC_RSP_SIZE, _singning_rx_buffer); // Put device back to sleep - atsha204.sha204c_sleep(); + atsha204_sleep(); DEBUG_SIGNING_PRINTBUF(F("HMAC: "), &_singning_rx_buffer[SHA204_BUFFER_POS_DATA], 32); } @@ -261,7 +261,7 @@ static void signerCalculateSignature(MyMessage &msg) { // The pointer to the hash is returned, but the hash is also stored in _singning_rx_buffer[SHA204_BUFFER_POS_DATA]) static uint8_t* signerSha256(const uint8_t* data, size_t sz) { // Initiate SHA256 calculator - (void)atsha204.sha204m_execute(SHA204_SHA, SHA_INIT, 0, 0, NULL, + (void)atsha204_execute(SHA204_SHA, SHA_INIT, 0, 0, NULL, SHA_COUNT_SHORT, _singning_tx_buffer, SHA_RSP_SIZE_SHORT, _singning_rx_buffer); // Calculate a hash @@ -271,11 +271,11 @@ static uint8_t* signerSha256(const uint8_t* data, size_t sz) { // Write length data to the last bytes _signing_temp_message[SHA_MSG_SIZE-2] = (sz >> 5); _signing_temp_message[SHA_MSG_SIZE-1] = (sz << 3); - (void)atsha204.sha204m_execute(SHA204_SHA, SHA_CALC, 0, SHA_MSG_SIZE, _signing_temp_message, + (void)atsha204_execute(SHA204_SHA, SHA_CALC, 0, SHA_MSG_SIZE, _signing_temp_message, SHA_COUNT_LONG, _singning_tx_buffer, SHA_RSP_SIZE_LONG, _singning_rx_buffer); // Put device back to sleep - atsha204.sha204c_sleep(); + atsha204_sleep(); DEBUG_SIGNING_PRINTBUF(F("SHA256: "), &_singning_rx_buffer[SHA204_BUFFER_POS_DATA], 32); return &_singning_rx_buffer[SHA204_BUFFER_POS_DATA]; diff --git a/libraries/MySensors/drivers/ATSHA204/ATSHA204.cpp b/libraries/MySensors/drivers/ATSHA204/ATSHA204.cpp index 16b82c3..c1dc99a 100644 --- a/libraries/MySensors/drivers/ATSHA204/ATSHA204.cpp +++ b/libraries/MySensors/drivers/ATSHA204/ATSHA204.cpp @@ -1,58 +1,27 @@ #include "Arduino.h" #include "ATSHA204.h" -// atsha204Class Constructor -// Feed this function the Arduino-ized pin number you want to assign to the ATSHA204's SDA pin -// This will find the DDRX, PORTX, and PINX registrs it'll need to point to to control that pin -// As well as the bit value for each of those registers -ATSHA204Class::ATSHA204Class(uint8_t pin) -{ -#if defined(ARDUINO_ARCH_AVR) - device_pin = digitalPinToBitMask(pin); // Find the bit value of the pin - uint8_t port = digitalPinToPort(pin); // temoporarily used to get the next three registers - - // Point to data direction register port of pin - device_port_DDR = portModeRegister(port); - // Point to output register of pin - device_port_OUT = portOutputRegister(port); - // Point to input register of pin - device_port_IN = portInputRegister(port); -#else - device_pin = pin; +/* Local data and function prototypes */ + +static uint8_t device_pin; +#ifdef ARDUINO_ARCH_AVR +static volatile uint8_t *device_port_DDR, *device_port_OUT, *device_port_IN; #endif -} - -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) - { - 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); - - 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)]; - - 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); - response[8] = readResponse[SHA204_BUFFER_POS_DATA]; // Byte 8 of SN should always be 0xEE - } - } - - return; -} +static void sha204c_calculate_crc(uint8_t length, uint8_t *data, uint8_t *crc); +static uint8_t sha204c_check_crc(uint8_t *response); +static void swi_set_signal_pin(uint8_t is_high); +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_wakeup(uint8_t *response); +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); /* SWI bit bang functions */ -void ATSHA204Class::swi_set_signal_pin(uint8_t is_high) +static void swi_set_signal_pin(uint8_t is_high) { SHA204_SET_OUTPUT(); @@ -62,7 +31,7 @@ void ATSHA204Class::swi_set_signal_pin(uint8_t is_high) SHA204_POUT_LOW(); } -uint8_t ATSHA204Class::swi_send_bytes(uint8_t count, uint8_t *buffer) +static uint8_t swi_send_bytes(uint8_t count, uint8_t *buffer) { uint8_t i, bit_mask; @@ -106,12 +75,12 @@ uint8_t ATSHA204Class::swi_send_bytes(uint8_t count, uint8_t *buffer) return SWI_FUNCTION_RETCODE_SUCCESS; } -uint8_t ATSHA204Class::swi_send_byte(uint8_t value) +static uint8_t swi_send_byte(uint8_t value) { return swi_send_bytes(1, &value); } -uint8_t ATSHA204Class::swi_receive_bytes(uint8_t count, uint8_t *buffer) +static uint8_t swi_receive_bytes(uint8_t count, uint8_t *buffer) { uint8_t status = SWI_FUNCTION_RETCODE_SUCCESS; uint8_t i; @@ -217,12 +186,7 @@ uint8_t ATSHA204Class::swi_receive_bytes(uint8_t count, uint8_t *buffer) /* Physical functions */ -void ATSHA204Class::sha204c_sleep() -{ - swi_send_byte(SHA204_SWI_FLAG_SLEEP); -} - -uint8_t ATSHA204Class::sha204p_receive_response(uint8_t size, uint8_t *response) +static uint8_t sha204p_receive_response(uint8_t size, uint8_t *response) { uint8_t count_byte; uint8_t i; @@ -254,7 +218,7 @@ uint8_t ATSHA204Class::sha204p_receive_response(uint8_t size, uint8_t *response) /* Communication functions */ -uint8_t ATSHA204Class::sha204c_wakeup(uint8_t *response) +static uint8_t sha204c_wakeup(uint8_t *response) { swi_set_signal_pin(0); delayMicroseconds(10*SHA204_WAKEUP_PULSE_WIDTH); @@ -282,7 +246,7 @@ uint8_t ATSHA204Class::sha204c_wakeup(uint8_t *response) return ret_code; } -uint8_t ATSHA204Class::sha204c_resync(uint8_t size, uint8_t *response) +static uint8_t sha204c_resync(uint8_t size, uint8_t *response) { // Try to re-synchronize without sending a Wake token // (step 1 of the re-synchronization process). @@ -294,7 +258,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). - sha204c_sleep(); + atsha204_sleep(); ret_code = sha204c_wakeup(response); // Translate a return value of success into one @@ -303,7 +267,7 @@ uint8_t ATSHA204Class::sha204c_resync(uint8_t size, uint8_t *response) return (ret_code == SHA204_SUCCESS ? SHA204_RESYNC_WITH_WAKEUP : ret_code); } -uint8_t ATSHA204Class::sha204c_send_and_receive(uint8_t *tx_buffer, uint8_t rx_size, uint8_t *rx_buffer, uint8_t execution_delay, uint8_t execution_timeout) +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) { uint8_t ret_code = SHA204_FUNC_FAIL; uint8_t ret_code_resync; @@ -445,7 +409,8 @@ uint8_t ATSHA204Class::sha204c_send_and_receive(uint8_t *tx_buffer, uint8_t rx_s /* Marshaling functions */ -uint8_t ATSHA204Class::sha204m_read(uint8_t *tx_buffer, uint8_t *rx_buffer, uint8_t zone, uint16_t address) + +static uint8_t sha204m_read(uint8_t *tx_buffer, uint8_t *rx_buffer, uint8_t zone, uint16_t address) { uint8_t rx_size; @@ -462,7 +427,71 @@ uint8_t ATSHA204Class::sha204m_read(uint8_t *tx_buffer, uint8_t *rx_buffer, uint return sha204c_send_and_receive(&tx_buffer[0], rx_size, &rx_buffer[0], READ_DELAY, READ_EXEC_MAX - READ_DELAY); } -uint8_t ATSHA204Class::sha204m_execute(uint8_t op_code, uint8_t param1, uint16_t param2, +/* CRC Calculator and Checker */ + +static void sha204c_calculate_crc(uint8_t length, uint8_t *data, uint8_t *crc) +{ + uint8_t counter; + uint16_t crc_register = 0; + uint16_t polynom = 0x8005; + uint8_t shift_register; + uint8_t data_bit, crc_bit; + + for (counter = 0; counter < length; counter++) + { + for (shift_register = 0x01; shift_register > 0x00; shift_register <<= 1) + { + data_bit = (data[counter] & shift_register) ? 1 : 0; + crc_bit = crc_register >> 15; + + // Shift CRC to the left by 1. + crc_register <<= 1; + + if ((data_bit ^ crc_bit) != 0) + crc_register ^= polynom; + } + } + crc[0] = (uint8_t) (crc_register & 0x00FF); + crc[1] = (uint8_t) (crc_register >> 8); +} + +static uint8_t sha204c_check_crc(uint8_t *response) +{ + uint8_t crc[SHA204_CRC_SIZE]; + uint8_t count = response[SHA204_BUFFER_POS_COUNT]; + + count -= SHA204_CRC_SIZE; + sha204c_calculate_crc(count, response, crc); + + return (crc[0] == response[count] && crc[1] == response[count + 1]) + ? SHA204_SUCCESS : SHA204_BAD_CRC; +} + +/* Public functions */ + +void atsha204_init(uint8_t pin) +{ +#if defined(ARDUINO_ARCH_AVR) + device_pin = digitalPinToBitMask(pin); // Find the bit value of the pin + uint8_t port = digitalPinToPort(pin); // temoporarily used to get the next three registers + + // Point to data direction register port of pin + device_port_DDR = portModeRegister(port); + // Point to output register of pin + device_port_OUT = portOutputRegister(port); + // Point to input register of pin + device_port_IN = portInputRegister(port); +#else + device_pin = pin; +#endif +} + +void atsha204_sleep(void) +{ + swi_send_byte(SHA204_SWI_FLAG_SLEEP); +} + +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 poll_delay, poll_timeout, response_size; @@ -538,43 +567,30 @@ uint8_t ATSHA204Class::sha204m_execute(uint8_t op_code, uint8_t param1, uint16_t &rx_buffer[0], poll_delay, poll_timeout); } -/* CRC Calculator and Checker */ - -void ATSHA204Class::sha204c_calculate_crc(uint8_t length, uint8_t *data, uint8_t *crc) +void atsha204_getSerialNumber(uint8_t * response) { - uint8_t counter; - uint16_t crc_register = 0; - uint16_t polynom = 0x8005; - uint8_t shift_register; - uint8_t data_bit, crc_bit; + uint8_t readCommand[READ_COUNT]; + uint8_t readResponse[READ_4_RSP_SIZE]; - for (counter = 0; counter < length; counter++) + /* read from bytes 0->3 of config zone */ + uint8_t returnCode = sha204m_read(readCommand, readResponse, SHA204_ZONE_CONFIG, ADDRESS_SN03); + if (!returnCode) { - for (shift_register = 0x01; shift_register > 0x00; shift_register <<= 1) - { - data_bit = (data[counter] & shift_register) ? 1 : 0; - crc_bit = crc_register >> 15; + for (int i=0; i<4; i++) // store bytes 0-3 into respones array + response[i] = readResponse[SHA204_BUFFER_POS_DATA+i]; - // Shift CRC to the left by 1. - crc_register <<= 1; + /* read from bytes 8->11 of config zone */ + returnCode = sha204m_read(readCommand, readResponse, SHA204_ZONE_CONFIG, ADDRESS_SN47); - if ((data_bit ^ crc_bit) != 0) - crc_register ^= polynom; + 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)]; + + 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); + response[8] = readResponse[SHA204_BUFFER_POS_DATA]; // Byte 8 of SN should always be 0xEE } } - crc[0] = (uint8_t) (crc_register & 0x00FF); - crc[1] = (uint8_t) (crc_register >> 8); + + return; } - -uint8_t ATSHA204Class::sha204c_check_crc(uint8_t *response) -{ - uint8_t crc[SHA204_CRC_SIZE]; - uint8_t count = response[SHA204_BUFFER_POS_COUNT]; - - count -= SHA204_CRC_SIZE; - sha204c_calculate_crc(count, response, crc); - - return (crc[0] == response[count] && crc[1] == response[count + 1]) - ? SHA204_SUCCESS : SHA204_BAD_CRC; -} - diff --git a/libraries/MySensors/drivers/ATSHA204/ATSHA204.h b/libraries/MySensors/drivers/ATSHA204/ATSHA204.h index c7034bf..9221e64 100644 --- a/libraries/MySensors/drivers/ATSHA204/ATSHA204.h +++ b/libraries/MySensors/drivers/ATSHA204/ATSHA204.h @@ -239,31 +239,12 @@ #define SHA204_PIN_READ() (*device_port_IN & device_pin) #endif -class ATSHA204Class -{ -private: - uint8_t device_pin; - #ifdef ARDUINO_ARCH_AVR - volatile uint8_t *device_port_DDR, *device_port_OUT, *device_port_IN; - #endif - void sha204c_calculate_crc(uint8_t length, uint8_t *data, uint8_t *crc); - uint8_t sha204c_check_crc(uint8_t *response); - void swi_set_signal_pin(uint8_t is_high); - uint8_t swi_receive_bytes(uint8_t count, uint8_t *buffer); - uint8_t swi_send_bytes(uint8_t count, uint8_t *buffer); - 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 - 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); - void getSerialNumber(uint8_t *response); -}; +void atsha204_init(uint8_t pin); +void atsha204_sleep(void); +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); +void atsha204_getSerialNumber(uint8_t *response); #endif #endif