1 /* $OpenBSD: p12_key.c,v 1.34 2023/02/16 08:38:17 tb Exp $ */ 2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL 3 * project 1999. 4 */ 5 /* ==================================================================== 6 * Copyright (c) 1999 The OpenSSL Project. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in 17 * the documentation and/or other materials provided with the 18 * distribution. 19 * 20 * 3. All advertising materials mentioning features or use of this 21 * software must display the following acknowledgment: 22 * "This product includes software developed by the OpenSSL Project 23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 24 * 25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 26 * endorse or promote products derived from this software without 27 * prior written permission. For written permission, please contact 28 * licensing@OpenSSL.org. 29 * 30 * 5. Products derived from this software may not be called "OpenSSL" 31 * nor may "OpenSSL" appear in their names without prior written 32 * permission of the OpenSSL Project. 33 * 34 * 6. Redistributions of any form whatsoever must retain the following 35 * acknowledgment: 36 * "This product includes software developed by the OpenSSL Project 37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 38 * 39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 50 * OF THE POSSIBILITY OF SUCH DAMAGE. 51 * ==================================================================== 52 * 53 * This product includes cryptographic software written by Eric Young 54 * (eay@cryptsoft.com). This product includes software written by Tim 55 * Hudson (tjh@cryptsoft.com). 56 * 57 */ 58 59 #include <stdio.h> 60 #include <string.h> 61 62 #include <openssl/bn.h> 63 #include <openssl/err.h> 64 #include <openssl/pkcs12.h> 65 66 #include "evp_local.h" 67 68 /* PKCS12 compatible key/IV generation */ 69 #ifndef min 70 #define min(a,b) ((a) < (b) ? (a) : (b)) 71 #endif 72 73 int 74 PKCS12_key_gen_asc(const char *pass, int passlen, unsigned char *salt, 75 int saltlen, int id, int iter, int n, unsigned char *out, 76 const EVP_MD *md_type) 77 { 78 int ret; 79 unsigned char *unipass; 80 int uniplen; 81 82 if (!pass) { 83 unipass = NULL; 84 uniplen = 0; 85 } else if (!OPENSSL_asc2uni(pass, passlen, &unipass, &uniplen)) { 86 PKCS12error(ERR_R_MALLOC_FAILURE); 87 return 0; 88 } 89 ret = PKCS12_key_gen_uni(unipass, uniplen, salt, saltlen, 90 id, iter, n, out, md_type); 91 if (ret <= 0) 92 return 0; 93 freezero(unipass, uniplen); 94 return ret; 95 } 96 LCRYPTO_ALIAS(PKCS12_key_gen_asc); 97 98 int 99 PKCS12_key_gen_uni(unsigned char *pass, int passlen, unsigned char *salt, 100 int saltlen, int id, int iter, int n, unsigned char *out, 101 const EVP_MD *md_type) 102 { 103 EVP_MD_CTX *ctx = NULL; 104 unsigned char *B = NULL, *D = NULL, *I = NULL, *Ai = NULL; 105 unsigned char *p; 106 int Slen, Plen, Ilen; 107 int i, j, u, v; 108 int ret = 0; 109 110 if ((ctx = EVP_MD_CTX_new()) == NULL) 111 goto err; 112 113 if ((v = EVP_MD_block_size(md_type)) <= 0) 114 goto err; 115 if ((u = EVP_MD_size(md_type)) <= 0) 116 goto err; 117 118 if ((D = malloc(v)) == NULL) 119 goto err; 120 if ((Ai = malloc(u)) == NULL) 121 goto err; 122 if ((B = malloc(v + 1)) == NULL) 123 goto err; 124 125 Slen = v * ((saltlen + v - 1) / v); 126 127 Plen = 0; 128 if (passlen) 129 Plen = v * ((passlen + v - 1) / v); 130 131 Ilen = Slen + Plen; 132 133 if ((I = malloc(Ilen)) == NULL) 134 goto err; 135 136 for (i = 0; i < v; i++) 137 D[i] = id; 138 139 p = I; 140 for (i = 0; i < Slen; i++) 141 *p++ = salt[i % saltlen]; 142 for (i = 0; i < Plen; i++) 143 *p++ = pass[i % passlen]; 144 145 for (;;) { 146 if (!EVP_DigestInit_ex(ctx, md_type, NULL)) 147 goto err; 148 if (!EVP_DigestUpdate(ctx, D, v)) 149 goto err; 150 if (!EVP_DigestUpdate(ctx, I, Ilen)) 151 goto err; 152 if (!EVP_DigestFinal_ex(ctx, Ai, NULL)) 153 goto err; 154 for (j = 1; j < iter; j++) { 155 if (!EVP_DigestInit_ex(ctx, md_type, NULL)) 156 goto err; 157 if (!EVP_DigestUpdate(ctx, Ai, u)) 158 goto err; 159 if (!EVP_DigestFinal_ex(ctx, Ai, NULL)) 160 goto err; 161 } 162 memcpy(out, Ai, min(n, u)); 163 if (u >= n) { 164 ret = 1; 165 goto end; 166 } 167 n -= u; 168 out += u; 169 for (j = 0; j < v; j++) 170 B[j] = Ai[j % u]; 171 172 for (j = 0; j < Ilen; j += v) { 173 uint16_t c = 1; 174 int k; 175 176 /* Work out I[j] = I[j] + B + 1. */ 177 for (k = v - 1; k >= 0; k--) { 178 c += I[j + k] + B[k]; 179 I[j + k] = (unsigned char)c; 180 c >>= 8; 181 } 182 } 183 } 184 185 err: 186 PKCS12error(ERR_R_MALLOC_FAILURE); 187 188 end: 189 free(Ai); 190 free(B); 191 free(D); 192 free(I); 193 EVP_MD_CTX_free(ctx); 194 195 return ret; 196 } 197 LCRYPTO_ALIAS(PKCS12_key_gen_uni); 198