• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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,
84                         aes_context ctx[1]);
85 #endif
86 
87 #if defined(AES_ENC_PREKEYED)
88 
89 return_type aes_encrypt(const unsigned char in[N_BLOCK],
90                         unsigned char out[N_BLOCK], const aes_context ctx[1]);
91 
92 return_type aes_cbc_encrypt(const unsigned char* in, unsigned char* out,
93                             int n_block, unsigned char iv[N_BLOCK],
94                             const aes_context ctx[1]);
95 #endif
96 
97 #if defined(AES_DEC_PREKEYED)
98 
99 return_type aes_decrypt(const unsigned char in[N_BLOCK],
100                         unsigned char out[N_BLOCK], const aes_context ctx[1]);
101 
102 return_type aes_cbc_decrypt(const unsigned char* in, unsigned char* out,
103                             int n_block, unsigned char iv[N_BLOCK],
104                             const aes_context ctx[1]);
105 #endif
106 
107 /*  The following calls are for 'on the fly' keying.  In this case the
108     encryption and decryption keys are different.
109 
110     The encryption subroutines take a key in an array of bytes in
111     key[L] where L is 16, 24 or 32 bytes for key lengths of 128,
112     192, and 256 bits respectively.  They then encrypts the input
113     data, in[] with this key and put the reult in the output array
114     out[].  In addition, the second key array, o_key[L], is used
115     to output the key that is needed by the decryption subroutine
116     to reverse the encryption operation.  The two key arrays can
117     be the same array but in this case the original key will be
118     overwritten.
119 
120     In the same way, the decryption subroutines output keys that
121     can be used to reverse their effect when used for encryption.
122 
123     Only 128 and 256 bit keys are supported in these 'on the fly'
124     modes.
125 */
126 
127 #if defined(AES_ENC_128_OTFK)
128 void aes_encrypt_128(const unsigned char in[N_BLOCK],
129                      unsigned char out[N_BLOCK],
130                      const unsigned char key[N_BLOCK], uint_8t o_key[N_BLOCK]);
131 #endif
132 
133 #if defined(AES_DEC_128_OTFK)
134 void aes_decrypt_128(const unsigned char in[N_BLOCK],
135                      unsigned char out[N_BLOCK],
136                      const unsigned char key[N_BLOCK],
137                      unsigned char o_key[N_BLOCK]);
138 #endif
139 
140 #if defined(AES_ENC_256_OTFK)
141 void aes_encrypt_256(const unsigned char in[N_BLOCK],
142                      unsigned char out[N_BLOCK],
143                      const unsigned char key[2 * N_BLOCK],
144                      unsigned char o_key[2 * N_BLOCK]);
145 #endif
146 
147 #if defined(AES_DEC_256_OTFK)
148 void aes_decrypt_256(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