9
9
#include " spi_flash_mmap.h"
10
10
#include " esp_ota_ops.h"
11
11
#include " esp_image_format.h"
12
+ #ifdef UPDATE_CRYPT
12
13
#include " mbedtls/aes.h"
14
+ #endif /* UPDATE_CRYPT */
13
15
14
16
static const char *_err2str (uint8_t _error) {
15
17
if (_error == UPDATE_ERROR_OK) {
@@ -38,8 +40,10 @@ static const char *_err2str(uint8_t _error) {
38
40
return (" Bad Argument" );
39
41
} else if (_error == UPDATE_ERROR_ABORT) {
40
42
return (" Aborted" );
43
+ #ifdef UPDATE_CRYPT
41
44
} else if (_error == UPDATE_ERROR_DECRYPT) {
42
45
return (" Decryption error" );
46
+ #endif /* UPDATE_CRYPT */
43
47
}
44
48
return (" UNKNOWN" );
45
49
}
@@ -67,8 +71,15 @@ bool UpdateClass::_enablePartition(const esp_partition_t *partition) {
67
71
}
68
72
69
73
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
+ {}
72
83
73
84
UpdateClass &UpdateClass::onProgress (THandlerFunction_Progress fn) {
74
85
_progress_callback = fn;
@@ -83,7 +94,9 @@ void UpdateClass::_reset() {
83
94
delete[] _skipBuffer;
84
95
}
85
96
97
+ #ifdef UPDATE_CRYPT
86
98
_cryptBuffer = nullptr ;
99
+ #endif /* UPDATE_CRYPT */
87
100
_buffer = nullptr ;
88
101
_skipBuffer = nullptr ;
89
102
_bufferLen = 0 ;
@@ -175,6 +188,7 @@ bool UpdateClass::begin(size_t size, int command, int ledPin, uint8_t ledOn, con
175
188
return true ;
176
189
}
177
190
191
+ #ifdef UPDATE_CRYPT
178
192
bool UpdateClass::setupCrypt (const uint8_t *cryptKey, size_t cryptAddress, uint8_t cryptConfig, int cryptMode) {
179
193
if (setCryptKey (cryptKey)) {
180
194
if (setCryptMode (cryptMode)) {
@@ -216,6 +230,7 @@ bool UpdateClass::setCryptMode(const int cryptMode) {
216
230
}
217
231
return true ;
218
232
}
233
+ #endif /* UPDATE_CRYPT */
219
234
220
235
void UpdateClass::_abort (uint8_t err) {
221
236
_reset ();
@@ -226,6 +241,7 @@ void UpdateClass::abort() {
226
241
_abort (UPDATE_ERROR_ABORT);
227
242
}
228
243
244
+ #ifdef UPDATE_CRYPT
229
245
void UpdateClass::_cryptKeyTweak (size_t cryptAddress, uint8_t *tweaked_key) {
230
246
memcpy (tweaked_key, _cryptKey, ENCRYPTED_KEY_SIZE);
231
247
if (_cryptCfg == 0 ) {
@@ -338,8 +354,10 @@ bool UpdateClass::_decryptBuffer() {
338
354
}
339
355
return true ;
340
356
}
357
+ #endif /* UPDATE_CRYPT */
341
358
342
359
bool UpdateClass::_writeBuffer () {
360
+ #ifdef UPDATE_CRYPT
343
361
// first bytes of loading image, check to see if loading image needs decrypting
344
362
if (!_progress) {
345
363
_cryptMode &= U_AES_DECRYPT_MODE_MASK;
@@ -360,6 +378,7 @@ bool UpdateClass::_writeBuffer() {
360
378
return false ;
361
379
}
362
380
}
381
+ #endif /* UPDATE_CRYPT */
363
382
// first bytes of new firmware
364
383
uint8_t skip = 0 ;
365
384
if (!_progress && _command == U_FLASH) {
@@ -409,9 +428,13 @@ bool UpdateClass::_writeBuffer() {
409
428
if (!_progress && _command == U_FLASH) {
410
429
_buffer[0 ] = ESP_IMAGE_HEADER_MAGIC;
411
430
}
431
+ #ifdef UPDATE_CRYPT
412
432
if (_target_md5_decrypted) {
433
+ #endif /* UPDATE_CRYPT */
413
434
_md5.add (_buffer, _bufferLen);
435
+ #ifdef UPDATE_CRYPT
414
436
}
437
+ #endif /* UPDATE_CRYPT */
415
438
_progress += _bufferLen;
416
439
_bufferLen = 0 ;
417
440
if (_progress_callback) {
@@ -453,13 +476,19 @@ bool UpdateClass::_verifyEnd() {
453
476
return false ;
454
477
}
455
478
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
+ ) {
457
484
if (strlen (expected_md5) != 32 ) {
458
485
return false ;
459
486
}
460
487
_target_md5 = expected_md5;
461
488
_target_md5.toLowerCase ();
489
+ #ifdef UPDATE_CRYPT
462
490
_target_md5_decrypted = calc_post_decryption;
491
+ #endif /* UPDATE_CRYPT */
463
492
return true ;
464
493
}
465
494
@@ -532,12 +561,16 @@ size_t UpdateClass::writeStream(Stream &data) {
532
561
return 0 ;
533
562
}
534
563
564
+ #ifdef UPDATE_CRYPT
535
565
if (_command == U_FLASH && !_cryptMode) {
566
+ #endif /* UPDATE_CRYPT */
536
567
if (!_verifyHeader (data.peek ())) {
537
568
_reset ();
538
569
return 0 ;
539
570
}
571
+ #ifdef UPDATE_CRYPT
540
572
}
573
+ #endif /* UPDATE_CRYPT */
541
574
542
575
if (_ledPin != -1 ) {
543
576
pinMode (_ledPin, OUTPUT);
0 commit comments