source: EcnlProtoTool/trunk/openssl-1.1.0e/crypto/pkcs12/p12_key.c@ 331

Last change on this file since 331 was 331, checked in by coas-nagasima, 6 years ago

prototoolに関連するプロジェクトをnewlibからmuslを使うよう変更・更新
ntshellをnewlibの下位の実装から、muslのsyscallの実装に変更・更新
以下のOSSをアップデート
・mruby-1.3.0
・musl-1.1.18
・onigmo-6.1.3
・tcc-0.9.27
以下のOSSを追加
・openssl-1.1.0e
・curl-7.57.0
・zlib-1.2.11
以下のmrbgemsを追加
・iij/mruby-digest
・iij/mruby-env
・iij/mruby-errno
・iij/mruby-iijson
・iij/mruby-ipaddr
・iij/mruby-mock
・iij/mruby-require
・iij/mruby-tls-openssl

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc
File size: 5.9 KB
Line 
1/*
2 * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9
10#include <stdio.h>
11#include "internal/cryptlib.h"
12#include <openssl/pkcs12.h>
13#include <openssl/bn.h>
14
15/* Uncomment out this line to get debugging info about key generation */
16/*
17 * #define OPENSSL_DEBUG_KEYGEN
18 */
19#ifdef OPENSSL_DEBUG_KEYGEN
20# include <openssl/bio.h>
21extern BIO *bio_err;
22void h__dump(unsigned char *p, int len);
23#endif
24
25/* PKCS12 compatible key/IV generation */
26#ifndef min
27# define min(a,b) ((a) < (b) ? (a) : (b))
28#endif
29
30int 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)
33{
34 int ret;
35 unsigned char *unipass;
36 int uniplen;
37
38 if (!pass) {
39 unipass = NULL;
40 uniplen = 0;
41 } else if (!OPENSSL_asc2uni(pass, passlen, &unipass, &uniplen)) {
42 PKCS12err(PKCS12_F_PKCS12_KEY_GEN_ASC, ERR_R_MALLOC_FAILURE);
43 return 0;
44 }
45 ret = PKCS12_key_gen_uni(unipass, uniplen, salt, saltlen,
46 id, iter, n, out, md_type);
47 if (ret <= 0)
48 return 0;
49 OPENSSL_clear_free(unipass, uniplen);
50 return ret;
51}
52
53int 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)
56{
57 int ret;
58 unsigned char *unipass;
59 int uniplen;
60
61 if (!pass) {
62 unipass = NULL;
63 uniplen = 0;
64 } else if (!OPENSSL_utf82uni(pass, passlen, &unipass, &uniplen)) {
65 PKCS12err(PKCS12_F_PKCS12_KEY_GEN_UTF8, ERR_R_MALLOC_FAILURE);
66 return 0;
67 }
68 ret = PKCS12_key_gen_uni(unipass, uniplen, salt, saltlen,
69 id, iter, n, out, md_type);
70 if (ret <= 0)
71 return 0;
72 OPENSSL_clear_free(unipass, uniplen);
73 return ret;
74}
75
76int 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)
79{
80 unsigned char *B = NULL, *D = NULL, *I = NULL, *p = NULL, *Ai = NULL;
81 int Slen, Plen, Ilen, Ijlen;
82 int i, j, u, v;
83 int ret = 0;
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;
88 int tmpn = n;
89#endif
90
91 ctx = EVP_MD_CTX_new();
92 if (ctx == NULL)
93 goto err;
94
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);
102#endif
103 v = EVP_MD_block_size(md_type);
104 u = EVP_MD_size(md_type);
105 if (u < 0 || v <= 0)
106 goto err;
107 D = OPENSSL_malloc(v);
108 Ai = OPENSSL_malloc(u);
109 B = OPENSSL_malloc(v + 1);
110 Slen = v * ((saltlen + v - 1) / v);
111 if (passlen)
112 Plen = v * ((passlen + v - 1) / v);
113 else
114 Plen = 0;
115 Ilen = Slen + Plen;
116 I = OPENSSL_malloc(Ilen);
117 Ij = BN_new();
118 Bpl1 = BN_new();
119 if (D == NULL || Ai == NULL || B == NULL || I == NULL || Ij == NULL
120 || Bpl1 == NULL)
121 goto err;
122 for (i = 0; i < v; i++)
123 D[i] = id;
124 p = 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];
129 for (;;) {
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))
134 goto err;
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))
139 goto err;
140 }
141 memcpy(out, Ai, min(n, u));
142 if (u >= n) {
143#ifdef OPENSSL_DEBUG_KEYGEN
144 fprintf(stderr, "Output KEY (length %d)\n", tmpn);
145 h__dump(tmpout, tmpn);
146#endif
147 ret = 1;
148 goto end;
149 }
150 n -= u;
151 out += u;
152 for (j = 0; j < v; j++)
153 B[j] = Ai[j % u];
154 /* Work out B + 1 first then can use B as tmp space */
155 if (!BN_bin2bn(B, v, Bpl1))
156 goto err;
157 if (!BN_add_word(Bpl1, 1))
158 goto err;
159 for (j = 0; j < Ilen; j += v) {
160 if (!BN_bin2bn(I + j, v, Ij))
161 goto err;
162 if (!BN_add(Ij, Ij, Bpl1))
163 goto err;
164 if (!BN_bn2bin(Ij, B))
165 goto err;
166 Ijlen = BN_num_bytes(Ij);
167 /* If more than 2^(v*8) - 1 cut off MSB */
168 if (Ijlen > v) {
169 if (!BN_bn2bin(Ij, B))
170 goto err;
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))
177 goto err;
178#endif
179 } else if (!BN_bn2bin(Ij, I + j))
180 goto err;
181 }
182 }
183
184 err:
185 PKCS12err(PKCS12_F_PKCS12_KEY_GEN_UNI, ERR_R_MALLOC_FAILURE);
186
187 end:
188 OPENSSL_free(Ai);
189 OPENSSL_free(B);
190 OPENSSL_free(D);
191 OPENSSL_free(I);
192 BN_free(Ij);
193 BN_free(Bpl1);
194 EVP_MD_CTX_free(ctx);
195 return ret;
196}
197
198#ifdef OPENSSL_DEBUG_KEYGEN
199void h__dump(unsigned char *p, int len)
200{
201 for (; len--; p++)
202 fprintf(stderr, "%02X", *p);
203 fprintf(stderr, "\n");
204}
205#endif
Note: See TracBrowser for help on using the repository browser.