| Manuel Pégourié-Gonnard | 2171876 | 2023-11-10 11:21:17 +0100 | [diff] [blame] | 1 | /** | 
|  | 2 | * \file block_cipher_internal.h | 
|  | 3 | * | 
|  | 4 | * \brief Lightweight abstraction layer for block ciphers with 128 bit blocks, | 
|  | 5 | * for use by the GCM and CCM modules. | 
|  | 6 | */ | 
|  | 7 | /* | 
|  | 8 | *  Copyright The Mbed TLS Contributors | 
|  | 9 | *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later | 
|  | 10 | */ | 
|  | 11 | #ifndef MBEDTLS_BLOCK_CIPHER_INTERNAL_H | 
|  | 12 | #define MBEDTLS_BLOCK_CIPHER_INTERNAL_H | 
|  | 13 |  | 
|  | 14 | #include "mbedtls/build_info.h" | 
|  | 15 |  | 
|  | 16 | #include "mbedtls/cipher.h" | 
|  | 17 |  | 
|  | 18 | #include "mbedtls/block_cipher.h" | 
|  | 19 |  | 
|  | 20 | #ifdef __cplusplus | 
|  | 21 | extern "C" { | 
|  | 22 | #endif | 
|  | 23 |  | 
|  | 24 | /** | 
|  | 25 | * \brief           Initialize the context. | 
|  | 26 | *                  This must be the first API call before using the context. | 
|  | 27 | * | 
|  | 28 | * \param ctx       The context to initialize. | 
|  | 29 | */ | 
|  | 30 | static inline void mbedtls_block_cipher_init(mbedtls_block_cipher_context_t *ctx) | 
|  | 31 | { | 
|  | 32 | memset(ctx, 0, sizeof(*ctx)); | 
|  | 33 | } | 
|  | 34 |  | 
|  | 35 | /** | 
|  | 36 | * \brief           Set the block cipher to use with this context. | 
|  | 37 | *                  This must be called after mbedtls_block_cipher_init(). | 
|  | 38 | * | 
|  | 39 | * \param ctx       The context to set up. | 
|  | 40 | * \param cipher_id The identifier of the cipher to use. | 
|  | 41 | *                  This must be either AES, ARIA or Camellia. | 
|  | 42 | *                  Warning: this is a ::mbedtls_cipher_id_t, | 
|  | 43 | *                  not a ::mbedtls_block_cipher_id_t! | 
|  | 44 | * | 
|  | 45 | * \retval          \c 0 on success. | 
|  | 46 | * \retval          #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA if \p cipher_id was | 
|  | 47 | *                  invalid. | 
|  | 48 | */ | 
|  | 49 | int mbedtls_block_cipher_setup(mbedtls_block_cipher_context_t *ctx, | 
|  | 50 | mbedtls_cipher_id_t cipher_id); | 
|  | 51 |  | 
|  | 52 | /** | 
| Manuel Pégourié-Gonnard | 3e0884f | 2023-11-10 11:52:10 +0100 | [diff] [blame] | 53 | * \brief           Set the key into the context. | 
|  | 54 | * | 
|  | 55 | * \param ctx       The context to configure. | 
|  | 56 | * \param key       The buffer holding the key material. | 
|  | 57 | * \param key_bitlen    The size of the key in bits. | 
|  | 58 | * | 
|  | 59 | * \retval          \c 0 on success. | 
|  | 60 | * \retval          #MBEDTLS_ERR_CIPHER_INVALID_CONTEXT if the context was not | 
|  | 61 | *                  properly set up before calling this function. | 
|  | 62 | * \retval          One of #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH, | 
|  | 63 | *                  #MBEDTLS_ERR_ARIA_BAD_INPUT_DATA, | 
|  | 64 | *                  #MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA if \p key_bitlen is | 
|  | 65 | *                  invalid. | 
|  | 66 | */ | 
|  | 67 | int mbedtls_block_cipher_setkey(mbedtls_block_cipher_context_t *ctx, | 
|  | 68 | const unsigned char *key, | 
|  | 69 | unsigned key_bitlen); | 
| Manuel Pégourié-Gonnard | 76fa16c | 2023-11-10 12:02:53 +0100 | [diff] [blame] | 70 |  | 
|  | 71 | /** | 
|  | 72 | * \brief           Encrypt one block (16 bytes) with the configured key. | 
|  | 73 | * | 
|  | 74 | * \param ctx       The context holding the key. | 
|  | 75 | * \param input     The buffer holding the input block. Must be 16 bytes. | 
|  | 76 | * \param output    The buffer to which the output block will be written. | 
|  | 77 | *                  Must be writable and 16 bytes long. | 
|  | 78 | *                  This must either not overlap with \p input, or be equal. | 
|  | 79 | * | 
|  | 80 | * \retval          \c 0 on success. | 
|  | 81 | * \retval          #MBEDTLS_ERR_CIPHER_INVALID_CONTEXT if the context was not | 
|  | 82 | *                  properly set up before calling this function. | 
|  | 83 | * \retval          Another negative value if encryption failed. | 
|  | 84 | */ | 
|  | 85 | int mbedtls_block_cipher_encrypt(mbedtls_block_cipher_context_t *ctx, | 
|  | 86 | const unsigned char input[16], | 
|  | 87 | unsigned char output[16]); | 
| Manuel Pégourié-Gonnard | 3e0884f | 2023-11-10 11:52:10 +0100 | [diff] [blame] | 88 | /** | 
| Manuel Pégourié-Gonnard | 2171876 | 2023-11-10 11:21:17 +0100 | [diff] [blame] | 89 | * \brief           Clear the context. | 
|  | 90 | * | 
|  | 91 | * \param ctx       The context to clear. | 
|  | 92 | */ | 
|  | 93 | void mbedtls_block_cipher_free(mbedtls_block_cipher_context_t *ctx); | 
|  | 94 |  | 
|  | 95 | #ifdef __cplusplus | 
|  | 96 | } | 
|  | 97 | #endif | 
|  | 98 |  | 
|  | 99 | #endif /* MBEDTLS_BLOCK_CIPHER_INTERNAL_H */ |