]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/evp/pmeth_gn.c
Identify and move common internal libcrypto header files
[thirdparty/openssl.git] / crypto / evp / pmeth_gn.c
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 "internal/cryptlib.h"
63 #include <openssl/objects.h>
64 #include <openssl/evp.h>
65 #include "internal/bn_int.h"
66 #include "internal/evp_int.h"
67
68 int EVP_PKEY_paramgen_init(EVP_PKEY_CTX *ctx)
69 {
70 int ret;
71 if (!ctx || !ctx->pmeth || !ctx->pmeth->paramgen) {
72 EVPerr(EVP_F_EVP_PKEY_PARAMGEN_INIT,
73 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
74 return -2;
75 }
76 ctx->operation = EVP_PKEY_OP_PARAMGEN;
77 if (!ctx->pmeth->paramgen_init)
78 return 1;
79 ret = ctx->pmeth->paramgen_init(ctx);
80 if (ret <= 0)
81 ctx->operation = EVP_PKEY_OP_UNDEFINED;
82 return ret;
83 }
84
85 int EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
86 {
87 int ret;
88 if (!ctx || !ctx->pmeth || !ctx->pmeth->paramgen) {
89 EVPerr(EVP_F_EVP_PKEY_PARAMGEN,
90 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
91 return -2;
92 }
93
94 if (ctx->operation != EVP_PKEY_OP_PARAMGEN) {
95 EVPerr(EVP_F_EVP_PKEY_PARAMGEN, EVP_R_OPERATON_NOT_INITIALIZED);
96 return -1;
97 }
98
99 if (!ppkey)
100 return -1;
101
102 if (!*ppkey)
103 *ppkey = EVP_PKEY_new();
104
105 ret = ctx->pmeth->paramgen(ctx, *ppkey);
106 if (ret <= 0) {
107 EVP_PKEY_free(*ppkey);
108 *ppkey = NULL;
109 }
110 return ret;
111 }
112
113 int EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx)
114 {
115 int ret;
116 if (!ctx || !ctx->pmeth || !ctx->pmeth->keygen) {
117 EVPerr(EVP_F_EVP_PKEY_KEYGEN_INIT,
118 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
119 return -2;
120 }
121 ctx->operation = EVP_PKEY_OP_KEYGEN;
122 if (!ctx->pmeth->keygen_init)
123 return 1;
124 ret = ctx->pmeth->keygen_init(ctx);
125 if (ret <= 0)
126 ctx->operation = EVP_PKEY_OP_UNDEFINED;
127 return ret;
128 }
129
130 int EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
131 {
132 int ret;
133
134 if (!ctx || !ctx->pmeth || !ctx->pmeth->keygen) {
135 EVPerr(EVP_F_EVP_PKEY_KEYGEN,
136 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
137 return -2;
138 }
139 if (ctx->operation != EVP_PKEY_OP_KEYGEN) {
140 EVPerr(EVP_F_EVP_PKEY_KEYGEN, EVP_R_OPERATON_NOT_INITIALIZED);
141 return -1;
142 }
143
144 if (!ppkey)
145 return -1;
146
147 if (!*ppkey)
148 *ppkey = EVP_PKEY_new();
149
150 ret = ctx->pmeth->keygen(ctx, *ppkey);
151 if (ret <= 0) {
152 EVP_PKEY_free(*ppkey);
153 *ppkey = NULL;
154 }
155 return ret;
156 }
157
158 void EVP_PKEY_CTX_set_cb(EVP_PKEY_CTX *ctx, EVP_PKEY_gen_cb *cb)
159 {
160 ctx->pkey_gencb = cb;
161 }
162
163 EVP_PKEY_gen_cb *EVP_PKEY_CTX_get_cb(EVP_PKEY_CTX *ctx)
164 {
165 return ctx->pkey_gencb;
166 }
167
168 /*
169 * "translation callback" to call EVP_PKEY_CTX callbacks using BN_GENCB style
170 * callbacks.
171 */
172
173 static int trans_cb(int a, int b, BN_GENCB *gcb)
174 {
175 EVP_PKEY_CTX *ctx = BN_GENCB_get_arg(gcb);
176 ctx->keygen_info[0] = a;
177 ctx->keygen_info[1] = b;
178 return ctx->pkey_gencb(ctx);
179 }
180
181 void evp_pkey_set_cb_translate(BN_GENCB *cb, EVP_PKEY_CTX *ctx)
182 {
183 BN_GENCB_set(cb, trans_cb, ctx);
184 }
185
186 int EVP_PKEY_CTX_get_keygen_info(EVP_PKEY_CTX *ctx, int idx)
187 {
188 if (idx == -1)
189 return ctx->keygen_info_count;
190 if (idx < 0 || idx > ctx->keygen_info_count)
191 return 0;
192 return ctx->keygen_info[idx];
193 }
194
195 EVP_PKEY *EVP_PKEY_new_mac_key(int type, ENGINE *e,
196 const unsigned char *key, int keylen)
197 {
198 EVP_PKEY_CTX *mac_ctx = NULL;
199 EVP_PKEY *mac_key = NULL;
200 mac_ctx = EVP_PKEY_CTX_new_id(type, e);
201 if (!mac_ctx)
202 return NULL;
203 if (EVP_PKEY_keygen_init(mac_ctx) <= 0)
204 goto merr;
205 if (EVP_PKEY_CTX_set_mac_key(mac_ctx, key, keylen) <= 0)
206 goto merr;
207 if (EVP_PKEY_keygen(mac_ctx, &mac_key) <= 0)
208 goto merr;
209 merr:
210 EVP_PKEY_CTX_free(mac_ctx);
211 return mac_key;
212 }