1 /* 2 --------------------------------------------------------------------------- 3 Copyright (c) 1998-2008, Brian Gladman, Worcester, UK. All rights reserved. 4 5 LICENSE TERMS 6 7 The redistribution and use of this software (with or without changes) 8 is allowed without the payment of fees or royalties provided that: 9 10 1. source code distributions include the above copyright notice, this 11 list of conditions and the following disclaimer; 12 13 2. binary distributions include the above copyright notice, this list 14 of conditions and the following disclaimer in their documentation; 15 16 3. the name of the copyright holder is not used to endorse products 17 built using this software without specific written permission. 18 19 DISCLAIMER 20 21 This software is provided 'as is' with no explicit or implied warranties 22 in respect of its properties, including, but not limited to, correctness 23 and/or fitness for purpose. 24 --------------------------------------------------------------------------- 25 Issue 09/09/2006 26 27 This is an AES implementation that uses only 8-bit byte operations on the 28 cipher state. 29 */ 30 31 #ifndef AES_H 32 #define AES_H 33 34 #ifdef __cplusplus 35 extern "C" { 36 #endif 37 38 #include <stdint.h> 39 40 #if 1 41 # define AES_ENC_PREKEYED /* AES encryption with a precomputed key schedule */ 42 #endif 43 #if 0 44 # define AES_DEC_PREKEYED /* AES decryption with a precomputed key schedule */ 45 #endif 46 #if 0 47 # define AES_ENC_128_OTFK /* AES encryption with 'on the fly' 128 bit keying */ 48 #endif 49 #if 0 50 # define AES_DEC_128_OTFK /* AES decryption with 'on the fly' 128 bit keying */ 51 #endif 52 #if 0 53 # define AES_ENC_256_OTFK /* AES encryption with 'on the fly' 256 bit keying */ 54 #endif 55 #if 0 56 # define AES_DEC_256_OTFK /* AES decryption with 'on the fly' 256 bit keying */ 57 #endif 58 59 #define N_ROW 4 60 #define N_COL 4 61 #define N_BLOCK (N_ROW * N_COL) 62 #define N_MAX_ROUNDS 14 63 64 typedef uint8_t return_type; 65 66 /* Warning: The key length for 256 bit keys overflows a byte 67 (see comment below) 68 */ 69 70 typedef uint8_t length_type; 71 72 typedef struct 73 { uint8_t ksch[(N_MAX_ROUNDS + 1) * N_BLOCK]; 74 uint8_t rnd; 75 } aes_context; 76 77 /* The following calls are for a precomputed key schedule 78 79 NOTE: If the length_type used for the key length is an 80 unsigned 8-bit character, a key length of 256 bits must 81 be entered as a length in bytes (valid inputs are hence 82 128, 192, 16, 24 and 32). 83 */ 84 85 #if defined( AES_ENC_PREKEYED ) || defined( AES_DEC_PREKEYED ) 86 87 return_type aes_set_key( const uint8_t key[], 88 length_type keylen, 89 aes_context ctx[1] ); 90 #endif 91 92 #if defined( AES_ENC_PREKEYED ) 93 94 return_type aes_encrypt( const uint8_t in[N_BLOCK], 95 uint8_t out[N_BLOCK], 96 const aes_context ctx[1] ); 97 98 return_type aes_cbc_encrypt( const uint8_t *in, 99 uint8_t *out, 100 int32_t n_block, 101 uint8_t iv[N_BLOCK], 102 const aes_context ctx[1] ); 103 #endif 104 105 #if defined( AES_DEC_PREKEYED ) 106 107 return_type aes_decrypt( const uint8_t in[N_BLOCK], 108 uint8_t out[N_BLOCK], 109 const aes_context ctx[1] ); 110 111 return_type aes_cbc_decrypt( const uint8_t *in, 112 uint8_t *out, 113 int32_t n_block, 114 uint8_t iv[N_BLOCK], 115 const aes_context ctx[1] ); 116 #endif 117 118 /* The following calls are for 'on the fly' keying. In this case the 119 encryption and decryption keys are different. 120 121 The encryption subroutines take a key in an array of bytes in 122 key[L] where L is 16, 24 or 32 bytes for key lengths of 128, 123 192, and 256 bits respectively. They then encrypts the input 124 data, in[] with this key and put the reult in the output array 125 out[]. In addition, the second key array, o_key[L], is used 126 to output the key that is needed by the decryption subroutine 127 to reverse the encryption operation. The two key arrays can 128 be the same array but in this case the original key will be 129 overwritten. 130 131 In the same way, the decryption subroutines output keys that 132 can be used to reverse their effect when used for encryption. 133 134 Only 128 and 256 bit keys are supported in these 'on the fly' 135 modes. 136 */ 137 138 #if defined( AES_ENC_128_OTFK ) 139 void aes_encrypt_128( const uint8_t in[N_BLOCK], 140 uint8_t out[N_BLOCK], 141 const uint8_t key[N_BLOCK], 142 uint8_t o_key[N_BLOCK] ); 143 #endif 144 145 #if defined( AES_DEC_128_OTFK ) 146 void aes_decrypt_128( const uint8_t in[N_BLOCK], 147 uint8_t out[N_BLOCK], 148 const uint8_t key[N_BLOCK], 149 uint8_t o_key[N_BLOCK] ); 150 #endif 151 152 #if defined( AES_ENC_256_OTFK ) 153 void aes_encrypt_256( const uint8_t in[N_BLOCK], 154 uint8_t out[N_BLOCK], 155 const uint8_t key[2 * N_BLOCK], 156 uint8_t o_key[2 * N_BLOCK] ); 157 #endif 158 159 #if defined( AES_DEC_256_OTFK ) 160 void aes_decrypt_256( const uint8_t in[N_BLOCK], 161 uint8_t out[N_BLOCK], 162 const uint8_t key[2 * N_BLOCK], 163 uint8_t o_key[2 * N_BLOCK] ); 164 #endif 165 166 #ifdef __cplusplus 167 } 168 #endif 169 170 #endif 171