rsa_internal.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**
  2. * \file rsa_internal.h
  3. *
  4. * \brief Internal-only RSA public-key cryptosystem API.
  5. *
  6. * This file declares RSA-related functions that are to be used
  7. * only from within the Mbed TLS library itself.
  8. *
  9. */
  10. /*
  11. * Copyright The Mbed TLS Contributors
  12. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  13. */
  14. #ifndef MBEDTLS_RSA_INTERNAL_H
  15. #define MBEDTLS_RSA_INTERNAL_H
  16. #include "mbedtls/rsa.h"
  17. #include "mbedtls/asn1.h"
  18. /**
  19. * \brief Parse a PKCS#1 (ASN.1) encoded private RSA key.
  20. *
  21. * \param rsa The RSA context where parsed data will be stored.
  22. * \param key The buffer that contains the key.
  23. * \param keylen The length of the key buffer in bytes.
  24. *
  25. * \return 0 on success.
  26. * \return MBEDTLS_ERR_ASN1_xxx in case of ASN.1 parsing errors.
  27. * \return MBEDTLS_ERR_RSA_xxx in case of RSA internal failures while
  28. * parsing data.
  29. * \return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED if validity checks on the
  30. * provided key fail.
  31. */
  32. int mbedtls_rsa_parse_key(mbedtls_rsa_context *rsa, const unsigned char *key, size_t keylen);
  33. /**
  34. * \brief Parse a PKCS#1 (ASN.1) encoded public RSA key.
  35. *
  36. * \param rsa The RSA context where parsed data will be stored.
  37. * \param key The buffer that contains the key.
  38. * \param keylen The length of the key buffer in bytes.
  39. *
  40. * \return 0 on success.
  41. * \return MBEDTLS_ERR_ASN1_xxx in case of ASN.1 parsing errors.
  42. * \return MBEDTLS_ERR_RSA_xxx in case of RSA internal failures while
  43. * parsing data.
  44. * \return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED if validity checks on the
  45. * provided key fail.
  46. */
  47. int mbedtls_rsa_parse_pubkey(mbedtls_rsa_context *rsa, const unsigned char *key, size_t keylen);
  48. /**
  49. * \brief Write a PKCS#1 (ASN.1) encoded private RSA key.
  50. *
  51. * \param rsa The RSA context which contains the data to be written.
  52. * \param start Beginning of the buffer that will be filled with the
  53. * private key.
  54. * \param p End of the buffer that will be filled with the private key.
  55. * On successful return, the referenced pointer will be
  56. * updated in order to point to the beginning of written data.
  57. *
  58. * \return On success, the number of bytes written to the output buffer
  59. * (i.e. a value > 0).
  60. * \return MBEDTLS_ERR_RSA_BAD_INPUT_DATA if the RSA context does not
  61. * contain a valid key pair.
  62. * \return MBEDTLS_ERR_ASN1_xxx in case of failure while writing to the
  63. * output buffer.
  64. *
  65. * \note The output buffer is filled backward, i.e. starting from its
  66. * end and moving toward its start.
  67. */
  68. int mbedtls_rsa_write_key(const mbedtls_rsa_context *rsa, unsigned char *start,
  69. unsigned char **p);
  70. /**
  71. * \brief Parse a PKCS#1 (ASN.1) encoded public RSA key.
  72. *
  73. * \param rsa The RSA context which contains the data to be written.
  74. * \param start Beginning of the buffer that will be filled with the
  75. * private key.
  76. * \param p End of the buffer that will be filled with the private key.
  77. * On successful return, the referenced pointer will be
  78. * updated in order to point to the beginning of written data.
  79. *
  80. * \return On success, the number of bytes written to the output buffer
  81. * (i.e. a value > 0).
  82. * \return MBEDTLS_ERR_RSA_BAD_INPUT_DATA if the RSA context does not
  83. * contain a valid public key.
  84. * \return MBEDTLS_ERR_ASN1_xxx in case of failure while writing to the
  85. * output buffer.
  86. *
  87. * \note The output buffer is filled backward, i.e. starting from its
  88. * end and moving toward its start.
  89. */
  90. int mbedtls_rsa_write_pubkey(const mbedtls_rsa_context *rsa, unsigned char *start,
  91. unsigned char **p);
  92. #if defined(MBEDTLS_PKCS1_V21)
  93. /**
  94. * \brief This function is analogue to \c mbedtls_rsa_rsassa_pss_sign().
  95. * The only difference between them is that this function is more flexible
  96. * on the parameters of \p ctx that are set with \c mbedtls_rsa_set_padding().
  97. *
  98. * \note Compared to its counterpart, this function:
  99. * - does not check the padding setting of \p ctx.
  100. * - allows the hash_id of \p ctx to be MBEDTLS_MD_NONE,
  101. * in which case it uses \p md_alg as the hash_id.
  102. *
  103. * \note Refer to \c mbedtls_rsa_rsassa_pss_sign() for a description
  104. * of the functioning and parameters of this function.
  105. */
  106. int mbedtls_rsa_rsassa_pss_sign_no_mode_check(mbedtls_rsa_context *ctx,
  107. int (*f_rng)(void *, unsigned char *, size_t),
  108. void *p_rng,
  109. mbedtls_md_type_t md_alg,
  110. unsigned int hashlen,
  111. const unsigned char *hash,
  112. unsigned char *sig);
  113. #endif /* MBEDTLS_PKCS1_V21 */
  114. #endif /* rsa_internal.h */