mbed TLS Build

Dependents:   Slave-prot-prod

Committer:
markrad
Date:
Thu Jan 05 00:18:44 2017 +0000
Revision:
0:cdf462088d13
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
markrad 0:cdf462088d13 1 /**
markrad 0:cdf462088d13 2 * \file aes.h
markrad 0:cdf462088d13 3 *
markrad 0:cdf462088d13 4 * \brief AES block cipher
markrad 0:cdf462088d13 5 *
markrad 0:cdf462088d13 6 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
markrad 0:cdf462088d13 7 * SPDX-License-Identifier: Apache-2.0
markrad 0:cdf462088d13 8 *
markrad 0:cdf462088d13 9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
markrad 0:cdf462088d13 10 * not use this file except in compliance with the License.
markrad 0:cdf462088d13 11 * You may obtain a copy of the License at
markrad 0:cdf462088d13 12 *
markrad 0:cdf462088d13 13 * http://www.apache.org/licenses/LICENSE-2.0
markrad 0:cdf462088d13 14 *
markrad 0:cdf462088d13 15 * Unless required by applicable law or agreed to in writing, software
markrad 0:cdf462088d13 16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
markrad 0:cdf462088d13 17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
markrad 0:cdf462088d13 18 * See the License for the specific language governing permissions and
markrad 0:cdf462088d13 19 * limitations under the License.
markrad 0:cdf462088d13 20 *
markrad 0:cdf462088d13 21 * This file is part of mbed TLS (https://tls.mbed.org)
markrad 0:cdf462088d13 22 */
markrad 0:cdf462088d13 23 #ifndef MBEDTLS_AES_H
markrad 0:cdf462088d13 24 #define MBEDTLS_AES_H
markrad 0:cdf462088d13 25
markrad 0:cdf462088d13 26 #if !defined(MBEDTLS_CONFIG_FILE)
markrad 0:cdf462088d13 27 #include "config.h"
markrad 0:cdf462088d13 28 #else
markrad 0:cdf462088d13 29 #include MBEDTLS_CONFIG_FILE
markrad 0:cdf462088d13 30 #endif
markrad 0:cdf462088d13 31
markrad 0:cdf462088d13 32 #include <stddef.h>
markrad 0:cdf462088d13 33 #include <stdint.h>
markrad 0:cdf462088d13 34
markrad 0:cdf462088d13 35 /* padlock.c and aesni.c rely on these values! */
markrad 0:cdf462088d13 36 #define MBEDTLS_AES_ENCRYPT 1
markrad 0:cdf462088d13 37 #define MBEDTLS_AES_DECRYPT 0
markrad 0:cdf462088d13 38
markrad 0:cdf462088d13 39 #define MBEDTLS_ERR_AES_INVALID_KEY_LENGTH -0x0020 /**< Invalid key length. */
markrad 0:cdf462088d13 40 #define MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH -0x0022 /**< Invalid data input length. */
markrad 0:cdf462088d13 41
markrad 0:cdf462088d13 42 #if !defined(MBEDTLS_AES_ALT)
markrad 0:cdf462088d13 43 // Regular implementation
markrad 0:cdf462088d13 44 //
markrad 0:cdf462088d13 45
markrad 0:cdf462088d13 46 #ifdef __cplusplus
markrad 0:cdf462088d13 47 extern "C" {
markrad 0:cdf462088d13 48 #endif
markrad 0:cdf462088d13 49
markrad 0:cdf462088d13 50 /**
markrad 0:cdf462088d13 51 * \brief AES context structure
markrad 0:cdf462088d13 52 *
markrad 0:cdf462088d13 53 * \note buf is able to hold 32 extra bytes, which can be used:
markrad 0:cdf462088d13 54 * - for alignment purposes if VIA padlock is used, and/or
markrad 0:cdf462088d13 55 * - to simplify key expansion in the 256-bit case by
markrad 0:cdf462088d13 56 * generating an extra round key
markrad 0:cdf462088d13 57 */
markrad 0:cdf462088d13 58 typedef struct
markrad 0:cdf462088d13 59 {
markrad 0:cdf462088d13 60 int nr; /*!< number of rounds */
markrad 0:cdf462088d13 61 uint32_t *rk; /*!< AES round keys */
markrad 0:cdf462088d13 62 uint32_t buf[68]; /*!< unaligned data */
markrad 0:cdf462088d13 63 }
markrad 0:cdf462088d13 64 mbedtls_aes_context;
markrad 0:cdf462088d13 65
markrad 0:cdf462088d13 66 /**
markrad 0:cdf462088d13 67 * \brief Initialize AES context
markrad 0:cdf462088d13 68 *
markrad 0:cdf462088d13 69 * \param ctx AES context to be initialized
markrad 0:cdf462088d13 70 */
markrad 0:cdf462088d13 71 void mbedtls_aes_init( mbedtls_aes_context *ctx );
markrad 0:cdf462088d13 72
markrad 0:cdf462088d13 73 /**
markrad 0:cdf462088d13 74 * \brief Clear AES context
markrad 0:cdf462088d13 75 *
markrad 0:cdf462088d13 76 * \param ctx AES context to be cleared
markrad 0:cdf462088d13 77 */
markrad 0:cdf462088d13 78 void mbedtls_aes_free( mbedtls_aes_context *ctx );
markrad 0:cdf462088d13 79
markrad 0:cdf462088d13 80 /**
markrad 0:cdf462088d13 81 * \brief AES key schedule (encryption)
markrad 0:cdf462088d13 82 *
markrad 0:cdf462088d13 83 * \param ctx AES context to be initialized
markrad 0:cdf462088d13 84 * \param key encryption key
markrad 0:cdf462088d13 85 * \param keybits must be 128, 192 or 256
markrad 0:cdf462088d13 86 *
markrad 0:cdf462088d13 87 * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
markrad 0:cdf462088d13 88 */
markrad 0:cdf462088d13 89 int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key,
markrad 0:cdf462088d13 90 unsigned int keybits );
markrad 0:cdf462088d13 91
markrad 0:cdf462088d13 92 /**
markrad 0:cdf462088d13 93 * \brief AES key schedule (decryption)
markrad 0:cdf462088d13 94 *
markrad 0:cdf462088d13 95 * \param ctx AES context to be initialized
markrad 0:cdf462088d13 96 * \param key decryption key
markrad 0:cdf462088d13 97 * \param keybits must be 128, 192 or 256
markrad 0:cdf462088d13 98 *
markrad 0:cdf462088d13 99 * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
markrad 0:cdf462088d13 100 */
markrad 0:cdf462088d13 101 int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx, const unsigned char *key,
markrad 0:cdf462088d13 102 unsigned int keybits );
markrad 0:cdf462088d13 103
markrad 0:cdf462088d13 104 /**
markrad 0:cdf462088d13 105 * \brief AES-ECB block encryption/decryption
markrad 0:cdf462088d13 106 *
markrad 0:cdf462088d13 107 * \param ctx AES context
markrad 0:cdf462088d13 108 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
markrad 0:cdf462088d13 109 * \param input 16-byte input block
markrad 0:cdf462088d13 110 * \param output 16-byte output block
markrad 0:cdf462088d13 111 *
markrad 0:cdf462088d13 112 * \return 0 if successful
markrad 0:cdf462088d13 113 */
markrad 0:cdf462088d13 114 int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx,
markrad 0:cdf462088d13 115 int mode,
markrad 0:cdf462088d13 116 const unsigned char input[16],
markrad 0:cdf462088d13 117 unsigned char output[16] );
markrad 0:cdf462088d13 118
markrad 0:cdf462088d13 119 #if defined(MBEDTLS_CIPHER_MODE_CBC)
markrad 0:cdf462088d13 120 /**
markrad 0:cdf462088d13 121 * \brief AES-CBC buffer encryption/decryption
markrad 0:cdf462088d13 122 * Length should be a multiple of the block
markrad 0:cdf462088d13 123 * size (16 bytes)
markrad 0:cdf462088d13 124 *
markrad 0:cdf462088d13 125 * \note Upon exit, the content of the IV is updated so that you can
markrad 0:cdf462088d13 126 * call the function same function again on the following
markrad 0:cdf462088d13 127 * block(s) of data and get the same result as if it was
markrad 0:cdf462088d13 128 * encrypted in one call. This allows a "streaming" usage.
markrad 0:cdf462088d13 129 * If on the other hand you need to retain the contents of the
markrad 0:cdf462088d13 130 * IV, you should either save it manually or use the cipher
markrad 0:cdf462088d13 131 * module instead.
markrad 0:cdf462088d13 132 *
markrad 0:cdf462088d13 133 * \param ctx AES context
markrad 0:cdf462088d13 134 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
markrad 0:cdf462088d13 135 * \param length length of the input data
markrad 0:cdf462088d13 136 * \param iv initialization vector (updated after use)
markrad 0:cdf462088d13 137 * \param input buffer holding the input data
markrad 0:cdf462088d13 138 * \param output buffer holding the output data
markrad 0:cdf462088d13 139 *
markrad 0:cdf462088d13 140 * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH
markrad 0:cdf462088d13 141 */
markrad 0:cdf462088d13 142 int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
markrad 0:cdf462088d13 143 int mode,
markrad 0:cdf462088d13 144 size_t length,
markrad 0:cdf462088d13 145 unsigned char iv[16],
markrad 0:cdf462088d13 146 const unsigned char *input,
markrad 0:cdf462088d13 147 unsigned char *output );
markrad 0:cdf462088d13 148 #endif /* MBEDTLS_CIPHER_MODE_CBC */
markrad 0:cdf462088d13 149
markrad 0:cdf462088d13 150 #if defined(MBEDTLS_CIPHER_MODE_CFB)
markrad 0:cdf462088d13 151 /**
markrad 0:cdf462088d13 152 * \brief AES-CFB128 buffer encryption/decryption.
markrad 0:cdf462088d13 153 *
markrad 0:cdf462088d13 154 * Note: Due to the nature of CFB you should use the same key schedule for
markrad 0:cdf462088d13 155 * both encryption and decryption. So a context initialized with
markrad 0:cdf462088d13 156 * mbedtls_aes_setkey_enc() for both MBEDTLS_AES_ENCRYPT and MBEDTLS_AES_DECRYPT.
markrad 0:cdf462088d13 157 *
markrad 0:cdf462088d13 158 * \note Upon exit, the content of the IV is updated so that you can
markrad 0:cdf462088d13 159 * call the function same function again on the following
markrad 0:cdf462088d13 160 * block(s) of data and get the same result as if it was
markrad 0:cdf462088d13 161 * encrypted in one call. This allows a "streaming" usage.
markrad 0:cdf462088d13 162 * If on the other hand you need to retain the contents of the
markrad 0:cdf462088d13 163 * IV, you should either save it manually or use the cipher
markrad 0:cdf462088d13 164 * module instead.
markrad 0:cdf462088d13 165 *
markrad 0:cdf462088d13 166 * \param ctx AES context
markrad 0:cdf462088d13 167 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
markrad 0:cdf462088d13 168 * \param length length of the input data
markrad 0:cdf462088d13 169 * \param iv_off offset in IV (updated after use)
markrad 0:cdf462088d13 170 * \param iv initialization vector (updated after use)
markrad 0:cdf462088d13 171 * \param input buffer holding the input data
markrad 0:cdf462088d13 172 * \param output buffer holding the output data
markrad 0:cdf462088d13 173 *
markrad 0:cdf462088d13 174 * \return 0 if successful
markrad 0:cdf462088d13 175 */
markrad 0:cdf462088d13 176 int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
markrad 0:cdf462088d13 177 int mode,
markrad 0:cdf462088d13 178 size_t length,
markrad 0:cdf462088d13 179 size_t *iv_off,
markrad 0:cdf462088d13 180 unsigned char iv[16],
markrad 0:cdf462088d13 181 const unsigned char *input,
markrad 0:cdf462088d13 182 unsigned char *output );
markrad 0:cdf462088d13 183
markrad 0:cdf462088d13 184 /**
markrad 0:cdf462088d13 185 * \brief AES-CFB8 buffer encryption/decryption.
markrad 0:cdf462088d13 186 *
markrad 0:cdf462088d13 187 * Note: Due to the nature of CFB you should use the same key schedule for
markrad 0:cdf462088d13 188 * both encryption and decryption. So a context initialized with
markrad 0:cdf462088d13 189 * mbedtls_aes_setkey_enc() for both MBEDTLS_AES_ENCRYPT and MBEDTLS_AES_DECRYPT.
markrad 0:cdf462088d13 190 *
markrad 0:cdf462088d13 191 * \note Upon exit, the content of the IV is updated so that you can
markrad 0:cdf462088d13 192 * call the function same function again on the following
markrad 0:cdf462088d13 193 * block(s) of data and get the same result as if it was
markrad 0:cdf462088d13 194 * encrypted in one call. This allows a "streaming" usage.
markrad 0:cdf462088d13 195 * If on the other hand you need to retain the contents of the
markrad 0:cdf462088d13 196 * IV, you should either save it manually or use the cipher
markrad 0:cdf462088d13 197 * module instead.
markrad 0:cdf462088d13 198 *
markrad 0:cdf462088d13 199 * \param ctx AES context
markrad 0:cdf462088d13 200 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
markrad 0:cdf462088d13 201 * \param length length of the input data
markrad 0:cdf462088d13 202 * \param iv initialization vector (updated after use)
markrad 0:cdf462088d13 203 * \param input buffer holding the input data
markrad 0:cdf462088d13 204 * \param output buffer holding the output data
markrad 0:cdf462088d13 205 *
markrad 0:cdf462088d13 206 * \return 0 if successful
markrad 0:cdf462088d13 207 */
markrad 0:cdf462088d13 208 int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx,
markrad 0:cdf462088d13 209 int mode,
markrad 0:cdf462088d13 210 size_t length,
markrad 0:cdf462088d13 211 unsigned char iv[16],
markrad 0:cdf462088d13 212 const unsigned char *input,
markrad 0:cdf462088d13 213 unsigned char *output );
markrad 0:cdf462088d13 214 #endif /*MBEDTLS_CIPHER_MODE_CFB */
markrad 0:cdf462088d13 215
markrad 0:cdf462088d13 216 #if defined(MBEDTLS_CIPHER_MODE_CTR)
markrad 0:cdf462088d13 217 /**
markrad 0:cdf462088d13 218 * \brief AES-CTR buffer encryption/decryption
markrad 0:cdf462088d13 219 *
markrad 0:cdf462088d13 220 * Warning: You have to keep the maximum use of your counter in mind!
markrad 0:cdf462088d13 221 *
markrad 0:cdf462088d13 222 * Note: Due to the nature of CTR you should use the same key schedule for
markrad 0:cdf462088d13 223 * both encryption and decryption. So a context initialized with
markrad 0:cdf462088d13 224 * mbedtls_aes_setkey_enc() for both MBEDTLS_AES_ENCRYPT and MBEDTLS_AES_DECRYPT.
markrad 0:cdf462088d13 225 *
markrad 0:cdf462088d13 226 * \param ctx AES context
markrad 0:cdf462088d13 227 * \param length The length of the data
markrad 0:cdf462088d13 228 * \param nc_off The offset in the current stream_block (for resuming
markrad 0:cdf462088d13 229 * within current cipher stream). The offset pointer to
markrad 0:cdf462088d13 230 * should be 0 at the start of a stream.
markrad 0:cdf462088d13 231 * \param nonce_counter The 128-bit nonce and counter.
markrad 0:cdf462088d13 232 * \param stream_block The saved stream-block for resuming. Is overwritten
markrad 0:cdf462088d13 233 * by the function.
markrad 0:cdf462088d13 234 * \param input The input data stream
markrad 0:cdf462088d13 235 * \param output The output data stream
markrad 0:cdf462088d13 236 *
markrad 0:cdf462088d13 237 * \return 0 if successful
markrad 0:cdf462088d13 238 */
markrad 0:cdf462088d13 239 int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
markrad 0:cdf462088d13 240 size_t length,
markrad 0:cdf462088d13 241 size_t *nc_off,
markrad 0:cdf462088d13 242 unsigned char nonce_counter[16],
markrad 0:cdf462088d13 243 unsigned char stream_block[16],
markrad 0:cdf462088d13 244 const unsigned char *input,
markrad 0:cdf462088d13 245 unsigned char *output );
markrad 0:cdf462088d13 246 #endif /* MBEDTLS_CIPHER_MODE_CTR */
markrad 0:cdf462088d13 247
markrad 0:cdf462088d13 248 /**
markrad 0:cdf462088d13 249 * \brief Internal AES block encryption function
markrad 0:cdf462088d13 250 * (Only exposed to allow overriding it,
markrad 0:cdf462088d13 251 * see MBEDTLS_AES_ENCRYPT_ALT)
markrad 0:cdf462088d13 252 *
markrad 0:cdf462088d13 253 * \param ctx AES context
markrad 0:cdf462088d13 254 * \param input Plaintext block
markrad 0:cdf462088d13 255 * \param output Output (ciphertext) block
markrad 0:cdf462088d13 256 */
markrad 0:cdf462088d13 257 void mbedtls_aes_encrypt( mbedtls_aes_context *ctx,
markrad 0:cdf462088d13 258 const unsigned char input[16],
markrad 0:cdf462088d13 259 unsigned char output[16] );
markrad 0:cdf462088d13 260
markrad 0:cdf462088d13 261 /**
markrad 0:cdf462088d13 262 * \brief Internal AES block decryption function
markrad 0:cdf462088d13 263 * (Only exposed to allow overriding it,
markrad 0:cdf462088d13 264 * see MBEDTLS_AES_DECRYPT_ALT)
markrad 0:cdf462088d13 265 *
markrad 0:cdf462088d13 266 * \param ctx AES context
markrad 0:cdf462088d13 267 * \param input Ciphertext block
markrad 0:cdf462088d13 268 * \param output Output (plaintext) block
markrad 0:cdf462088d13 269 */
markrad 0:cdf462088d13 270 void mbedtls_aes_decrypt( mbedtls_aes_context *ctx,
markrad 0:cdf462088d13 271 const unsigned char input[16],
markrad 0:cdf462088d13 272 unsigned char output[16] );
markrad 0:cdf462088d13 273
markrad 0:cdf462088d13 274 #ifdef __cplusplus
markrad 0:cdf462088d13 275 }
markrad 0:cdf462088d13 276 #endif
markrad 0:cdf462088d13 277
markrad 0:cdf462088d13 278 #else /* MBEDTLS_AES_ALT */
markrad 0:cdf462088d13 279 #include "aes_alt.h"
markrad 0:cdf462088d13 280 #endif /* MBEDTLS_AES_ALT */
markrad 0:cdf462088d13 281
markrad 0:cdf462088d13 282 #ifdef __cplusplus
markrad 0:cdf462088d13 283 extern "C" {
markrad 0:cdf462088d13 284 #endif
markrad 0:cdf462088d13 285
markrad 0:cdf462088d13 286 /**
markrad 0:cdf462088d13 287 * \brief Checkup routine
markrad 0:cdf462088d13 288 *
markrad 0:cdf462088d13 289 * \return 0 if successful, or 1 if the test failed
markrad 0:cdf462088d13 290 */
markrad 0:cdf462088d13 291 int mbedtls_aes_self_test( int verbose );
markrad 0:cdf462088d13 292
markrad 0:cdf462088d13 293 #ifdef __cplusplus
markrad 0:cdf462088d13 294 }
markrad 0:cdf462088d13 295 #endif
markrad 0:cdf462088d13 296
markrad 0:cdf462088d13 297 #endif /* aes.h */