crypto_builtin_key_derivation.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 key derivation algorithms
  5. * which need to rely on other 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_KEY_DERIVATION_H
  21. #define PSA_CRYPTO_BUILTIN_KEY_DERIVATION_H
  22. #include "mbedtls/private_access.h"
  23. #include <psa/crypto_driver_common.h>
  24. #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
  25. defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
  26. defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
  27. typedef struct {
  28. uint8_t *MBEDTLS_PRIVATE(info);
  29. size_t MBEDTLS_PRIVATE(info_length);
  30. #if PSA_HASH_MAX_SIZE > 0xff
  31. #error "PSA_HASH_MAX_SIZE does not fit in uint8_t"
  32. #endif
  33. uint8_t MBEDTLS_PRIVATE(offset_in_block);
  34. uint8_t MBEDTLS_PRIVATE(block_number);
  35. unsigned int MBEDTLS_PRIVATE(state) : 2;
  36. unsigned int MBEDTLS_PRIVATE(info_set) : 1;
  37. uint8_t MBEDTLS_PRIVATE(output_block)[PSA_HASH_MAX_SIZE];
  38. uint8_t MBEDTLS_PRIVATE(prk)[PSA_HASH_MAX_SIZE];
  39. struct psa_mac_operation_s MBEDTLS_PRIVATE(hmac);
  40. } psa_hkdf_key_derivation_t;
  41. #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF ||
  42. MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
  43. MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
  44. #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
  45. typedef struct {
  46. uint8_t MBEDTLS_PRIVATE(data)[PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE];
  47. } psa_tls12_ecjpake_to_pms_t;
  48. #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
  49. #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
  50. defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
  51. typedef enum {
  52. PSA_TLS12_PRF_STATE_INIT, /* no input provided */
  53. PSA_TLS12_PRF_STATE_SEED_SET, /* seed has been set */
  54. PSA_TLS12_PRF_STATE_OTHER_KEY_SET, /* other key has been set - optional */
  55. PSA_TLS12_PRF_STATE_KEY_SET, /* key has been set */
  56. PSA_TLS12_PRF_STATE_LABEL_SET, /* label has been set */
  57. PSA_TLS12_PRF_STATE_OUTPUT /* output has been started */
  58. } psa_tls12_prf_key_derivation_state_t;
  59. typedef struct psa_tls12_prf_key_derivation_s {
  60. #if PSA_HASH_MAX_SIZE > 0xff
  61. #error "PSA_HASH_MAX_SIZE does not fit in uint8_t"
  62. #endif
  63. /* Indicates how many bytes in the current HMAC block have
  64. * not yet been read by the user. */
  65. uint8_t MBEDTLS_PRIVATE(left_in_block);
  66. /* The 1-based number of the block. */
  67. uint8_t MBEDTLS_PRIVATE(block_number);
  68. psa_tls12_prf_key_derivation_state_t MBEDTLS_PRIVATE(state);
  69. uint8_t *MBEDTLS_PRIVATE(secret);
  70. size_t MBEDTLS_PRIVATE(secret_length);
  71. uint8_t *MBEDTLS_PRIVATE(seed);
  72. size_t MBEDTLS_PRIVATE(seed_length);
  73. uint8_t *MBEDTLS_PRIVATE(label);
  74. size_t MBEDTLS_PRIVATE(label_length);
  75. #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
  76. uint8_t *MBEDTLS_PRIVATE(other_secret);
  77. size_t MBEDTLS_PRIVATE(other_secret_length);
  78. #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
  79. uint8_t MBEDTLS_PRIVATE(Ai)[PSA_HASH_MAX_SIZE];
  80. /* `HMAC_hash( prk, A( i ) + seed )` in the notation of RFC 5246, Sect. 5. */
  81. uint8_t MBEDTLS_PRIVATE(output_block)[PSA_HASH_MAX_SIZE];
  82. } psa_tls12_prf_key_derivation_t;
  83. #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
  84. * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
  85. #if defined(PSA_HAVE_SOFT_PBKDF2)
  86. typedef enum {
  87. PSA_PBKDF2_STATE_INIT, /* no input provided */
  88. PSA_PBKDF2_STATE_INPUT_COST_SET, /* input cost has been set */
  89. PSA_PBKDF2_STATE_SALT_SET, /* salt has been set */
  90. PSA_PBKDF2_STATE_PASSWORD_SET, /* password has been set */
  91. PSA_PBKDF2_STATE_OUTPUT /* output has been started */
  92. } psa_pbkdf2_key_derivation_state_t;
  93. typedef struct {
  94. psa_pbkdf2_key_derivation_state_t MBEDTLS_PRIVATE(state);
  95. uint64_t MBEDTLS_PRIVATE(input_cost);
  96. uint8_t *MBEDTLS_PRIVATE(salt);
  97. size_t MBEDTLS_PRIVATE(salt_length);
  98. uint8_t MBEDTLS_PRIVATE(password)[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
  99. size_t MBEDTLS_PRIVATE(password_length);
  100. uint8_t MBEDTLS_PRIVATE(output_block)[PSA_HASH_MAX_SIZE];
  101. uint8_t MBEDTLS_PRIVATE(bytes_used);
  102. uint32_t MBEDTLS_PRIVATE(block_number);
  103. } psa_pbkdf2_key_derivation_t;
  104. #endif /* PSA_HAVE_SOFT_PBKDF2 */
  105. #endif /* PSA_CRYPTO_BUILTIN_KEY_DERIVATION_H */