Skip to content

Commit ea7f7be

Browse files
authored
AVR: Fix variable scope in SHA256 ASM (#1188)
* AVR: Disable SHA256 ASM implementation * AVR: Fix variable scope in SHA256 ASM
1 parent 5051d51 commit ea7f7be

File tree

3 files changed

+34
-29
lines changed

3 files changed

+34
-29
lines changed

core/MySigningAtsha204Soft.cpp

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,6 @@ void signerAtsha204SoftPutNonce(MyMessage &msg)
187187
if (!_signing_init_ok) {
188188
return;
189189
}
190-
191190
(void)memcpy((void *)_signing_nonce, (const void *)msg.getCustom(), MIN(MAX_PAYLOAD, 32));
192191
if (MAX_PAYLOAD < 32) {
193192
// We set the part of the 32-byte nonce that does not fit into a message to 0xAA
@@ -355,36 +354,40 @@ static void signerAtsha204AHmac(uint8_t *dest, const uint8_t *nonce, const uint8
355354
// 25 bytes zeroes
356355
// 32 bytes nonce
357356

358-
uint8_t buffer[96];
357+
#if defined(MY_CRYPTO_SHA256_ASM)
358+
static uint8_t _signing_buffer[96]; // static for AVR ASM SHA256
359+
#else
360+
uint8_t _signing_buffer[96];
361+
#endif
359362
// Calculate message digest first
360-
(void)memset((void *)buffer, 0x00, sizeof(buffer));
361-
(void)memcpy((void *)buffer, (const void *)data, 32);
362-
buffer[0 + 32] = 0x15; // OPCODE
363-
buffer[1 + 32] = 0x02; // param1
364-
buffer[2 + 32] = 0x08; // param2(1)
365-
//buffer[3 + 32] = 0x00; // param2(2)
366-
buffer[4 + 32] = 0xEE; // SN[8]
367-
buffer[5 + 32] = 0x01; // SN[0]
368-
buffer[6 + 32] = 0x23; // SN[1]
369-
// buffer[7 + 32..31 + 32] => 0x00;
370-
(void)memcpy((void *)&buffer[64], (const void *)nonce, 32);
371-
SHA256(_signing_hmac, buffer, 96);
363+
(void)memset((void *)_signing_buffer, 0x00, sizeof(_signing_buffer));
364+
(void)memcpy((void *)_signing_buffer, (const void *)data, 32);
365+
_signing_buffer[0 + 32] = 0x15; // OPCODE
366+
_signing_buffer[1 + 32] = 0x02; // param1
367+
_signing_buffer[2 + 32] = 0x08; // param2(1)
368+
//_signing_buffer[3 + 32] = 0x00; // param2(2)
369+
_signing_buffer[4 + 32] = 0xEE; // SN[8]
370+
_signing_buffer[5 + 32] = 0x01; // SN[0]
371+
_signing_buffer[6 + 32] = 0x23; // SN[1]
372+
// _signing_buffer[7 + 32..31 + 32] => 0x00;
373+
(void)memcpy((void *)&_signing_buffer[64], (const void *)nonce, 32);
374+
SHA256(_signing_hmac, _signing_buffer, 96);
372375

373376
// Feed "message" to HMAC calculator
374-
(void)memset((void *)buffer, 0x00, sizeof(buffer));
375-
(void)memcpy((void *)&buffer[32], (const void *)_signing_hmac, 32);
376-
buffer[0 + 64] = 0x11; // OPCODE
377-
buffer[1 + 64] = 0x04; // Mode
378-
//buffer[2 + 64] = 0x00; // SlotID(1)
379-
//buffer[3 + 64] = 0x00; // SlotID(2)
380-
//buffer[4 + 64..14 + 64] => 0x00; // 11 bytes zeroes
381-
buffer[15 + 64] = 0xEE; // SN[8]
382-
//buffer[16 + 64..19 + 64] => 0x00; // 4 bytes zeroes
383-
buffer[20 + 64] = 0x01;
384-
buffer[21 + 64] = 0x23;
385-
//buffer[22 + 64] = 0x00; // SN[0]
386-
//buffer[23 + 64] = 0x00; // SN[1]
387-
SHA256HMAC(dest, _signing_hmac_key, 32, buffer, 88);
377+
(void)memset((void *)_signing_buffer, 0x00, sizeof(_signing_buffer));
378+
(void)memcpy((void *)&_signing_buffer[32], (const void *)_signing_hmac, 32);
379+
_signing_buffer[0 + 64] = 0x11; // OPCODE
380+
_signing_buffer[1 + 64] = 0x04; // Mode
381+
//_signing_buffer[2 + 64] = 0x00; // SlotID(1)
382+
//_signing_buffer[3 + 64] = 0x00; // SlotID(2)
383+
//_signing_buffer[4 + 64..14 + 64] => 0x00; // 11 bytes zeroes
384+
_signing_buffer[15 + 64] = 0xEE; // SN[8]
385+
//_signing_buffer[16 + 64..19 + 64] => 0x00; // 4 bytes zeroes
386+
_signing_buffer[20 + 64] = 0x01;
387+
_signing_buffer[21 + 64] = 0x23;
388+
//_signing_buffer[22 + 64] = 0x00; // SN[0]
389+
//_signing_buffer[23 + 64] = 0x00; // SN[1]
390+
SHA256HMAC(dest, _signing_hmac_key, 32, _signing_buffer, 88);
388391
}
389392

390393
#endif //MY_SIGNING_SOFT

hal/architecture/AVR/MyHwAVR.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
#include <Arduino.h>
3434
#endif
3535

36-
#define MYSENSORS_SHA256_ASM_AVR // use the ASM implementation for the sha256 code
36+
#define CRYPTO_LITTLE_ENDIAN
3737

3838
#ifndef MY_SERIALDEVICE
3939
#define MY_SERIALDEVICE Serial

hal/crypto/AVR/MyCryptoAVR.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848

4949
#include "hal/crypto/MyCryptoHAL.h"
5050

51+
#define MY_CRYPTO_SHA256_ASM //!< Switch to define correct variable scope for ASM SHA256 implementation
52+
5153
#define SHA256_HASH_BITS 256 //!< Defines the size of a SHA-256 hash value in bits
5254
#define SHA256_HASH_BYTES (SHA256_HASH_BITS/8) //!< Defines the size of a SHA-256 hash value in bytes
5355
#define SHA256_BLOCK_BITS 512 //!< Defines the size of a SHA-256 input block in bits

0 commit comments

Comments
 (0)