mbed TLS library

Dependents:   HTTPClient-SSL WS_SERVER

Committer:
ansond
Date:
Thu Jun 11 03:27:03 2015 +0000
Revision:
0:137634ff4186
initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 0:137634ff4186 1 /**
ansond 0:137634ff4186 2 * \file aes.h
ansond 0:137634ff4186 3 *
ansond 0:137634ff4186 4 * \brief AES block cipher
ansond 0:137634ff4186 5 *
ansond 0:137634ff4186 6 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
ansond 0:137634ff4186 7 *
ansond 0:137634ff4186 8 * This file is part of mbed TLS (https://tls.mbed.org)
ansond 0:137634ff4186 9 *
ansond 0:137634ff4186 10 * This program is free software; you can redistribute it and/or modify
ansond 0:137634ff4186 11 * it under the terms of the GNU General Public License as published by
ansond 0:137634ff4186 12 * the Free Software Foundation; either version 2 of the License, or
ansond 0:137634ff4186 13 * (at your option) any later version.
ansond 0:137634ff4186 14 *
ansond 0:137634ff4186 15 * This program is distributed in the hope that it will be useful,
ansond 0:137634ff4186 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ansond 0:137634ff4186 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ansond 0:137634ff4186 18 * GNU General Public License for more details.
ansond 0:137634ff4186 19 *
ansond 0:137634ff4186 20 * You should have received a copy of the GNU General Public License along
ansond 0:137634ff4186 21 * with this program; if not, write to the Free Software Foundation, Inc.,
ansond 0:137634ff4186 22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
ansond 0:137634ff4186 23 */
ansond 0:137634ff4186 24 #ifndef POLARSSL_AES_H
ansond 0:137634ff4186 25 #define POLARSSL_AES_H
ansond 0:137634ff4186 26
ansond 0:137634ff4186 27 #if !defined(POLARSSL_CONFIG_FILE)
ansond 0:137634ff4186 28 #include "config.h"
ansond 0:137634ff4186 29 #else
ansond 0:137634ff4186 30 #include POLARSSL_CONFIG_FILE
ansond 0:137634ff4186 31 #endif
ansond 0:137634ff4186 32
ansond 0:137634ff4186 33 #include <stddef.h>
ansond 0:137634ff4186 34
ansond 0:137634ff4186 35 #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
ansond 0:137634ff4186 36 #include <basetsd.h>
ansond 0:137634ff4186 37 typedef UINT32 uint32_t;
ansond 0:137634ff4186 38 #else
ansond 0:137634ff4186 39 #include <inttypes.h>
ansond 0:137634ff4186 40 #endif
ansond 0:137634ff4186 41
ansond 0:137634ff4186 42 /* padlock.c and aesni.c rely on these values! */
ansond 0:137634ff4186 43 #define AES_ENCRYPT 1
ansond 0:137634ff4186 44 #define AES_DECRYPT 0
ansond 0:137634ff4186 45
ansond 0:137634ff4186 46 #define POLARSSL_ERR_AES_INVALID_KEY_LENGTH -0x0020 /**< Invalid key length. */
ansond 0:137634ff4186 47 #define POLARSSL_ERR_AES_INVALID_INPUT_LENGTH -0x0022 /**< Invalid data input length. */
ansond 0:137634ff4186 48
ansond 0:137634ff4186 49 #if !defined(POLARSSL_AES_ALT)
ansond 0:137634ff4186 50 // Regular implementation
ansond 0:137634ff4186 51 //
ansond 0:137634ff4186 52
ansond 0:137634ff4186 53 #ifdef __cplusplus
ansond 0:137634ff4186 54 extern "C" {
ansond 0:137634ff4186 55 #endif
ansond 0:137634ff4186 56
ansond 0:137634ff4186 57 /**
ansond 0:137634ff4186 58 * \brief AES context structure
ansond 0:137634ff4186 59 *
ansond 0:137634ff4186 60 * \note buf is able to hold 32 extra bytes, which can be used:
ansond 0:137634ff4186 61 * - for alignment purposes if VIA padlock is used, and/or
ansond 0:137634ff4186 62 * - to simplify key expansion in the 256-bit case by
ansond 0:137634ff4186 63 * generating an extra round key
ansond 0:137634ff4186 64 */
ansond 0:137634ff4186 65 typedef struct
ansond 0:137634ff4186 66 {
ansond 0:137634ff4186 67 int nr; /*!< number of rounds */
ansond 0:137634ff4186 68 uint32_t *rk; /*!< AES round keys */
ansond 0:137634ff4186 69 uint32_t buf[68]; /*!< unaligned data */
ansond 0:137634ff4186 70 }
ansond 0:137634ff4186 71 aes_context;
ansond 0:137634ff4186 72
ansond 0:137634ff4186 73 /**
ansond 0:137634ff4186 74 * \brief Initialize AES context
ansond 0:137634ff4186 75 *
ansond 0:137634ff4186 76 * \param ctx AES context to be initialized
ansond 0:137634ff4186 77 */
ansond 0:137634ff4186 78 void aes_init( aes_context *ctx );
ansond 0:137634ff4186 79
ansond 0:137634ff4186 80 /**
ansond 0:137634ff4186 81 * \brief Clear AES context
ansond 0:137634ff4186 82 *
ansond 0:137634ff4186 83 * \param ctx AES context to be cleared
ansond 0:137634ff4186 84 */
ansond 0:137634ff4186 85 void aes_free( aes_context *ctx );
ansond 0:137634ff4186 86
ansond 0:137634ff4186 87 /**
ansond 0:137634ff4186 88 * \brief AES key schedule (encryption)
ansond 0:137634ff4186 89 *
ansond 0:137634ff4186 90 * \param ctx AES context to be initialized
ansond 0:137634ff4186 91 * \param key encryption key
ansond 0:137634ff4186 92 * \param keysize must be 128, 192 or 256
ansond 0:137634ff4186 93 *
ansond 0:137634ff4186 94 * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_KEY_LENGTH
ansond 0:137634ff4186 95 */
ansond 0:137634ff4186 96 int aes_setkey_enc( aes_context *ctx, const unsigned char *key,
ansond 0:137634ff4186 97 unsigned int keysize );
ansond 0:137634ff4186 98
ansond 0:137634ff4186 99 /**
ansond 0:137634ff4186 100 * \brief AES key schedule (decryption)
ansond 0:137634ff4186 101 *
ansond 0:137634ff4186 102 * \param ctx AES context to be initialized
ansond 0:137634ff4186 103 * \param key decryption key
ansond 0:137634ff4186 104 * \param keysize must be 128, 192 or 256
ansond 0:137634ff4186 105 *
ansond 0:137634ff4186 106 * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_KEY_LENGTH
ansond 0:137634ff4186 107 */
ansond 0:137634ff4186 108 int aes_setkey_dec( aes_context *ctx, const unsigned char *key,
ansond 0:137634ff4186 109 unsigned int keysize );
ansond 0:137634ff4186 110
ansond 0:137634ff4186 111 /**
ansond 0:137634ff4186 112 * \brief AES-ECB block encryption/decryption
ansond 0:137634ff4186 113 *
ansond 0:137634ff4186 114 * \param ctx AES context
ansond 0:137634ff4186 115 * \param mode AES_ENCRYPT or AES_DECRYPT
ansond 0:137634ff4186 116 * \param input 16-byte input block
ansond 0:137634ff4186 117 * \param output 16-byte output block
ansond 0:137634ff4186 118 *
ansond 0:137634ff4186 119 * \return 0 if successful
ansond 0:137634ff4186 120 */
ansond 0:137634ff4186 121 int aes_crypt_ecb( aes_context *ctx,
ansond 0:137634ff4186 122 int mode,
ansond 0:137634ff4186 123 const unsigned char input[16],
ansond 0:137634ff4186 124 unsigned char output[16] );
ansond 0:137634ff4186 125
ansond 0:137634ff4186 126 #if defined(POLARSSL_CIPHER_MODE_CBC)
ansond 0:137634ff4186 127 /**
ansond 0:137634ff4186 128 * \brief AES-CBC buffer encryption/decryption
ansond 0:137634ff4186 129 * Length should be a multiple of the block
ansond 0:137634ff4186 130 * size (16 bytes)
ansond 0:137634ff4186 131 *
ansond 0:137634ff4186 132 * \note Upon exit, the content of the IV is updated so that you can
ansond 0:137634ff4186 133 * call the function same function again on the following
ansond 0:137634ff4186 134 * block(s) of data and get the same result as if it was
ansond 0:137634ff4186 135 * encrypted in one call. This allows a "streaming" usage.
ansond 0:137634ff4186 136 * If on the other hand you need to retain the contents of the
ansond 0:137634ff4186 137 * IV, you should either save it manually or use the cipher
ansond 0:137634ff4186 138 * module instead.
ansond 0:137634ff4186 139 *
ansond 0:137634ff4186 140 * \param ctx AES context
ansond 0:137634ff4186 141 * \param mode AES_ENCRYPT or AES_DECRYPT
ansond 0:137634ff4186 142 * \param length length of the input data
ansond 0:137634ff4186 143 * \param iv initialization vector (updated after use)
ansond 0:137634ff4186 144 * \param input buffer holding the input data
ansond 0:137634ff4186 145 * \param output buffer holding the output data
ansond 0:137634ff4186 146 *
ansond 0:137634ff4186 147 * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_INPUT_LENGTH
ansond 0:137634ff4186 148 */
ansond 0:137634ff4186 149 int aes_crypt_cbc( aes_context *ctx,
ansond 0:137634ff4186 150 int mode,
ansond 0:137634ff4186 151 size_t length,
ansond 0:137634ff4186 152 unsigned char iv[16],
ansond 0:137634ff4186 153 const unsigned char *input,
ansond 0:137634ff4186 154 unsigned char *output );
ansond 0:137634ff4186 155 #endif /* POLARSSL_CIPHER_MODE_CBC */
ansond 0:137634ff4186 156
ansond 0:137634ff4186 157 #if defined(POLARSSL_CIPHER_MODE_CFB)
ansond 0:137634ff4186 158 /**
ansond 0:137634ff4186 159 * \brief AES-CFB128 buffer encryption/decryption.
ansond 0:137634ff4186 160 *
ansond 0:137634ff4186 161 * Note: Due to the nature of CFB you should use the same key schedule for
ansond 0:137634ff4186 162 * both encryption and decryption. So a context initialized with
ansond 0:137634ff4186 163 * aes_setkey_enc() for both AES_ENCRYPT and AES_DECRYPT.
ansond 0:137634ff4186 164 *
ansond 0:137634ff4186 165 * \note Upon exit, the content of the IV is updated so that you can
ansond 0:137634ff4186 166 * call the function same function again on the following
ansond 0:137634ff4186 167 * block(s) of data and get the same result as if it was
ansond 0:137634ff4186 168 * encrypted in one call. This allows a "streaming" usage.
ansond 0:137634ff4186 169 * If on the other hand you need to retain the contents of the
ansond 0:137634ff4186 170 * IV, you should either save it manually or use the cipher
ansond 0:137634ff4186 171 * module instead.
ansond 0:137634ff4186 172 *
ansond 0:137634ff4186 173 * \param ctx AES context
ansond 0:137634ff4186 174 * \param mode AES_ENCRYPT or AES_DECRYPT
ansond 0:137634ff4186 175 * \param length length of the input data
ansond 0:137634ff4186 176 * \param iv_off offset in IV (updated after use)
ansond 0:137634ff4186 177 * \param iv initialization vector (updated after use)
ansond 0:137634ff4186 178 * \param input buffer holding the input data
ansond 0:137634ff4186 179 * \param output buffer holding the output data
ansond 0:137634ff4186 180 *
ansond 0:137634ff4186 181 * \return 0 if successful
ansond 0:137634ff4186 182 */
ansond 0:137634ff4186 183 int aes_crypt_cfb128( aes_context *ctx,
ansond 0:137634ff4186 184 int mode,
ansond 0:137634ff4186 185 size_t length,
ansond 0:137634ff4186 186 size_t *iv_off,
ansond 0:137634ff4186 187 unsigned char iv[16],
ansond 0:137634ff4186 188 const unsigned char *input,
ansond 0:137634ff4186 189 unsigned char *output );
ansond 0:137634ff4186 190
ansond 0:137634ff4186 191 /**
ansond 0:137634ff4186 192 * \brief AES-CFB8 buffer encryption/decryption.
ansond 0:137634ff4186 193 *
ansond 0:137634ff4186 194 * Note: Due to the nature of CFB you should use the same key schedule for
ansond 0:137634ff4186 195 * both encryption and decryption. So a context initialized with
ansond 0:137634ff4186 196 * aes_setkey_enc() for both AES_ENCRYPT and AES_DECRYPT.
ansond 0:137634ff4186 197 *
ansond 0:137634ff4186 198 * \note Upon exit, the content of the IV is updated so that you can
ansond 0:137634ff4186 199 * call the function same function again on the following
ansond 0:137634ff4186 200 * block(s) of data and get the same result as if it was
ansond 0:137634ff4186 201 * encrypted in one call. This allows a "streaming" usage.
ansond 0:137634ff4186 202 * If on the other hand you need to retain the contents of the
ansond 0:137634ff4186 203 * IV, you should either save it manually or use the cipher
ansond 0:137634ff4186 204 * module instead.
ansond 0:137634ff4186 205 *
ansond 0:137634ff4186 206 * \param ctx AES context
ansond 0:137634ff4186 207 * \param mode AES_ENCRYPT or AES_DECRYPT
ansond 0:137634ff4186 208 * \param length length of the input data
ansond 0:137634ff4186 209 * \param iv initialization vector (updated after use)
ansond 0:137634ff4186 210 * \param input buffer holding the input data
ansond 0:137634ff4186 211 * \param output buffer holding the output data
ansond 0:137634ff4186 212 *
ansond 0:137634ff4186 213 * \return 0 if successful
ansond 0:137634ff4186 214 */
ansond 0:137634ff4186 215 int aes_crypt_cfb8( aes_context *ctx,
ansond 0:137634ff4186 216 int mode,
ansond 0:137634ff4186 217 size_t length,
ansond 0:137634ff4186 218 unsigned char iv[16],
ansond 0:137634ff4186 219 const unsigned char *input,
ansond 0:137634ff4186 220 unsigned char *output );
ansond 0:137634ff4186 221 #endif /*POLARSSL_CIPHER_MODE_CFB */
ansond 0:137634ff4186 222
ansond 0:137634ff4186 223 #if defined(POLARSSL_CIPHER_MODE_CTR)
ansond 0:137634ff4186 224 /**
ansond 0:137634ff4186 225 * \brief AES-CTR buffer encryption/decryption
ansond 0:137634ff4186 226 *
ansond 0:137634ff4186 227 * Warning: You have to keep the maximum use of your counter in mind!
ansond 0:137634ff4186 228 *
ansond 0:137634ff4186 229 * Note: Due to the nature of CTR you should use the same key schedule for
ansond 0:137634ff4186 230 * both encryption and decryption. So a context initialized with
ansond 0:137634ff4186 231 * aes_setkey_enc() for both AES_ENCRYPT and AES_DECRYPT.
ansond 0:137634ff4186 232 *
ansond 0:137634ff4186 233 * \param ctx AES context
ansond 0:137634ff4186 234 * \param length The length of the data
ansond 0:137634ff4186 235 * \param nc_off The offset in the current stream_block (for resuming
ansond 0:137634ff4186 236 * within current cipher stream). The offset pointer to
ansond 0:137634ff4186 237 * should be 0 at the start of a stream.
ansond 0:137634ff4186 238 * \param nonce_counter The 128-bit nonce and counter.
ansond 0:137634ff4186 239 * \param stream_block The saved stream-block for resuming. Is overwritten
ansond 0:137634ff4186 240 * by the function.
ansond 0:137634ff4186 241 * \param input The input data stream
ansond 0:137634ff4186 242 * \param output The output data stream
ansond 0:137634ff4186 243 *
ansond 0:137634ff4186 244 * \return 0 if successful
ansond 0:137634ff4186 245 */
ansond 0:137634ff4186 246 int aes_crypt_ctr( aes_context *ctx,
ansond 0:137634ff4186 247 size_t length,
ansond 0:137634ff4186 248 size_t *nc_off,
ansond 0:137634ff4186 249 unsigned char nonce_counter[16],
ansond 0:137634ff4186 250 unsigned char stream_block[16],
ansond 0:137634ff4186 251 const unsigned char *input,
ansond 0:137634ff4186 252 unsigned char *output );
ansond 0:137634ff4186 253 #endif /* POLARSSL_CIPHER_MODE_CTR */
ansond 0:137634ff4186 254
ansond 0:137634ff4186 255 #ifdef __cplusplus
ansond 0:137634ff4186 256 }
ansond 0:137634ff4186 257 #endif
ansond 0:137634ff4186 258
ansond 0:137634ff4186 259 #else /* POLARSSL_AES_ALT */
ansond 0:137634ff4186 260 #include "aes_alt.h"
ansond 0:137634ff4186 261 #endif /* POLARSSL_AES_ALT */
ansond 0:137634ff4186 262
ansond 0:137634ff4186 263 #ifdef __cplusplus
ansond 0:137634ff4186 264 extern "C" {
ansond 0:137634ff4186 265 #endif
ansond 0:137634ff4186 266
ansond 0:137634ff4186 267 /**
ansond 0:137634ff4186 268 * \brief Checkup routine
ansond 0:137634ff4186 269 *
ansond 0:137634ff4186 270 * \return 0 if successful, or 1 if the test failed
ansond 0:137634ff4186 271 */
ansond 0:137634ff4186 272 int aes_self_test( int verbose );
ansond 0:137634ff4186 273
ansond 0:137634ff4186 274 #ifdef __cplusplus
ansond 0:137634ff4186 275 }
ansond 0:137634ff4186 276 #endif
ansond 0:137634ff4186 277
ansond 0:137634ff4186 278 #endif /* aes.h */
ansond 0:137634ff4186 279