Mistake on this page?
Report an issue in GitHub or email us
TARGET_NUVOTON/TARGET_M480/aes/aes_alt.h
1 /**
2  * \file aes_alt.h
3  *
4  * \brief AES block cipher
5  *
6  * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
7  * SPDX-License-Identifier: Apache-2.0
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_ALT_H
24 #define MBEDTLS_AES_ALT_H
25 
26 #include "mbedtls/aes.h"
27 
28 #if defined(MBEDTLS_AES_ALT)
29 // Regular implementation
30 //
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
35 /**
36  * \brief AES context structure
37  */
38 typedef struct {
39  uint32_t keySize; /* Key size: AES_KEY_SIZE_128/192/256 */
40  uint32_t encDec; /* 0: decrypt, 1: encrypt */
41  uint32_t opMode; /* AES_MODE_ECB/CBC/CFB */
42  uint32_t iv[4]; /* IV for next block cipher */
43  uint32_t keys[8]; /* Cipher key */
44 }
45 mbedtls_aes_context;
46 
47 /**
48  * \brief Initialize AES context
49  *
50  * \param ctx AES context to be initialized
51  */
52 void mbedtls_aes_init( mbedtls_aes_context *ctx );
53 
54 /**
55  * \brief Clear AES context
56  *
57  * \param ctx AES context to be cleared
58  */
59 void mbedtls_aes_free( mbedtls_aes_context *ctx );
60 
61 /**
62  * \brief AES key schedule (encryption)
63  *
64  * \param ctx AES context to be initialized
65  * \param key encryption key
66  * \param keybits must be 128, 192 or 256
67  *
68  * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
69  */
70 int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key,
71  unsigned int keybits );
72 
73 /**
74  * \brief AES key schedule (decryption)
75  *
76  * \param ctx AES context to be initialized
77  * \param key decryption key
78  * \param keybits must be 128, 192 or 256
79  *
80  * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
81  */
82 int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx, const unsigned char *key,
83  unsigned int keybits );
84 
85 /**
86  * \brief AES-ECB block encryption/decryption
87  *
88  * \param ctx AES context
89  * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
90  * \param input 16-byte input block
91  * \param output 16-byte output block
92  *
93  * \return 0 if successful
94  */
95 int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx,
96  int mode,
97  const unsigned char input[16],
98  unsigned char output[16] );
99 
100 #if defined(MBEDTLS_CIPHER_MODE_CBC)
101 /**
102  * \brief AES-CBC buffer encryption/decryption
103  * Length should be a multiple of the block
104  * size (16 bytes)
105  *
106  * \note Upon exit, the content of the IV is updated so that you can
107  * call the function same function again on the following
108  * block(s) of data and get the same result as if it was
109  * encrypted in one call. This allows a "streaming" usage.
110  * If on the other hand you need to retain the contents of the
111  * IV, you should either save it manually or use the cipher
112  * module instead.
113  *
114  * \param ctx AES context
115  * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
116  * \param length length of the input data
117  * \param iv initialization vector (updated after use)
118  * \param input buffer holding the input data
119  * \param output buffer holding the output data
120  *
121  * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH
122  */
123 int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
124  int mode,
125  size_t length,
126  unsigned char iv[16],
127  const unsigned char *input,
128  unsigned char *output );
129 #endif /* MBEDTLS_CIPHER_MODE_CBC */
130 
131 #if defined(MBEDTLS_CIPHER_MODE_CFB)
132 /**
133  * \brief AES-CFB128 buffer encryption/decryption.
134  *
135  * Note: Due to the nature of CFB you should use the same key schedule for
136  * both encryption and decryption. So a context initialized with
137  * mbedtls_aes_setkey_enc() for both MBEDTLS_AES_ENCRYPT and MBEDTLS_AES_DECRYPT.
138  *
139  * \note Upon exit, the content of the IV is updated so that you can
140  * call the function same function again on the following
141  * block(s) of data and get the same result as if it was
142  * encrypted in one call. This allows a "streaming" usage.
143  * If on the other hand you need to retain the contents of the
144  * IV, you should either save it manually or use the cipher
145  * module instead.
146  *
147  * \param ctx AES context
148  * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
149  * \param length length of the input data
150  * \param iv_off offset in IV (updated after use)
151  * \param iv initialization vector (updated after use)
152  * \param input buffer holding the input data
153  * \param output buffer holding the output data
154  *
155  * \return 0 if successful
156  */
157 int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
158  int mode,
159  size_t length,
160  size_t *iv_off,
161  unsigned char iv[16],
162  const unsigned char *input,
163  unsigned char *output );
164 
165 /**
166  * \brief AES-CFB8 buffer encryption/decryption.
167  *
168  * Note: Due to the nature of CFB you should use the same key schedule for
169  * both encryption and decryption. So a context initialized with
170  * mbedtls_aes_setkey_enc() for both MBEDTLS_AES_ENCRYPT and MBEDTLS_AES_DECRYPT.
171  *
172  * \note Upon exit, the content of the IV is updated so that you can
173  * call the function same function again on the following
174  * block(s) of data and get the same result as if it was
175  * encrypted in one call. This allows a "streaming" usage.
176  * If on the other hand you need to retain the contents of the
177  * IV, you should either save it manually or use the cipher
178  * module instead.
179  *
180  * \param ctx AES context
181  * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
182  * \param length length of the input data
183  * \param iv initialization vector (updated after use)
184  * \param input buffer holding the input data
185  * \param output buffer holding the output data
186  *
187  * \return 0 if successful
188  */
189 int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx,
190  int mode,
191  size_t length,
192  unsigned char iv[16],
193  const unsigned char *input,
194  unsigned char *output );
195 #endif /*MBEDTLS_CIPHER_MODE_CFB */
196 
197 #if defined(MBEDTLS_CIPHER_MODE_CTR)
198 /**
199  * \brief AES-CTR buffer encryption/decryption
200  *
201  * Warning: You have to keep the maximum use of your counter in mind!
202  *
203  * Note: Due to the nature of CTR you should use the same key schedule for
204  * both encryption and decryption. So a context initialized with
205  * mbedtls_aes_setkey_enc() for both MBEDTLS_AES_ENCRYPT and MBEDTLS_AES_DECRYPT.
206  *
207  * \param ctx AES context
208  * \param length The length of the data
209  * \param nc_off The offset in the current stream_block (for resuming
210  * within current cipher stream). The offset pointer to
211  * should be 0 at the start of a stream.
212  * \param nonce_counter The 128-bit nonce and counter.
213  * \param stream_block The saved stream-block for resuming. Is overwritten
214  * by the function.
215  * \param input The input data stream
216  * \param output The output data stream
217  *
218  * \return 0 if successful
219  */
220 int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
221  size_t length,
222  size_t *nc_off,
223  unsigned char nonce_counter[16],
224  unsigned char stream_block[16],
225  const unsigned char *input,
226  unsigned char *output );
227 #endif /* MBEDTLS_CIPHER_MODE_CTR */
228 
229 /**
230  * \brief Internal AES block encryption function
231  * (Only exposed to allow overriding it,
232  * see MBEDTLS_AES_ENCRYPT_ALT)
233  *
234  * \param ctx AES context
235  * \param input Plaintext block
236  * \param output Output (ciphertext) block
237  */
238 void mbedtls_aes_encrypt( mbedtls_aes_context *ctx,
239  const unsigned char input[16],
240  unsigned char output[16] );
241 
242 /**
243  * \brief Internal AES block decryption function
244  * (Only exposed to allow overriding it,
245  * see MBEDTLS_AES_DECRYPT_ALT)
246  *
247  * \param ctx AES context
248  * \param input Ciphertext block
249  * \param output Output (plaintext) block
250  */
251 void mbedtls_aes_decrypt( mbedtls_aes_context *ctx,
252  const unsigned char input[16],
253  unsigned char output[16] );
254 
255 #ifdef __cplusplus
256 }
257 #endif
258 
259 
260 #endif /* MBEDTLS_AES_ALT */
261 
262 #endif /* 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.