pk_wrap.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /**
  2. * \file pk_wrap.h
  3. *
  4. * \brief Public Key abstraction layer: wrapper functions
  5. */
  6. /*
  7. * Copyright The Mbed TLS Contributors
  8. * SPDX-License-Identifier: Apache-2.0
  9. *
  10. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  11. * not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  18. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. */
  22. #ifndef MBEDTLS_PK_WRAP_H
  23. #define MBEDTLS_PK_WRAP_H
  24. #include "mbedtls/build_info.h"
  25. #include "mbedtls/pk.h"
  26. #if defined(MBEDTLS_PSA_CRYPTO_C)
  27. #include "psa/crypto.h"
  28. #endif /* MBEDTLS_PSA_CRYPTO_C */
  29. struct mbedtls_pk_info_t {
  30. /** Public key type */
  31. mbedtls_pk_type_t type;
  32. /** Type name */
  33. const char *name;
  34. /** Get key size in bits */
  35. size_t (*get_bitlen)(const void *);
  36. /** Tell if the context implements this type (e.g. ECKEY can do ECDSA) */
  37. int (*can_do)(mbedtls_pk_type_t type);
  38. /** Verify signature */
  39. int (*verify_func)(void *ctx, mbedtls_md_type_t md_alg,
  40. const unsigned char *hash, size_t hash_len,
  41. const unsigned char *sig, size_t sig_len);
  42. /** Make signature */
  43. int (*sign_func)(void *ctx, mbedtls_md_type_t md_alg,
  44. const unsigned char *hash, size_t hash_len,
  45. unsigned char *sig, size_t sig_size, size_t *sig_len,
  46. int (*f_rng)(void *, unsigned char *, size_t),
  47. void *p_rng);
  48. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  49. /** Verify signature (restartable) */
  50. int (*verify_rs_func)(void *ctx, mbedtls_md_type_t md_alg,
  51. const unsigned char *hash, size_t hash_len,
  52. const unsigned char *sig, size_t sig_len,
  53. void *rs_ctx);
  54. /** Make signature (restartable) */
  55. int (*sign_rs_func)(void *ctx, mbedtls_md_type_t md_alg,
  56. const unsigned char *hash, size_t hash_len,
  57. unsigned char *sig, size_t sig_size, size_t *sig_len,
  58. int (*f_rng)(void *, unsigned char *, size_t),
  59. void *p_rng, void *rs_ctx);
  60. #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
  61. /** Decrypt message */
  62. int (*decrypt_func)(void *ctx, const unsigned char *input, size_t ilen,
  63. unsigned char *output, size_t *olen, size_t osize,
  64. int (*f_rng)(void *, unsigned char *, size_t),
  65. void *p_rng);
  66. /** Encrypt message */
  67. int (*encrypt_func)(void *ctx, const unsigned char *input, size_t ilen,
  68. unsigned char *output, size_t *olen, size_t osize,
  69. int (*f_rng)(void *, unsigned char *, size_t),
  70. void *p_rng);
  71. /** Check public-private key pair */
  72. int (*check_pair_func)(const void *pub, const void *prv,
  73. int (*f_rng)(void *, unsigned char *, size_t),
  74. void *p_rng);
  75. /** Allocate a new context */
  76. void * (*ctx_alloc_func)(void);
  77. /** Free the given context */
  78. void (*ctx_free_func)(void *ctx);
  79. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  80. /** Allocate the restart context */
  81. void *(*rs_alloc_func)(void);
  82. /** Free the restart context */
  83. void (*rs_free_func)(void *rs_ctx);
  84. #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
  85. /** Interface with the debug module */
  86. void (*debug_func)(const void *ctx, mbedtls_pk_debug_item *items);
  87. };
  88. #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
  89. /* Container for RSA-alt */
  90. typedef struct {
  91. void *key;
  92. mbedtls_pk_rsa_alt_decrypt_func decrypt_func;
  93. mbedtls_pk_rsa_alt_sign_func sign_func;
  94. mbedtls_pk_rsa_alt_key_len_func key_len_func;
  95. } mbedtls_rsa_alt_context;
  96. #endif
  97. #if defined(MBEDTLS_RSA_C)
  98. extern const mbedtls_pk_info_t mbedtls_rsa_info;
  99. #endif
  100. #if defined(MBEDTLS_ECP_C)
  101. extern const mbedtls_pk_info_t mbedtls_eckey_info;
  102. extern const mbedtls_pk_info_t mbedtls_eckeydh_info;
  103. #endif
  104. #if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
  105. extern const mbedtls_pk_info_t mbedtls_ecdsa_info;
  106. #endif
  107. #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
  108. extern const mbedtls_pk_info_t mbedtls_rsa_alt_info;
  109. #endif
  110. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  111. extern const mbedtls_pk_info_t mbedtls_pk_ecdsa_opaque_info;
  112. extern const mbedtls_pk_info_t mbedtls_pk_rsa_opaque_info;
  113. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  114. #if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
  115. int MBEDTLS_DEPRECATED mbedtls_pk_error_from_psa_ecdsa(psa_status_t status);
  116. #endif
  117. #endif
  118. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  119. #if defined(MBEDTLS_PSA_CRYPTO_C)
  120. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  121. int MBEDTLS_DEPRECATED mbedtls_pk_error_from_psa(psa_status_t status);
  122. #if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY) || \
  123. defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
  124. int MBEDTLS_DEPRECATED mbedtls_pk_error_from_psa_rsa(psa_status_t status);
  125. #endif /* PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY || PSA_WANT_KEY_TYPE_RSA_KEY_PAIR */
  126. #endif /* !MBEDTLS_DEPRECATED_REMOVED */
  127. #if defined(MBEDTLS_RSA_C)
  128. int mbedtls_pk_psa_rsa_sign_ext(psa_algorithm_t psa_alg_md,
  129. mbedtls_rsa_context *rsa_ctx,
  130. const unsigned char *hash, size_t hash_len,
  131. unsigned char *sig, size_t sig_size,
  132. size_t *sig_len);
  133. #endif /* MBEDTLS_RSA_C */
  134. #endif /* MBEDTLS_PSA_CRYPTO_C */
  135. #endif /* MBEDTLS_PK_WRAP_H */