PicoWAN SDK Documentation
aes.h
Go to the documentation of this file.
1 
35 #ifndef AES_H
36 #define AES_H
37 
38 #include <stdint.h>
39 
40 #if 1
41 # define AES_ENC_PREKEYED /* AES encryption with a precomputed key schedule */
42 #endif
43 #if 1
44 # define AES_DEC_PREKEYED /* AES decryption with a precomputed key schedule */
45 #endif
46 #if 0
47 # define AES_ENC_128_OTFK /* AES encryption with 'on the fly' 128 bit keying */
48 #endif
49 #if 0
50 # define AES_DEC_128_OTFK /* AES decryption with 'on the fly' 128 bit keying */
51 #endif
52 #if 0
53 # define AES_ENC_256_OTFK /* AES encryption with 'on the fly' 256 bit keying */
54 #endif
55 #if 0
56 # define AES_DEC_256_OTFK /* AES decryption with 'on the fly' 256 bit keying */
57 #endif
58 
59 #define N_ROW 4
60 #define N_COL 4
61 #define N_BLOCK (N_ROW * N_COL)
62 #define N_MAX_ROUNDS 14
63 
64 typedef uint8_t return_type;
65 
66 /* Warning: The key length for 256 bit keys overflows a byte
67  (see comment below)
68 */
69 
70 typedef uint8_t length_type;
71 
72 typedef struct
73 { uint8_t ksch[(N_MAX_ROUNDS + 1) * N_BLOCK];
74  uint8_t rnd;
75 } aes_context;
76 
77 /* The following calls are for a precomputed key schedule
78 
79  NOTE: If the length_type used for the key length is an
80  unsigned 8-bit character, a key length of 256 bits must
81  be entered as a length in bytes (valid inputs are hence
82  128, 192, 16, 24 and 32).
83 */
84 
85 #if defined( AES_ENC_PREKEYED ) || defined( AES_DEC_PREKEYED )
86 
96 return_type aes_set_key( const uint8_t key[],
97  length_type keylen,
98  aes_context ctx[1] );
99 #endif
100 
101 #if defined( AES_ENC_PREKEYED )
102 
112 return_type aes_encrypt( const uint8_t in[N_BLOCK],
113  uint8_t out[N_BLOCK],
114  const aes_context ctx[1] );
115 
126 return_type aes_encrypt_all( const uint8_t *in,
127  uint8_t *out,
128  uint32_t len,
129  const aes_context ctx[1] );
130 
131 return_type aes_cbc_encrypt( const uint8_t *in,
132  uint8_t *out,
133  int32_t n_block,
134  uint8_t iv[N_BLOCK],
135  const aes_context ctx[1] );
136 #endif
137 
138 #if defined( AES_DEC_PREKEYED )
139 
149 return_type aes_decrypt( const uint8_t in[N_BLOCK],
150  uint8_t out[N_BLOCK],
151  const aes_context ctx[1] );
152 
163 return_type aes_decrypt_all( const uint8_t *in,
164  uint8_t *out,
165  uint32_t len,
166  const aes_context ctx[1] );
167 
168 return_type aes_cbc_decrypt( const uint8_t *in,
169  uint8_t *out,
170  int32_t n_block,
171  uint8_t iv[N_BLOCK],
172  const aes_context ctx[1] );
173 #endif
174 
175 /* The following calls are for 'on the fly' keying. In this case the
176  encryption and decryption keys are different.
177 
178  The encryption subroutines take a key in an array of bytes in
179  key[L] where L is 16, 24 or 32 bytes for key lengths of 128,
180  192, and 256 bits respectively. They then encrypts the input
181  data, in[] with this key and put the reult in the output array
182  out[]. In addition, the second key array, o_key[L], is used
183  to output the key that is needed by the decryption subroutine
184  to reverse the encryption operation. The two key arrays can
185  be the same array but in this case the original key will be
186  overwritten.
187 
188  In the same way, the decryption subroutines output keys that
189  can be used to reverse their effect when used for encryption.
190 
191  Only 128 and 256 bit keys are supported in these 'on the fly'
192  modes.
193 */
194 
195 #if defined( AES_ENC_128_OTFK )
196 void aes_encrypt_128( const uint8_t in[N_BLOCK],
197  uint8_t out[N_BLOCK],
198  const uint8_t key[N_BLOCK],
199  uint8_t o_key[N_BLOCK] );
200 #endif
201 
202 #if defined( AES_DEC_128_OTFK )
203 void aes_decrypt_128( const uint8_t in[N_BLOCK],
204  uint8_t out[N_BLOCK],
205  const uint8_t key[N_BLOCK],
206  uint8_t o_key[N_BLOCK] );
207 #endif
208 
209 #if defined( AES_ENC_256_OTFK )
210 void aes_encrypt_256( const uint8_t in[N_BLOCK],
211  uint8_t out[N_BLOCK],
212  const uint8_t key[2 * N_BLOCK],
213  uint8_t o_key[2 * N_BLOCK] );
214 #endif
215 
216 #if defined( AES_DEC_256_OTFK )
217 void aes_decrypt_256( const uint8_t in[N_BLOCK],
218  uint8_t out[N_BLOCK],
219  const uint8_t key[2 * N_BLOCK],
220  uint8_t o_key[2 * N_BLOCK] );
221 #endif
222 
223 #endif
return_type aes_encrypt_all(const uint8_t *in, uint8_t *out, uint32_t len, const aes_context ctx[1])
Encrypts a buffer of n x 16 bytes.
Definition: aes.c:599
Definition: aes.h:72
return_type aes_encrypt(const uint8_t in[N_BLOCK], uint8_t out[N_BLOCK], const aes_context ctx[1])
Encrypts a single block of 16 bytes.
Definition: aes.c:572
return_type aes_decrypt(const uint8_t in[N_BLOCK], uint8_t out[N_BLOCK], const aes_context ctx[1])
Decrypts a single block of 16 bytes.
Definition: aes.c:643
return_type aes_set_key(const uint8_t key[], length_type keylen, aes_context ctx[1])
Sets the key for AES encryption/decryption.
Definition: aes.c:517
return_type aes_decrypt_all(const uint8_t *in, uint8_t *out, uint32_t len, const aes_context ctx[1])
Decrypts a buffer of n x 16 bytes.
Definition: aes.c:670