Skip to content

Commit 97a74ad

Browse files
committed
Use secure_compare
1 parent f0e4050 commit 97a74ad

1 file changed

Lines changed: 2 additions & 4 deletions

File tree

src/Utils.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "Utils.h"
22
#include <AES.h>
3+
#include <Crypto.h>
34
#include <SHA256.h>
45

56
#ifdef ARDUINO
@@ -81,10 +82,7 @@ int Utils::MACThenDecrypt(const uint8_t* shared_secret, uint8_t* dest, const uin
8182
sha.update(src + CIPHER_MAC_SIZE, src_len - CIPHER_MAC_SIZE);
8283
sha.finalizeHMAC(shared_secret, PUB_KEY_SIZE, hmac, CIPHER_MAC_SIZE);
8384
}
84-
// constant-time comparison to prevent timing side-channel attacks
85-
uint8_t diff = 0;
86-
for (int i = 0; i < CIPHER_MAC_SIZE; i++) diff |= hmac[i] ^ src[i];
87-
if (diff == 0) {
85+
if (secure_compare(hmac, src, CIPHER_MAC_SIZE)) {
8886
return decrypt(shared_secret, dest, src + CIPHER_MAC_SIZE, src_len - CIPHER_MAC_SIZE);
8987
}
9088
return 0; // invalid HMAC

0 commit comments

Comments
 (0)