155714Skris/* crypto/evp/evp_key.c */
255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
355714Skris * All rights reserved.
455714Skris *
555714Skris * This package is an SSL implementation written
655714Skris * by Eric Young (eay@cryptsoft.com).
755714Skris * The implementation was written so as to conform with Netscapes SSL.
8296341Sdelphij *
955714Skris * This library is free for commercial and non-commercial use as long as
1055714Skris * the following conditions are aheared to.  The following conditions
1155714Skris * apply to all code found in this distribution, be it the RC4, RSA,
1255714Skris * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1355714Skris * included with this distribution is covered by the same copyright terms
1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15296341Sdelphij *
1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in
1755714Skris * the code are not to be removed.
1855714Skris * If this package is used in a product, Eric Young should be given attribution
1955714Skris * as the author of the parts of the library used.
2055714Skris * This can be in the form of a textual message at program startup or
2155714Skris * in documentation (online or textual) provided with the package.
22296341Sdelphij *
2355714Skris * Redistribution and use in source and binary forms, with or without
2455714Skris * modification, are permitted provided that the following conditions
2555714Skris * are met:
2655714Skris * 1. Redistributions of source code must retain the copyright
2755714Skris *    notice, this list of conditions and the following disclaimer.
2855714Skris * 2. Redistributions in binary form must reproduce the above copyright
2955714Skris *    notice, this list of conditions and the following disclaimer in the
3055714Skris *    documentation and/or other materials provided with the distribution.
3155714Skris * 3. All advertising materials mentioning features or use of this software
3255714Skris *    must display the following acknowledgement:
3355714Skris *    "This product includes cryptographic software written by
3455714Skris *     Eric Young (eay@cryptsoft.com)"
3555714Skris *    The word 'cryptographic' can be left out if the rouines from the library
3655714Skris *    being used are not cryptographic related :-).
37296341Sdelphij * 4. If you include any Windows specific code (or a derivative thereof) from
3855714Skris *    the apps directory (application code) you must include an acknowledgement:
3955714Skris *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40296341Sdelphij *
4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4455714Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5155714Skris * SUCH DAMAGE.
52296341Sdelphij *
5355714Skris * The licence and distribution terms for any publically available version or
5455714Skris * derivative of this code cannot be changed.  i.e. this code cannot simply be
5555714Skris * copied and put under another distribution licence
5655714Skris * [including the GNU Public Licence.]
5755714Skris */
5855714Skris
5955714Skris#include <stdio.h>
6055714Skris#include "cryptlib.h"
6155714Skris#include <openssl/x509.h>
6255714Skris#include <openssl/objects.h>
6355714Skris#include <openssl/evp.h>
64109998Smarkm#include <openssl/ui.h>
6555714Skris
6655714Skris/* should be init to zeros. */
6755714Skrisstatic char prompt_string[80];
6855714Skris
69160814Ssimonvoid EVP_set_pw_prompt(const char *prompt)
70296341Sdelphij{
71296341Sdelphij    if (prompt == NULL)
72296341Sdelphij        prompt_string[0] = '\0';
73296341Sdelphij    else {
74296341Sdelphij        strncpy(prompt_string, prompt, 79);
75296341Sdelphij        prompt_string[79] = '\0';
76296341Sdelphij    }
77296341Sdelphij}
7855714Skris
7955714Skrischar *EVP_get_pw_prompt(void)
80296341Sdelphij{
81296341Sdelphij    if (prompt_string[0] == '\0')
82296341Sdelphij        return (NULL);
83296341Sdelphij    else
84296341Sdelphij        return (prompt_string);
85296341Sdelphij}
8655714Skris
87296341Sdelphij/*
88296341Sdelphij * For historical reasons, the standard function for reading passwords is in
89296341Sdelphij * the DES library -- if someone ever wants to disable DES, this function
90296341Sdelphij * will fail
91296341Sdelphij */
9255714Skrisint EVP_read_pw_string(char *buf, int len, const char *prompt, int verify)
93296341Sdelphij{
94296341Sdelphij    return EVP_read_pw_string_min(buf, 0, len, prompt, verify);
95296341Sdelphij}
96238405Sjkim
97296341Sdelphijint EVP_read_pw_string_min(char *buf, int min, int len, const char *prompt,
98296341Sdelphij                           int verify)
99296341Sdelphij{
100296341Sdelphij    int ret;
101296341Sdelphij    char buff[BUFSIZ];
102296341Sdelphij    UI *ui;
103109998Smarkm
104296341Sdelphij    if ((prompt == NULL) && (prompt_string[0] != '\0'))
105296341Sdelphij        prompt = prompt_string;
106296341Sdelphij    ui = UI_new();
107296341Sdelphij    UI_add_input_string(ui, prompt, 0, buf, min,
108296341Sdelphij                        (len >= BUFSIZ) ? BUFSIZ - 1 : len);
109296341Sdelphij    if (verify)
110296341Sdelphij        UI_add_verify_string(ui, prompt, 0,
111296341Sdelphij                             buff, min, (len >= BUFSIZ) ? BUFSIZ - 1 : len,
112296341Sdelphij                             buf);
113296341Sdelphij    ret = UI_process(ui);
114296341Sdelphij    UI_free(ui);
115296341Sdelphij    OPENSSL_cleanse(buff, BUFSIZ);
116296341Sdelphij    return ret;
117296341Sdelphij}
11855714Skris
119296341Sdelphijint EVP_BytesToKey(const EVP_CIPHER *type, const EVP_MD *md,
120296341Sdelphij                   const unsigned char *salt, const unsigned char *data,
121296341Sdelphij                   int datal, int count, unsigned char *key,
122296341Sdelphij                   unsigned char *iv)
123296341Sdelphij{
124296341Sdelphij    EVP_MD_CTX c;
125296341Sdelphij    unsigned char md_buf[EVP_MAX_MD_SIZE];
126296341Sdelphij    int niv, nkey, addmd = 0;
127296341Sdelphij    unsigned int mds = 0, i;
128296341Sdelphij    int rv = 0;
129296341Sdelphij    nkey = type->key_len;
130296341Sdelphij    niv = type->iv_len;
131296341Sdelphij    OPENSSL_assert(nkey <= EVP_MAX_KEY_LENGTH);
132296341Sdelphij    OPENSSL_assert(niv <= EVP_MAX_IV_LENGTH);
13355714Skris
134296341Sdelphij    if (data == NULL)
135296341Sdelphij        return (nkey);
13655714Skris
137296341Sdelphij    EVP_MD_CTX_init(&c);
138296341Sdelphij    for (;;) {
139296341Sdelphij        if (!EVP_DigestInit_ex(&c, md, NULL))
140296341Sdelphij            return 0;
141296341Sdelphij        if (addmd++)
142296341Sdelphij            if (!EVP_DigestUpdate(&c, &(md_buf[0]), mds))
143296341Sdelphij                goto err;
144296341Sdelphij        if (!EVP_DigestUpdate(&c, data, datal))
145296341Sdelphij            goto err;
146296341Sdelphij        if (salt != NULL)
147296341Sdelphij            if (!EVP_DigestUpdate(&c, salt, PKCS5_SALT_LEN))
148296341Sdelphij                goto err;
149296341Sdelphij        if (!EVP_DigestFinal_ex(&c, &(md_buf[0]), &mds))
150296341Sdelphij            goto err;
15155714Skris
152296341Sdelphij        for (i = 1; i < (unsigned int)count; i++) {
153296341Sdelphij            if (!EVP_DigestInit_ex(&c, md, NULL))
154296341Sdelphij                goto err;
155296341Sdelphij            if (!EVP_DigestUpdate(&c, &(md_buf[0]), mds))
156296341Sdelphij                goto err;
157296341Sdelphij            if (!EVP_DigestFinal_ex(&c, &(md_buf[0]), &mds))
158296341Sdelphij                goto err;
159296341Sdelphij        }
160296341Sdelphij        i = 0;
161296341Sdelphij        if (nkey) {
162296341Sdelphij            for (;;) {
163296341Sdelphij                if (nkey == 0)
164296341Sdelphij                    break;
165296341Sdelphij                if (i == mds)
166296341Sdelphij                    break;
167296341Sdelphij                if (key != NULL)
168296341Sdelphij                    *(key++) = md_buf[i];
169296341Sdelphij                nkey--;
170296341Sdelphij                i++;
171296341Sdelphij            }
172296341Sdelphij        }
173296341Sdelphij        if (niv && (i != mds)) {
174296341Sdelphij            for (;;) {
175296341Sdelphij                if (niv == 0)
176296341Sdelphij                    break;
177296341Sdelphij                if (i == mds)
178296341Sdelphij                    break;
179296341Sdelphij                if (iv != NULL)
180296341Sdelphij                    *(iv++) = md_buf[i];
181296341Sdelphij                niv--;
182296341Sdelphij                i++;
183296341Sdelphij            }
184296341Sdelphij        }
185296341Sdelphij        if ((nkey == 0) && (niv == 0))
186296341Sdelphij            break;
187296341Sdelphij    }
188296341Sdelphij    rv = type->key_len;
189296341Sdelphij err:
190296341Sdelphij    EVP_MD_CTX_cleanup(&c);
191296341Sdelphij    OPENSSL_cleanse(&(md_buf[0]), EVP_MAX_MD_SIZE);
192296341Sdelphij    return rv;
193296341Sdelphij}
194