25
25
#define UPDATE_ERROR_NO_PARTITION (10 )
26
26
#define UPDATE_ERROR_BAD_ARGUMENT (11 )
27
27
#define UPDATE_ERROR_ABORT (12 )
28
+ #define UPDATE_ERROR_DECRYPT (13 )
28
29
29
30
#define UPDATE_SIZE_UNKNOWN 0xFFFFFFFF
30
31
31
32
#define U_FLASH 0
32
33
#define U_SPIFFS 100
33
34
#define U_AUTH 200
34
35
35
- #define ENCRYPTED_BLOCK_SIZE 16
36
+ #define ENCRYPTED_BLOCK_SIZE 16
37
+ #define ENCRYPTED_TWEAK_BLOCK_SIZE 32
38
+ #define ENCRYPTED_KEY_SIZE 32
39
+
40
+ #define U_AES_DECRYPT_NONE 0
41
+ #define U_AES_DECRYPT_AUTO 1
42
+ #define U_AES_DECRYPT_ON 2
43
+ #define U_AES_DECRYPT_MODE_MASK 3
44
+ #define U_AES_IMAGE_DECRYPTING_BIT 4
36
45
37
46
#define SPI_SECTORS_PER_BLOCK 16 // usually large erase block is 32k/64k
38
47
#define SPI_FLASH_BLOCK_SIZE (SPI_SECTORS_PER_BLOCK * SPI_FLASH_SEC_SIZE)
@@ -54,6 +63,17 @@ class UpdateClass {
54
63
*/
55
64
bool begin (size_t size = UPDATE_SIZE_UNKNOWN, int command = U_FLASH, int ledPin = -1 , uint8_t ledOn = LOW, const char *label = NULL );
56
65
66
+ #ifndef UPDATE_NOCRYPT
67
+ /*
68
+ Setup decryption configuration
69
+ Crypt Key is 32bytes(256bits) block of data, use the same key as used to encrypt image file
70
+ Crypt Address, use the same value as used to encrypt image file
71
+ Crypt Config, use the same value as used to encrypt image file
72
+ Crypt Mode, used to select if image files should be decrypted or not
73
+ */
74
+ bool setupCrypt (const uint8_t *cryptKey = 0 , size_t cryptAddress = 0 , uint8_t cryptConfig = 0xf , int cryptMode = U_AES_DECRYPT_AUTO);
75
+ #endif /* UPDATE_NOCRYPT */
76
+
57
77
/*
58
78
Writes a buffer to the flash and increments the address
59
79
Returns the amount written
@@ -81,6 +101,32 @@ class UpdateClass {
81
101
*/
82
102
bool end (bool evenIfRemaining = false );
83
103
104
+ #ifndef UPDATE_NOCRYPT
105
+ /*
106
+ sets AES256 key(32 bytes) used for decrypting image file
107
+ */
108
+ bool setCryptKey (const uint8_t *cryptKey);
109
+
110
+ /*
111
+ sets crypt mode used on image files
112
+ */
113
+ bool setCryptMode (const int cryptMode);
114
+
115
+ /*
116
+ sets address used for decrypting image file
117
+ */
118
+ void setCryptAddress (const size_t cryptAddress) {
119
+ _cryptAddress = cryptAddress & 0x00fffff0 ;
120
+ }
121
+
122
+ /*
123
+ sets crypt config used for decrypting image file
124
+ */
125
+ void setCryptConfig (const uint8_t cryptConfig) {
126
+ _cryptCfg = cryptConfig & 0x0f ;
127
+ }
128
+ #endif /* UPDATE_NOCRYPT */
129
+
84
130
/*
85
131
Aborts the running update
86
132
*/
@@ -95,8 +141,13 @@ class UpdateClass {
95
141
96
142
/*
97
143
sets the expected MD5 for the firmware (hexString)
144
+ If calc_post_decryption is true, the update library will calculate the MD5 after the decryption, if false the calculation occurs before the decryption
98
145
*/
99
- bool setMD5 (const char *expected_md5);
146
+ bool setMD5 (const char *expected_md5
147
+ #ifndef UPDATE_NOCRYPT
148
+ , bool calc_post_decryption = true
149
+ #endif /* #ifdef UPDATE_NOCRYPT */
150
+ );
100
151
101
152
/*
102
153
returns the MD5 String of the successfully ended firmware
@@ -193,13 +244,21 @@ class UpdateClass {
193
244
private:
194
245
void _reset ();
195
246
void _abort (uint8_t err);
247
+ #ifndef UPDATE_NOCRYPT
248
+ void _cryptKeyTweak (size_t cryptAddress, uint8_t *tweaked_key);
249
+ bool _decryptBuffer ();
250
+ #endif /* UPDATE_NOCRYPT */
196
251
bool _writeBuffer ();
197
252
bool _verifyHeader (uint8_t data);
198
253
bool _verifyEnd ();
199
254
bool _enablePartition (const esp_partition_t *partition);
200
255
bool _chkDataInBlock (const uint8_t *data, size_t len) const ; // check if block contains any data or is empty
201
256
202
257
uint8_t _error;
258
+ #ifndef UPDATE_NOCRYPT
259
+ uint8_t *_cryptKey;
260
+ uint8_t *_cryptBuffer;
261
+ #endif /* UPDATE_NOCRYPT */
203
262
uint8_t *_buffer;
204
263
uint8_t *_skipBuffer;
205
264
size_t _bufferLen;
@@ -211,10 +270,19 @@ class UpdateClass {
211
270
const esp_partition_t *_partition;
212
271
213
272
String _target_md5;
273
+ #ifndef UPDATE_NOCRYPT
274
+ bool _target_md5_decrypted = true ;
275
+ #endif /* UPDATE_NOCRYPT */
214
276
MD5Builder _md5;
215
277
216
278
int _ledPin;
217
279
uint8_t _ledOn;
280
+
281
+ #ifndef UPDATE_NOCRYPT
282
+ uint8_t _cryptMode;
283
+ size_t _cryptAddress;
284
+ uint8_t _cryptCfg;
285
+ #endif /* UPDATE_NOCRYPT */
218
286
};
219
287
220
288
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_UPDATE)
0 commit comments