Skip to content

Commit 442531a

Browse files
authored
guard update crypt
1 parent a9cde70 commit 442531a

File tree

1 file changed

+36
-3
lines changed

1 file changed

+36
-3
lines changed

libraries/Update/src/Updater.cpp

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
#include "spi_flash_mmap.h"
1010
#include "esp_ota_ops.h"
1111
#include "esp_image_format.h"
12+
#ifdef UPDATE_CRYPT
1213
#include "mbedtls/aes.h"
14+
#endif /* UPDATE_CRYPT */
1315

1416
static const char *_err2str(uint8_t _error) {
1517
if (_error == UPDATE_ERROR_OK) {
@@ -38,8 +40,10 @@ static const char *_err2str(uint8_t _error) {
3840
return ("Bad Argument");
3941
} else if (_error == UPDATE_ERROR_ABORT) {
4042
return ("Aborted");
43+
#ifdef UPDATE_CRYPT
4144
} else if (_error == UPDATE_ERROR_DECRYPT) {
4245
return ("Decryption error");
46+
#endif /* UPDATE_CRYPT */
4347
}
4448
return ("UNKNOWN");
4549
}
@@ -67,8 +71,15 @@ bool UpdateClass::_enablePartition(const esp_partition_t *partition) {
6771
}
6872

6973
UpdateClass::UpdateClass()
70-
: _error(0), _cryptKey(0), _cryptBuffer(0), _buffer(0), _skipBuffer(0), _bufferLen(0), _size(0), _progress_callback(NULL), _progress(0), _paroffset(0),
71-
_command(U_FLASH), _partition(NULL), _cryptMode(U_AES_DECRYPT_AUTO), _cryptAddress(0), _cryptCfg(0xf) {}
74+
: _error(0),
75+
#ifdef UPDATE_CRYPT
76+
_cryptKey(0), _cryptBuffer(0),
77+
#endif /* UPDATE_CRYPT */
78+
_buffer(0), _skipBuffer(0), _bufferLen(0), _size(0), _progress_callback(NULL), _progress(0), _paroffset(0), _command(U_FLASH), _partition(NULL),
79+
#ifdef UPDATE_CRYPT
80+
_cryptMode(U_AES_DECRYPT_AUTO), _cryptAddress(0), _cryptCfg(0xf)
81+
#endif /* UPDATE_CRYPT */
82+
{}
7283

7384
UpdateClass &UpdateClass::onProgress(THandlerFunction_Progress fn) {
7485
_progress_callback = fn;
@@ -83,7 +94,9 @@ void UpdateClass::_reset() {
8394
delete[] _skipBuffer;
8495
}
8596

97+
#ifdef UPDATE_CRYPT
8698
_cryptBuffer = nullptr;
99+
#endif /* UPDATE_CRYPT */
87100
_buffer = nullptr;
88101
_skipBuffer = nullptr;
89102
_bufferLen = 0;
@@ -175,6 +188,7 @@ bool UpdateClass::begin(size_t size, int command, int ledPin, uint8_t ledOn, con
175188
return true;
176189
}
177190

191+
#ifdef UPDATE_CRYPT
178192
bool UpdateClass::setupCrypt(const uint8_t *cryptKey, size_t cryptAddress, uint8_t cryptConfig, int cryptMode) {
179193
if (setCryptKey(cryptKey)) {
180194
if (setCryptMode(cryptMode)) {
@@ -216,6 +230,7 @@ bool UpdateClass::setCryptMode(const int cryptMode) {
216230
}
217231
return true;
218232
}
233+
#endif /* UPDATE_CRYPT */
219234

220235
void UpdateClass::_abort(uint8_t err) {
221236
_reset();
@@ -226,6 +241,7 @@ void UpdateClass::abort() {
226241
_abort(UPDATE_ERROR_ABORT);
227242
}
228243

244+
#ifdef UPDATE_CRYPT
229245
void UpdateClass::_cryptKeyTweak(size_t cryptAddress, uint8_t *tweaked_key) {
230246
memcpy(tweaked_key, _cryptKey, ENCRYPTED_KEY_SIZE);
231247
if (_cryptCfg == 0) {
@@ -338,8 +354,10 @@ bool UpdateClass::_decryptBuffer() {
338354
}
339355
return true;
340356
}
357+
#endif /* UPDATE_CRYPT */
341358

342359
bool UpdateClass::_writeBuffer() {
360+
#ifdef UPDATE_CRYPT
343361
//first bytes of loading image, check to see if loading image needs decrypting
344362
if (!_progress) {
345363
_cryptMode &= U_AES_DECRYPT_MODE_MASK;
@@ -360,6 +378,7 @@ bool UpdateClass::_writeBuffer() {
360378
return false;
361379
}
362380
}
381+
#endif /* UPDATE_CRYPT */
363382
//first bytes of new firmware
364383
uint8_t skip = 0;
365384
if (!_progress && _command == U_FLASH) {
@@ -409,9 +428,13 @@ bool UpdateClass::_writeBuffer() {
409428
if (!_progress && _command == U_FLASH) {
410429
_buffer[0] = ESP_IMAGE_HEADER_MAGIC;
411430
}
431+
#ifdef UPDATE_CRYPT
412432
if (_target_md5_decrypted) {
433+
#endif /* UPDATE_CRYPT */
413434
_md5.add(_buffer, _bufferLen);
435+
#ifdef UPDATE_CRYPT
414436
}
437+
#endif /* UPDATE_CRYPT */
415438
_progress += _bufferLen;
416439
_bufferLen = 0;
417440
if (_progress_callback) {
@@ -453,13 +476,19 @@ bool UpdateClass::_verifyEnd() {
453476
return false;
454477
}
455478

456-
bool UpdateClass::setMD5(const char *expected_md5, bool calc_post_decryption) {
479+
bool UpdateClass::setMD5(const char *expected_md5
480+
#ifdef UPDATE_CRYPT
481+
,bool calc_post_decryption
482+
#endif /* UPDATE_CRYPT */
483+
) {
457484
if (strlen(expected_md5) != 32) {
458485
return false;
459486
}
460487
_target_md5 = expected_md5;
461488
_target_md5.toLowerCase();
489+
#ifdef UPDATE_CRYPT
462490
_target_md5_decrypted = calc_post_decryption;
491+
#endif /* UPDATE_CRYPT */
463492
return true;
464493
}
465494

@@ -532,12 +561,16 @@ size_t UpdateClass::writeStream(Stream &data) {
532561
return 0;
533562
}
534563

564+
#ifdef UPDATE_CRYPT
535565
if (_command == U_FLASH && !_cryptMode) {
566+
#endif /* UPDATE_CRYPT */
536567
if (!_verifyHeader(data.peek())) {
537568
_reset();
538569
return 0;
539570
}
571+
#ifdef UPDATE_CRYPT
540572
}
573+
#endif /* UPDATE_CRYPT */
541574

542575
if (_ledPin != -1) {
543576
pinMode(_ledPin, OUTPUT);

0 commit comments

Comments
 (0)