1/* 2 --------------------------------------------------------------------------- 3 Copyright (c) 2003, Dr Brian Gladman, Worcester, UK. All rights reserved. 4 5 LICENSE TERMS 6 7 The free distribution and use of this software in both source and binary 8 form is allowed (with or without changes) provided that: 9 10 1. distributions of this source code include the above copyright 11 notice, this list of conditions and the following disclaimer; 12 13 2. distributions in binary form include the above copyright 14 notice, this list of conditions and the following disclaimer 15 in the documentation and/or other associated materials; 16 17 3. the copyright holder's name is not used to endorse products 18 built using this software without specific written permission. 19 20 ALTERNATIVELY, provided that this notice is retained in full, this product 21 may be distributed under the terms of the GNU General Public License (GPL), 22 in which case the provisions of the GPL apply INSTEAD OF those given above. 23 24 DISCLAIMER 25 26 This software is provided 'as is' with no explicit or implied warranties 27 in respect of its properties, including, but not limited to, correctness 28 and/or fitness for purpose. 29 --------------------------------------------------------------------------- 30 Issue 31/01/2006 31 32 This file contains the definitions required to use AES in C. See aesopt.h 33 for optimisation details. 34*/ 35 36#ifndef _AES_H 37#define _AES_H 38 39 40#if defined(__cplusplus) 41extern "C" 42{ 43#endif 44 45#define AES_128 /* define if AES with 128 bit keys is needed */ 46#define AES_192 /* define if AES with 192 bit keys is needed */ 47#define AES_256 /* define if AES with 256 bit keys is needed */ 48#define AES_VAR /* define if a variable key size is needed */ 49#define AES_MODES /* define if support is needed for modes */ 50 51/* The following must also be set in assembler files if being used */ 52 53#define AES_ENCRYPT /* if support for encryption is needed */ 54#define AES_DECRYPT /* if support for decryption is needed */ 55#define AES_ERR_CHK /* for parameter checks & error return codes */ 56#define AES_REV_DKS /* define to reverse decryption key schedule */ 57 58#define AES_BLOCK_SIZE 16 /* the AES block size in bytes */ 59#define N_COLS 4 /* the number of columns in the state */ 60 61typedef unsigned long uint_32t; 62typedef unsigned char uint_8t; 63typedef unsigned short uint_16t; 64typedef unsigned char aes_08t; 65typedef unsigned long aes_32t; 66 67#define void_ret void 68#define int_ret int 69 70/* The key schedule length is 11, 13 or 15 16-byte blocks for 128, */ 71/* 192 or 256-bit keys respectively. That is 176, 208 or 240 bytes */ 72/* or 44, 52 or 60 32-bit words. */ 73 74#if defined( AES_VAR ) || defined( AES_256 ) 75#define KS_LENGTH 60 76#elif defined( AES_192 ) 77#define KS_LENGTH 52 78#else 79#define KS_LENGTH 44 80#endif 81 82 83 84/* the character array 'inf' in the following structures is used */ 85/* to hold AES context information. This AES code uses cx->inf.b[0] */ 86/* to hold the number of rounds multiplied by 16. The other three */ 87/* elements can be used by code that implements additional modes */ 88 89#if defined (__i386__) 90 91#if defined( AES_ERR_CHK ) 92#define aes_rval int_ret 93#else 94#define aes_rval void_ret 95#endif 96 97typedef union 98{ uint_32t l; 99 uint_8t b[4]; 100} aes_inf; 101 102typedef struct 103{ uint_32t ks[KS_LENGTH]; 104 aes_inf inf; 105} aes_encrypt_ctx; 106 107typedef struct 108{ uint_32t ks[KS_LENGTH]; 109 aes_inf inf; 110} aes_decrypt_ctx; 111 112#else 113 114#if defined( AES_ERR_CHK ) 115#define aes_ret int 116#define aes_good 0 117#define aes_error -1 118#else 119#define aes_ret void 120#endif 121 122#define aes_rval aes_ret 123 124typedef struct 125{ aes_32t ks[KS_LENGTH]; 126 aes_32t rn; 127} aes_encrypt_ctx; 128 129typedef struct 130{ aes_32t ks[KS_LENGTH]; 131 aes_32t rn; 132} aes_decrypt_ctx; 133 134#endif 135 136typedef struct 137{ 138 aes_decrypt_ctx decrypt; 139 aes_encrypt_ctx encrypt; 140} aes_ctx; 141 142 143/* implemented in case of wrong call for fixed tables */ 144 145void gen_tabs(void); 146 147 148/* Key lengths in the range 16 <= key_len <= 32 are given in bytes, */ 149/* those in the range 128 <= key_len <= 256 are given in bits */ 150 151#if defined( AES_ENCRYPT ) 152 153#if defined(AES_128) || defined(AES_VAR) 154aes_rval aes_encrypt_key128(const unsigned char *key, aes_encrypt_ctx cx[1]); 155#endif 156 157#if defined(AES_192) || defined(AES_VAR) 158aes_rval aes_encrypt_key192(const unsigned char *key, aes_encrypt_ctx cx[1]); 159#endif 160 161#if defined(AES_256) || defined(AES_VAR) 162aes_rval aes_encrypt_key256(const unsigned char *key, aes_encrypt_ctx cx[1]); 163#endif 164 165#if defined(AES_VAR) 166aes_rval aes_encrypt_key(const unsigned char *key, int key_len, aes_encrypt_ctx cx[1]); 167#endif 168 169#if defined (__i386__) 170aes_rval aes_encrypt(const unsigned char *in, unsigned char *out, const aes_encrypt_ctx cx[1]); 171#endif 172 173aes_rval aes_encrypt_cbc(const unsigned char *in_blk, const unsigned char *in_iv, unsigned int num_blk, 174 unsigned char *out_blk, const aes_encrypt_ctx cx[1]); 175 176#endif 177 178#if defined( AES_DECRYPT ) 179 180#if defined(AES_128) || defined(AES_VAR) 181aes_rval aes_decrypt_key128(const unsigned char *key, aes_decrypt_ctx cx[1]); 182#endif 183 184#if defined(AES_192) || defined(AES_VAR) 185aes_rval aes_decrypt_key192(const unsigned char *key, aes_decrypt_ctx cx[1]); 186#endif 187 188#if defined(AES_256) || defined(AES_VAR) 189aes_rval aes_decrypt_key256(const unsigned char *key, aes_decrypt_ctx cx[1]); 190#endif 191 192#if defined(AES_VAR) 193aes_rval aes_decrypt_key(const unsigned char *key, int key_len, aes_decrypt_ctx cx[1]); 194#endif 195 196#if defined (__i386__) 197aes_rval aes_decrypt(const unsigned char *in, unsigned char *out, const aes_decrypt_ctx cx[1]); 198#endif 199 200aes_rval aes_decrypt_cbc(const unsigned char *in_blk, const unsigned char *in_iv, unsigned int num_blk, 201 unsigned char *out_blk, const aes_decrypt_ctx cx[1]); 202 203 204#endif 205 206 207#if defined(__cplusplus) 208} 209#endif 210 211#endif 212