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 e4d4083

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

File tree

5 files changed

+36
-12
lines changed

5 files changed

+36
-12
lines changed

‎libraries/Hash/src/PBKDF2_HMACBuilder.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414

1515
#include <Arduino.h>
16+
#include <os.h> // For forced_memzero
1617
#include "PBKDF2_HMACBuilder.h"
1718

1819
// Block size for HMAC (64 bytes for SHA-1, SHA-256, SHA-512)
@@ -47,6 +48,7 @@ PBKDF2_HMACBuilder::~PBKDF2_HMACBuilder() {
4748

4849
void PBKDF2_HMACBuilder::clearData() {
4950
if (derivedKey != nullptr) {
51+
forced_memzero(derivedKey, derivedKeyLen);
5052
delete[] derivedKey;
5153
derivedKey = nullptr;
5254
}
@@ -126,6 +128,7 @@ void PBKDF2_HMACBuilder::calculate() {
126128

127129
// Allocate output buffer
128130
if (derivedKey != nullptr) {
131+
forced_memzero(derivedKey, derivedKeyLen);
129132
delete[] derivedKey;
130133
}
131134
derivedKey = new uint8_t[derivedKeyLen];
@@ -148,9 +151,8 @@ void PBKDF2_HMACBuilder::getChars(char *output) {
148151
log_e("Error: PBKDF2-HMAC not calculated or no output buffer provided.");
149152
return;
150153
}
151-
for (size_t i = 0; i < derivedKeyLen; i++) {
152-
output[i] = (char)derivedKey[i];
153-
}
154+
155+
bytes2hex(output, derivedKeyLen * 2 + 1, derivedKey, derivedKeyLen);
154156
}
155157

156158
String PBKDF2_HMACBuilder::toString() {
@@ -159,19 +161,15 @@ String PBKDF2_HMACBuilder::toString() {
159161
return "";
160162
}
161163

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;
164+
char out[(derivedKeyLen * 2) + 1];
165+
getChars(out);
166+
return String(out);
170167
}
171168

172169
// PBKDF2 specific methods
173170
void PBKDF2_HMACBuilder::setPassword(const uint8_t* password, size_t len) {
174171
if (this->password != nullptr) {
172+
forced_memzero(this->password, len);
175173
delete[] this->password;
176174
}
177175
this->password = new uint8_t[len];
@@ -190,6 +188,7 @@ void PBKDF2_HMACBuilder::setPassword(String password) {
190188

191189
void PBKDF2_HMACBuilder::setSalt(const uint8_t* salt, size_t len) {
192190
if (this->salt != nullptr) {
191+
forced_memzero(this->salt, len);
193192
delete[] this->salt;
194193
}
195194
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 によって変換されたページ (->オリジナル) /