2 * Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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 "internal/bn_int.h"
16 #include "internal/asn1_int.h"
17 #include "internal/evp_int.h"
19 int EVP_PKEY_paramgen_init(EVP_PKEY_CTX *ctx)
22 if (!ctx || !ctx->pmeth || !ctx->pmeth->paramgen) {
23 EVPerr(EVP_F_EVP_PKEY_PARAMGEN_INIT,
24 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
27 ctx->operation = EVP_PKEY_OP_PARAMGEN;
28 if (!ctx->pmeth->paramgen_init)
30 ret = ctx->pmeth->paramgen_init(ctx);
32 ctx->operation = EVP_PKEY_OP_UNDEFINED;
36 int EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
39 if (!ctx || !ctx->pmeth || !ctx->pmeth->paramgen) {
40 EVPerr(EVP_F_EVP_PKEY_PARAMGEN,
41 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
45 if (ctx->operation != EVP_PKEY_OP_PARAMGEN) {
46 EVPerr(EVP_F_EVP_PKEY_PARAMGEN, EVP_R_OPERATON_NOT_INITIALIZED);
54 *ppkey = EVP_PKEY_new();
57 EVPerr(EVP_F_EVP_PKEY_PARAMGEN, ERR_R_MALLOC_FAILURE);
61 ret = ctx->pmeth->paramgen(ctx, *ppkey);
63 EVP_PKEY_free(*ppkey);
69 int EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx)
72 if (!ctx || !ctx->pmeth || !ctx->pmeth->keygen) {
73 EVPerr(EVP_F_EVP_PKEY_KEYGEN_INIT,
74 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
77 ctx->operation = EVP_PKEY_OP_KEYGEN;
78 if (!ctx->pmeth->keygen_init)
80 ret = ctx->pmeth->keygen_init(ctx);
82 ctx->operation = EVP_PKEY_OP_UNDEFINED;
86 int EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
90 if (!ctx || !ctx->pmeth || !ctx->pmeth->keygen) {
91 EVPerr(EVP_F_EVP_PKEY_KEYGEN,
92 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
95 if (ctx->operation != EVP_PKEY_OP_KEYGEN) {
96 EVPerr(EVP_F_EVP_PKEY_KEYGEN, EVP_R_OPERATON_NOT_INITIALIZED);
104 *ppkey = EVP_PKEY_new();
108 ret = ctx->pmeth->keygen(ctx, *ppkey);
110 EVP_PKEY_free(*ppkey);
116 void EVP_PKEY_CTX_set_cb(EVP_PKEY_CTX *ctx, EVP_PKEY_gen_cb *cb)
118 ctx->pkey_gencb = cb;
121 EVP_PKEY_gen_cb *EVP_PKEY_CTX_get_cb(EVP_PKEY_CTX *ctx)
123 return ctx->pkey_gencb;
127 * "translation callback" to call EVP_PKEY_CTX callbacks using BN_GENCB style
131 static int trans_cb(int a, int b, BN_GENCB *gcb)
133 EVP_PKEY_CTX *ctx = BN_GENCB_get_arg(gcb);
134 ctx->keygen_info[0] = a;
135 ctx->keygen_info[1] = b;
136 return ctx->pkey_gencb(ctx);
139 void evp_pkey_set_cb_translate(BN_GENCB *cb, EVP_PKEY_CTX *ctx)
141 BN_GENCB_set(cb, trans_cb, ctx);
144 int EVP_PKEY_CTX_get_keygen_info(EVP_PKEY_CTX *ctx, int idx)
147 return ctx->keygen_info_count;
148 if (idx < 0 || idx > ctx->keygen_info_count)
150 return ctx->keygen_info[idx];
153 EVP_PKEY *EVP_PKEY_new_mac_key(int type, ENGINE *e,
154 const unsigned char *key, int keylen)
156 EVP_PKEY_CTX *mac_ctx = NULL;
157 EVP_PKEY *mac_key = NULL;
158 mac_ctx = EVP_PKEY_CTX_new_id(type, e);
161 if (EVP_PKEY_keygen_init(mac_ctx) <= 0)
163 if (EVP_PKEY_CTX_set_mac_key(mac_ctx, key, keylen) <= 0)
165 if (EVP_PKEY_keygen(mac_ctx, &mac_key) <= 0)
168 EVP_PKEY_CTX_free(mac_ctx);
172 int EVP_PKEY_check(EVP_PKEY_CTX *ctx)
174 EVP_PKEY *pkey = ctx->pkey;
177 EVPerr(EVP_F_EVP_PKEY_CHECK, EVP_R_NO_KEY_SET);
181 /* call customized check function first */
182 if (ctx->pmeth->check != NULL)
183 return ctx->pmeth->check(pkey);
185 /* use default check function in ameth */
186 if (pkey->ameth == NULL || pkey->ameth->pkey_check == NULL) {
187 EVPerr(EVP_F_EVP_PKEY_CHECK,
188 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
192 return pkey->ameth->pkey_check(pkey);
195 int EVP_PKEY_public_check(EVP_PKEY_CTX *ctx)
197 EVP_PKEY *pkey = ctx->pkey;
200 EVPerr(EVP_F_EVP_PKEY_PUBLIC_CHECK, EVP_R_NO_KEY_SET);
204 /* call customized public key check function first */
205 if (ctx->pmeth->public_check != NULL)
206 return ctx->pmeth->public_check(pkey);
208 /* use default public key check function in ameth */
209 if (pkey->ameth == NULL || pkey->ameth->pkey_public_check == NULL) {
210 EVPerr(EVP_F_EVP_PKEY_PUBLIC_CHECK,
211 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
215 return pkey->ameth->pkey_public_check(pkey);
218 int EVP_PKEY_param_check(EVP_PKEY_CTX *ctx)
220 EVP_PKEY *pkey = ctx->pkey;
223 EVPerr(EVP_F_EVP_PKEY_PARAM_CHECK, EVP_R_NO_KEY_SET);
227 /* call customized param check function first */
228 if (ctx->pmeth->param_check != NULL)
229 return ctx->pmeth->param_check(pkey);
231 /* use default param check function in ameth */
232 if (pkey->ameth == NULL || pkey->ameth->pkey_param_check == NULL) {
233 EVPerr(EVP_F_EVP_PKEY_PARAM_CHECK,
234 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
238 return pkey->ameth->pkey_param_check(pkey);