blob: e004f4555c6db83d24211b9b25ea54be7f44a96c [file] [log] [blame]
Paul Bakkerb0c19a42013-06-24 19:26:38 +02001/**
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +02002 * \file pkcs5.h
Paul Bakkerb0c19a42013-06-24 19:26:38 +02003 *
4 * \brief PKCS#5 functions
5 *
6 * \author Mathias Olsson <mathias@kompetensum.com>
Darryl Greena40a1012018-01-05 15:33:17 +00007 */
8/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02009 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +000010 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakkerb0c19a42013-06-24 19:26:38 +020011 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012#ifndef MBEDTLS_PKCS5_H
13#define MBEDTLS_PKCS5_H
Paul Bakkerb0c19a42013-06-24 19:26:38 +020014
Bence Szépkútic662b362021-05-27 11:25:03 +020015#include "mbedtls/build_info.h"
Waleed Elmelegyc9f40402023-08-08 15:28:15 +010016#include "mbedtls/platform_util.h"
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050017
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010018#include "mbedtls/asn1.h"
19#include "mbedtls/md.h"
Paul Bakkerb0c19a42013-06-24 19:26:38 +020020
Rich Evans00ab4702015-02-06 13:43:58 +000021#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020022#include <stdint.h>
Paul Bakkerb0c19a42013-06-24 19:26:38 +020023
Gilles Peskined2971572021-07-26 18:48:10 +020024/** Bad input parameters to function. */
25#define MBEDTLS_ERR_PKCS5_BAD_INPUT_DATA -0x2f80
26/** Unexpected ASN.1 data. */
27#define MBEDTLS_ERR_PKCS5_INVALID_FORMAT -0x2f00
28/** Requested encryption or digest alg not available. */
29#define MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE -0x2e80
30/** Given private key password does not allow for correct decryption. */
31#define MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH -0x2e00
Paul Bakker28144de2013-06-24 19:28:55 +020032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#define MBEDTLS_PKCS5_DECRYPT 0
34#define MBEDTLS_PKCS5_ENCRYPT 1
Paul Bakker28144de2013-06-24 19:28:55 +020035
Paul Bakkerb0c19a42013-06-24 19:26:38 +020036#ifdef __cplusplus
37extern "C" {
38#endif
39
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050040#if defined(MBEDTLS_ASN1_PARSE_C)
41
Waleed Elmelegyc9f40402023-08-08 15:28:15 +010042#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Paul Bakkerb0c19a42013-06-24 19:26:38 +020043/**
Paul Bakker28144de2013-06-24 19:28:55 +020044 * \brief PKCS#5 PBES2 function
45 *
Waleed Elmelegy708d78f2023-07-19 14:01:35 +010046 * \note When encrypting, #MBEDTLS_CIPHER_PADDING_PKCS7 must
47 * be enabled at compile time.
48 *
Waleed Elmelegyc9f40402023-08-08 15:28:15 +010049 * \deprecated This function is deprecated and will be removed in a
50 * future version of the library.
51 * Please use mbedtls_pkcs5_pbes2_ext() instead.
52 *
Waleed Elmelegy708d78f2023-07-19 14:01:35 +010053 * \warning When decrypting:
54 * - if #MBEDTLS_CIPHER_PADDING_PKCS7 is enabled at compile
55 * time, this function validates the CBC padding and returns
56 * #MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH if the padding is
57 * invalid. Note that this can help active adversaries
58 * attempting to brute-forcing the password. Note also that
59 * there is no guarantee that an invalid password will be
60 * detected (the chances of a valid padding with a random
61 * password are about 1/255).
62 * - if #MBEDTLS_CIPHER_PADDING_PKCS7 is disabled at compile
63 * time, this function does not validate the CBC padding.
64 *
Paul Bakker28144de2013-06-24 19:28:55 +020065 * \param pbe_params the ASN.1 algorithm parameters
Waleed Elmelegy79b6e262023-08-29 14:55:03 +010066 * \param mode either #MBEDTLS_PKCS5_DECRYPT or #MBEDTLS_PKCS5_ENCRYPT
Paul Bakker28144de2013-06-24 19:28:55 +020067 * \param pwd password to use when generating key
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +020068 * \param pwdlen length of password
Paul Bakker28144de2013-06-24 19:28:55 +020069 * \param data data to process
70 * \param datalen length of data
Waleed Elmelegy708d78f2023-07-19 14:01:35 +010071 * \param output Output buffer.
Waleed Elmelegyf50767d2023-08-03 15:42:55 +010072 * On success, it contains the encrypted or decrypted data,
73 * possibly followed by the CBC padding.
74 * On failure, the content is indeterminate.
Waleed Elmelegy708d78f2023-07-19 14:01:35 +010075 * For decryption, there must be enough room for \p datalen
76 * bytes.
77 * For encryption, there must be enough room for
78 * \p datalen + 1 bytes, rounded up to the block size of
79 * the block cipher identified by \p pbe_params.
Paul Bakker28144de2013-06-24 19:28:55 +020080 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081 * \returns 0 on success, or a MBEDTLS_ERR_XXX code if verification fails.
Paul Bakker28144de2013-06-24 19:28:55 +020082 */
Waleed Elmelegyc9f40402023-08-08 15:28:15 +010083int MBEDTLS_DEPRECATED mbedtls_pkcs5_pbes2(const mbedtls_asn1_buf *pbe_params, int mode,
84 const unsigned char *pwd, size_t pwdlen,
85 const unsigned char *data, size_t datalen,
86 unsigned char *output);
87#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker28144de2013-06-24 19:28:55 +020088
Waleed Elmelegy5d3f3152023-08-01 14:56:30 +010089#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
90
91/**
92 * \brief PKCS#5 PBES2 function
93 *
94 * \warning When decrypting:
95 * - This function validates the CBC padding and returns
96 * #MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH if the padding is
97 * invalid. Note that this can help active adversaries
98 * attempting to brute-forcing the password. Note also that
99 * there is no guarantee that an invalid password will be
100 * detected (the chances of a valid padding with a random
101 * password are about 1/255).
102 *
103 * \param pbe_params the ASN.1 algorithm parameters
Waleed Elmelegy79b6e262023-08-29 14:55:03 +0100104 * \param mode either #MBEDTLS_PKCS5_DECRYPT or #MBEDTLS_PKCS5_ENCRYPT
Waleed Elmelegy5d3f3152023-08-01 14:56:30 +0100105 * \param pwd password to use when generating key
106 * \param pwdlen length of password
107 * \param data data to process
108 * \param datalen length of data
109 * \param output Output buffer.
Waleed Elmelegy12dd0402023-08-17 15:08:03 +0100110 * On success, it contains the decrypted data.
Waleed Elmelegy5d3f3152023-08-01 14:56:30 +0100111 * On failure, the content is indetermidate.
112 * For decryption, there must be enough room for \p datalen
113 * bytes.
114 * For encryption, there must be enough room for
115 * \p datalen + 1 bytes, rounded up to the block size of
116 * the block cipher identified by \p pbe_params.
117 * \param output_size size of output buffer.
118 * This must be big enough to accommodate for output plus
119 * padding data.
Waleed Elmelegy12dd0402023-08-17 15:08:03 +0100120 * \param output_len On success, length of actual data written to the output buffer.
Waleed Elmelegy5d3f3152023-08-01 14:56:30 +0100121 *
Waleed Elmelegy79b6e262023-08-29 14:55:03 +0100122 * \returns 0 on success, or a MBEDTLS_ERR_XXX code if parsing or decryption fails.
Waleed Elmelegy5d3f3152023-08-01 14:56:30 +0100123 */
124int mbedtls_pkcs5_pbes2_ext(const mbedtls_asn1_buf *pbe_params, int mode,
125 const unsigned char *pwd, size_t pwdlen,
126 const unsigned char *data, size_t datalen,
127 unsigned char *output, size_t output_size,
128 size_t *output_len);
129
130#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
131
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500132#endif /* MBEDTLS_ASN1_PARSE_C */
133
Paul Bakker28144de2013-06-24 19:28:55 +0200134/**
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400135 * \brief PKCS#5 PBKDF2 using HMAC without using the HMAC context
136 *
137 * \param md_type Hash algorithm used
138 * \param password Password to use when generating key
139 * \param plen Length of password
140 * \param salt Salt to use when generating key
141 * \param slen Length of salt
142 * \param iteration_count Iteration count
143 * \param key_length Length of generated key in bytes
144 * \param output Generated key. Must be at least as big as key_length
145 *
146 * \returns 0 on success, or a MBEDTLS_ERR_XXX code if verification fails.
147 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100148int mbedtls_pkcs5_pbkdf2_hmac_ext(mbedtls_md_type_t md_type,
149 const unsigned char *password,
150 size_t plen, const unsigned char *salt, size_t slen,
151 unsigned int iteration_count,
152 uint32_t key_length, unsigned char *output);
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400153
Andrzej Kurekf0004712022-08-31 19:10:42 -0400154#if defined(MBEDTLS_MD_C)
Andrzej Kurek890e78a2022-08-31 14:43:53 -0400155#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400156/**
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200157 * \brief PKCS#5 PBKDF2 using HMAC
158 *
Andrzej Kurek890e78a2022-08-31 14:43:53 -0400159 * \deprecated Superseded by mbedtls_pkcs5_pbkdf2_hmac_ext().
160 *
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200161 * \param ctx Generic HMAC context
162 * \param password Password to use when generating key
163 * \param plen Length of password
164 * \param salt Salt to use when generating key
165 * \param slen Length of salt
166 * \param iteration_count Iteration count
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200167 * \param key_length Length of generated key in bytes
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200168 * \param output Generated key. Must be at least as big as key_length
169 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 * \returns 0 on success, or a MBEDTLS_ERR_XXX code if verification fails.
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200171 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100172int MBEDTLS_DEPRECATED mbedtls_pkcs5_pbkdf2_hmac(mbedtls_md_context_t *ctx,
173 const unsigned char *password,
174 size_t plen,
175 const unsigned char *salt,
176 size_t slen,
177 unsigned int iteration_count,
178 uint32_t key_length,
179 unsigned char *output);
Andrzej Kurek890e78a2022-08-31 14:43:53 -0400180#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Andrzej Kurekf0004712022-08-31 19:10:42 -0400181#endif /* MBEDTLS_MD_C */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500182#if defined(MBEDTLS_SELF_TEST)
183
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200184/**
185 * \brief Checkup routine
186 *
187 * \return 0 if successful, or 1 if the test failed
188 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100189int mbedtls_pkcs5_self_test(int verbose);
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200190
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500191#endif /* MBEDTLS_SELF_TEST */
192
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200193#ifdef __cplusplus
194}
195#endif
196
197#endif /* pkcs5.h */