33
33
34
34
unsigned long _signing_timestamp;
35
35
bool _signing_verification_ongoing = false ;
36
- uint8_t _signing_current_nonce[NONCE_NUMIN_SIZE_PASSTHROUGH+SHA204_SERIAL_SZ+1 ];
36
+ uint8_t _signing_verifying_nonce[NONCE_NUMIN_SIZE_PASSTHROUGH+SHA204_SERIAL_SZ+1 ];
37
+ uint8_t _signing_signing_nonce[NONCE_NUMIN_SIZE_PASSTHROUGH+SHA204_SERIAL_SZ+1 ];
37
38
uint8_t _signing_temp_message[SHA_MSG_SIZE];
38
- uint8_t _singning_rx_buffer [SHA204_RSP_SIZE_MAX];
39
- uint8_t _singning_tx_buffer [SHA204_CMD_SIZE_MAX];
39
+ uint8_t _signing_rx_buffer [SHA204_RSP_SIZE_MAX];
40
+ uint8_t _signing_tx_buffer [SHA204_CMD_SIZE_MAX];
40
41
extern uint8_t _doWhitelist[32 ];
41
42
42
43
#ifdef MY_SIGNING_NODE_WHITELISTING
43
44
const whitelist_entry_t _signing_whitelist[] = MY_SIGNING_NODE_WHITELISTING;
44
45
#endif
45
46
46
- static void signerCalculateSignature (MyMessage &msg);
47
+ static void signerCalculateSignature (MyMessage &msg, bool signing );
47
48
static uint8_t * signerSha256 (const uint8_t * data, size_t sz);
48
49
49
50
@@ -94,7 +95,8 @@ bool signerAtsha204CheckTimer(void) {
94
95
if (hwMillis () < _signing_timestamp || hwMillis () > _signing_timestamp + MY_VERIFICATION_TIMEOUT_MS) {
95
96
DEBUG_SIGNING_PRINTBUF (F (" Verification timeout" ), NULL , 0 );
96
97
// Purge nonce
97
- memset (_signing_current_nonce, 0x00 , NONCE_NUMIN_SIZE_PASSTHROUGH);
98
+ memset (_signing_signing_nonce, 0x00 , NONCE_NUMIN_SIZE_PASSTHROUGH);
99
+ memset (_signing_verifying_nonce, 0x00 , NONCE_NUMIN_SIZE_PASSTHROUGH);
98
100
_signing_verification_ongoing = false ;
99
101
return false ;
100
102
}
@@ -109,22 +111,22 @@ bool signerAtsha204GetNonce(MyMessage &msg) {
109
111
// This 32-byte random value is then hashed (SHA256) to produce the resulting nonce
110
112
(void )atsha204_wakeup (_signing_temp_message);
111
113
if (atsha204_execute (SHA204_RANDOM, RANDOM_SEED_UPDATE, 0 , 0 , NULL ,
112
- RANDOM_COUNT, _singning_tx_buffer , RANDOM_RSP_SIZE, _singning_rx_buffer ) != SHA204_SUCCESS) {
114
+ RANDOM_COUNT, _signing_tx_buffer , RANDOM_RSP_SIZE, _signing_rx_buffer ) != SHA204_SUCCESS) {
113
115
DEBUG_SIGNING_PRINTBUF (F (" Failed to generate nonce" ), NULL , 0 );
114
116
return false ;
115
117
}
116
118
for (int i = 0 ; i < 32 ; i++) {
117
- _signing_current_nonce [i] = _singning_rx_buffer [SHA204_BUFFER_POS_DATA+i] ^ (hwMillis ()&0xFF );
119
+ _signing_verifying_nonce [i] = _signing_rx_buffer [SHA204_BUFFER_POS_DATA+i] ^ (hwMillis ()&0xFF );
118
120
}
119
- memcpy (_signing_current_nonce , signerSha256 (_signing_current_nonce , 32 ), MAX_PAYLOAD);
121
+ memcpy (_signing_verifying_nonce , signerSha256 (_signing_verifying_nonce , 32 ), MAX_PAYLOAD);
120
122
121
123
atsha204_idle (); // We just idle the chip now since we expect to use it soon when the signed message arrives
122
124
123
125
// We set the part of the 32-byte nonce that does not fit into a message to 0xAA
124
- memset (&_signing_current_nonce [MAX_PAYLOAD], 0xAA , sizeof (_signing_current_nonce )-MAX_PAYLOAD);
126
+ memset (&_signing_verifying_nonce [MAX_PAYLOAD], 0xAA , sizeof (_signing_verifying_nonce )-MAX_PAYLOAD);
125
127
126
128
// Transfer the first part of the nonce to the message
127
- msg.set (_signing_current_nonce , MAX_PAYLOAD);
129
+ msg.set (_signing_verifying_nonce , MAX_PAYLOAD);
128
130
_signing_verification_ongoing = true ;
129
131
_signing_timestamp = hwMillis (); // Set timestamp to determine when to purge nonce
130
132
// Be a little fancy to handle turnover (prolong the time allowed to timeout after turnover)
@@ -137,9 +139,9 @@ bool signerAtsha204GetNonce(MyMessage &msg) {
137
139
void signerAtsha204PutNonce (MyMessage &msg) {
138
140
DEBUG_SIGNING_PRINTBUF (F (" Signing backend: ATSHA204" ), NULL , 0 );
139
141
140
- memcpy (_signing_current_nonce , (uint8_t *)msg.getCustom (), MAX_PAYLOAD);
142
+ memcpy (_signing_signing_nonce , (uint8_t *)msg.getCustom (), MAX_PAYLOAD);
141
143
// We set the part of the 32-byte nonce that does not fit into a message to 0xAA
142
- memset (&_signing_current_nonce [MAX_PAYLOAD], 0xAA , sizeof (_signing_current_nonce )-MAX_PAYLOAD);
144
+ memset (&_signing_signing_nonce [MAX_PAYLOAD], 0xAA , sizeof (_signing_signing_nonce )-MAX_PAYLOAD);
143
145
}
144
146
145
147
bool signerAtsha204SignMsg (MyMessage &msg) {
@@ -151,25 +153,25 @@ bool signerAtsha204SignMsg(MyMessage &msg) {
151
153
152
154
// Calculate signature of message
153
155
mSetSigned (msg, 1 ); // make sure signing flag is set before signature is calculated
154
- signerCalculateSignature (msg);
156
+ signerCalculateSignature (msg, true );
155
157
156
158
if (DO_WHITELIST (msg.destination )) {
157
159
// Salt the signature with the senders nodeId and the unique serial of the ATSHA device
158
- 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
159
- _signing_current_nonce [32 ] = msg.sender ;
160
- atsha204_getSerialNumber (&_signing_current_nonce [33 ]);
161
- (void )signerSha256 (_signing_current_nonce , 32 +1 +SHA204_SERIAL_SZ); // we can 'void' sha256 because the hash is already put in the correct place
160
+ memcpy (_signing_signing_nonce , &_signing_rx_buffer [SHA204_BUFFER_POS_DATA], 32 ); // We can reuse the nonce buffer now since it is no longer needed
161
+ _signing_signing_nonce [32 ] = msg.sender ;
162
+ atsha204_getSerialNumber (&_signing_signing_nonce [33 ]);
163
+ (void )signerSha256 (_signing_signing_nonce , 32 +1 +SHA204_SERIAL_SZ); // we can 'void' sha256 because the hash is already put in the correct place
162
164
DEBUG_SIGNING_PRINTBUF (F (" Signature salted with serial" ), NULL , 0 );
163
165
}
164
166
165
167
// Put device back to sleep
166
168
atsha204_sleep ();
167
169
168
170
// Overwrite the first byte in the signature with the signing identifier
169
- _singning_rx_buffer [SHA204_BUFFER_POS_DATA] = SIGNING_IDENTIFIER;
171
+ _signing_rx_buffer [SHA204_BUFFER_POS_DATA] = SIGNING_IDENTIFIER;
170
172
171
173
// Transfer as much signature data as the remaining space in the message permits
172
- memcpy (&msg.data [mGetLength (msg)], &_singning_rx_buffer [SHA204_BUFFER_POS_DATA], MAX_PAYLOAD-mGetLength (msg));
174
+ memcpy (&msg.data [mGetLength (msg)], &_signing_rx_buffer [SHA204_BUFFER_POS_DATA], MAX_PAYLOAD-mGetLength (msg));
173
175
DEBUG_SIGNING_PRINTBUF (F (" Signature in message: " ), (uint8_t *)&msg.data [mGetLength (msg)], MAX_PAYLOAD-mGetLength (msg));
174
176
175
177
return true ;
@@ -193,18 +195,18 @@ bool signerAtsha204VerifyMsg(MyMessage &msg) {
193
195
}
194
196
195
197
DEBUG_SIGNING_PRINTBUF (F (" Signature in message: " ), (uint8_t *)&msg.data [mGetLength (msg)], MAX_PAYLOAD-mGetLength (msg));
196
- signerCalculateSignature (msg); // Get signature of message
198
+ signerCalculateSignature (msg, false ); // Get signature of message
197
199
198
200
#ifdef MY_SIGNING_NODE_WHITELISTING
199
201
// Look up the senders nodeId in our whitelist and salt the signature with that data
200
202
size_t j;
201
203
for (j=0 ; j < NUM_OF (_signing_whitelist); j++) {
202
204
if (_signing_whitelist[j].nodeId == msg.sender ) {
203
205
DEBUG_SIGNING_PRINTBUF (F (" Sender found in whitelist" ), NULL , 0 );
204
- 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
205
- _signing_current_nonce [32 ] = msg.sender ;
206
- memcpy (&_signing_current_nonce [33 ], _signing_whitelist[j].serial , SHA204_SERIAL_SZ);
207
- (void )signerSha256 (_signing_current_nonce , 32 +1 +SHA204_SERIAL_SZ); // we can 'void' sha256 because the hash is already put in the correct place
206
+ 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
207
+ _signing_verifying_nonce [32 ] = msg.sender ;
208
+ memcpy (&_signing_verifying_nonce [33 ], _signing_whitelist[j].serial , SHA204_SERIAL_SZ);
209
+ (void )signerSha256 (_signing_verifying_nonce , 32 +1 +SHA204_SERIAL_SZ); // we can 'void' sha256 because the hash is already put in the correct place
208
210
break ;
209
211
}
210
212
}
@@ -220,11 +222,11 @@ bool signerAtsha204VerifyMsg(MyMessage &msg) {
220
222
atsha204_sleep ();
221
223
222
224
// Overwrite the first byte in the signature with the signing identifier
223
- _singning_rx_buffer [SHA204_BUFFER_POS_DATA] = SIGNING_IDENTIFIER;
225
+ _signing_rx_buffer [SHA204_BUFFER_POS_DATA] = SIGNING_IDENTIFIER;
224
226
225
227
// Compare the caluclated signature with the provided signature
226
- if (signerMemcmp (&msg.data [mGetLength (msg)], &_singning_rx_buffer [SHA204_BUFFER_POS_DATA], MAX_PAYLOAD-mGetLength (msg))) {
227
- DEBUG_SIGNING_PRINTBUF (F (" Signature bad: " ), &_singning_rx_buffer [SHA204_BUFFER_POS_DATA], MAX_PAYLOAD-mGetLength (msg));
228
+ if (signerMemcmp (&msg.data [mGetLength (msg)], &_signing_rx_buffer [SHA204_BUFFER_POS_DATA], MAX_PAYLOAD-mGetLength (msg))) {
229
+ DEBUG_SIGNING_PRINTBUF (F (" Signature bad: " ), &_signing_rx_buffer [SHA204_BUFFER_POS_DATA], MAX_PAYLOAD-mGetLength (msg));
228
230
#ifdef MY_SIGNING_NODE_WHITELISTING
229
231
DEBUG_SIGNING_PRINTBUF (F (" Is the sender whitelisted and serial correct?" ), NULL , 0 );
230
232
#endif
@@ -236,42 +238,43 @@ bool signerAtsha204VerifyMsg(MyMessage &msg) {
236
238
}
237
239
}
238
240
239
- // Helper to calculate signature of msg (returned in _singning_rx_buffer [SHA204_BUFFER_POS_DATA])
240
- static void signerCalculateSignature (MyMessage &msg) {
241
+ // Helper to calculate signature of msg (returned in _signing_rx_buffer [SHA204_BUFFER_POS_DATA])
242
+ static void signerCalculateSignature (MyMessage &msg, bool signing ) {
241
243
(void )atsha204_wakeup (_signing_temp_message);
242
244
memset (_signing_temp_message, 0 , 32 );
243
245
memcpy (_signing_temp_message, (uint8_t *)&msg.data [1 -HEADER_SIZE], MAX_MESSAGE_LENGTH-1 -(MAX_PAYLOAD-mGetLength (msg)));
244
246
245
247
// Program the data to sign into the ATSHA204
246
248
DEBUG_SIGNING_PRINTBUF (F (" Message to process: " ), (uint8_t *)&msg.data [1 -HEADER_SIZE], MAX_MESSAGE_LENGTH-1 -(MAX_PAYLOAD-mGetLength (msg)));
247
- DEBUG_SIGNING_PRINTBUF (F (" Current nonce: " ), _signing_current_nonce , 32 );
249
+ DEBUG_SIGNING_PRINTBUF (F (" Current nonce: " ), signing ? _signing_signing_nonce : _signing_verifying_nonce , 32 );
248
250
(void )atsha204_execute (SHA204_WRITE, SHA204_ZONE_DATA | SHA204_ZONE_COUNT_FLAG, 8 << 3 , 32 , _signing_temp_message,
249
- WRITE_COUNT_LONG, _singning_tx_buffer , WRITE_RSP_SIZE, _singning_rx_buffer );
251
+ WRITE_COUNT_LONG, _signing_tx_buffer , WRITE_RSP_SIZE, _signing_rx_buffer );
250
252
251
253
// Program the nonce to use for the signature (has to be done just before GENDIG due to chip limitations)
252
- (void )atsha204_execute (SHA204_NONCE, NONCE_MODE_PASSTHROUGH, 0 , NONCE_NUMIN_SIZE_PASSTHROUGH, _signing_current_nonce,
253
- NONCE_COUNT_LONG, _singning_tx_buffer, NONCE_RSP_SIZE_SHORT, _singning_rx_buffer);
254
+ (void )atsha204_execute (SHA204_NONCE, NONCE_MODE_PASSTHROUGH, 0 , NONCE_NUMIN_SIZE_PASSTHROUGH,
255
+ signing ? _signing_signing_nonce : _signing_verifying_nonce,
256
+ NONCE_COUNT_LONG, _signing_tx_buffer, NONCE_RSP_SIZE_SHORT, _signing_rx_buffer);
254
257
255
258
// Purge nonce when used
256
- memset (_signing_current_nonce , 0x00 , NONCE_NUMIN_SIZE_PASSTHROUGH);
259
+ memset (signing ? _signing_signing_nonce : _signing_verifying_nonce , 0x00 , NONCE_NUMIN_SIZE_PASSTHROUGH);
257
260
258
261
// Generate digest of data and nonce
259
262
(void )atsha204_execute (SHA204_GENDIG, GENDIG_ZONE_DATA, 8 , 0 , NULL ,
260
- GENDIG_COUNT_DATA, _singning_tx_buffer , GENDIG_RSP_SIZE, _singning_rx_buffer );
263
+ GENDIG_COUNT_DATA, _signing_tx_buffer , GENDIG_RSP_SIZE, _signing_rx_buffer );
261
264
262
265
// Calculate HMAC of message+nonce digest and secret key
263
266
(void )atsha204_execute (SHA204_HMAC, HMAC_MODE_SOURCE_FLAG_MATCH, 0 , 0 , NULL ,
264
- HMAC_COUNT, _singning_tx_buffer , HMAC_RSP_SIZE, _singning_rx_buffer );
267
+ HMAC_COUNT, _signing_tx_buffer , HMAC_RSP_SIZE, _signing_rx_buffer );
265
268
266
- DEBUG_SIGNING_PRINTBUF (F (" HMAC: " ), &_singning_rx_buffer [SHA204_BUFFER_POS_DATA], 32 );
269
+ DEBUG_SIGNING_PRINTBUF (F (" HMAC: " ), &_signing_rx_buffer [SHA204_BUFFER_POS_DATA], 32 );
267
270
}
268
271
269
272
// Helper to calculate a generic SHA256 digest of provided buffer (only supports one block)
270
- // The pointer to the hash is returned, but the hash is also stored in _singning_rx_buffer [SHA204_BUFFER_POS_DATA])
273
+ // The pointer to the hash is returned, but the hash is also stored in _signing_rx_buffer [SHA204_BUFFER_POS_DATA])
271
274
static uint8_t * signerSha256 (const uint8_t * data, size_t sz) {
272
275
// Initiate SHA256 calculator
273
276
(void )atsha204_execute (SHA204_SHA, SHA_INIT, 0 , 0 , NULL ,
274
- SHA_COUNT_SHORT, _singning_tx_buffer , SHA_RSP_SIZE_SHORT, _singning_rx_buffer );
277
+ SHA_COUNT_SHORT, _signing_tx_buffer , SHA_RSP_SIZE_SHORT, _signing_rx_buffer );
275
278
276
279
// Calculate a hash
277
280
memset (_signing_temp_message, 0x00 , SHA_MSG_SIZE);
@@ -281,8 +284,8 @@ static uint8_t* signerSha256(const uint8_t* data, size_t sz) {
281
284
_signing_temp_message[SHA_MSG_SIZE-2 ] = (sz >> 5 );
282
285
_signing_temp_message[SHA_MSG_SIZE-1 ] = (sz << 3 );
283
286
(void )atsha204_execute (SHA204_SHA, SHA_CALC, 0 , SHA_MSG_SIZE, _signing_temp_message,
284
- SHA_COUNT_LONG, _singning_tx_buffer , SHA_RSP_SIZE_LONG, _singning_rx_buffer );
287
+ SHA_COUNT_LONG, _signing_tx_buffer , SHA_RSP_SIZE_LONG, _signing_rx_buffer );
285
288
286
- DEBUG_SIGNING_PRINTBUF (F (" SHA256: " ), &_singning_rx_buffer [SHA204_BUFFER_POS_DATA], 32 );
287
- return &_singning_rx_buffer [SHA204_BUFFER_POS_DATA];
289
+ DEBUG_SIGNING_PRINTBUF (F (" SHA256: " ), &_signing_rx_buffer [SHA204_BUFFER_POS_DATA], 32 );
290
+ return &_signing_rx_buffer [SHA204_BUFFER_POS_DATA];
288
291
}
0 commit comments