cipher_wrap.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /**
  2. * \file cipher_wrap.h
  3. *
  4. * \brief Cipher wrappers.
  5. *
  6. * \author Adriaan de Jong <[email protected]>
  7. */
  8. /*
  9. * Copyright The Mbed TLS Contributors
  10. * SPDX-License-Identifier: Apache-2.0
  11. *
  12. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  13. * not use this file except in compliance with the License.
  14. * You may obtain a copy of the License at
  15. *
  16. * http://www.apache.org/licenses/LICENSE-2.0
  17. *
  18. * Unless required by applicable law or agreed to in writing, software
  19. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  20. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21. * See the License for the specific language governing permissions and
  22. * limitations under the License.
  23. */
  24. #ifndef MBEDTLS_CIPHER_WRAP_H
  25. #define MBEDTLS_CIPHER_WRAP_H
  26. #include "mbedtls/build_info.h"
  27. #include "mbedtls/cipher.h"
  28. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  29. #include "psa/crypto.h"
  30. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  31. #ifdef __cplusplus
  32. extern "C" {
  33. #endif
  34. /**
  35. * Base cipher information. The non-mode specific functions and values.
  36. */
  37. struct mbedtls_cipher_base_t
  38. {
  39. /** Base Cipher type (e.g. MBEDTLS_CIPHER_ID_AES) */
  40. mbedtls_cipher_id_t cipher;
  41. /** Encrypt using ECB */
  42. int (*ecb_func)( void *ctx, mbedtls_operation_t mode,
  43. const unsigned char *input, unsigned char *output );
  44. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  45. /** Encrypt using CBC */
  46. int (*cbc_func)( void *ctx, mbedtls_operation_t mode, size_t length,
  47. unsigned char *iv, const unsigned char *input,
  48. unsigned char *output );
  49. #endif
  50. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  51. /** Encrypt using CFB (Full length) */
  52. int (*cfb_func)( void *ctx, mbedtls_operation_t mode, size_t length, size_t *iv_off,
  53. unsigned char *iv, const unsigned char *input,
  54. unsigned char *output );
  55. #endif
  56. #if defined(MBEDTLS_CIPHER_MODE_OFB)
  57. /** Encrypt using OFB (Full length) */
  58. int (*ofb_func)( void *ctx, size_t length, size_t *iv_off,
  59. unsigned char *iv,
  60. const unsigned char *input,
  61. unsigned char *output );
  62. #endif
  63. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  64. /** Encrypt using CTR */
  65. int (*ctr_func)( void *ctx, size_t length, size_t *nc_off,
  66. unsigned char *nonce_counter, unsigned char *stream_block,
  67. const unsigned char *input, unsigned char *output );
  68. #endif
  69. #if defined(MBEDTLS_CIPHER_MODE_XTS)
  70. /** Encrypt or decrypt using XTS. */
  71. int (*xts_func)( void *ctx, mbedtls_operation_t mode, size_t length,
  72. const unsigned char data_unit[16],
  73. const unsigned char *input, unsigned char *output );
  74. #endif
  75. #if defined(MBEDTLS_CIPHER_MODE_STREAM)
  76. /** Encrypt using STREAM */
  77. int (*stream_func)( void *ctx, size_t length,
  78. const unsigned char *input, unsigned char *output );
  79. #endif
  80. /** Set key for encryption purposes */
  81. int (*setkey_enc_func)( void *ctx, const unsigned char *key,
  82. unsigned int key_bitlen );
  83. /** Set key for decryption purposes */
  84. int (*setkey_dec_func)( void *ctx, const unsigned char *key,
  85. unsigned int key_bitlen);
  86. /** Allocate a new context */
  87. void * (*ctx_alloc_func)( void );
  88. /** Free the given context */
  89. void (*ctx_free_func)( void *ctx );
  90. };
  91. typedef struct
  92. {
  93. mbedtls_cipher_type_t type;
  94. const mbedtls_cipher_info_t *info;
  95. } mbedtls_cipher_definition_t;
  96. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  97. typedef enum
  98. {
  99. MBEDTLS_CIPHER_PSA_KEY_UNSET = 0,
  100. MBEDTLS_CIPHER_PSA_KEY_OWNED, /* Used for PSA-based cipher contexts which */
  101. /* use raw key material internally imported */
  102. /* as a volatile key, and which hence need */
  103. /* to destroy that key when the context is */
  104. /* freed. */
  105. MBEDTLS_CIPHER_PSA_KEY_NOT_OWNED, /* Used for PSA-based cipher contexts */
  106. /* which use a key provided by the */
  107. /* user, and which hence will not be */
  108. /* destroyed when the context is freed. */
  109. } mbedtls_cipher_psa_key_ownership;
  110. typedef struct
  111. {
  112. psa_algorithm_t alg;
  113. psa_key_id_t slot;
  114. mbedtls_cipher_psa_key_ownership slot_state;
  115. } mbedtls_cipher_context_psa;
  116. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  117. extern const mbedtls_cipher_definition_t mbedtls_cipher_definitions[];
  118. extern int mbedtls_cipher_supported[];
  119. #ifdef __cplusplus
  120. }
  121. #endif
  122. #endif /* MBEDTLS_CIPHER_WRAP_H */