xref: /openbsd/lib/libcrypto/evp/evp_key.c (revision 9bac3682)
1*9bac3682Sbeck /* $OpenBSD: evp_key.c,v 1.36 2024/04/09 13:52:41 beck Exp $ */
25b37fcf3Sryker /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
35b37fcf3Sryker  * All rights reserved.
45b37fcf3Sryker  *
55b37fcf3Sryker  * This package is an SSL implementation written
65b37fcf3Sryker  * by Eric Young (eay@cryptsoft.com).
75b37fcf3Sryker  * The implementation was written so as to conform with Netscapes SSL.
85b37fcf3Sryker  *
95b37fcf3Sryker  * This library is free for commercial and non-commercial use as long as
105b37fcf3Sryker  * the following conditions are aheared to.  The following conditions
115b37fcf3Sryker  * apply to all code found in this distribution, be it the RC4, RSA,
125b37fcf3Sryker  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
135b37fcf3Sryker  * included with this distribution is covered by the same copyright terms
145b37fcf3Sryker  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
155b37fcf3Sryker  *
165b37fcf3Sryker  * Copyright remains Eric Young's, and as such any Copyright notices in
175b37fcf3Sryker  * the code are not to be removed.
185b37fcf3Sryker  * If this package is used in a product, Eric Young should be given attribution
195b37fcf3Sryker  * as the author of the parts of the library used.
205b37fcf3Sryker  * This can be in the form of a textual message at program startup or
215b37fcf3Sryker  * in documentation (online or textual) provided with the package.
225b37fcf3Sryker  *
235b37fcf3Sryker  * Redistribution and use in source and binary forms, with or without
245b37fcf3Sryker  * modification, are permitted provided that the following conditions
255b37fcf3Sryker  * are met:
265b37fcf3Sryker  * 1. Redistributions of source code must retain the copyright
275b37fcf3Sryker  *    notice, this list of conditions and the following disclaimer.
285b37fcf3Sryker  * 2. Redistributions in binary form must reproduce the above copyright
295b37fcf3Sryker  *    notice, this list of conditions and the following disclaimer in the
305b37fcf3Sryker  *    documentation and/or other materials provided with the distribution.
315b37fcf3Sryker  * 3. All advertising materials mentioning features or use of this software
325b37fcf3Sryker  *    must display the following acknowledgement:
335b37fcf3Sryker  *    "This product includes cryptographic software written by
345b37fcf3Sryker  *     Eric Young (eay@cryptsoft.com)"
355b37fcf3Sryker  *    The word 'cryptographic' can be left out if the rouines from the library
365b37fcf3Sryker  *    being used are not cryptographic related :-).
375b37fcf3Sryker  * 4. If you include any Windows specific code (or a derivative thereof) from
385b37fcf3Sryker  *    the apps directory (application code) you must include an acknowledgement:
395b37fcf3Sryker  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
405b37fcf3Sryker  *
415b37fcf3Sryker  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
425b37fcf3Sryker  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
435b37fcf3Sryker  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
445b37fcf3Sryker  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
455b37fcf3Sryker  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
465b37fcf3Sryker  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
475b37fcf3Sryker  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
485b37fcf3Sryker  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
495b37fcf3Sryker  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
505b37fcf3Sryker  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
515b37fcf3Sryker  * SUCH DAMAGE.
525b37fcf3Sryker  *
535b37fcf3Sryker  * The licence and distribution terms for any publically available version or
545b37fcf3Sryker  * derivative of this code cannot be changed.  i.e. this code cannot simply be
555b37fcf3Sryker  * copied and put under another distribution licence
565b37fcf3Sryker  * [including the GNU Public Licence.]
575b37fcf3Sryker  */
585b37fcf3Sryker 
595b37fcf3Sryker #include <stdio.h>
60a8913c44Sjsing #include <string.h>
61a8913c44Sjsing 
628ad85bf2Smiod #include <openssl/err.h>
63913ec974Sbeck #include <openssl/evp.h>
64b6ab114eSjsing #include <openssl/objects.h>
65da347917Sbeck #include <openssl/ui.h>
66b6ab114eSjsing #include <openssl/x509.h>
675b37fcf3Sryker 
68c9675a23Stb #include "evp_local.h"
69bc366ef8Stb 
705b37fcf3Sryker /* should be init to zeros. */
715b37fcf3Sryker static char prompt_string[80];
725b37fcf3Sryker 
732792286eSjsing void
EVP_set_pw_prompt(const char * prompt)742792286eSjsing EVP_set_pw_prompt(const char *prompt)
755b37fcf3Sryker {
765b37fcf3Sryker 	if (prompt == NULL)
775b37fcf3Sryker 		prompt_string[0] = '\0';
78fb8a40b8Stb 	else
7978e6ae94Sbeck 		strlcpy(prompt_string, prompt, sizeof(prompt_string));
80da347917Sbeck }
81*9bac3682Sbeck LCRYPTO_ALIAS(EVP_set_pw_prompt);
825b37fcf3Sryker 
832792286eSjsing char *
EVP_get_pw_prompt(void)842792286eSjsing EVP_get_pw_prompt(void)
855b37fcf3Sryker {
865b37fcf3Sryker 	if (prompt_string[0] == '\0')
87fb8a40b8Stb 		return NULL;
88fb8a40b8Stb 
89fb8a40b8Stb 	return prompt_string;
905b37fcf3Sryker }
91*9bac3682Sbeck LCRYPTO_ALIAS(EVP_get_pw_prompt);
925b37fcf3Sryker 
932792286eSjsing int
EVP_read_pw_string(char * buf,int len,const char * prompt,int verify)942792286eSjsing EVP_read_pw_string(char *buf, int len, const char *prompt, int verify)
955b37fcf3Sryker {
960a5d6edeSdjm 	return EVP_read_pw_string_min(buf, 0, len, prompt, verify);
970a5d6edeSdjm }
98*9bac3682Sbeck LCRYPTO_ALIAS(EVP_read_pw_string);
990a5d6edeSdjm 
1002792286eSjsing int
EVP_read_pw_string_min(char * buf,int min,int len,const char * prompt,int verify)1012792286eSjsing EVP_read_pw_string_min(char *buf, int min, int len, const char *prompt,
1022792286eSjsing     int verify)
1030a5d6edeSdjm {
104b0c223ceStb 	UI *ui = NULL;
105da347917Sbeck 	char buff[BUFSIZ];
106b0c223ceStb 	int ret = -1;
107da347917Sbeck 
108ddcb65f9Stb 	if (len > BUFSIZ)
109ddcb65f9Stb 		len = BUFSIZ;
110fcb1ddc7Stb 	/* Ensure that 0 <= min <= len - 1. In particular, 1 <= len. */
111ddcb65f9Stb 	if (min < 0 || len - 1 < min)
112b0c223ceStb 		goto err;
113b0c223ceStb 
1147c4f9497Stb 	if (prompt == NULL && prompt_string[0] != '\0')
1155b37fcf3Sryker 		prompt = prompt_string;
116b0c223ceStb 
117b0c223ceStb 	if ((ui = UI_new()) == NULL)
118b0c223ceStb 		goto err;
119ddcb65f9Stb 	if (UI_add_input_string(ui, prompt, 0, buf, min, len - 1) < 0)
120b0c223ceStb 		goto err;
121c40ede09Smiod 	if (verify) {
1227c4f9497Stb 		if (UI_add_verify_string(ui, prompt, 0, buff, min, len - 1,
1237c4f9497Stb 		    buf) < 0)
124b0c223ceStb 			goto err;
125c40ede09Smiod 	}
126b0c223ceStb 
127da347917Sbeck 	ret = UI_process(ui);
128b0c223ceStb 
129b0c223ceStb  err:
130da347917Sbeck 	UI_free(ui);
1310f777b12Sjsing 	explicit_bzero(buff, BUFSIZ);
132b0c223ceStb 
133da347917Sbeck 	return ret;
1345b37fcf3Sryker }
135*9bac3682Sbeck LCRYPTO_ALIAS(EVP_read_pw_string_min);
1365b37fcf3Sryker 
1372792286eSjsing int
EVP_BytesToKey(const EVP_CIPHER * type,const EVP_MD * md,const unsigned char * salt,const unsigned char * data,int datal,int count,unsigned char * key,unsigned char * iv)1382792286eSjsing EVP_BytesToKey(const EVP_CIPHER *type, const EVP_MD *md,
139200c0290Sbeck     const unsigned char *salt, const unsigned char *data, int datal,
140200c0290Sbeck     int count, unsigned char *key, unsigned char *iv)
1415b37fcf3Sryker {
142048f63e9Sjoshua 	EVP_MD_CTX *md_ctx;
1435b37fcf3Sryker 	unsigned char md_buf[EVP_MAX_MD_SIZE];
1445b37fcf3Sryker 	int niv, nkey, addmd = 0;
1455b37fcf3Sryker 	unsigned int mds = 0, i;
1465cdd308eSdjm 	int rv = 0;
1478ad85bf2Smiod 
1485b37fcf3Sryker 	nkey = type->key_len;
1495b37fcf3Sryker 	niv = type->iv_len;
1508ad85bf2Smiod 
1518ad85bf2Smiod 	if ((size_t)nkey > EVP_MAX_KEY_LENGTH) {
1525067ae9fSbeck 		EVPerror(EVP_R_BAD_KEY_LENGTH);
1538ad85bf2Smiod 		return 0;
1548ad85bf2Smiod 	}
1558ad85bf2Smiod 	if ((size_t)niv > EVP_MAX_IV_LENGTH) {
1565067ae9fSbeck 		EVPerror(EVP_R_IV_TOO_LARGE);
1578ad85bf2Smiod 		return 0;
1588ad85bf2Smiod 	}
1595b37fcf3Sryker 
1602792286eSjsing 	if (data == NULL)
161fb8a40b8Stb 		return nkey;
1625b37fcf3Sryker 
163048f63e9Sjoshua 	if ((md_ctx = EVP_MD_CTX_new()) == NULL)
164048f63e9Sjoshua 		goto err;
165048f63e9Sjoshua 
1662792286eSjsing 	for (;;) {
167048f63e9Sjoshua 		if (!EVP_DigestInit_ex(md_ctx, md, NULL))
16881c3b911Smiod 			goto err;
1695b37fcf3Sryker 		if (addmd++)
170048f63e9Sjoshua 			if (!EVP_DigestUpdate(md_ctx, &(md_buf[0]), mds))
1715cdd308eSdjm 				goto err;
172048f63e9Sjoshua 		if (!EVP_DigestUpdate(md_ctx, data, datal))
1735cdd308eSdjm 			goto err;
1745b37fcf3Sryker 		if (salt != NULL)
175048f63e9Sjoshua 			if (!EVP_DigestUpdate(md_ctx, salt, PKCS5_SALT_LEN))
1765cdd308eSdjm 				goto err;
177048f63e9Sjoshua 		if (!EVP_DigestFinal_ex(md_ctx, &(md_buf[0]), &mds))
1785cdd308eSdjm 			goto err;
1795b37fcf3Sryker 
1802792286eSjsing 		for (i = 1; i < (unsigned int)count; i++) {
181048f63e9Sjoshua 			if (!EVP_DigestInit_ex(md_ctx, md, NULL))
1825cdd308eSdjm 				goto err;
183048f63e9Sjoshua 			if (!EVP_DigestUpdate(md_ctx, &(md_buf[0]), mds))
1845cdd308eSdjm 				goto err;
185048f63e9Sjoshua 			if (!EVP_DigestFinal_ex(md_ctx, &(md_buf[0]), &mds))
1865cdd308eSdjm 				goto err;
1875b37fcf3Sryker 		}
1885b37fcf3Sryker 		i = 0;
1892792286eSjsing 		if (nkey) {
1902792286eSjsing 			for (;;) {
1912792286eSjsing 				if (nkey == 0)
1922792286eSjsing 					break;
1932792286eSjsing 				if (i == mds)
1942792286eSjsing 					break;
1955b37fcf3Sryker 				if (key != NULL)
1965b37fcf3Sryker 					*(key++) = md_buf[i];
1975b37fcf3Sryker 				nkey--;
1985b37fcf3Sryker 				i++;
1995b37fcf3Sryker 			}
2005b37fcf3Sryker 		}
2012792286eSjsing 		if (niv && (i != mds)) {
2022792286eSjsing 			for (;;) {
2032792286eSjsing 				if (niv == 0)
2042792286eSjsing 					break;
2052792286eSjsing 				if (i == mds)
2062792286eSjsing 					break;
2075b37fcf3Sryker 				if (iv != NULL)
2085b37fcf3Sryker 					*(iv++) = md_buf[i];
2095b37fcf3Sryker 				niv--;
2105b37fcf3Sryker 				i++;
2115b37fcf3Sryker 			}
2125b37fcf3Sryker 		}
2132792286eSjsing 		if ((nkey == 0) && (niv == 0))
2142792286eSjsing 			break;
2155b37fcf3Sryker 	}
2165cdd308eSdjm 	rv = type->key_len;
2172792286eSjsing 
2185cdd308eSdjm err:
219048f63e9Sjoshua 	EVP_MD_CTX_free(md_ctx);
2200f777b12Sjsing 	explicit_bzero(md_buf, sizeof md_buf);
2215cdd308eSdjm 	return rv;
2225b37fcf3Sryker }
223*9bac3682Sbeck LCRYPTO_ALIAS(EVP_BytesToKey);
224