evp_key.c revision 296341
1251018Sgonzo/* crypto/evp/evp_key.c */ 2251018Sgonzo/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 3251018Sgonzo * All rights reserved. 4251018Sgonzo * 5251018Sgonzo * This package is an SSL implementation written 6251018Sgonzo * by Eric Young (eay@cryptsoft.com). 7251018Sgonzo * The implementation was written so as to conform with Netscapes SSL. 8251018Sgonzo * 9251018Sgonzo * This library is free for commercial and non-commercial use as long as 10251018Sgonzo * the following conditions are aheared to. The following conditions 11251018Sgonzo * apply to all code found in this distribution, be it the RC4, RSA, 12251018Sgonzo * lhash, DES, etc., code; not just the SSL code. The SSL documentation 13251018Sgonzo * included with this distribution is covered by the same copyright terms 14251018Sgonzo * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15251018Sgonzo * 16251018Sgonzo * Copyright remains Eric Young's, and as such any Copyright notices in 17251018Sgonzo * the code are not to be removed. 18251018Sgonzo * If this package is used in a product, Eric Young should be given attribution 19251018Sgonzo * as the author of the parts of the library used. 20251018Sgonzo * This can be in the form of a textual message at program startup or 21251018Sgonzo * in documentation (online or textual) provided with the package. 22251018Sgonzo * 23251018Sgonzo * Redistribution and use in source and binary forms, with or without 24251018Sgonzo * modification, are permitted provided that the following conditions 25251018Sgonzo * are met: 26251018Sgonzo * 1. Redistributions of source code must retain the copyright 27251018Sgonzo * notice, this list of conditions and the following disclaimer. 28251018Sgonzo * 2. Redistributions in binary form must reproduce the above copyright 29251018Sgonzo * notice, this list of conditions and the following disclaimer in the 30251018Sgonzo * documentation and/or other materials provided with the distribution. 31251018Sgonzo * 3. All advertising materials mentioning features or use of this software 32251018Sgonzo * must display the following acknowledgement: 33251018Sgonzo * "This product includes cryptographic software written by 34251018Sgonzo * Eric Young (eay@cryptsoft.com)" 35251018Sgonzo * The word 'cryptographic' can be left out if the rouines from the library 36251018Sgonzo * being used are not cryptographic related :-). 37251018Sgonzo * 4. If you include any Windows specific code (or a derivative thereof) from 38251018Sgonzo * the apps directory (application code) you must include an acknowledgement: 39251018Sgonzo * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40251018Sgonzo * 41251018Sgonzo * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 42251018Sgonzo * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43251018Sgonzo * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44251018Sgonzo * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45251018Sgonzo * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46251018Sgonzo * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47251018Sgonzo * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48251018Sgonzo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49251018Sgonzo * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50251018Sgonzo * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51251018Sgonzo * SUCH DAMAGE. 52251018Sgonzo * 53251018Sgonzo * The licence and distribution terms for any publically available version or 54251018Sgonzo * derivative of this code cannot be changed. i.e. this code cannot simply be 55251018Sgonzo * copied and put under another distribution licence 56251018Sgonzo * [including the GNU Public Licence.] 57251018Sgonzo */ 58251018Sgonzo 59251018Sgonzo#include <stdio.h> 60251018Sgonzo#include "cryptlib.h" 61251018Sgonzo#include <openssl/x509.h> 62251018Sgonzo#include <openssl/objects.h> 63251018Sgonzo#include <openssl/evp.h> 64251018Sgonzo#include <openssl/ui.h> 65251018Sgonzo 66251018Sgonzo/* should be init to zeros. */ 67251018Sgonzostatic char prompt_string[80]; 68251018Sgonzo 69251018Sgonzovoid EVP_set_pw_prompt(const char *prompt) 70251018Sgonzo{ 71251018Sgonzo if (prompt == NULL) 72251018Sgonzo prompt_string[0] = '\0'; 73251018Sgonzo else { 74251018Sgonzo strncpy(prompt_string, prompt, 79); 75251018Sgonzo prompt_string[79] = '\0'; 76251018Sgonzo } 77251018Sgonzo} 78251018Sgonzo 79251018Sgonzochar *EVP_get_pw_prompt(void) 80251018Sgonzo{ 81251018Sgonzo if (prompt_string[0] == '\0') 82251018Sgonzo return (NULL); 83251018Sgonzo else 84251018Sgonzo return (prompt_string); 85251018Sgonzo} 86251018Sgonzo 87251018Sgonzo/* 88251018Sgonzo * For historical reasons, the standard function for reading passwords is in 89251018Sgonzo * the DES library -- if someone ever wants to disable DES, this function 90251018Sgonzo * will fail 91251018Sgonzo */ 92251018Sgonzoint EVP_read_pw_string(char *buf, int len, const char *prompt, int verify) 93251018Sgonzo{ 94251018Sgonzo return EVP_read_pw_string_min(buf, 0, len, prompt, verify); 95251018Sgonzo} 96251018Sgonzo 97251018Sgonzoint EVP_read_pw_string_min(char *buf, int min, int len, const char *prompt, 98251018Sgonzo int verify) 99251018Sgonzo{ 100251018Sgonzo int ret; 101251018Sgonzo char buff[BUFSIZ]; 102251018Sgonzo UI *ui; 103251018Sgonzo 104251018Sgonzo if ((prompt == NULL) && (prompt_string[0] != '\0')) 105251018Sgonzo prompt = prompt_string; 106251018Sgonzo ui = UI_new(); 107251018Sgonzo UI_add_input_string(ui, prompt, 0, buf, min, 108251018Sgonzo (len >= BUFSIZ) ? BUFSIZ - 1 : len); 109251018Sgonzo if (verify) 110251018Sgonzo UI_add_verify_string(ui, prompt, 0, 111251018Sgonzo buff, min, (len >= BUFSIZ) ? BUFSIZ - 1 : len, 112251018Sgonzo buf); 113251018Sgonzo ret = UI_process(ui); 114251018Sgonzo UI_free(ui); 115251018Sgonzo OPENSSL_cleanse(buff, BUFSIZ); 116251018Sgonzo return ret; 117251018Sgonzo} 118251018Sgonzo 119251018Sgonzoint EVP_BytesToKey(const EVP_CIPHER *type, const EVP_MD *md, 120251018Sgonzo const unsigned char *salt, const unsigned char *data, 121251018Sgonzo int datal, int count, unsigned char *key, 122251018Sgonzo unsigned char *iv) 123251018Sgonzo{ 124251018Sgonzo EVP_MD_CTX c; 125251018Sgonzo unsigned char md_buf[EVP_MAX_MD_SIZE]; 126251018Sgonzo int niv, nkey, addmd = 0; 127251018Sgonzo unsigned int mds = 0, i; 128251018Sgonzo int rv = 0; 129251018Sgonzo nkey = type->key_len; 130251018Sgonzo niv = type->iv_len; 131251018Sgonzo OPENSSL_assert(nkey <= EVP_MAX_KEY_LENGTH); 132251018Sgonzo OPENSSL_assert(niv <= EVP_MAX_IV_LENGTH); 133251018Sgonzo 134251018Sgonzo if (data == NULL) 135251018Sgonzo return (nkey); 136251018Sgonzo 137251018Sgonzo EVP_MD_CTX_init(&c); 138251018Sgonzo for (;;) { 139251018Sgonzo if (!EVP_DigestInit_ex(&c, md, NULL)) 140251018Sgonzo return 0; 141251018Sgonzo if (addmd++) 142251018Sgonzo if (!EVP_DigestUpdate(&c, &(md_buf[0]), mds)) 143251018Sgonzo goto err; 144251018Sgonzo if (!EVP_DigestUpdate(&c, data, datal)) 145251018Sgonzo goto err; 146251018Sgonzo if (salt != NULL) 147251018Sgonzo if (!EVP_DigestUpdate(&c, salt, PKCS5_SALT_LEN)) 148251018Sgonzo goto err; 149251018Sgonzo if (!EVP_DigestFinal_ex(&c, &(md_buf[0]), &mds)) 150251018Sgonzo goto err; 151251018Sgonzo 152251018Sgonzo for (i = 1; i < (unsigned int)count; i++) { 153251018Sgonzo if (!EVP_DigestInit_ex(&c, md, NULL)) 154251018Sgonzo goto err; 155251018Sgonzo if (!EVP_DigestUpdate(&c, &(md_buf[0]), mds)) 156251018Sgonzo goto err; 157251018Sgonzo if (!EVP_DigestFinal_ex(&c, &(md_buf[0]), &mds)) 158251018Sgonzo goto err; 159251018Sgonzo } 160251018Sgonzo i = 0; 161251018Sgonzo if (nkey) { 162251018Sgonzo for (;;) { 163251018Sgonzo if (nkey == 0) 164251018Sgonzo break; 165251018Sgonzo if (i == mds) 166251018Sgonzo break; 167251018Sgonzo if (key != NULL) 168251018Sgonzo *(key++) = md_buf[i]; 169251018Sgonzo nkey--; 170251018Sgonzo i++; 171251018Sgonzo } 172251018Sgonzo } 173251018Sgonzo if (niv && (i != mds)) { 174251018Sgonzo for (;;) { 175251018Sgonzo if (niv == 0) 176251018Sgonzo break; 177251018Sgonzo if (i == mds) 178251018Sgonzo break; 179251018Sgonzo if (iv != NULL) 180251018Sgonzo *(iv++) = md_buf[i]; 181251018Sgonzo niv--; 182251018Sgonzo i++; 183251018Sgonzo } 184251018Sgonzo } 185251018Sgonzo if ((nkey == 0) && (niv == 0)) 186251018Sgonzo break; 187251018Sgonzo } 188251018Sgonzo rv = type->key_len; 189251018Sgonzo err: 190251018Sgonzo EVP_MD_CTX_cleanup(&c); 191251018Sgonzo OPENSSL_cleanse(&(md_buf[0]), EVP_MAX_MD_SIZE); 192251018Sgonzo return rv; 193251018Sgonzo} 194251018Sgonzo