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. 855714Skris * 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). 1555714Skris * 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. 2255714Skris * 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 :-). 3755714Skris * 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)" 4055714Skris * 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. 5255714Skris * 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) 7055714Skris { 7155714Skris if (prompt == NULL) 7255714Skris prompt_string[0]='\0'; 7355714Skris else 74109998Smarkm { 7555714Skris strncpy(prompt_string,prompt,79); 76109998Smarkm prompt_string[79]='\0'; 77109998Smarkm } 7855714Skris } 7955714Skris 8055714Skrischar *EVP_get_pw_prompt(void) 8155714Skris { 8255714Skris if (prompt_string[0] == '\0') 8355714Skris return(NULL); 8455714Skris else 8555714Skris return(prompt_string); 8655714Skris } 8755714Skris 8859191Skris/* For historical reasons, the standard function for reading passwords is 8959191Skris * in the DES library -- if someone ever wants to disable DES, 9059191Skris * this function will fail */ 9155714Skrisint EVP_read_pw_string(char *buf, int len, const char *prompt, int verify) 9255714Skris { 93238405Sjkim return EVP_read_pw_string_min(buf, 0, len, prompt, verify); 94238405Sjkim } 95238405Sjkim 96238405Sjkimint EVP_read_pw_string_min(char *buf, int min, int len, const char *prompt, int verify) 97238405Sjkim { 98109998Smarkm int ret; 99109998Smarkm char buff[BUFSIZ]; 100109998Smarkm UI *ui; 101109998Smarkm 10255714Skris if ((prompt == NULL) && (prompt_string[0] != '\0')) 10355714Skris prompt=prompt_string; 104109998Smarkm ui = UI_new(); 105238405Sjkim UI_add_input_string(ui,prompt,0,buf,min,(len>=BUFSIZ)?BUFSIZ-1:len); 106109998Smarkm if (verify) 107109998Smarkm UI_add_verify_string(ui,prompt,0, 108238405Sjkim buff,min,(len>=BUFSIZ)?BUFSIZ-1:len,buf); 109109998Smarkm ret = UI_process(ui); 110109998Smarkm UI_free(ui); 111109998Smarkm OPENSSL_cleanse(buff,BUFSIZ); 112109998Smarkm return ret; 11355714Skris } 11455714Skris 115109998Smarkmint EVP_BytesToKey(const EVP_CIPHER *type, const EVP_MD *md, 116109998Smarkm const unsigned char *salt, const unsigned char *data, int datal, 117109998Smarkm int count, unsigned char *key, unsigned char *iv) 11855714Skris { 11955714Skris EVP_MD_CTX c; 12055714Skris unsigned char md_buf[EVP_MAX_MD_SIZE]; 12155714Skris int niv,nkey,addmd=0; 12255714Skris unsigned int mds=0,i; 123238405Sjkim int rv = 0; 12455714Skris nkey=type->key_len; 12555714Skris niv=type->iv_len; 126109998Smarkm OPENSSL_assert(nkey <= EVP_MAX_KEY_LENGTH); 127109998Smarkm OPENSSL_assert(niv <= EVP_MAX_IV_LENGTH); 12855714Skris 12955714Skris if (data == NULL) return(nkey); 13055714Skris 131109998Smarkm EVP_MD_CTX_init(&c); 13255714Skris for (;;) 13355714Skris { 134160814Ssimon if (!EVP_DigestInit_ex(&c,md, NULL)) 135160814Ssimon return 0; 13655714Skris if (addmd++) 137238405Sjkim if (!EVP_DigestUpdate(&c,&(md_buf[0]),mds)) 138238405Sjkim goto err; 139238405Sjkim if (!EVP_DigestUpdate(&c,data,datal)) 140238405Sjkim goto err; 14155714Skris if (salt != NULL) 142238405Sjkim if (!EVP_DigestUpdate(&c,salt,PKCS5_SALT_LEN)) 143238405Sjkim goto err; 144238405Sjkim if (!EVP_DigestFinal_ex(&c,&(md_buf[0]),&mds)) 145238405Sjkim goto err; 14655714Skris 14755714Skris for (i=1; i<(unsigned int)count; i++) 14855714Skris { 149238405Sjkim if (!EVP_DigestInit_ex(&c,md, NULL)) 150238405Sjkim goto err; 151238405Sjkim if (!EVP_DigestUpdate(&c,&(md_buf[0]),mds)) 152238405Sjkim goto err; 153238405Sjkim if (!EVP_DigestFinal_ex(&c,&(md_buf[0]),&mds)) 154238405Sjkim goto err; 15555714Skris } 15655714Skris i=0; 15755714Skris if (nkey) 15855714Skris { 15955714Skris for (;;) 16055714Skris { 16155714Skris if (nkey == 0) break; 16255714Skris if (i == mds) break; 16355714Skris if (key != NULL) 16455714Skris *(key++)=md_buf[i]; 16555714Skris nkey--; 16655714Skris i++; 16755714Skris } 16855714Skris } 16955714Skris if (niv && (i != mds)) 17055714Skris { 17155714Skris for (;;) 17255714Skris { 17355714Skris if (niv == 0) break; 17455714Skris if (i == mds) break; 17555714Skris if (iv != NULL) 17655714Skris *(iv++)=md_buf[i]; 17755714Skris niv--; 17855714Skris i++; 17955714Skris } 18055714Skris } 18155714Skris if ((nkey == 0) && (niv == 0)) break; 18255714Skris } 183238405Sjkim rv = type->key_len; 184238405Sjkim err: 185109998Smarkm EVP_MD_CTX_cleanup(&c); 186109998Smarkm OPENSSL_cleanse(&(md_buf[0]),EVP_MAX_MD_SIZE); 187238405Sjkim return rv; 18855714Skris } 18955714Skris 190