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.
8280304Sjkim *
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).
15280304Sjkim *
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.
22280304Sjkim *
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 :-).
37280304Sjkim * 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)"
40280304Sjkim *
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.
52280304Sjkim *
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)
70280304Sjkim{
71280304Sjkim    if (prompt == NULL)
72280304Sjkim        prompt_string[0] = '\0';
73280304Sjkim    else {
74280304Sjkim        strncpy(prompt_string, prompt, 79);
75280304Sjkim        prompt_string[79] = '\0';
76280304Sjkim    }
77280304Sjkim}
7855714Skris
7955714Skrischar *EVP_get_pw_prompt(void)
80280304Sjkim{
81280304Sjkim    if (prompt_string[0] == '\0')
82280304Sjkim        return (NULL);
83280304Sjkim    else
84280304Sjkim        return (prompt_string);
85280304Sjkim}
8655714Skris
87280304Sjkim/*
88280304Sjkim * For historical reasons, the standard function for reading passwords is in
89280304Sjkim * the DES library -- if someone ever wants to disable DES, this function
90280304Sjkim * will fail
91280304Sjkim */
9255714Skrisint EVP_read_pw_string(char *buf, int len, const char *prompt, int verify)
93280304Sjkim{
94280304Sjkim    return EVP_read_pw_string_min(buf, 0, len, prompt, verify);
95280304Sjkim}
96238405Sjkim
97280304Sjkimint EVP_read_pw_string_min(char *buf, int min, int len, const char *prompt,
98280304Sjkim                           int verify)
99280304Sjkim{
100280304Sjkim    int ret;
101280304Sjkim    char buff[BUFSIZ];
102280304Sjkim    UI *ui;
103109998Smarkm
104280304Sjkim    if ((prompt == NULL) && (prompt_string[0] != '\0'))
105280304Sjkim        prompt = prompt_string;
106280304Sjkim    ui = UI_new();
107291721Sjkim    if (ui == NULL)
108291721Sjkim        return -1;
109280304Sjkim    UI_add_input_string(ui, prompt, 0, buf, min,
110280304Sjkim                        (len >= BUFSIZ) ? BUFSIZ - 1 : len);
111280304Sjkim    if (verify)
112280304Sjkim        UI_add_verify_string(ui, prompt, 0,
113280304Sjkim                             buff, min, (len >= BUFSIZ) ? BUFSIZ - 1 : len,
114280304Sjkim                             buf);
115280304Sjkim    ret = UI_process(ui);
116280304Sjkim    UI_free(ui);
117280304Sjkim    OPENSSL_cleanse(buff, BUFSIZ);
118280304Sjkim    return ret;
119280304Sjkim}
12055714Skris
121280304Sjkimint EVP_BytesToKey(const EVP_CIPHER *type, const EVP_MD *md,
122280304Sjkim                   const unsigned char *salt, const unsigned char *data,
123280304Sjkim                   int datal, int count, unsigned char *key,
124280304Sjkim                   unsigned char *iv)
125280304Sjkim{
126280304Sjkim    EVP_MD_CTX c;
127280304Sjkim    unsigned char md_buf[EVP_MAX_MD_SIZE];
128280304Sjkim    int niv, nkey, addmd = 0;
129280304Sjkim    unsigned int mds = 0, i;
130280304Sjkim    int rv = 0;
131280304Sjkim    nkey = type->key_len;
132280304Sjkim    niv = type->iv_len;
133280304Sjkim    OPENSSL_assert(nkey <= EVP_MAX_KEY_LENGTH);
134280304Sjkim    OPENSSL_assert(niv <= EVP_MAX_IV_LENGTH);
13555714Skris
136280304Sjkim    if (data == NULL)
137280304Sjkim        return (nkey);
13855714Skris
139280304Sjkim    EVP_MD_CTX_init(&c);
140280304Sjkim    for (;;) {
141280304Sjkim        if (!EVP_DigestInit_ex(&c, md, NULL))
142291721Sjkim            goto err;
143280304Sjkim        if (addmd++)
144280304Sjkim            if (!EVP_DigestUpdate(&c, &(md_buf[0]), mds))
145280304Sjkim                goto err;
146280304Sjkim        if (!EVP_DigestUpdate(&c, data, datal))
147280304Sjkim            goto err;
148280304Sjkim        if (salt != NULL)
149280304Sjkim            if (!EVP_DigestUpdate(&c, salt, PKCS5_SALT_LEN))
150280304Sjkim                goto err;
151280304Sjkim        if (!EVP_DigestFinal_ex(&c, &(md_buf[0]), &mds))
152280304Sjkim            goto err;
15355714Skris
154280304Sjkim        for (i = 1; i < (unsigned int)count; i++) {
155280304Sjkim            if (!EVP_DigestInit_ex(&c, md, NULL))
156280304Sjkim                goto err;
157280304Sjkim            if (!EVP_DigestUpdate(&c, &(md_buf[0]), mds))
158280304Sjkim                goto err;
159280304Sjkim            if (!EVP_DigestFinal_ex(&c, &(md_buf[0]), &mds))
160280304Sjkim                goto err;
161280304Sjkim        }
162280304Sjkim        i = 0;
163280304Sjkim        if (nkey) {
164280304Sjkim            for (;;) {
165280304Sjkim                if (nkey == 0)
166280304Sjkim                    break;
167280304Sjkim                if (i == mds)
168280304Sjkim                    break;
169280304Sjkim                if (key != NULL)
170280304Sjkim                    *(key++) = md_buf[i];
171280304Sjkim                nkey--;
172280304Sjkim                i++;
173280304Sjkim            }
174280304Sjkim        }
175280304Sjkim        if (niv && (i != mds)) {
176280304Sjkim            for (;;) {
177280304Sjkim                if (niv == 0)
178280304Sjkim                    break;
179280304Sjkim                if (i == mds)
180280304Sjkim                    break;
181280304Sjkim                if (iv != NULL)
182280304Sjkim                    *(iv++) = md_buf[i];
183280304Sjkim                niv--;
184280304Sjkim                i++;
185280304Sjkim            }
186280304Sjkim        }
187280304Sjkim        if ((nkey == 0) && (niv == 0))
188280304Sjkim            break;
189280304Sjkim    }
190280304Sjkim    rv = type->key_len;
191280304Sjkim err:
192280304Sjkim    EVP_MD_CTX_cleanup(&c);
193291721Sjkim    OPENSSL_cleanse(md_buf, sizeof(md_buf));
194280304Sjkim    return rv;
195280304Sjkim}
196