blob: b8c742e97c08c9dd0a7b922945c363b4fc280f7c [file] [log] [blame]
Paul Bakkerb0c19a42013-06-24 19:26:38 +02001/**
2 * \file pkcs#5.h
3 *
4 * \brief PKCS#5 functions
5 *
6 * \author Mathias Olsson <mathias@kompetensum.com>
7 *
8 * Copyright (C) 2006-2013, Brainspark B.V.
9 *
10 * This file is part of PolarSSL (http://www.polarssl.org)
11 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
12 *
13 * All rights reserved.
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License along
26 * with this program; if not, write to the Free Software Foundation, Inc.,
27 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 */
29#ifndef POLARSSL_PKCS5_H
30#define POLARSSL_PKCS5_H
31
32#include <string.h>
33
Paul Bakker28144de2013-06-24 19:28:55 +020034#include "asn1.h"
Paul Bakkerb0c19a42013-06-24 19:26:38 +020035#include "md.h"
36
37#ifdef _MSC_VER
38#include <basetsd.h>
39typedef UINT32 uint32_t;
40#else
41#include <inttypes.h>
42#endif
43
Paul Bakker28144de2013-06-24 19:28:55 +020044#define POLARSSL_ERR_PKCS5_BAD_INPUT_DATA -0x3f80 /**< Bad input parameters to function. */
45#define POLARSSL_ERR_PKCS5_INVALID_FORMAT -0x3f00 /**< Unexpected ASN.1 data. */
46#define POLARSSL_ERR_PKCS5_FEATURE_UNAVAILABLE -0x3e80 /**< Requested encryption or digest alg not available. */
47#define POLARSSL_ERR_PKCS5_PASSWORD_MISMATCH -0x3e00 /**< Given private key password does not allow for correct decryption. */
48
49#define PKCS5_DECRYPT 0
50#define PKCS5_ENCRYPT 1
51
52/*
53 * PKCS#5 OIDs
54 */
55#define OID_PKCS5 "\x2a\x86\x48\x86\xf7\x0d\x01\x05"
56#define OID_PKCS5_PBES2 OID_PKCS5 "\x0d"
57#define OID_PKCS5_PBKDF2 OID_PKCS5 "\x0c"
58
59/*
60 * Encryption Algorithm OIDs
61 */
62#define OID_DES_CBC "\x2b\x0e\x03\x02\x07"
63#define OID_DES_EDE3_CBC "\x2a\x86\x48\x86\xf7\x0d\x03\x07"
64
65/*
66 * Digest Algorithm OIDs
67 */
68#define OID_HMAC_SHA1 "\x2a\x86\x48\x86\xf7\x0d\x02\x07"
Paul Bakkerb0c19a42013-06-24 19:26:38 +020069
70#ifdef __cplusplus
71extern "C" {
72#endif
73
74/**
Paul Bakker28144de2013-06-24 19:28:55 +020075 * \brief PKCS#5 PBES2 function
76 *
77 * \param pbe_params the ASN.1 algorithm parameters
78 * \param mode either PKCS5_DECRYPT or PKCS5_ENCRYPT
79 * \param pwd password to use when generating key
80 * \param plen length of password
81 * \param data data to process
82 * \param datalen length of data
83 * \param output output buffer
84 *
85 * \returns 0 on success, or a PolarSSL error code if verification fails.
86 */
87int pkcs5_pbes2( asn1_buf *pbe_params, int mode,
88 const unsigned char *pwd, size_t pwdlen,
89 const unsigned char *data, size_t datalen,
90 unsigned char *output );
91
92/**
Paul Bakkerb0c19a42013-06-24 19:26:38 +020093 * \brief PKCS#5 PBKDF2 using HMAC
94 *
95 * \param ctx Generic HMAC context
96 * \param password Password to use when generating key
97 * \param plen Length of password
98 * \param salt Salt to use when generating key
99 * \param slen Length of salt
100 * \param iteration_count Iteration count
101 * \param key_length Length of generated key
102 * \param output Generated key. Must be at least as big as key_length
103 *
104 * \returns 0 on success, or a PolarSSL error code if verification fails.
105 */
106int pkcs5_pbkdf2_hmac( md_context_t *ctx, const unsigned char *password,
107 size_t plen, const unsigned char *salt, size_t slen,
108 unsigned int iteration_count,
109 uint32_t key_length, unsigned char *output );
110
111/**
112 * \brief Checkup routine
113 *
114 * \return 0 if successful, or 1 if the test failed
115 */
116int pkcs5_self_test( int verbose );
117
118#ifdef __cplusplus
119}
120#endif
121
122#endif /* pkcs5.h */