libbtc
bitcoinclibrary
aes.h
Go to the documentation of this file.
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  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
25  * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27  * OTHER DEALINGS IN THE SOFTWARE.
28 
29  ---------------------------------------------------------------------------
30  Issue 09/09/2006
31 
32  This is an AES implementation that uses only 8-bit byte operations on the
33  cipher state.
34  */
35 
36 #ifndef AES_H
37 #define AES_H
38 
39 #if 1
40 #define AES_ENC_PREKEYED /* AES encryption with a precomputed key schedule */
41 #endif
42 #if 1
43 #define AES_DEC_PREKEYED /* AES decryption with a precomputed key schedule */
44 #endif
45 #if 0
46 #define AES_ENC_128_OTFK /* AES encryption with 'on the fly' 128 bit keying */
47 #endif
48 #if 0
49 #define AES_DEC_128_OTFK /* AES decryption with 'on the fly' 128 bit keying */
50 #endif
51 #if 0
52 #define AES_ENC_256_OTFK /* AES encryption with 'on the fly' 256 bit keying */
53 #endif
54 #if 0
55 #define AES_DEC_256_OTFK /* AES decryption with 'on the fly' 256 bit keying */
56 #endif
57 
58 #define N_ROW 4
59 #define N_COL 4
60 #define N_BLOCK (N_ROW * N_COL)
61 #define N_MAX_ROUNDS 14
62 
63 typedef unsigned char uint_8t;
64 
66 
67 /* Warning: The key length for 256 bit keys overflows a byte
68  (see comment below)
69 */
70 
72 
73 typedef struct
74 {
75  uint_8t ksch[(N_MAX_ROUNDS + 1) * N_BLOCK];
77 } aes_context;
78 
79 /* The following calls are for a precomputed key schedule
80 
81  NOTE: If the length_type used for the key length is an
82  unsigned 8-bit character, a key length of 256 bits must
83  be entered as a length in bytes (valid inputs are hence
84  128, 192, 16, 24 and 32).
85 */
86 
87 #if defined(AES_ENC_PREKEYED) || defined(AES_DEC_PREKEYED)
88 
89 return_type aes_set_key(const unsigned char key[],
90  length_type keylen,
91  aes_context ctx[1]);
92 #endif
93 
94 #if defined(AES_ENC_PREKEYED)
95 
96 return_type aes_encrypt(const unsigned char in[N_BLOCK],
97  unsigned char out[N_BLOCK],
98  const aes_context ctx[1]);
99 
100 return_type aes_cbc_encrypt(const unsigned char* in,
101  unsigned char* out,
102  int n_block,
103  unsigned char iv[N_BLOCK],
104  const aes_context ctx[1]);
105 #endif
106 
107 #if defined(AES_DEC_PREKEYED)
108 
109 return_type aes_decrypt(const unsigned char in[N_BLOCK],
110  unsigned char out[N_BLOCK],
111  const aes_context ctx[1]);
112 
113 return_type aes_cbc_decrypt(const unsigned char* in,
114  unsigned char* out,
115  int n_block,
116  unsigned char iv[N_BLOCK],
117  const aes_context ctx[1]);
118 #endif
119 
120 /* The following calls are for 'on the fly' keying. In this case the
121  encryption and decryption keys are different.
122 
123  The encryption subroutines take a key in an array of bytes in
124  key[L] where L is 16, 24 or 32 bytes for key lengths of 128,
125  192, and 256 bits respectively. They then encrypts the input
126  data, in[] with this key and put the reult in the output array
127  out[]. In addition, the second key array, o_key[L], is used
128  to output the key that is needed by the decryption subroutine
129  to reverse the encryption operation. The two key arrays can
130  be the same array but in this case the original key will be
131  overwritten.
132 
133  In the same way, the decryption subroutines output keys that
134  can be used to reverse their effect when used for encryption.
135 
136  Only 128 and 256 bit keys are supported in these 'on the fly'
137  modes.
138 */
139 
140 #if defined(AES_ENC_128_OTFK)
141 void aes_encrypt_128(const unsigned char in[N_BLOCK],
142  unsigned char out[N_BLOCK],
143  const unsigned char key[N_BLOCK],
144  uint_8t o_key[N_BLOCK]);
145 #endif
146 
147 #if defined(AES_DEC_128_OTFK)
148 void aes_decrypt_128(const unsigned char in[N_BLOCK],
149  unsigned char out[N_BLOCK],
150  const unsigned char key[N_BLOCK],
151  unsigned char o_key[N_BLOCK]);
152 #endif
153 
154 #if defined(AES_ENC_256_OTFK)
155 void aes_encrypt_256(const unsigned char in[N_BLOCK],
156  unsigned char out[N_BLOCK],
157  const unsigned char key[2 * N_BLOCK],
158  unsigned char o_key[2 * N_BLOCK]);
159 #endif
160 
161 #if defined(AES_DEC_256_OTFK)
162 void aes_decrypt_256(const unsigned char in[N_BLOCK],
163  unsigned char out[N_BLOCK],
164  const unsigned char key[2 * N_BLOCK],
165  unsigned char o_key[2 * N_BLOCK]);
166 #endif
167 
168 
169 #endif
return_type aes_decrypt(const unsigned char in[N_BLOCK], unsigned char out[N_BLOCK], const aes_context ctx[1])
Definition: aes.c:557
#define N_MAX_ROUNDS
Definition: aes.h:61
return_type aes_set_key(const unsigned char key[], length_type keylen, aes_context ctx[1])
Definition: aes.c:449
return_type aes_cbc_decrypt(const unsigned char *in, unsigned char *out, int n_block, unsigned char iv[N_BLOCK], const aes_context ctx[1])
Definition: aes.c:586
uint_8t rnd
Definition: aes.h:76
return_type aes_cbc_encrypt(const unsigned char *in, unsigned char *out, int n_block, unsigned char iv[N_BLOCK], const aes_context ctx[1])
Definition: aes.c:537
return_type aes_encrypt(const unsigned char in[N_BLOCK], unsigned char out[N_BLOCK], const aes_context ctx[1])
Definition: aes.c:508
uint_8t length_type
Definition: aes.h:71
unsigned char uint_8t
Definition: aes.h:63
uint_8t return_type
Definition: aes.h:65
#define N_BLOCK
Definition: aes.h:60