Skip to content

Commit 6ce81e7

Browse files
rojercesantabot
authored andcommitted
ESP32: Update to ESP-IDF 4.2
cesanta/mongoose-os#565 PUBLISHED_FROM=18b13b32475dc4a41d91e5b30a857fea992633b2
1 parent 9ee0562 commit 6ce81e7

File tree

3 files changed

+355
-2
lines changed

3 files changed

+355
-2
lines changed

include/esp32/aes_alt.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ extern "C" {
2828
#endif
2929

3030
#if defined(MBEDTLS_AES_ALT)
31-
#include "hwcrypto/aes.h"
31+
#include "esp32_aes.h"
3232

3333
typedef esp_aes_context mbedtls_aes_context;
3434

include/esp32/esp32_aes.h

Lines changed: 353 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,353 @@
1+
/**
2+
* \brief AES block cipher, ESP32 hardware accelerated version
3+
* Based on mbedTLS FIPS-197 compliant version.
4+
*
5+
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
6+
* Additions Copyright (C) 2016, Espressif Systems (Shanghai) PTE Ltd
7+
* SPDX-License-Identifier: Apache-2.0
8+
*
9+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
10+
* not use this file except in compliance with the License.
11+
* You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
* See the License for the specific language governing permissions and
19+
* limitations under the License.
20+
*
21+
*
22+
*/
23+
24+
#ifndef ESP_AES_H
25+
#define ESP_AES_H
26+
27+
#include "esp_types.h"
28+
#include "esp32/rom/aes.h"
29+
30+
#ifdef __cplusplus
31+
extern "C" {
32+
#endif
33+
34+
/* padlock.c and aesni.c rely on these values! */
35+
#define ESP_AES_ENCRYPT 1
36+
#define ESP_AES_DECRYPT 0
37+
38+
#define ERR_ESP_AES_INVALID_KEY_LENGTH -0x0020 /**< Invalid key length. */
39+
#define ERR_ESP_AES_INVALID_INPUT_LENGTH -0x0022 /**< Invalid data input length. */
40+
41+
/**
42+
* \brief AES context structure
43+
*
44+
*/
45+
typedef struct {
46+
uint8_t key_bytes;
47+
volatile uint8_t key_in_hardware; /* This variable is used for fault injection checks, so marked volatile to avoid optimisation */
48+
uint8_t key[32];
49+
} esp_aes_context;
50+
51+
/**
52+
* \brief The AES XTS context-type definition.
53+
*/
54+
typedef struct
55+
{
56+
esp_aes_context crypt; /*!< The AES context to use for AES block
57+
encryption or decryption. */
58+
esp_aes_context tweak; /*!< The AES context used for tweak
59+
computation. */
60+
} esp_aes_xts_context;
61+
62+
63+
/**
64+
* \brief Lock access to AES hardware unit
65+
*
66+
* AES hardware unit can only be used by one
67+
* consumer at a time.
68+
*
69+
* esp_aes_xxx API calls automatically manage locking & unlocking of
70+
* hardware, this function is only needed if you want to call
71+
* ets_aes_xxx functions directly.
72+
*/
73+
void esp_aes_acquire_hardware( void );
74+
75+
/**
76+
* \brief Unlock access to AES hardware unit
77+
*
78+
* esp_aes_xxx API calls automatically manage locking & unlocking of
79+
* hardware, this function is only needed if you want to call
80+
* ets_aes_xxx functions directly.
81+
*/
82+
void esp_aes_release_hardware( void );
83+
84+
/**
85+
* \brief Initialize AES context
86+
*
87+
* \param ctx AES context to be initialized
88+
*/
89+
void esp_aes_init( esp_aes_context *ctx );
90+
91+
/**
92+
* \brief Clear AES context
93+
*
94+
* \param ctx AES context to be cleared
95+
*/
96+
void esp_aes_free( esp_aes_context *ctx );
97+
98+
/**
99+
* \brief This function initializes the specified AES XTS context.
100+
*
101+
* It must be the first API called before using
102+
* the context.
103+
*
104+
* \param ctx The AES XTS context to initialize.
105+
*/
106+
void esp_aes_xts_init( esp_aes_xts_context *ctx );
107+
108+
/**
109+
* \brief This function releases and clears the specified AES XTS context.
110+
*
111+
* \param ctx The AES XTS context to clear.
112+
*/
113+
void esp_aes_xts_free( esp_aes_xts_context *ctx );
114+
115+
/**
116+
* \brief AES set key schedule (encryption or decryption)
117+
*
118+
* \param ctx AES context to be initialized
119+
* \param key encryption key
120+
* \param keybits must be 128, 192 or 256
121+
*
122+
* \return 0 if successful, or ERR_AES_INVALID_KEY_LENGTH
123+
*/
124+
int esp_aes_setkey( esp_aes_context *ctx, const unsigned char *key, unsigned int keybits );
125+
126+
/**
127+
* \brief AES-ECB block encryption/decryption
128+
*
129+
* \param ctx AES context
130+
* \param mode AES_ENCRYPT or AES_DECRYPT
131+
* \param input 16-byte input block
132+
* \param output 16-byte output block
133+
*
134+
* \return 0 if successful
135+
*/
136+
int esp_aes_crypt_ecb( esp_aes_context *ctx, int mode, const unsigned char input[16], unsigned char output[16] );
137+
138+
/**
139+
* \brief AES-CBC buffer encryption/decryption
140+
* Length should be a multiple of the block
141+
* size (16 bytes)
142+
*
143+
* \note Upon exit, the content of the IV is updated so that you can
144+
* call the function same function again on the following
145+
* block(s) of data and get the same result as if it was
146+
* encrypted in one call. This allows a "streaming" usage.
147+
* If on the other hand you need to retain the contents of the
148+
* IV, you should either save it manually or use the cipher
149+
* module instead.
150+
*
151+
* \param ctx AES context
152+
* \param mode AES_ENCRYPT or AES_DECRYPT
153+
* \param length length of the input data
154+
* \param iv initialization vector (updated after use)
155+
* \param input buffer holding the input data
156+
* \param output buffer holding the output data
157+
*
158+
* \return 0 if successful, or ERR_AES_INVALID_INPUT_LENGTH
159+
*/
160+
int esp_aes_crypt_cbc( esp_aes_context *ctx,
161+
int mode,
162+
size_t length,
163+
unsigned char iv[16],
164+
const unsigned char *input,
165+
unsigned char *output );
166+
167+
168+
/**
169+
* \brief AES-CFB128 buffer encryption/decryption.
170+
*
171+
* Note: Due to the nature of CFB you should use the same key schedule for
172+
* both encryption and decryption. So a context initialized with
173+
* esp_aes_setkey_enc() for both AES_ENCRYPT and AES_DECRYPT.
174+
*
175+
* \note Upon exit, the content of the IV is updated so that you can
176+
* call the function same function again on the following
177+
* block(s) of data and get the same result as if it was
178+
* encrypted in one call. This allows a "streaming" usage.
179+
* If on the other hand you need to retain the contents of the
180+
* IV, you should either save it manually or use the cipher
181+
* module instead.
182+
*
183+
* \param ctx AES context
184+
* \param mode AES_ENCRYPT or AES_DECRYPT
185+
* \param length length of the input data
186+
* \param iv_off offset in IV (updated after use)
187+
* \param iv initialization vector (updated after use)
188+
* \param input buffer holding the input data
189+
* \param output buffer holding the output data
190+
*
191+
* \return 0 if successful
192+
*/
193+
int esp_aes_crypt_cfb128( esp_aes_context *ctx,
194+
int mode,
195+
size_t length,
196+
size_t *iv_off,
197+
unsigned char iv[16],
198+
const unsigned char *input,
199+
unsigned char *output );
200+
201+
/**
202+
* \brief AES-CFB8 buffer encryption/decryption.
203+
*
204+
* Note: Due to the nature of CFB you should use the same key schedule for
205+
* both encryption and decryption. So a context initialized with
206+
* esp_aes_setkey_enc() for both AES_ENCRYPT and AES_DECRYPT.
207+
*
208+
* \note Upon exit, the content of the IV is updated so that you can
209+
* call the function same function again on the following
210+
* block(s) of data and get the same result as if it was
211+
* encrypted in one call. This allows a "streaming" usage.
212+
* If on the other hand you need to retain the contents of the
213+
* IV, you should either save it manually or use the cipher
214+
* module instead.
215+
*
216+
* \param ctx AES context
217+
* \param mode AES_ENCRYPT or AES_DECRYPT
218+
* \param length length of the input data
219+
* \param iv initialization vector (updated after use)
220+
* \param input buffer holding the input data
221+
* \param output buffer holding the output data
222+
*
223+
* \return 0 if successful
224+
*/
225+
int esp_aes_crypt_cfb8( esp_aes_context *ctx,
226+
int mode,
227+
size_t length,
228+
unsigned char iv[16],
229+
const unsigned char *input,
230+
unsigned char *output );
231+
232+
/**
233+
* \brief AES-CTR buffer encryption/decryption
234+
*
235+
* Warning: You have to keep the maximum use of your counter in mind!
236+
*
237+
* Note: Due to the nature of CTR you should use the same key schedule for
238+
* both encryption and decryption. So a context initialized with
239+
* esp_aes_setkey_enc() for both AES_ENCRYPT and AES_DECRYPT.
240+
*
241+
* \param ctx AES context
242+
* \param length The length of the data
243+
* \param nc_off The offset in the current stream_block (for resuming
244+
* within current cipher stream). The offset pointer to
245+
* should be 0 at the start of a stream.
246+
* \param nonce_counter The 128-bit nonce and counter.
247+
* \param stream_block The saved stream-block for resuming. Is overwritten
248+
* by the function.
249+
* \param input The input data stream
250+
* \param output The output data stream
251+
*
252+
* \return 0 if successful
253+
*/
254+
int esp_aes_crypt_ctr( esp_aes_context *ctx,
255+
size_t length,
256+
size_t *nc_off,
257+
unsigned char nonce_counter[16],
258+
unsigned char stream_block[16],
259+
const unsigned char *input,
260+
unsigned char *output );
261+
262+
/**
263+
* \brief This function prepares an XTS context for encryption and
264+
* sets the encryption key.
265+
*
266+
* \param ctx The AES XTS context to which the key should be bound.
267+
* \param key The encryption key. This is comprised of the XTS key1
268+
* concatenated with the XTS key2.
269+
* \param keybits The size of \p key passed in bits. Valid options are:
270+
* <ul><li>256 bits (each of key1 and key2 is a 128-bit key)</li>
271+
* <li>512 bits (each of key1 and key2 is a 256-bit key)</li></ul>
272+
*
273+
* \return \c 0 on success.
274+
* \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
275+
*/
276+
int esp_aes_xts_setkey_enc( esp_aes_xts_context *ctx,
277+
const unsigned char *key,
278+
unsigned int keybits );
279+
280+
/**
281+
* \brief This function performs an AES-OFB (Output Feedback Mode)
282+
* encryption or decryption operation.
283+
*
284+
* \param ctx The AES context to use for encryption or decryption.
285+
* It must be initialized and bound to a key.
286+
* \param length The length of the input data.
287+
* \param iv_off The offset in IV (updated after use).
288+
* It must point to a valid \c size_t.
289+
* \param iv The initialization vector (updated after use).
290+
* It must be a readable and writeable buffer of \c 16 Bytes.
291+
* \param input The buffer holding the input data.
292+
* It must be readable and of size \p length Bytes.
293+
* \param output The buffer holding the output data.
294+
* It must be writeable and of size \p length Bytes.
295+
*
296+
* \return \c 0 on success.
297+
*/
298+
int esp_aes_crypt_ofb( esp_aes_context *ctx,
299+
size_t length,
300+
size_t *iv_off,
301+
unsigned char iv[16],
302+
const unsigned char *input,
303+
unsigned char *output );
304+
305+
/**
306+
* \brief This function prepares an XTS context for decryption and
307+
* sets the decryption key.
308+
*
309+
* \param ctx The AES XTS context to which the key should be bound.
310+
* \param key The decryption key. This is comprised of the XTS key1
311+
* concatenated with the XTS key2.
312+
* \param keybits The size of \p key passed in bits. Valid options are:
313+
* <ul><li>256 bits (each of key1 and key2 is a 128-bit key)</li>
314+
* <li>512 bits (each of key1 and key2 is a 256-bit key)</li></ul>
315+
*
316+
* \return \c 0 on success.
317+
* \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
318+
*/
319+
int esp_aes_xts_setkey_dec( esp_aes_xts_context *ctx,
320+
const unsigned char *key,
321+
unsigned int keybits );
322+
323+
324+
/**
325+
* \brief Internal AES block encryption function
326+
* (Only exposed to allow overriding it,
327+
* see AES_ENCRYPT_ALT)
328+
*
329+
* \param ctx AES context
330+
* \param input Plaintext block
331+
* \param output Output (ciphertext) block
332+
*/
333+
int esp_internal_aes_encrypt( esp_aes_context *ctx, const unsigned char input[16], unsigned char output[16] );
334+
335+
/**
336+
* \brief Internal AES block decryption function
337+
* (Only exposed to allow overriding it,
338+
* see AES_DECRYPT_ALT)
339+
*
340+
* \param ctx AES context
341+
* \param input Ciphertext block
342+
* \param output Output (plaintext) block
343+
*/
344+
int esp_internal_aes_decrypt( esp_aes_context *ctx, const unsigned char input[16], unsigned char output[16] );
345+
346+
/** AES-XTS buffer encryption/decryption */
347+
int esp_aes_crypt_xts( esp_aes_xts_context *ctx, int mode, size_t length, const unsigned char data_unit[16], const unsigned char *input, unsigned char *output );
348+
349+
#ifdef __cplusplus
350+
}
351+
#endif
352+
353+
#endif /* aes.h */

include/esp32/mbedtls_platform_config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010
#define MBEDTLS_MPI_EXP_MOD_ALT
1111

1212
#define MBEDTLS_CIPHER_MODE_XTS
13-
#define ESP32_MBEDTLS_DYN_BUF_CANARY
13+
// #define ESP32_MBEDTLS_DYN_BUF_CANARY
1414

1515
/* no_extern_c_check */

0 commit comments

Comments
 (0)