|
| 1 | +/* |
| 2 | + * AES block cipher |
| 3 | + * |
| 4 | + * Copyright (C) 2015-2017, Silicon Labs, http://www.silabs.com |
| 5 | + * SPDX-License-Identifier: Apache-2.0 |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | + * not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | +#ifndef MBEDTLS_AES_ALT_H |
| 20 | +#define MBEDTLS_AES_ALT_H |
| 21 | + |
| 22 | +/***************************************************************************//** |
| 23 | + * \addtogroup sl_crypto |
| 24 | + * \{ |
| 25 | + ******************************************************************************/ |
| 26 | + |
| 27 | +/***************************************************************************//** |
| 28 | + * \addtogroup sl_crypto_aes AES block cipher |
| 29 | + * \brief Hardware accelerated AES block cipher. |
| 30 | + * \{ |
| 31 | + ******************************************************************************/ |
| 32 | + |
| 33 | +#if defined(MBEDTLS_AES_ALT) |
| 34 | +/* SiliconLabs CRYPTO hardware acceleration implementation */ |
| 35 | + |
| 36 | +#ifdef __cplusplus |
| 37 | +extern "C" { |
| 38 | +#endif |
| 39 | + |
| 40 | +/** |
| 41 | + * \brief AES context structure |
| 42 | + */ |
| 43 | +typedef struct |
| 44 | +{ |
| 45 | + unsigned int keybits; /*!< size of key */ |
| 46 | + unsigned char key[32]; /*!< AES key 128 or 256 bits */ |
| 47 | +} |
| 48 | +mbedtls_aes_context; |
| 49 | + |
| 50 | +/** |
| 51 | + * \brief Initialize AES context |
| 52 | + * |
| 53 | + * \param ctx AES context to be initialized |
| 54 | + */ |
| 55 | +void mbedtls_aes_init( mbedtls_aes_context *ctx ); |
| 56 | + |
| 57 | +/** |
| 58 | + * \brief Clear AES context |
| 59 | + * |
| 60 | + * \param ctx AES context to be cleared |
| 61 | + */ |
| 62 | +void mbedtls_aes_free( mbedtls_aes_context *ctx ); |
| 63 | + |
| 64 | +/** |
| 65 | + * \brief AES key schedule (encryption) |
| 66 | + * |
| 67 | + * \param ctx AES context to be initialized |
| 68 | + * \param key encryption key |
| 69 | + * \param keybits must be 128 or 256 |
| 70 | + * |
| 71 | + * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH |
| 72 | + */ |
| 73 | +int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key, |
| 74 | + unsigned int keybits ); |
| 75 | + |
| 76 | +/** |
| 77 | + * \brief AES key schedule (decryption) |
| 78 | + * |
| 79 | + * \param ctx AES context to be initialized |
| 80 | + * \param key decryption key |
| 81 | + * \param keybits must be 128 or 256 |
| 82 | + * |
| 83 | + * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH |
| 84 | + */ |
| 85 | +int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx, const unsigned char *key, |
| 86 | + unsigned int keybits ); |
| 87 | + |
| 88 | +/** |
| 89 | + * \brief AES-ECB block encryption/decryption |
| 90 | + * |
| 91 | + * \param ctx AES context |
| 92 | + * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT |
| 93 | + * \param input 16-byte input block |
| 94 | + * \param output 16-byte output block |
| 95 | + * |
| 96 | + * \return 0 if successful |
| 97 | + */ |
| 98 | +int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx, |
| 99 | + int mode, |
| 100 | + const unsigned char input[16], |
| 101 | + unsigned char output[16] ); |
| 102 | + |
| 103 | +#if defined(MBEDTLS_CIPHER_MODE_CBC) |
| 104 | +/** |
| 105 | + * \brief AES-CBC buffer encryption/decryption |
| 106 | + * Length should be a multiple of the block |
| 107 | + * size (16 bytes) |
| 108 | + * |
| 109 | + * \note Upon exit, the content of the IV is updated so that you can |
| 110 | + * call the function same function again on the following |
| 111 | + * block(s) of data and get the same result as if it was |
| 112 | + * encrypted in one call. This allows a "streaming" usage. |
| 113 | + * If on the other hand you need to retain the contents of the |
| 114 | + * IV, you should either save it manually or use the cipher |
| 115 | + * module instead. |
| 116 | + * |
| 117 | + * \param ctx AES context |
| 118 | + * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT |
| 119 | + * \param length length of the input data |
| 120 | + * \param iv initialization vector (updated after use) |
| 121 | + * \param input buffer holding the input data |
| 122 | + * \param output buffer holding the output data |
| 123 | + * |
| 124 | + * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH |
| 125 | + */ |
| 126 | +int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx, |
| 127 | + int mode, |
| 128 | + size_t length, |
| 129 | + unsigned char iv[16], |
| 130 | + const unsigned char *input, |
| 131 | + unsigned char *output ); |
| 132 | +#endif /* MBEDTLS_CIPHER_MODE_CBC */ |
| 133 | + |
| 134 | +#if defined(MBEDTLS_CIPHER_MODE_CFB) |
| 135 | +/** |
| 136 | + * \brief AES-CFB128 buffer encryption/decryption. |
| 137 | + * |
| 138 | + * Note: Due to the nature of CFB you should use the same key schedule for |
| 139 | + * both encryption and decryption. So a context initialized with |
| 140 | + * mbedtls_aes_setkey_enc() for both MBEDTLS_AES_ENCRYPT and MBEDTLS_AES_DECRYPT. |
| 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 MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT |
| 152 | + * \param length length of the input data |
| 153 | + * \param iv_off offset in IV (updated after use) |
| 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 |
| 159 | + */ |
| 160 | +int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx, |
| 161 | + int mode, |
| 162 | + size_t length, |
| 163 | + size_t *iv_off, |
| 164 | + unsigned char iv[16], |
| 165 | + const unsigned char *input, |
| 166 | + unsigned char *output ); |
| 167 | + |
| 168 | +/** |
| 169 | + * \brief AES-CFB8 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 | + * mbedtls_aes_setkey_enc() for both MBEDTLS_AES_ENCRYPT and MBEDTLS_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 MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT |
| 185 | + * \param length length of the input data |
| 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 mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx, |
| 193 | + int mode, |
| 194 | + size_t length, |
| 195 | + unsigned char iv[16], |
| 196 | + const unsigned char *input, |
| 197 | + unsigned char *output ); |
| 198 | +#endif /*MBEDTLS_CIPHER_MODE_CFB */ |
| 199 | + |
| 200 | +#if defined(MBEDTLS_CIPHER_MODE_CTR) |
| 201 | +/** |
| 202 | + * \brief AES-CTR buffer encryption/decryption |
| 203 | + * |
| 204 | + * Warning: You have to keep the maximum use of your counter in mind! |
| 205 | + * |
| 206 | + * Note: Due to the nature of CTR you should use the same key schedule for |
| 207 | + * both encryption and decryption. So a context initialized with |
| 208 | + * mbedtls_aes_setkey_enc() for both MBEDTLS_AES_ENCRYPT and MBEDTLS_AES_DECRYPT. |
| 209 | + * |
| 210 | + * \param ctx AES context |
| 211 | + * \param length The length of the data |
| 212 | + * \param nc_off The offset in the current stream_block (for resuming |
| 213 | + * within current cipher stream). The offset pointer to |
| 214 | + * should be 0 at the start of a stream. |
| 215 | + * \param nonce_counter The 128-bit nonce and counter. |
| 216 | + * \param stream_block The saved stream-block for resuming. Is overwritten |
| 217 | + * by the function. |
| 218 | + * \param input The input data stream |
| 219 | + * \param output The output data stream |
| 220 | + * |
| 221 | + * \return 0 if successful |
| 222 | + */ |
| 223 | +int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx, |
| 224 | + size_t length, |
| 225 | + size_t *nc_off, |
| 226 | + unsigned char nonce_counter[16], |
| 227 | + unsigned char stream_block[16], |
| 228 | + const unsigned char *input, |
| 229 | + unsigned char *output ); |
| 230 | +#endif /* MBEDTLS_CIPHER_MODE_CTR */ |
| 231 | + |
| 232 | +/** |
| 233 | + * \brief Internal AES block encryption function |
| 234 | + * (Only exposed to allow overriding it, |
| 235 | + * see MBEDTLS_AES_ENCRYPT_ALT) |
| 236 | + * |
| 237 | + * \param ctx AES context |
| 238 | + * \param input Plaintext block |
| 239 | + * \param output Output (ciphertext) block |
| 240 | + */ |
| 241 | +void mbedtls_aes_encrypt( mbedtls_aes_context *ctx, |
| 242 | + const unsigned char input[16], |
| 243 | + unsigned char output[16] ); |
| 244 | + |
| 245 | +/** |
| 246 | + * \brief Internal AES block decryption function |
| 247 | + * (Only exposed to allow overriding it, |
| 248 | + * see MBEDTLS_AES_DECRYPT_ALT) |
| 249 | + * |
| 250 | + * \param ctx AES context |
| 251 | + * \param input Ciphertext block |
| 252 | + * \param output Output (plaintext) block |
| 253 | + */ |
| 254 | +void mbedtls_aes_decrypt( mbedtls_aes_context *ctx, |
| 255 | + const unsigned char input[16], |
| 256 | + unsigned char output[16] ); |
| 257 | + |
| 258 | +#ifdef __cplusplus |
| 259 | +} |
| 260 | +#endif |
| 261 | + |
| 262 | +#endif /* MBEDTLS_AES_ALT */ |
| 263 | + |
| 264 | +/** \} (end addtogroup sl_crypto_aes) */ |
| 265 | +/** \} (end addtogroup sl_crypto) */ |
| 266 | + |
| 267 | +#endif /* MBEDTLS_AES_ALT_H */ |
0 commit comments