blob: 8b086aa2e2f0317712ee396f7e8ff71b1c7cd35e [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
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020010 * SPDX-License-Identifier: Apache-2.0
11 *
12 * Licensed under the Apache License, Version 2.0 (the "License"); you may
13 * not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 * http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
20 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
Paul Bakkerb0c19a42013-06-24 19:26:38 +020023 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#ifndef MBEDTLS_PKCS5_H
25#define MBEDTLS_PKCS5_H
Paul Bakkerb0c19a42013-06-24 19:26:38 +020026
Bence Szépkútic662b362021-05-27 11:25:03 +020027#include "mbedtls/build_info.h"
Waleed Elmelegyc9f40402023-08-08 15:28:15 +010028#include "mbedtls/platform_util.h"
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050029
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010030#include "mbedtls/asn1.h"
31#include "mbedtls/md.h"
Paul Bakkerb0c19a42013-06-24 19:26:38 +020032
Rich Evans00ab4702015-02-06 13:43:58 +000033#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020034#include <stdint.h>
Paul Bakkerb0c19a42013-06-24 19:26:38 +020035
Gilles Peskined2971572021-07-26 18:48:10 +020036/** Bad input parameters to function. */
37#define MBEDTLS_ERR_PKCS5_BAD_INPUT_DATA -0x2f80
38/** Unexpected ASN.1 data. */
39#define MBEDTLS_ERR_PKCS5_INVALID_FORMAT -0x2f00
40/** Requested encryption or digest alg not available. */
41#define MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE -0x2e80
42/** Given private key password does not allow for correct decryption. */
43#define MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH -0x2e00
Paul Bakker28144de2013-06-24 19:28:55 +020044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#define MBEDTLS_PKCS5_DECRYPT 0
46#define MBEDTLS_PKCS5_ENCRYPT 1
Paul Bakker28144de2013-06-24 19:28:55 +020047
Paul Bakkerb0c19a42013-06-24 19:26:38 +020048#ifdef __cplusplus
49extern "C" {
50#endif
51
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050052#if defined(MBEDTLS_ASN1_PARSE_C)
53
Waleed Elmelegyc9f40402023-08-08 15:28:15 +010054#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Paul Bakkerb0c19a42013-06-24 19:26:38 +020055/**
Paul Bakker28144de2013-06-24 19:28:55 +020056 * \brief PKCS#5 PBES2 function
57 *
Waleed Elmelegy708d78f2023-07-19 14:01:35 +010058 * \note When encrypting, #MBEDTLS_CIPHER_PADDING_PKCS7 must
59 * be enabled at compile time.
60 *
Waleed Elmelegyc9f40402023-08-08 15:28:15 +010061 * \deprecated This function is deprecated and will be removed in a
62 * future version of the library.
63 * Please use mbedtls_pkcs5_pbes2_ext() instead.
64 *
Waleed Elmelegy708d78f2023-07-19 14:01:35 +010065 * \warning When decrypting:
66 * - if #MBEDTLS_CIPHER_PADDING_PKCS7 is enabled at compile
67 * time, this function validates the CBC padding and returns
68 * #MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH if the padding is
69 * invalid. Note that this can help active adversaries
70 * attempting to brute-forcing the password. Note also that
71 * there is no guarantee that an invalid password will be
72 * detected (the chances of a valid padding with a random
73 * password are about 1/255).
74 * - if #MBEDTLS_CIPHER_PADDING_PKCS7 is disabled at compile
75 * time, this function does not validate the CBC padding.
76 *
Paul Bakker28144de2013-06-24 19:28:55 +020077 * \param pbe_params the ASN.1 algorithm parameters
Waleed Elmelegy79b6e262023-08-29 14:55:03 +010078 * \param mode either #MBEDTLS_PKCS5_DECRYPT or #MBEDTLS_PKCS5_ENCRYPT
Paul Bakker28144de2013-06-24 19:28:55 +020079 * \param pwd password to use when generating key
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +020080 * \param pwdlen length of password
Paul Bakker28144de2013-06-24 19:28:55 +020081 * \param data data to process
82 * \param datalen length of data
Waleed Elmelegy708d78f2023-07-19 14:01:35 +010083 * \param output Output buffer.
Waleed Elmelegyf50767d2023-08-03 15:42:55 +010084 * On success, it contains the encrypted or decrypted data,
85 * possibly followed by the CBC padding.
86 * On failure, the content is indeterminate.
Waleed Elmelegy708d78f2023-07-19 14:01:35 +010087 * For decryption, there must be enough room for \p datalen
88 * bytes.
89 * For encryption, there must be enough room for
90 * \p datalen + 1 bytes, rounded up to the block size of
91 * the block cipher identified by \p pbe_params.
Paul Bakker28144de2013-06-24 19:28:55 +020092 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093 * \returns 0 on success, or a MBEDTLS_ERR_XXX code if verification fails.
Paul Bakker28144de2013-06-24 19:28:55 +020094 */
Waleed Elmelegyc9f40402023-08-08 15:28:15 +010095int MBEDTLS_DEPRECATED mbedtls_pkcs5_pbes2(const mbedtls_asn1_buf *pbe_params, int mode,
96 const unsigned char *pwd, size_t pwdlen,
97 const unsigned char *data, size_t datalen,
98 unsigned char *output);
99#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker28144de2013-06-24 19:28:55 +0200100
Waleed Elmelegy5d3f3152023-08-01 14:56:30 +0100101#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
102
103/**
104 * \brief PKCS#5 PBES2 function
105 *
106 * \warning When decrypting:
107 * - This function validates the CBC padding and returns
108 * #MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH if the padding is
109 * invalid. Note that this can help active adversaries
110 * attempting to brute-forcing the password. Note also that
111 * there is no guarantee that an invalid password will be
112 * detected (the chances of a valid padding with a random
113 * password are about 1/255).
114 *
115 * \param pbe_params the ASN.1 algorithm parameters
Waleed Elmelegy79b6e262023-08-29 14:55:03 +0100116 * \param mode either #MBEDTLS_PKCS5_DECRYPT or #MBEDTLS_PKCS5_ENCRYPT
Waleed Elmelegy5d3f3152023-08-01 14:56:30 +0100117 * \param pwd password to use when generating key
118 * \param pwdlen length of password
119 * \param data data to process
120 * \param datalen length of data
121 * \param output Output buffer.
Waleed Elmelegy12dd0402023-08-17 15:08:03 +0100122 * On success, it contains the decrypted data.
Waleed Elmelegy5d3f3152023-08-01 14:56:30 +0100123 * On failure, the content is indetermidate.
124 * For decryption, there must be enough room for \p datalen
125 * bytes.
126 * For encryption, there must be enough room for
127 * \p datalen + 1 bytes, rounded up to the block size of
128 * the block cipher identified by \p pbe_params.
129 * \param output_size size of output buffer.
130 * This must be big enough to accommodate for output plus
131 * padding data.
Waleed Elmelegy12dd0402023-08-17 15:08:03 +0100132 * \param output_len On success, length of actual data written to the output buffer.
Waleed Elmelegy5d3f3152023-08-01 14:56:30 +0100133 *
Waleed Elmelegy79b6e262023-08-29 14:55:03 +0100134 * \returns 0 on success, or a MBEDTLS_ERR_XXX code if parsing or decryption fails.
Waleed Elmelegy5d3f3152023-08-01 14:56:30 +0100135 */
136int mbedtls_pkcs5_pbes2_ext(const mbedtls_asn1_buf *pbe_params, int mode,
137 const unsigned char *pwd, size_t pwdlen,
138 const unsigned char *data, size_t datalen,
139 unsigned char *output, size_t output_size,
140 size_t *output_len);
141
142#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
143
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500144#endif /* MBEDTLS_ASN1_PARSE_C */
145
Paul Bakker28144de2013-06-24 19:28:55 +0200146/**
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400147 * \brief PKCS#5 PBKDF2 using HMAC without using the HMAC context
148 *
149 * \param md_type Hash algorithm used
150 * \param password Password to use when generating key
151 * \param plen Length of password
152 * \param salt Salt to use when generating key
153 * \param slen Length of salt
154 * \param iteration_count Iteration count
155 * \param key_length Length of generated key in bytes
156 * \param output Generated key. Must be at least as big as key_length
157 *
158 * \returns 0 on success, or a MBEDTLS_ERR_XXX code if verification fails.
159 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100160int mbedtls_pkcs5_pbkdf2_hmac_ext(mbedtls_md_type_t md_type,
161 const unsigned char *password,
162 size_t plen, const unsigned char *salt, size_t slen,
163 unsigned int iteration_count,
164 uint32_t key_length, unsigned char *output);
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400165
Andrzej Kurekf0004712022-08-31 19:10:42 -0400166#if defined(MBEDTLS_MD_C)
Andrzej Kurek890e78a2022-08-31 14:43:53 -0400167#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400168/**
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200169 * \brief PKCS#5 PBKDF2 using HMAC
170 *
Andrzej Kurek890e78a2022-08-31 14:43:53 -0400171 * \deprecated Superseded by mbedtls_pkcs5_pbkdf2_hmac_ext().
172 *
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200173 * \param ctx Generic HMAC context
174 * \param password Password to use when generating key
175 * \param plen Length of password
176 * \param salt Salt to use when generating key
177 * \param slen Length of salt
178 * \param iteration_count Iteration count
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200179 * \param key_length Length of generated key in bytes
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200180 * \param output Generated key. Must be at least as big as key_length
181 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182 * \returns 0 on success, or a MBEDTLS_ERR_XXX code if verification fails.
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200183 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100184int MBEDTLS_DEPRECATED mbedtls_pkcs5_pbkdf2_hmac(mbedtls_md_context_t *ctx,
185 const unsigned char *password,
186 size_t plen,
187 const unsigned char *salt,
188 size_t slen,
189 unsigned int iteration_count,
190 uint32_t key_length,
191 unsigned char *output);
Andrzej Kurek890e78a2022-08-31 14:43:53 -0400192#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Andrzej Kurekf0004712022-08-31 19:10:42 -0400193#endif /* MBEDTLS_MD_C */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500194#if defined(MBEDTLS_SELF_TEST)
195
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200196/**
197 * \brief Checkup routine
198 *
199 * \return 0 if successful, or 1 if the test failed
200 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100201int mbedtls_pkcs5_self_test(int verbose);
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200202
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500203#endif /* MBEDTLS_SELF_TEST */
204
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200205#ifdef __cplusplus
206}
207#endif
208
209#endif /* pkcs5.h */