sl@0: /* p5_crpt2.c */ sl@0: /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL sl@0: * project 1999. sl@0: */ sl@0: /* ==================================================================== sl@0: * Copyright (c) 1999 The OpenSSL Project. All rights reserved. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * sl@0: * 1. Redistributions of source code must retain the above copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in sl@0: * the documentation and/or other materials provided with the sl@0: * distribution. sl@0: * sl@0: * 3. All advertising materials mentioning features or use of this sl@0: * software must display the following acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" sl@0: * sl@0: * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to sl@0: * endorse or promote products derived from this software without sl@0: * prior written permission. For written permission, please contact sl@0: * licensing@OpenSSL.org. sl@0: * sl@0: * 5. Products derived from this software may not be called "OpenSSL" sl@0: * nor may "OpenSSL" appear in their names without prior written sl@0: * permission of the OpenSSL Project. sl@0: * sl@0: * 6. Redistributions of any form whatsoever must retain the following sl@0: * acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY sl@0: * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR sl@0: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR sl@0: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, sl@0: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT sl@0: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; sl@0: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, sl@0: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) sl@0: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED sl@0: * OF THE POSSIBILITY OF SUCH DAMAGE. sl@0: * ==================================================================== sl@0: * sl@0: * This product includes cryptographic software written by Eric Young sl@0: * (eay@cryptsoft.com). This product includes software written by Tim sl@0: * Hudson (tjh@cryptsoft.com). sl@0: * sl@0: */ sl@0: #include sl@0: #include sl@0: #include "cryptlib.h" sl@0: #if !defined(OPENSSL_NO_HMAC) && !defined(OPENSSL_NO_SHA) sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: /* set this to print out info about the keygen algorithm */ sl@0: /* #define DEBUG_PKCS5V2 */ sl@0: sl@0: #ifdef DEBUG_PKCS5V2 sl@0: static void h__dump (const unsigned char *p, int len); sl@0: #endif sl@0: sl@0: /* This is an implementation of PKCS#5 v2.0 password based encryption key sl@0: * derivation function PBKDF2 using the only currently defined function HMAC sl@0: * with SHA1. Verified against test vectors posted by Peter Gutmann sl@0: * to the PKCS-TNG mailing list. sl@0: */ sl@0: sl@0: EXPORT_C int PKCS5_PBKDF2_HMAC_SHA1(const char *pass, int passlen, sl@0: const unsigned char *salt, int saltlen, int iter, sl@0: int keylen, unsigned char *out) sl@0: { sl@0: unsigned char digtmp[SHA_DIGEST_LENGTH], *p, itmp[4]; sl@0: int cplen, j, k, tkeylen; sl@0: unsigned long i = 1; sl@0: HMAC_CTX hctx; sl@0: sl@0: HMAC_CTX_init(&hctx); sl@0: p = out; sl@0: tkeylen = keylen; sl@0: if(!pass) passlen = 0; sl@0: else if(passlen == -1) passlen = strlen(pass); sl@0: while(tkeylen) { sl@0: if(tkeylen > SHA_DIGEST_LENGTH) cplen = SHA_DIGEST_LENGTH; sl@0: else cplen = tkeylen; sl@0: /* We are unlikely to ever use more than 256 blocks (5120 bits!) sl@0: * but just in case... sl@0: */ sl@0: itmp[0] = (unsigned char)((i >> 24) & 0xff); sl@0: itmp[1] = (unsigned char)((i >> 16) & 0xff); sl@0: itmp[2] = (unsigned char)((i >> 8) & 0xff); sl@0: itmp[3] = (unsigned char)(i & 0xff); sl@0: HMAC_Init_ex(&hctx, pass, passlen, EVP_sha1(), NULL); sl@0: HMAC_Update(&hctx, salt, saltlen); sl@0: HMAC_Update(&hctx, itmp, 4); sl@0: HMAC_Final(&hctx, digtmp, NULL); sl@0: memcpy(p, digtmp, cplen); sl@0: for(j = 1; j < iter; j++) { sl@0: HMAC(EVP_sha1(), pass, passlen, sl@0: digtmp, SHA_DIGEST_LENGTH, digtmp, NULL); sl@0: for(k = 0; k < cplen; k++) p[k] ^= digtmp[k]; sl@0: } sl@0: tkeylen-= cplen; sl@0: i++; sl@0: p+= cplen; sl@0: } sl@0: HMAC_CTX_cleanup(&hctx); sl@0: #ifdef DEBUG_PKCS5V2 sl@0: fprintf(stderr, "Password:\n"); sl@0: h__dump (pass, passlen); sl@0: fprintf(stderr, "Salt:\n"); sl@0: h__dump (salt, saltlen); sl@0: fprintf(stderr, "Iteration count %d\n", iter); sl@0: fprintf(stderr, "Key:\n"); sl@0: h__dump (out, keylen); sl@0: #endif sl@0: return 1; sl@0: } sl@0: sl@0: #ifdef DO_TEST sl@0: main() sl@0: { sl@0: unsigned char out[4]; sl@0: unsigned char salt[] = {0x12, 0x34, 0x56, 0x78}; sl@0: PKCS5_PBKDF2_HMAC_SHA1("password", -1, salt, 4, 5, 4, out); sl@0: fprintf(stderr, "Out %02X %02X %02X %02X\n", sl@0: out[0], out[1], out[2], out[3]); sl@0: } sl@0: sl@0: #endif sl@0: sl@0: /* Now the key derivation function itself. This is a bit evil because sl@0: * it has to check the ASN1 parameters are valid: and there are quite a sl@0: * few of them... sl@0: */ sl@0: sl@0: EXPORT_C int PKCS5_v2_PBE_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass, int passlen, sl@0: ASN1_TYPE *param, const EVP_CIPHER *c, const EVP_MD *md, sl@0: int en_de) sl@0: { sl@0: unsigned char *salt, key[EVP_MAX_KEY_LENGTH]; sl@0: const unsigned char *pbuf; sl@0: int saltlen, iter, plen; sl@0: unsigned int keylen; sl@0: PBE2PARAM *pbe2 = NULL; sl@0: const EVP_CIPHER *cipher; sl@0: PBKDF2PARAM *kdf = NULL; sl@0: sl@0: if (param == NULL || param->type != V_ASN1_SEQUENCE || sl@0: param->value.sequence == NULL) { sl@0: EVPerr(EVP_F_PKCS5_V2_PBE_KEYIVGEN,EVP_R_DECODE_ERROR); sl@0: return 0; sl@0: } sl@0: sl@0: pbuf = param->value.sequence->data; sl@0: plen = param->value.sequence->length; sl@0: if(!(pbe2 = d2i_PBE2PARAM(NULL, &pbuf, plen))) { sl@0: EVPerr(EVP_F_PKCS5_V2_PBE_KEYIVGEN,EVP_R_DECODE_ERROR); sl@0: return 0; sl@0: } sl@0: sl@0: /* See if we recognise the key derivation function */ sl@0: sl@0: if(OBJ_obj2nid(pbe2->keyfunc->algorithm) != NID_id_pbkdf2) { sl@0: EVPerr(EVP_F_PKCS5_V2_PBE_KEYIVGEN, sl@0: EVP_R_UNSUPPORTED_KEY_DERIVATION_FUNCTION); sl@0: goto err; sl@0: } sl@0: sl@0: /* lets see if we recognise the encryption algorithm. sl@0: */ sl@0: sl@0: cipher = EVP_get_cipherbyname( sl@0: OBJ_nid2sn(OBJ_obj2nid(pbe2->encryption->algorithm))); sl@0: sl@0: if(!cipher) { sl@0: EVPerr(EVP_F_PKCS5_V2_PBE_KEYIVGEN, sl@0: EVP_R_UNSUPPORTED_CIPHER); sl@0: goto err; sl@0: } sl@0: sl@0: /* Fixup cipher based on AlgorithmIdentifier */ sl@0: EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, en_de); sl@0: if(EVP_CIPHER_asn1_to_param(ctx, pbe2->encryption->parameter) < 0) { sl@0: EVPerr(EVP_F_PKCS5_V2_PBE_KEYIVGEN, sl@0: EVP_R_CIPHER_PARAMETER_ERROR); sl@0: goto err; sl@0: } sl@0: keylen = EVP_CIPHER_CTX_key_length(ctx); sl@0: OPENSSL_assert(keylen <= sizeof key); sl@0: sl@0: /* Now decode key derivation function */ sl@0: sl@0: if(!pbe2->keyfunc->parameter || sl@0: (pbe2->keyfunc->parameter->type != V_ASN1_SEQUENCE)) sl@0: { sl@0: EVPerr(EVP_F_PKCS5_V2_PBE_KEYIVGEN,EVP_R_DECODE_ERROR); sl@0: goto err; sl@0: } sl@0: sl@0: pbuf = pbe2->keyfunc->parameter->value.sequence->data; sl@0: plen = pbe2->keyfunc->parameter->value.sequence->length; sl@0: if(!(kdf = d2i_PBKDF2PARAM(NULL, &pbuf, plen)) ) { sl@0: EVPerr(EVP_F_PKCS5_V2_PBE_KEYIVGEN,EVP_R_DECODE_ERROR); sl@0: goto err; sl@0: } sl@0: sl@0: PBE2PARAM_free(pbe2); sl@0: pbe2 = NULL; sl@0: sl@0: /* Now check the parameters of the kdf */ sl@0: sl@0: if(kdf->keylength && (ASN1_INTEGER_get(kdf->keylength) != (int)keylen)){ sl@0: EVPerr(EVP_F_PKCS5_V2_PBE_KEYIVGEN, sl@0: EVP_R_UNSUPPORTED_KEYLENGTH); sl@0: goto err; sl@0: } sl@0: sl@0: if(kdf->prf && (OBJ_obj2nid(kdf->prf->algorithm) != NID_hmacWithSHA1)) { sl@0: EVPerr(EVP_F_PKCS5_V2_PBE_KEYIVGEN, EVP_R_UNSUPPORTED_PRF); sl@0: goto err; sl@0: } sl@0: sl@0: if(kdf->salt->type != V_ASN1_OCTET_STRING) { sl@0: EVPerr(EVP_F_PKCS5_V2_PBE_KEYIVGEN, sl@0: EVP_R_UNSUPPORTED_SALT_TYPE); sl@0: goto err; sl@0: } sl@0: sl@0: /* it seems that its all OK */ sl@0: salt = kdf->salt->value.octet_string->data; sl@0: saltlen = kdf->salt->value.octet_string->length; sl@0: iter = ASN1_INTEGER_get(kdf->iter); sl@0: PKCS5_PBKDF2_HMAC_SHA1(pass, passlen, salt, saltlen, iter, keylen, key); sl@0: EVP_CipherInit_ex(ctx, NULL, NULL, key, NULL, en_de); sl@0: OPENSSL_cleanse(key, keylen); sl@0: PBKDF2PARAM_free(kdf); sl@0: return 1; sl@0: sl@0: err: sl@0: PBE2PARAM_free(pbe2); sl@0: PBKDF2PARAM_free(kdf); sl@0: return 0; sl@0: } sl@0: sl@0: #ifdef DEBUG_PKCS5V2 sl@0: static void h__dump (const unsigned char *p, int len) sl@0: { sl@0: for (; len --; p++) fprintf(stderr, "%02X ", *p); sl@0: fprintf(stderr, "\n"); sl@0: } sl@0: #endif sl@0: #endif