fota lib for mdot

Dependents:   UQ_LoraWAN

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers aes.h Source File

aes.h

00001 /*
00002  ---------------------------------------------------------------------------
00003  Copyright (c) 1998-2008, Brian Gladman, Worcester, UK. All rights reserved.
00004 
00005  LICENSE TERMS
00006 
00007  The redistribution and use of this software (with or without changes)
00008  is allowed without the payment of fees or royalties provided that:
00009 
00010   1. source code distributions include the above copyright notice, this
00011      list of conditions and the following disclaimer;
00012 
00013   2. binary distributions include the above copyright notice, this list
00014      of conditions and the following disclaimer in their documentation;
00015 
00016   3. the name of the copyright holder is not used to endorse products
00017      built using this software without specific written permission.
00018 
00019  DISCLAIMER
00020 
00021  This software is provided 'as is' with no explicit or implied warranties
00022  in respect of its properties, including, but not limited to, correctness
00023  and/or fitness for purpose.
00024  ---------------------------------------------------------------------------
00025  Issue 09/09/2006
00026 
00027  This is an AES implementation that uses only 8-bit byte operations on the
00028  cipher state.
00029  */
00030 
00031 #ifndef AES_H
00032 #define AES_H
00033 
00034 #ifdef __cplusplus
00035 extern "C" {
00036 #endif
00037 
00038 #if 1
00039 #  define AES_ENC_PREKEYED  /* AES encryption with a precomputed key schedule  */
00040 #endif
00041 #if 1
00042 #  define AES_DEC_PREKEYED  /* AES decryption with a precomputed key schedule  */
00043 #endif
00044 #if 0
00045 #  define AES_ENC_128_OTFK  /* AES encryption with 'on the fly' 128 bit keying */
00046 #endif
00047 #if 0
00048 #  define AES_DEC_128_OTFK  /* AES decryption with 'on the fly' 128 bit keying */
00049 #endif
00050 #if 0
00051 #  define AES_ENC_256_OTFK  /* AES encryption with 'on the fly' 256 bit keying */
00052 #endif
00053 #if 0
00054 #  define AES_DEC_256_OTFK  /* AES decryption with 'on the fly' 256 bit keying */
00055 #endif
00056 
00057 #define N_ROW                   4
00058 #define N_COL                   4
00059 #define N_BLOCK   (N_ROW * N_COL)
00060 #define N_MAX_ROUNDS           14
00061 
00062 typedef unsigned char uint_8t;
00063 
00064 typedef uint_8t return_type;
00065 
00066 /*  Warning: The key length for 256 bit keys overflows a byte
00067     (see comment below)
00068 */
00069 
00070 typedef uint_8t length_type;
00071 
00072 typedef struct
00073 {   uint_8t ksch[(N_MAX_ROUNDS + 1) * N_BLOCK];
00074     uint_8t rnd;
00075 } aes_context;
00076 
00077 /*  The following calls are for a precomputed key schedule
00078 
00079     NOTE: If the length_type used for the key length is an
00080     unsigned 8-bit character, a key length of 256 bits must
00081     be entered as a length in bytes (valid inputs are hence
00082     128, 192, 16, 24 and 32).
00083 */
00084 
00085 #if defined( AES_ENC_PREKEYED ) || defined( AES_DEC_PREKEYED )
00086 
00087 return_type aes_set_key( const unsigned char key[],
00088                          length_type keylen,
00089                          aes_context ctx[1] );
00090 #endif
00091 
00092 #if defined( AES_ENC_PREKEYED )
00093 
00094 return_type aes_encrypt( const unsigned char in[N_BLOCK],
00095                          unsigned char out[N_BLOCK],
00096                          const aes_context ctx[1] );
00097 
00098 return_type aes_cbc_encrypt( const unsigned char *in,
00099                          unsigned char *out,
00100                          int n_block,
00101                          unsigned char iv[N_BLOCK],
00102                          const aes_context ctx[1] );
00103 #endif
00104 
00105 #if defined( AES_DEC_PREKEYED )
00106 
00107 return_type aes_decrypt( const unsigned char in[N_BLOCK],
00108                          unsigned char out[N_BLOCK],
00109                          const aes_context ctx[1] );
00110 
00111 return_type aes_cbc_decrypt( const unsigned char *in,
00112                          unsigned char *out,
00113                          int n_block,
00114                          unsigned char iv[N_BLOCK],
00115                          const aes_context ctx[1] );
00116 #endif
00117 
00118 /*  The following calls are for 'on the fly' keying.  In this case the
00119     encryption and decryption keys are different.
00120 
00121     The encryption subroutines take a key in an array of bytes in
00122     key[L] where L is 16, 24 or 32 bytes for key lengths of 128,
00123     192, and 256 bits respectively.  They then encrypts the input
00124     data, in[] with this key and put the reult in the output array
00125     out[].  In addition, the second key array, o_key[L], is used
00126     to output the key that is needed by the decryption subroutine
00127     to reverse the encryption operation.  The two key arrays can
00128     be the same array but in this case the original key will be
00129     overwritten.
00130 
00131     In the same way, the decryption subroutines output keys that
00132     can be used to reverse their effect when used for encryption.
00133 
00134     Only 128 and 256 bit keys are supported in these 'on the fly'
00135     modes.
00136 */
00137 
00138 #if defined( AES_ENC_128_OTFK )
00139 void aes_encrypt_128( const unsigned char in[N_BLOCK],
00140                       unsigned char out[N_BLOCK],
00141                       const unsigned char key[N_BLOCK],
00142                       uint_8t o_key[N_BLOCK] );
00143 #endif
00144 
00145 #if defined( AES_DEC_128_OTFK )
00146 void aes_decrypt_128( const unsigned char in[N_BLOCK],
00147                       unsigned char out[N_BLOCK],
00148                       const unsigned char key[N_BLOCK],
00149                       unsigned char o_key[N_BLOCK] );
00150 #endif
00151 
00152 #if defined( AES_ENC_256_OTFK )
00153 void aes_encrypt_256( const unsigned char in[N_BLOCK],
00154                       unsigned char out[N_BLOCK],
00155                       const unsigned char key[2 * N_BLOCK],
00156                       unsigned char o_key[2 * N_BLOCK] );
00157 #endif
00158 
00159 #if defined( AES_DEC_256_OTFK )
00160 void aes_decrypt_256( const unsigned char in[N_BLOCK],
00161                       unsigned char out[N_BLOCK],
00162                       const unsigned char key[2 * N_BLOCK],
00163                       unsigned char o_key[2 * N_BLOCK] );
00164 #endif
00165 
00166 #ifdef __cplusplus
00167 }
00168 #endif
00169 
00170 #endif
00171