blob: 40927fb7eb3799a01304de0e4296255b49076c73 [file] [log] [blame]
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -05001/*
2---------------------------------------------------------------------------
3Copyright (c) 1998-2010, Brian Gladman, Worcester, UK. All rights reserved.
4
5The redistribution and use of this software (with or without changes)
6is 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
14This software is provided 'as is' with no explicit or implied warranties
15in respect of its operation, including, but not limited to, correctness
16and fitness for purpose.
17---------------------------------------------------------------------------
18Issue Date: 20/12/2007
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#include <stdlib.h>
28
29/* This include is used to find 8 & 32 bit unsigned integer types */
30#include "brg_types.h"
31
32#if defined(__cplusplus)
33extern "C"
34{
35#endif
36
37#define AES_128 /* if a fast 128 bit key scheduler is needed */
38#define AES_192 /* if a fast 192 bit key scheduler is needed */
39#define AES_256 /* if a fast 256 bit key scheduler is needed */
40#define AES_VAR /* if variable key size scheduler is needed */
41#define AES_MODES /* if support is needed for modes */
42
43/* The following must also be set in assembler files if being used */
44
45#define AES_ENCRYPT /* if support for encryption is needed */
46#define AES_DECRYPT /* if support for decryption is needed */
47#define AES_REV_DKS /* define to reverse decryption key schedule */
48
49#define AES_BLOCK_SIZE 16 /* the AES block size in bytes */
50#define N_COLS 4 /* the number of columns in the state */
51
52/* The key schedule length is 11, 13 or 15 16-byte blocks for 128, */
53/* 192 or 256-bit keys respectively. That is 176, 208 or 240 bytes */
54/* or 44, 52 or 60 32-bit words. */
55
56#if defined( AES_VAR ) || defined( AES_256 )
57#define KS_LENGTH 60
58#elif defined( AES_192 )
59#define KS_LENGTH 52
60#else
61#define KS_LENGTH 44
62#endif
63
64#define AES_RETURN INT_RETURN
65
66/* the character array 'inf' in the following structures is used */
67/* to hold AES context information. This AES code uses cx->inf.b[0] */
68/* to hold the number of rounds multiplied by 16. The other three */
69/* elements can be used by code that implements additional modes */
70
71typedef union
72{ uint_32t l;
73 uint_8t b[4];
74} aes_inf;
75
76typedef struct
77{ uint_32t ks[KS_LENGTH];
78 aes_inf inf;
79} aes_encrypt_ctx;
80
81typedef struct
82{ uint_32t ks[KS_LENGTH];
83 aes_inf inf;
84} aes_decrypt_ctx;
85
86/* This routine must be called before first use if non-static */
87/* tables are being used */
88
89AES_RETURN aes_init(void);
90
91/* Key lengths in the range 16 <= key_len <= 32 are given in bytes, */
92/* those in the range 128 <= key_len <= 256 are given in bits */
93
94#if defined( AES_ENCRYPT )
95
96#if defined( AES_128 ) || defined( AES_VAR)
97AES_RETURN aes_encrypt_key128(const unsigned char *key, aes_encrypt_ctx cx[1]);
98#endif
99
100#if defined( AES_192 ) || defined( AES_VAR)
101AES_RETURN aes_encrypt_key192(const unsigned char *key, aes_encrypt_ctx cx[1]);
102#endif
103
104#if defined( AES_256 ) || defined( AES_VAR)
105AES_RETURN aes_encrypt_key256(const unsigned char *key, aes_encrypt_ctx cx[1]);
106#endif
107
108#if defined( AES_VAR )
109AES_RETURN aes_encrypt_key(const unsigned char *key, int key_len, aes_encrypt_ctx cx[1]);
110#endif
111
112AES_RETURN aes_encrypt(const unsigned char *in, unsigned char *out, const aes_encrypt_ctx cx[1]);
113
114#endif
115
116#if defined( AES_DECRYPT )
117
118#if defined( AES_128 ) || defined( AES_VAR)
119AES_RETURN aes_decrypt_key128(const unsigned char *key, aes_decrypt_ctx cx[1]);
120#endif
121
122#if defined( AES_192 ) || defined( AES_VAR)
123AES_RETURN aes_decrypt_key192(const unsigned char *key, aes_decrypt_ctx cx[1]);
124#endif
125
126#if defined( AES_256 ) || defined( AES_VAR)
127AES_RETURN aes_decrypt_key256(const unsigned char *key, aes_decrypt_ctx cx[1]);
128#endif
129
130#if defined( AES_VAR )
131AES_RETURN aes_decrypt_key(const unsigned char *key, int key_len, aes_decrypt_ctx cx[1]);
132#endif
133
134AES_RETURN aes_decrypt(const unsigned char *in, unsigned char *out, const aes_decrypt_ctx cx[1]);
135
136#endif
137
138#if defined( AES_MODES )
139
140/* Multiple calls to the following subroutines for multiple block */
141/* ECB, CBC, CFB, OFB and CTR mode encryption can be used to handle */
142/* long messages incremantally provided that the context AND the iv */
143/* are preserved between all such calls. For the ECB and CBC modes */
144/* each individual call within a series of incremental calls must */
145/* process only full blocks (i.e. len must be a multiple of 16) but */
146/* the CFB, OFB and CTR mode calls can handle multiple incremental */
147/* calls of any length. Each mode is reset when a new AES key is */
148/* set but ECB and CBC operations can be reset without setting a */
149/* new key by setting a new IV value. To reset CFB, OFB and CTR */
150/* without setting the key, aes_mode_reset() must be called and the */
151/* IV must be set. NOTE: All these calls update the IV on exit so */
152/* this has to be reset if a new operation with the same IV as the */
153/* previous one is required (or decryption follows encryption with */
154/* the same IV array). */
155
156AES_RETURN aes_test_alignment_detection(unsigned int n);
157
158AES_RETURN aes_ecb_encrypt(const unsigned char *ibuf, unsigned char *obuf,
159 int len, const aes_encrypt_ctx cx[1]);
160
161AES_RETURN aes_ecb_decrypt(const unsigned char *ibuf, unsigned char *obuf,
162 int len, const aes_decrypt_ctx cx[1]);
163
164AES_RETURN aes_cbc_encrypt(const unsigned char *ibuf, unsigned char *obuf,
165 int len, unsigned char *iv, const aes_encrypt_ctx cx[1]);
166
167AES_RETURN aes_cbc_decrypt(const unsigned char *ibuf, unsigned char *obuf,
168 int len, unsigned char *iv, const aes_decrypt_ctx cx[1]);
169
170AES_RETURN aes_mode_reset(aes_encrypt_ctx cx[1]);
171
172AES_RETURN aes_cfb_encrypt(const unsigned char *ibuf, unsigned char *obuf,
173 int len, unsigned char *iv, aes_encrypt_ctx cx[1]);
174
175AES_RETURN aes_cfb_decrypt(const unsigned char *ibuf, unsigned char *obuf,
176 int len, unsigned char *iv, aes_encrypt_ctx cx[1]);
177
178#define aes_ofb_encrypt aes_ofb_crypt
179#define aes_ofb_decrypt aes_ofb_crypt
180
181AES_RETURN aes_ofb_crypt(const unsigned char *ibuf, unsigned char *obuf,
182 int len, unsigned char *iv, aes_encrypt_ctx cx[1]);
183
184typedef void cbuf_inc(unsigned char *cbuf);
185
186#define aes_ctr_encrypt aes_ctr_crypt
187#define aes_ctr_decrypt aes_ctr_crypt
188
189AES_RETURN aes_ctr_crypt(const unsigned char *ibuf, unsigned char *obuf,
190 int len, unsigned char *cbuf, cbuf_inc ctr_inc, aes_encrypt_ctx cx[1]);
191
192#endif
193
194#if defined(__cplusplus)
195}
196#endif
197
198#endif