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