| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /** | 
|  | 2 | * \file aes.h | 
| Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 3 | * | 
| Rose Zadik | 5ad7aea | 2018-03-26 12:00:09 +0100 | [diff] [blame] | 4 | * \brief   This file contains AES definitions and functions. | 
|  | 5 | * | 
|  | 6 | *          The Advanced Encryption Standard (AES) specifies a FIPS-approved | 
| Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 7 | *          cryptographic algorithm that can be used to protect electronic | 
|  | 8 | *          data. | 
|  | 9 | * | 
|  | 10 | *          The AES algorithm is a symmetric block cipher that can | 
|  | 11 | *          encrypt and decrypt information. For more information, see | 
|  | 12 | *          <em>FIPS Publication 197: Advanced Encryption Standard</em> and | 
|  | 13 | *          <em>ISO/IEC 18033-2:2006: Information technology -- Security | 
|  | 14 | *          techniques -- Encryption algorithms -- Part 2: Asymmetric | 
|  | 15 | *          ciphers</em>. | 
| Jaeden Amero | f167deb | 2018-05-30 19:20:48 +0100 | [diff] [blame] | 16 | * | 
|  | 17 | *          The AES-XTS block mode is standardized by NIST SP 800-38E | 
|  | 18 | *          <https://nvlpubs.nist.gov/nistpubs/legacy/sp/nistspecialpublication800-38e.pdf> | 
|  | 19 | *          and described in detail by IEEE P1619 | 
|  | 20 | *          <https://ieeexplore.ieee.org/servlet/opac?punumber=4375278>. | 
| Darryl Green | a40a101 | 2018-01-05 15:33:17 +0000 | [diff] [blame] | 21 | */ | 
| Rose Zadik | 5ad7aea | 2018-03-26 12:00:09 +0100 | [diff] [blame] | 22 |  | 
| Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 23 | /*  Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved. | 
| Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 24 | *  SPDX-License-Identifier: Apache-2.0 | 
|  | 25 | * | 
|  | 26 | *  Licensed under the Apache License, Version 2.0 (the "License"); you may | 
|  | 27 | *  not use this file except in compliance with the License. | 
|  | 28 | *  You may obtain a copy of the License at | 
|  | 29 | * | 
|  | 30 | *  http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 31 | * | 
|  | 32 | *  Unless required by applicable law or agreed to in writing, software | 
|  | 33 | *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | 
|  | 34 | *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 35 | *  See the License for the specific language governing permissions and | 
|  | 36 | *  limitations under the License. | 
| Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 37 | * | 
| Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 38 | *  This file is part of Mbed TLS (https://tls.mbed.org) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 39 | */ | 
| Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 40 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 41 | #ifndef MBEDTLS_AES_H | 
|  | 42 | #define MBEDTLS_AES_H | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 43 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 44 | #if !defined(MBEDTLS_CONFIG_FILE) | 
| Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 45 | #include "config.h" | 
| Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 46 | #else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 47 | #include MBEDTLS_CONFIG_FILE | 
| Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 48 | #endif | 
| Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 49 |  | 
| Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 50 | #include <stddef.h> | 
| Manuel Pégourié-Gonnard | ab22910 | 2015-04-15 11:53:16 +0200 | [diff] [blame] | 51 | #include <stdint.h> | 
| Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 52 |  | 
| Manuel Pégourié-Gonnard | 5b68565 | 2013-12-18 11:45:21 +0100 | [diff] [blame] | 53 | /* padlock.c and aesni.c rely on these values! */ | 
| Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 54 | #define MBEDTLS_AES_ENCRYPT     1 /**< AES encryption. */ | 
|  | 55 | #define MBEDTLS_AES_DECRYPT     0 /**< AES decryption. */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 56 |  | 
| Andres Amaya Garcia | c538064 | 2017-11-28 19:57:51 +0000 | [diff] [blame] | 57 | /* Error codes in range 0x0020-0x0022 */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 58 | #define MBEDTLS_ERR_AES_INVALID_KEY_LENGTH                -0x0020  /**< Invalid key length. */ | 
|  | 59 | #define MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH              -0x0022  /**< Invalid data input length. */ | 
| Paul Bakker | 2b222c8 | 2009-07-27 21:03:45 +0000 | [diff] [blame] | 60 |  | 
| Mohammad Azim Khan | e5b5bd7 | 2017-11-24 10:52:51 +0000 | [diff] [blame] | 61 | /* Error codes in range 0x0021-0x0025 */ | 
|  | 62 | #define MBEDTLS_ERR_AES_BAD_INPUT_DATA                    -0x0021  /**< Invalid input data. */ | 
| Ron Eldor | 9924bdc | 2018-10-04 10:59:13 +0300 | [diff] [blame] | 63 |  | 
|  | 64 | /* MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE is deprecated and should not be used. */ | 
| Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 65 | #define MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE               -0x0023  /**< Feature not available. For example, an unsupported AES key size. */ | 
| Ron Eldor | 9924bdc | 2018-10-04 10:59:13 +0300 | [diff] [blame] | 66 |  | 
|  | 67 | /* MBEDTLS_ERR_AES_HW_ACCEL_FAILED is deprecated and should not be used. */ | 
| Gilles Peskine | 7ecab3d | 2018-01-26 17:56:38 +0100 | [diff] [blame] | 68 | #define MBEDTLS_ERR_AES_HW_ACCEL_FAILED                   -0x0025  /**< AES hardware accelerator failed. */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 69 |  | 
| Andres AG | f5bf718 | 2017-03-03 14:09:56 +0000 | [diff] [blame] | 70 | #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \ | 
|  | 71 | !defined(inline) && !defined(__cplusplus) | 
|  | 72 | #define inline __inline | 
|  | 73 | #endif | 
|  | 74 |  | 
| Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 75 | #ifdef __cplusplus | 
|  | 76 | extern "C" { | 
|  | 77 | #endif | 
|  | 78 |  | 
| Ron Eldor | b2aacec | 2017-05-18 16:53:08 +0300 | [diff] [blame] | 79 | #if !defined(MBEDTLS_AES_ALT) | 
|  | 80 | // Regular implementation | 
|  | 81 | // | 
|  | 82 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 83 | /** | 
| Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 84 | * \brief The AES context-type definition. | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 85 | */ | 
| Dawid Drozd | 428cc52 | 2018-07-24 10:02:47 +0200 | [diff] [blame] | 86 | typedef struct mbedtls_aes_context | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 87 | { | 
| Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 88 | int nr;                     /*!< The number of rounds. */ | 
|  | 89 | uint32_t *rk;               /*!< AES round keys. */ | 
|  | 90 | uint32_t buf[68];           /*!< Unaligned data buffer. This buffer can | 
|  | 91 | hold 32 extra Bytes, which can be used for | 
|  | 92 | one of the following purposes: | 
|  | 93 | <ul><li>Alignment if VIA padlock is | 
|  | 94 | used.</li> | 
|  | 95 | <li>Simplifying key expansion in the 256-bit | 
|  | 96 | case by generating an extra round key. | 
|  | 97 | </li></ul> */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 98 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 99 | mbedtls_aes_context; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 100 |  | 
| Jaeden Amero | 9366feb | 2018-05-29 18:55:17 +0100 | [diff] [blame] | 101 | #if defined(MBEDTLS_CIPHER_MODE_XTS) | 
|  | 102 | /** | 
|  | 103 | * \brief The AES XTS context-type definition. | 
|  | 104 | */ | 
| Dawid Drozd | 428cc52 | 2018-07-24 10:02:47 +0200 | [diff] [blame] | 105 | typedef struct mbedtls_aes_xts_context | 
| Jaeden Amero | 9366feb | 2018-05-29 18:55:17 +0100 | [diff] [blame] | 106 | { | 
|  | 107 | mbedtls_aes_context crypt; /*!< The AES context to use for AES block | 
|  | 108 | encryption or decryption. */ | 
|  | 109 | mbedtls_aes_context tweak; /*!< The AES context used for tweak | 
|  | 110 | computation. */ | 
|  | 111 | } mbedtls_aes_xts_context; | 
|  | 112 | #endif /* MBEDTLS_CIPHER_MODE_XTS */ | 
|  | 113 |  | 
| Ron Eldor | b2aacec | 2017-05-18 16:53:08 +0300 | [diff] [blame] | 114 | #else  /* MBEDTLS_AES_ALT */ | 
|  | 115 | #include "aes_alt.h" | 
|  | 116 | #endif /* MBEDTLS_AES_ALT */ | 
|  | 117 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 118 | /** | 
| Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 119 | * \brief          This function initializes the specified AES context. | 
| Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 120 | * | 
| Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 121 | *                 It must be the first API called before using | 
|  | 122 | *                 the context. | 
|  | 123 | * | 
|  | 124 | * \param ctx      The AES context to initialize. | 
| Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 125 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 126 | void mbedtls_aes_init( mbedtls_aes_context *ctx ); | 
| Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 127 |  | 
|  | 128 | /** | 
| Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 129 | * \brief          This function releases and clears the specified AES context. | 
| Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 130 | * | 
| Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 131 | * \param ctx      The AES context to clear. | 
| Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 132 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 133 | void mbedtls_aes_free( mbedtls_aes_context *ctx ); | 
| Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 134 |  | 
| Jaeden Amero | 9366feb | 2018-05-29 18:55:17 +0100 | [diff] [blame] | 135 | #if defined(MBEDTLS_CIPHER_MODE_XTS) | 
|  | 136 | /** | 
|  | 137 | * \brief          This function initializes the specified AES XTS context. | 
|  | 138 | * | 
|  | 139 | *                 It must be the first API called before using | 
|  | 140 | *                 the context. | 
|  | 141 | * | 
|  | 142 | * \param ctx      The AES XTS context to initialize. | 
|  | 143 | */ | 
|  | 144 | void mbedtls_aes_xts_init( mbedtls_aes_xts_context *ctx ); | 
|  | 145 |  | 
|  | 146 | /** | 
|  | 147 | * \brief          This function releases and clears the specified AES XTS context. | 
|  | 148 | * | 
|  | 149 | * \param ctx      The AES XTS context to clear. | 
|  | 150 | */ | 
|  | 151 | void mbedtls_aes_xts_free( mbedtls_aes_xts_context *ctx ); | 
|  | 152 | #endif /* MBEDTLS_CIPHER_MODE_XTS */ | 
|  | 153 |  | 
| Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 154 | /** | 
| Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 155 | * \brief          This function sets the encryption key. | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 156 | * | 
| Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 157 | * \param ctx      The AES context to which the key should be bound. | 
|  | 158 | * \param key      The encryption key. | 
|  | 159 | * \param keybits  The size of data passed in bits. Valid options are: | 
|  | 160 | *                 <ul><li>128 bits</li> | 
|  | 161 | *                 <li>192 bits</li> | 
|  | 162 | *                 <li>256 bits</li></ul> | 
| Paul Bakker | 2b222c8 | 2009-07-27 21:03:45 +0000 | [diff] [blame] | 163 | * | 
| Rose Zadik | 5ad7aea | 2018-03-26 12:00:09 +0100 | [diff] [blame] | 164 | * \return         \c 0 on success. | 
| Rose Zadik | 819d13d | 2018-04-16 09:35:15 +0100 | [diff] [blame] | 165 | * \return         #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure. | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 166 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 167 | int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key, | 
| Manuel Pégourié-Gonnard | b8186a5 | 2015-06-18 14:58:58 +0200 | [diff] [blame] | 168 | unsigned int keybits ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 169 |  | 
|  | 170 | /** | 
| Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 171 | * \brief          This function sets the decryption key. | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 172 | * | 
| Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 173 | * \param ctx      The AES context to which the key should be bound. | 
|  | 174 | * \param key      The decryption key. | 
|  | 175 | * \param keybits  The size of data passed. Valid options are: | 
|  | 176 | *                 <ul><li>128 bits</li> | 
|  | 177 | *                 <li>192 bits</li> | 
|  | 178 | *                 <li>256 bits</li></ul> | 
| Paul Bakker | 2b222c8 | 2009-07-27 21:03:45 +0000 | [diff] [blame] | 179 | * | 
| Rose Zadik | 5ad7aea | 2018-03-26 12:00:09 +0100 | [diff] [blame] | 180 | * \return         \c 0 on success. | 
|  | 181 | * \return         #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure. | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 182 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 183 | int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx, const unsigned char *key, | 
| Manuel Pégourié-Gonnard | b8186a5 | 2015-06-18 14:58:58 +0200 | [diff] [blame] | 184 | unsigned int keybits ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 185 |  | 
| Jaeden Amero | 9366feb | 2018-05-29 18:55:17 +0100 | [diff] [blame] | 186 | #if defined(MBEDTLS_CIPHER_MODE_XTS) | 
|  | 187 | /** | 
|  | 188 | * \brief          This function prepares an XTS context for encryption and | 
|  | 189 | *                 sets the encryption key. | 
|  | 190 | * | 
|  | 191 | * \param ctx      The AES XTS context to which the key should be bound. | 
|  | 192 | * \param key      The encryption key. This is comprised of the XTS key1 | 
|  | 193 | *                 concatenated with the XTS key2. | 
|  | 194 | * \param keybits  The size of \p key passed in bits. Valid options are: | 
|  | 195 | *                 <ul><li>256 bits (each of key1 and key2 is a 128-bit key)</li> | 
|  | 196 | *                 <li>512 bits (each of key1 and key2 is a 256-bit key)</li></ul> | 
|  | 197 | * | 
|  | 198 | * \return         \c 0 on success. | 
|  | 199 | * \return         #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure. | 
|  | 200 | */ | 
|  | 201 | int mbedtls_aes_xts_setkey_enc( mbedtls_aes_xts_context *ctx, | 
|  | 202 | const unsigned char *key, | 
|  | 203 | unsigned int keybits ); | 
|  | 204 |  | 
|  | 205 | /** | 
|  | 206 | * \brief          This function prepares an XTS context for decryption and | 
|  | 207 | *                 sets the decryption key. | 
|  | 208 | * | 
|  | 209 | * \param ctx      The AES XTS context to which the key should be bound. | 
|  | 210 | * \param key      The decryption key. This is comprised of the XTS key1 | 
|  | 211 | *                 concatenated with the XTS key2. | 
|  | 212 | * \param keybits  The size of \p key passed in bits. Valid options are: | 
|  | 213 | *                 <ul><li>256 bits (each of key1 and key2 is a 128-bit key)</li> | 
|  | 214 | *                 <li>512 bits (each of key1 and key2 is a 256-bit key)</li></ul> | 
|  | 215 | * | 
|  | 216 | * \return         \c 0 on success. | 
|  | 217 | * \return         #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure. | 
|  | 218 | */ | 
|  | 219 | int mbedtls_aes_xts_setkey_dec( mbedtls_aes_xts_context *ctx, | 
|  | 220 | const unsigned char *key, | 
|  | 221 | unsigned int keybits ); | 
|  | 222 | #endif /* MBEDTLS_CIPHER_MODE_XTS */ | 
|  | 223 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 224 | /** | 
| Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 225 | * \brief          This function performs an AES single-block encryption or | 
|  | 226 | *                 decryption operation. | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 227 | * | 
| Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 228 | *                 It performs the operation defined in the \p mode parameter | 
|  | 229 | *                 (encrypt or decrypt), on the input data buffer defined in | 
|  | 230 | *                 the \p input parameter. | 
| Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 231 | * | 
| Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 232 | *                 mbedtls_aes_init(), and either mbedtls_aes_setkey_enc() or | 
|  | 233 | *                 mbedtls_aes_setkey_dec() must be called before the first | 
|  | 234 | *                 call to this API with the same context. | 
|  | 235 | * | 
|  | 236 | * \param ctx      The AES context to use for encryption or decryption. | 
|  | 237 | * \param mode     The AES operation: #MBEDTLS_AES_ENCRYPT or | 
|  | 238 | *                 #MBEDTLS_AES_DECRYPT. | 
|  | 239 | * \param input    The 16-Byte buffer holding the input data. | 
|  | 240 | * \param output   The 16-Byte buffer holding the output data. | 
|  | 241 |  | 
|  | 242 | * \return         \c 0 on success. | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 243 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 244 | int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx, | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 245 | int mode, | 
| Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 246 | const unsigned char input[16], | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 247 | unsigned char output[16] ); | 
|  | 248 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 249 | #if defined(MBEDTLS_CIPHER_MODE_CBC) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 250 | /** | 
| Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 251 | * \brief  This function performs an AES-CBC encryption or decryption operation | 
|  | 252 | *         on full blocks. | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 253 | * | 
| Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 254 | *         It performs the operation defined in the \p mode | 
|  | 255 | *         parameter (encrypt/decrypt), on the input data buffer defined in | 
|  | 256 | *         the \p input parameter. | 
| Manuel Pégourié-Gonnard | 2be147a | 2015-01-23 16:19:47 +0000 | [diff] [blame] | 257 | * | 
| Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 258 | *         It can be called as many times as needed, until all the input | 
|  | 259 | *         data is processed. mbedtls_aes_init(), and either | 
|  | 260 | *         mbedtls_aes_setkey_enc() or mbedtls_aes_setkey_dec() must be called | 
|  | 261 | *         before the first call to this API with the same context. | 
| Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 262 | * | 
| Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 263 | * \note   This function operates on aligned blocks, that is, the input size | 
|  | 264 | *         must be a multiple of the AES block size of 16 Bytes. | 
|  | 265 | * | 
|  | 266 | * \note   Upon exit, the content of the IV is updated so that you can | 
|  | 267 | *         call the same function again on the next | 
|  | 268 | *         block(s) of data and get the same result as if it was | 
|  | 269 | *         encrypted in one call. This allows a "streaming" usage. | 
|  | 270 | *         If you need to retain the contents of the IV, you should | 
|  | 271 | *         either save it manually or use the cipher module instead. | 
|  | 272 | * | 
|  | 273 | * | 
|  | 274 | * \param ctx      The AES context to use for encryption or decryption. | 
|  | 275 | * \param mode     The AES operation: #MBEDTLS_AES_ENCRYPT or | 
|  | 276 | *                 #MBEDTLS_AES_DECRYPT. | 
|  | 277 | * \param length   The length of the input data in Bytes. This must be a | 
|  | 278 | *                 multiple of the block size (16 Bytes). | 
|  | 279 | * \param iv       Initialization vector (updated after use). | 
|  | 280 | * \param input    The buffer holding the input data. | 
|  | 281 | * \param output   The buffer holding the output data. | 
|  | 282 | * | 
| Rose Zadik | 5ad7aea | 2018-03-26 12:00:09 +0100 | [diff] [blame] | 283 | * \return         \c 0 on success. | 
|  | 284 | * \return         #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH | 
| Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 285 | *                 on failure. | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 286 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 287 | int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx, | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 288 | int mode, | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 289 | size_t length, | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 290 | unsigned char iv[16], | 
| Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 291 | const unsigned char *input, | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 292 | unsigned char *output ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 293 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 294 |  | 
| Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 295 | #if defined(MBEDTLS_CIPHER_MODE_XTS) | 
|  | 296 | /** | 
| Jaeden Amero | 9366feb | 2018-05-29 18:55:17 +0100 | [diff] [blame] | 297 | * \brief      This function performs an AES-XTS encryption or decryption | 
|  | 298 | *             operation for an entire XTS data unit. | 
| Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 299 | * | 
| Jaeden Amero | 9366feb | 2018-05-29 18:55:17 +0100 | [diff] [blame] | 300 | *             AES-XTS encrypts or decrypts blocks based on their location as | 
|  | 301 | *             defined by a data unit number. The data unit number must be | 
| Jaeden Amero | cd9fc5e | 2018-05-30 15:23:24 +0100 | [diff] [blame] | 302 | *             provided by \p data_unit. | 
| Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 303 | * | 
| Jaeden Amero | 0a8b020 | 2018-05-30 15:36:06 +0100 | [diff] [blame] | 304 | *             NIST SP 800-38E limits the maximum size of a data unit to 2^20 | 
|  | 305 | *             AES blocks. If the data unit is larger than this, this function | 
|  | 306 | *             returns #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH. | 
|  | 307 | * | 
| Jaeden Amero | 9366feb | 2018-05-29 18:55:17 +0100 | [diff] [blame] | 308 | * \param ctx          The AES XTS context to use for AES XTS operations. | 
|  | 309 | * \param mode         The AES operation: #MBEDTLS_AES_ENCRYPT or | 
|  | 310 | *                     #MBEDTLS_AES_DECRYPT. | 
| Jaeden Amero | 0a8b020 | 2018-05-30 15:36:06 +0100 | [diff] [blame] | 311 | * \param length       The length of a data unit in bytes. This can be any | 
|  | 312 | *                     length between 16 bytes and 2^24 bytes inclusive | 
|  | 313 | *                     (between 1 and 2^20 block cipher blocks). | 
| Jaeden Amero | cd9fc5e | 2018-05-30 15:23:24 +0100 | [diff] [blame] | 314 | * \param data_unit    The address of the data unit encoded as an array of 16 | 
| Jaeden Amero | 9366feb | 2018-05-29 18:55:17 +0100 | [diff] [blame] | 315 | *                     bytes in little-endian format. For disk encryption, this | 
|  | 316 | *                     is typically the index of the block device sector that | 
|  | 317 | *                     contains the data. | 
|  | 318 | * \param input        The buffer holding the input data (which is an entire | 
|  | 319 | *                     data unit). This function reads \p length bytes from \p | 
|  | 320 | *                     input. | 
|  | 321 | * \param output       The buffer holding the output data (which is an entire | 
|  | 322 | *                     data unit). This function writes \p length bytes to \p | 
|  | 323 | *                     output. | 
| Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 324 | * | 
| Jaeden Amero | 9366feb | 2018-05-29 18:55:17 +0100 | [diff] [blame] | 325 | * \return             \c 0 on success. | 
|  | 326 | * \return             #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH if \p length is | 
| Jaeden Amero | 0a8b020 | 2018-05-30 15:36:06 +0100 | [diff] [blame] | 327 | *                     smaller than an AES block in size (16 bytes) or if \p | 
|  | 328 | *                     length is larger than 2^20 blocks (16 MiB). | 
| Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 329 | */ | 
| Jaeden Amero | 9366feb | 2018-05-29 18:55:17 +0100 | [diff] [blame] | 330 | int mbedtls_aes_crypt_xts( mbedtls_aes_xts_context *ctx, | 
|  | 331 | int mode, | 
| Jaeden Amero | 5162b93 | 2018-05-29 12:55:24 +0100 | [diff] [blame] | 332 | size_t length, | 
| Jaeden Amero | cd9fc5e | 2018-05-30 15:23:24 +0100 | [diff] [blame] | 333 | const unsigned char data_unit[16], | 
| Jaeden Amero | 9366feb | 2018-05-29 18:55:17 +0100 | [diff] [blame] | 334 | const unsigned char *input, | 
|  | 335 | unsigned char *output ); | 
| Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 336 | #endif /* MBEDTLS_CIPHER_MODE_XTS */ | 
|  | 337 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 338 | #if defined(MBEDTLS_CIPHER_MODE_CFB) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 339 | /** | 
| Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 340 | * \brief This function performs an AES-CFB128 encryption or decryption | 
|  | 341 | *        operation. | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 342 | * | 
| Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 343 | *        It performs the operation defined in the \p mode | 
|  | 344 | *        parameter (encrypt or decrypt), on the input data buffer | 
|  | 345 | *        defined in the \p input parameter. | 
| Paul Bakker | ca6f3e2 | 2011-10-06 13:11:08 +0000 | [diff] [blame] | 346 | * | 
| Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 347 | *        For CFB, you must set up the context with mbedtls_aes_setkey_enc(), | 
|  | 348 | *        regardless of whether you are performing an encryption or decryption | 
|  | 349 | *        operation, that is, regardless of the \p mode parameter. This is | 
|  | 350 | *        because CFB mode uses the same key schedule for encryption and | 
|  | 351 | *        decryption. | 
| Manuel Pégourié-Gonnard | 2be147a | 2015-01-23 16:19:47 +0000 | [diff] [blame] | 352 | * | 
| Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 353 | * \note  Upon exit, the content of the IV is updated so that you can | 
|  | 354 | *        call the same function again on the next | 
|  | 355 | *        block(s) of data and get the same result as if it was | 
|  | 356 | *        encrypted in one call. This allows a "streaming" usage. | 
|  | 357 | *        If you need to retain the contents of the | 
|  | 358 | *        IV, you must either save it manually or use the cipher | 
|  | 359 | *        module instead. | 
| Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 360 | * | 
| Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 361 | * | 
|  | 362 | * \param ctx      The AES context to use for encryption or decryption. | 
|  | 363 | * \param mode     The AES operation: #MBEDTLS_AES_ENCRYPT or | 
|  | 364 | *                 #MBEDTLS_AES_DECRYPT. | 
|  | 365 | * \param length   The length of the input data. | 
|  | 366 | * \param iv_off   The offset in IV (updated after use). | 
|  | 367 | * \param iv       The initialization vector (updated after use). | 
|  | 368 | * \param input    The buffer holding the input data. | 
|  | 369 | * \param output   The buffer holding the output data. | 
|  | 370 | * | 
|  | 371 | * \return         \c 0 on success. | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 372 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 373 | int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx, | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 374 | int mode, | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 375 | size_t length, | 
| Paul Bakker | 1ef71df | 2011-06-09 14:14:58 +0000 | [diff] [blame] | 376 | size_t *iv_off, | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 377 | unsigned char iv[16], | 
| Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 378 | const unsigned char *input, | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 379 | unsigned char *output ); | 
|  | 380 |  | 
| Paul Bakker | 9a73632 | 2012-11-14 12:39:52 +0000 | [diff] [blame] | 381 | /** | 
| Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 382 | * \brief This function performs an AES-CFB8 encryption or decryption | 
|  | 383 | *        operation. | 
| Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 384 | * | 
| Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 385 | *        It performs the operation defined in the \p mode | 
|  | 386 | *        parameter (encrypt/decrypt), on the input data buffer defined | 
|  | 387 | *        in the \p input parameter. | 
| Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 388 | * | 
| Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 389 | *        Due to the nature of CFB, you must use the same key schedule for | 
|  | 390 | *        both encryption and decryption operations. Therefore, you must | 
|  | 391 | *        use the context initialized with mbedtls_aes_setkey_enc() for | 
|  | 392 | *        both #MBEDTLS_AES_ENCRYPT and #MBEDTLS_AES_DECRYPT. | 
| Manuel Pégourié-Gonnard | 2be147a | 2015-01-23 16:19:47 +0000 | [diff] [blame] | 393 | * | 
| Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 394 | * \note  Upon exit, the content of the IV is updated so that you can | 
|  | 395 | *        call the same function again on the next | 
|  | 396 | *        block(s) of data and get the same result as if it was | 
|  | 397 | *        encrypted in one call. This allows a "streaming" usage. | 
|  | 398 | *        If you need to retain the contents of the | 
|  | 399 | *        IV, you should either save it manually or use the cipher | 
|  | 400 | *        module instead. | 
| Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 401 | * | 
| Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 402 | * | 
|  | 403 | * \param ctx      The AES context to use for encryption or decryption. | 
|  | 404 | * \param mode     The AES operation: #MBEDTLS_AES_ENCRYPT or | 
|  | 405 | *                 #MBEDTLS_AES_DECRYPT | 
|  | 406 | * \param length   The length of the input data. | 
|  | 407 | * \param iv       The initialization vector (updated after use). | 
|  | 408 | * \param input    The buffer holding the input data. | 
|  | 409 | * \param output   The buffer holding the output data. | 
|  | 410 | * | 
|  | 411 | * \return         \c 0 on success. | 
| Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 412 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 413 | int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx, | 
| Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 414 | int mode, | 
|  | 415 | size_t length, | 
|  | 416 | unsigned char iv[16], | 
|  | 417 | const unsigned char *input, | 
|  | 418 | unsigned char *output ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 419 | #endif /*MBEDTLS_CIPHER_MODE_CFB */ | 
| Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 420 |  | 
| Simon Butcher | 76a5b22 | 2018-04-22 22:57:27 +0100 | [diff] [blame] | 421 | #if defined(MBEDTLS_CIPHER_MODE_OFB) | 
|  | 422 | /** | 
| Simon Butcher | 5db1362 | 2018-06-04 22:11:25 +0100 | [diff] [blame] | 423 | * \brief       This function performs an AES-OFB (Output Feedback Mode) | 
|  | 424 | *              encryption or decryption operation. | 
| Simon Butcher | 76a5b22 | 2018-04-22 22:57:27 +0100 | [diff] [blame] | 425 | * | 
| Simon Butcher | 5db1362 | 2018-06-04 22:11:25 +0100 | [diff] [blame] | 426 | *              For OFB, you must set up the context with | 
|  | 427 | *              mbedtls_aes_setkey_enc(), regardless of whether you are | 
|  | 428 | *              performing an encryption or decryption operation. This is | 
|  | 429 | *              because OFB mode uses the same key schedule for encryption and | 
|  | 430 | *              decryption. | 
| Simon Butcher | 76a5b22 | 2018-04-22 22:57:27 +0100 | [diff] [blame] | 431 | * | 
| Simon Butcher | 5db1362 | 2018-06-04 22:11:25 +0100 | [diff] [blame] | 432 | *              The OFB operation is identical for encryption or decryption, | 
|  | 433 | *              therefore no operation mode needs to be specified. | 
| Simon Butcher | 76a5b22 | 2018-04-22 22:57:27 +0100 | [diff] [blame] | 434 | * | 
| Simon Butcher | 5db1362 | 2018-06-04 22:11:25 +0100 | [diff] [blame] | 435 | * \note        Upon exit, the content of iv, the Initialisation Vector, is | 
|  | 436 | *              updated so that you can call the same function again on the next | 
|  | 437 | *              block(s) of data and get the same result as if it was encrypted | 
|  | 438 | *              in one call. This allows a "streaming" usage, by initialising | 
|  | 439 | *              iv_off to 0 before the first call, and preserving its value | 
|  | 440 | *              between calls. | 
| Simon Butcher | 968646c | 2018-06-02 18:27:04 +0100 | [diff] [blame] | 441 | * | 
| Simon Butcher | 5db1362 | 2018-06-04 22:11:25 +0100 | [diff] [blame] | 442 | *              For non-streaming use, the iv should be initialised on each call | 
|  | 443 | *              to a unique value, and iv_off set to 0 on each call. | 
| Simon Butcher | 968646c | 2018-06-02 18:27:04 +0100 | [diff] [blame] | 444 | * | 
| Simon Butcher | 5db1362 | 2018-06-04 22:11:25 +0100 | [diff] [blame] | 445 | *              If you need to retain the contents of the initialisation vector, | 
|  | 446 | *              you must either save it manually or use the cipher module | 
|  | 447 | *              instead. | 
| Simon Butcher | 968646c | 2018-06-02 18:27:04 +0100 | [diff] [blame] | 448 | * | 
| Jaeden Amero | cb2c935 | 2018-06-08 10:34:08 +0100 | [diff] [blame] | 449 | * \warning     For the OFB mode, the initialisation vector must be unique | 
|  | 450 | *              every encryption operation. Reuse of an initialisation vector | 
|  | 451 | *              will compromise security. | 
| Simon Butcher | 76a5b22 | 2018-04-22 22:57:27 +0100 | [diff] [blame] | 452 | * | 
|  | 453 | * \param ctx      The AES context to use for encryption or decryption. | 
|  | 454 | * \param length   The length of the input data. | 
|  | 455 | * \param iv_off   The offset in IV (updated after use). | 
|  | 456 | * \param iv       The initialization vector (updated after use). | 
|  | 457 | * \param input    The buffer holding the input data. | 
|  | 458 | * \param output   The buffer holding the output data. | 
|  | 459 | * | 
|  | 460 | * \return         \c 0 on success. | 
|  | 461 | */ | 
|  | 462 | int mbedtls_aes_crypt_ofb( mbedtls_aes_context *ctx, | 
|  | 463 | size_t length, | 
|  | 464 | size_t *iv_off, | 
|  | 465 | unsigned char iv[16], | 
|  | 466 | const unsigned char *input, | 
|  | 467 | unsigned char *output ); | 
|  | 468 |  | 
|  | 469 | #endif /* MBEDTLS_CIPHER_MODE_OFB */ | 
|  | 470 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 471 | #if defined(MBEDTLS_CIPHER_MODE_CTR) | 
| Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 472 | /** | 
| Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 473 | * \brief      This function performs an AES-CTR encryption or decryption | 
|  | 474 | *             operation. | 
| Paul Bakker | b6ecaf5 | 2011-04-19 14:29:23 +0000 | [diff] [blame] | 475 | * | 
| Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 476 | *             This function performs the operation defined in the \p mode | 
|  | 477 | *             parameter (encrypt/decrypt), on the input data buffer | 
|  | 478 | *             defined in the \p input parameter. | 
| Paul Bakker | b6ecaf5 | 2011-04-19 14:29:23 +0000 | [diff] [blame] | 479 | * | 
| Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 480 | *             Due to the nature of CTR, you must use the same key schedule | 
|  | 481 | *             for both encryption and decryption operations. Therefore, you | 
|  | 482 | *             must use the context initialized with mbedtls_aes_setkey_enc() | 
|  | 483 | *             for both #MBEDTLS_AES_ENCRYPT and #MBEDTLS_AES_DECRYPT. | 
| Paul Bakker | ca6f3e2 | 2011-10-06 13:11:08 +0000 | [diff] [blame] | 484 | * | 
| Manuel Pégourié-Gonnard | 22997b7 | 2018-02-28 12:29:41 +0100 | [diff] [blame] | 485 | * \warning    You must never reuse a nonce value with the same key. Doing so | 
|  | 486 | *             would void the encryption for the two messages encrypted with | 
|  | 487 | *             the same nonce and key. | 
|  | 488 | * | 
|  | 489 | *             There are two common strategies for managing nonces with CTR: | 
|  | 490 | * | 
| Manuel Pégourié-Gonnard | 4f24e95 | 2018-05-24 11:59:30 +0200 | [diff] [blame] | 491 | *             1. You can handle everything as a single message processed over | 
|  | 492 | *             successive calls to this function. In that case, you want to | 
|  | 493 | *             set \p nonce_counter and \p nc_off to 0 for the first call, and | 
|  | 494 | *             then preserve the values of \p nonce_counter, \p nc_off and \p | 
|  | 495 | *             stream_block across calls to this function as they will be | 
|  | 496 | *             updated by this function. | 
| Manuel Pégourié-Gonnard | 22997b7 | 2018-02-28 12:29:41 +0100 | [diff] [blame] | 497 | * | 
| Manuel Pégourié-Gonnard | 4f24e95 | 2018-05-24 11:59:30 +0200 | [diff] [blame] | 498 | *             With this strategy, you must not encrypt more than 2**128 | 
|  | 499 | *             blocks of data with the same key. | 
|  | 500 | * | 
|  | 501 | *             2. You can encrypt separate messages by dividing the \p | 
|  | 502 | *             nonce_counter buffer in two areas: the first one used for a | 
|  | 503 | *             per-message nonce, handled by yourself, and the second one | 
|  | 504 | *             updated by this function internally. | 
|  | 505 | * | 
|  | 506 | *             For example, you might reserve the first 12 bytes for the | 
|  | 507 | *             per-message nonce, and the last 4 bytes for internal use. In that | 
|  | 508 | *             case, before calling this function on a new message you need to | 
|  | 509 | *             set the first 12 bytes of \p nonce_counter to your chosen nonce | 
|  | 510 | *             value, the last 4 to 0, and \p nc_off to 0 (which will cause \p | 
|  | 511 | *             stream_block to be ignored). That way, you can encrypt at most | 
|  | 512 | *             2**96 messages of up to 2**32 blocks each with the same key. | 
|  | 513 | * | 
|  | 514 | *             The per-message nonce (or information sufficient to reconstruct | 
|  | 515 | *             it) needs to be communicated with the ciphertext and must be unique. | 
|  | 516 | *             The recommended way to ensure uniqueness is to use a message | 
|  | 517 | *             counter. An alternative is to generate random nonces, but this | 
|  | 518 | *             limits the number of messages that can be securely encrypted: | 
|  | 519 | *             for example, with 96-bit random nonces, you should not encrypt | 
|  | 520 | *             more than 2**32 messages with the same key. | 
|  | 521 | * | 
|  | 522 | *             Note that for both stategies, sizes are measured in blocks and | 
|  | 523 | *             that an AES block is 16 bytes. | 
| Paul Bakker | b6ecaf5 | 2011-04-19 14:29:23 +0000 | [diff] [blame] | 524 | * | 
| Manuel Pégourié-Gonnard | fa0c47d | 2018-05-24 19:02:06 +0200 | [diff] [blame] | 525 | * \warning    Upon return, \p stream_block contains sensitive data. Its | 
|  | 526 | *             content must not be written to insecure storage and should be | 
|  | 527 | *             securely discarded as soon as it's no longer needed. | 
| Paul Bakker | b6ecaf5 | 2011-04-19 14:29:23 +0000 | [diff] [blame] | 528 | * | 
| Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 529 | * \param ctx              The AES context to use for encryption or decryption. | 
|  | 530 | * \param length           The length of the input data. | 
|  | 531 | * \param nc_off           The offset in the current \p stream_block, for | 
|  | 532 | *                         resuming within the current cipher stream. The | 
|  | 533 | *                         offset pointer should be 0 at the start of a stream. | 
|  | 534 | * \param nonce_counter    The 128-bit nonce and counter. | 
|  | 535 | * \param stream_block     The saved stream block for resuming. This is | 
|  | 536 | *                         overwritten by the function. | 
|  | 537 | * \param input            The buffer holding the input data. | 
|  | 538 | * \param output           The buffer holding the output data. | 
|  | 539 | * | 
| Rose Zadik | 5ad7aea | 2018-03-26 12:00:09 +0100 | [diff] [blame] | 540 | * \return                 \c 0 on success. | 
| Paul Bakker | b6ecaf5 | 2011-04-19 14:29:23 +0000 | [diff] [blame] | 541 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 542 | int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx, | 
| Paul Bakker | 1ef71df | 2011-06-09 14:14:58 +0000 | [diff] [blame] | 543 | size_t length, | 
|  | 544 | size_t *nc_off, | 
| Paul Bakker | b6ecaf5 | 2011-04-19 14:29:23 +0000 | [diff] [blame] | 545 | unsigned char nonce_counter[16], | 
|  | 546 | unsigned char stream_block[16], | 
|  | 547 | const unsigned char *input, | 
|  | 548 | unsigned char *output ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 549 | #endif /* MBEDTLS_CIPHER_MODE_CTR */ | 
| Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 550 |  | 
| Manuel Pégourié-Gonnard | 31993f2 | 2015-05-12 15:41:08 +0200 | [diff] [blame] | 551 | /** | 
| Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 552 | * \brief           Internal AES block encryption function. This is only | 
|  | 553 | *                  exposed to allow overriding it using | 
|  | 554 | *                  \c MBEDTLS_AES_ENCRYPT_ALT. | 
| Manuel Pégourié-Gonnard | 31993f2 | 2015-05-12 15:41:08 +0200 | [diff] [blame] | 555 | * | 
| Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 556 | * \param ctx       The AES context to use for encryption. | 
|  | 557 | * \param input     The plaintext block. | 
|  | 558 | * \param output    The output (ciphertext) block. | 
| Andres AG | f5bf718 | 2017-03-03 14:09:56 +0000 | [diff] [blame] | 559 | * | 
| Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 560 | * \return          \c 0 on success. | 
| Manuel Pégourié-Gonnard | 31993f2 | 2015-05-12 15:41:08 +0200 | [diff] [blame] | 561 | */ | 
| Andres AG | f5bf718 | 2017-03-03 14:09:56 +0000 | [diff] [blame] | 562 | int mbedtls_internal_aes_encrypt( mbedtls_aes_context *ctx, | 
|  | 563 | const unsigned char input[16], | 
|  | 564 | unsigned char output[16] ); | 
| Manuel Pégourié-Gonnard | 31993f2 | 2015-05-12 15:41:08 +0200 | [diff] [blame] | 565 |  | 
|  | 566 | /** | 
| Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 567 | * \brief           Internal AES block decryption function. This is only | 
|  | 568 | *                  exposed to allow overriding it using see | 
|  | 569 | *                  \c MBEDTLS_AES_DECRYPT_ALT. | 
| Manuel Pégourié-Gonnard | 31993f2 | 2015-05-12 15:41:08 +0200 | [diff] [blame] | 570 | * | 
| Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 571 | * \param ctx       The AES context to use for decryption. | 
|  | 572 | * \param input     The ciphertext block. | 
|  | 573 | * \param output    The output (plaintext) block. | 
| Andres AG | f5bf718 | 2017-03-03 14:09:56 +0000 | [diff] [blame] | 574 | * | 
| Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 575 | * \return          \c 0 on success. | 
| Manuel Pégourié-Gonnard | 31993f2 | 2015-05-12 15:41:08 +0200 | [diff] [blame] | 576 | */ | 
| Andres AG | f5bf718 | 2017-03-03 14:09:56 +0000 | [diff] [blame] | 577 | int mbedtls_internal_aes_decrypt( mbedtls_aes_context *ctx, | 
|  | 578 | const unsigned char input[16], | 
|  | 579 | unsigned char output[16] ); | 
|  | 580 |  | 
|  | 581 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) | 
|  | 582 | #if defined(MBEDTLS_DEPRECATED_WARNING) | 
|  | 583 | #define MBEDTLS_DEPRECATED      __attribute__((deprecated)) | 
|  | 584 | #else | 
|  | 585 | #define MBEDTLS_DEPRECATED | 
|  | 586 | #endif | 
|  | 587 | /** | 
| Hanno Becker | ca1cdb2 | 2017-07-20 09:50:59 +0100 | [diff] [blame] | 588 | * \brief           Deprecated internal AES block encryption function | 
|  | 589 | *                  without return value. | 
| Andres AG | f5bf718 | 2017-03-03 14:09:56 +0000 | [diff] [blame] | 590 | * | 
| Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 591 | * \deprecated      Superseded by mbedtls_aes_encrypt_ext() in 2.5.0. | 
| Andres AG | f5bf718 | 2017-03-03 14:09:56 +0000 | [diff] [blame] | 592 | * | 
| Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 593 | * \param ctx       The AES context to use for encryption. | 
|  | 594 | * \param input     Plaintext block. | 
|  | 595 | * \param output    Output (ciphertext) block. | 
| Andres AG | f5bf718 | 2017-03-03 14:09:56 +0000 | [diff] [blame] | 596 | */ | 
| Hanno Becker | bedc205 | 2017-06-26 12:46:56 +0100 | [diff] [blame] | 597 | MBEDTLS_DEPRECATED void mbedtls_aes_encrypt( mbedtls_aes_context *ctx, | 
|  | 598 | const unsigned char input[16], | 
|  | 599 | unsigned char output[16] ); | 
| Andres AG | f5bf718 | 2017-03-03 14:09:56 +0000 | [diff] [blame] | 600 |  | 
|  | 601 | /** | 
| Hanno Becker | ca1cdb2 | 2017-07-20 09:50:59 +0100 | [diff] [blame] | 602 | * \brief           Deprecated internal AES block decryption function | 
|  | 603 | *                  without return value. | 
| Andres AG | f5bf718 | 2017-03-03 14:09:56 +0000 | [diff] [blame] | 604 | * | 
| Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 605 | * \deprecated      Superseded by mbedtls_aes_decrypt_ext() in 2.5.0. | 
| Andres AG | f5bf718 | 2017-03-03 14:09:56 +0000 | [diff] [blame] | 606 | * | 
| Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 607 | * \param ctx       The AES context to use for decryption. | 
|  | 608 | * \param input     Ciphertext block. | 
|  | 609 | * \param output    Output (plaintext) block. | 
| Andres AG | f5bf718 | 2017-03-03 14:09:56 +0000 | [diff] [blame] | 610 | */ | 
| Hanno Becker | bedc205 | 2017-06-26 12:46:56 +0100 | [diff] [blame] | 611 | MBEDTLS_DEPRECATED void mbedtls_aes_decrypt( mbedtls_aes_context *ctx, | 
|  | 612 | const unsigned char input[16], | 
|  | 613 | unsigned char output[16] ); | 
| Andres AG | f5bf718 | 2017-03-03 14:09:56 +0000 | [diff] [blame] | 614 |  | 
|  | 615 | #undef MBEDTLS_DEPRECATED | 
|  | 616 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ | 
| Manuel Pégourié-Gonnard | 31993f2 | 2015-05-12 15:41:08 +0200 | [diff] [blame] | 617 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 618 | /** | 
| Rose Zadik | 7f44127 | 2018-01-22 11:48:23 +0000 | [diff] [blame] | 619 | * \brief          Checkup routine. | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 620 | * | 
| Rose Zadik | 5ad7aea | 2018-03-26 12:00:09 +0100 | [diff] [blame] | 621 | * \return         \c 0 on success. | 
|  | 622 | * \return         \c 1 on failure. | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 623 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 624 | int mbedtls_aes_self_test( int verbose ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 625 |  | 
|  | 626 | #ifdef __cplusplus | 
|  | 627 | } | 
|  | 628 | #endif | 
|  | 629 |  | 
|  | 630 | #endif /* aes.h */ |