1 /* 2 --------------------------------------------------------------------------- 3 Copyright (c) 1998-2013, Brian Gladman, Worcester, UK. All rights reserved. 4 5 The redistribution and use of this software (with or without changes) 6 is allowed without the payment of fees or royalties provided that: 7 8 source code distributions include the above copyright notice, this 9 list of conditions and the following disclaimer; 10 11 binary distributions include the above copyright notice, this list 12 of conditions and the following disclaimer in their documentation. 13 14 This software is provided 'as is' with no explicit or implied warranties 15 in respect of its operation, including, but not limited to, correctness 16 and fitness for purpose. 17 --------------------------------------------------------------------------- 18 Issue Date: 02/09/2018 19 20 This file contains the definitions required to use AES in C. See aesopt.h 21 for optimisation details. 22 */ 23 24 #ifndef _AES_H 25 #define _AES_H 26 27 /* For MIT krb5, give the linker-visible symbols a prefix. */ 28 #define aes_decrypt k5_aes_decrypt 29 #define aes_decrypt_key k5_aes_decrypt_key 30 #define aes_decrypt_key128 k5_aes_decrypt_key128 31 #define aes_decrypt_key256 k5_aes_decrypt_key256 32 #define aes_encrypt k5_aes_encrypt 33 #define aes_encrypt_key k5_aes_encrypt_key 34 #define aes_encrypt_key128 k5_aes_encrypt_key128 35 #define aes_encrypt_key256 k5_aes_encrypt_key256 36 #define aes_init k5_aes_init 37 38 #include <stdlib.h> 39 40 /* This include is used to find 8 & 32 bit unsigned integer types */ 41 #include "brg_types.h" 42 43 #if defined(__cplusplus) 44 extern "C" 45 { 46 #endif 47 48 #define AES_128 /* if a fast 128 bit key scheduler is needed */ 49 #undef AES_192 /* if a fast 192 bit key scheduler is needed */ 50 #define AES_256 /* if a fast 256 bit key scheduler is needed */ 51 #define AES_VAR /* if variable key size scheduler is needed */ 52 #if 1 53 # define AES_MODES /* if support is needed for modes in the C code */ 54 #endif /* (these will use AES_NI if it is present) */ 55 #if 0 /* add this to make direct calls to the AES_NI */ 56 # /* implemented CBC and CTR modes available */ 57 # define ADD_AESNI_MODE_CALLS 58 #endif 59 60 /* The following must also be set in assembler files if being used */ 61 62 #define AES_ENCRYPT /* if support for encryption is needed */ 63 #define AES_DECRYPT /* if support for decryption is needed */ 64 65 #define AES_BLOCK_SIZE_P2 4 /* AES block size as a power of 2 */ 66 #define AES_BLOCK_SIZE (1 << AES_BLOCK_SIZE_P2) /* AES block size */ 67 #define N_COLS 4 /* the number of columns in the state */ 68 69 /* The key schedule length is 11, 13 or 15 16-byte blocks for 128, */ 70 /* 192 or 256-bit keys respectively. That is 176, 208 or 240 bytes */ 71 /* or 44, 52 or 60 32-bit words. */ 72 73 #if defined( AES_VAR ) || defined( AES_256 ) 74 #define KS_LENGTH 60 75 #elif defined( AES_192 ) 76 #define KS_LENGTH 52 77 #else 78 #define KS_LENGTH 44 79 #endif 80 81 #define AES_RETURN INT_RETURN 82 83 /* the character array 'inf' in the following structures is used */ 84 /* to hold AES context information. This AES code uses cx->inf.b[0] */ 85 /* to hold the number of rounds multiplied by 16. The other three */ 86 /* elements can be used by code that implements additional modes */ 87 88 typedef union 89 { uint32_t l; 90 uint8_t b[4]; 91 } aes_inf; 92 93 /* Macros for detecting whether a given context was initialized for */ 94 /* use with encryption or decryption code. These should only be used */ 95 /* by e.g. language bindings which lose type information when the */ 96 /* context pointer is passed to the calling language's runtime. */ 97 #define IS_ENCRYPTION_CTX(cx) (((cx)->inf.b[2] & (uint8_t)0x01) == 1) 98 #define IS_DECRYPTION_CTX(cx) (((cx)->inf.b[2] & (uint8_t)0x01) == 0) 99 100 #ifdef _MSC_VER 101 # pragma warning( disable : 4324 ) 102 #endif 103 104 #if defined(_MSC_VER) && defined(_WIN64) 105 #define ALIGNED_(x) __declspec(align(x)) 106 #elif defined(__GNUC__) && defined(__x86_64__) 107 #define ALIGNED_(x) __attribute__ ((aligned(x))) 108 #else 109 #define ALIGNED_(x) 110 #endif 111 112 typedef struct ALIGNED_(16) 113 { uint32_t ks[KS_LENGTH]; 114 aes_inf inf; 115 } aes_crypt_ctx; 116 117 typedef aes_crypt_ctx aes_encrypt_ctx; 118 typedef aes_crypt_ctx aes_decrypt_ctx; 119 120 #ifdef _MSC_VER 121 # pragma warning( default : 4324 ) 122 #endif 123 124 /* This routine must be called before first use if non-static */ 125 /* tables are being used */ 126 127 AES_RETURN aes_init(void); 128 129 /* Key lengths in the range 16 <= key_len <= 32 are given in bytes, */ 130 /* those in the range 128 <= key_len <= 256 are given in bits */ 131 132 #if defined( AES_ENCRYPT ) 133 134 #if defined( AES_128 ) || defined( AES_VAR) 135 AES_RETURN aes_encrypt_key128(const unsigned char *key, aes_encrypt_ctx cx[1]); 136 #endif 137 138 #if defined( AES_192 ) || defined( AES_VAR) 139 AES_RETURN aes_encrypt_key192(const unsigned char *key, aes_encrypt_ctx cx[1]); 140 #endif 141 142 #if defined( AES_256 ) || defined( AES_VAR) 143 AES_RETURN aes_encrypt_key256(const unsigned char *key, aes_encrypt_ctx cx[1]); 144 #endif 145 146 #if defined( AES_VAR ) 147 AES_RETURN aes_encrypt_key(const unsigned char *key, int key_len, aes_encrypt_ctx cx[1]); 148 #endif 149 150 AES_RETURN aes_encrypt(const unsigned char *in, unsigned char *out, const aes_encrypt_ctx cx[1]); 151 152 #endif 153 154 #if defined( AES_DECRYPT ) 155 156 #if defined( AES_128 ) || defined( AES_VAR) 157 AES_RETURN aes_decrypt_key128(const unsigned char *key, aes_decrypt_ctx cx[1]); 158 #endif 159 160 #if defined( AES_192 ) || defined( AES_VAR) 161 AES_RETURN aes_decrypt_key192(const unsigned char *key, aes_decrypt_ctx cx[1]); 162 #endif 163 164 #if defined( AES_256 ) || defined( AES_VAR) 165 AES_RETURN aes_decrypt_key256(const unsigned char *key, aes_decrypt_ctx cx[1]); 166 #endif 167 168 #if defined( AES_VAR ) 169 AES_RETURN aes_decrypt_key(const unsigned char *key, int key_len, aes_decrypt_ctx cx[1]); 170 #endif 171 172 AES_RETURN aes_decrypt(const unsigned char *in, unsigned char *out, const aes_decrypt_ctx cx[1]); 173 174 #endif 175 176 #if defined( AES_MODES ) 177 178 /* Multiple calls to the following subroutines for multiple block */ 179 /* ECB, CBC, CFB, OFB and CTR mode encryption can be used to handle */ 180 /* long messages incrementally provided that the context AND the iv */ 181 /* are preserved between all such calls. For the ECB and CBC modes */ 182 /* each individual call within a series of incremental calls must */ 183 /* process only full blocks (i.e. len must be a multiple of 16) but */ 184 /* the CFB, OFB and CTR mode calls can handle multiple incremental */ 185 /* calls of any length. Each mode is reset when a new AES key is */ 186 /* set but ECB needs no reset and CBC can be reset without setting */ 187 /* a new key by setting a new IV value. To reset CFB, OFB and CTR */ 188 /* without setting the key, aes_mode_reset() must be called and the */ 189 /* IV must be set. NOTE: All these calls update the IV on exit so */ 190 /* this has to be reset if a new operation with the same IV as the */ 191 /* previous one is required (or decryption follows encryption with */ 192 /* the same IV array). */ 193 194 AES_RETURN aes_test_alignment_detection(unsigned int n); 195 196 AES_RETURN aes_ecb_encrypt(const unsigned char *ibuf, unsigned char *obuf, 197 int len, const aes_encrypt_ctx cx[1]); 198 199 AES_RETURN aes_ecb_decrypt(const unsigned char *ibuf, unsigned char *obuf, 200 int len, const aes_decrypt_ctx cx[1]); 201 202 AES_RETURN aes_cbc_encrypt(const unsigned char *ibuf, unsigned char *obuf, 203 int len, unsigned char *iv, const aes_encrypt_ctx cx[1]); 204 205 AES_RETURN aes_cbc_decrypt(const unsigned char *ibuf, unsigned char *obuf, 206 int len, unsigned char *iv, const aes_decrypt_ctx cx[1]); 207 208 AES_RETURN aes_mode_reset(aes_encrypt_ctx cx[1]); 209 210 AES_RETURN aes_cfb_encrypt(const unsigned char *ibuf, unsigned char *obuf, 211 int len, unsigned char *iv, aes_encrypt_ctx cx[1]); 212 213 AES_RETURN aes_cfb_decrypt(const unsigned char *ibuf, unsigned char *obuf, 214 int len, unsigned char *iv, aes_encrypt_ctx cx[1]); 215 216 #define aes_ofb_encrypt aes_ofb_crypt 217 #define aes_ofb_decrypt aes_ofb_crypt 218 219 AES_RETURN aes_ofb_crypt(const unsigned char *ibuf, unsigned char *obuf, 220 int len, unsigned char *iv, aes_encrypt_ctx cx[1]); 221 222 typedef void cbuf_inc(unsigned char *cbuf); 223 224 #define aes_ctr_encrypt aes_ctr_crypt 225 #define aes_ctr_decrypt aes_ctr_crypt 226 227 AES_RETURN aes_ctr_crypt(const unsigned char *ibuf, unsigned char *obuf, 228 int len, unsigned char *cbuf, cbuf_inc ctr_inc, aes_encrypt_ctx cx[1]); 229 230 #endif 231 232 #if 0 && defined( ADD_AESNI_MODE_CALLS ) 233 # define USE_AES_CONTEXT 234 #endif 235 236 #ifdef ADD_AESNI_MODE_CALLS 237 # ifdef USE_AES_CONTEXT 238 239 AES_RETURN aes_CBC_encrypt(const unsigned char *in, 240 unsigned char *out, 241 unsigned char ivec[16], 242 unsigned long length, 243 const aes_encrypt_ctx cx[1]); 244 245 AES_RETURN aes_CBC_decrypt(const unsigned char *in, 246 unsigned char *out, 247 unsigned char ivec[16], 248 unsigned long length, 249 const aes_decrypt_ctx cx[1]); 250 251 AES_RETURN AES_CTR_encrypt(const unsigned char *in, 252 unsigned char *out, 253 const unsigned char ivec[8], 254 const unsigned char nonce[4], 255 unsigned long length, 256 const aes_encrypt_ctx cx[1]); 257 258 # else 259 260 void aes_CBC_encrypt(const unsigned char *in, 261 unsigned char *out, 262 unsigned char ivec[16], 263 unsigned long length, 264 unsigned char *key, 265 int number_of_rounds); 266 267 void aes_CBC_decrypt(const unsigned char *in, 268 unsigned char *out, 269 unsigned char ivec[16], 270 unsigned long length, 271 unsigned char *key, 272 int number_of_rounds); 273 274 void aes_CTR_encrypt(const unsigned char *in, 275 unsigned char *out, 276 const unsigned char ivec[8], 277 const unsigned char nonce[4], 278 unsigned long length, 279 const unsigned char *key, 280 int number_of_rounds); 281 282 # endif 283 #endif 284 285 #if defined(__cplusplus) 286 } 287 #endif 288 289 #endif 290