@@ -107,6 +107,7 @@ bool signerAtsha204GetNonce(MyMessage &msg) {
107
107
// Generate random number for use as nonce
108
108
// We used a basic whitening technique that XORs each byte in a 32byte random value with current hwMillis() counter
109
109
// This 32-byte random value is then hashed (SHA256) to produce the resulting nonce
110
+ (void )atsha204_wakeup (_signing_temp_message);
110
111
if (atsha204_execute (SHA204_RANDOM, RANDOM_SEED_UPDATE, 0 , 0 , NULL ,
111
112
RANDOM_COUNT, _singning_tx_buffer, RANDOM_RSP_SIZE, _singning_rx_buffer) != SHA204_SUCCESS) {
112
113
DEBUG_SIGNING_PRINTBUF (F (" Failed to generate nonce" ), NULL , 0 );
@@ -117,6 +118,8 @@ bool signerAtsha204GetNonce(MyMessage &msg) {
117
118
}
118
119
memcpy (_signing_current_nonce, signerSha256 (_signing_current_nonce, 32 ), MAX_PAYLOAD);
119
120
121
+ atsha204_idle (); // We just idle the chip now since we expect to use it soon when the signed message arrives
122
+
120
123
// We set the part of the 32-byte nonce that does not fit into a message to 0xAA
121
124
memset (&_signing_current_nonce[MAX_PAYLOAD], 0xAA , sizeof (_signing_current_nonce)-MAX_PAYLOAD);
122
125
@@ -159,6 +162,9 @@ bool signerAtsha204SignMsg(MyMessage &msg) {
159
162
DEBUG_SIGNING_PRINTBUF (F (" Signature salted with serial" ), NULL , 0 );
160
163
}
161
164
165
+ // Put device back to sleep
166
+ atsha204_sleep ();
167
+
162
168
// Overwrite the first byte in the signature with the signing identifier
163
169
_singning_rx_buffer[SHA204_BUFFER_POS_DATA] = SIGNING_IDENTIFIER;
164
170
@@ -204,10 +210,15 @@ bool signerAtsha204VerifyMsg(MyMessage &msg) {
204
210
}
205
211
if (j == NUM_OF (_signing_whitelist)) {
206
212
DEBUG_SIGNING_PRINTBUF (F (" Sender not found in whitelist, message rejected!" ), NULL , 0 );
213
+ // Put device back to sleep
214
+ atsha204_sleep ();
207
215
return false ;
208
216
}
209
217
#endif
210
218
219
+ // Put device back to sleep
220
+ atsha204_sleep ();
221
+
211
222
// Overwrite the first byte in the signature with the signing identifier
212
223
_singning_rx_buffer[SHA204_BUFFER_POS_DATA] = SIGNING_IDENTIFIER;
213
224
@@ -227,6 +238,7 @@ bool signerAtsha204VerifyMsg(MyMessage &msg) {
227
238
228
239
// Helper to calculate signature of msg (returned in _singning_rx_buffer[SHA204_BUFFER_POS_DATA])
229
240
static void signerCalculateSignature (MyMessage &msg) {
241
+ (void )atsha204_wakeup (_signing_temp_message);
230
242
memset (_signing_temp_message, 0 , 32 );
231
243
memcpy (_signing_temp_message, (uint8_t *)&msg.data [1 -HEADER_SIZE], MAX_MESSAGE_LENGTH-1 -(MAX_PAYLOAD-mGetLength (msg)));
232
244
@@ -251,9 +263,6 @@ static void signerCalculateSignature(MyMessage &msg) {
251
263
(void )atsha204_execute (SHA204_HMAC, HMAC_MODE_SOURCE_FLAG_MATCH, 0 , 0 , NULL ,
252
264
HMAC_COUNT, _singning_tx_buffer, HMAC_RSP_SIZE, _singning_rx_buffer);
253
265
254
- // Put device back to sleep
255
- atsha204_sleep ();
256
-
257
266
DEBUG_SIGNING_PRINTBUF (F (" HMAC: " ), &_singning_rx_buffer[SHA204_BUFFER_POS_DATA], 32 );
258
267
}
259
268
@@ -274,9 +283,6 @@ static uint8_t* signerSha256(const uint8_t* data, size_t sz) {
274
283
(void )atsha204_execute (SHA204_SHA, SHA_CALC, 0 , SHA_MSG_SIZE, _signing_temp_message,
275
284
SHA_COUNT_LONG, _singning_tx_buffer, SHA_RSP_SIZE_LONG, _singning_rx_buffer);
276
285
277
- // Put device back to sleep
278
- atsha204_sleep ();
279
-
280
286
DEBUG_SIGNING_PRINTBUF (F (" SHA256: " ), &_singning_rx_buffer[SHA204_BUFFER_POS_DATA], 32 );
281
287
return &_singning_rx_buffer[SHA204_BUFFER_POS_DATA];
282
288
}
0 commit comments