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