first draft

Dependencies:   mbed LoRaWAN-lib SX1272Liby

Fork of LoRaWAN-demo-72 by Semtech

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 #if 1
00035 #  define AES_ENC_PREKEYED  /* AES encryption with a precomputed key schedule  */
00036 #endif
00037 #if 0
00038 #  define AES_DEC_PREKEYED  /* AES decryption with a precomputed key schedule  */
00039 #endif
00040 #if 0
00041 #  define AES_ENC_128_OTFK  /* AES encryption with 'on the fly' 128 bit keying */
00042 #endif
00043 #if 0
00044 #  define AES_DEC_128_OTFK  /* AES decryption with 'on the fly' 128 bit keying */
00045 #endif
00046 #if 0
00047 #  define AES_ENC_256_OTFK  /* AES encryption with 'on the fly' 256 bit keying */
00048 #endif
00049 #if 0
00050 #  define AES_DEC_256_OTFK  /* AES decryption with 'on the fly' 256 bit keying */
00051 #endif
00052 
00053 #define N_ROW                   4
00054 #define N_COL                   4
00055 #define N_BLOCK   (N_ROW * N_COL)
00056 #define N_MAX_ROUNDS           14
00057 
00058 typedef uint8_t return_type;
00059 
00060 /*  Warning: The key length for 256 bit keys overflows a byte
00061     (see comment below)
00062 */
00063 
00064 typedef uint8_t length_type;
00065 
00066 typedef struct
00067 {   uint8_t ksch[(N_MAX_ROUNDS + 1) * N_BLOCK];
00068     uint8_t rnd;
00069 } aes_context;
00070 
00071 /*  The following calls are for a precomputed key schedule
00072 
00073     NOTE: If the length_type used for the key length is an
00074     unsigned 8-bit character, a key length of 256 bits must
00075     be entered as a length in bytes (valid inputs are hence
00076     128, 192, 16, 24 and 32).
00077 */
00078 
00079 #if defined( AES_ENC_PREKEYED ) || defined( AES_DEC_PREKEYED )
00080 
00081 return_type aes_set_key( const uint8_t key[],
00082                          length_type keylen,
00083                          aes_context ctx[1] );
00084 #endif
00085 
00086 #if defined( AES_ENC_PREKEYED )
00087 
00088 return_type aes_encrypt( const uint8_t in[N_BLOCK],
00089                          uint8_t out[N_BLOCK],
00090                          const aes_context ctx[1] );
00091 
00092 return_type aes_cbc_encrypt( const uint8_t *in,
00093                          uint8_t *out,
00094                          int32_t n_block,
00095                          uint8_t iv[N_BLOCK],
00096                          const aes_context ctx[1] );
00097 #endif
00098 
00099 #if defined( AES_DEC_PREKEYED )
00100 
00101 return_type aes_decrypt( const uint8_t in[N_BLOCK],
00102                          uint8_t out[N_BLOCK],
00103                          const aes_context ctx[1] );
00104 
00105 return_type aes_cbc_decrypt( const uint8_t *in,
00106                          uint8_t *out,
00107                          int32_t n_block,
00108                          uint8_t iv[N_BLOCK],
00109                          const aes_context ctx[1] );
00110 #endif
00111 
00112 /*  The following calls are for 'on the fly' keying.  In this case the
00113     encryption and decryption keys are different.
00114 
00115     The encryption subroutines take a key in an array of bytes in
00116     key[L] where L is 16, 24 or 32 bytes for key lengths of 128,
00117     192, and 256 bits respectively.  They then encrypts the input
00118     data, in[] with this key and put the reult in the output array
00119     out[].  In addition, the second key array, o_key[L], is used
00120     to output the key that is needed by the decryption subroutine
00121     to reverse the encryption operation.  The two key arrays can
00122     be the same array but in this case the original key will be
00123     overwritten.
00124 
00125     In the same way, the decryption subroutines output keys that
00126     can be used to reverse their effect when used for encryption.
00127 
00128     Only 128 and 256 bit keys are supported in these 'on the fly'
00129     modes.
00130 */
00131 
00132 #if defined( AES_ENC_128_OTFK )
00133 void aes_encrypt_128( const uint8_t in[N_BLOCK],
00134                       uint8_t out[N_BLOCK],
00135                       const uint8_t key[N_BLOCK],
00136                       uint8_t o_key[N_BLOCK] );
00137 #endif
00138 
00139 #if defined( AES_DEC_128_OTFK )
00140 void aes_decrypt_128( const uint8_t in[N_BLOCK],
00141                       uint8_t out[N_BLOCK],
00142                       const uint8_t key[N_BLOCK],
00143                       uint8_t o_key[N_BLOCK] );
00144 #endif
00145 
00146 #if defined( AES_ENC_256_OTFK )
00147 void aes_encrypt_256( const uint8_t in[N_BLOCK],
00148                       uint8_t out[N_BLOCK],
00149                       const uint8_t key[2 * N_BLOCK],
00150                       uint8_t o_key[2 * N_BLOCK] );
00151 #endif
00152 
00153 #if defined( AES_DEC_256_OTFK )
00154 void aes_decrypt_256( const uint8_t in[N_BLOCK],
00155                       uint8_t out[N_BLOCK],
00156                       const uint8_t key[2 * N_BLOCK],
00157                       uint8_t o_key[2 * N_BLOCK] );
00158 #endif
00159 
00160 #endif