155714Skris/* p5_crpt.c */ 2194206Ssimon/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL 355714Skris * project 1999. 455714Skris */ 555714Skris/* ==================================================================== 655714Skris * Copyright (c) 1999 The OpenSSL Project. All rights reserved. 755714Skris * 855714Skris * Redistribution and use in source and binary forms, with or without 955714Skris * modification, are permitted provided that the following conditions 1055714Skris * are met: 1155714Skris * 1255714Skris * 1. Redistributions of source code must retain the above copyright 1355714Skris * notice, this list of conditions and the following disclaimer. 1455714Skris * 1555714Skris * 2. Redistributions in binary form must reproduce the above copyright 1655714Skris * notice, this list of conditions and the following disclaimer in 1755714Skris * the documentation and/or other materials provided with the 1855714Skris * distribution. 1955714Skris * 2055714Skris * 3. All advertising materials mentioning features or use of this 2155714Skris * software must display the following acknowledgment: 2255714Skris * "This product includes software developed by the OpenSSL Project 2355714Skris * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 2455714Skris * 2555714Skris * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 2655714Skris * endorse or promote products derived from this software without 2755714Skris * prior written permission. For written permission, please contact 2855714Skris * licensing@OpenSSL.org. 2955714Skris * 3055714Skris * 5. Products derived from this software may not be called "OpenSSL" 3155714Skris * nor may "OpenSSL" appear in their names without prior written 3255714Skris * permission of the OpenSSL Project. 3355714Skris * 3455714Skris * 6. Redistributions of any form whatsoever must retain the following 3555714Skris * acknowledgment: 3655714Skris * "This product includes software developed by the OpenSSL Project 3755714Skris * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 3855714Skris * 3955714Skris * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 4055714Skris * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4155714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 4255714Skris * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 4355714Skris * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 4455714Skris * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 4555714Skris * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 4655714Skris * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4755714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 4855714Skris * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 4955714Skris * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 5055714Skris * OF THE POSSIBILITY OF SUCH DAMAGE. 5155714Skris * ==================================================================== 5255714Skris * 5355714Skris * This product includes cryptographic software written by Eric Young 5455714Skris * (eay@cryptsoft.com). This product includes software written by Tim 5555714Skris * Hudson (tjh@cryptsoft.com). 5655714Skris * 5755714Skris */ 5855714Skris 5955714Skris#include <stdio.h> 6055714Skris#include <stdlib.h> 61109998Smarkm#include "cryptlib.h" 6255714Skris#include <openssl/x509.h> 6355714Skris#include <openssl/evp.h> 6455714Skris 65238405Sjkim/* Doesn't do anything now: Builtin PBE algorithms in static table. 6655714Skris */ 6755714Skris 6855714Skrisvoid PKCS5_PBE_add(void) 6955714Skris{ 7055714Skris} 7155714Skris 7255714Skrisint PKCS5_PBE_keyivgen(EVP_CIPHER_CTX *cctx, const char *pass, int passlen, 73109998Smarkm ASN1_TYPE *param, const EVP_CIPHER *cipher, const EVP_MD *md, 7455714Skris int en_de) 7555714Skris{ 7655714Skris EVP_MD_CTX ctx; 7755714Skris unsigned char md_tmp[EVP_MAX_MD_SIZE]; 7855714Skris unsigned char key[EVP_MAX_KEY_LENGTH], iv[EVP_MAX_IV_LENGTH]; 7955714Skris int i; 8055714Skris PBEPARAM *pbe; 8155714Skris int saltlen, iter; 82160814Ssimon unsigned char *salt; 83160814Ssimon const unsigned char *pbuf; 84238405Sjkim int mdsize; 85238405Sjkim int rv = 0; 86238405Sjkim EVP_MD_CTX_init(&ctx); 8755714Skris 8855714Skris /* Extract useful info from parameter */ 89160814Ssimon if (param == NULL || param->type != V_ASN1_SEQUENCE || 90160814Ssimon param->value.sequence == NULL) { 91160814Ssimon EVPerr(EVP_F_PKCS5_PBE_KEYIVGEN,EVP_R_DECODE_ERROR); 92160814Ssimon return 0; 93160814Ssimon } 94160814Ssimon 9555714Skris pbuf = param->value.sequence->data; 96160814Ssimon if (!(pbe = d2i_PBEPARAM(NULL, &pbuf, param->value.sequence->length))) { 9755714Skris EVPerr(EVP_F_PKCS5_PBE_KEYIVGEN,EVP_R_DECODE_ERROR); 9855714Skris return 0; 9955714Skris } 10055714Skris 10155714Skris if (!pbe->iter) iter = 1; 10255714Skris else iter = ASN1_INTEGER_get (pbe->iter); 10355714Skris salt = pbe->salt->data; 10455714Skris saltlen = pbe->salt->length; 10555714Skris 10668651Skris if(!pass) passlen = 0; 10768651Skris else if(passlen == -1) passlen = strlen(pass); 10868651Skris 109238405Sjkim if (!EVP_DigestInit_ex(&ctx, md, NULL)) 110238405Sjkim goto err; 111238405Sjkim if (!EVP_DigestUpdate(&ctx, pass, passlen)) 112238405Sjkim goto err; 113238405Sjkim if (!EVP_DigestUpdate(&ctx, salt, saltlen)) 114238405Sjkim goto err; 11555714Skris PBEPARAM_free(pbe); 116238405Sjkim if (!EVP_DigestFinal_ex(&ctx, md_tmp, NULL)) 117238405Sjkim goto err; 118238405Sjkim mdsize = EVP_MD_size(md); 119238405Sjkim if (mdsize < 0) 120238405Sjkim return 0; 12155714Skris for (i = 1; i < iter; i++) { 122238405Sjkim if (!EVP_DigestInit_ex(&ctx, md, NULL)) 123238405Sjkim goto err; 124238405Sjkim if (!EVP_DigestUpdate(&ctx, md_tmp, mdsize)) 125238405Sjkim goto err; 126238405Sjkim if (!EVP_DigestFinal_ex (&ctx, md_tmp, NULL)) 127238405Sjkim goto err; 12855714Skris } 129160814Ssimon OPENSSL_assert(EVP_CIPHER_key_length(cipher) <= (int)sizeof(md_tmp)); 130109998Smarkm memcpy(key, md_tmp, EVP_CIPHER_key_length(cipher)); 131109998Smarkm OPENSSL_assert(EVP_CIPHER_iv_length(cipher) <= 16); 132109998Smarkm memcpy(iv, md_tmp + (16 - EVP_CIPHER_iv_length(cipher)), 13355714Skris EVP_CIPHER_iv_length(cipher)); 134238405Sjkim if (!EVP_CipherInit_ex(cctx, cipher, NULL, key, iv, en_de)) 135238405Sjkim goto err; 136109998Smarkm OPENSSL_cleanse(md_tmp, EVP_MAX_MD_SIZE); 137109998Smarkm OPENSSL_cleanse(key, EVP_MAX_KEY_LENGTH); 138109998Smarkm OPENSSL_cleanse(iv, EVP_MAX_IV_LENGTH); 139238405Sjkim rv = 1; 140238405Sjkim err: 141238405Sjkim EVP_MD_CTX_cleanup(&ctx); 142238405Sjkim return rv; 14355714Skris} 144