mbed TLS v2.24.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 
43 #if !defined(MBEDTLS_CONFIG_FILE)
44 #include "mbedtls/config.h"
45 #else
46 #include MBEDTLS_CONFIG_FILE
47 #endif
48 
49 #include <stddef.h>
50 #include <stdint.h>
51 
52 /* padlock.c and aesni.c rely on these values! */
53 #define MBEDTLS_AES_ENCRYPT 1
54 #define MBEDTLS_AES_DECRYPT 0
56 /* Error codes in range 0x0020-0x0022 */
57 #define MBEDTLS_ERR_AES_INVALID_KEY_LENGTH -0x0020
58 #define MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH -0x0022
60 /* Error codes in range 0x0021-0x0025 */
61 #define MBEDTLS_ERR_AES_BAD_INPUT_DATA -0x0021
63 /* MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE is deprecated and should not be used. */
64 #define MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE -0x0023
66 /* MBEDTLS_ERR_AES_HW_ACCEL_FAILED is deprecated and should not be used. */
67 #define MBEDTLS_ERR_AES_HW_ACCEL_FAILED -0x0025
69 #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
70  !defined(inline) && !defined(__cplusplus)
71 #define inline __inline
72 #endif
73 
74 #ifdef __cplusplus
75 extern "C" {
76 #endif
77 
78 #if !defined(MBEDTLS_AES_ALT)
79 // Regular implementation
80 //
81 
85 typedef struct mbedtls_aes_context
86 {
87  int nr;
88  uint32_t *rk;
89  uint32_t buf[68];
97 }
98 mbedtls_aes_context;
99 
100 #if defined(MBEDTLS_CIPHER_MODE_XTS)
101 
104 typedef struct mbedtls_aes_xts_context
105 {
106  mbedtls_aes_context crypt;
108  mbedtls_aes_context tweak;
110 } mbedtls_aes_xts_context;
111 #endif /* MBEDTLS_CIPHER_MODE_XTS */
112 
113 #else /* MBEDTLS_AES_ALT */
114 #include "aes_alt.h"
115 #endif /* MBEDTLS_AES_ALT */
116 
125 void mbedtls_aes_init( mbedtls_aes_context *ctx );
126 
134 void mbedtls_aes_free( mbedtls_aes_context *ctx );
135 
136 #if defined(MBEDTLS_CIPHER_MODE_XTS)
137 
145 void mbedtls_aes_xts_init( mbedtls_aes_xts_context *ctx );
146 
154 void mbedtls_aes_xts_free( mbedtls_aes_xts_context *ctx );
155 #endif /* MBEDTLS_CIPHER_MODE_XTS */
156 
172 int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key,
173  unsigned int keybits );
174 
190 int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx, const unsigned char *key,
191  unsigned int keybits );
192 
193 #if defined(MBEDTLS_CIPHER_MODE_XTS)
194 
210 int mbedtls_aes_xts_setkey_enc( mbedtls_aes_xts_context *ctx,
211  const unsigned char *key,
212  unsigned int keybits );
213 
230 int mbedtls_aes_xts_setkey_dec( mbedtls_aes_xts_context *ctx,
231  const unsigned char *key,
232  unsigned int keybits );
233 #endif /* MBEDTLS_CIPHER_MODE_XTS */
234 
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 
305 int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
306  int mode,
307  size_t length,
308  unsigned char iv[16],
309  const unsigned char *input,
310  unsigned char *output );
311 #endif /* MBEDTLS_CIPHER_MODE_CBC */
312 
313 #if defined(MBEDTLS_CIPHER_MODE_XTS)
314 
349 int mbedtls_aes_crypt_xts( mbedtls_aes_xts_context *ctx,
350  int mode,
351  size_t length,
352  const unsigned char data_unit[16],
353  const unsigned char *input,
354  unsigned char *output );
355 #endif /* MBEDTLS_CIPHER_MODE_XTS */
356 
357 #if defined(MBEDTLS_CIPHER_MODE_CFB)
358 
397 int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
398  int mode,
399  size_t length,
400  size_t *iv_off,
401  unsigned char iv[16],
402  const unsigned char *input,
403  unsigned char *output );
404 
441 int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx,
442  int mode,
443  size_t length,
444  unsigned char iv[16],
445  const unsigned char *input,
446  unsigned char *output );
447 #endif /*MBEDTLS_CIPHER_MODE_CFB */
448 
449 #if defined(MBEDTLS_CIPHER_MODE_OFB)
450 
495 int mbedtls_aes_crypt_ofb( mbedtls_aes_context *ctx,
496  size_t length,
497  size_t *iv_off,
498  unsigned char iv[16],
499  const unsigned char *input,
500  unsigned char *output );
501 
502 #endif /* MBEDTLS_CIPHER_MODE_OFB */
503 
504 #if defined(MBEDTLS_CIPHER_MODE_CTR)
505 
581 int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
582  size_t length,
583  size_t *nc_off,
584  unsigned char nonce_counter[16],
585  unsigned char stream_block[16],
586  const unsigned char *input,
587  unsigned char *output );
588 #endif /* MBEDTLS_CIPHER_MODE_CTR */
589 
601 int mbedtls_internal_aes_encrypt( mbedtls_aes_context *ctx,
602  const unsigned char input[16],
603  unsigned char output[16] );
604 
616 int mbedtls_internal_aes_decrypt( mbedtls_aes_context *ctx,
617  const unsigned char input[16],
618  unsigned char output[16] );
619 
620 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
621 #if defined(MBEDTLS_DEPRECATED_WARNING)
622 #define MBEDTLS_DEPRECATED __attribute__((deprecated))
623 #else
624 #define MBEDTLS_DEPRECATED
625 #endif
626 
636 MBEDTLS_DEPRECATED void mbedtls_aes_encrypt( mbedtls_aes_context *ctx,
637  const unsigned char input[16],
638  unsigned char output[16] );
639 
650 MBEDTLS_DEPRECATED void mbedtls_aes_decrypt( mbedtls_aes_context *ctx,
651  const unsigned char input[16],
652  unsigned char output[16] );
653 
654 #undef MBEDTLS_DEPRECATED
655 #endif /* !MBEDTLS_DEPRECATED_REMOVED */
656 
657 
658 #if defined(MBEDTLS_SELF_TEST)
659 
665 int mbedtls_aes_self_test( int verbose );
666 
667 #endif /* MBEDTLS_SELF_TEST */
668 
669 #ifdef __cplusplus
670 }
671 #endif
672 
673 #endif /* aes.h */
int mbedtls_aes_self_test(int verbose)
Checkup routine.
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...
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. ...
Configuration options (set of defines)
void mbedtls_aes_init(mbedtls_aes_context *ctx)
This function initializes the specified AES context.
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...
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_DEPRECATED
Definition: pkcs11.h:60
void mbedtls_aes_xts_init(mbedtls_aes_xts_context *ctx)
This function initializes the specified AES XTS context.
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.
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_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.
int mbedtls_aes_setkey_dec(mbedtls_aes_context *ctx, const unsigned char *key, unsigned int keybits)
This function sets the decryption key.
void mbedtls_aes_xts_free(mbedtls_aes_xts_context *ctx)
This function releases and clears the specified AES XTS context.
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.
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. ...
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.
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. This is only exposed to allow overriding it using see MBEDTLS...