crypto_builtin_composites.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Context structure declaration of the Mbed TLS software-based PSA drivers
  3. * called through the PSA Crypto driver dispatch layer.
  4. * This file contains the context structures of those algorithms which need to
  5. * rely on other algorithms, i.e. are 'composite' algorithms.
  6. *
  7. * \note This file may not be included directly. Applications must
  8. * include psa/crypto.h.
  9. *
  10. * \note This header and its content are not part of the Mbed TLS API and
  11. * applications must not depend on it. Its main purpose is to define the
  12. * multi-part state objects of the Mbed TLS software-based PSA drivers. The
  13. * definitions of these objects are then used by crypto_struct.h to define the
  14. * implementation-defined types of PSA multi-part state objects.
  15. */
  16. /*
  17. * Copyright The Mbed TLS Contributors
  18. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  19. */
  20. #ifndef PSA_CRYPTO_BUILTIN_COMPOSITES_H
  21. #define PSA_CRYPTO_BUILTIN_COMPOSITES_H
  22. #include "mbedtls/private_access.h"
  23. #include <psa/crypto_driver_common.h>
  24. #include "mbedtls/cmac.h"
  25. #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
  26. #include "mbedtls/gcm.h"
  27. #endif
  28. #if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
  29. #include "mbedtls/ccm.h"
  30. #endif
  31. #include "mbedtls/chachapoly.h"
  32. /*
  33. * MAC multi-part operation definitions.
  34. */
  35. #if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC) || \
  36. defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
  37. #define MBEDTLS_PSA_BUILTIN_MAC
  38. #endif
  39. #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) || defined(PSA_CRYPTO_DRIVER_TEST)
  40. typedef struct {
  41. /** The HMAC algorithm in use */
  42. psa_algorithm_t MBEDTLS_PRIVATE(alg);
  43. /** The hash context. */
  44. struct psa_hash_operation_s hash_ctx;
  45. /** The HMAC part of the context. */
  46. uint8_t MBEDTLS_PRIVATE(opad)[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
  47. } mbedtls_psa_hmac_operation_t;
  48. #define MBEDTLS_PSA_HMAC_OPERATION_INIT { 0, PSA_HASH_OPERATION_INIT, { 0 } }
  49. #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
  50. typedef struct {
  51. psa_algorithm_t MBEDTLS_PRIVATE(alg);
  52. union {
  53. unsigned MBEDTLS_PRIVATE(dummy); /* Make the union non-empty even with no supported algorithms. */
  54. #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) || defined(PSA_CRYPTO_DRIVER_TEST)
  55. mbedtls_psa_hmac_operation_t MBEDTLS_PRIVATE(hmac);
  56. #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
  57. #if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC) || defined(PSA_CRYPTO_DRIVER_TEST)
  58. mbedtls_cipher_context_t MBEDTLS_PRIVATE(cmac);
  59. #endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */
  60. } MBEDTLS_PRIVATE(ctx);
  61. } mbedtls_psa_mac_operation_t;
  62. #define MBEDTLS_PSA_MAC_OPERATION_INIT { 0, { 0 } }
  63. #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM) || \
  64. defined(MBEDTLS_PSA_BUILTIN_ALG_CCM) || \
  65. defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
  66. #define MBEDTLS_PSA_BUILTIN_AEAD 1
  67. #endif
  68. /* Context structure for the Mbed TLS AEAD implementation. */
  69. typedef struct {
  70. psa_algorithm_t MBEDTLS_PRIVATE(alg);
  71. psa_key_type_t MBEDTLS_PRIVATE(key_type);
  72. unsigned int MBEDTLS_PRIVATE(is_encrypt) : 1;
  73. uint8_t MBEDTLS_PRIVATE(tag_length);
  74. union {
  75. unsigned dummy; /* Enable easier initializing of the union. */
  76. #if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
  77. mbedtls_ccm_context MBEDTLS_PRIVATE(ccm);
  78. #endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
  79. #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
  80. mbedtls_gcm_context MBEDTLS_PRIVATE(gcm);
  81. #endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
  82. #if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
  83. mbedtls_chachapoly_context MBEDTLS_PRIVATE(chachapoly);
  84. #endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
  85. } ctx;
  86. } mbedtls_psa_aead_operation_t;
  87. #define MBEDTLS_PSA_AEAD_OPERATION_INIT { 0, 0, 0, 0, { 0 } }
  88. #include "mbedtls/ecdsa.h"
  89. /* Context structure for the Mbed TLS interruptible sign hash implementation. */
  90. typedef struct {
  91. #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
  92. defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
  93. defined(MBEDTLS_ECP_RESTARTABLE)
  94. mbedtls_ecdsa_context *MBEDTLS_PRIVATE(ctx);
  95. mbedtls_ecdsa_restart_ctx MBEDTLS_PRIVATE(restart_ctx);
  96. uint32_t MBEDTLS_PRIVATE(num_ops);
  97. size_t MBEDTLS_PRIVATE(coordinate_bytes);
  98. psa_algorithm_t MBEDTLS_PRIVATE(alg);
  99. mbedtls_md_type_t MBEDTLS_PRIVATE(md_alg);
  100. uint8_t MBEDTLS_PRIVATE(hash)[PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS)];
  101. size_t MBEDTLS_PRIVATE(hash_length);
  102. #else
  103. /* Make the struct non-empty if algs not supported. */
  104. unsigned MBEDTLS_PRIVATE(dummy);
  105. #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
  106. * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
  107. * defined( MBEDTLS_ECP_RESTARTABLE ) */
  108. } mbedtls_psa_sign_hash_interruptible_operation_t;
  109. #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
  110. defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
  111. defined(MBEDTLS_ECP_RESTARTABLE)
  112. #define MBEDTLS_PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT { { 0 }, { 0 }, 0, 0, 0, 0, 0, 0 }
  113. #else
  114. #define MBEDTLS_PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT { 0 }
  115. #endif
  116. /* Context structure for the Mbed TLS interruptible verify hash
  117. * implementation.*/
  118. typedef struct {
  119. #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
  120. defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
  121. defined(MBEDTLS_ECP_RESTARTABLE)
  122. mbedtls_ecdsa_context *MBEDTLS_PRIVATE(ctx);
  123. mbedtls_ecdsa_restart_ctx MBEDTLS_PRIVATE(restart_ctx);
  124. uint32_t MBEDTLS_PRIVATE(num_ops);
  125. uint8_t MBEDTLS_PRIVATE(hash)[PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS)];
  126. size_t MBEDTLS_PRIVATE(hash_length);
  127. mbedtls_mpi MBEDTLS_PRIVATE(r);
  128. mbedtls_mpi MBEDTLS_PRIVATE(s);
  129. #else
  130. /* Make the struct non-empty if algs not supported. */
  131. unsigned MBEDTLS_PRIVATE(dummy);
  132. #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
  133. * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
  134. * defined( MBEDTLS_ECP_RESTARTABLE ) */
  135. } mbedtls_psa_verify_hash_interruptible_operation_t;
  136. #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
  137. defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
  138. defined(MBEDTLS_ECP_RESTARTABLE)
  139. #define MBEDTLS_VERIFY_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT { { 0 }, { 0 }, 0, 0, 0, 0, { 0 }, \
  140. { 0 } }
  141. #else
  142. #define MBEDTLS_VERIFY_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT { 0 }
  143. #endif
  144. /* EC-JPAKE operation definitions */
  145. #include "mbedtls/ecjpake.h"
  146. #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
  147. #define MBEDTLS_PSA_BUILTIN_PAKE 1
  148. #endif
  149. /* Note: the format for mbedtls_ecjpake_read/write function has an extra
  150. * length byte for each step, plus an extra 3 bytes for ECParameters in the
  151. * server's 2nd round. */
  152. #define MBEDTLS_PSA_JPAKE_BUFFER_SIZE ((3 + 1 + 65 + 1 + 65 + 1 + 32) * 2)
  153. typedef struct {
  154. psa_algorithm_t MBEDTLS_PRIVATE(alg);
  155. uint8_t *MBEDTLS_PRIVATE(password);
  156. size_t MBEDTLS_PRIVATE(password_len);
  157. #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
  158. mbedtls_ecjpake_role MBEDTLS_PRIVATE(role);
  159. uint8_t MBEDTLS_PRIVATE(buffer[MBEDTLS_PSA_JPAKE_BUFFER_SIZE]);
  160. size_t MBEDTLS_PRIVATE(buffer_length);
  161. size_t MBEDTLS_PRIVATE(buffer_offset);
  162. #endif
  163. /* Context structure for the Mbed TLS EC-JPAKE implementation. */
  164. union {
  165. unsigned int MBEDTLS_PRIVATE(dummy);
  166. #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
  167. mbedtls_ecjpake_context MBEDTLS_PRIVATE(jpake);
  168. #endif
  169. } MBEDTLS_PRIVATE(ctx);
  170. } mbedtls_psa_pake_operation_t;
  171. #define MBEDTLS_PSA_PAKE_OPERATION_INIT { { 0 } }
  172. #endif /* PSA_CRYPTO_BUILTIN_COMPOSITES_H */