Mbed OS Reference
Loading...
Searching...
No Matches
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#if defined(MBEDTLS_AES_ALT)
24/* SiliconLabs CRYPTO hardware acceleration implementation */
25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
30/**
31 * \brief AES context structure
32 */
33typedef struct
34{
35 unsigned int keybits; /*!< size of key */
36 unsigned char key[32]; /*!< AES key 128 or 256 bits */
37}
39
40/**
41 * \brief Initialize AES context
42 *
43 * \param ctx AES context to be initialized
44 */
46
47/**
48 * \brief Clear AES context
49 *
50 * \param ctx AES context to be cleared
51 */
53
54/**
55 * \brief AES key schedule (encryption)
56 *
57 * \param ctx AES context to be initialized
58 * \param key encryption key
59 * \param keybits must be 128 or 256
60 *
61 * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
62 */
63int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key,
64 unsigned int keybits );
65
66/**
67 * \brief AES key schedule (decryption)
68 *
69 * \param ctx AES context to be initialized
70 * \param key decryption key
71 * \param keybits must be 128 or 256
72 *
73 * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
74 */
75int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx, const unsigned char *key,
76 unsigned int keybits );
77
78/**
79 * \brief AES-ECB block encryption/decryption
80 *
81 * \param ctx AES context
82 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
83 * \param input 16-byte input block
84 * \param output 16-byte output block
85 *
86 * \return 0 if successful
87 */
89 int mode,
90 const unsigned char input[16],
91 unsigned char output[16] );
92
93#if defined(MBEDTLS_CIPHER_MODE_CBC)
94/**
95 * \brief AES-CBC buffer encryption/decryption
96 * Length should be a multiple of the block
97 * size (16 bytes)
98 *
99 * \note Upon exit, the content of the IV is updated so that you can
100 * call the same function again on the following block(s) of
101 * data and get the same result as if it was encrypted in one
102 * call. This allows a "streaming" usage.
103 * If on the other hand you need to retain the contents of the
104 * IV, you should either save it manually or use the cipher
105 * module instead.
106 *
107 * \param ctx AES context
108 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
109 * \param length length of the input data
110 * \param iv initialization vector (updated after use)
111 * \param input buffer holding the input data
112 * \param output buffer holding the output data
113 *
114 * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH
115 */
116int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
117 int mode,
118 size_t length,
119 unsigned char iv[16],
120 const unsigned char *input,
121 unsigned char *output );
122#endif /* MBEDTLS_CIPHER_MODE_CBC */
123
124#if defined(MBEDTLS_CIPHER_MODE_CFB)
125/**
126 * \brief AES-CFB128 buffer encryption/decryption.
127 *
128 * Note: Due to the nature of CFB you should use the same key schedule for
129 * both encryption and decryption. So a context initialized with
130 * mbedtls_aes_setkey_enc() for both MBEDTLS_AES_ENCRYPT and MBEDTLS_AES_DECRYPT.
131 *
132 * \note Upon exit, the content of the IV is updated so that you can
133 * call the function same function again on the following
134 * block(s) of data and get the same result as if it was
135 * encrypted in one call. This allows a "streaming" usage.
136 * If on the other hand you need to retain the contents of the
137 * IV, you should either save it manually or use the cipher
138 * module instead.
139 *
140 * \param ctx AES context
141 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
142 * \param length length of the input data
143 * \param iv_off offset in IV (updated after use)
144 * \param iv initialization vector (updated after use)
145 * \param input buffer holding the input data
146 * \param output buffer holding the output data
147 *
148 * \return 0 if successful
149 */
150int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
151 int mode,
152 size_t length,
153 size_t *iv_off,
154 unsigned char iv[16],
155 const unsigned char *input,
156 unsigned char *output );
157
158/**
159 * \brief AES-CFB8 buffer encryption/decryption.
160 *
161 * Note: Due to the nature of CFB you should use the same key schedule for
162 * both encryption and decryption. So a context initialized with
163 * mbedtls_aes_setkey_enc() for both MBEDTLS_AES_ENCRYPT and MBEDTLS_AES_DECRYPT.
164 *
165 * \note Upon exit, the content of the IV is updated so that you can
166 * call the function same function again on the following
167 * block(s) of data and get the same result as if it was
168 * encrypted in one call. This allows a "streaming" usage.
169 * If on the other hand you need to retain the contents of the
170 * IV, you should either save it manually or use the cipher
171 * module instead.
172 *
173 * \param ctx AES context
174 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
175 * \param length length of the input data
176 * \param iv initialization vector (updated after use)
177 * \param input buffer holding the input data
178 * \param output buffer holding the output data
179 *
180 * \return 0 if successful
181 */
182int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx,
183 int mode,
184 size_t length,
185 unsigned char iv[16],
186 const unsigned char *input,
187 unsigned char *output );
188#endif /*MBEDTLS_CIPHER_MODE_CFB */
189
190#if defined(MBEDTLS_CIPHER_MODE_CTR)
191/**
192 * \brief AES-CTR buffer encryption/decryption
193 *
194 * Warning: You have to keep the maximum use of your counter in mind!
195 *
196 * Note: Due to the nature of CTR you should use the same key schedule for
197 * both encryption and decryption. So a context initialized with
198 * mbedtls_aes_setkey_enc() for both MBEDTLS_AES_ENCRYPT and MBEDTLS_AES_DECRYPT.
199 *
200 * \param ctx AES context
201 * \param length The length of the data
202 * \param nc_off The offset in the current stream_block (for resuming
203 * within current cipher stream). The offset pointer to
204 * should be 0 at the start of a stream.
205 * \param nonce_counter The 128-bit nonce and counter.
206 * \param stream_block The saved stream-block for resuming. Is overwritten
207 * by the function.
208 * \param input The input data stream
209 * \param output The output data stream
210 *
211 * \return 0 if successful
212 */
214 size_t length,
215 size_t *nc_off,
216 unsigned char nonce_counter[16],
217 unsigned char stream_block[16],
218 const unsigned char *input,
219 unsigned char *output );
220#endif /* MBEDTLS_CIPHER_MODE_CTR */
221
222/**
223 * \brief Internal AES block encryption function
224 * (Only exposed to allow overriding it,
225 * see MBEDTLS_AES_ENCRYPT_ALT)
226 *
227 * \param ctx AES context
228 * \param input Plaintext block
229 * \param output Output (ciphertext) block
230 *
231 * \return 0 if successful
232 */
234 const unsigned char input[16],
235 unsigned char output[16] );
236
237/**
238 * \brief Internal AES block decryption function
239 * (Only exposed to allow overriding it,
240 * see MBEDTLS_AES_DECRYPT_ALT)
241 *
242 * \param ctx AES context
243 * \param input Ciphertext block
244 * \param output Output (plaintext) block
245 *
246 * \return 0 if successful
247 */
249 const unsigned char input[16],
250 unsigned char output[16] );
251
252#if !defined(MBEDTLS_DEPRECATED_REMOVED)
253#if defined(MBEDTLS_DEPRECATED_WARNING)
254#define MBEDTLS_DEPRECATED __attribute__((deprecated))
255#else
256#define MBEDTLS_DEPRECATED
257#endif
258/**
259 * \brief Internal AES block encryption function
260 * (Only exposed to allow overriding it,
261 * see MBEDTLS_AES_ENCRYPT_ALT)
262 *
263 * \deprecated Superseded by mbedtls_aes_encrypt_ext() in 2.5.0
264 *
265 * \param ctx AES context
266 * \param input Plaintext block
267 * \param output Output (ciphertext) block
268 */
269MBEDTLS_DEPRECATED static inline void mbedtls_aes_encrypt(
271 const unsigned char input[16],
272 unsigned char output[16] )
273{
274 mbedtls_internal_aes_encrypt( ctx, input, output );
275}
276
277/**
278 * \brief Internal AES block decryption function
279 * (Only exposed to allow overriding it,
280 * see MBEDTLS_AES_DECRYPT_ALT)
281 *
282 * \deprecated Superseded by mbedtls_aes_decrypt_ext() in 2.5.0
283 *
284 * \param ctx AES context
285 * \param input Ciphertext block
286 * \param output Output (plaintext) block
287 */
288MBEDTLS_DEPRECATED static inline void mbedtls_aes_decrypt(
290 const unsigned char input[16],
291 unsigned char output[16] )
292{
293 mbedtls_internal_aes_decrypt( ctx, input, output );
294}
295
296#undef MBEDTLS_DEPRECATED
297#endif /* !MBEDTLS_DEPRECATED_REMOVED */
298
299#ifdef __cplusplus
300}
301#endif
302
303#endif /* MBEDTLS_AES_ALT */
304
305
306#endif /* MBEDTLS_AES_ALT_H */
int mbedtls_aes_crypt_ecb(mbedtls_aes_context *ctx, int mode, const unsigned char input[16], unsigned char output[16])
This function performs an AES single-block encryption or decryption operation.
int mbedtls_aes_setkey_dec(mbedtls_aes_context *ctx, const unsigned char *key, unsigned int keybits)
This function sets the decryption key.
int mbedtls_aes_crypt_ctr(mbedtls_aes_context *ctx, size_t length, size_t *nc_off, unsigned char nonce_counter[16], unsigned char stream_block[16], const unsigned char *input, unsigned char *output)
This function performs an AES-CTR encryption or decryption operation.
void mbedtls_aes_encrypt(mbedtls_aes_context *ctx, const unsigned char input[16], unsigned char output[16])
Deprecated internal AES block encryption function without return value.
int mbedtls_internal_aes_encrypt(mbedtls_aes_context *ctx, const unsigned char input[16], unsigned char output[16])
Internal AES block encryption function.
void mbedtls_aes_init(mbedtls_aes_context *ctx)
This function initializes the specified AES context.
int mbedtls_aes_setkey_enc(mbedtls_aes_context *ctx, const unsigned char *key, unsigned int keybits)
This function sets the encryption key.
void mbedtls_aes_free(mbedtls_aes_context *ctx)
This function releases and clears the specified AES context.
int mbedtls_internal_aes_decrypt(mbedtls_aes_context *ctx, const unsigned char input[16], unsigned char output[16])
Internal AES block decryption function.
void mbedtls_aes_decrypt(mbedtls_aes_context *ctx, const unsigned char input[16], unsigned char output[16])
Deprecated internal AES block decryption function without return value.
The AES context-type definition.
Definition: aes.h:93