9
9
#include " spi_flash_mmap.h"
10
10
#include " esp_ota_ops.h"
11
11
#include " esp_image_format.h"
12
- #include " mbedtls/aes.h"
13
12
14
13
static const char *_err2str (uint8_t _error) {
15
14
if (_error == UPDATE_ERROR_OK) {
@@ -38,8 +37,6 @@ static const char *_err2str(uint8_t _error) {
38
37
return (" Bad Argument" );
39
38
} else if (_error == UPDATE_ERROR_ABORT) {
40
39
return (" Aborted" );
41
- } else if (_error == UPDATE_ERROR_DECRYPT) {
42
- return (" Decryption error" );
43
40
}
44
41
return (" UNKNOWN" );
45
42
}
@@ -67,8 +64,7 @@ bool UpdateClass::_enablePartition(const esp_partition_t *partition) {
67
64
}
68
65
69
66
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 ) {}
67
+ : _error(0 ), _buffer(0 ), _bufferLen(0 ), _size(0 ), _progress_callback(NULL ), _progress(0 ), _paroffset(0 ), _command(U_FLASH), _partition(NULL ) {}
72
68
73
69
UpdateClass &UpdateClass::onProgress (THandlerFunction_Progress fn) {
74
70
_progress_callback = fn;
@@ -83,7 +79,6 @@ void UpdateClass::_reset() {
83
79
delete[] _skipBuffer;
84
80
}
85
81
86
- _cryptBuffer = nullptr ;
87
82
_buffer = nullptr ;
88
83
_skipBuffer = nullptr ;
89
84
_bufferLen = 0 ;
@@ -175,48 +170,6 @@ bool UpdateClass::begin(size_t size, int command, int ledPin, uint8_t ledOn, con
175
170
return true ;
176
171
}
177
172
178
- bool UpdateClass::setupCrypt (const uint8_t *cryptKey, size_t cryptAddress, uint8_t cryptConfig, int cryptMode) {
179
- if (setCryptKey (cryptKey)) {
180
- if (setCryptMode (cryptMode)) {
181
- setCryptAddress (cryptAddress);
182
- setCryptConfig (cryptConfig);
183
- return true ;
184
- }
185
- }
186
- return false ;
187
- }
188
-
189
- bool UpdateClass::setCryptKey (const uint8_t *cryptKey) {
190
- if (!cryptKey) {
191
- if (_cryptKey) {
192
- delete[] _cryptKey;
193
- _cryptKey = 0 ;
194
- log_d (" AES key unset" );
195
- }
196
- return false ; // key cleared, no key to decrypt with
197
- }
198
- // initialize
199
- if (!_cryptKey) {
200
- _cryptKey = new (std::nothrow) uint8_t [ENCRYPTED_KEY_SIZE];
201
- }
202
- if (!_cryptKey) {
203
- log_e (" new failed" );
204
- return false ;
205
- }
206
- memcpy (_cryptKey, cryptKey, ENCRYPTED_KEY_SIZE);
207
- return true ;
208
- }
209
-
210
- bool UpdateClass::setCryptMode (const int cryptMode) {
211
- if (cryptMode >= U_AES_DECRYPT_NONE && cryptMode <= U_AES_DECRYPT_ON) {
212
- _cryptMode = cryptMode;
213
- } else {
214
- log_e (" bad crypt mode argument %i" , cryptMode);
215
- return false ;
216
- }
217
- return true ;
218
- }
219
-
220
173
void UpdateClass::_abort (uint8_t err) {
221
174
_reset ();
222
175
_error = err;
@@ -226,140 +179,7 @@ void UpdateClass::abort() {
226
179
_abort (UPDATE_ERROR_ABORT);
227
180
}
228
181
229
- void UpdateClass::_cryptKeyTweak (size_t cryptAddress, uint8_t *tweaked_key) {
230
- memcpy (tweaked_key, _cryptKey, ENCRYPTED_KEY_SIZE);
231
- if (_cryptCfg == 0 ) {
232
- return ; // no tweaking needed, use crypt key as-is
233
- }
234
-
235
- const uint8_t pattern[] = {23 , 23 , 23 , 14 , 23 , 23 , 23 , 12 , 23 , 23 , 23 , 10 , 23 , 23 , 23 , 8 };
236
- int pattern_idx = 0 ;
237
- int key_idx = 0 ;
238
- int bit_len = 0 ;
239
- uint32_t tweak = 0 ;
240
- cryptAddress &= 0x00ffffe0 ; // bit 23-5
241
- cryptAddress <<= 8 ; // bit23 shifted to bit31(MSB)
242
- while (pattern_idx < sizeof (pattern)) {
243
- tweak = cryptAddress << (23 - pattern[pattern_idx]); // bit shift for small patterns
244
- // alternative to: tweak = rotl32(tweak,8 - bit_len);
245
- tweak = (tweak << (8 - bit_len)) | (tweak >> (24 + bit_len)); // rotate to line up with end of previous tweak bits
246
- bit_len += pattern[pattern_idx++] - 4 ; // add number of bits in next pattern(23-4 = 19bits = 23bit to 5bit)
247
- while (bit_len > 7 ) {
248
- tweaked_key[key_idx++] ^= tweak; // XOR byte
249
- // alternative to: tweak = rotl32(tweak, 8);
250
- tweak = (tweak << 8 ) | (tweak >> 24 ); // compiler should optimize to use rotate(fast)
251
- bit_len -= 8 ;
252
- }
253
- tweaked_key[key_idx] ^= tweak; // XOR remaining bits, will XOR zeros if no remaining bits
254
- }
255
- if (_cryptCfg == 0xf ) {
256
- return ; // return with fully tweaked key
257
- }
258
-
259
- // some of tweaked key bits need to be restore back to crypt key bits
260
- const uint8_t cfg_bits[] = {67 , 65 , 63 , 61 };
261
- key_idx = 0 ;
262
- pattern_idx = 0 ;
263
- while (key_idx < ENCRYPTED_KEY_SIZE) {
264
- bit_len += cfg_bits[pattern_idx];
265
- if ((_cryptCfg & (1 << pattern_idx)) == 0 ) { // restore crypt key bits
266
- while (bit_len > 0 ) {
267
- if (bit_len > 7 || ((_cryptCfg & (2 << pattern_idx)) == 0 )) { // restore a crypt key byte
268
- tweaked_key[key_idx] = _cryptKey[key_idx];
269
- } else { // MSBits restore crypt key bits, LSBits keep as tweaked bits
270
- tweaked_key[key_idx] &= (0xff >> bit_len);
271
- tweaked_key[key_idx] |= (_cryptKey[key_idx] & (~(0xff >> bit_len)));
272
- }
273
- key_idx++;
274
- bit_len -= 8 ;
275
- }
276
- } else { // keep tweaked key bits
277
- while (bit_len > 0 ) {
278
- if (bit_len < 8 && ((_cryptCfg & (2 << pattern_idx)) == 0 )) { // MSBits keep as tweaked bits, LSBits restore crypt key bits
279
- tweaked_key[key_idx] &= (~(0xff >> bit_len));
280
- tweaked_key[key_idx] |= (_cryptKey[key_idx] & (0xff >> bit_len));
281
- }
282
- key_idx++;
283
- bit_len -= 8 ;
284
- }
285
- }
286
- pattern_idx++;
287
- }
288
- }
289
-
290
- bool UpdateClass::_decryptBuffer () {
291
- if (!_cryptKey) {
292
- log_w (" AES key not set" );
293
- return false ;
294
- }
295
- if (_bufferLen % ENCRYPTED_BLOCK_SIZE != 0 ) {
296
- log_e (" buffer size error" );
297
- return false ;
298
- }
299
- if (!_cryptBuffer) {
300
- _cryptBuffer = new (std::nothrow) uint8_t [ENCRYPTED_BLOCK_SIZE];
301
- }
302
- if (!_cryptBuffer) {
303
- log_e (" new failed" );
304
- return false ;
305
- }
306
- uint8_t tweaked_key[ENCRYPTED_KEY_SIZE]; // tweaked crypt key
307
- int done = 0 ;
308
-
309
- /*
310
- Mbedtls functions will be replaced with esp_aes functions when hardware acceleration is available
311
-
312
- To Do:
313
- Replace mbedtls for the cases where there's no hardware acceleration
314
- */
315
-
316
- mbedtls_aes_context ctx; // initialize AES
317
- mbedtls_aes_init (&ctx);
318
- while ((_bufferLen - done) >= ENCRYPTED_BLOCK_SIZE) {
319
- for (int i = 0 ; i < ENCRYPTED_BLOCK_SIZE; i++) {
320
- _cryptBuffer[(ENCRYPTED_BLOCK_SIZE - 1 ) - i] = _buffer[i + done]; // reverse order 16 bytes to decrypt
321
- }
322
- if (((_cryptAddress + _progress + done) % ENCRYPTED_TWEAK_BLOCK_SIZE) == 0 || done == 0 ) {
323
- _cryptKeyTweak (_cryptAddress + _progress + done, tweaked_key); // update tweaked crypt key
324
- if (mbedtls_aes_setkey_enc (&ctx, tweaked_key, 256 )) {
325
- return false ;
326
- }
327
- if (mbedtls_aes_setkey_dec (&ctx, tweaked_key, 256 )) {
328
- return false ;
329
- }
330
- }
331
- if (mbedtls_aes_crypt_ecb (&ctx, MBEDTLS_AES_ENCRYPT, _cryptBuffer, _cryptBuffer)) { // use MBEDTLS_AES_ENCRYPT to decrypt flash code
332
- return false ;
333
- }
334
- for (int i = 0 ; i < ENCRYPTED_BLOCK_SIZE; i++) {
335
- _buffer[i + done] = _cryptBuffer[(ENCRYPTED_BLOCK_SIZE - 1 ) - i]; // reverse order 16 bytes from decrypt
336
- }
337
- done += ENCRYPTED_BLOCK_SIZE;
338
- }
339
- return true ;
340
- }
341
-
342
182
bool UpdateClass::_writeBuffer () {
343
- // first bytes of loading image, check to see if loading image needs decrypting
344
- if (!_progress) {
345
- _cryptMode &= U_AES_DECRYPT_MODE_MASK;
346
- if ((_cryptMode == U_AES_DECRYPT_ON) || ((_command == U_FLASH) && (_cryptMode & U_AES_DECRYPT_AUTO) && (_buffer[0 ] != ESP_IMAGE_HEADER_MAGIC))) {
347
- _cryptMode |= U_AES_IMAGE_DECRYPTING_BIT; // set to decrypt the loading image
348
- log_d (" Decrypting OTA Image" );
349
- }
350
- }
351
-
352
- if (!_target_md5_decrypted) {
353
- _md5.add (_buffer, _bufferLen);
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
183
// first bytes of new firmware
364
184
uint8_t skip = 0 ;
365
185
if (!_progress && _command == U_FLASH) {
@@ -409,9 +229,7 @@ bool UpdateClass::_writeBuffer() {
409
229
if (!_progress && _command == U_FLASH) {
410
230
_buffer[0 ] = ESP_IMAGE_HEADER_MAGIC;
411
231
}
412
- if (_target_md5_decrypted) {
413
- _md5.add (_buffer, _bufferLen);
414
- }
232
+ _md5.add (_buffer, _bufferLen);
415
233
_progress += _bufferLen;
416
234
_bufferLen = 0 ;
417
235
if (_progress_callback) {
@@ -453,13 +271,12 @@ bool UpdateClass::_verifyEnd() {
453
271
return false ;
454
272
}
455
273
456
- bool UpdateClass::setMD5 (const char *expected_md5, bool calc_post_decryption ) {
274
+ bool UpdateClass::setMD5 (const char *expected_md5) {
457
275
if (strlen (expected_md5) != 32 ) {
458
276
return false ;
459
277
}
460
278
_target_md5 = expected_md5;
461
279
_target_md5.toLowerCase ();
462
- _target_md5_decrypted = calc_post_decryption;
463
280
return true ;
464
281
}
465
282
@@ -532,11 +349,9 @@ size_t UpdateClass::writeStream(Stream &data) {
532
349
return 0 ;
533
350
}
534
351
535
- if (_command == U_FLASH && !_cryptMode) {
536
- if (!_verifyHeader (data.peek ())) {
537
- _reset ();
538
- return 0 ;
539
- }
352
+ if (!_verifyHeader (data.peek ())) {
353
+ _reset ();
354
+ return 0 ;
540
355
}
541
356
542
357
if (_ledPin != -1 ) {
0 commit comments