Skip to content

Commit cadbc3c

Browse files
committed
Merge pull request arduino#234 from fallberg/sign_debug
Refactored signing backend debug prints
2 parents 6928e6e + 8a134f4 commit cadbc3c

File tree

3 files changed

+98
-71
lines changed

3 files changed

+98
-71
lines changed

libraries/MySensors/core/MySigning.h

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <stdint.h>
3232
#include "MyMessage.h"
3333
#include "drivers/ATSHA204/ATSHA204.h"
34+
#include "MySensorCore.h"
3435

3536
#ifdef MY_SIGNING_NODE_WHITELISTING
3637
typedef struct {

libraries/MySensors/core/MySigningAtsha204.cpp

+48-36
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@
3030

3131
#define SIGNING_IDENTIFIER (1)
3232

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
3534

3635
ATSHA204Class atsha204(MY_SIGNING_ATSHA204_PIN);
3736
unsigned long _signing_timestamp;
@@ -49,40 +48,54 @@ void signerCalculateSignature(MyMessage &msg);
4948
uint8_t* signerSha256(const uint8_t* data, size_t sz);
5049

5150

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+
}
5760

58-
#ifdef DEBUG_SIGNING
5961
static void DEBUG_SIGNING_PRINTBUF(const __FlashStringHelper* str, uint8_t* buf, uint8_t sz)
6062
{
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
6269
for (int i=0; i<sz; i++)
6370
{
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]);
6973
}
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("");
7185
}
7286
#else
7387
#define DEBUG_SIGNING_PRINTBUF(str, buf, sz)
7488
#endif
7589

76-
7790
bool signerGetNonce(MyMessage &msg) {
78-
DEBUG_SIGNING_PRINTLN(F("ATSHA204"));
91+
DEBUG_SIGNING_PRINTBUF(F("Signing backend: ATSHA204"), NULL, 0);
7992
// Generate random number for use as nonce
8093
// We used a basic whitening technique that takes the first byte of a new random value and builds up a 32-byte random value
8194
// This 32-byte random value is then hashed (SHA256) to produce the resulting nonce
8295
for (int i = 0; i < 32; i++) {
8396
if (atsha204.sha204m_execute(SHA204_RANDOM, RANDOM_NO_SEED_UPDATE, 0, 0, NULL,
8497
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);
8699
return false;
87100
}
88101
_signing_current_nonce[i] = _singning_rx_buffer[SHA204_BUFFER_POS_DATA];
@@ -109,7 +122,7 @@ bool signerGetNonce(MyMessage &msg) {
109122
bool signerCheckTimer() {
110123
if (_signing_verification_ongoing) {
111124
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);
113126
// Purge nonce
114127
memset(_signing_current_nonce, 0x00, NONCE_NUMIN_SIZE_PASSTHROUGH);
115128
_signing_verification_ongoing = false;
@@ -120,9 +133,9 @@ bool signerCheckTimer() {
120133
}
121134

122135
bool signerPutNonce(MyMessage &msg) {
123-
DEBUG_SIGNING_PRINTLN(F("ATSHA204"));
136+
DEBUG_SIGNING_PRINTBUF(F("Signing backend: ATSHA204"), NULL, 0);
124137
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);
126139
return false;
127140
}
128141

@@ -135,7 +148,7 @@ bool signerPutNonce(MyMessage &msg) {
135148
bool signerSignMsg(MyMessage &msg) {
136149
// If we cannot fit any signature in the message, refuse to sign it
137150
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);
139152
return false;
140153
}
141154

@@ -149,22 +162,22 @@ bool signerSignMsg(MyMessage &msg) {
149162
_signing_current_nonce[32] = msg.sender;
150163
atsha204.getSerialNumber(&_signing_current_nonce[33]);
151164
(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);
153166
#endif
154167

155168
// Overwrite the first byte in the signature with the signing identifier
156169
_singning_rx_buffer[SHA204_BUFFER_POS_DATA] = SIGNING_IDENTIFIER;
157170

158171
// Transfer as much signature data as the remaining space in the message permits
159172
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));
161174

162175
return true;
163176
}
164177

165178
bool signerVerifyMsg(MyMessage &msg) {
166179
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);
168181
return false;
169182
} else {
170183
// Make sure we have not expired
@@ -175,18 +188,18 @@ bool signerVerifyMsg(MyMessage &msg) {
175188
_signing_verification_ongoing = false;
176189

177190
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);
179192
return false;
180193
}
181194

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));
183196
signerCalculateSignature(msg); // Get signature of message
184197

185198
#ifdef MY_SIGNING_NODE_WHITELISTING
186199
// Look up the senders nodeId in our whitelist and salt the signature with that data
187200
for (int j=0; j < NUM_OF(_signing_whitelist); j++) {
188201
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);
190203
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
191204
_signing_current_nonce[32] = msg.sender;
192205
memcpy(&_signing_current_nonce[33], _signing_whitelist[j].serial, SHA204_SERIAL_SZ);
@@ -201,13 +214,13 @@ bool signerVerifyMsg(MyMessage &msg) {
201214

202215
// Compare the caluclated signature with the provided signature
203216
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));
205218
#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);
207220
#endif
208221
return false;
209222
} else {
210-
DEBUG_SIGNING_PRINTLN(F("SOK")); // SOK = Signature OK
223+
DEBUG_SIGNING_PRINTBUF(F("Signature OK"), NULL, 0);
211224
return true;
212225
}
213226
}
@@ -219,8 +232,8 @@ void signerCalculateSignature(MyMessage &msg) {
219232
memcpy(_signing_temp_message, (uint8_t*)&msg.data[1-HEADER_SIZE], MAX_MESSAGE_LENGTH-1-(MAX_PAYLOAD-mGetLength(msg)));
220233

221234
// 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);
224237
(void)atsha204.sha204m_execute(SHA204_WRITE, SHA204_ZONE_DATA | SHA204_ZONE_COUNT_FLAG, 8 << 3, 32, _signing_temp_message,
225238
WRITE_COUNT_LONG, _singning_tx_buffer, WRITE_RSP_SIZE, _singning_rx_buffer);
226239

@@ -242,7 +255,7 @@ void signerCalculateSignature(MyMessage &msg) {
242255
// Put device back to sleep
243256
atsha204.sha204c_sleep();
244257

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);
246259
}
247260

248261
// 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) {
259272
// Write length data to the last bytes
260273
_signing_temp_message[SHA_MSG_SIZE-2] = (sz >> 5);
261274
_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
263275
(void)atsha204.sha204m_execute(SHA204_SHA, SHA_CALC, 0, SHA_MSG_SIZE, _signing_temp_message,
264276
SHA_COUNT_LONG, _singning_tx_buffer, SHA_RSP_SIZE_LONG, _singning_rx_buffer);
265277

266278
// Put device back to sleep
267279
atsha204.sha204c_sleep();
268280

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);
270282
return &_singning_rx_buffer[SHA204_BUFFER_POS_DATA];
271283
}
272284
#endif // #if defined(ARDUINO_ARCH_AVR)

0 commit comments

Comments
 (0)