Skip to content

Commit a5b0e8d

Browse files
committed
Purge ATSHA204 backend of c++ code
There is no need for class structures in the atsha low-end driver. Removing this saves both code space and compilation time.
1 parent b01282c commit a5b0e8d

File tree

3 files changed

+128
-131
lines changed

3 files changed

+128
-131
lines changed

libraries/MySensors/core/MySigningAtsha204.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131

3232
// Define MY_DEBUG_VERBOSE_SIGNING in your sketch to enable signing backend debugprints
3333

34-
ATSHA204Class atsha204(MY_SIGNING_ATSHA204_PIN);
3534
unsigned long _signing_timestamp;
3635
bool _signing_verification_ongoing = false;
3736
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,
8786
#endif
8887

8988
void signerAtsha204Init(void) {
89+
atsha204_init(MY_SIGNING_ATSHA204_PIN);
9090
}
9191

9292
bool signerAtsha204CheckTimer(void) {
@@ -107,8 +107,8 @@ bool signerAtsha204GetNonce(MyMessage &msg) {
107107
// Generate random number for use as nonce
108108
// We used a basic whitening technique that XORs each byte in a 32byte random value with current hwMillis() counter
109109
// This 32-byte random value is then hashed (SHA256) to produce the resulting nonce
110-
if (atsha204.sha204m_execute(SHA204_RANDOM, RANDOM_SEED_UPDATE, 0, 0, NULL,
111-
RANDOM_COUNT, _singning_tx_buffer, RANDOM_RSP_SIZE, _singning_rx_buffer) != SHA204_SUCCESS) {
110+
if (atsha204_execute(SHA204_RANDOM, RANDOM_SEED_UPDATE, 0, 0, NULL,
111+
RANDOM_COUNT, _singning_tx_buffer, RANDOM_RSP_SIZE, _singning_rx_buffer) != SHA204_SUCCESS) {
112112
DEBUG_SIGNING_PRINTBUF(F("Failed to generate nonce"), NULL, 0);
113113
return false;
114114
}
@@ -154,7 +154,7 @@ bool signerAtsha204SignMsg(MyMessage &msg) {
154154
// Salt the signature with the senders nodeId and the unique serial of the ATSHA device
155155
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
156156
_signing_current_nonce[32] = msg.sender;
157-
atsha204.getSerialNumber(&_signing_current_nonce[33]);
157+
atsha204_getSerialNumber(&_signing_current_nonce[33]);
158158
(void)signerSha256(_signing_current_nonce, 32+1+SHA204_SERIAL_SZ); // we can 'void' sha256 because the hash is already put in the correct place
159159
DEBUG_SIGNING_PRINTBUF(F("Signature salted with serial"), NULL, 0);
160160
}
@@ -233,26 +233,26 @@ static void signerCalculateSignature(MyMessage &msg) {
233233
// Program the data to sign into the ATSHA204
234234
DEBUG_SIGNING_PRINTBUF(F("Message to process: "), (uint8_t*)&msg.data[1-HEADER_SIZE], MAX_MESSAGE_LENGTH-1-(MAX_PAYLOAD-mGetLength(msg)));
235235
DEBUG_SIGNING_PRINTBUF(F("Current nonce: "), _signing_current_nonce, 32);
236-
(void)atsha204.sha204m_execute(SHA204_WRITE, SHA204_ZONE_DATA | SHA204_ZONE_COUNT_FLAG, 8 << 3, 32, _signing_temp_message,
236+
(void)atsha204_execute(SHA204_WRITE, SHA204_ZONE_DATA | SHA204_ZONE_COUNT_FLAG, 8 << 3, 32, _signing_temp_message,
237237
WRITE_COUNT_LONG, _singning_tx_buffer, WRITE_RSP_SIZE, _singning_rx_buffer);
238238

239239
// Program the nonce to use for the signature (has to be done just before GENDIG due to chip limitations)
240-
(void)atsha204.sha204m_execute(SHA204_NONCE, NONCE_MODE_PASSTHROUGH, 0, NONCE_NUMIN_SIZE_PASSTHROUGH, _signing_current_nonce,
240+
(void)atsha204_execute(SHA204_NONCE, NONCE_MODE_PASSTHROUGH, 0, NONCE_NUMIN_SIZE_PASSTHROUGH, _signing_current_nonce,
241241
NONCE_COUNT_LONG, _singning_tx_buffer, NONCE_RSP_SIZE_SHORT, _singning_rx_buffer);
242242

243243
// Purge nonce when used
244244
memset(_signing_current_nonce, 0x00, NONCE_NUMIN_SIZE_PASSTHROUGH);
245245

246246
// Generate digest of data and nonce
247-
(void)atsha204.sha204m_execute(SHA204_GENDIG, GENDIG_ZONE_DATA, 8, 0, NULL,
247+
(void)atsha204_execute(SHA204_GENDIG, GENDIG_ZONE_DATA, 8, 0, NULL,
248248
GENDIG_COUNT_DATA, _singning_tx_buffer, GENDIG_RSP_SIZE, _singning_rx_buffer);
249249

250250
// Calculate HMAC of message+nonce digest and secret key
251-
(void)atsha204.sha204m_execute(SHA204_HMAC, HMAC_MODE_SOURCE_FLAG_MATCH, 0, 0, NULL,
251+
(void)atsha204_execute(SHA204_HMAC, HMAC_MODE_SOURCE_FLAG_MATCH, 0, 0, NULL,
252252
HMAC_COUNT, _singning_tx_buffer, HMAC_RSP_SIZE, _singning_rx_buffer);
253253

254254
// Put device back to sleep
255-
atsha204.sha204c_sleep();
255+
atsha204_sleep();
256256

257257
DEBUG_SIGNING_PRINTBUF(F("HMAC: "), &_singning_rx_buffer[SHA204_BUFFER_POS_DATA], 32);
258258
}
@@ -261,7 +261,7 @@ static void signerCalculateSignature(MyMessage &msg) {
261261
// The pointer to the hash is returned, but the hash is also stored in _singning_rx_buffer[SHA204_BUFFER_POS_DATA])
262262
static uint8_t* signerSha256(const uint8_t* data, size_t sz) {
263263
// Initiate SHA256 calculator
264-
(void)atsha204.sha204m_execute(SHA204_SHA, SHA_INIT, 0, 0, NULL,
264+
(void)atsha204_execute(SHA204_SHA, SHA_INIT, 0, 0, NULL,
265265
SHA_COUNT_SHORT, _singning_tx_buffer, SHA_RSP_SIZE_SHORT, _singning_rx_buffer);
266266

267267
// Calculate a hash
@@ -271,11 +271,11 @@ static uint8_t* signerSha256(const uint8_t* data, size_t sz) {
271271
// Write length data to the last bytes
272272
_signing_temp_message[SHA_MSG_SIZE-2] = (sz >> 5);
273273
_signing_temp_message[SHA_MSG_SIZE-1] = (sz << 3);
274-
(void)atsha204.sha204m_execute(SHA204_SHA, SHA_CALC, 0, SHA_MSG_SIZE, _signing_temp_message,
274+
(void)atsha204_execute(SHA204_SHA, SHA_CALC, 0, SHA_MSG_SIZE, _signing_temp_message,
275275
SHA_COUNT_LONG, _singning_tx_buffer, SHA_RSP_SIZE_LONG, _singning_rx_buffer);
276276

277277
// Put device back to sleep
278-
atsha204.sha204c_sleep();
278+
atsha204_sleep();
279279

280280
DEBUG_SIGNING_PRINTBUF(F("SHA256: "), &_singning_rx_buffer[SHA204_BUFFER_POS_DATA], 32);
281281
return &_singning_rx_buffer[SHA204_BUFFER_POS_DATA];

libraries/MySensors/drivers/ATSHA204/ATSHA204.cpp

Lines changed: 110 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,27 @@
11
#include "Arduino.h"
22
#include "ATSHA204.h"
33

4-
// atsha204Class Constructor
5-
// Feed this function the Arduino-ized pin number you want to assign to the ATSHA204's SDA pin
6-
// This will find the DDRX, PORTX, and PINX registrs it'll need to point to to control that pin
7-
// As well as the bit value for each of those registers
8-
ATSHA204Class::ATSHA204Class(uint8_t pin)
9-
{
10-
#if defined(ARDUINO_ARCH_AVR)
11-
device_pin = digitalPinToBitMask(pin); // Find the bit value of the pin
12-
uint8_t port = digitalPinToPort(pin); // temoporarily used to get the next three registers
13-
14-
// Point to data direction register port of pin
15-
device_port_DDR = portModeRegister(port);
16-
// Point to output register of pin
17-
device_port_OUT = portOutputRegister(port);
18-
// Point to input register of pin
19-
device_port_IN = portInputRegister(port);
20-
#else
21-
device_pin = pin;
22-
#endif
23-
}
24-
25-
void ATSHA204Class::getSerialNumber(uint8_t * response)
26-
{
27-
uint8_t readCommand[READ_COUNT];
28-
uint8_t readResponse[READ_4_RSP_SIZE];
4+
/* Local data and function prototypes */
295

30-
/* read from bytes 0->3 of config zone */
31-
uint8_t returnCode = sha204m_read(readCommand, readResponse, SHA204_ZONE_CONFIG, ADDRESS_SN03);
32-
if (!returnCode)
33-
{
34-
for (int i=0; i<4; i++) // store bytes 0-3 into respones array
35-
response[i] = readResponse[SHA204_BUFFER_POS_DATA+i];
36-
37-
/* read from bytes 8->11 of config zone */
38-
returnCode = sha204m_read(readCommand, readResponse, SHA204_ZONE_CONFIG, ADDRESS_SN47);
39-
40-
for (int i=4; i<8; i++) // store bytes 4-7 of SN into response array
41-
response[i] = readResponse[SHA204_BUFFER_POS_DATA+(i-4)];
42-
43-
if (!returnCode)
44-
{ /* Finally if last two reads were successful, read byte 8 of the SN */
45-
returnCode = sha204m_read(readCommand, readResponse, SHA204_ZONE_CONFIG, ADDRESS_SN8);
46-
response[8] = readResponse[SHA204_BUFFER_POS_DATA]; // Byte 8 of SN should always be 0xEE
47-
}
48-
}
49-
50-
return;
51-
}
6+
static uint8_t device_pin;
7+
#ifdef ARDUINO_ARCH_AVR
8+
static volatile uint8_t *device_port_DDR, *device_port_OUT, *device_port_IN;
9+
#endif
10+
static void sha204c_calculate_crc(uint8_t length, uint8_t *data, uint8_t *crc);
11+
static uint8_t sha204c_check_crc(uint8_t *response);
12+
static void swi_set_signal_pin(uint8_t is_high);
13+
static uint8_t swi_receive_bytes(uint8_t count, uint8_t *buffer);
14+
static uint8_t swi_send_bytes(uint8_t count, uint8_t *buffer);
15+
static uint8_t swi_send_byte(uint8_t value);
16+
static uint8_t sha204p_receive_response(uint8_t size, uint8_t *response);
17+
static uint8_t sha204m_read(uint8_t *tx_buffer, uint8_t *rx_buffer, uint8_t zone, uint16_t address);
18+
static uint8_t sha204c_wakeup(uint8_t *response);
19+
static uint8_t sha204c_resync(uint8_t size, uint8_t *response);
20+
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);
5221

5322
/* SWI bit bang functions */
5423

55-
void ATSHA204Class::swi_set_signal_pin(uint8_t is_high)
24+
static void swi_set_signal_pin(uint8_t is_high)
5625
{
5726
SHA204_SET_OUTPUT();
5827

@@ -62,7 +31,7 @@ void ATSHA204Class::swi_set_signal_pin(uint8_t is_high)
6231
SHA204_POUT_LOW();
6332
}
6433

65-
uint8_t ATSHA204Class::swi_send_bytes(uint8_t count, uint8_t *buffer)
34+
static uint8_t swi_send_bytes(uint8_t count, uint8_t *buffer)
6635
{
6736
uint8_t i, bit_mask;
6837

@@ -106,12 +75,12 @@ uint8_t ATSHA204Class::swi_send_bytes(uint8_t count, uint8_t *buffer)
10675
return SWI_FUNCTION_RETCODE_SUCCESS;
10776
}
10877

109-
uint8_t ATSHA204Class::swi_send_byte(uint8_t value)
78+
static uint8_t swi_send_byte(uint8_t value)
11079
{
11180
return swi_send_bytes(1, &value);
11281
}
11382

114-
uint8_t ATSHA204Class::swi_receive_bytes(uint8_t count, uint8_t *buffer)
83+
static uint8_t swi_receive_bytes(uint8_t count, uint8_t *buffer)
11584
{
11685
uint8_t status = SWI_FUNCTION_RETCODE_SUCCESS;
11786
uint8_t i;
@@ -217,12 +186,7 @@ uint8_t ATSHA204Class::swi_receive_bytes(uint8_t count, uint8_t *buffer)
217186

218187
/* Physical functions */
219188

220-
void ATSHA204Class::sha204c_sleep()
221-
{
222-
swi_send_byte(SHA204_SWI_FLAG_SLEEP);
223-
}
224-
225-
uint8_t ATSHA204Class::sha204p_receive_response(uint8_t size, uint8_t *response)
189+
static uint8_t sha204p_receive_response(uint8_t size, uint8_t *response)
226190
{
227191
uint8_t count_byte;
228192
uint8_t i;
@@ -254,7 +218,7 @@ uint8_t ATSHA204Class::sha204p_receive_response(uint8_t size, uint8_t *response)
254218

255219
/* Communication functions */
256220

257-
uint8_t ATSHA204Class::sha204c_wakeup(uint8_t *response)
221+
static uint8_t sha204c_wakeup(uint8_t *response)
258222
{
259223
swi_set_signal_pin(0);
260224
delayMicroseconds(10*SHA204_WAKEUP_PULSE_WIDTH);
@@ -282,7 +246,7 @@ uint8_t ATSHA204Class::sha204c_wakeup(uint8_t *response)
282246
return ret_code;
283247
}
284248

285-
uint8_t ATSHA204Class::sha204c_resync(uint8_t size, uint8_t *response)
249+
static uint8_t sha204c_resync(uint8_t size, uint8_t *response)
286250
{
287251
// Try to re-synchronize without sending a Wake token
288252
// (step 1 of the re-synchronization process).
@@ -294,7 +258,7 @@ uint8_t ATSHA204Class::sha204c_resync(uint8_t size, uint8_t *response)
294258
// We lost communication. Send a Wake pulse and try
295259
// to receive a response (steps 2 and 3 of the
296260
// re-synchronization process).
297-
sha204c_sleep();
261+
atsha204_sleep();
298262
ret_code = sha204c_wakeup(response);
299263

300264
// Translate a return value of success into one
@@ -303,7 +267,7 @@ uint8_t ATSHA204Class::sha204c_resync(uint8_t size, uint8_t *response)
303267
return (ret_code == SHA204_SUCCESS ? SHA204_RESYNC_WITH_WAKEUP : ret_code);
304268
}
305269

306-
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)
270+
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)
307271
{
308272
uint8_t ret_code = SHA204_FUNC_FAIL;
309273
uint8_t ret_code_resync;
@@ -445,7 +409,8 @@ uint8_t ATSHA204Class::sha204c_send_and_receive(uint8_t *tx_buffer, uint8_t rx_s
445409

446410

447411
/* Marshaling functions */
448-
uint8_t ATSHA204Class::sha204m_read(uint8_t *tx_buffer, uint8_t *rx_buffer, uint8_t zone, uint16_t address)
412+
413+
static uint8_t sha204m_read(uint8_t *tx_buffer, uint8_t *rx_buffer, uint8_t zone, uint16_t address)
449414
{
450415
uint8_t rx_size;
451416

@@ -462,7 +427,71 @@ uint8_t ATSHA204Class::sha204m_read(uint8_t *tx_buffer, uint8_t *rx_buffer, uint
462427
return sha204c_send_and_receive(&tx_buffer[0], rx_size, &rx_buffer[0], READ_DELAY, READ_EXEC_MAX - READ_DELAY);
463428
}
464429

465-
uint8_t ATSHA204Class::sha204m_execute(uint8_t op_code, uint8_t param1, uint16_t param2,
430+
/* CRC Calculator and Checker */
431+
432+
static void sha204c_calculate_crc(uint8_t length, uint8_t *data, uint8_t *crc)
433+
{
434+
uint8_t counter;
435+
uint16_t crc_register = 0;
436+
uint16_t polynom = 0x8005;
437+
uint8_t shift_register;
438+
uint8_t data_bit, crc_bit;
439+
440+
for (counter = 0; counter < length; counter++)
441+
{
442+
for (shift_register = 0x01; shift_register > 0x00; shift_register <<= 1)
443+
{
444+
data_bit = (data[counter] & shift_register) ? 1 : 0;
445+
crc_bit = crc_register >> 15;
446+
447+
// Shift CRC to the left by 1.
448+
crc_register <<= 1;
449+
450+
if ((data_bit ^ crc_bit) != 0)
451+
crc_register ^= polynom;
452+
}
453+
}
454+
crc[0] = (uint8_t) (crc_register & 0x00FF);
455+
crc[1] = (uint8_t) (crc_register >> 8);
456+
}
457+
458+
static uint8_t sha204c_check_crc(uint8_t *response)
459+
{
460+
uint8_t crc[SHA204_CRC_SIZE];
461+
uint8_t count = response[SHA204_BUFFER_POS_COUNT];
462+
463+
count -= SHA204_CRC_SIZE;
464+
sha204c_calculate_crc(count, response, crc);
465+
466+
return (crc[0] == response[count] && crc[1] == response[count + 1])
467+
? SHA204_SUCCESS : SHA204_BAD_CRC;
468+
}
469+
470+
/* Public functions */
471+
472+
void atsha204_init(uint8_t pin)
473+
{
474+
#if defined(ARDUINO_ARCH_AVR)
475+
device_pin = digitalPinToBitMask(pin); // Find the bit value of the pin
476+
uint8_t port = digitalPinToPort(pin); // temoporarily used to get the next three registers
477+
478+
// Point to data direction register port of pin
479+
device_port_DDR = portModeRegister(port);
480+
// Point to output register of pin
481+
device_port_OUT = portOutputRegister(port);
482+
// Point to input register of pin
483+
device_port_IN = portInputRegister(port);
484+
#else
485+
device_pin = pin;
486+
#endif
487+
}
488+
489+
void atsha204_sleep(void)
490+
{
491+
swi_send_byte(SHA204_SWI_FLAG_SLEEP);
492+
}
493+
494+
uint8_t atsha204_execute(uint8_t op_code, uint8_t param1, uint16_t param2,
466495
uint8_t datalen1, uint8_t *data1, uint8_t tx_size, uint8_t *tx_buffer, uint8_t rx_size, uint8_t *rx_buffer)
467496
{
468497
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
538567
&rx_buffer[0], poll_delay, poll_timeout);
539568
}
540569

541-
/* CRC Calculator and Checker */
542-
543-
void ATSHA204Class::sha204c_calculate_crc(uint8_t length, uint8_t *data, uint8_t *crc)
570+
void atsha204_getSerialNumber(uint8_t * response)
544571
{
545-
uint8_t counter;
546-
uint16_t crc_register = 0;
547-
uint16_t polynom = 0x8005;
548-
uint8_t shift_register;
549-
uint8_t data_bit, crc_bit;
572+
uint8_t readCommand[READ_COUNT];
573+
uint8_t readResponse[READ_4_RSP_SIZE];
550574

551-
for (counter = 0; counter < length; counter++)
575+
/* read from bytes 0->3 of config zone */
576+
uint8_t returnCode = sha204m_read(readCommand, readResponse, SHA204_ZONE_CONFIG, ADDRESS_SN03);
577+
if (!returnCode)
552578
{
553-
for (shift_register = 0x01; shift_register > 0x00; shift_register <<= 1)
554-
{
555-
data_bit = (data[counter] & shift_register) ? 1 : 0;
556-
crc_bit = crc_register >> 15;
579+
for (int i=0; i<4; i++) // store bytes 0-3 into respones array
580+
response[i] = readResponse[SHA204_BUFFER_POS_DATA+i];
557581

558-
// Shift CRC to the left by 1.
559-
crc_register <<= 1;
582+
/* read from bytes 8->11 of config zone */
583+
returnCode = sha204m_read(readCommand, readResponse, SHA204_ZONE_CONFIG, ADDRESS_SN47);
560584

561-
if ((data_bit ^ crc_bit) != 0)
562-
crc_register ^= polynom;
585+
for (int i=4; i<8; i++) // store bytes 4-7 of SN into response array
586+
response[i] = readResponse[SHA204_BUFFER_POS_DATA+(i-4)];
587+
588+
if (!returnCode)
589+
{ /* Finally if last two reads were successful, read byte 8 of the SN */
590+
returnCode = sha204m_read(readCommand, readResponse, SHA204_ZONE_CONFIG, ADDRESS_SN8);
591+
response[8] = readResponse[SHA204_BUFFER_POS_DATA]; // Byte 8 of SN should always be 0xEE
563592
}
564593
}
565-
crc[0] = (uint8_t) (crc_register & 0x00FF);
566-
crc[1] = (uint8_t) (crc_register >> 8);
567-
}
568-
569-
uint8_t ATSHA204Class::sha204c_check_crc(uint8_t *response)
570-
{
571-
uint8_t crc[SHA204_CRC_SIZE];
572-
uint8_t count = response[SHA204_BUFFER_POS_COUNT];
573-
574-
count -= SHA204_CRC_SIZE;
575-
sha204c_calculate_crc(count, response, crc);
576594

577-
return (crc[0] == response[count] && crc[1] == response[count + 1])
578-
? SHA204_SUCCESS : SHA204_BAD_CRC;
595+
return;
579596
}
580-

0 commit comments

Comments
 (0)