2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
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 * ====================================================================
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).
62 #include <openssl/objects.h>
63 #include <openssl/evp.h>
64 #include <openssl/bn.h>
67 int EVP_PKEY_paramgen_init(EVP_PKEY_CTX
*ctx
)
70 if (!ctx
|| !ctx
->pmeth
|| !ctx
->pmeth
->paramgen
)
72 EVPerr(EVP_F_EVP_PKEY_PARAMGEN_INIT
,
73 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
76 ctx
->operation
= EVP_PKEY_OP_PARAMGEN
;
77 if (!ctx
->pmeth
->paramgen_init
)
79 ret
= ctx
->pmeth
->paramgen_init(ctx
);
81 ctx
->operation
= EVP_PKEY_OP_UNDEFINED
;
85 int EVP_PKEY_paramgen(EVP_PKEY_CTX
*ctx
, EVP_PKEY
**ppkey
)
88 if (!ctx
|| !ctx
->pmeth
|| !ctx
->pmeth
->paramgen
)
90 EVPerr(EVP_F_EVP_PKEY_PARAMGEN
,
91 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
95 if (ctx
->operation
!= EVP_PKEY_OP_PARAMGEN
)
97 EVPerr(EVP_F_EVP_PKEY_PARAMGEN
, EVP_R_OPERATON_NOT_INITIALIZED
);
105 *ppkey
= EVP_PKEY_new();
107 ret
= ctx
->pmeth
->paramgen(ctx
, *ppkey
);
110 EVP_PKEY_free(*ppkey
);
116 int EVP_PKEY_keygen_init(EVP_PKEY_CTX
*ctx
)
119 if (!ctx
|| !ctx
->pmeth
|| !ctx
->pmeth
->keygen
)
121 EVPerr(EVP_F_EVP_PKEY_KEYGEN_INIT
,
122 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
125 ctx
->operation
= EVP_PKEY_OP_KEYGEN
;
126 if (!ctx
->pmeth
->keygen_init
)
128 ret
= ctx
->pmeth
->keygen_init(ctx
);
130 ctx
->operation
= EVP_PKEY_OP_UNDEFINED
;
134 int EVP_PKEY_keygen(EVP_PKEY_CTX
*ctx
, EVP_PKEY
**ppkey
)
138 if (!ctx
|| !ctx
->pmeth
|| !ctx
->pmeth
->keygen
)
140 EVPerr(EVP_F_EVP_PKEY_KEYGEN
,
141 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
144 if (ctx
->operation
!= EVP_PKEY_OP_KEYGEN
)
146 EVPerr(EVP_F_EVP_PKEY_KEYGEN
, EVP_R_OPERATON_NOT_INITIALIZED
);
154 *ppkey
= EVP_PKEY_new();
156 ret
= ctx
->pmeth
->keygen(ctx
, *ppkey
);
159 EVP_PKEY_free(*ppkey
);
165 void EVP_PKEY_CTX_set_cb(EVP_PKEY_CTX
*ctx
, EVP_PKEY_gen_cb
*cb
)
167 ctx
->pkey_gencb
= cb
;
170 EVP_PKEY_gen_cb
*EVP_PKEY_CTX_get_cb(EVP_PKEY_CTX
*ctx
)
172 return ctx
->pkey_gencb
;
175 /* "translation callback" to call EVP_PKEY_CTX callbacks using BN_GENCB
179 static int trans_cb(int a
, int b
, BN_GENCB
*gcb
)
181 EVP_PKEY_CTX
*ctx
= gcb
->arg
;
182 ctx
->keygen_info
[0] = a
;
183 ctx
->keygen_info
[1] = b
;
184 return ctx
->pkey_gencb(ctx
);
187 void evp_pkey_set_cb_translate(BN_GENCB
*cb
, EVP_PKEY_CTX
*ctx
)
189 BN_GENCB_set(cb
, trans_cb
, ctx
)
192 int EVP_PKEY_CTX_get_keygen_info(EVP_PKEY_CTX
*ctx
, int idx
)
195 return ctx
->keygen_info_count
;
196 if (idx
< 0 || idx
> ctx
->keygen_info_count
)
198 return ctx
->keygen_info
[idx
];
201 EVP_PKEY
*EVP_PKEY_new_mac_key(int type
, ENGINE
*e
,
202 const unsigned char *key
, int keylen
)
204 EVP_PKEY_CTX
*mac_ctx
= NULL
;
205 EVP_PKEY
*mac_key
= NULL
;
206 mac_ctx
= EVP_PKEY_CTX_new_id(type
, e
);
209 if (EVP_PKEY_keygen_init(mac_ctx
) <= 0)
211 if (EVP_PKEY_CTX_ctrl(mac_ctx
, -1, EVP_PKEY_OP_KEYGEN
,
212 EVP_PKEY_CTRL_SET_MAC_KEY
,
213 keylen
, (void *)key
) <= 0)
215 if (EVP_PKEY_keygen(mac_ctx
, &mac_key
) <= 0)
219 EVP_PKEY_CTX_free(mac_ctx
);