1 /* $OpenBSD: p12_key.c,v 1.31 2022/07/30 11:27:06 tb Exp $ */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3  * project 1999.
4  */
5 /* ====================================================================
6  * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    licensing@OpenSSL.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This product includes cryptographic software written by Eric Young
54  * (eay@cryptsoft.com).  This product includes software written by Tim
55  * Hudson (tjh@cryptsoft.com).
56  *
57  */
58 
59 #include <stdio.h>
60 #include <string.h>
61 
62 #include <openssl/bn.h>
63 #include <openssl/err.h>
64 #include <openssl/pkcs12.h>
65 
66 #include "evp_locl.h"
67 
68 /* PKCS12 compatible key/IV generation */
69 #ifndef min
70 #define min(a,b) ((a) < (b) ? (a) : (b))
71 #endif
72 
73 int
74 PKCS12_key_gen_asc(const char *pass, int passlen, unsigned char *salt,
75     int saltlen, int id, int iter, int n, unsigned char *out,
76     const EVP_MD *md_type)
77 {
78 	int ret;
79 	unsigned char *unipass;
80 	int uniplen;
81 
82 	if (!pass) {
83 		unipass = NULL;
84 		uniplen = 0;
85 	} else if (!OPENSSL_asc2uni(pass, passlen, &unipass, &uniplen)) {
86 		PKCS12error(ERR_R_MALLOC_FAILURE);
87 		return 0;
88 	}
89 	ret = PKCS12_key_gen_uni(unipass, uniplen, salt, saltlen,
90 	    id, iter, n, out, md_type);
91 	if (ret <= 0)
92 		return 0;
93 	freezero(unipass, uniplen);
94 	return ret;
95 }
96 
97 int
98 PKCS12_key_gen_uni(unsigned char *pass, int passlen, unsigned char *salt,
99     int saltlen, int id, int iter, int n, unsigned char *out,
100     const EVP_MD *md_type)
101 {
102 	EVP_MD_CTX *ctx = NULL;
103 	unsigned char *B = NULL, *D = NULL, *I = NULL, *Ai = NULL;
104 	unsigned char *p;
105 	int Slen, Plen, Ilen;
106 	int i, j, u, v;
107 	int ret = 0;
108 
109 	if ((ctx = EVP_MD_CTX_new()) == NULL)
110 		goto err;
111 
112 	if ((v = EVP_MD_block_size(md_type)) <= 0)
113 		goto err;
114 	if ((u = EVP_MD_size(md_type)) <= 0)
115 		goto err;
116 
117 	if ((D = malloc(v)) == NULL)
118 		goto err;
119 	if ((Ai = malloc(u)) == NULL)
120 		goto err;
121 	if ((B = malloc(v + 1)) == NULL)
122 		goto err;
123 
124 	Slen = v * ((saltlen + v - 1) / v);
125 
126 	Plen = 0;
127 	if (passlen)
128 		Plen = v * ((passlen + v - 1) / v);
129 
130 	Ilen = Slen + Plen;
131 
132 	if ((I = malloc(Ilen)) == NULL)
133 		goto err;
134 
135 	for (i = 0; i < v; i++)
136 		D[i] = id;
137 
138 	p = I;
139 	for (i = 0; i < Slen; i++)
140 		*p++ = salt[i % saltlen];
141 	for (i = 0; i < Plen; i++)
142 		*p++ = pass[i % passlen];
143 
144 	for (;;) {
145 		if (!EVP_DigestInit_ex(ctx, md_type, NULL))
146 			goto err;
147 		if (!EVP_DigestUpdate(ctx, D, v))
148 			goto err;
149 		if (!EVP_DigestUpdate(ctx, I, Ilen))
150 			goto err;
151 		if (!EVP_DigestFinal_ex(ctx, Ai, NULL))
152 			goto err;
153 		for (j = 1; j < iter; j++) {
154 			if (!EVP_DigestInit_ex(ctx, md_type, NULL))
155 				goto err;
156 			if (!EVP_DigestUpdate(ctx, Ai, u))
157 				goto err;
158 			if (!EVP_DigestFinal_ex(ctx, Ai, NULL))
159 				goto err;
160 		}
161 		memcpy(out, Ai, min(n, u));
162 		if (u >= n) {
163 			ret = 1;
164 			goto end;
165 		}
166 		n -= u;
167 		out += u;
168 		for (j = 0; j < v; j++)
169 			B[j] = Ai[j % u];
170 
171 		for (j = 0; j < Ilen; j += v) {
172 			uint16_t c = 1;
173 			int k;
174 
175 			/* Work out I[j] = I[j] + B + 1. */
176 			for (k = v - 1; k >= 0; k--) {
177 				c += I[j + k] + B[k];
178 				I[j + k] = (unsigned char)c;
179 				c >>= 8;
180 			}
181 		}
182 	}
183 
184  err:
185 	PKCS12error(ERR_R_MALLOC_FAILURE);
186 
187  end:
188 	free(Ai);
189 	free(B);
190 	free(D);
191 	free(I);
192 	EVP_MD_CTX_free(ctx);
193 
194 	return ret;
195 }
196