mbed TLS v3.1.0
aes.h
Go to the documentation of this file.
1 
23 /*
24  * Copyright The Mbed TLS Contributors
25  * SPDX-License-Identifier: Apache-2.0
26  *
27  * Licensed under the Apache License, Version 2.0 (the "License"); you may
28  * not use this file except in compliance with the License.
29  * You may obtain a copy of the License at
30  *
31  * http://www.apache.org/licenses/LICENSE-2.0
32  *
33  * Unless required by applicable law or agreed to in writing, software
34  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
35  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
36  * See the License for the specific language governing permissions and
37  * limitations under the License.
38  */
39 
40 #ifndef MBEDTLS_AES_H
41 #define MBEDTLS_AES_H
42 #include "mbedtls/private_access.h"
43 
44 #include "mbedtls/build_info.h"
45 #include "mbedtls/platform_util.h"
46 
47 #include <stddef.h>
48 #include <stdint.h>
49 
50 /* padlock.c and aesni.c rely on these values! */
51 #define MBEDTLS_AES_ENCRYPT 1
52 #define MBEDTLS_AES_DECRYPT 0
54 /* Error codes in range 0x0020-0x0022 */
55 
56 #define MBEDTLS_ERR_AES_INVALID_KEY_LENGTH -0x0020
57 
58 #define MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH -0x0022
59 
60 /* Error codes in range 0x0021-0x0025 */
62 #define MBEDTLS_ERR_AES_BAD_INPUT_DATA -0x0021
63 
64 #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
65  !defined(inline) && !defined(__cplusplus)
66 #define inline __inline
67 #endif
68 
69 #ifdef __cplusplus
70 extern "C" {
71 #endif
72 
73 #if !defined(MBEDTLS_AES_ALT)
74 // Regular implementation
75 //
76 
80 typedef struct mbedtls_aes_context
81 {
82  int MBEDTLS_PRIVATE(nr);
83  uint32_t *MBEDTLS_PRIVATE(rk);
84  uint32_t MBEDTLS_PRIVATE(buf)[68];
92 }
93 mbedtls_aes_context;
94 
95 #if defined(MBEDTLS_CIPHER_MODE_XTS)
96 
99 typedef struct mbedtls_aes_xts_context
100 {
101  mbedtls_aes_context MBEDTLS_PRIVATE(crypt);
103  mbedtls_aes_context MBEDTLS_PRIVATE(tweak);
105 } mbedtls_aes_xts_context;
106 #endif /* MBEDTLS_CIPHER_MODE_XTS */
107 
108 #else /* MBEDTLS_AES_ALT */
109 #include "aes_alt.h"
110 #endif /* MBEDTLS_AES_ALT */
111 
120 void mbedtls_aes_init( mbedtls_aes_context *ctx );
121 
129 void mbedtls_aes_free( mbedtls_aes_context *ctx );
130 
131 #if defined(MBEDTLS_CIPHER_MODE_XTS)
132 
140 void mbedtls_aes_xts_init( mbedtls_aes_xts_context *ctx );
141 
149 void mbedtls_aes_xts_free( mbedtls_aes_xts_context *ctx );
150 #endif /* MBEDTLS_CIPHER_MODE_XTS */
151 
168 int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key,
169  unsigned int keybits );
170 
187 int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx, const unsigned char *key,
188  unsigned int keybits );
189 
190 #if defined(MBEDTLS_CIPHER_MODE_XTS)
191 
208 int mbedtls_aes_xts_setkey_enc( mbedtls_aes_xts_context *ctx,
209  const unsigned char *key,
210  unsigned int keybits );
211 
229 int mbedtls_aes_xts_setkey_dec( mbedtls_aes_xts_context *ctx,
230  const unsigned char *key,
231  unsigned int keybits );
232 #endif /* MBEDTLS_CIPHER_MODE_XTS */
233 
258 int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx,
259  int mode,
260  const unsigned char input[16],
261  unsigned char output[16] );
262 
263 #if defined(MBEDTLS_CIPHER_MODE_CBC)
264 
306 int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
307  int mode,
308  size_t length,
309  unsigned char iv[16],
310  const unsigned char *input,
311  unsigned char *output );
312 #endif /* MBEDTLS_CIPHER_MODE_CBC */
313 
314 #if defined(MBEDTLS_CIPHER_MODE_XTS)
315 
351 int mbedtls_aes_crypt_xts( mbedtls_aes_xts_context *ctx,
352  int mode,
353  size_t length,
354  const unsigned char data_unit[16],
355  const unsigned char *input,
356  unsigned char *output );
357 #endif /* MBEDTLS_CIPHER_MODE_XTS */
358 
359 #if defined(MBEDTLS_CIPHER_MODE_CFB)
360 
400 int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
401  int mode,
402  size_t length,
403  size_t *iv_off,
404  unsigned char iv[16],
405  const unsigned char *input,
406  unsigned char *output );
407 
445 int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx,
446  int mode,
447  size_t length,
448  unsigned char iv[16],
449  const unsigned char *input,
450  unsigned char *output );
451 #endif /*MBEDTLS_CIPHER_MODE_CFB */
452 
453 #if defined(MBEDTLS_CIPHER_MODE_OFB)
454 
500 int mbedtls_aes_crypt_ofb( mbedtls_aes_context *ctx,
501  size_t length,
502  size_t *iv_off,
503  unsigned char iv[16],
504  const unsigned char *input,
505  unsigned char *output );
506 
507 #endif /* MBEDTLS_CIPHER_MODE_OFB */
508 
509 #if defined(MBEDTLS_CIPHER_MODE_CTR)
510 
583 int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
584  size_t length,
585  size_t *nc_off,
586  unsigned char nonce_counter[16],
587  unsigned char stream_block[16],
588  const unsigned char *input,
589  unsigned char *output );
590 #endif /* MBEDTLS_CIPHER_MODE_CTR */
591 
604 int mbedtls_internal_aes_encrypt( mbedtls_aes_context *ctx,
605  const unsigned char input[16],
606  unsigned char output[16] );
607 
620 int mbedtls_internal_aes_decrypt( mbedtls_aes_context *ctx,
621  const unsigned char input[16],
622  unsigned char output[16] );
623 
624 #if defined(MBEDTLS_SELF_TEST)
625 
632 int mbedtls_aes_self_test( int verbose );
633 
634 #endif /* MBEDTLS_SELF_TEST */
635 
636 #ifdef __cplusplus
637 }
638 #endif
639 
640 #endif /* aes.h */
MBEDTLS_CHECK_RETURN_TYPICAL int mbedtls_aes_crypt_cfb128(mbedtls_aes_context *ctx, int mode, size_t length, size_t *iv_off, unsigned char iv[16], const unsigned char *input, unsigned char *output)
This function performs an AES-CFB128 encryption or decryption operation.
MBEDTLS_CHECK_RETURN_TYPICAL int mbedtls_aes_crypt_xts(mbedtls_aes_xts_context *ctx, int mode, size_t length, const unsigned char data_unit[16], const unsigned char *input, unsigned char *output)
This function performs an AES-XTS encryption or decryption operation for an entire XTS data unit...
#define MBEDTLS_CHECK_RETURN_TYPICAL
MBEDTLS_CHECK_RETURN_TYPICAL int mbedtls_aes_crypt_cbc(mbedtls_aes_context *ctx, int mode, size_t length, unsigned char iv[16], const unsigned char *input, unsigned char *output)
This function performs an AES-CBC encryption or decryption operation on full blocks.
#define MBEDTLS_PRIVATE(member)
void mbedtls_aes_init(mbedtls_aes_context *ctx)
This function initializes the specified AES context.
Common and shared functions used by multiple modules in the Mbed TLS library.
MBEDTLS_CHECK_RETURN_TYPICAL int mbedtls_internal_aes_encrypt(mbedtls_aes_context *ctx, const unsigned char input[16], unsigned char output[16])
Internal AES block encryption function. This is only exposed to allow overriding it using MBEDTLS_AES...
void mbedtls_aes_xts_init(mbedtls_aes_xts_context *ctx)
This function initializes the specified AES XTS context.
MBEDTLS_CHECK_RETURN_TYPICAL int mbedtls_aes_setkey_dec(mbedtls_aes_context *ctx, const unsigned char *key, unsigned int keybits)
This function sets the decryption key.
MBEDTLS_CHECK_RETURN_TYPICAL int mbedtls_aes_xts_setkey_dec(mbedtls_aes_xts_context *ctx, const unsigned char *key, unsigned int keybits)
This function prepares an XTS context for decryption and sets the decryption key. ...
MBEDTLS_CHECK_RETURN_CRITICAL int mbedtls_aes_self_test(int verbose)
Checkup routine.
Macro wrapper for struct's memebrs.
MBEDTLS_CHECK_RETURN_TYPICAL 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_xts_free(mbedtls_aes_xts_context *ctx)
This function releases and clears the specified AES XTS context.
Build-time configuration info.
MBEDTLS_CHECK_RETURN_TYPICAL int mbedtls_aes_crypt_cfb8(mbedtls_aes_context *ctx, int mode, size_t length, unsigned char iv[16], const unsigned char *input, unsigned char *output)
This function performs an AES-CFB8 encryption or decryption operation.
MBEDTLS_CHECK_RETURN_TYPICAL int mbedtls_aes_setkey_enc(mbedtls_aes_context *ctx, const unsigned char *key, unsigned int keybits)
This function sets the encryption key.
#define MBEDTLS_CHECK_RETURN_CRITICAL
Definition: platform_util.h:93
MBEDTLS_CHECK_RETURN_TYPICAL 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.
MBEDTLS_CHECK_RETURN_TYPICAL int mbedtls_internal_aes_decrypt(mbedtls_aes_context *ctx, const unsigned char input[16], unsigned char output[16])
Internal AES block decryption function. This is only exposed to allow overriding it using see MBEDTLS...
MBEDTLS_CHECK_RETURN_TYPICAL int mbedtls_aes_xts_setkey_enc(mbedtls_aes_xts_context *ctx, const unsigned char *key, unsigned int keybits)
This function prepares an XTS context for encryption and sets the encryption key. ...
void mbedtls_aes_free(mbedtls_aes_context *ctx)
This function releases and clears the specified AES context.
MBEDTLS_CHECK_RETURN_TYPICAL int mbedtls_aes_crypt_ofb(mbedtls_aes_context *ctx, size_t length, size_t *iv_off, unsigned char iv[16], const unsigned char *input, unsigned char *output)
This function performs an AES-OFB (Output Feedback Mode) encryption or decryption operation...