block_cipher_internal.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. #include "mbedtls/build_info.h"
  14. #include "mbedtls/cipher.h"
  15. #include "mbedtls/block_cipher.h"
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. /**
  20. * \brief Initialize the context.
  21. * This must be the first API call before using the context.
  22. *
  23. * \param ctx The context to initialize.
  24. */
  25. static inline void mbedtls_block_cipher_init(mbedtls_block_cipher_context_t *ctx)
  26. {
  27. memset(ctx, 0, sizeof(*ctx));
  28. }
  29. /**
  30. * \brief Set the block cipher to use with this context.
  31. * This must be called after mbedtls_block_cipher_init().
  32. *
  33. * \param ctx The context to set up.
  34. * \param cipher_id The identifier of the cipher to use.
  35. * This must be either AES, ARIA or Camellia.
  36. * Warning: this is a ::mbedtls_cipher_id_t,
  37. * not a ::mbedtls_block_cipher_id_t!
  38. *
  39. * \retval \c 0 on success.
  40. * \retval #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA if \p cipher_id was
  41. * invalid.
  42. */
  43. int mbedtls_block_cipher_setup(mbedtls_block_cipher_context_t *ctx,
  44. mbedtls_cipher_id_t cipher_id);
  45. /**
  46. * \brief Set the key into the context.
  47. *
  48. * \param ctx The context to configure.
  49. * \param key The buffer holding the key material.
  50. * \param key_bitlen The size of the key in bits.
  51. *
  52. * \retval \c 0 on success.
  53. * \retval #MBEDTLS_ERR_CIPHER_INVALID_CONTEXT if the context was not
  54. * properly set up before calling this function.
  55. * \retval One of #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH,
  56. * #MBEDTLS_ERR_ARIA_BAD_INPUT_DATA,
  57. * #MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA if \p key_bitlen is
  58. * invalid.
  59. */
  60. int mbedtls_block_cipher_setkey(mbedtls_block_cipher_context_t *ctx,
  61. const unsigned char *key,
  62. unsigned key_bitlen);
  63. /**
  64. * \brief Encrypt one block (16 bytes) with the configured key.
  65. *
  66. * \param ctx The context holding the key.
  67. * \param input The buffer holding the input block. Must be 16 bytes.
  68. * \param output The buffer to which the output block will be written.
  69. * Must be writable and 16 bytes long.
  70. * This must either not overlap with \p input, or be equal.
  71. *
  72. * \retval \c 0 on success.
  73. * \retval #MBEDTLS_ERR_CIPHER_INVALID_CONTEXT if the context was not
  74. * properly set up before calling this function.
  75. * \retval Another negative value if encryption failed.
  76. */
  77. int mbedtls_block_cipher_encrypt(mbedtls_block_cipher_context_t *ctx,
  78. const unsigned char input[16],
  79. unsigned char output[16]);
  80. /**
  81. * \brief Clear the context.
  82. *
  83. * \param ctx The context to clear.
  84. */
  85. void mbedtls_block_cipher_free(mbedtls_block_cipher_context_t *ctx);
  86. #ifdef __cplusplus
  87. }
  88. #endif
  89. #endif /* MBEDTLS_BLOCK_CIPHER_INTERNAL_H */