3
3
#include " spi_flash_mmap.h"
4
4
#include " esp_ota_ops.h"
5
5
#include " esp_image_format.h"
6
- #include " mbedtls/aes.h"
7
6
8
7
static const char * _err2str (uint8_t _error){
9
8
if (_error == UPDATE_ERROR_OK){
@@ -32,8 +31,6 @@ static const char * _err2str(uint8_t _error){
32
31
return (" Bad Argument" );
33
32
} else if (_error == UPDATE_ERROR_ABORT){
34
33
return (" Aborted" );
35
- } else if (_error == UPDATE_ERROR_DECRYPT){
36
- return (" Decryption error" );
37
34
}
38
35
return (" UNKNOWN" );
39
36
}
@@ -62,20 +59,14 @@ bool UpdateClass::_enablePartition(const esp_partition_t* partition){
62
59
63
60
UpdateClass::UpdateClass ()
64
61
: _error(0 )
65
- , _cryptKey(0 )
66
- , _cryptBuffer(0 )
67
62
, _buffer(0 )
68
- , _skipBuffer(0 )
69
63
, _bufferLen(0 )
70
64
, _size(0 )
71
65
, _progress_callback(NULL )
72
66
, _progress(0 )
73
67
, _paroffset(0 )
74
68
, _command(U_FLASH)
75
69
, _partition(NULL )
76
- , _cryptMode(U_AES_DECRYPT_AUTO)
77
- , _cryptAddress(0 )
78
- , _cryptCfg(0xf )
79
70
{
80
71
}
81
72
@@ -92,7 +83,6 @@ void UpdateClass::_reset() {
92
83
delete[] _skipBuffer;
93
84
}
94
85
95
- _cryptBuffer = nullptr ;
96
86
_buffer = nullptr ;
97
87
_skipBuffer = nullptr ;
98
88
_bufferLen = 0 ;
@@ -186,48 +176,6 @@ bool UpdateClass::begin(size_t size, int command, int ledPin, uint8_t ledOn, con
186
176
return true ;
187
177
}
188
178
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
-
231
179
void UpdateClass::_abort (uint8_t err){
232
180
_reset ();
233
181
_error = err;
@@ -237,129 +185,7 @@ void UpdateClass::abort(){
237
185
_abort (UPDATE_ERROR_ABORT);
238
186
}
239
187
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
-
345
188
bool 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
- }
363
189
// first bytes of new firmware
364
190
uint8_t skip = 0 ;
365
191
if (!_progress && _command == U_FLASH){
0 commit comments