155714Skris/* p5_crpt.c */ 2296341Sdelphij/* 3296341Sdelphij * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project 4296341Sdelphij * 1999. 555714Skris */ 655714Skris/* ==================================================================== 755714Skris * Copyright (c) 1999 The OpenSSL Project. All rights reserved. 855714Skris * 955714Skris * Redistribution and use in source and binary forms, with or without 1055714Skris * modification, are permitted provided that the following conditions 1155714Skris * are met: 1255714Skris * 1355714Skris * 1. Redistributions of source code must retain the above copyright 14296341Sdelphij * notice, this list of conditions and the following disclaimer. 1555714Skris * 1655714Skris * 2. Redistributions in binary form must reproduce the above copyright 1755714Skris * notice, this list of conditions and the following disclaimer in 1855714Skris * the documentation and/or other materials provided with the 1955714Skris * distribution. 2055714Skris * 2155714Skris * 3. All advertising materials mentioning features or use of this 2255714Skris * software must display the following acknowledgment: 2355714Skris * "This product includes software developed by the OpenSSL Project 2455714Skris * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 2555714Skris * 2655714Skris * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 2755714Skris * endorse or promote products derived from this software without 2855714Skris * prior written permission. For written permission, please contact 2955714Skris * licensing@OpenSSL.org. 3055714Skris * 3155714Skris * 5. Products derived from this software may not be called "OpenSSL" 3255714Skris * nor may "OpenSSL" appear in their names without prior written 3355714Skris * permission of the OpenSSL Project. 3455714Skris * 3555714Skris * 6. Redistributions of any form whatsoever must retain the following 3655714Skris * acknowledgment: 3755714Skris * "This product includes software developed by the OpenSSL Project 3855714Skris * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 3955714Skris * 4055714Skris * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 4155714Skris * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4255714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 4355714Skris * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 4455714Skris * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 4555714Skris * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 4655714Skris * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 4755714Skris * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 4955714Skris * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 5055714Skris * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 5155714Skris * OF THE POSSIBILITY OF SUCH DAMAGE. 5255714Skris * ==================================================================== 5355714Skris * 5455714Skris * This product includes cryptographic software written by Eric Young 5555714Skris * (eay@cryptsoft.com). This product includes software written by Tim 5655714Skris * Hudson (tjh@cryptsoft.com). 5755714Skris * 5855714Skris */ 5955714Skris 6055714Skris#include <stdio.h> 6155714Skris#include <stdlib.h> 62109998Smarkm#include "cryptlib.h" 6355714Skris#include <openssl/x509.h> 6455714Skris#include <openssl/evp.h> 6555714Skris 66296341Sdelphij/* 67296341Sdelphij * Doesn't do anything now: Builtin PBE algorithms in static table. 6855714Skris */ 6955714Skris 7055714Skrisvoid PKCS5_PBE_add(void) 7155714Skris{ 7255714Skris} 7355714Skris 7455714Skrisint PKCS5_PBE_keyivgen(EVP_CIPHER_CTX *cctx, const char *pass, int passlen, 75296341Sdelphij ASN1_TYPE *param, const EVP_CIPHER *cipher, 76296341Sdelphij const EVP_MD *md, int en_de) 7755714Skris{ 78296341Sdelphij EVP_MD_CTX ctx; 79296341Sdelphij unsigned char md_tmp[EVP_MAX_MD_SIZE]; 80296341Sdelphij unsigned char key[EVP_MAX_KEY_LENGTH], iv[EVP_MAX_IV_LENGTH]; 81296341Sdelphij int i; 82296341Sdelphij PBEPARAM *pbe; 83296341Sdelphij int saltlen, iter; 84296341Sdelphij unsigned char *salt; 85296341Sdelphij const unsigned char *pbuf; 86296341Sdelphij int mdsize; 87296341Sdelphij int rv = 0; 88296341Sdelphij EVP_MD_CTX_init(&ctx); 8955714Skris 90296341Sdelphij /* Extract useful info from parameter */ 91296341Sdelphij if (param == NULL || param->type != V_ASN1_SEQUENCE || 92296341Sdelphij param->value.sequence == NULL) { 93296341Sdelphij EVPerr(EVP_F_PKCS5_PBE_KEYIVGEN, EVP_R_DECODE_ERROR); 94296341Sdelphij return 0; 95296341Sdelphij } 96160814Ssimon 97296341Sdelphij pbuf = param->value.sequence->data; 98296341Sdelphij if (!(pbe = d2i_PBEPARAM(NULL, &pbuf, param->value.sequence->length))) { 99296341Sdelphij EVPerr(EVP_F_PKCS5_PBE_KEYIVGEN, EVP_R_DECODE_ERROR); 100296341Sdelphij return 0; 101296341Sdelphij } 10255714Skris 103296341Sdelphij if (!pbe->iter) 104296341Sdelphij iter = 1; 105296341Sdelphij else 106296341Sdelphij iter = ASN1_INTEGER_get(pbe->iter); 107296341Sdelphij salt = pbe->salt->data; 108296341Sdelphij saltlen = pbe->salt->length; 10955714Skris 110296341Sdelphij if (!pass) 111296341Sdelphij passlen = 0; 112296341Sdelphij else if (passlen == -1) 113296341Sdelphij passlen = strlen(pass); 11468651Skris 115296341Sdelphij if (!EVP_DigestInit_ex(&ctx, md, NULL)) 116296341Sdelphij goto err; 117296341Sdelphij if (!EVP_DigestUpdate(&ctx, pass, passlen)) 118296341Sdelphij goto err; 119296341Sdelphij if (!EVP_DigestUpdate(&ctx, salt, saltlen)) 120296341Sdelphij goto err; 121296341Sdelphij PBEPARAM_free(pbe); 122296341Sdelphij if (!EVP_DigestFinal_ex(&ctx, md_tmp, NULL)) 123296341Sdelphij goto err; 124296341Sdelphij mdsize = EVP_MD_size(md); 125296341Sdelphij if (mdsize < 0) 126296341Sdelphij return 0; 127296341Sdelphij for (i = 1; i < iter; i++) { 128296341Sdelphij if (!EVP_DigestInit_ex(&ctx, md, NULL)) 129296341Sdelphij goto err; 130296341Sdelphij if (!EVP_DigestUpdate(&ctx, md_tmp, mdsize)) 131296341Sdelphij goto err; 132296341Sdelphij if (!EVP_DigestFinal_ex(&ctx, md_tmp, NULL)) 133296341Sdelphij goto err; 134296341Sdelphij } 135296341Sdelphij OPENSSL_assert(EVP_CIPHER_key_length(cipher) <= (int)sizeof(md_tmp)); 136296341Sdelphij memcpy(key, md_tmp, EVP_CIPHER_key_length(cipher)); 137296341Sdelphij OPENSSL_assert(EVP_CIPHER_iv_length(cipher) <= 16); 138296341Sdelphij memcpy(iv, md_tmp + (16 - EVP_CIPHER_iv_length(cipher)), 139296341Sdelphij EVP_CIPHER_iv_length(cipher)); 140296341Sdelphij if (!EVP_CipherInit_ex(cctx, cipher, NULL, key, iv, en_de)) 141296341Sdelphij goto err; 142296341Sdelphij OPENSSL_cleanse(md_tmp, EVP_MAX_MD_SIZE); 143296341Sdelphij OPENSSL_cleanse(key, EVP_MAX_KEY_LENGTH); 144296341Sdelphij OPENSSL_cleanse(iv, EVP_MAX_IV_LENGTH); 145296341Sdelphij rv = 1; 146296341Sdelphij err: 147296341Sdelphij EVP_MD_CTX_cleanup(&ctx); 148296341Sdelphij return rv; 14955714Skris} 150