1221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom/* pmeth_gn.c */
2221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * project 2006.
4221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom */
5221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom/* ====================================================================
6221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * Copyright (c) 2006 The OpenSSL Project.  All rights reserved.
7221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *
8221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * Redistribution and use in source and binary forms, with or without
9221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * modification, are permitted provided that the following conditions
10221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * are met:
11221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *
12221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * 1. Redistributions of source code must retain the above copyright
13221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *    notice, this list of conditions and the following disclaimer.
14221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *
15221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * 2. Redistributions in binary form must reproduce the above copyright
16221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *    notice, this list of conditions and the following disclaimer in
17221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *    the documentation and/or other materials provided with the
18221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *    distribution.
19221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *
20221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * 3. All advertising materials mentioning features or use of this
21221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *    software must display the following acknowledgment:
22221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *    "This product includes software developed by the OpenSSL Project
23221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *
25221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *    endorse or promote products derived from this software without
27221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *    prior written permission. For written permission, please contact
28221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *    licensing@OpenSSL.org.
29221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *
30221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * 5. Products derived from this software may not be called "OpenSSL"
31221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *    nor may "OpenSSL" appear in their names without prior written
32221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *    permission of the OpenSSL Project.
33221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *
34221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * 6. Redistributions of any form whatsoever must retain the following
35221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *    acknowledgment:
36221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *    "This product includes software developed by the OpenSSL Project
37221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *
39221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * OF THE POSSIBILITY OF SUCH DAMAGE.
51221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * ====================================================================
52221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *
53221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * This product includes cryptographic software written by Eric Young
54221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * (eay@cryptsoft.com).  This product includes software written by Tim
55221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * Hudson (tjh@cryptsoft.com).
56221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *
57221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom */
58221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
59221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom#include <stdio.h>
60221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom#include <stdlib.h>
61221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom#include "cryptlib.h"
62221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom#include <openssl/objects.h>
63221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom#include <openssl/evp.h>
64221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom#include <openssl/bn.h>
65221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom#include "evp_locl.h"
66221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
67221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromint EVP_PKEY_paramgen_init(EVP_PKEY_CTX *ctx)
68221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	{
69221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	int ret;
70221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (!ctx || !ctx->pmeth || !ctx->pmeth->paramgen)
71221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		{
72221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		EVPerr(EVP_F_EVP_PKEY_PARAMGEN_INIT,
73221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
74221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		return -2;
75221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		}
76221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	ctx->operation = EVP_PKEY_OP_PARAMGEN;
77221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (!ctx->pmeth->paramgen_init)
78221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		return 1;
79221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	ret = ctx->pmeth->paramgen_init(ctx);
80221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (ret <= 0)
81221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		ctx->operation = EVP_PKEY_OP_UNDEFINED;
82221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	return ret;
83221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	}
84221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
85221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromint EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
86221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	{
87221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	int ret;
88221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (!ctx || !ctx->pmeth || !ctx->pmeth->paramgen)
89221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		{
90221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		EVPerr(EVP_F_EVP_PKEY_PARAMGEN,
91221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
92221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		return -2;
93221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		}
94221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
95221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (ctx->operation != EVP_PKEY_OP_PARAMGEN)
96221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		{
97221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		EVPerr(EVP_F_EVP_PKEY_PARAMGEN, EVP_R_OPERATON_NOT_INITIALIZED);
98221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		return -1;
99221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		}
100221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
101221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (!ppkey)
102221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		return -1;
103221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
104221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (!*ppkey)
105221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		*ppkey = EVP_PKEY_new();
106221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
107221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	ret = ctx->pmeth->paramgen(ctx, *ppkey);
108221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (ret <= 0)
109221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		{
110221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		EVP_PKEY_free(*ppkey);
111221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		*ppkey = NULL;
112221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		}
113221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	return ret;
114221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	}
115221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
116221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromint EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx)
117221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	{
118221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	int ret;
119221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (!ctx || !ctx->pmeth || !ctx->pmeth->keygen)
120221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		{
121221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		EVPerr(EVP_F_EVP_PKEY_KEYGEN_INIT,
122221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
123221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		return -2;
124221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		}
125221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	ctx->operation = EVP_PKEY_OP_KEYGEN;
126221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (!ctx->pmeth->keygen_init)
127221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		return 1;
128221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	ret = ctx->pmeth->keygen_init(ctx);
129221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (ret <= 0)
130221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		ctx->operation = EVP_PKEY_OP_UNDEFINED;
131221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	return ret;
132221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	}
133221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
134221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromint EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
135221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	{
136221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	int ret;
137221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
138221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (!ctx || !ctx->pmeth || !ctx->pmeth->keygen)
139221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		{
140221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		EVPerr(EVP_F_EVP_PKEY_KEYGEN,
141221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
142221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		return -2;
143221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		}
144221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (ctx->operation != EVP_PKEY_OP_KEYGEN)
145221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		{
146221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		EVPerr(EVP_F_EVP_PKEY_KEYGEN, EVP_R_OPERATON_NOT_INITIALIZED);
147221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		return -1;
148221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		}
149221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
150221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (!ppkey)
151221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		return -1;
152221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
153221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (!*ppkey)
154221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		*ppkey = EVP_PKEY_new();
155221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
156221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	ret = ctx->pmeth->keygen(ctx, *ppkey);
157221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (ret <= 0)
158221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		{
159221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		EVP_PKEY_free(*ppkey);
160221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		*ppkey = NULL;
161221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		}
162221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	return ret;
163221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	}
164221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
165221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromvoid EVP_PKEY_CTX_set_cb(EVP_PKEY_CTX *ctx, EVP_PKEY_gen_cb *cb)
166221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	{
167221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	ctx->pkey_gencb = cb;
168221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	}
169221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
170221304ee937bc0910948a8be1320cb8cc4eb6d36Brian CarlstromEVP_PKEY_gen_cb *EVP_PKEY_CTX_get_cb(EVP_PKEY_CTX *ctx)
171221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	{
172221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	return ctx->pkey_gencb;
173221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	}
174221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
175221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom/* "translation callback" to call EVP_PKEY_CTX callbacks using BN_GENCB
176221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * style callbacks.
177221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom */
178221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
179221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromstatic int trans_cb(int a, int b, BN_GENCB *gcb)
180221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	{
181221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	EVP_PKEY_CTX *ctx = gcb->arg;
182221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	ctx->keygen_info[0] = a;
183221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	ctx->keygen_info[1] = b;
184221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	return ctx->pkey_gencb(ctx);
185221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	}
186221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
187221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromvoid evp_pkey_set_cb_translate(BN_GENCB *cb, EVP_PKEY_CTX *ctx)
188221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	{
189221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	BN_GENCB_set(cb, trans_cb, ctx)
190221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	}
191221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
192221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromint EVP_PKEY_CTX_get_keygen_info(EVP_PKEY_CTX *ctx, int idx)
193221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	{
194221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (idx == -1)
195221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		return ctx->keygen_info_count;
196221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (idx < 0 || idx > ctx->keygen_info_count)
197221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		return 0;
198221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	return ctx->keygen_info[idx];
199221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	}
200221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
201221304ee937bc0910948a8be1320cb8cc4eb6d36Brian CarlstromEVP_PKEY *EVP_PKEY_new_mac_key(int type, ENGINE *e,
202392aa7cc7d2b122614c5393c3e357da07fd07af3Brian Carlstrom				const unsigned char *key, int keylen)
203221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	{
204221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	EVP_PKEY_CTX *mac_ctx = NULL;
205221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	EVP_PKEY *mac_key = NULL;
206221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	mac_ctx = EVP_PKEY_CTX_new_id(type, e);
207221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (!mac_ctx)
208221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		return NULL;
209221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (EVP_PKEY_keygen_init(mac_ctx) <= 0)
210221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		goto merr;
211221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (EVP_PKEY_CTX_ctrl(mac_ctx, -1, EVP_PKEY_OP_KEYGEN,
212392aa7cc7d2b122614c5393c3e357da07fd07af3Brian Carlstrom				EVP_PKEY_CTRL_SET_MAC_KEY,
213392aa7cc7d2b122614c5393c3e357da07fd07af3Brian Carlstrom				keylen, (void *)key) <= 0)
214221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		goto merr;
215221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (EVP_PKEY_keygen(mac_ctx, &mac_key) <= 0)
216221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		goto merr;
217221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	merr:
218221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (mac_ctx)
219221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		EVP_PKEY_CTX_free(mac_ctx);
220221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	return mac_key;
221221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	}
222