Skip to content

Commit 0a8aaed

Browse files
committed
Optimize ATSHA204 respose times
Instead of shutting down the ATSHA between calls in a session, it is put in idle state. This removes the need for a full resync when the device is woken and that saves time which translate to a quicker turnaround time for signed messages with HW signing. To save power, it is still shut down when not in use.
1 parent a5b0e8d commit 0a8aaed

File tree

3 files changed

+49
-37
lines changed

3 files changed

+49
-37
lines changed

libraries/MySensors/core/MySigningAtsha204.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ 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+
(void)atsha204_wakeup(_signing_temp_message);
110111
if (atsha204_execute(SHA204_RANDOM, RANDOM_SEED_UPDATE, 0, 0, NULL,
111112
RANDOM_COUNT, _singning_tx_buffer, RANDOM_RSP_SIZE, _singning_rx_buffer) != SHA204_SUCCESS) {
112113
DEBUG_SIGNING_PRINTBUF(F("Failed to generate nonce"), NULL, 0);
@@ -117,6 +118,8 @@ bool signerAtsha204GetNonce(MyMessage &msg) {
117118
}
118119
memcpy(_signing_current_nonce, signerSha256(_signing_current_nonce, 32), MAX_PAYLOAD);
119120

121+
atsha204_idle(); // We just idle the chip now since we expect to use it soon when the signed message arrives
122+
120123
// We set the part of the 32-byte nonce that does not fit into a message to 0xAA
121124
memset(&_signing_current_nonce[MAX_PAYLOAD], 0xAA, sizeof(_signing_current_nonce)-MAX_PAYLOAD);
122125

@@ -159,6 +162,9 @@ bool signerAtsha204SignMsg(MyMessage &msg) {
159162
DEBUG_SIGNING_PRINTBUF(F("Signature salted with serial"), NULL, 0);
160163
}
161164

165+
// Put device back to sleep
166+
atsha204_sleep();
167+
162168
// Overwrite the first byte in the signature with the signing identifier
163169
_singning_rx_buffer[SHA204_BUFFER_POS_DATA] = SIGNING_IDENTIFIER;
164170

@@ -204,10 +210,15 @@ bool signerAtsha204VerifyMsg(MyMessage &msg) {
204210
}
205211
if (j == NUM_OF(_signing_whitelist)) {
206212
DEBUG_SIGNING_PRINTBUF(F("Sender not found in whitelist, message rejected!"), NULL, 0);
213+
// Put device back to sleep
214+
atsha204_sleep();
207215
return false;
208216
}
209217
#endif
210218

219+
// Put device back to sleep
220+
atsha204_sleep();
221+
211222
// Overwrite the first byte in the signature with the signing identifier
212223
_singning_rx_buffer[SHA204_BUFFER_POS_DATA] = SIGNING_IDENTIFIER;
213224

@@ -227,6 +238,7 @@ bool signerAtsha204VerifyMsg(MyMessage &msg) {
227238

228239
// Helper to calculate signature of msg (returned in _singning_rx_buffer[SHA204_BUFFER_POS_DATA])
229240
static void signerCalculateSignature(MyMessage &msg) {
241+
(void)atsha204_wakeup(_signing_temp_message);
230242
memset(_signing_temp_message, 0, 32);
231243
memcpy(_signing_temp_message, (uint8_t*)&msg.data[1-HEADER_SIZE], MAX_MESSAGE_LENGTH-1-(MAX_PAYLOAD-mGetLength(msg)));
232244

@@ -251,9 +263,6 @@ static void signerCalculateSignature(MyMessage &msg) {
251263
(void)atsha204_execute(SHA204_HMAC, HMAC_MODE_SOURCE_FLAG_MATCH, 0, 0, NULL,
252264
HMAC_COUNT, _singning_tx_buffer, HMAC_RSP_SIZE, _singning_rx_buffer);
253265

254-
// Put device back to sleep
255-
atsha204_sleep();
256-
257266
DEBUG_SIGNING_PRINTBUF(F("HMAC: "), &_singning_rx_buffer[SHA204_BUFFER_POS_DATA], 32);
258267
}
259268

@@ -274,9 +283,6 @@ static uint8_t* signerSha256(const uint8_t* data, size_t sz) {
274283
(void)atsha204_execute(SHA204_SHA, SHA_CALC, 0, SHA_MSG_SIZE, _signing_temp_message,
275284
SHA_COUNT_LONG, _singning_tx_buffer, SHA_RSP_SIZE_LONG, _singning_rx_buffer);
276285

277-
// Put device back to sleep
278-
atsha204_sleep();
279-
280286
DEBUG_SIGNING_PRINTBUF(F("SHA256: "), &_singning_rx_buffer[SHA204_BUFFER_POS_DATA], 32);
281287
return &_singning_rx_buffer[SHA204_BUFFER_POS_DATA];
282288
}

libraries/MySensors/drivers/ATSHA204/ATSHA204.cpp

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ static uint8_t swi_send_bytes(uint8_t count, uint8_t *buffer);
1515
static uint8_t swi_send_byte(uint8_t value);
1616
static uint8_t sha204p_receive_response(uint8_t size, uint8_t *response);
1717
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);
1918
static uint8_t sha204c_resync(uint8_t size, uint8_t *response);
2019
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);
2120

@@ -218,34 +217,6 @@ static uint8_t sha204p_receive_response(uint8_t size, uint8_t *response)
218217

219218
/* Communication functions */
220219

221-
static uint8_t sha204c_wakeup(uint8_t *response)
222-
{
223-
swi_set_signal_pin(0);
224-
delayMicroseconds(10*SHA204_WAKEUP_PULSE_WIDTH);
225-
swi_set_signal_pin(1);
226-
delay(SHA204_WAKEUP_DELAY);
227-
228-
uint8_t ret_code = sha204p_receive_response(SHA204_RSP_SIZE_MIN, response);
229-
if (ret_code != SHA204_SUCCESS)
230-
return ret_code;
231-
232-
// Verify status response.
233-
if (response[SHA204_BUFFER_POS_COUNT] != SHA204_RSP_SIZE_MIN)
234-
ret_code = SHA204_INVALID_SIZE;
235-
else if (response[SHA204_BUFFER_POS_STATUS] != SHA204_STATUS_BYTE_WAKEUP)
236-
ret_code = SHA204_COMM_FAIL;
237-
else
238-
{
239-
if ((response[SHA204_RSP_SIZE_MIN - SHA204_CRC_SIZE] != 0x33)
240-
|| (response[SHA204_RSP_SIZE_MIN + 1 - SHA204_CRC_SIZE] != 0x43))
241-
ret_code = SHA204_BAD_CRC;
242-
}
243-
if (ret_code != SHA204_SUCCESS)
244-
delay(SHA204_COMMAND_EXEC_MAX);
245-
246-
return ret_code;
247-
}
248-
249220
static uint8_t sha204c_resync(uint8_t size, uint8_t *response)
250221
{
251222
// Try to re-synchronize without sending a Wake token
@@ -259,7 +230,7 @@ static uint8_t sha204c_resync(uint8_t size, uint8_t *response)
259230
// to receive a response (steps 2 and 3 of the
260231
// re-synchronization process).
261232
atsha204_sleep();
262-
ret_code = sha204c_wakeup(response);
233+
ret_code = atsha204_wakeup(response);
263234

264235
// Translate a return value of success into one
265236
// that indicates that the device had to be woken up
@@ -486,11 +457,44 @@ void atsha204_init(uint8_t pin)
486457
#endif
487458
}
488459

460+
void atsha204_idle(void)
461+
{
462+
swi_send_byte(SHA204_SWI_FLAG_IDLE);
463+
}
464+
489465
void atsha204_sleep(void)
490466
{
491467
swi_send_byte(SHA204_SWI_FLAG_SLEEP);
492468
}
493469

470+
uint8_t atsha204_wakeup(uint8_t *response)
471+
{
472+
swi_set_signal_pin(0);
473+
delayMicroseconds(10*SHA204_WAKEUP_PULSE_WIDTH);
474+
swi_set_signal_pin(1);
475+
delay(SHA204_WAKEUP_DELAY);
476+
477+
uint8_t ret_code = sha204p_receive_response(SHA204_RSP_SIZE_MIN, response);
478+
if (ret_code != SHA204_SUCCESS)
479+
return ret_code;
480+
481+
// Verify status response.
482+
if (response[SHA204_BUFFER_POS_COUNT] != SHA204_RSP_SIZE_MIN)
483+
ret_code = SHA204_INVALID_SIZE;
484+
else if (response[SHA204_BUFFER_POS_STATUS] != SHA204_STATUS_BYTE_WAKEUP)
485+
ret_code = SHA204_COMM_FAIL;
486+
else
487+
{
488+
if ((response[SHA204_RSP_SIZE_MIN - SHA204_CRC_SIZE] != 0x33)
489+
|| (response[SHA204_RSP_SIZE_MIN + 1 - SHA204_CRC_SIZE] != 0x43))
490+
ret_code = SHA204_BAD_CRC;
491+
}
492+
if (ret_code != SHA204_SUCCESS)
493+
delay(SHA204_COMMAND_EXEC_MAX);
494+
495+
return ret_code;
496+
}
497+
494498
uint8_t atsha204_execute(uint8_t op_code, uint8_t param1, uint16_t param2,
495499
uint8_t datalen1, uint8_t *data1, uint8_t tx_size, uint8_t *tx_buffer, uint8_t rx_size, uint8_t *rx_buffer)
496500
{

libraries/MySensors/drivers/ATSHA204/ATSHA204.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#if !DOXYGEN
44
#include "Arduino.h"
55

6-
/* This is a scaled down variant of the ATSHA204 library, tweaked to meet the specific needs of MySensors. */
6+
/* This is a scaled down variant of the ATSHA204 library, tweaked to meet the specific needs of the MySensors library. */
77

88
/* Library return codes */
99
#define SHA204_SUCCESS ((uint8_t) 0x00) //!< Function succeeded.
@@ -240,7 +240,9 @@
240240
#endif
241241

242242
void atsha204_init(uint8_t pin);
243+
void atsha204_idle(void);
243244
void atsha204_sleep(void);
245+
uint8_t atsha204_wakeup(uint8_t *response);
244246
uint8_t atsha204_execute(uint8_t op_code, uint8_t param1, uint16_t param2,
245247
uint8_t datalen1, uint8_t *data1, uint8_t tx_size,
246248
uint8_t *tx_buffer, uint8_t rx_size, uint8_t *rx_buffer);

0 commit comments

Comments
 (0)