2 * Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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
12 #include "internal/cryptlib.h"
13 #include <openssl/objects.h>
14 #include <openssl/evp.h>
15 #include "crypto/bn.h"
16 #include "crypto/asn1.h"
17 #include "crypto/evp.h"
18 #include "evp_local.h"
21 int EVP_PKEY_paramgen_init(EVP_PKEY_CTX *ctx)
24 if (!ctx || !ctx->pmeth || !ctx->pmeth->paramgen) {
25 EVPerr(EVP_F_EVP_PKEY_PARAMGEN_INIT,
26 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
29 ctx->operation = EVP_PKEY_OP_PARAMGEN;
30 if (!ctx->pmeth->paramgen_init)
32 ret = ctx->pmeth->paramgen_init(ctx);
34 ctx->operation = EVP_PKEY_OP_UNDEFINED;
38 int EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
41 if (!ctx || !ctx->pmeth || !ctx->pmeth->paramgen) {
42 EVPerr(EVP_F_EVP_PKEY_PARAMGEN,
43 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
47 if (ctx->operation != EVP_PKEY_OP_PARAMGEN) {
48 EVPerr(EVP_F_EVP_PKEY_PARAMGEN, EVP_R_OPERATON_NOT_INITIALIZED);
56 *ppkey = EVP_PKEY_new();
59 EVPerr(EVP_F_EVP_PKEY_PARAMGEN, ERR_R_MALLOC_FAILURE);
63 ret = ctx->pmeth->paramgen(ctx, *ppkey);
65 EVP_PKEY_free(*ppkey);
71 int EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx)
74 if (!ctx || !ctx->pmeth || !ctx->pmeth->keygen) {
75 EVPerr(EVP_F_EVP_PKEY_KEYGEN_INIT,
76 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
79 ctx->operation = EVP_PKEY_OP_KEYGEN;
80 if (!ctx->pmeth->keygen_init)
82 ret = ctx->pmeth->keygen_init(ctx);
84 ctx->operation = EVP_PKEY_OP_UNDEFINED;
88 int EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
92 if (!ctx || !ctx->pmeth || !ctx->pmeth->keygen) {
93 EVPerr(EVP_F_EVP_PKEY_KEYGEN,
94 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
97 if (ctx->operation != EVP_PKEY_OP_KEYGEN) {
98 EVPerr(EVP_F_EVP_PKEY_KEYGEN, EVP_R_OPERATON_NOT_INITIALIZED);
106 *ppkey = EVP_PKEY_new();
110 ret = ctx->pmeth->keygen(ctx, *ppkey);
112 EVP_PKEY_free(*ppkey);
118 void EVP_PKEY_CTX_set_cb(EVP_PKEY_CTX *ctx, EVP_PKEY_gen_cb *cb)
120 ctx->pkey_gencb = cb;
123 EVP_PKEY_gen_cb *EVP_PKEY_CTX_get_cb(EVP_PKEY_CTX *ctx)
125 return ctx->pkey_gencb;
129 * "translation callback" to call EVP_PKEY_CTX callbacks using BN_GENCB style
133 static int trans_cb(int a, int b, BN_GENCB *gcb)
135 EVP_PKEY_CTX *ctx = BN_GENCB_get_arg(gcb);
136 ctx->keygen_info[0] = a;
137 ctx->keygen_info[1] = b;
138 return ctx->pkey_gencb(ctx);
141 void evp_pkey_set_cb_translate(BN_GENCB *cb, EVP_PKEY_CTX *ctx)
143 BN_GENCB_set(cb, trans_cb, ctx);
146 int EVP_PKEY_CTX_get_keygen_info(EVP_PKEY_CTX *ctx, int idx)
149 return ctx->keygen_info_count;
150 if (idx < 0 || idx > ctx->keygen_info_count)
152 return ctx->keygen_info[idx];
155 EVP_PKEY *EVP_PKEY_new_mac_key(int type, ENGINE *e,
156 const unsigned char *key, int keylen)
158 EVP_PKEY_CTX *mac_ctx = NULL;
159 EVP_PKEY *mac_key = NULL;
160 mac_ctx = EVP_PKEY_CTX_new_id(type, e);
163 if (EVP_PKEY_keygen_init(mac_ctx) <= 0)
165 if (EVP_PKEY_CTX_set_mac_key(mac_ctx, key, keylen) <= 0)
167 if (EVP_PKEY_keygen(mac_ctx, &mac_key) <= 0)
170 EVP_PKEY_CTX_free(mac_ctx);
174 #endif /* FIPS_MODE */
176 /*- All methods below can also be used in FIPS_MODE */
178 static int fromdata_init(EVP_PKEY_CTX *ctx, int operation)
180 if (ctx == NULL || ctx->keytype == NULL)
183 evp_pkey_ctx_free_old_ops(ctx);
184 ctx->operation = operation;
185 if (ctx->keymgmt == NULL)
186 ctx->keymgmt = EVP_KEYMGMT_fetch(ctx->libctx, ctx->keytype,
188 if (ctx->keymgmt == NULL)
194 ctx->operation = EVP_PKEY_OP_UNDEFINED;
195 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
199 int EVP_PKEY_param_fromdata_init(EVP_PKEY_CTX *ctx)
201 return fromdata_init(ctx, EVP_PKEY_OP_PARAMFROMDATA);
204 int EVP_PKEY_key_fromdata_init(EVP_PKEY_CTX *ctx)
206 return fromdata_init(ctx, EVP_PKEY_OP_KEYFROMDATA);
209 int EVP_PKEY_fromdata(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey, OSSL_PARAM params[])
211 void *keydata = NULL;
214 if (ctx == NULL || (ctx->operation & EVP_PKEY_OP_TYPE_FROMDATA) == 0) {
215 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
223 *ppkey = EVP_PKEY_new();
225 if (*ppkey == NULL) {
226 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
230 if (ctx->operation == EVP_PKEY_OP_PARAMFROMDATA)
231 selection = OSSL_KEYMGMT_SELECT_ALL_PARAMETERS;
233 selection = OSSL_KEYMGMT_SELECT_ALL;
234 keydata = evp_keymgmt_util_fromdata(*ppkey, ctx->keymgmt, selection,
239 /* keydata is cached in *ppkey, so we need not bother with it further */
244 * TODO(3.0) Re-evaluate the names, it's possible that we find these to be
247 * EVP_PKEY_param_settable()
248 * EVP_PKEY_param_gettable()
250 const OSSL_PARAM *EVP_PKEY_param_fromdata_settable(EVP_PKEY_CTX *ctx)
252 /* We call fromdata_init to get ctx->keymgmt populated */
253 if (fromdata_init(ctx, EVP_PKEY_OP_UNDEFINED))
254 return evp_keymgmt_import_types(ctx->keymgmt,
255 OSSL_KEYMGMT_SELECT_ALL_PARAMETERS);
259 const OSSL_PARAM *EVP_PKEY_key_fromdata_settable(EVP_PKEY_CTX *ctx)
261 /* We call fromdata_init to get ctx->keymgmt populated */
262 if (fromdata_init(ctx, EVP_PKEY_OP_UNDEFINED))
263 return evp_keymgmt_import_types(ctx->keymgmt,
264 OSSL_KEYMGMT_SELECT_ALL);