pk_internal.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /**
  2. * \file pk_internal.h
  3. *
  4. * \brief Public Key abstraction layer: internal (i.e. library only) functions
  5. * and definitions.
  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_PK_INTERNAL_H
  12. #define MBEDTLS_PK_INTERNAL_H
  13. #include "mbedtls/pk.h"
  14. #if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
  15. #include "mbedtls/ecp.h"
  16. #endif
  17. #if defined(MBEDTLS_PSA_CRYPTO_CLIENT)
  18. #include "psa/crypto.h"
  19. #include "psa_util_internal.h"
  20. #define PSA_PK_TO_MBEDTLS_ERR(status) psa_pk_status_to_mbedtls(status)
  21. #define PSA_PK_RSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
  22. psa_to_pk_rsa_errors, \
  23. psa_pk_status_to_mbedtls)
  24. #define PSA_PK_ECDSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
  25. psa_to_pk_ecdsa_errors, \
  26. psa_pk_status_to_mbedtls)
  27. #endif /* MBEDTLS_PSA_CRYPTO_CLIENT */
  28. /* Headers/footers for PEM files */
  29. #define PEM_BEGIN_PUBLIC_KEY "-----BEGIN PUBLIC KEY-----"
  30. #define PEM_END_PUBLIC_KEY "-----END PUBLIC KEY-----"
  31. #define PEM_BEGIN_PRIVATE_KEY_RSA "-----BEGIN RSA PRIVATE KEY-----"
  32. #define PEM_END_PRIVATE_KEY_RSA "-----END RSA PRIVATE KEY-----"
  33. #define PEM_BEGIN_PUBLIC_KEY_RSA "-----BEGIN RSA PUBLIC KEY-----"
  34. #define PEM_END_PUBLIC_KEY_RSA "-----END RSA PUBLIC KEY-----"
  35. #define PEM_BEGIN_PRIVATE_KEY_EC "-----BEGIN EC PRIVATE KEY-----"
  36. #define PEM_END_PRIVATE_KEY_EC "-----END EC PRIVATE KEY-----"
  37. #define PEM_BEGIN_PRIVATE_KEY_PKCS8 "-----BEGIN PRIVATE KEY-----"
  38. #define PEM_END_PRIVATE_KEY_PKCS8 "-----END PRIVATE KEY-----"
  39. #define PEM_BEGIN_ENCRYPTED_PRIVATE_KEY_PKCS8 "-----BEGIN ENCRYPTED PRIVATE KEY-----"
  40. #define PEM_END_ENCRYPTED_PRIVATE_KEY_PKCS8 "-----END ENCRYPTED PRIVATE KEY-----"
  41. #if defined(MBEDTLS_PK_HAVE_ECC_KEYS) && !defined(MBEDTLS_PK_USE_PSA_EC_DATA)
  42. /**
  43. * Public function mbedtls_pk_ec() can be used to get direct access to the
  44. * wrapped ecp_keypair structure pointed to the pk_ctx. However this is not
  45. * ideal because it bypasses the PK module on the control of its internal
  46. * structure (pk_context) fields.
  47. * For backward compatibility we keep mbedtls_pk_ec() when ECP_C is defined, but
  48. * we provide 2 very similar functions when only ECP_LIGHT is enabled and not
  49. * ECP_C.
  50. * These variants embed the "ro" or "rw" keywords in their name to make the
  51. * usage of the returned pointer explicit. Of course the returned value is
  52. * const or non-const accordingly.
  53. */
  54. static inline const mbedtls_ecp_keypair *mbedtls_pk_ec_ro(const mbedtls_pk_context pk)
  55. {
  56. switch (mbedtls_pk_get_type(&pk)) {
  57. case MBEDTLS_PK_ECKEY:
  58. case MBEDTLS_PK_ECKEY_DH:
  59. case MBEDTLS_PK_ECDSA:
  60. return (const mbedtls_ecp_keypair *) (pk).MBEDTLS_PRIVATE(pk_ctx);
  61. default:
  62. return NULL;
  63. }
  64. }
  65. static inline mbedtls_ecp_keypair *mbedtls_pk_ec_rw(const mbedtls_pk_context pk)
  66. {
  67. switch (mbedtls_pk_get_type(&pk)) {
  68. case MBEDTLS_PK_ECKEY:
  69. case MBEDTLS_PK_ECKEY_DH:
  70. case MBEDTLS_PK_ECDSA:
  71. return (mbedtls_ecp_keypair *) (pk).MBEDTLS_PRIVATE(pk_ctx);
  72. default:
  73. return NULL;
  74. }
  75. }
  76. #endif /* MBEDTLS_PK_HAVE_ECC_KEYS && !MBEDTLS_PK_USE_PSA_EC_DATA */
  77. #if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
  78. static inline mbedtls_ecp_group_id mbedtls_pk_get_ec_group_id(const mbedtls_pk_context *pk)
  79. {
  80. mbedtls_ecp_group_id id;
  81. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  82. if (mbedtls_pk_get_type(pk) == MBEDTLS_PK_OPAQUE) {
  83. psa_key_attributes_t opaque_attrs = PSA_KEY_ATTRIBUTES_INIT;
  84. psa_key_type_t opaque_key_type;
  85. psa_ecc_family_t curve;
  86. if (psa_get_key_attributes(pk->priv_id, &opaque_attrs) != PSA_SUCCESS) {
  87. return MBEDTLS_ECP_DP_NONE;
  88. }
  89. opaque_key_type = psa_get_key_type(&opaque_attrs);
  90. curve = PSA_KEY_TYPE_ECC_GET_FAMILY(opaque_key_type);
  91. id = mbedtls_ecc_group_from_psa(curve, psa_get_key_bits(&opaque_attrs));
  92. psa_reset_key_attributes(&opaque_attrs);
  93. } else
  94. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  95. {
  96. #if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
  97. id = mbedtls_ecc_group_from_psa(pk->ec_family, pk->ec_bits);
  98. #else /* MBEDTLS_PK_USE_PSA_EC_DATA */
  99. id = mbedtls_pk_ec_ro(*pk)->grp.id;
  100. #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
  101. }
  102. return id;
  103. }
  104. /* Helper for Montgomery curves */
  105. #if defined(MBEDTLS_ECP_HAVE_CURVE25519) || defined(MBEDTLS_ECP_HAVE_CURVE448)
  106. #define MBEDTLS_PK_HAVE_RFC8410_CURVES
  107. #endif /* MBEDTLS_ECP_HAVE_CURVE25519 || MBEDTLS_ECP_DP_CURVE448 */
  108. #define MBEDTLS_PK_IS_RFC8410_GROUP_ID(id) \
  109. ((id == MBEDTLS_ECP_DP_CURVE25519) || (id == MBEDTLS_ECP_DP_CURVE448))
  110. static inline int mbedtls_pk_is_rfc8410(const mbedtls_pk_context *pk)
  111. {
  112. mbedtls_ecp_group_id id = mbedtls_pk_get_ec_group_id(pk);
  113. return MBEDTLS_PK_IS_RFC8410_GROUP_ID(id);
  114. }
  115. /*
  116. * Set the group used by this key.
  117. *
  118. * [in/out] pk: in: must have been pk_setup() to an ECC type
  119. * out: will have group (curve) information set
  120. * [in] grp_in: a supported group ID (not NONE)
  121. */
  122. int mbedtls_pk_ecc_set_group(mbedtls_pk_context *pk, mbedtls_ecp_group_id grp_id);
  123. /*
  124. * Set the private key material
  125. *
  126. * [in/out] pk: in: must have the group set already, see mbedtls_pk_ecc_set_group().
  127. * out: will have the private key set.
  128. * [in] key, key_len: the raw private key (no ASN.1 wrapping).
  129. */
  130. int mbedtls_pk_ecc_set_key(mbedtls_pk_context *pk, unsigned char *key, size_t key_len);
  131. /*
  132. * Set the public key.
  133. *
  134. * [in/out] pk: in: must have its group set, see mbedtls_pk_ecc_set_group().
  135. * out: will have the public key set.
  136. * [in] pub, pub_len: the raw public key (an ECPoint).
  137. *
  138. * Return:
  139. * - 0 on success;
  140. * - MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the format is potentially valid
  141. * but not supported;
  142. * - another error code otherwise.
  143. */
  144. int mbedtls_pk_ecc_set_pubkey(mbedtls_pk_context *pk, const unsigned char *pub, size_t pub_len);
  145. /*
  146. * Derive a public key from its private counterpart.
  147. * Computationally intensive, only use when public key is not available.
  148. *
  149. * [in/out] pk: in: must have the private key set, see mbedtls_pk_ecc_set_key().
  150. * out: will have the public key set.
  151. * [in] prv, prv_len: the raw private key (see note below).
  152. * [in] f_rng, p_rng: RNG function and context.
  153. *
  154. * Note: the private key information is always available from pk,
  155. * however for convenience the serialized version is also passed,
  156. * as it's available at each calling site, and useful in some configs
  157. * (as otherwise we would have to re-serialize it from the pk context).
  158. *
  159. * There are three implementations of this function:
  160. * 1. MBEDTLS_PK_USE_PSA_EC_DATA,
  161. * 2. MBEDTLS_USE_PSA_CRYPTO but not MBEDTLS_PK_USE_PSA_EC_DATA,
  162. * 3. not MBEDTLS_USE_PSA_CRYPTO.
  163. */
  164. int mbedtls_pk_ecc_set_pubkey_from_prv(mbedtls_pk_context *pk,
  165. const unsigned char *prv, size_t prv_len,
  166. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
  167. #endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
  168. /* Helper for (deterministic) ECDSA */
  169. #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
  170. #define MBEDTLS_PK_PSA_ALG_ECDSA_MAYBE_DET PSA_ALG_DETERMINISTIC_ECDSA
  171. #else
  172. #define MBEDTLS_PK_PSA_ALG_ECDSA_MAYBE_DET PSA_ALG_ECDSA
  173. #endif
  174. #if defined(MBEDTLS_TEST_HOOKS)
  175. MBEDTLS_STATIC_TESTABLE int mbedtls_pk_parse_key_pkcs8_encrypted_der(
  176. mbedtls_pk_context *pk,
  177. unsigned char *key, size_t keylen,
  178. const unsigned char *pwd, size_t pwdlen,
  179. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
  180. #endif
  181. #if defined(MBEDTLS_FS_IO)
  182. int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n);
  183. #endif
  184. #endif /* MBEDTLS_PK_INTERNAL_H */