Greg Steiert / pegasus_dev

Dependents:   blinky_max32630fthr

Committer:
switches
Date:
Fri Nov 11 20:59:50 2016 +0000
Revision:
0:5c4d7b2438d3
Initial commit

Who changed what in which revision?

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