cipher_wrap.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**
  2. * \file cipher_wrap.h
  3. *
  4. * \brief Cipher wrappers.
  5. *
  6. * \author Adriaan de Jong <dejong@fox-it.com>
  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. /** Base Cipher type (e.g. MBEDTLS_CIPHER_ID_AES) */
  39. mbedtls_cipher_id_t cipher;
  40. /** Encrypt using ECB */
  41. int (*ecb_func)(void *ctx, mbedtls_operation_t mode,
  42. const unsigned char *input, unsigned char *output);
  43. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  44. /** Encrypt using CBC */
  45. int (*cbc_func)(void *ctx, mbedtls_operation_t mode, size_t length,
  46. unsigned char *iv, const unsigned char *input,
  47. unsigned char *output);
  48. #endif
  49. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  50. /** Encrypt using CFB (Full length) */
  51. int (*cfb_func)(void *ctx, mbedtls_operation_t mode, size_t length, size_t *iv_off,
  52. unsigned char *iv, const unsigned char *input,
  53. unsigned char *output);
  54. #endif
  55. #if defined(MBEDTLS_CIPHER_MODE_OFB)
  56. /** Encrypt using OFB (Full length) */
  57. int (*ofb_func)(void *ctx, size_t length, size_t *iv_off,
  58. unsigned char *iv,
  59. const unsigned char *input,
  60. unsigned char *output);
  61. #endif
  62. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  63. /** Encrypt using CTR */
  64. int (*ctr_func)(void *ctx, size_t length, size_t *nc_off,
  65. unsigned char *nonce_counter, unsigned char *stream_block,
  66. const unsigned char *input, unsigned char *output);
  67. #endif
  68. #if defined(MBEDTLS_CIPHER_MODE_XTS)
  69. /** Encrypt or decrypt using XTS. */
  70. int (*xts_func)(void *ctx, mbedtls_operation_t mode, size_t length,
  71. const unsigned char data_unit[16],
  72. const unsigned char *input, unsigned char *output);
  73. #endif
  74. #if defined(MBEDTLS_CIPHER_MODE_STREAM)
  75. /** Encrypt using STREAM */
  76. int (*stream_func)(void *ctx, size_t length,
  77. const unsigned char *input, unsigned char *output);
  78. #endif
  79. /** Set key for encryption purposes */
  80. int (*setkey_enc_func)(void *ctx, const unsigned char *key,
  81. unsigned int key_bitlen);
  82. /** Set key for decryption purposes */
  83. int (*setkey_dec_func)(void *ctx, const unsigned char *key,
  84. unsigned int key_bitlen);
  85. /** Allocate a new context */
  86. void * (*ctx_alloc_func)(void);
  87. /** Free the given context */
  88. void (*ctx_free_func)(void *ctx);
  89. };
  90. typedef struct {
  91. mbedtls_cipher_type_t type;
  92. const mbedtls_cipher_info_t *info;
  93. } mbedtls_cipher_definition_t;
  94. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  95. typedef enum {
  96. MBEDTLS_CIPHER_PSA_KEY_UNSET = 0,
  97. MBEDTLS_CIPHER_PSA_KEY_OWNED, /* Used for PSA-based cipher contexts which */
  98. /* use raw key material internally imported */
  99. /* as a volatile key, and which hence need */
  100. /* to destroy that key when the context is */
  101. /* freed. */
  102. MBEDTLS_CIPHER_PSA_KEY_NOT_OWNED, /* Used for PSA-based cipher contexts */
  103. /* which use a key provided by the */
  104. /* user, and which hence will not be */
  105. /* destroyed when the context is freed. */
  106. } mbedtls_cipher_psa_key_ownership;
  107. typedef struct {
  108. psa_algorithm_t alg;
  109. mbedtls_svc_key_id_t slot;
  110. mbedtls_cipher_psa_key_ownership slot_state;
  111. } mbedtls_cipher_context_psa;
  112. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  113. extern const mbedtls_cipher_definition_t mbedtls_cipher_definitions[];
  114. extern int mbedtls_cipher_supported[];
  115. #ifdef __cplusplus
  116. }
  117. #endif
  118. #endif /* MBEDTLS_CIPHER_WRAP_H */