10Sstevel@tonic-gate /* p12_key.c */
20Sstevel@tonic-gate /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
30Sstevel@tonic-gate * project 1999.
40Sstevel@tonic-gate */
50Sstevel@tonic-gate /* ====================================================================
60Sstevel@tonic-gate * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
90Sstevel@tonic-gate * modification, are permitted provided that the following conditions
100Sstevel@tonic-gate * are met:
110Sstevel@tonic-gate *
120Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
130Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
140Sstevel@tonic-gate *
150Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
160Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in
170Sstevel@tonic-gate * the documentation and/or other materials provided with the
180Sstevel@tonic-gate * distribution.
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this
210Sstevel@tonic-gate * software must display the following acknowledgment:
220Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project
230Sstevel@tonic-gate * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
240Sstevel@tonic-gate *
250Sstevel@tonic-gate * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
260Sstevel@tonic-gate * endorse or promote products derived from this software without
270Sstevel@tonic-gate * prior written permission. For written permission, please contact
280Sstevel@tonic-gate * licensing@OpenSSL.org.
290Sstevel@tonic-gate *
300Sstevel@tonic-gate * 5. Products derived from this software may not be called "OpenSSL"
310Sstevel@tonic-gate * nor may "OpenSSL" appear in their names without prior written
320Sstevel@tonic-gate * permission of the OpenSSL Project.
330Sstevel@tonic-gate *
340Sstevel@tonic-gate * 6. Redistributions of any form whatsoever must retain the following
350Sstevel@tonic-gate * acknowledgment:
360Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project
370Sstevel@tonic-gate * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
380Sstevel@tonic-gate *
390Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
400Sstevel@tonic-gate * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
410Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
420Sstevel@tonic-gate * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
430Sstevel@tonic-gate * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
440Sstevel@tonic-gate * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
450Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
460Sstevel@tonic-gate * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
470Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
480Sstevel@tonic-gate * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
490Sstevel@tonic-gate * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
500Sstevel@tonic-gate * OF THE POSSIBILITY OF SUCH DAMAGE.
510Sstevel@tonic-gate * ====================================================================
520Sstevel@tonic-gate *
530Sstevel@tonic-gate * This product includes cryptographic software written by Eric Young
540Sstevel@tonic-gate * (eay@cryptsoft.com). This product includes software written by Tim
550Sstevel@tonic-gate * Hudson (tjh@cryptsoft.com).
560Sstevel@tonic-gate *
570Sstevel@tonic-gate */
580Sstevel@tonic-gate
590Sstevel@tonic-gate #include <stdio.h>
600Sstevel@tonic-gate #include "cryptlib.h"
610Sstevel@tonic-gate #include <openssl/pkcs12.h>
62*2139Sjp161948 #include <openssl/bn.h>
630Sstevel@tonic-gate
640Sstevel@tonic-gate /* Uncomment out this line to get debugging info about key generation */
650Sstevel@tonic-gate /*#define DEBUG_KEYGEN*/
660Sstevel@tonic-gate #ifdef DEBUG_KEYGEN
670Sstevel@tonic-gate #include <openssl/bio.h>
680Sstevel@tonic-gate extern BIO *bio_err;
690Sstevel@tonic-gate void h__dump (unsigned char *p, int len);
700Sstevel@tonic-gate #endif
710Sstevel@tonic-gate
720Sstevel@tonic-gate /* PKCS12 compatible key/IV generation */
730Sstevel@tonic-gate #ifndef min
740Sstevel@tonic-gate #define min(a,b) ((a) < (b) ? (a) : (b))
750Sstevel@tonic-gate #endif
760Sstevel@tonic-gate
PKCS12_key_gen_asc(const char * pass,int passlen,unsigned char * salt,int saltlen,int id,int iter,int n,unsigned char * out,const EVP_MD * md_type)770Sstevel@tonic-gate int PKCS12_key_gen_asc(const char *pass, int passlen, unsigned char *salt,
780Sstevel@tonic-gate int saltlen, int id, int iter, int n, unsigned char *out,
790Sstevel@tonic-gate const EVP_MD *md_type)
800Sstevel@tonic-gate {
810Sstevel@tonic-gate int ret;
820Sstevel@tonic-gate unsigned char *unipass;
830Sstevel@tonic-gate int uniplen;
840Sstevel@tonic-gate if(!pass) {
850Sstevel@tonic-gate unipass = NULL;
860Sstevel@tonic-gate uniplen = 0;
870Sstevel@tonic-gate } else if (!asc2uni(pass, passlen, &unipass, &uniplen)) {
880Sstevel@tonic-gate PKCS12err(PKCS12_F_PKCS12_KEY_GEN_ASC,ERR_R_MALLOC_FAILURE);
890Sstevel@tonic-gate return 0;
900Sstevel@tonic-gate }
910Sstevel@tonic-gate ret = PKCS12_key_gen_uni(unipass, uniplen, salt, saltlen,
920Sstevel@tonic-gate id, iter, n, out, md_type);
930Sstevel@tonic-gate if(unipass) {
940Sstevel@tonic-gate OPENSSL_cleanse(unipass, uniplen); /* Clear password from memory */
950Sstevel@tonic-gate OPENSSL_free(unipass);
960Sstevel@tonic-gate }
970Sstevel@tonic-gate return ret;
980Sstevel@tonic-gate }
990Sstevel@tonic-gate
PKCS12_key_gen_uni(unsigned char * pass,int passlen,unsigned char * salt,int saltlen,int id,int iter,int n,unsigned char * out,const EVP_MD * md_type)1000Sstevel@tonic-gate int PKCS12_key_gen_uni(unsigned char *pass, int passlen, unsigned char *salt,
1010Sstevel@tonic-gate int saltlen, int id, int iter, int n, unsigned char *out,
1020Sstevel@tonic-gate const EVP_MD *md_type)
1030Sstevel@tonic-gate {
1040Sstevel@tonic-gate unsigned char *B, *D, *I, *p, *Ai;
1050Sstevel@tonic-gate int Slen, Plen, Ilen, Ijlen;
1060Sstevel@tonic-gate int i, j, u, v;
1070Sstevel@tonic-gate BIGNUM *Ij, *Bpl1; /* These hold Ij and B + 1 */
1080Sstevel@tonic-gate EVP_MD_CTX ctx;
1090Sstevel@tonic-gate #ifdef DEBUG_KEYGEN
1100Sstevel@tonic-gate unsigned char *tmpout = out;
1110Sstevel@tonic-gate int tmpn = n;
1120Sstevel@tonic-gate #endif
1130Sstevel@tonic-gate
1140Sstevel@tonic-gate #if 0
1150Sstevel@tonic-gate if (!pass) {
1160Sstevel@tonic-gate PKCS12err(PKCS12_F_PKCS12_KEY_GEN_UNI,ERR_R_PASSED_NULL_PARAMETER);
1170Sstevel@tonic-gate return 0;
1180Sstevel@tonic-gate }
1190Sstevel@tonic-gate #endif
1200Sstevel@tonic-gate
1210Sstevel@tonic-gate EVP_MD_CTX_init(&ctx);
1220Sstevel@tonic-gate #ifdef DEBUG_KEYGEN
1230Sstevel@tonic-gate fprintf(stderr, "KEYGEN DEBUG\n");
1240Sstevel@tonic-gate fprintf(stderr, "ID %d, ITER %d\n", id, iter);
1250Sstevel@tonic-gate fprintf(stderr, "Password (length %d):\n", passlen);
1260Sstevel@tonic-gate h__dump(pass, passlen);
1270Sstevel@tonic-gate fprintf(stderr, "Salt (length %d):\n", saltlen);
1280Sstevel@tonic-gate h__dump(salt, saltlen);
1290Sstevel@tonic-gate #endif
1300Sstevel@tonic-gate v = EVP_MD_block_size (md_type);
1310Sstevel@tonic-gate u = EVP_MD_size (md_type);
1320Sstevel@tonic-gate D = OPENSSL_malloc (v);
1330Sstevel@tonic-gate Ai = OPENSSL_malloc (u);
1340Sstevel@tonic-gate B = OPENSSL_malloc (v + 1);
1350Sstevel@tonic-gate Slen = v * ((saltlen+v-1)/v);
1360Sstevel@tonic-gate if(passlen) Plen = v * ((passlen+v-1)/v);
1370Sstevel@tonic-gate else Plen = 0;
1380Sstevel@tonic-gate Ilen = Slen + Plen;
1390Sstevel@tonic-gate I = OPENSSL_malloc (Ilen);
1400Sstevel@tonic-gate Ij = BN_new();
1410Sstevel@tonic-gate Bpl1 = BN_new();
1420Sstevel@tonic-gate if (!D || !Ai || !B || !I || !Ij || !Bpl1) {
1430Sstevel@tonic-gate PKCS12err(PKCS12_F_PKCS12_KEY_GEN_UNI,ERR_R_MALLOC_FAILURE);
1440Sstevel@tonic-gate return 0;
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate for (i = 0; i < v; i++) D[i] = id;
1470Sstevel@tonic-gate p = I;
1480Sstevel@tonic-gate for (i = 0; i < Slen; i++) *p++ = salt[i % saltlen];
1490Sstevel@tonic-gate for (i = 0; i < Plen; i++) *p++ = pass[i % passlen];
1500Sstevel@tonic-gate for (;;) {
1510Sstevel@tonic-gate EVP_DigestInit_ex(&ctx, md_type, NULL);
1520Sstevel@tonic-gate EVP_DigestUpdate(&ctx, D, v);
1530Sstevel@tonic-gate EVP_DigestUpdate(&ctx, I, Ilen);
1540Sstevel@tonic-gate EVP_DigestFinal_ex(&ctx, Ai, NULL);
1550Sstevel@tonic-gate for (j = 1; j < iter; j++) {
1560Sstevel@tonic-gate EVP_DigestInit_ex(&ctx, md_type, NULL);
1570Sstevel@tonic-gate EVP_DigestUpdate(&ctx, Ai, u);
1580Sstevel@tonic-gate EVP_DigestFinal_ex(&ctx, Ai, NULL);
1590Sstevel@tonic-gate }
1600Sstevel@tonic-gate memcpy (out, Ai, min (n, u));
1610Sstevel@tonic-gate if (u >= n) {
1620Sstevel@tonic-gate OPENSSL_free (Ai);
1630Sstevel@tonic-gate OPENSSL_free (B);
1640Sstevel@tonic-gate OPENSSL_free (D);
1650Sstevel@tonic-gate OPENSSL_free (I);
1660Sstevel@tonic-gate BN_free (Ij);
1670Sstevel@tonic-gate BN_free (Bpl1);
1680Sstevel@tonic-gate EVP_MD_CTX_cleanup(&ctx);
1690Sstevel@tonic-gate #ifdef DEBUG_KEYGEN
1700Sstevel@tonic-gate fprintf(stderr, "Output KEY (length %d)\n", tmpn);
1710Sstevel@tonic-gate h__dump(tmpout, tmpn);
1720Sstevel@tonic-gate #endif
1730Sstevel@tonic-gate return 1;
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate n -= u;
1760Sstevel@tonic-gate out += u;
1770Sstevel@tonic-gate for (j = 0; j < v; j++) B[j] = Ai[j % u];
1780Sstevel@tonic-gate /* Work out B + 1 first then can use B as tmp space */
1790Sstevel@tonic-gate BN_bin2bn (B, v, Bpl1);
1800Sstevel@tonic-gate BN_add_word (Bpl1, 1);
1810Sstevel@tonic-gate for (j = 0; j < Ilen ; j+=v) {
1820Sstevel@tonic-gate BN_bin2bn (I + j, v, Ij);
1830Sstevel@tonic-gate BN_add (Ij, Ij, Bpl1);
1840Sstevel@tonic-gate BN_bn2bin (Ij, B);
1850Sstevel@tonic-gate Ijlen = BN_num_bytes (Ij);
1860Sstevel@tonic-gate /* If more than 2^(v*8) - 1 cut off MSB */
1870Sstevel@tonic-gate if (Ijlen > v) {
1880Sstevel@tonic-gate BN_bn2bin (Ij, B);
1890Sstevel@tonic-gate memcpy (I + j, B + 1, v);
1900Sstevel@tonic-gate #ifndef PKCS12_BROKEN_KEYGEN
1910Sstevel@tonic-gate /* If less than v bytes pad with zeroes */
1920Sstevel@tonic-gate } else if (Ijlen < v) {
1930Sstevel@tonic-gate memset(I + j, 0, v - Ijlen);
1940Sstevel@tonic-gate BN_bn2bin(Ij, I + j + v - Ijlen);
1950Sstevel@tonic-gate #endif
1960Sstevel@tonic-gate } else BN_bn2bin (Ij, I + j);
1970Sstevel@tonic-gate }
1980Sstevel@tonic-gate }
1990Sstevel@tonic-gate }
2000Sstevel@tonic-gate #ifdef DEBUG_KEYGEN
h__dump(unsigned char * p,int len)2010Sstevel@tonic-gate void h__dump (unsigned char *p, int len)
2020Sstevel@tonic-gate {
2030Sstevel@tonic-gate for (; len --; p++) fprintf(stderr, "%02X", *p);
2040Sstevel@tonic-gate fprintf(stderr, "\n");
2050Sstevel@tonic-gate }
2060Sstevel@tonic-gate #endif
207