xref: /optee_os/lib/libmbedtls/mbedtls/include/mbedtls/aes.h (revision c6672fdcd95b9a895eb5b4191f8ba3483a34a442)
1 /* SPDX-License-Identifier: Apache-2.0 */
2 /**
3  * \file aes.h
4  *
5  * \brief AES block cipher
6  *
7  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
8  *
9  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
10  *  not use this file except in compliance with the License.
11  *  You may obtain a copy of the License at
12  *
13  *  http://www.apache.org/licenses/LICENSE-2.0
14  *
15  *  Unless required by applicable law or agreed to in writing, software
16  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  *  See the License for the specific language governing permissions and
19  *  limitations under the License.
20  *
21  *  This file is part of mbed TLS (https://tls.mbed.org)
22  */
23 #ifndef MBEDTLS_AES_H
24 #define MBEDTLS_AES_H
25 
26 #if !defined(MBEDTLS_CONFIG_FILE)
27 #include "config.h"
28 #else
29 #include MBEDTLS_CONFIG_FILE
30 #endif
31 
32 #include <stddef.h>
33 #include <stdint.h>
34 
35 /* padlock.c and aesni.c rely on these values! */
36 #define MBEDTLS_AES_ENCRYPT     1
37 #define MBEDTLS_AES_DECRYPT     0
38 
39 #define MBEDTLS_ERR_AES_INVALID_KEY_LENGTH                -0x0020  /**< Invalid key length. */
40 #define MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH              -0x0022  /**< Invalid data input length. */
41 
42 #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
43     !defined(inline) && !defined(__cplusplus)
44 #define inline __inline
45 #endif
46 
47 #if !defined(MBEDTLS_AES_ALT)
48 // Regular implementation
49 //
50 
51 #ifdef __cplusplus
52 extern "C" {
53 #endif
54 
55 /**
56  * \brief          AES context structure
57  *
58  * \note           buf is able to hold 32 extra bytes, which can be used:
59  *                 - for alignment purposes if VIA padlock is used, and/or
60  *                 - to simplify key expansion in the 256-bit case by
61  *                 generating an extra round key
62  */
63 typedef struct
64 {
65     int nr;                     /*!<  number of rounds  */
66     uint32_t *rk;               /*!<  AES round keys    */
67     uint32_t buf[68];           /*!<  unaligned data    */
68 }
69 mbedtls_aes_context;
70 
71 /**
72  * \brief          Initialize AES context
73  *
74  * \param ctx      AES context to be initialized
75  */
76 void mbedtls_aes_init( mbedtls_aes_context *ctx );
77 
78 /**
79  * \brief          Clear AES context
80  *
81  * \param ctx      AES context to be cleared
82  */
83 void mbedtls_aes_free( mbedtls_aes_context *ctx );
84 
85 /**
86  * \brief          AES key schedule (encryption)
87  *
88  * \param ctx      AES context to be initialized
89  * \param key      encryption key
90  * \param keybits  must be 128, 192 or 256
91  *
92  * \return         0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
93  */
94 int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key,
95                     unsigned int keybits );
96 
97 /**
98  * \brief          AES key schedule (decryption)
99  *
100  * \param ctx      AES context to be initialized
101  * \param key      decryption key
102  * \param keybits  must be 128, 192 or 256
103  *
104  * \return         0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
105  */
106 int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx, const unsigned char *key,
107                     unsigned int keybits );
108 
109 /**
110  * \brief          AES-ECB block encryption/decryption
111  *
112  * \param ctx      AES context
113  * \param mode     MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
114  * \param input    16-byte input block
115  * \param output   16-byte output block
116  *
117  * \return         0 if successful
118  */
119 int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx,
120                     int mode,
121                     const unsigned char input[16],
122                     unsigned char output[16] );
123 
124 #if defined(MBEDTLS_CIPHER_MODE_CBC)
125 /**
126  * \brief          AES-CBC buffer encryption/decryption
127  *                 Length should be a multiple of the block
128  *                 size (16 bytes)
129  *
130  * \note           Upon exit, the content of the IV is updated so that you can
131  *                 call the function same function again on the following
132  *                 block(s) of data and get the same result as if it was
133  *                 encrypted in one call. This allows a "streaming" usage.
134  *                 If on the other hand you need to retain the contents of the
135  *                 IV, you should either save it manually or use the cipher
136  *                 module instead.
137  *
138  * \param ctx      AES context
139  * \param mode     MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
140  * \param length   length of the input data
141  * \param iv       initialization vector (updated after use)
142  * \param input    buffer holding the input data
143  * \param output   buffer holding the output data
144  *
145  * \return         0 if successful, or MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH
146  */
147 int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
148                     int mode,
149                     size_t length,
150                     unsigned char iv[16],
151                     const unsigned char *input,
152                     unsigned char *output );
153 #endif /* MBEDTLS_CIPHER_MODE_CBC */
154 
155 #if defined(MBEDTLS_CIPHER_MODE_CFB)
156 /**
157  * \brief          AES-CFB128 buffer encryption/decryption.
158  *
159  * Note: Due to the nature of CFB you should use the same key schedule for
160  * both encryption and decryption. So a context initialized with
161  * mbedtls_aes_setkey_enc() for both MBEDTLS_AES_ENCRYPT and MBEDTLS_AES_DECRYPT.
162  *
163  * \note           Upon exit, the content of the IV is updated so that you can
164  *                 call the function same function again on the following
165  *                 block(s) of data and get the same result as if it was
166  *                 encrypted in one call. This allows a "streaming" usage.
167  *                 If on the other hand you need to retain the contents of the
168  *                 IV, you should either save it manually or use the cipher
169  *                 module instead.
170  *
171  * \param ctx      AES context
172  * \param mode     MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
173  * \param length   length of the input data
174  * \param iv_off   offset in IV (updated after use)
175  * \param iv       initialization vector (updated after use)
176  * \param input    buffer holding the input data
177  * \param output   buffer holding the output data
178  *
179  * \return         0 if successful
180  */
181 int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
182                        int mode,
183                        size_t length,
184                        size_t *iv_off,
185                        unsigned char iv[16],
186                        const unsigned char *input,
187                        unsigned char *output );
188 
189 /**
190  * \brief          AES-CFB8 buffer encryption/decryption.
191  *
192  * Note: Due to the nature of CFB you should use the same key schedule for
193  * both encryption and decryption. So a context initialized with
194  * mbedtls_aes_setkey_enc() for both MBEDTLS_AES_ENCRYPT and MBEDTLS_AES_DECRYPT.
195  *
196  * \note           Upon exit, the content of the IV is updated so that you can
197  *                 call the function same function again on the following
198  *                 block(s) of data and get the same result as if it was
199  *                 encrypted in one call. This allows a "streaming" usage.
200  *                 If on the other hand you need to retain the contents of the
201  *                 IV, you should either save it manually or use the cipher
202  *                 module instead.
203  *
204  * \param ctx      AES context
205  * \param mode     MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
206  * \param length   length of the input data
207  * \param iv       initialization vector (updated after use)
208  * \param input    buffer holding the input data
209  * \param output   buffer holding the output data
210  *
211  * \return         0 if successful
212  */
213 int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx,
214                     int mode,
215                     size_t length,
216                     unsigned char iv[16],
217                     const unsigned char *input,
218                     unsigned char *output );
219 #endif /*MBEDTLS_CIPHER_MODE_CFB */
220 
221 #if defined(MBEDTLS_CIPHER_MODE_CTR)
222 /**
223  * \brief               AES-CTR buffer encryption/decryption
224  *
225  * Warning: You have to keep the maximum use of your counter in mind!
226  *
227  * Note: Due to the nature of CTR you should use the same key schedule for
228  * both encryption and decryption. So a context initialized with
229  * mbedtls_aes_setkey_enc() for both MBEDTLS_AES_ENCRYPT and MBEDTLS_AES_DECRYPT.
230  *
231  * \param ctx           AES context
232  * \param length        The length of the data
233  * \param nc_off        The offset in the current stream_block (for resuming
234  *                      within current cipher stream). The offset pointer to
235  *                      should be 0 at the start of a stream.
236  * \param nonce_counter The 128-bit nonce and counter.
237  * \param stream_block  The saved stream-block for resuming. Is overwritten
238  *                      by the function.
239  * \param input         The input data stream
240  * \param output        The output data stream
241  *
242  * \return         0 if successful
243  */
244 int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
245                        size_t length,
246                        size_t *nc_off,
247                        unsigned char nonce_counter[16],
248                        unsigned char stream_block[16],
249                        const unsigned char *input,
250                        unsigned char *output );
251 #endif /* MBEDTLS_CIPHER_MODE_CTR */
252 
253 /**
254  * \brief           Internal AES block encryption function
255  *                  (Only exposed to allow overriding it,
256  *                  see MBEDTLS_AES_ENCRYPT_ALT)
257  *
258  * \param ctx       AES context
259  * \param input     Plaintext block
260  * \param output    Output (ciphertext) block
261  *
262  * \return          0 if successful
263  */
264 int mbedtls_internal_aes_encrypt( mbedtls_aes_context *ctx,
265                                   const unsigned char input[16],
266                                   unsigned char output[16] );
267 
268 /**
269  * \brief           Internal AES block decryption function
270  *                  (Only exposed to allow overriding it,
271  *                  see MBEDTLS_AES_DECRYPT_ALT)
272  *
273  * \param ctx       AES context
274  * \param input     Ciphertext block
275  * \param output    Output (plaintext) block
276  *
277  * \return          0 if successful
278  */
279 int mbedtls_internal_aes_decrypt( mbedtls_aes_context *ctx,
280                                   const unsigned char input[16],
281                                   unsigned char output[16] );
282 
283 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
284 #if defined(MBEDTLS_DEPRECATED_WARNING)
285 #define MBEDTLS_DEPRECATED      __attribute__((deprecated))
286 #else
287 #define MBEDTLS_DEPRECATED
288 #endif
289 /**
290  * \brief           Deprecated internal AES block encryption function
291  *                  without return value.
292  *
293  * \deprecated      Superseded by mbedtls_aes_encrypt_ext() in 2.5.0
294  *
295  * \param ctx       AES context
296  * \param input     Plaintext block
297  * \param output    Output (ciphertext) block
298  */
299 MBEDTLS_DEPRECATED void mbedtls_aes_encrypt( mbedtls_aes_context *ctx,
300                                              const unsigned char input[16],
301                                              unsigned char output[16] );
302 
303 /**
304  * \brief           Deprecated internal AES block decryption function
305  *                  without return value.
306  *
307  * \deprecated      Superseded by mbedtls_aes_decrypt_ext() in 2.5.0
308  *
309  * \param ctx       AES context
310  * \param input     Ciphertext block
311  * \param output    Output (plaintext) block
312  */
313 MBEDTLS_DEPRECATED void mbedtls_aes_decrypt( mbedtls_aes_context *ctx,
314                                              const unsigned char input[16],
315                                              unsigned char output[16] );
316 
317 #undef MBEDTLS_DEPRECATED
318 #endif /* !MBEDTLS_DEPRECATED_REMOVED */
319 
320 #ifdef __cplusplus
321 }
322 #endif
323 
324 #else  /* MBEDTLS_AES_ALT */
325 #include "aes_alt.h"
326 #endif /* MBEDTLS_AES_ALT */
327 
328 #ifdef __cplusplus
329 extern "C" {
330 #endif
331 
332 /**
333  * \brief          Checkup routine
334  *
335  * \return         0 if successful, or 1 if the test failed
336  */
337 int mbedtls_aes_self_test( int verbose );
338 
339 #ifdef __cplusplus
340 }
341 #endif
342 
343 #endif /* aes.h */
344