/src/openssl/crypto/evp/pmeth_gn.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* pmeth_gn.c */ |
2 | | /* |
3 | | * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project |
4 | | * 2006. |
5 | | */ |
6 | | /* ==================================================================== |
7 | | * Copyright (c) 2006 The OpenSSL Project. All rights reserved. |
8 | | * |
9 | | * Redistribution and use in source and binary forms, with or without |
10 | | * modification, are permitted provided that the following conditions |
11 | | * are met: |
12 | | * |
13 | | * 1. Redistributions of source code must retain the above copyright |
14 | | * notice, this list of conditions and the following disclaimer. |
15 | | * |
16 | | * 2. Redistributions in binary form must reproduce the above copyright |
17 | | * notice, this list of conditions and the following disclaimer in |
18 | | * the documentation and/or other materials provided with the |
19 | | * distribution. |
20 | | * |
21 | | * 3. All advertising materials mentioning features or use of this |
22 | | * software must display the following acknowledgment: |
23 | | * "This product includes software developed by the OpenSSL Project |
24 | | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" |
25 | | * |
26 | | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
27 | | * endorse or promote products derived from this software without |
28 | | * prior written permission. For written permission, please contact |
29 | | * licensing@OpenSSL.org. |
30 | | * |
31 | | * 5. Products derived from this software may not be called "OpenSSL" |
32 | | * nor may "OpenSSL" appear in their names without prior written |
33 | | * permission of the OpenSSL Project. |
34 | | * |
35 | | * 6. Redistributions of any form whatsoever must retain the following |
36 | | * acknowledgment: |
37 | | * "This product includes software developed by the OpenSSL Project |
38 | | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" |
39 | | * |
40 | | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
41 | | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
42 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
43 | | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
44 | | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
45 | | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
46 | | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
47 | | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
48 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
49 | | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
50 | | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
51 | | * OF THE POSSIBILITY OF SUCH DAMAGE. |
52 | | * ==================================================================== |
53 | | * |
54 | | * This product includes cryptographic software written by Eric Young |
55 | | * (eay@cryptsoft.com). This product includes software written by Tim |
56 | | * Hudson (tjh@cryptsoft.com). |
57 | | * |
58 | | */ |
59 | | |
60 | | #include <stdio.h> |
61 | | #include <stdlib.h> |
62 | | #include "cryptlib.h" |
63 | | #include <openssl/objects.h> |
64 | | #include <openssl/evp.h> |
65 | | #include <openssl/bn.h> |
66 | | #include "evp_locl.h" |
67 | | |
68 | | int EVP_PKEY_paramgen_init(EVP_PKEY_CTX *ctx) |
69 | 0 | { |
70 | 0 | int ret; |
71 | 0 | if (!ctx || !ctx->pmeth || !ctx->pmeth->paramgen) { |
72 | 0 | EVPerr(EVP_F_EVP_PKEY_PARAMGEN_INIT, |
73 | 0 | EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
74 | 0 | return -2; |
75 | 0 | } |
76 | 0 | ctx->operation = EVP_PKEY_OP_PARAMGEN; |
77 | 0 | if (!ctx->pmeth->paramgen_init) |
78 | 0 | return 1; |
79 | 0 | ret = ctx->pmeth->paramgen_init(ctx); |
80 | 0 | if (ret <= 0) |
81 | 0 | ctx->operation = EVP_PKEY_OP_UNDEFINED; |
82 | 0 | return ret; |
83 | 0 | } |
84 | | |
85 | | int EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey) |
86 | 0 | { |
87 | 0 | int ret; |
88 | 0 | if (!ctx || !ctx->pmeth || !ctx->pmeth->paramgen) { |
89 | 0 | EVPerr(EVP_F_EVP_PKEY_PARAMGEN, |
90 | 0 | EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
91 | 0 | return -2; |
92 | 0 | } |
93 | | |
94 | 0 | if (ctx->operation != EVP_PKEY_OP_PARAMGEN) { |
95 | 0 | EVPerr(EVP_F_EVP_PKEY_PARAMGEN, EVP_R_OPERATON_NOT_INITIALIZED); |
96 | 0 | return -1; |
97 | 0 | } |
98 | | |
99 | 0 | if (ppkey == NULL) |
100 | 0 | return -1; |
101 | | |
102 | 0 | if (*ppkey == NULL) |
103 | 0 | *ppkey = EVP_PKEY_new(); |
104 | |
|
105 | 0 | if (*ppkey == NULL) { |
106 | 0 | EVPerr(EVP_F_EVP_PKEY_PARAMGEN, ERR_R_MALLOC_FAILURE); |
107 | 0 | return -1; |
108 | 0 | } |
109 | | |
110 | 0 | ret = ctx->pmeth->paramgen(ctx, *ppkey); |
111 | 0 | if (ret <= 0) { |
112 | 0 | EVP_PKEY_free(*ppkey); |
113 | 0 | *ppkey = NULL; |
114 | 0 | } |
115 | 0 | return ret; |
116 | 0 | } |
117 | | |
118 | | int EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx) |
119 | 0 | { |
120 | 0 | int ret; |
121 | 0 | if (!ctx || !ctx->pmeth || !ctx->pmeth->keygen) { |
122 | 0 | EVPerr(EVP_F_EVP_PKEY_KEYGEN_INIT, |
123 | 0 | EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
124 | 0 | return -2; |
125 | 0 | } |
126 | 0 | ctx->operation = EVP_PKEY_OP_KEYGEN; |
127 | 0 | if (!ctx->pmeth->keygen_init) |
128 | 0 | return 1; |
129 | 0 | ret = ctx->pmeth->keygen_init(ctx); |
130 | 0 | if (ret <= 0) |
131 | 0 | ctx->operation = EVP_PKEY_OP_UNDEFINED; |
132 | 0 | return ret; |
133 | 0 | } |
134 | | |
135 | | int EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey) |
136 | 0 | { |
137 | 0 | int ret; |
138 | |
|
139 | 0 | if (!ctx || !ctx->pmeth || !ctx->pmeth->keygen) { |
140 | 0 | EVPerr(EVP_F_EVP_PKEY_KEYGEN, |
141 | 0 | EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
142 | 0 | return -2; |
143 | 0 | } |
144 | 0 | if (ctx->operation != EVP_PKEY_OP_KEYGEN) { |
145 | 0 | EVPerr(EVP_F_EVP_PKEY_KEYGEN, EVP_R_OPERATON_NOT_INITIALIZED); |
146 | 0 | return -1; |
147 | 0 | } |
148 | | |
149 | 0 | if (!ppkey) |
150 | 0 | return -1; |
151 | | |
152 | 0 | if (*ppkey == NULL) |
153 | 0 | *ppkey = EVP_PKEY_new(); |
154 | 0 | if (*ppkey == NULL) |
155 | 0 | return -1; |
156 | | |
157 | 0 | ret = ctx->pmeth->keygen(ctx, *ppkey); |
158 | 0 | if (ret <= 0) { |
159 | 0 | EVP_PKEY_free(*ppkey); |
160 | 0 | *ppkey = NULL; |
161 | 0 | } |
162 | 0 | return ret; |
163 | 0 | } |
164 | | |
165 | | void EVP_PKEY_CTX_set_cb(EVP_PKEY_CTX *ctx, EVP_PKEY_gen_cb *cb) |
166 | 0 | { |
167 | 0 | ctx->pkey_gencb = cb; |
168 | 0 | } |
169 | | |
170 | | EVP_PKEY_gen_cb *EVP_PKEY_CTX_get_cb(EVP_PKEY_CTX *ctx) |
171 | 0 | { |
172 | 0 | return ctx->pkey_gencb; |
173 | 0 | } |
174 | | |
175 | | /* |
176 | | * "translation callback" to call EVP_PKEY_CTX callbacks using BN_GENCB style |
177 | | * callbacks. |
178 | | */ |
179 | | |
180 | | static int trans_cb(int a, int b, BN_GENCB *gcb) |
181 | 0 | { |
182 | 0 | EVP_PKEY_CTX *ctx = gcb->arg; |
183 | 0 | ctx->keygen_info[0] = a; |
184 | 0 | ctx->keygen_info[1] = b; |
185 | 0 | return ctx->pkey_gencb(ctx); |
186 | 0 | } |
187 | | |
188 | | void evp_pkey_set_cb_translate(BN_GENCB *cb, EVP_PKEY_CTX *ctx) |
189 | 0 | { |
190 | 0 | BN_GENCB_set(cb, trans_cb, ctx) |
191 | 0 | } |
192 | | |
193 | | int EVP_PKEY_CTX_get_keygen_info(EVP_PKEY_CTX *ctx, int idx) |
194 | 0 | { |
195 | 0 | if (idx == -1) |
196 | 0 | return ctx->keygen_info_count; |
197 | 0 | if (idx < 0 || idx > ctx->keygen_info_count) |
198 | 0 | return 0; |
199 | 0 | return ctx->keygen_info[idx]; |
200 | 0 | } |
201 | | |
202 | | EVP_PKEY *EVP_PKEY_new_mac_key(int type, ENGINE *e, |
203 | | const unsigned char *key, int keylen) |
204 | 0 | { |
205 | 0 | EVP_PKEY_CTX *mac_ctx = NULL; |
206 | 0 | EVP_PKEY *mac_key = NULL; |
207 | 0 | mac_ctx = EVP_PKEY_CTX_new_id(type, e); |
208 | 0 | if (!mac_ctx) |
209 | 0 | return NULL; |
210 | 0 | if (EVP_PKEY_keygen_init(mac_ctx) <= 0) |
211 | 0 | goto merr; |
212 | 0 | if (EVP_PKEY_CTX_ctrl(mac_ctx, -1, EVP_PKEY_OP_KEYGEN, |
213 | 0 | EVP_PKEY_CTRL_SET_MAC_KEY, |
214 | 0 | keylen, (void *)key) <= 0) |
215 | 0 | goto merr; |
216 | 0 | if (EVP_PKEY_keygen(mac_ctx, &mac_key) <= 0) |
217 | 0 | goto merr; |
218 | 0 | merr: |
219 | 0 | if (mac_ctx) |
220 | 0 | EVP_PKEY_CTX_free(mac_ctx); |
221 | 0 | return mac_key; |
222 | 0 | } |