sl@0: /* p12_key.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: sl@0: #include sl@0: #include "cryptlib.h" sl@0: #include sl@0: #include sl@0: sl@0: /* Uncomment out this line to get debugging info about key generation */ sl@0: /*#define DEBUG_KEYGEN*/ sl@0: #ifdef DEBUG_KEYGEN sl@0: #include sl@0: extern BIO *bio_err; sl@0: void h__dump (unsigned char *p, int len); sl@0: #endif sl@0: sl@0: /* PKCS12 compatible key/IV generation */ sl@0: #ifndef min sl@0: #define min(a,b) ((a) < (b) ? (a) : (b)) sl@0: #endif sl@0: sl@0: EXPORT_C int PKCS12_key_gen_asc(const char *pass, int passlen, unsigned char *salt, sl@0: int saltlen, int id, int iter, int n, unsigned char *out, sl@0: const EVP_MD *md_type) sl@0: { sl@0: int ret; sl@0: unsigned char *unipass; sl@0: int uniplen; sl@0: if(!pass) { sl@0: unipass = NULL; sl@0: uniplen = 0; sl@0: } else if (!asc2uni(pass, passlen, &unipass, &uniplen)) { sl@0: PKCS12err(PKCS12_F_PKCS12_KEY_GEN_ASC,ERR_R_MALLOC_FAILURE); sl@0: return 0; sl@0: } sl@0: ret = PKCS12_key_gen_uni(unipass, uniplen, salt, saltlen, sl@0: id, iter, n, out, md_type); sl@0: if(unipass) { sl@0: OPENSSL_cleanse(unipass, uniplen); /* Clear password from memory */ sl@0: OPENSSL_free(unipass); sl@0: } sl@0: return ret; sl@0: } sl@0: sl@0: EXPORT_C int PKCS12_key_gen_uni(unsigned char *pass, int passlen, unsigned char *salt, sl@0: int saltlen, int id, int iter, int n, unsigned char *out, sl@0: const EVP_MD *md_type) sl@0: { sl@0: unsigned char *B, *D, *I, *p, *Ai; sl@0: int Slen, Plen, Ilen, Ijlen; sl@0: int i, j, u, v; sl@0: BIGNUM *Ij, *Bpl1; /* These hold Ij and B + 1 */ sl@0: EVP_MD_CTX ctx; sl@0: #ifdef DEBUG_KEYGEN sl@0: unsigned char *tmpout = out; sl@0: int tmpn = n; sl@0: #endif sl@0: sl@0: #if 0 sl@0: if (!pass) { sl@0: PKCS12err(PKCS12_F_PKCS12_KEY_GEN_UNI,ERR_R_PASSED_NULL_PARAMETER); sl@0: return 0; sl@0: } sl@0: #endif sl@0: sl@0: EVP_MD_CTX_init(&ctx); sl@0: #ifdef DEBUG_KEYGEN sl@0: fprintf(stderr, "KEYGEN DEBUG\n"); sl@0: fprintf(stderr, "ID %d, ITER %d\n", id, iter); sl@0: fprintf(stderr, "Password (length %d):\n", passlen); sl@0: h__dump(pass, passlen); sl@0: fprintf(stderr, "Salt (length %d):\n", saltlen); sl@0: h__dump(salt, saltlen); sl@0: #endif sl@0: v = EVP_MD_block_size (md_type); sl@0: u = EVP_MD_size (md_type); sl@0: D = OPENSSL_malloc (v); sl@0: Ai = OPENSSL_malloc (u); sl@0: B = OPENSSL_malloc (v + 1); sl@0: Slen = v * ((saltlen+v-1)/v); sl@0: if(passlen) Plen = v * ((passlen+v-1)/v); sl@0: else Plen = 0; sl@0: Ilen = Slen + Plen; sl@0: I = OPENSSL_malloc (Ilen); sl@0: Ij = BN_new(); sl@0: Bpl1 = BN_new(); sl@0: if (!D || !Ai || !B || !I || !Ij || !Bpl1) { sl@0: PKCS12err(PKCS12_F_PKCS12_KEY_GEN_UNI,ERR_R_MALLOC_FAILURE); sl@0: return 0; sl@0: } sl@0: for (i = 0; i < v; i++) D[i] = id; sl@0: p = I; sl@0: for (i = 0; i < Slen; i++) *p++ = salt[i % saltlen]; sl@0: for (i = 0; i < Plen; i++) *p++ = pass[i % passlen]; sl@0: for (;;) { sl@0: EVP_DigestInit_ex(&ctx, md_type, NULL); sl@0: EVP_DigestUpdate(&ctx, D, v); sl@0: EVP_DigestUpdate(&ctx, I, Ilen); sl@0: EVP_DigestFinal_ex(&ctx, Ai, NULL); sl@0: for (j = 1; j < iter; j++) { sl@0: EVP_DigestInit_ex(&ctx, md_type, NULL); sl@0: EVP_DigestUpdate(&ctx, Ai, u); sl@0: EVP_DigestFinal_ex(&ctx, Ai, NULL); sl@0: } sl@0: memcpy (out, Ai, min (n, u)); sl@0: if (u >= n) { sl@0: OPENSSL_free (Ai); sl@0: OPENSSL_free (B); sl@0: OPENSSL_free (D); sl@0: OPENSSL_free (I); sl@0: BN_free (Ij); sl@0: BN_free (Bpl1); sl@0: EVP_MD_CTX_cleanup(&ctx); sl@0: #ifdef DEBUG_KEYGEN sl@0: fprintf(stderr, "Output KEY (length %d)\n", tmpn); sl@0: h__dump(tmpout, tmpn); sl@0: #endif sl@0: return 1; sl@0: } sl@0: n -= u; sl@0: out += u; sl@0: for (j = 0; j < v; j++) B[j] = Ai[j % u]; sl@0: /* Work out B + 1 first then can use B as tmp space */ sl@0: BN_bin2bn (B, v, Bpl1); sl@0: BN_add_word (Bpl1, 1); sl@0: for (j = 0; j < Ilen ; j+=v) { sl@0: BN_bin2bn (I + j, v, Ij); sl@0: BN_add (Ij, Ij, Bpl1); sl@0: BN_bn2bin (Ij, B); sl@0: Ijlen = BN_num_bytes (Ij); sl@0: /* If more than 2^(v*8) - 1 cut off MSB */ sl@0: if (Ijlen > v) { sl@0: BN_bn2bin (Ij, B); sl@0: memcpy (I + j, B + 1, v); sl@0: #ifndef PKCS12_BROKEN_KEYGEN sl@0: /* If less than v bytes pad with zeroes */ sl@0: } else if (Ijlen < v) { sl@0: memset(I + j, 0, v - Ijlen); sl@0: BN_bn2bin(Ij, I + j + v - Ijlen); sl@0: #endif sl@0: } else BN_bn2bin (Ij, I + j); sl@0: } sl@0: } sl@0: } sl@0: #ifdef DEBUG_KEYGEN sl@0: void h__dump (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