VirtualBox

source: vbox/trunk/src/libs/openssl-1.1.0g/crypto/evp/p5_crpt2.c@ 69890

Last change on this file since 69890 was 69890, checked in by vboxsync, 7 years ago

Added OpenSSL 1.1.0g with unneeded files removed, otherwise unmodified.
bugref:8070: src/libs maintenance

  • Property svn:eol-style set to native
File size: 8.2 KB
Line 
1/*
2 * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10#include <stdio.h>
11#include <stdlib.h>
12#include "internal/cryptlib.h"
13# include <openssl/x509.h>
14# include <openssl/evp.h>
15# include <openssl/hmac.h>
16# include "evp_locl.h"
17
18/* set this to print out info about the keygen algorithm */
19/* #define OPENSSL_DEBUG_PKCS5V2 */
20
21# ifdef OPENSSL_DEBUG_PKCS5V2
22static void h__dump(const unsigned char *p, int len);
23# endif
24
25/*
26 * This is an implementation of PKCS#5 v2.0 password based encryption key
27 * derivation function PBKDF2. SHA1 version verified against test vectors
28 * posted by Peter Gutmann <[email protected]> to the PKCS-TNG
29 * <[email protected]> mailing list.
30 */
31
32int PKCS5_PBKDF2_HMAC(const char *pass, int passlen,
33 const unsigned char *salt, int saltlen, int iter,
34 const EVP_MD *digest, int keylen, unsigned char *out)
35{
36 const char *empty = "";
37 unsigned char digtmp[EVP_MAX_MD_SIZE], *p, itmp[4];
38 int cplen, j, k, tkeylen, mdlen;
39 unsigned long i = 1;
40 HMAC_CTX *hctx_tpl = NULL, *hctx = NULL;
41
42 mdlen = EVP_MD_size(digest);
43 if (mdlen < 0)
44 return 0;
45
46 hctx_tpl = HMAC_CTX_new();
47 if (hctx_tpl == NULL)
48 return 0;
49 p = out;
50 tkeylen = keylen;
51 if (pass == NULL) {
52 pass = empty;
53 passlen = 0;
54 } else if (passlen == -1) {
55 passlen = strlen(pass);
56 }
57 if (!HMAC_Init_ex(hctx_tpl, pass, passlen, digest, NULL)) {
58 HMAC_CTX_free(hctx_tpl);
59 return 0;
60 }
61 hctx = HMAC_CTX_new();
62 if (hctx == NULL) {
63 HMAC_CTX_free(hctx_tpl);
64 return 0;
65 }
66 while (tkeylen) {
67 if (tkeylen > mdlen)
68 cplen = mdlen;
69 else
70 cplen = tkeylen;
71 /*
72 * We are unlikely to ever use more than 256 blocks (5120 bits!) but
73 * just in case...
74 */
75 itmp[0] = (unsigned char)((i >> 24) & 0xff);
76 itmp[1] = (unsigned char)((i >> 16) & 0xff);
77 itmp[2] = (unsigned char)((i >> 8) & 0xff);
78 itmp[3] = (unsigned char)(i & 0xff);
79 if (!HMAC_CTX_copy(hctx, hctx_tpl)) {
80 HMAC_CTX_free(hctx);
81 HMAC_CTX_free(hctx_tpl);
82 return 0;
83 }
84 if (!HMAC_Update(hctx, salt, saltlen)
85 || !HMAC_Update(hctx, itmp, 4)
86 || !HMAC_Final(hctx, digtmp, NULL)) {
87 HMAC_CTX_free(hctx);
88 HMAC_CTX_free(hctx_tpl);
89 return 0;
90 }
91 HMAC_CTX_reset(hctx);
92 memcpy(p, digtmp, cplen);
93 for (j = 1; j < iter; j++) {
94 if (!HMAC_CTX_copy(hctx, hctx_tpl)) {
95 HMAC_CTX_free(hctx);
96 HMAC_CTX_free(hctx_tpl);
97 return 0;
98 }
99 if (!HMAC_Update(hctx, digtmp, mdlen)
100 || !HMAC_Final(hctx, digtmp, NULL)) {
101 HMAC_CTX_free(hctx);
102 HMAC_CTX_free(hctx_tpl);
103 return 0;
104 }
105 HMAC_CTX_reset(hctx);
106 for (k = 0; k < cplen; k++)
107 p[k] ^= digtmp[k];
108 }
109 tkeylen -= cplen;
110 i++;
111 p += cplen;
112 }
113 HMAC_CTX_free(hctx);
114 HMAC_CTX_free(hctx_tpl);
115# ifdef OPENSSL_DEBUG_PKCS5V2
116 fprintf(stderr, "Password:\n");
117 h__dump(pass, passlen);
118 fprintf(stderr, "Salt:\n");
119 h__dump(salt, saltlen);
120 fprintf(stderr, "Iteration count %d\n", iter);
121 fprintf(stderr, "Key:\n");
122 h__dump(out, keylen);
123# endif
124 return 1;
125}
126
127int PKCS5_PBKDF2_HMAC_SHA1(const char *pass, int passlen,
128 const unsigned char *salt, int saltlen, int iter,
129 int keylen, unsigned char *out)
130{
131 return PKCS5_PBKDF2_HMAC(pass, passlen, salt, saltlen, iter, EVP_sha1(),
132 keylen, out);
133}
134
135# ifdef DO_TEST
136main()
137{
138 unsigned char out[4];
139 unsigned char salt[] = { 0x12, 0x34, 0x56, 0x78 };
140 PKCS5_PBKDF2_HMAC_SHA1("password", -1, salt, 4, 5, 4, out);
141 fprintf(stderr, "Out %02X %02X %02X %02X\n",
142 out[0], out[1], out[2], out[3]);
143}
144
145# endif
146
147/*
148 * Now the key derivation function itself. This is a bit evil because it has
149 * to check the ASN1 parameters are valid: and there are quite a few of
150 * them...
151 */
152
153int PKCS5_v2_PBE_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass, int passlen,
154 ASN1_TYPE *param, const EVP_CIPHER *c,
155 const EVP_MD *md, int en_de)
156{
157 PBE2PARAM *pbe2 = NULL;
158 const EVP_CIPHER *cipher;
159 EVP_PBE_KEYGEN *kdf;
160
161 int rv = 0;
162
163 pbe2 = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(PBE2PARAM), param);
164 if (pbe2 == NULL) {
165 EVPerr(EVP_F_PKCS5_V2_PBE_KEYIVGEN, EVP_R_DECODE_ERROR);
166 goto err;
167 }
168
169 /* See if we recognise the key derivation function */
170 if (!EVP_PBE_find(EVP_PBE_TYPE_KDF, OBJ_obj2nid(pbe2->keyfunc->algorithm),
171 NULL, NULL, &kdf)) {
172 EVPerr(EVP_F_PKCS5_V2_PBE_KEYIVGEN,
173 EVP_R_UNSUPPORTED_KEY_DERIVATION_FUNCTION);
174 goto err;
175 }
176
177 /*
178 * lets see if we recognise the encryption algorithm.
179 */
180
181 cipher = EVP_get_cipherbyobj(pbe2->encryption->algorithm);
182
183 if (!cipher) {
184 EVPerr(EVP_F_PKCS5_V2_PBE_KEYIVGEN, EVP_R_UNSUPPORTED_CIPHER);
185 goto err;
186 }
187
188 /* Fixup cipher based on AlgorithmIdentifier */
189 if (!EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, en_de))
190 goto err;
191 if (EVP_CIPHER_asn1_to_param(ctx, pbe2->encryption->parameter) < 0) {
192 EVPerr(EVP_F_PKCS5_V2_PBE_KEYIVGEN, EVP_R_CIPHER_PARAMETER_ERROR);
193 goto err;
194 }
195 rv = kdf(ctx, pass, passlen, pbe2->keyfunc->parameter, NULL, NULL, en_de);
196 err:
197 PBE2PARAM_free(pbe2);
198 return rv;
199}
200
201int PKCS5_v2_PBKDF2_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass,
202 int passlen, ASN1_TYPE *param,
203 const EVP_CIPHER *c, const EVP_MD *md, int en_de)
204{
205 unsigned char *salt, key[EVP_MAX_KEY_LENGTH];
206 int saltlen, iter;
207 int rv = 0;
208 unsigned int keylen = 0;
209 int prf_nid, hmac_md_nid;
210 PBKDF2PARAM *kdf = NULL;
211 const EVP_MD *prfmd;
212
213 if (EVP_CIPHER_CTX_cipher(ctx) == NULL) {
214 EVPerr(EVP_F_PKCS5_V2_PBKDF2_KEYIVGEN, EVP_R_NO_CIPHER_SET);
215 goto err;
216 }
217 keylen = EVP_CIPHER_CTX_key_length(ctx);
218 OPENSSL_assert(keylen <= sizeof key);
219
220 /* Decode parameter */
221
222 kdf = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(PBKDF2PARAM), param);
223
224 if (kdf == NULL) {
225 EVPerr(EVP_F_PKCS5_V2_PBKDF2_KEYIVGEN, EVP_R_DECODE_ERROR);
226 goto err;
227 }
228
229 keylen = EVP_CIPHER_CTX_key_length(ctx);
230
231 /* Now check the parameters of the kdf */
232
233 if (kdf->keylength && (ASN1_INTEGER_get(kdf->keylength) != (int)keylen)) {
234 EVPerr(EVP_F_PKCS5_V2_PBKDF2_KEYIVGEN, EVP_R_UNSUPPORTED_KEYLENGTH);
235 goto err;
236 }
237
238 if (kdf->prf)
239 prf_nid = OBJ_obj2nid(kdf->prf->algorithm);
240 else
241 prf_nid = NID_hmacWithSHA1;
242
243 if (!EVP_PBE_find(EVP_PBE_TYPE_PRF, prf_nid, NULL, &hmac_md_nid, 0)) {
244 EVPerr(EVP_F_PKCS5_V2_PBKDF2_KEYIVGEN, EVP_R_UNSUPPORTED_PRF);
245 goto err;
246 }
247
248 prfmd = EVP_get_digestbynid(hmac_md_nid);
249 if (prfmd == NULL) {
250 EVPerr(EVP_F_PKCS5_V2_PBKDF2_KEYIVGEN, EVP_R_UNSUPPORTED_PRF);
251 goto err;
252 }
253
254 if (kdf->salt->type != V_ASN1_OCTET_STRING) {
255 EVPerr(EVP_F_PKCS5_V2_PBKDF2_KEYIVGEN, EVP_R_UNSUPPORTED_SALT_TYPE);
256 goto err;
257 }
258
259 /* it seems that its all OK */
260 salt = kdf->salt->value.octet_string->data;
261 saltlen = kdf->salt->value.octet_string->length;
262 iter = ASN1_INTEGER_get(kdf->iter);
263 if (!PKCS5_PBKDF2_HMAC(pass, passlen, salt, saltlen, iter, prfmd,
264 keylen, key))
265 goto err;
266 rv = EVP_CipherInit_ex(ctx, NULL, NULL, key, NULL, en_de);
267 err:
268 OPENSSL_cleanse(key, keylen);
269 PBKDF2PARAM_free(kdf);
270 return rv;
271}
272
273# ifdef OPENSSL_DEBUG_PKCS5V2
274static void h__dump(const unsigned char *p, int len)
275{
276 for (; len--; p++)
277 fprintf(stderr, "%02X ", *p);
278 fprintf(stderr, "\n");
279}
280# endif
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette