Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 50a3db2

Browse files
fix(hash): Apply copilot suggestions
1 parent 6a98c23 commit 50a3db2

File tree

6 files changed

+54
-12
lines changed

6 files changed

+54
-12
lines changed

‎cores/esp32/HashBuilder.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,25 @@
2020

2121
#include "HEXBuilder.h"
2222

23+
/* Try to prevent most compilers from optimizing out clearing of memory that
24+
* becomes unaccessible after this function is called. This is mostly the case
25+
* for clearing local stack variables at the end of a function. This is not
26+
* exactly perfect, i.e., someone could come up with a compiler that figures out
27+
* the pointer is pointing to memset and then end up optimizing the call out, so
28+
* try go a bit further by storing the first octet (now zero) to make this even
29+
* a bit more difficult to optimize out. Once memset_s() is available, that
30+
* could be used here instead. */
31+
static void * (* const volatile memset_func)(void *, int, size_t) = memset;
32+
static uint8_t forced_memzero_val;
33+
34+
static inline void forced_memzero(void *ptr, size_t len)
35+
{
36+
memset_func(ptr, 0, len);
37+
if (len) {
38+
forced_memzero_val = ((uint8_t *) ptr)[0];
39+
}
40+
}
41+
2342
// Base class for hash builders
2443

2544
class HashBuilder : public HEXBuilder {

‎libraries/Hash/src/PBKDF2_HMACBuilder.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ PBKDF2_HMACBuilder::~PBKDF2_HMACBuilder() {
4747

4848
void PBKDF2_HMACBuilder::clearData() {
4949
if (derivedKey != nullptr) {
50+
forced_memzero(derivedKey, derivedKeyLen);
5051
delete[] derivedKey;
5152
derivedKey = nullptr;
5253
}
@@ -126,6 +127,7 @@ void PBKDF2_HMACBuilder::calculate() {
126127

127128
// Allocate output buffer
128129
if (derivedKey != nullptr) {
130+
forced_memzero(derivedKey, derivedKeyLen);
129131
delete[] derivedKey;
130132
}
131133
derivedKey = new uint8_t[derivedKeyLen];
@@ -148,9 +150,8 @@ void PBKDF2_HMACBuilder::getChars(char *output) {
148150
log_e("Error: PBKDF2-HMAC not calculated or no output buffer provided.");
149151
return;
150152
}
151-
for (size_t i = 0; i < derivedKeyLen; i++) {
152-
output[i] = (char)derivedKey[i];
153-
}
153+
154+
bytes2hex(output, derivedKeyLen * 2 + 1, derivedKey, derivedKeyLen);
154155
}
155156

156157
String PBKDF2_HMACBuilder::toString() {
@@ -159,19 +160,15 @@ String PBKDF2_HMACBuilder::toString() {
159160
return "";
160161
}
161162

162-
String result = "";
163-
for (size_t i = 0; i < derivedKeyLen; i++) {
164-
if (derivedKey[i] < 0x10) {
165-
result += "0";
166-
}
167-
result += String(derivedKey[i], HEX);
168-
}
169-
return result;
163+
char out[(derivedKeyLen * 2) + 1];
164+
getChars(out);
165+
return String(out);
170166
}
171167

172168
// PBKDF2 specific methods
173169
void PBKDF2_HMACBuilder::setPassword(const uint8_t* password, size_t len) {
174170
if (this->password != nullptr) {
171+
forced_memzero(this->password, len);
175172
delete[] this->password;
176173
}
177174
this->password = new uint8_t[len];
@@ -190,6 +187,7 @@ void PBKDF2_HMACBuilder::setPassword(String password) {
190187

191188
void PBKDF2_HMACBuilder::setSalt(const uint8_t* salt, size_t len) {
192189
if (this->salt != nullptr) {
190+
forced_memzero(this->salt, len);
193191
delete[] this->salt;
194192
}
195193
this->salt = new uint8_t[len];

‎libraries/Hash/src/SHA1Builder.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ void SHA1Builder::process(const uint8_t *data) {
195195
// Public methods
196196

197197
void SHA1Builder::begin(void) {
198+
finalized = false;
199+
198200
total[0] = 0;
199201
total[1] = 0;
200202

@@ -212,7 +214,7 @@ void SHA1Builder::add(const uint8_t *data, size_t len) {
212214
size_t fill;
213215
uint32_t left;
214216

215-
if (len == 0) {
217+
if (finalized || len == 0) {
216218
return;
217219
}
218220

@@ -289,6 +291,10 @@ void SHA1Builder::calculate(void) {
289291
uint32_t high, low;
290292
uint8_t msglen[8];
291293

294+
if (finalized) {
295+
return;
296+
}
297+
292298
high = (total[0] >> 29) | (total[1] << 3);
293299
low = (total[0] << 3);
294300

@@ -306,13 +312,20 @@ void SHA1Builder::calculate(void) {
306312
PUT_UINT32_BE(state[2], hash, 8);
307313
PUT_UINT32_BE(state[3], hash, 12);
308314
PUT_UINT32_BE(state[4], hash, 16);
315+
316+
finalized = true;
309317
}
310318

311319
void SHA1Builder::getBytes(uint8_t *output) {
312320
memcpy(output, hash, SHA1_HASH_SIZE);
313321
}
314322

315323
void SHA1Builder::getChars(char *output) {
324+
if (!finalized || output == nullptr) {
325+
log_e("Error: SHA1 not calculated or no output buffer provided.");
326+
return;
327+
}
328+
316329
bytes2hex(output, SHA1_HASH_SIZE * 2 + 1, hash, SHA1_HASH_SIZE);
317330
}
318331

‎libraries/Hash/src/SHA1Builder.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@ class SHA1Builder : public HashBuilder {
2828
uint32_t state[5]; /* intermediate digest state */
2929
unsigned char buffer[64]; /* data block being processed */
3030
uint8_t hash[SHA1_HASH_SIZE]; /* SHA-1 result */
31+
bool finalized; /* Whether hash has been finalized */
3132

3233
void process(const uint8_t *data);
3334

3435
public:
3536
using HashBuilder::add;
3637

38+
SHA1Builder() : finalized(false) {}
3739
void begin() override;
3840
void add(const uint8_t *data, size_t len) override;
3941
bool addStream(Stream &stream, const size_t maxLen) override;

‎libraries/Hash/src/SHA2Builder.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,11 @@ void SHA2Builder::getBytes(uint8_t *output) {
423423

424424
// Get the hash as hex string
425425
void SHA2Builder::getChars(char *output) {
426+
if (!finalized || output == nullptr) {
427+
log_e("Error: SHA2 not calculated or no output buffer provided.");
428+
return;
429+
}
430+
426431
bytes2hex(output, hash_size * 2 + 1, hash, hash_size);
427432
}
428433

‎libraries/Hash/src/SHA3Builder.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,11 @@ void SHA3Builder::getBytes(uint8_t *output) {
253253

254254
// Get the hash as hex string
255255
void SHA3Builder::getChars(char *output) {
256+
if (!finalized || output == nullptr) {
257+
log_e("Error: SHA3 not calculated or no output buffer provided.");
258+
return;
259+
}
260+
256261
bytes2hex(output, hash_size * 2 + 1, hash, hash_size);
257262
}
258263

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /