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 #if 1 35 #define AES_ENC_PREKEYED /* AES encryption with a precomputed key schedule */ 36 #endif 37 #if 1 38 #define AES_DEC_PREKEYED /* AES decryption with a precomputed key schedule */ 39 #endif 40 #if 1 41 #define AES_ENC_128_OTFK /* AES encryption with 'on the fly' 128 bit keying */ 42 #endif 43 #if 1 44 #define AES_DEC_128_OTFK /* AES decryption with 'on the fly' 128 bit keying */ 45 #endif 46 #if 1 47 #define AES_ENC_256_OTFK /* AES encryption with 'on the fly' 256 bit keying */ 48 #endif 49 #if 1 50 #define AES_DEC_256_OTFK /* AES decryption with 'on the fly' 256 bit keying */ 51 #endif 52 53 #define N_ROW 4 54 #define N_COL 4 55 #define N_BLOCK (N_ROW * N_COL) 56 #define N_MAX_ROUNDS 14 57 58 typedef unsigned char uint_8t; 59 60 typedef uint_8t return_type; 61 62 /* Warning: The key length for 256 bit keys overflows a byte 63 (see comment below) 64 */ 65 66 typedef uint_8t length_type; 67 68 typedef struct { 69 uint_8t ksch[(N_MAX_ROUNDS + 1) * N_BLOCK]; 70 uint_8t rnd; 71 } aes_context; 72 73 /* The following calls are for a precomputed key schedule 74 75 NOTE: If the length_type used for the key length is an 76 unsigned 8-bit character, a key length of 256 bits must 77 be entered as a length in bytes (valid inputs are hence 78 128, 192, 16, 24 and 32). 79 */ 80 81 #if defined(AES_ENC_PREKEYED) || defined(AES_DEC_PREKEYED) 82 83 return_type aes_set_key(const unsigned char key[], length_type keylen, aes_context ctx[1]); 84 #endif 85 86 #if defined(AES_ENC_PREKEYED) 87 88 return_type aes_encrypt(const unsigned char in[N_BLOCK], unsigned char out[N_BLOCK], const aes_context ctx[1]); 89 90 return_type aes_cbc_encrypt( 91 const unsigned char* in, unsigned char* out, int n_block, unsigned char iv[N_BLOCK], const aes_context ctx[1]); 92 #endif 93 94 #if defined(AES_DEC_PREKEYED) 95 96 return_type aes_decrypt(const unsigned char in[N_BLOCK], unsigned char out[N_BLOCK], const aes_context ctx[1]); 97 98 return_type aes_cbc_decrypt( 99 const unsigned char* in, unsigned char* out, int n_block, unsigned char iv[N_BLOCK], const aes_context ctx[1]); 100 #endif 101 102 /* The following calls are for 'on the fly' keying. In this case the 103 encryption and decryption keys are different. 104 105 The encryption subroutines take a key in an array of bytes in 106 key[L] where L is 16, 24 or 32 bytes for key lengths of 128, 107 192, and 256 bits respectively. They then encrypts the input 108 data, in[] with this key and put the reult in the output array 109 out[]. In addition, the second key array, o_key[L], is used 110 to output the key that is needed by the decryption subroutine 111 to reverse the encryption operation. The two key arrays can 112 be the same array but in this case the original key will be 113 overwritten. 114 115 In the same way, the decryption subroutines output keys that 116 can be used to reverse their effect when used for encryption. 117 118 Only 128 and 256 bit keys are supported in these 'on the fly' 119 modes. 120 */ 121 122 #if defined(AES_ENC_128_OTFK) 123 void aes_encrypt_128( 124 const unsigned char in[N_BLOCK], 125 unsigned char out[N_BLOCK], 126 const unsigned char key[N_BLOCK], 127 uint_8t o_key[N_BLOCK]); 128 #endif 129 130 #if defined(AES_DEC_128_OTFK) 131 void aes_decrypt_128( 132 const unsigned char in[N_BLOCK], 133 unsigned char out[N_BLOCK], 134 const unsigned char key[N_BLOCK], 135 unsigned char o_key[N_BLOCK]); 136 #endif 137 138 #if defined(AES_ENC_256_OTFK) 139 void aes_encrypt_256( 140 const unsigned char in[N_BLOCK], 141 unsigned char out[N_BLOCK], 142 const unsigned char key[2 * N_BLOCK], 143 unsigned char o_key[2 * N_BLOCK]); 144 #endif 145 146 #if defined(AES_DEC_256_OTFK) 147 void aes_decrypt_256( 148 const unsigned char in[N_BLOCK], 149 unsigned char out[N_BLOCK], 150 const unsigned char key[2 * N_BLOCK], 151 unsigned char o_key[2 * N_BLOCK]); 152 #endif 153 154 #endif 155