Skip to content

Commit eb1933f

Browse files
authored
Guard crypto parts in "Update" to save resources (#10630)
* guard crypt update * guard update crypt * Update Updater.cpp * revert logic to disable * change disable logic * formatting * formatting * remove trailing space
1 parent 1730e4e commit eb1933f

File tree

2 files changed

+59
-4
lines changed

2 files changed

+59
-4
lines changed

Diff for: libraries/Update/src/Update.h

+19-1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ class UpdateClass {
6363
*/
6464
bool begin(size_t size = UPDATE_SIZE_UNKNOWN, int command = U_FLASH, int ledPin = -1, uint8_t ledOn = LOW, const char *label = NULL);
6565

66+
#ifndef UPDATE_NOCRYPT
6667
/*
6768
Setup decryption configuration
6869
Crypt Key is 32bytes(256bits) block of data, use the same key as used to encrypt image file
@@ -71,6 +72,7 @@ class UpdateClass {
7172
Crypt Mode, used to select if image files should be decrypted or not
7273
*/
7374
bool setupCrypt(const uint8_t *cryptKey = 0, size_t cryptAddress = 0, uint8_t cryptConfig = 0xf, int cryptMode = U_AES_DECRYPT_AUTO);
75+
#endif /* UPDATE_NOCRYPT */
7476

7577
/*
7678
Writes a buffer to the flash and increments the address
@@ -99,6 +101,7 @@ class UpdateClass {
99101
*/
100102
bool end(bool evenIfRemaining = false);
101103

104+
#ifndef UPDATE_NOCRYPT
102105
/*
103106
sets AES256 key(32 bytes) used for decrypting image file
104107
*/
@@ -122,6 +125,7 @@ class UpdateClass {
122125
void setCryptConfig(const uint8_t cryptConfig) {
123126
_cryptCfg = cryptConfig & 0x0f;
124127
}
128+
#endif /* UPDATE_NOCRYPT */
125129

126130
/*
127131
Aborts the running update
@@ -139,7 +143,13 @@ class UpdateClass {
139143
sets the expected MD5 for the firmware (hexString)
140144
If calc_post_decryption is true, the update library will calculate the MD5 after the decryption, if false the calculation occurs before the decryption
141145
*/
142-
bool setMD5(const char *expected_md5, bool calc_post_decryption = true);
146+
bool setMD5(
147+
const char *expected_md5
148+
#ifndef UPDATE_NOCRYPT
149+
,
150+
bool calc_post_decryption = true
151+
#endif /* #ifdef UPDATE_NOCRYPT */
152+
);
143153

144154
/*
145155
returns the MD5 String of the successfully ended firmware
@@ -236,17 +246,21 @@ class UpdateClass {
236246
private:
237247
void _reset();
238248
void _abort(uint8_t err);
249+
#ifndef UPDATE_NOCRYPT
239250
void _cryptKeyTweak(size_t cryptAddress, uint8_t *tweaked_key);
240251
bool _decryptBuffer();
252+
#endif /* UPDATE_NOCRYPT */
241253
bool _writeBuffer();
242254
bool _verifyHeader(uint8_t data);
243255
bool _verifyEnd();
244256
bool _enablePartition(const esp_partition_t *partition);
245257
bool _chkDataInBlock(const uint8_t *data, size_t len) const; // check if block contains any data or is empty
246258

247259
uint8_t _error;
260+
#ifndef UPDATE_NOCRYPT
248261
uint8_t *_cryptKey;
249262
uint8_t *_cryptBuffer;
263+
#endif /* UPDATE_NOCRYPT */
250264
uint8_t *_buffer;
251265
uint8_t *_skipBuffer;
252266
size_t _bufferLen;
@@ -258,15 +272,19 @@ class UpdateClass {
258272
const esp_partition_t *_partition;
259273

260274
String _target_md5;
275+
#ifndef UPDATE_NOCRYPT
261276
bool _target_md5_decrypted = true;
277+
#endif /* UPDATE_NOCRYPT */
262278
MD5Builder _md5;
263279

264280
int _ledPin;
265281
uint8_t _ledOn;
266282

283+
#ifndef UPDATE_NOCRYPT
267284
uint8_t _cryptMode;
268285
size_t _cryptAddress;
269286
uint8_t _cryptCfg;
287+
#endif /* UPDATE_NOCRYPT */
270288
};
271289

272290
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_UPDATE)

Diff for: libraries/Update/src/Updater.cpp

+40-3
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+
#ifndef UPDATE_NOCRYPT
1213
#include "mbedtls/aes.h"
14+
#endif /* UPDATE_NOCRYPT */
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+
#ifndef UPDATE_NOCRYPT
4144
} else if (_error == UPDATE_ERROR_DECRYPT) {
4245
return ("Decryption error");
46+
#endif /* UPDATE_NOCRYPT */
4347
}
4448
return ("UNKNOWN");
4549
}
@@ -67,8 +71,17 @@ 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+
#ifndef UPDATE_NOCRYPT
76+
_cryptKey(0), _cryptBuffer(0),
77+
#endif /* UPDATE_NOCRYPT */
78+
_buffer(0), _skipBuffer(0), _bufferLen(0), _size(0), _progress_callback(NULL), _progress(0), _paroffset(0), _command(U_FLASH), _partition(NULL)
79+
#ifndef UPDATE_NOCRYPT
80+
,
81+
_cryptMode(U_AES_DECRYPT_AUTO), _cryptAddress(0), _cryptCfg(0xf)
82+
#endif /* UPDATE_NOCRYPT */
83+
{
84+
}
7285

7386
UpdateClass &UpdateClass::onProgress(THandlerFunction_Progress fn) {
7487
_progress_callback = fn;
@@ -83,7 +96,9 @@ void UpdateClass::_reset() {
8396
delete[] _skipBuffer;
8497
}
8598

99+
#ifndef UPDATE_NOCRYPT
86100
_cryptBuffer = nullptr;
101+
#endif /* UPDATE_NOCRYPT */
87102
_buffer = nullptr;
88103
_skipBuffer = nullptr;
89104
_bufferLen = 0;
@@ -175,6 +190,7 @@ bool UpdateClass::begin(size_t size, int command, int ledPin, uint8_t ledOn, con
175190
return true;
176191
}
177192

193+
#ifndef UPDATE_NOCRYPT
178194
bool UpdateClass::setupCrypt(const uint8_t *cryptKey, size_t cryptAddress, uint8_t cryptConfig, int cryptMode) {
179195
if (setCryptKey(cryptKey)) {
180196
if (setCryptMode(cryptMode)) {
@@ -216,6 +232,7 @@ bool UpdateClass::setCryptMode(const int cryptMode) {
216232
}
217233
return true;
218234
}
235+
#endif /* UPDATE_NOCRYPT */
219236

220237
void UpdateClass::_abort(uint8_t err) {
221238
_reset();
@@ -226,6 +243,7 @@ void UpdateClass::abort() {
226243
_abort(UPDATE_ERROR_ABORT);
227244
}
228245

246+
#ifndef UPDATE_NOCRYPT
229247
void UpdateClass::_cryptKeyTweak(size_t cryptAddress, uint8_t *tweaked_key) {
230248
memcpy(tweaked_key, _cryptKey, ENCRYPTED_KEY_SIZE);
231249
if (_cryptCfg == 0) {
@@ -338,8 +356,10 @@ bool UpdateClass::_decryptBuffer() {
338356
}
339357
return true;
340358
}
359+
#endif /* UPDATE_NOCRYPT */
341360

342361
bool UpdateClass::_writeBuffer() {
362+
#ifndef UPDATE_NOCRYPT
343363
//first bytes of loading image, check to see if loading image needs decrypting
344364
if (!_progress) {
345365
_cryptMode &= U_AES_DECRYPT_MODE_MASK;
@@ -360,6 +380,7 @@ bool UpdateClass::_writeBuffer() {
360380
return false;
361381
}
362382
}
383+
#endif /* UPDATE_NOCRYPT */
363384
//first bytes of new firmware
364385
uint8_t skip = 0;
365386
if (!_progress && _command == U_FLASH) {
@@ -409,9 +430,13 @@ bool UpdateClass::_writeBuffer() {
409430
if (!_progress && _command == U_FLASH) {
410431
_buffer[0] = ESP_IMAGE_HEADER_MAGIC;
411432
}
433+
#ifndef UPDATE_NOCRYPT
412434
if (_target_md5_decrypted) {
435+
#endif /* UPDATE_NOCRYPT */
413436
_md5.add(_buffer, _bufferLen);
437+
#ifndef UPDATE_NOCRYPT
414438
}
439+
#endif /* UPDATE_NOCRYPT */
415440
_progress += _bufferLen;
416441
_bufferLen = 0;
417442
if (_progress_callback) {
@@ -453,13 +478,21 @@ bool UpdateClass::_verifyEnd() {
453478
return false;
454479
}
455480

456-
bool UpdateClass::setMD5(const char *expected_md5, bool calc_post_decryption) {
481+
bool UpdateClass::setMD5(
482+
const char *expected_md5
483+
#ifndef UPDATE_NOCRYPT
484+
,
485+
bool calc_post_decryption
486+
#endif /* UPDATE_NOCRYPT */
487+
) {
457488
if (strlen(expected_md5) != 32) {
458489
return false;
459490
}
460491
_target_md5 = expected_md5;
461492
_target_md5.toLowerCase();
493+
#ifndef UPDATE_NOCRYPT
462494
_target_md5_decrypted = calc_post_decryption;
495+
#endif /* UPDATE_NOCRYPT */
463496
return true;
464497
}
465498

@@ -532,12 +565,16 @@ size_t UpdateClass::writeStream(Stream &data) {
532565
return 0;
533566
}
534567

568+
#ifndef UPDATE_NOCRYPT
535569
if (_command == U_FLASH && !_cryptMode) {
570+
#endif /* UPDATE_NOCRYPT */
536571
if (!_verifyHeader(data.peek())) {
537572
_reset();
538573
return 0;
539574
}
575+
#ifndef UPDATE_NOCRYPT
540576
}
577+
#endif /* UPDATE_NOCRYPT */
541578

542579
if (_ledPin != -1) {
543580
pinMode(_ledPin, OUTPUT);

0 commit comments

Comments
 (0)