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

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.