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