155714Skris/* p12_key.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 "cryptlib.h"
6255714Skris#include <openssl/pkcs12.h>
63160814Ssimon#include <openssl/bn.h>
6455714Skris
6555714Skris/* Uncomment out this line to get debugging info about key generation */
66296341Sdelphij/*
67296341Sdelphij * #define DEBUG_KEYGEN
68296341Sdelphij */
6955714Skris#ifdef DEBUG_KEYGEN
70296341Sdelphij# include <openssl/bio.h>
7155714Skrisextern BIO *bio_err;
72296341Sdelphijvoid h__dump(unsigned char *p, int len);
7355714Skris#endif
7455714Skris
7555714Skris/* PKCS12 compatible key/IV generation */
7655714Skris#ifndef min
77296341Sdelphij# define min(a,b) ((a) < (b) ? (a) : (b))
7855714Skris#endif
7955714Skris
8068651Skrisint PKCS12_key_gen_asc(const char *pass, int passlen, unsigned char *salt,
81296341Sdelphij                       int saltlen, int id, int iter, int n,
82296341Sdelphij                       unsigned char *out, const EVP_MD *md_type)
8355714Skris{
84296341Sdelphij    int ret;
85296341Sdelphij    unsigned char *unipass;
86296341Sdelphij    int uniplen;
87238405Sjkim
88296341Sdelphij    if (!pass) {
89296341Sdelphij        unipass = NULL;
90296341Sdelphij        uniplen = 0;
91296341Sdelphij    } else if (!OPENSSL_asc2uni(pass, passlen, &unipass, &uniplen)) {
92296341Sdelphij        PKCS12err(PKCS12_F_PKCS12_KEY_GEN_ASC, ERR_R_MALLOC_FAILURE);
93296341Sdelphij        return 0;
94296341Sdelphij    }
95296341Sdelphij    ret = PKCS12_key_gen_uni(unipass, uniplen, salt, saltlen,
96296341Sdelphij                             id, iter, n, out, md_type);
97296341Sdelphij    if (ret <= 0)
98296341Sdelphij        return 0;
99296341Sdelphij    if (unipass) {
100296341Sdelphij        OPENSSL_cleanse(unipass, uniplen); /* Clear password from memory */
101296341Sdelphij        OPENSSL_free(unipass);
102296341Sdelphij    }
103296341Sdelphij    return ret;
10455714Skris}
10555714Skris
10668651Skrisint PKCS12_key_gen_uni(unsigned char *pass, int passlen, unsigned char *salt,
107296341Sdelphij                       int saltlen, int id, int iter, int n,
108296341Sdelphij                       unsigned char *out, const EVP_MD *md_type)
10955714Skris{
110296341Sdelphij    unsigned char *B, *D, *I, *p, *Ai;
111296341Sdelphij    int Slen, Plen, Ilen, Ijlen;
112296341Sdelphij    int i, j, u, v;
113296341Sdelphij    int ret = 0;
114296341Sdelphij    BIGNUM *Ij, *Bpl1;          /* These hold Ij and B + 1 */
115296341Sdelphij    EVP_MD_CTX ctx;
11655714Skris#ifdef  DEBUG_KEYGEN
117296341Sdelphij    unsigned char *tmpout = out;
118296341Sdelphij    int tmpn = n;
11955714Skris#endif
12059191Skris
12168651Skris#if 0
122296341Sdelphij    if (!pass) {
123296341Sdelphij        PKCS12err(PKCS12_F_PKCS12_KEY_GEN_UNI, ERR_R_PASSED_NULL_PARAMETER);
124296341Sdelphij        return 0;
125296341Sdelphij    }
12668651Skris#endif
12759191Skris
128296341Sdelphij    EVP_MD_CTX_init(&ctx);
12959191Skris#ifdef  DEBUG_KEYGEN
130296341Sdelphij    fprintf(stderr, "KEYGEN DEBUG\n");
131296341Sdelphij    fprintf(stderr, "ID %d, ITER %d\n", id, iter);
132296341Sdelphij    fprintf(stderr, "Password (length %d):\n", passlen);
133296341Sdelphij    h__dump(pass, passlen);
134296341Sdelphij    fprintf(stderr, "Salt (length %d):\n", saltlen);
135296341Sdelphij    h__dump(salt, saltlen);
13659191Skris#endif
137296341Sdelphij    v = EVP_MD_block_size(md_type);
138296341Sdelphij    u = EVP_MD_size(md_type);
139296341Sdelphij    if (u < 0)
140296341Sdelphij        return 0;
141296341Sdelphij    D = OPENSSL_malloc(v);
142296341Sdelphij    Ai = OPENSSL_malloc(u);
143296341Sdelphij    B = OPENSSL_malloc(v + 1);
144296341Sdelphij    Slen = v * ((saltlen + v - 1) / v);
145296341Sdelphij    if (passlen)
146296341Sdelphij        Plen = v * ((passlen + v - 1) / v);
147296341Sdelphij    else
148296341Sdelphij        Plen = 0;
149296341Sdelphij    Ilen = Slen + Plen;
150296341Sdelphij    I = OPENSSL_malloc(Ilen);
151296341Sdelphij    Ij = BN_new();
152296341Sdelphij    Bpl1 = BN_new();
153296341Sdelphij    if (!D || !Ai || !B || !I || !Ij || !Bpl1)
154296341Sdelphij        goto err;
155296341Sdelphij    for (i = 0; i < v; i++)
156296341Sdelphij        D[i] = id;
157296341Sdelphij    p = I;
158296341Sdelphij    for (i = 0; i < Slen; i++)
159296341Sdelphij        *p++ = salt[i % saltlen];
160296341Sdelphij    for (i = 0; i < Plen; i++)
161296341Sdelphij        *p++ = pass[i % passlen];
162296341Sdelphij    for (;;) {
163296341Sdelphij        if (!EVP_DigestInit_ex(&ctx, md_type, NULL)
164296341Sdelphij            || !EVP_DigestUpdate(&ctx, D, v)
165296341Sdelphij            || !EVP_DigestUpdate(&ctx, I, Ilen)
166296341Sdelphij            || !EVP_DigestFinal_ex(&ctx, Ai, NULL))
167296341Sdelphij            goto err;
168296341Sdelphij        for (j = 1; j < iter; j++) {
169296341Sdelphij            if (!EVP_DigestInit_ex(&ctx, md_type, NULL)
170296341Sdelphij                || !EVP_DigestUpdate(&ctx, Ai, u)
171296341Sdelphij                || !EVP_DigestFinal_ex(&ctx, Ai, NULL))
172296341Sdelphij                goto err;
173296341Sdelphij        }
174296341Sdelphij        memcpy(out, Ai, min(n, u));
175296341Sdelphij        if (u >= n) {
17655714Skris#ifdef DEBUG_KEYGEN
177296341Sdelphij            fprintf(stderr, "Output KEY (length %d)\n", tmpn);
178296341Sdelphij            h__dump(tmpout, tmpn);
17955714Skris#endif
180296341Sdelphij            ret = 1;
181296341Sdelphij            goto end;
182296341Sdelphij        }
183296341Sdelphij        n -= u;
184296341Sdelphij        out += u;
185296341Sdelphij        for (j = 0; j < v; j++)
186296341Sdelphij            B[j] = Ai[j % u];
187296341Sdelphij        /* Work out B + 1 first then can use B as tmp space */
188296341Sdelphij        if (!BN_bin2bn(B, v, Bpl1))
189296341Sdelphij            goto err;
190296341Sdelphij        if (!BN_add_word(Bpl1, 1))
191296341Sdelphij            goto err;
192296341Sdelphij        for (j = 0; j < Ilen; j += v) {
193296341Sdelphij            if (!BN_bin2bn(I + j, v, Ij))
194296341Sdelphij                goto err;
195296341Sdelphij            if (!BN_add(Ij, Ij, Bpl1))
196296341Sdelphij                goto err;
197296341Sdelphij            if (!BN_bn2bin(Ij, B))
198296341Sdelphij                goto err;
199296341Sdelphij            Ijlen = BN_num_bytes(Ij);
200296341Sdelphij            /* If more than 2^(v*8) - 1 cut off MSB */
201296341Sdelphij            if (Ijlen > v) {
202296341Sdelphij                if (!BN_bn2bin(Ij, B))
203296341Sdelphij                    goto err;
204296341Sdelphij                memcpy(I + j, B + 1, v);
20576866Skris#ifndef PKCS12_BROKEN_KEYGEN
206296341Sdelphij                /* If less than v bytes pad with zeroes */
207296341Sdelphij            } else if (Ijlen < v) {
208296341Sdelphij                memset(I + j, 0, v - Ijlen);
209296341Sdelphij                if (!BN_bn2bin(Ij, I + j + v - Ijlen))
210296341Sdelphij                    goto err;
21176866Skris#endif
212296341Sdelphij            } else if (!BN_bn2bin(Ij, I + j))
213296341Sdelphij                goto err;
214296341Sdelphij        }
215296341Sdelphij    }
216215697Ssimon
217296341Sdelphij err:
218296341Sdelphij    PKCS12err(PKCS12_F_PKCS12_KEY_GEN_UNI, ERR_R_MALLOC_FAILURE);
219215697Ssimon
220296341Sdelphij end:
221296341Sdelphij    OPENSSL_free(Ai);
222296341Sdelphij    OPENSSL_free(B);
223296341Sdelphij    OPENSSL_free(D);
224296341Sdelphij    OPENSSL_free(I);
225296341Sdelphij    BN_free(Ij);
226296341Sdelphij    BN_free(Bpl1);
227296341Sdelphij    EVP_MD_CTX_cleanup(&ctx);
228296341Sdelphij    return ret;
22955714Skris}
230296341Sdelphij
23155714Skris#ifdef DEBUG_KEYGEN
232296341Sdelphijvoid h__dump(unsigned char *p, int len)
23355714Skris{
234296341Sdelphij    for (; len--; p++)
235296341Sdelphij        fprintf(stderr, "%02X", *p);
236296341Sdelphij    fprintf(stderr, "\n");
23755714Skris}
23855714Skris#endif
239