pkcs5.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /**
  2. * \file pkcs5.h
  3. *
  4. * \brief PKCS#5 functions
  5. *
  6. * \author Mathias Olsson <mathias@kompetensum.com>
  7. */
  8. /*
  9. * Copyright The Mbed TLS Contributors
  10. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  11. */
  12. #ifndef MBEDTLS_PKCS5_H
  13. #define MBEDTLS_PKCS5_H
  14. #include "mbedtls/build_info.h"
  15. #include "mbedtls/platform_util.h"
  16. #include "mbedtls/asn1.h"
  17. #include "mbedtls/md.h"
  18. #include "mbedtls/cipher.h"
  19. #include <stddef.h>
  20. #include <stdint.h>
  21. /** Bad input parameters to function. */
  22. #define MBEDTLS_ERR_PKCS5_BAD_INPUT_DATA -0x2f80
  23. /** Unexpected ASN.1 data. */
  24. #define MBEDTLS_ERR_PKCS5_INVALID_FORMAT -0x2f00
  25. /** Requested encryption or digest alg not available. */
  26. #define MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE -0x2e80
  27. /** Given private key password does not allow for correct decryption. */
  28. #define MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH -0x2e00
  29. #define MBEDTLS_PKCS5_DECRYPT MBEDTLS_DECRYPT
  30. #define MBEDTLS_PKCS5_ENCRYPT MBEDTLS_ENCRYPT
  31. #ifdef __cplusplus
  32. extern "C" {
  33. #endif
  34. #if defined(MBEDTLS_ASN1_PARSE_C) && defined(MBEDTLS_CIPHER_C)
  35. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  36. /**
  37. * \brief PKCS#5 PBES2 function
  38. *
  39. * \note When encrypting, #MBEDTLS_CIPHER_PADDING_PKCS7 must
  40. * be enabled at compile time.
  41. *
  42. * \deprecated This function is deprecated and will be removed in a
  43. * future version of the library.
  44. * Please use mbedtls_pkcs5_pbes2_ext() instead.
  45. *
  46. * \warning When decrypting:
  47. * - if #MBEDTLS_CIPHER_PADDING_PKCS7 is enabled at compile
  48. * time, this function validates the CBC padding and returns
  49. * #MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH if the padding is
  50. * invalid. Note that this can help active adversaries
  51. * attempting to brute-forcing the password. Note also that
  52. * there is no guarantee that an invalid password will be
  53. * detected (the chances of a valid padding with a random
  54. * password are about 1/255).
  55. * - if #MBEDTLS_CIPHER_PADDING_PKCS7 is disabled at compile
  56. * time, this function does not validate the CBC padding.
  57. *
  58. * \param pbe_params the ASN.1 algorithm parameters
  59. * \param mode either #MBEDTLS_PKCS5_DECRYPT or #MBEDTLS_PKCS5_ENCRYPT
  60. * \param pwd password to use when generating key
  61. * \param pwdlen length of password
  62. * \param data data to process
  63. * \param datalen length of data
  64. * \param output Output buffer.
  65. * On success, it contains the encrypted or decrypted data,
  66. * possibly followed by the CBC padding.
  67. * On failure, the content is indeterminate.
  68. * For decryption, there must be enough room for \p datalen
  69. * bytes.
  70. * For encryption, there must be enough room for
  71. * \p datalen + 1 bytes, rounded up to the block size of
  72. * the block cipher identified by \p pbe_params.
  73. *
  74. * \returns 0 on success, or a MBEDTLS_ERR_XXX code if verification fails.
  75. */
  76. int MBEDTLS_DEPRECATED mbedtls_pkcs5_pbes2(const mbedtls_asn1_buf *pbe_params, int mode,
  77. const unsigned char *pwd, size_t pwdlen,
  78. const unsigned char *data, size_t datalen,
  79. unsigned char *output);
  80. #endif /* MBEDTLS_DEPRECATED_REMOVED */
  81. #if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
  82. /**
  83. * \brief PKCS#5 PBES2 function
  84. *
  85. * \warning When decrypting:
  86. * - This function validates the CBC padding and returns
  87. * #MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH if the padding is
  88. * invalid. Note that this can help active adversaries
  89. * attempting to brute-forcing the password. Note also that
  90. * there is no guarantee that an invalid password will be
  91. * detected (the chances of a valid padding with a random
  92. * password are about 1/255).
  93. *
  94. * \param pbe_params the ASN.1 algorithm parameters
  95. * \param mode either #MBEDTLS_PKCS5_DECRYPT or #MBEDTLS_PKCS5_ENCRYPT
  96. * \param pwd password to use when generating key
  97. * \param pwdlen length of password
  98. * \param data data to process
  99. * \param datalen length of data
  100. * \param output Output buffer.
  101. * On success, it contains the decrypted data.
  102. * On failure, the content is indetermidate.
  103. * For decryption, there must be enough room for \p datalen
  104. * bytes.
  105. * For encryption, there must be enough room for
  106. * \p datalen + 1 bytes, rounded up to the block size of
  107. * the block cipher identified by \p pbe_params.
  108. * \param output_size size of output buffer.
  109. * This must be big enough to accommodate for output plus
  110. * padding data.
  111. * \param output_len On success, length of actual data written to the output buffer.
  112. *
  113. * \returns 0 on success, or a MBEDTLS_ERR_XXX code if parsing or decryption fails.
  114. */
  115. int mbedtls_pkcs5_pbes2_ext(const mbedtls_asn1_buf *pbe_params, int mode,
  116. const unsigned char *pwd, size_t pwdlen,
  117. const unsigned char *data, size_t datalen,
  118. unsigned char *output, size_t output_size,
  119. size_t *output_len);
  120. #endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
  121. #endif /* MBEDTLS_ASN1_PARSE_C && MBEDTLS_CIPHER_C*/
  122. /**
  123. * \brief PKCS#5 PBKDF2 using HMAC without using the HMAC context
  124. *
  125. * \param md_type Hash algorithm used
  126. * \param password Password to use when generating key
  127. * \param plen Length of password
  128. * \param salt Salt to use when generating key
  129. * \param slen Length of salt
  130. * \param iteration_count Iteration count
  131. * \param key_length Length of generated key in bytes
  132. * \param output Generated key. Must be at least as big as key_length
  133. *
  134. * \returns 0 on success, or a MBEDTLS_ERR_XXX code if verification fails.
  135. */
  136. int mbedtls_pkcs5_pbkdf2_hmac_ext(mbedtls_md_type_t md_type,
  137. const unsigned char *password,
  138. size_t plen, const unsigned char *salt, size_t slen,
  139. unsigned int iteration_count,
  140. uint32_t key_length, unsigned char *output);
  141. #if defined(MBEDTLS_MD_C)
  142. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  143. /**
  144. * \brief PKCS#5 PBKDF2 using HMAC
  145. *
  146. * \deprecated Superseded by mbedtls_pkcs5_pbkdf2_hmac_ext().
  147. *
  148. * \param ctx Generic HMAC context
  149. * \param password Password to use when generating key
  150. * \param plen Length of password
  151. * \param salt Salt to use when generating key
  152. * \param slen Length of salt
  153. * \param iteration_count Iteration count
  154. * \param key_length Length of generated key in bytes
  155. * \param output Generated key. Must be at least as big as key_length
  156. *
  157. * \returns 0 on success, or a MBEDTLS_ERR_XXX code if verification fails.
  158. */
  159. int MBEDTLS_DEPRECATED mbedtls_pkcs5_pbkdf2_hmac(mbedtls_md_context_t *ctx,
  160. const unsigned char *password,
  161. size_t plen,
  162. const unsigned char *salt,
  163. size_t slen,
  164. unsigned int iteration_count,
  165. uint32_t key_length,
  166. unsigned char *output);
  167. #endif /* !MBEDTLS_DEPRECATED_REMOVED */
  168. #endif /* MBEDTLS_MD_C */
  169. #if defined(MBEDTLS_SELF_TEST)
  170. /**
  171. * \brief Checkup routine
  172. *
  173. * \return 0 if successful, or 1 if the test failed
  174. */
  175. int mbedtls_pkcs5_self_test(int verbose);
  176. #endif /* MBEDTLS_SELF_TEST */
  177. #ifdef __cplusplus
  178. }
  179. #endif
  180. #endif /* pkcs5.h */