13
13
// limitations under the License.
14
14
15
15
#include < Arduino.h>
16
+ #include < os.h> // For forced_memzero
16
17
#include " PBKDF2_HMACBuilder.h"
17
18
18
19
// Block size for HMAC (64 bytes for SHA-1, SHA-256, SHA-512)
@@ -47,6 +48,7 @@ PBKDF2_HMACBuilder::~PBKDF2_HMACBuilder() {
47
48
48
49
void PBKDF2_HMACBuilder::clearData () {
49
50
if (derivedKey != nullptr ) {
51
+ forced_memzero (derivedKey, derivedKeyLen);
50
52
delete[] derivedKey;
51
53
derivedKey = nullptr ;
52
54
}
@@ -126,6 +128,7 @@ void PBKDF2_HMACBuilder::calculate() {
126
128
127
129
// Allocate output buffer
128
130
if (derivedKey != nullptr ) {
131
+ forced_memzero (derivedKey, derivedKeyLen);
129
132
delete[] derivedKey;
130
133
}
131
134
derivedKey = new uint8_t [derivedKeyLen];
@@ -148,9 +151,8 @@ void PBKDF2_HMACBuilder::getChars(char *output) {
148
151
log_e (" Error: PBKDF2-HMAC not calculated or no output buffer provided." );
149
152
return ;
150
153
}
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);
154
156
}
155
157
156
158
String PBKDF2_HMACBuilder::toString () {
@@ -159,19 +161,15 @@ String PBKDF2_HMACBuilder::toString() {
159
161
return " " ;
160
162
}
161
163
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);
170
167
}
171
168
172
169
// PBKDF2 specific methods
173
170
void PBKDF2_HMACBuilder::setPassword (const uint8_t * password, size_t len) {
174
171
if (this ->password != nullptr ) {
172
+ forced_memzero (this ->password , len);
175
173
delete[] this ->password ;
176
174
}
177
175
this ->password = new uint8_t [len];
@@ -190,6 +188,7 @@ void PBKDF2_HMACBuilder::setPassword(String password) {
190
188
191
189
void PBKDF2_HMACBuilder::setSalt (const uint8_t * salt, size_t len) {
192
190
if (this ->salt != nullptr ) {
191
+ forced_memzero (this ->salt , len);
193
192
delete[] this ->salt ;
194
193
}
195
194
this ->salt = new uint8_t [len];
0 commit comments