2 * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 #include "internal/cryptlib.h"
12 #include <openssl/pkcs12.h>
13 #include <openssl/bn.h>
15 /* Uncomment out this line to get debugging info about key generation */
17 * #define OPENSSL_DEBUG_KEYGEN
19 #ifdef OPENSSL_DEBUG_KEYGEN
20 # include <openssl/bio.h>
22 void h__dump(unsigned char *p, int len);
25 /* PKCS12 compatible key/IV generation */
27 # define min(a,b) ((a) < (b) ? (a) : (b))
30 int PKCS12_key_gen_asc(const char *pass, int passlen, unsigned char *salt,
31 int saltlen, int id, int iter, int n,
32 unsigned char *out, const EVP_MD *md_type)
35 unsigned char *unipass;
41 } else if (!OPENSSL_asc2uni(pass, passlen, &unipass, &uniplen)) {
42 PKCS12err(PKCS12_F_PKCS12_KEY_GEN_ASC, ERR_R_MALLOC_FAILURE);
45 ret = PKCS12_key_gen_uni(unipass, uniplen, salt, saltlen,
46 id, iter, n, out, md_type);
49 OPENSSL_clear_free(unipass, uniplen);
53 int PKCS12_key_gen_utf8(const char *pass, int passlen, unsigned char *salt,
54 int saltlen, int id, int iter, int n,
55 unsigned char *out, const EVP_MD *md_type)
58 unsigned char *unipass;
64 } else if (!OPENSSL_utf82uni(pass, passlen, &unipass, &uniplen)) {
65 PKCS12err(PKCS12_F_PKCS12_KEY_GEN_UTF8, ERR_R_MALLOC_FAILURE);
68 ret = PKCS12_key_gen_uni(unipass, uniplen, salt, saltlen,
69 id, iter, n, out, md_type);
72 OPENSSL_clear_free(unipass, uniplen);
76 int PKCS12_key_gen_uni(unsigned char *pass, int passlen, unsigned char *salt,
77 int saltlen, int id, int iter, int n,
78 unsigned char *out, const EVP_MD *md_type)
80 unsigned char *B = NULL, *D = NULL, *I = NULL, *p = NULL, *Ai = NULL;
81 int Slen, Plen, Ilen, Ijlen;
84 BIGNUM *Ij = NULL, *Bpl1 = NULL; /* These hold Ij and B + 1 */
85 EVP_MD_CTX *ctx = NULL;
86 #ifdef OPENSSL_DEBUG_KEYGEN
87 unsigned char *tmpout = out;
91 ctx = EVP_MD_CTX_new();
95 #ifdef OPENSSL_DEBUG_KEYGEN
96 fprintf(stderr, "KEYGEN DEBUG\n");
97 fprintf(stderr, "ID %d, ITER %d\n", id, iter);
98 fprintf(stderr, "Password (length %d):\n", passlen);
99 h__dump(pass, passlen);
100 fprintf(stderr, "Salt (length %d):\n", saltlen);
101 h__dump(salt, saltlen);
103 v = EVP_MD_block_size(md_type);
104 u = EVP_MD_size(md_type);
107 D = OPENSSL_malloc(v);
108 Ai = OPENSSL_malloc(u);
109 B = OPENSSL_malloc(v + 1);
110 Slen = v * ((saltlen + v - 1) / v);
112 Plen = v * ((passlen + v - 1) / v);
116 I = OPENSSL_malloc(Ilen);
119 if (D == NULL || Ai == NULL || B == NULL || I == NULL || Ij == NULL
122 for (i = 0; i < v; i++)
125 for (i = 0; i < Slen; i++)
126 *p++ = salt[i % saltlen];
127 for (i = 0; i < Plen; i++)
128 *p++ = pass[i % passlen];
130 if (!EVP_DigestInit_ex(ctx, md_type, NULL)
131 || !EVP_DigestUpdate(ctx, D, v)
132 || !EVP_DigestUpdate(ctx, I, Ilen)
133 || !EVP_DigestFinal_ex(ctx, Ai, NULL))
135 for (j = 1; j < iter; j++) {
136 if (!EVP_DigestInit_ex(ctx, md_type, NULL)
137 || !EVP_DigestUpdate(ctx, Ai, u)
138 || !EVP_DigestFinal_ex(ctx, Ai, NULL))
141 memcpy(out, Ai, min(n, u));
143 #ifdef OPENSSL_DEBUG_KEYGEN
144 fprintf(stderr, "Output KEY (length %d)\n", tmpn);
145 h__dump(tmpout, tmpn);
152 for (j = 0; j < v; j++)
154 /* Work out B + 1 first then can use B as tmp space */
155 if (!BN_bin2bn(B, v, Bpl1))
157 if (!BN_add_word(Bpl1, 1))
159 for (j = 0; j < Ilen; j += v) {
160 if (!BN_bin2bn(I + j, v, Ij))
162 if (!BN_add(Ij, Ij, Bpl1))
164 if (!BN_bn2bin(Ij, B))
166 Ijlen = BN_num_bytes(Ij);
167 /* If more than 2^(v*8) - 1 cut off MSB */
169 if (!BN_bn2bin(Ij, B))
171 memcpy(I + j, B + 1, v);
172 #ifndef PKCS12_BROKEN_KEYGEN
173 /* If less than v bytes pad with zeroes */
174 } else if (Ijlen < v) {
175 memset(I + j, 0, v - Ijlen);
176 if (!BN_bn2bin(Ij, I + j + v - Ijlen))
179 } else if (!BN_bn2bin(Ij, I + j))
185 PKCS12err(PKCS12_F_PKCS12_KEY_GEN_UNI, ERR_R_MALLOC_FAILURE);
194 EVP_MD_CTX_free(ctx);
198 #ifdef OPENSSL_DEBUG_KEYGEN
199 void h__dump(unsigned char *p, int len)
202 fprintf(stderr, "%02X", *p);
203 fprintf(stderr, "\n");