Skip to content

Commit 1040fbe

Browse files
fallberghenrikekblad
authored andcommitted
Refactor signing backend (#690)
Cleaned up the AES driver and removed some cppcheck isues in the process.
1 parent 61e6cec commit 1040fbe

File tree

27 files changed

+398
-14004
lines changed

27 files changed

+398
-14004
lines changed

core/MySigning.cpp

Lines changed: 286 additions & 247 deletions
Large diffs are not rendered by default.

core/MySigning.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -490,14 +490,6 @@ typedef struct {
490490
/** @brief Helper macro to determine the number of elements in a array */
491491
#define NUM_OF(x) (sizeof(x)/sizeof(x[0]))
492492

493-
/** @brief Helper macro to determine if node require serial salted signatures */
494-
#define DO_WHITELIST(node) (~_doWhitelist[node>>3]&(1<<node%8))
495-
/** @brief Helper macro to set that node require serial salted signatures */
496-
#define SET_WHITELIST(node) (_doWhitelist[node>>3]&=~(1<<node%8))
497-
/** @brief Helper macro to set that node does not require serial salted signatures */
498-
#define CLEAR_WHITELIST(node) (_doWhitelist[node>>3]|=(1<<node%8))
499-
500-
501493
/**
502494
* @brief Initializes signing infrastructure and associated backend.
503495
*

core/MySigningAtsha204.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ static char i2h(uint8_t i)
6262
static void DEBUG_SIGNING_PRINTBUF(const __FlashStringHelper* str, uint8_t* buf, uint8_t sz)
6363
{
6464
static char printBuffer[300];
65+
if (NULL == buf) {
66+
return;
67+
}
6568
#ifdef MY_GATEWAY_FEATURE
6669
// prepend debug message to be handled correctly by controller (C_INTERNAL, I_LOG_MESSAGE)
6770
snprintf_P(printBuffer, 299, PSTR("0;255;%d;0;%d;"), C_INTERNAL, I_LOG_MESSAGE);

core/MySigningAtsha204Soft.cpp

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -64,25 +64,17 @@ static char i2h(uint8_t i)
6464
}
6565

6666
#ifdef __linux__
67-
static void DEBUG_SIGNING_PRINTBUF(const char *str, uint8_t* buf, uint8_t sz)
68-
{
69-
static char printBuffer[300];
67+
#define __FlashStringHelper char
68+
#define MY_SERIALDEVICE.print debug
69+
#endif
7070

71-
for (int i=0; i<sz; i++) {
72-
printBuffer[i * 2] = i2h(buf[i] >> 4);
73-
printBuffer[(i * 2) + 1] = i2h(buf[i]);
74-
}
75-
printBuffer[sz * 2] = '\0';
76-
debug(str);
77-
if (sz > 0) {
78-
debug(printBuffer);
79-
}
80-
}
81-
#else
8271
static void DEBUG_SIGNING_PRINTBUF(const __FlashStringHelper* str, uint8_t* buf, uint8_t sz)
8372
{
8473
static char printBuffer[300];
85-
#ifdef MY_GATEWAY_FEATURE
74+
if (NULL == buf) {
75+
return;
76+
}
77+
#if defined(MY_GATEWAY_FEATURE) && !defined(__linux__)
8678
// prepend debug message to be handled correctly by controller (C_INTERNAL, I_LOG_MESSAGE)
8779
snprintf_P(printBuffer, 299, PSTR("0;255;%d;0;%d;"), C_INTERNAL, I_LOG_MESSAGE);
8880
MY_SERIALDEVICE.print(printBuffer);
@@ -92,17 +84,18 @@ static void DEBUG_SIGNING_PRINTBUF(const __FlashStringHelper* str, uint8_t* buf,
9284
printBuffer[(i * 2) + 1] = i2h(buf[i]);
9385
}
9486
printBuffer[sz * 2] = '\0';
95-
#ifdef MY_GATEWAY_FEATURE
87+
#if defined(MY_GATEWAY_FEATURE) && !defined(__linux__)
9688
// Truncate message if this is gateway node
9789
printBuffer[MY_GATEWAY_MAX_SEND_LENGTH-1-strlen_P((const char*)str)] = '\0';
9890
#endif
9991
MY_SERIALDEVICE.print(str);
10092
if (sz > 0) {
10193
MY_SERIALDEVICE.print(printBuffer);
10294
}
95+
#if !defined(__linux__)
10396
MY_SERIALDEVICE.println("");
97+
#endif
10498
}
105-
#endif /* __linux__ */
10699
#else
107100
#define DEBUG_SIGNING_PRINTBUF(str, buf, sz)
108101
#endif

drivers/AES/AES.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ const static byte s_inv [0x100] PROGMEM = {
104104
} ;
105105

106106
// times 2 in the GF(2^8)
107-
#define f2(x) ((x) & 0x80 ? (x << 1) ^ WPOLY : x << 1)
107+
#define f2(x) (((x) & 0x80) ? (x << 1) ^ WPOLY : x << 1)
108108
#define d2(x) (((x) >> 1) ^ ((x) & 1 ? DPOLY : 0))
109109

110110
static byte s_box (byte x)
@@ -248,6 +248,11 @@ static void inv_mix_sub_columns (byte dt[N_BLOCK], byte st[N_BLOCK])
248248
AES::AES()
249249
{
250250
byte ar_iv[8] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01 };
251+
IVC = 0x01;
252+
round = 0;
253+
pad = 0;
254+
size = 0;
255+
memset(key_sched, 0, KEY_SCHEDULE_BYTES);
251256
memcpy(iv,ar_iv,8);
252257
memcpy(iv+8,ar_iv,8);
253258
arr_pad[0] = 0x01;

drivers/AES/AES.h

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -312,42 +312,6 @@ class AES
312312

313313
#endif
314314

315-
/**
316-
* @example aes.pde
317-
* <b>For Arduino</b><br>
318-
* <b>Updated: spaniakos 2015 </b><br>
319-
*
320-
* This is an example of how to use AES in CBC mode easily.
321-
* The text and keys can be either in HEX or String format.<br />
322-
*/
323-
324-
/**
325-
* @example aes.cpp
326-
* <b>For Rasberry pi</b><br>
327-
* <b>Updated: spaniakos 2015 </b><br>
328-
*
329-
* This is an example of how to use AES in CBC mode easily.
330-
* The text and keys can be either in HEX or String format.<br />
331-
*/
332-
333-
/**
334-
* @example test_vectors.pde
335-
* <b>For Arduino</b><br>
336-
* <b>Updated: spaniakos 2015 </b><br>
337-
*
338-
* This is an example of monte carlo test vectors, in order to justify the effectiveness of the algorithm.<br />
339-
* plus is a classical approach to the AES encryption library with out the easy to use add-on modifications.
340-
*/
341-
342-
/**
343-
* @example test_vectors.cpp
344-
* <b>For Rasberry pi</b><br>
345-
* <b>Updated: spaniakos 2015 </b><br>
346-
*
347-
* This is an example of monte carlo test vectors, in order to justify the effectiveness of the algorithm.<br />
348-
* plus is a classical approach to the AES encryption library with out the easy to use add-on modifications.
349-
*/
350-
351315
/**
352316
* @defgroup aeslib AES library for Arduino and Raspberry pi
353317
* @ingroup internals

0 commit comments

Comments
 (0)