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
1416static  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
6973UpdateClass::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
7386UpdateClass &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
178194bool  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
220237void  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
229247void  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
342361bool  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