33#include " spi_flash_mmap.h"
44#include " esp_ota_ops.h"
55#include " esp_image_format.h"
6- #include " mbedtls/aes.h"
76
87static const char * _err2str (uint8_t _error){
98 if (_error == UPDATE_ERROR_OK){
@@ -32,8 +31,6 @@ static const char * _err2str(uint8_t _error){
3231 return (" Bad Argument" );
3332 } else if (_error == UPDATE_ERROR_ABORT){
3433 return (" Aborted" );
35- } else if (_error == UPDATE_ERROR_DECRYPT){
36- return (" Decryption error" );
3734 }
3835 return (" UNKNOWN" );
3936}
@@ -62,20 +59,14 @@ bool UpdateClass::_enablePartition(const esp_partition_t* partition){
6259
6360UpdateClass::UpdateClass ()
6461: _error(0 )
65- , _cryptKey(0 )
66- , _cryptBuffer(0 )
6762, _buffer(0 )
68- , _skipBuffer(0 )
6963, _bufferLen(0 )
7064, _size(0 )
7165, _progress_callback(NULL )
7266, _progress(0 )
7367, _paroffset(0 )
7468, _command(U_FLASH)
7569, _partition(NULL )
76- , _cryptMode(U_AES_DECRYPT_AUTO)
77- , _cryptAddress(0 )
78- , _cryptCfg(0xf )
7970{
8071}
8172
@@ -92,7 +83,6 @@ void UpdateClass::_reset() {
9283 delete[] _skipBuffer;
9384 }
9485
95- _cryptBuffer = nullptr ;
9686 _buffer = nullptr ;
9787 _skipBuffer = nullptr ;
9888 _bufferLen = 0 ;
@@ -186,48 +176,6 @@ bool UpdateClass::begin(size_t size, int command, int ledPin, uint8_t ledOn, con
186176 return true ;
187177}
188178
189- bool UpdateClass::setupCrypt (const uint8_t *cryptKey, size_t cryptAddress, uint8_t cryptConfig, int cryptMode){
190- if (setCryptKey (cryptKey)){
191- if (setCryptMode (cryptMode)){
192- setCryptAddress (cryptAddress);
193- setCryptConfig (cryptConfig);
194- return true ;
195- }
196- }
197- return false ;
198- }
199-
200- bool UpdateClass::setCryptKey (const uint8_t *cryptKey){
201- if (!cryptKey){
202- if (_cryptKey){
203- delete[] _cryptKey;
204- _cryptKey = 0 ;
205- log_d (" AES key unset" );
206- }
207- return false ; // key cleared, no key to decrypt with
208- }
209- // initialize
210- if (!_cryptKey){
211- _cryptKey = new (std::nothrow) uint8_t [ENCRYPTED_KEY_SIZE];
212- }
213- if (!_cryptKey){
214- log_e (" new failed" );
215- return false ;
216- }
217- memcpy (_cryptKey, cryptKey, ENCRYPTED_KEY_SIZE);
218- return true ;
219- }
220-
221- bool UpdateClass::setCryptMode (const int cryptMode){
222- if (cryptMode >= U_AES_DECRYPT_NONE && cryptMode <= U_AES_DECRYPT_ON){
223- _cryptMode = cryptMode;
224- }else {
225- log_e (" bad crypt mode arguement %i" , cryptMode);
226- return false ;
227- }
228- return true ;
229- }
230-
231179void UpdateClass::_abort (uint8_t err){
232180 _reset ();
233181 _error = err;
@@ -237,129 +185,7 @@ void UpdateClass::abort(){
237185 _abort (UPDATE_ERROR_ABORT);
238186}
239187
240- void UpdateClass::_cryptKeyTweak (size_t cryptAddress, uint8_t *tweaked_key){
241- memcpy (tweaked_key, _cryptKey, ENCRYPTED_KEY_SIZE );
242- if (_cryptCfg == 0 ) return ; // no tweaking needed, use crypt key as-is
243-
244- const uint8_t pattern[] = { 23 , 23 , 23 , 14 , 23 , 23 , 23 , 12 , 23 , 23 , 23 , 10 , 23 , 23 , 23 , 8 };
245- int pattern_idx = 0 ;
246- int key_idx = 0 ;
247- int bit_len = 0 ;
248- uint32_t tweak = 0 ;
249- cryptAddress &= 0x00ffffe0 ; // bit 23-5
250- cryptAddress <<= 8 ; // bit23 shifted to bit31(MSB)
251- while (pattern_idx < sizeof (pattern)){
252- tweak = cryptAddress<<(23 - pattern[pattern_idx]); // bit shift for small patterns
253- // alternative to: tweak = rotl32(tweak,8 - bit_len);
254- tweak = (tweak<<(8 - bit_len)) | (tweak>>(24 + bit_len)); // rotate to line up with end of previous tweak bits
255- bit_len += pattern[pattern_idx++] - 4 ; // add number of bits in next pattern(23-4 = 19bits = 23bit to 5bit)
256- while (bit_len > 7 ){
257- tweaked_key[key_idx++] ^= tweak; // XOR byte
258- // alternative to: tweak = rotl32(tweak, 8);
259- tweak = (tweak<<8 ) | (tweak>>24 ); // compiler should optimize to use rotate(fast)
260- bit_len -=8 ;
261- }
262- tweaked_key[key_idx] ^= tweak; // XOR remaining bits, will XOR zeros if no remaining bits
263- }
264- if (_cryptCfg == 0xf ) return ; // return with fully tweaked key
265-
266- // some of tweaked key bits need to be restore back to crypt key bits
267- const uint8_t cfg_bits[] = { 67 , 65 , 63 , 61 };
268- key_idx = 0 ;
269- pattern_idx = 0 ;
270- while (key_idx < ENCRYPTED_KEY_SIZE){
271- bit_len += cfg_bits[pattern_idx];
272- if ( (_cryptCfg & (1 <<pattern_idx)) == 0 ){ // restore crypt key bits
273- while (bit_len > 0 ){
274- if ( bit_len > 7 || ((_cryptCfg & (2 <<pattern_idx)) == 0 ) ){ // restore a crypt key byte
275- tweaked_key[key_idx] = _cryptKey[key_idx];
276- }else { // MSBits restore crypt key bits, LSBits keep as tweaked bits
277- tweaked_key[key_idx] &= (0xff >>bit_len);
278- tweaked_key[key_idx] |= (_cryptKey[key_idx] & (~(0xff >>bit_len)) );
279- }
280- key_idx++;
281- bit_len -= 8 ;
282- }
283- }else { // keep tweaked key bits
284- while (bit_len > 0 ){
285- if ( bit_len <8 && ((_cryptCfg & (2 <<pattern_idx)) == 0 ) ){ // MSBits keep as tweaked bits, LSBits restore crypt key bits
286- tweaked_key[key_idx] &= (~(0xff >>bit_len));
287- tweaked_key[key_idx] |= (_cryptKey[key_idx] & (0xff >>bit_len));
288- }
289- key_idx++;
290- bit_len -= 8 ;
291- }
292- }
293- pattern_idx++;
294- }
295- }
296-
297- bool UpdateClass::_decryptBuffer (){
298- if (!_cryptKey){
299- log_w (" AES key not set" );
300- return false ;
301- }
302- if (_bufferLen%ENCRYPTED_BLOCK_SIZE !=0 ){
303- log_e (" buffer size error" );
304- return false ;
305- }
306- if (!_cryptBuffer){
307- _cryptBuffer = new (std::nothrow) uint8_t [ENCRYPTED_BLOCK_SIZE];
308- }
309- if (!_cryptBuffer){
310- log_e (" new failed" );
311- return false ;
312- }
313- uint8_t tweaked_key[ENCRYPTED_KEY_SIZE]; // tweaked crypt key
314- int done = 0 ;
315-
316- /*
317- Mbedtls functions will be replaced with esp_aes functions when hardware acceleration is available
318-
319- To Do:
320- Replace mbedtls for the cases where there's no hardware acceleration
321- */
322-
323- mbedtls_aes_context ctx; // initialize AES
324- mbedtls_aes_init ( &ctx );
325- while ((_bufferLen - done) >= ENCRYPTED_BLOCK_SIZE){
326- for (int i=0 ; i < ENCRYPTED_BLOCK_SIZE; i++) _cryptBuffer[(ENCRYPTED_BLOCK_SIZE - 1 ) - i] = _buffer[i + done]; // reverse order 16 bytes to decrypt
327- if ( ((_cryptAddress + _progress + done) % ENCRYPTED_TWEAK_BLOCK_SIZE) == 0 || done == 0 ){
328- _cryptKeyTweak (_cryptAddress + _progress + done, tweaked_key); // update tweaked crypt key
329- if ( mbedtls_aes_setkey_enc ( &ctx, tweaked_key, 256 ) ){
330- return false ;
331- }
332- if ( mbedtls_aes_setkey_dec ( &ctx, tweaked_key, 256 ) ){
333- return false ;
334- }
335- }
336- if ( mbedtls_aes_crypt_ecb ( &ctx, MBEDTLS_AES_ENCRYPT, _cryptBuffer, _cryptBuffer ) ){ // use MBEDTLS_AES_ENCRYPT to decrypt flash code
337- return false ;
338- }
339- for (int i=0 ; i < ENCRYPTED_BLOCK_SIZE; i++) _buffer[i + done] = _cryptBuffer[(ENCRYPTED_BLOCK_SIZE - 1 ) - i]; // reverse order 16 bytes from decrypt
340- done += ENCRYPTED_BLOCK_SIZE;
341- }
342- return true ;
343- }
344-
345188bool UpdateClass::_writeBuffer (){
346- // first bytes of loading image, check to see if loading image needs decrypting
347- if (!_progress){
348- _cryptMode &= U_AES_DECRYPT_MODE_MASK;
349- if ( ( _cryptMode == U_AES_DECRYPT_ON )
350- || ((_command == U_FLASH) && (_cryptMode & U_AES_DECRYPT_AUTO) && (_buffer[0 ] != ESP_IMAGE_HEADER_MAGIC))
351- ){
352- _cryptMode |= U_AES_IMAGE_DECRYPTING_BIT; // set to decrypt the loading image
353- log_d (" Decrypting OTA Image" );
354- }
355- }
356- // check if data in buffer needs decrypting
357- if ( _cryptMode & U_AES_IMAGE_DECRYPTING_BIT ){
358- if ( !_decryptBuffer () ){
359- _abort (UPDATE_ERROR_DECRYPT);
360- return false ;
361- }
362- }
363189 // first bytes of new firmware
364190 uint8_t skip = 0 ;
365191 if (!_progress && _command == U_FLASH){
0 commit comments