blob: aaf8fe725765a63fd4705bcc6334876c06e05f5c [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"
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050028
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010029#include "mbedtls/asn1.h"
30#include "mbedtls/md.h"
Paul Bakkerb0c19a42013-06-24 19:26:38 +020031
Rich Evans00ab4702015-02-06 13:43:58 +000032#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020033#include <stdint.h>
Paul Bakkerb0c19a42013-06-24 19:26:38 +020034
Gilles Peskined2971572021-07-26 18:48:10 +020035/** Bad input parameters to function. */
36#define MBEDTLS_ERR_PKCS5_BAD_INPUT_DATA -0x2f80
37/** Unexpected ASN.1 data. */
38#define MBEDTLS_ERR_PKCS5_INVALID_FORMAT -0x2f00
39/** Requested encryption or digest alg not available. */
40#define MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE -0x2e80
41/** Given private key password does not allow for correct decryption. */
42#define MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH -0x2e00
Paul Bakker28144de2013-06-24 19:28:55 +020043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#define MBEDTLS_PKCS5_DECRYPT 0
45#define MBEDTLS_PKCS5_ENCRYPT 1
Paul Bakker28144de2013-06-24 19:28:55 +020046
Paul Bakkerb0c19a42013-06-24 19:26:38 +020047#ifdef __cplusplus
48extern "C" {
49#endif
50
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050051#if defined(MBEDTLS_ASN1_PARSE_C)
52
Paul Bakkerb0c19a42013-06-24 19:26:38 +020053/**
Paul Bakker28144de2013-06-24 19:28:55 +020054 * \brief PKCS#5 PBES2 function
55 *
Waleed Elmelegy708d78f2023-07-19 14:01:35 +010056 * \note When encrypting, #MBEDTLS_CIPHER_PADDING_PKCS7 must
57 * be enabled at compile time.
58 *
59 * \warning When decrypting:
60 * - if #MBEDTLS_CIPHER_PADDING_PKCS7 is enabled at compile
61 * time, this function validates the CBC padding and returns
62 * #MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH if the padding is
63 * invalid. Note that this can help active adversaries
64 * attempting to brute-forcing the password. Note also that
65 * there is no guarantee that an invalid password will be
66 * detected (the chances of a valid padding with a random
67 * password are about 1/255).
68 * - if #MBEDTLS_CIPHER_PADDING_PKCS7 is disabled at compile
69 * time, this function does not validate the CBC padding.
70 *
Paul Bakker28144de2013-06-24 19:28:55 +020071 * \param pbe_params the ASN.1 algorithm parameters
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072 * \param mode either MBEDTLS_PKCS5_DECRYPT or MBEDTLS_PKCS5_ENCRYPT
Paul Bakker28144de2013-06-24 19:28:55 +020073 * \param pwd password to use when generating key
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +020074 * \param pwdlen length of password
Paul Bakker28144de2013-06-24 19:28:55 +020075 * \param data data to process
76 * \param datalen length of data
Waleed Elmelegy708d78f2023-07-19 14:01:35 +010077 * \param output Output buffer.
Waleed Elmelegyf50767d2023-08-03 15:42:55 +010078 * On success, it contains the encrypted or decrypted data,
79 * possibly followed by the CBC padding.
80 * On failure, the content is indeterminate.
Waleed Elmelegy708d78f2023-07-19 14:01:35 +010081 * For decryption, there must be enough room for \p datalen
82 * bytes.
83 * For encryption, there must be enough room for
84 * \p datalen + 1 bytes, rounded up to the block size of
85 * the block cipher identified by \p pbe_params.
Paul Bakker28144de2013-06-24 19:28:55 +020086 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087 * \returns 0 on success, or a MBEDTLS_ERR_XXX code if verification fails.
Paul Bakker28144de2013-06-24 19:28:55 +020088 */
Gilles Peskine449bd832023-01-11 14:50:10 +010089int mbedtls_pkcs5_pbes2(const mbedtls_asn1_buf *pbe_params, int mode,
90 const unsigned char *pwd, size_t pwdlen,
91 const unsigned char *data, size_t datalen,
92 unsigned char *output);
Paul Bakker28144de2013-06-24 19:28:55 +020093
Waleed Elmelegy5d3f3152023-08-01 14:56:30 +010094#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
95
96/**
97 * \brief PKCS#5 PBES2 function
98 *
99 * \warning When decrypting:
100 * - This function validates the CBC padding and returns
101 * #MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH if the padding is
102 * invalid. Note that this can help active adversaries
103 * attempting to brute-forcing the password. Note also that
104 * there is no guarantee that an invalid password will be
105 * detected (the chances of a valid padding with a random
106 * password are about 1/255).
107 *
108 * \param pbe_params the ASN.1 algorithm parameters
109 * \param mode either MBEDTLS_PKCS5_DECRYPT or MBEDTLS_PKCS5_ENCRYPT
110 * \param pwd password to use when generating key
111 * \param pwdlen length of password
112 * \param data data to process
113 * \param datalen length of data
114 * \param output Output buffer.
Waleed Elmelegy12dd0402023-08-17 15:08:03 +0100115 * On success, it contains the decrypted data.
Waleed Elmelegy5d3f3152023-08-01 14:56:30 +0100116 * On failure, the content is indetermidate.
117 * For decryption, there must be enough room for \p datalen
118 * bytes.
119 * For encryption, there must be enough room for
120 * \p datalen + 1 bytes, rounded up to the block size of
121 * the block cipher identified by \p pbe_params.
122 * \param output_size size of output buffer.
123 * This must be big enough to accommodate for output plus
124 * padding data.
Waleed Elmelegy12dd0402023-08-17 15:08:03 +0100125 * \param output_len On success, length of actual data written to the output buffer.
Waleed Elmelegy5d3f3152023-08-01 14:56:30 +0100126 *
127 * \returns 0 on success, or a MBEDTLS_ERR_XXX code if verification fails.
128 */
129int mbedtls_pkcs5_pbes2_ext(const mbedtls_asn1_buf *pbe_params, int mode,
130 const unsigned char *pwd, size_t pwdlen,
131 const unsigned char *data, size_t datalen,
132 unsigned char *output, size_t output_size,
133 size_t *output_len);
134
135#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
136
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500137#endif /* MBEDTLS_ASN1_PARSE_C */
138
Paul Bakker28144de2013-06-24 19:28:55 +0200139/**
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400140 * \brief PKCS#5 PBKDF2 using HMAC without using the HMAC context
141 *
142 * \param md_type Hash algorithm used
143 * \param password Password to use when generating key
144 * \param plen Length of password
145 * \param salt Salt to use when generating key
146 * \param slen Length of salt
147 * \param iteration_count Iteration count
148 * \param key_length Length of generated key in bytes
149 * \param output Generated key. Must be at least as big as key_length
150 *
151 * \returns 0 on success, or a MBEDTLS_ERR_XXX code if verification fails.
152 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100153int mbedtls_pkcs5_pbkdf2_hmac_ext(mbedtls_md_type_t md_type,
154 const unsigned char *password,
155 size_t plen, const unsigned char *salt, size_t slen,
156 unsigned int iteration_count,
157 uint32_t key_length, unsigned char *output);
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400158
Andrzej Kurekf0004712022-08-31 19:10:42 -0400159#if defined(MBEDTLS_MD_C)
Andrzej Kurek890e78a2022-08-31 14:43:53 -0400160#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Andrzej Kurekdd36c762022-08-31 13:29:38 -0400161/**
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200162 * \brief PKCS#5 PBKDF2 using HMAC
163 *
Andrzej Kurek890e78a2022-08-31 14:43:53 -0400164 * \deprecated Superseded by mbedtls_pkcs5_pbkdf2_hmac_ext().
165 *
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200166 * \param ctx Generic HMAC context
167 * \param password Password to use when generating key
168 * \param plen Length of password
169 * \param salt Salt to use when generating key
170 * \param slen Length of salt
171 * \param iteration_count Iteration count
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200172 * \param key_length Length of generated key in bytes
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200173 * \param output Generated key. Must be at least as big as key_length
174 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 * \returns 0 on success, or a MBEDTLS_ERR_XXX code if verification fails.
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200176 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100177int MBEDTLS_DEPRECATED mbedtls_pkcs5_pbkdf2_hmac(mbedtls_md_context_t *ctx,
178 const unsigned char *password,
179 size_t plen,
180 const unsigned char *salt,
181 size_t slen,
182 unsigned int iteration_count,
183 uint32_t key_length,
184 unsigned char *output);
Andrzej Kurek890e78a2022-08-31 14:43:53 -0400185#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Andrzej Kurekf0004712022-08-31 19:10:42 -0400186#endif /* MBEDTLS_MD_C */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500187#if defined(MBEDTLS_SELF_TEST)
188
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200189/**
190 * \brief Checkup routine
191 *
192 * \return 0 if successful, or 1 if the test failed
193 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100194int mbedtls_pkcs5_self_test(int verbose);
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200195
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500196#endif /* MBEDTLS_SELF_TEST */
197
Paul Bakkerb0c19a42013-06-24 19:26:38 +0200198#ifdef __cplusplus
199}
200#endif
201
202#endif /* pkcs5.h */