30
30
31
31
#define SIGNING_IDENTIFIER (1 )
32
32
33
- // Uncomment this to get some useful serial debug info (Serial.print and Serial.println expected)
34
- // #define DEBUG_SIGNING
33
+ // Define MY_DEBUG in your sketch to enable signing backend debugprints
35
34
36
35
ATSHA204Class atsha204 (MY_SIGNING_ATSHA204_PIN);
37
36
unsigned long _signing_timestamp;
@@ -49,40 +48,54 @@ void signerCalculateSignature(MyMessage &msg);
49
48
uint8_t * signerSha256 (const uint8_t * data, size_t sz);
50
49
51
50
52
- #ifdef DEBUG_SIGNING
53
- #define DEBUG_SIGNING_PRINTLN (args ) Serial.println(args)
54
- #else
55
- #define DEBUG_SIGNING_PRINTLN (args )
56
- #endif
51
+ #ifdef MY_DEBUG
52
+ static char i2h (uint8_t i)
53
+ {
54
+ uint8_t k = i & 0x0F ;
55
+ if (k <= 9 )
56
+ return ' 0' + k;
57
+ else
58
+ return ' A' + k - 10 ;
59
+ }
57
60
58
- #ifdef DEBUG_SIGNING
59
61
static void DEBUG_SIGNING_PRINTBUF (const __FlashStringHelper* str, uint8_t * buf, uint8_t sz)
60
62
{
61
- Serial.println (str);
63
+ static char printBuffer[32 *2 +1 ];
64
+ #ifdef MY_GATEWAY_FEATURE
65
+ // prepend debug message to be handled correctly by controller (C_INTERNAL, I_LOG_MESSAGE)
66
+ snprintf_P (printBuffer, 299 , PSTR (" 0;0;%d;0;%d;" ), C_INTERNAL, I_LOG_MESSAGE);
67
+ Serial.print (printBuffer);
68
+ #endif
62
69
for (int i=0 ; i<sz; i++)
63
70
{
64
- if (buf[i] < 0x10 )
65
- {
66
- Serial.print (' 0' ); // Because Serial.print does not 0-pad HEX
67
- }
68
- Serial.print (buf[i], HEX);
71
+ printBuffer[i * 2 ] = i2h (buf[i] >> 4 );
72
+ printBuffer[(i * 2 ) + 1 ] = i2h (buf[i]);
69
73
}
70
- Serial.println ();
74
+ printBuffer[sz * 2 ] = ' \0 ' ;
75
+ #ifdef MY_GATEWAY_FEATURE
76
+ // Truncate message if this is gateway node
77
+ printBuffer[59 -strlen_P ((const char *)str)] = ' \0 ' ;
78
+ #endif
79
+ Serial.print (str);
80
+ if (sz > 0 )
81
+ {
82
+ Serial.print (printBuffer);
83
+ }
84
+ Serial.println (" " );
71
85
}
72
86
#else
73
87
#define DEBUG_SIGNING_PRINTBUF (str, buf, sz )
74
88
#endif
75
89
76
-
77
90
bool signerGetNonce (MyMessage &msg) {
78
- DEBUG_SIGNING_PRINTLN (F (" ATSHA204" ));
91
+ DEBUG_SIGNING_PRINTBUF (F (" Signing backend: ATSHA204" ), NULL , 0 );
79
92
// Generate random number for use as nonce
80
93
// We used a basic whitening technique that takes the first byte of a new random value and builds up a 32-byte random value
81
94
// This 32-byte random value is then hashed (SHA256) to produce the resulting nonce
82
95
for (int i = 0 ; i < 32 ; i++) {
83
96
if (atsha204.sha204m_execute (SHA204_RANDOM, RANDOM_NO_SEED_UPDATE, 0 , 0 , NULL ,
84
97
RANDOM_COUNT, _singning_tx_buffer, RANDOM_RSP_SIZE, _singning_rx_buffer) != SHA204_SUCCESS) {
85
- DEBUG_SIGNING_PRINTLN (F (" FTGN " )); // FTGN = Failed to generate nonce
98
+ DEBUG_SIGNING_PRINTBUF (F (" Failed to generate nonce" ), NULL , 0 );
86
99
return false ;
87
100
}
88
101
_signing_current_nonce[i] = _singning_rx_buffer[SHA204_BUFFER_POS_DATA];
@@ -109,7 +122,7 @@ bool signerGetNonce(MyMessage &msg) {
109
122
bool signerCheckTimer () {
110
123
if (_signing_verification_ongoing) {
111
124
if (millis () < _signing_timestamp || millis () > _signing_timestamp + MY_VERIFICATION_TIMEOUT_MS) {
112
- DEBUG_SIGNING_PRINTLN (F (" VT " )); // VT = Verification timeout
125
+ DEBUG_SIGNING_PRINTBUF (F (" Verification timeout " ), NULL , 0 );
113
126
// Purge nonce
114
127
memset (_signing_current_nonce, 0x00 , NONCE_NUMIN_SIZE_PASSTHROUGH);
115
128
_signing_verification_ongoing = false ;
@@ -120,9 +133,9 @@ bool signerCheckTimer() {
120
133
}
121
134
122
135
bool signerPutNonce (MyMessage &msg) {
123
- DEBUG_SIGNING_PRINTLN (F (" ATSHA204" ));
136
+ DEBUG_SIGNING_PRINTBUF (F (" Signing backend: ATSHA204" ), NULL , 0 );
124
137
if (((uint8_t *)msg.getCustom ())[0 ] != SIGNING_IDENTIFIER) {
125
- DEBUG_SIGNING_PRINTLN (F (" ISI " )); // ISI = Incorrect signing identifier
138
+ DEBUG_SIGNING_PRINTBUF (F (" Incorrect signing identifier" ), NULL , 0 );
126
139
return false ;
127
140
}
128
141
@@ -135,7 +148,7 @@ bool signerPutNonce(MyMessage &msg) {
135
148
bool signerSignMsg (MyMessage &msg) {
136
149
// If we cannot fit any signature in the message, refuse to sign it
137
150
if (mGetLength (msg) > MAX_PAYLOAD-2 ) {
138
- DEBUG_SIGNING_PRINTLN (F (" MTOL " )); // Message too large for signature to fit
151
+ DEBUG_SIGNING_PRINTBUF (F (" Message too large" ), NULL , 0 );
139
152
return false ;
140
153
}
141
154
@@ -149,22 +162,22 @@ bool signerSignMsg(MyMessage &msg) {
149
162
_signing_current_nonce[32 ] = msg.sender ;
150
163
atsha204.getSerialNumber (&_signing_current_nonce[33 ]);
151
164
(void )signerSha256 (_signing_current_nonce, 32 +1 +SHA204_SERIAL_SZ); // we can 'void' sha256 because the hash is already put in the correct place
152
- DEBUG_SIGNING_PRINTLN (F (" SWS " )); // SWS = Signature whitelist salted
165
+ DEBUG_SIGNING_PRINTBUF (F (" Signature salted with serial " ), NULL , 0 );
153
166
#endif
154
167
155
168
// Overwrite the first byte in the signature with the signing identifier
156
169
_singning_rx_buffer[SHA204_BUFFER_POS_DATA] = SIGNING_IDENTIFIER;
157
170
158
171
// Transfer as much signature data as the remaining space in the message permits
159
172
memcpy (&msg.data [mGetLength (msg)], &_singning_rx_buffer[SHA204_BUFFER_POS_DATA], MAX_PAYLOAD-mGetLength (msg));
160
- DEBUG_SIGNING_PRINTBUF (F (" SIM: " ), (uint8_t *)&msg.data [mGetLength (msg)], MAX_PAYLOAD-mGetLength (msg)); // SIM = Signature in message
173
+ DEBUG_SIGNING_PRINTBUF (F (" Signature in message: " ), (uint8_t *)&msg.data [mGetLength (msg)], MAX_PAYLOAD-mGetLength (msg));
161
174
162
175
return true ;
163
176
}
164
177
165
178
bool signerVerifyMsg (MyMessage &msg) {
166
179
if (!_signing_verification_ongoing) {
167
- DEBUG_SIGNING_PRINTLN (F (" NAVS " )); // NAVS = No active verification session
180
+ DEBUG_SIGNING_PRINTBUF (F (" No active verification session" ), NULL , 0 );
168
181
return false ;
169
182
} else {
170
183
// Make sure we have not expired
@@ -175,18 +188,18 @@ bool signerVerifyMsg(MyMessage &msg) {
175
188
_signing_verification_ongoing = false ;
176
189
177
190
if (msg.data [mGetLength (msg)] != SIGNING_IDENTIFIER) {
178
- DEBUG_SIGNING_PRINTLN (F (" ISI " )); // ISI = Incorrect signing identifier
191
+ DEBUG_SIGNING_PRINTBUF (F (" Incorrect signing identifier" ), NULL , 0 );
179
192
return false ;
180
193
}
181
194
182
- DEBUG_SIGNING_PRINTBUF (F (" SIM: " ), (uint8_t *)&msg.data [mGetLength (msg)], MAX_PAYLOAD-mGetLength (msg)); // SIM = Signature in message
195
+ DEBUG_SIGNING_PRINTBUF (F (" Signature in message: " ), (uint8_t *)&msg.data [mGetLength (msg)], MAX_PAYLOAD-mGetLength (msg));
183
196
signerCalculateSignature (msg); // Get signature of message
184
197
185
198
#ifdef MY_SIGNING_NODE_WHITELISTING
186
199
// Look up the senders nodeId in our whitelist and salt the signature with that data
187
200
for (int j=0 ; j < NUM_OF (_signing_whitelist); j++) {
188
201
if (_signing_whitelist[j].nodeId == msg.sender ) {
189
- DEBUG_SIGNING_PRINTLN (F (" SIW " )); // SIW = Sender found in whitelist
202
+ DEBUG_SIGNING_PRINTBUF (F (" Sender found in whitelist" ), NULL , 0 );
190
203
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
191
204
_signing_current_nonce[32 ] = msg.sender ;
192
205
memcpy (&_signing_current_nonce[33 ], _signing_whitelist[j].serial , SHA204_SERIAL_SZ);
@@ -201,13 +214,13 @@ bool signerVerifyMsg(MyMessage &msg) {
201
214
202
215
// Compare the caluclated signature with the provided signature
203
216
if (memcmp (&msg.data [mGetLength (msg)], &_singning_rx_buffer[SHA204_BUFFER_POS_DATA], MAX_PAYLOAD-mGetLength (msg))) {
204
- DEBUG_SIGNING_PRINTBUF (F (" SNOK: " ), &_singning_rx_buffer[SHA204_BUFFER_POS_DATA], MAX_PAYLOAD-mGetLength (msg)); // SNOK = Signature bad
217
+ DEBUG_SIGNING_PRINTBUF (F (" Signature bad: " ), &_singning_rx_buffer[SHA204_BUFFER_POS_DATA], MAX_PAYLOAD-mGetLength (msg));
205
218
#ifdef MY_SIGNING_NODE_WHITELISTING
206
- DEBUG_SIGNING_PRINTLN (F (" W? " )); // W? = Is the sender whitelisted?
219
+ DEBUG_SIGNING_PRINTBUF (F (" Is the sender whitelisted and serial correct? " ), NULL , 0 );
207
220
#endif
208
221
return false ;
209
222
} else {
210
- DEBUG_SIGNING_PRINTLN (F (" SOK " )); // SOK = Signature OK
223
+ DEBUG_SIGNING_PRINTBUF (F (" Signature OK " ), NULL , 0 );
211
224
return true ;
212
225
}
213
226
}
@@ -219,8 +232,8 @@ void signerCalculateSignature(MyMessage &msg) {
219
232
memcpy (_signing_temp_message, (uint8_t *)&msg.data [1 -HEADER_SIZE], MAX_MESSAGE_LENGTH-1 -(MAX_PAYLOAD-mGetLength (msg)));
220
233
221
234
// Program the data to sign into the ATSHA204
222
- DEBUG_SIGNING_PRINTBUF (F (" MSG: " ), (uint8_t *)&msg.data [1 -HEADER_SIZE], MAX_MESSAGE_LENGTH-1 -(MAX_PAYLOAD-mGetLength (msg))); // MSG = Message to sign
223
- DEBUG_SIGNING_PRINTBUF (F (" CNC: " ), _signing_current_nonce, 32 ); // CNC = Current nonce
235
+ DEBUG_SIGNING_PRINTBUF (F (" Message to process: " ), (uint8_t *)&msg.data [1 -HEADER_SIZE], MAX_MESSAGE_LENGTH-1 -(MAX_PAYLOAD-mGetLength (msg)));
236
+ DEBUG_SIGNING_PRINTBUF (F (" Current nonce: " ), _signing_current_nonce, 32 );
224
237
(void )atsha204.sha204m_execute (SHA204_WRITE, SHA204_ZONE_DATA | SHA204_ZONE_COUNT_FLAG, 8 << 3 , 32 , _signing_temp_message,
225
238
WRITE_COUNT_LONG, _singning_tx_buffer, WRITE_RSP_SIZE, _singning_rx_buffer);
226
239
@@ -242,7 +255,7 @@ void signerCalculateSignature(MyMessage &msg) {
242
255
// Put device back to sleep
243
256
atsha204.sha204c_sleep ();
244
257
245
- DEBUG_SIGNING_PRINTBUF (F (" HMAC:" ), &_singning_rx_buffer[SHA204_BUFFER_POS_DATA], 32 );
258
+ DEBUG_SIGNING_PRINTBUF (F (" HMAC: " ), &_singning_rx_buffer[SHA204_BUFFER_POS_DATA], 32 );
246
259
}
247
260
248
261
// Helper to calculate a generic SHA256 digest of provided buffer (only supports one block)
@@ -259,14 +272,13 @@ uint8_t* signerSha256(const uint8_t* data, size_t sz) {
259
272
// Write length data to the last bytes
260
273
_signing_temp_message[SHA_MSG_SIZE-2 ] = (sz >> 5 );
261
274
_signing_temp_message[SHA_MSG_SIZE-1 ] = (sz << 3 );
262
- DEBUG_SIGNING_PRINTBUF (F (" DTH:" ), _signing_temp_message, SHA_MSG_SIZE); // DTH = Data to hash
263
275
(void )atsha204.sha204m_execute (SHA204_SHA, SHA_CALC, 0 , SHA_MSG_SIZE, _signing_temp_message,
264
276
SHA_COUNT_LONG, _singning_tx_buffer, SHA_RSP_SIZE_LONG, _singning_rx_buffer);
265
277
266
278
// Put device back to sleep
267
279
atsha204.sha204c_sleep ();
268
280
269
- DEBUG_SIGNING_PRINTBUF (F (" SHA: " ), &_singning_rx_buffer[SHA204_BUFFER_POS_DATA], 32 );
281
+ DEBUG_SIGNING_PRINTBUF (F (" SHA256: " ), &_singning_rx_buffer[SHA204_BUFFER_POS_DATA], 32 );
270
282
return &_singning_rx_buffer[SHA204_BUFFER_POS_DATA];
271
283
}
272
284
#endif // #if defined(ARDUINO_ARCH_AVR)
0 commit comments