Example program to test AES-GCM functionality. Used for a workshop

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers aes.h Source File

aes.h

Go to the documentation of this file.
00001 /**
00002  * \file aes.h
00003  *
00004  * \brief AES block cipher
00005  *
00006  *  Copyright (C) 2006-2014, Brainspark B.V.
00007  *
00008  *  This file is part of PolarSSL (http://www.polarssl.org)
00009  *  Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
00010  *
00011  *  All rights reserved.
00012  *
00013  *  This program is free software; you can redistribute it and/or modify
00014  *  it under the terms of the GNU General Public License as published by
00015  *  the Free Software Foundation; either version 2 of the License, or
00016  *  (at your option) any later version.
00017  *
00018  *  This program is distributed in the hope that it will be useful,
00019  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00020  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00021  *  GNU General Public License for more details.
00022  *
00023  *  You should have received a copy of the GNU General Public License along
00024  *  with this program; if not, write to the Free Software Foundation, Inc.,
00025  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00026  */
00027 #ifndef POLARSSL_AES_H
00028 #define POLARSSL_AES_H
00029 
00030 #if !defined(POLARSSL_CONFIG_FILE)
00031 #include "config.h"
00032 #else
00033 #include POLARSSL_CONFIG_FILE
00034 #endif
00035 
00036 #include <string.h>
00037 
00038 #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
00039 #include <basetsd.h>
00040 typedef UINT32 uint32_t;
00041 #else
00042 #include <inttypes.h>
00043 #endif
00044 
00045 /* padlock.c and aesni.c rely on these values! */
00046 #define AES_ENCRYPT     1
00047 #define AES_DECRYPT     0
00048 
00049 #define POLARSSL_ERR_AES_INVALID_KEY_LENGTH                -0x0020  /**< Invalid key length. */
00050 #define POLARSSL_ERR_AES_INVALID_INPUT_LENGTH              -0x0022  /**< Invalid data input length. */
00051 
00052 #if !defined(POLARSSL_AES_ALT)
00053 // Regular implementation
00054 //
00055 
00056 #ifdef __cplusplus
00057 extern "C" {
00058 #endif
00059 
00060 /**
00061  * \brief          AES context structure
00062  *
00063  * \note           buf is able to hold 32 extra bytes, which can be used:
00064  *                 - for alignment purposes if VIA padlock is used, and/or
00065  *                 - to simplify key expansion in the 256-bit case by
00066  *                 generating an extra round key
00067  */
00068 typedef struct
00069 {
00070     int nr ;                     /*!<  number of rounds  */
00071     uint32_t *rk ;               /*!<  AES round keys    */
00072     uint32_t buf[68];           /*!<  unaligned data    */
00073 }
00074 aes_context;
00075 
00076 /**
00077  * \brief          AES key schedule (encryption)
00078  *
00079  * \param ctx      AES context to be initialized
00080  * \param key      encryption key
00081  * \param keysize  must be 128, 192 or 256
00082  *
00083  * \return         0 if successful, or POLARSSL_ERR_AES_INVALID_KEY_LENGTH
00084  */
00085 int aes_setkey_enc( aes_context *ctx, const unsigned char *key,
00086                     unsigned int keysize );
00087 
00088 /**
00089  * \brief          AES key schedule (decryption)
00090  *
00091  * \param ctx      AES context to be initialized
00092  * \param key      decryption key
00093  * \param keysize  must be 128, 192 or 256
00094  *
00095  * \return         0 if successful, or POLARSSL_ERR_AES_INVALID_KEY_LENGTH
00096  */
00097 int aes_setkey_dec( aes_context *ctx, const unsigned char *key,
00098                     unsigned int keysize );
00099 
00100 /**
00101  * \brief          AES-ECB block encryption/decryption
00102  *
00103  * \param ctx      AES context
00104  * \param mode     AES_ENCRYPT or AES_DECRYPT
00105  * \param input    16-byte input block
00106  * \param output   16-byte output block
00107  *
00108  * \return         0 if successful
00109  */
00110 int aes_crypt_ecb( aes_context *ctx,
00111                     int mode,
00112                     const unsigned char input[16],
00113                     unsigned char output[16] );
00114 
00115 #if defined(POLARSSL_CIPHER_MODE_CBC)
00116 /**
00117  * \brief          AES-CBC buffer encryption/decryption
00118  *                 Length should be a multiple of the block
00119  *                 size (16 bytes)
00120  *
00121  * \param ctx      AES context
00122  * \param mode     AES_ENCRYPT or AES_DECRYPT
00123  * \param length   length of the input data
00124  * \param iv       initialization vector (updated after use)
00125  * \param input    buffer holding the input data
00126  * \param output   buffer holding the output data
00127  *
00128  * \return         0 if successful, or POLARSSL_ERR_AES_INVALID_INPUT_LENGTH
00129  */
00130 int aes_crypt_cbc( aes_context *ctx,
00131                     int mode,
00132                     size_t length,
00133                     unsigned char iv[16],
00134                     const unsigned char *input,
00135                     unsigned char *output );
00136 #endif /* POLARSSL_CIPHER_MODE_CBC */
00137 
00138 #if defined(POLARSSL_CIPHER_MODE_CFB)
00139 /**
00140  * \brief          AES-CFB128 buffer encryption/decryption.
00141  *
00142  * Note: Due to the nature of CFB you should use the same key schedule for
00143  * both encryption and decryption. So a context initialized with
00144  * aes_setkey_enc() for both AES_ENCRYPT and AES_DECRYPT.
00145  *
00146  * \param ctx      AES context
00147  * \param mode     AES_ENCRYPT or AES_DECRYPT
00148  * \param length   length of the input data
00149  * \param iv_off   offset in IV (updated after use)
00150  * \param iv       initialization vector (updated after use)
00151  * \param input    buffer holding the input data
00152  * \param output   buffer holding the output data
00153  *
00154  * \return         0 if successful
00155  */
00156 int aes_crypt_cfb128( aes_context *ctx,
00157                        int mode,
00158                        size_t length,
00159                        size_t *iv_off,
00160                        unsigned char iv[16],
00161                        const unsigned char *input,
00162                        unsigned char *output );
00163 
00164 /**
00165  * \brief          AES-CFB8 buffer encryption/decryption.
00166  *
00167  * Note: Due to the nature of CFB you should use the same key schedule for
00168  * both encryption and decryption. So a context initialized with
00169  * aes_setkey_enc() for both AES_ENCRYPT and AES_DECRYPT.
00170  *
00171  * \param ctx      AES context
00172  * \param mode     AES_ENCRYPT or AES_DECRYPT
00173  * \param length   length of the input data
00174  * \param iv       initialization vector (updated after use)
00175  * \param input    buffer holding the input data
00176  * \param output   buffer holding the output data
00177  *
00178  * \return         0 if successful
00179  */
00180 int aes_crypt_cfb8( aes_context *ctx,
00181                     int mode,
00182                     size_t length,
00183                     unsigned char iv[16],
00184                     const unsigned char *input,
00185                     unsigned char *output );
00186 #endif /*POLARSSL_CIPHER_MODE_CFB */
00187 
00188 #if defined(POLARSSL_CIPHER_MODE_CTR)
00189 /**
00190  * \brief               AES-CTR buffer encryption/decryption
00191  *
00192  * Warning: You have to keep the maximum use of your counter in mind!
00193  *
00194  * Note: Due to the nature of CTR you should use the same key schedule for
00195  * both encryption and decryption. So a context initialized with
00196  * aes_setkey_enc() for both AES_ENCRYPT and AES_DECRYPT.
00197  *
00198  * \param ctx           AES context
00199  * \param length        The length of the data
00200  * \param nc_off        The offset in the current stream_block (for resuming
00201  *                      within current cipher stream). The offset pointer to
00202  *                      should be 0 at the start of a stream.
00203  * \param nonce_counter The 128-bit nonce and counter.
00204  * \param stream_block  The saved stream-block for resuming. Is overwritten
00205  *                      by the function.
00206  * \param input         The input data stream
00207  * \param output        The output data stream
00208  *
00209  * \return         0 if successful
00210  */
00211 int aes_crypt_ctr( aes_context *ctx,
00212                        size_t length,
00213                        size_t *nc_off,
00214                        unsigned char nonce_counter[16],
00215                        unsigned char stream_block[16],
00216                        const unsigned char *input,
00217                        unsigned char *output );
00218 #endif /* POLARSSL_CIPHER_MODE_CTR */
00219 
00220 #ifdef __cplusplus
00221 }
00222 #endif
00223 
00224 #else  /* POLARSSL_AES_ALT */
00225 #include "aes_alt.h"
00226 #endif /* POLARSSL_AES_ALT */
00227 
00228 #ifdef __cplusplus
00229 extern "C" {
00230 #endif
00231 
00232 /**
00233  * \brief          Checkup routine
00234  *
00235  * \return         0 if successful, or 1 if the test failed
00236  */
00237 int aes_self_test( int verbose );
00238 
00239 #ifdef __cplusplus
00240 }
00241 #endif
00242 
00243 #endif /* aes.h */
00244 
00245