blob: 8d092ee7e79145d621c0a858d5c29df3ae942db0 [file] [log] [blame]
Adam Langleyd9e397b2015-01-22 14:27:53 -08001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.] */
56
57#include <openssl/evp.h>
58
Adam Langleyd9e397b2015-01-22 14:27:53 -080059#include <string.h>
60
Robert Sloan572a4e22017-04-17 10:52:19 -070061#include <openssl/digest.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -080062#include <openssl/err.h>
63#include <openssl/mem.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -080064
Robert Sloan69939df2017-01-09 10:53:07 -080065#include "../internal.h"
Adam Langleyd9e397b2015-01-22 14:27:53 -080066#include "internal.h"
67
68
Adam Langleyd9e397b2015-01-22 14:27:53 -080069static const EVP_PKEY_METHOD *const evp_methods[] = {
70 &rsa_pkey_meth,
Adam Langleyd9e397b2015-01-22 14:27:53 -080071 &ec_pkey_meth,
Robert Sloan572a4e22017-04-17 10:52:19 -070072 &ed25519_pkey_meth,
Adam Langleyd9e397b2015-01-22 14:27:53 -080073};
74
75static const EVP_PKEY_METHOD *evp_pkey_meth_find(int type) {
76 unsigned i;
77
78 for (i = 0; i < sizeof(evp_methods)/sizeof(EVP_PKEY_METHOD*); i++) {
79 if (evp_methods[i]->pkey_id == type) {
80 return evp_methods[i];
81 }
82 }
83
84 return NULL;
85}
86
87static EVP_PKEY_CTX *evp_pkey_ctx_new(EVP_PKEY *pkey, ENGINE *e, int id) {
88 EVP_PKEY_CTX *ret;
89 const EVP_PKEY_METHOD *pmeth;
90
91 if (id == -1) {
92 if (!pkey || !pkey->ameth) {
93 return NULL;
94 }
95 id = pkey->ameth->pkey_id;
96 }
97
98 pmeth = evp_pkey_meth_find(id);
99
100 if (pmeth == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +0000101 OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM);
David Benjamin4969cc92016-04-22 15:02:23 -0400102 ERR_add_error_dataf("algorithm %d", id);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800103 return NULL;
104 }
105
106 ret = OPENSSL_malloc(sizeof(EVP_PKEY_CTX));
107 if (!ret) {
Kenny Rootb8494592015-09-25 02:29:14 +0000108 OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800109 return NULL;
110 }
Robert Sloan69939df2017-01-09 10:53:07 -0800111 OPENSSL_memset(ret, 0, sizeof(EVP_PKEY_CTX));
Adam Langleyd9e397b2015-01-22 14:27:53 -0800112
113 ret->engine = e;
114 ret->pmeth = pmeth;
115 ret->operation = EVP_PKEY_OP_UNDEFINED;
116
117 if (pkey) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400118 EVP_PKEY_up_ref(pkey);
119 ret->pkey = pkey;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800120 }
121
122 if (pmeth->init) {
123 if (pmeth->init(ret) <= 0) {
Adam Langleye9ada862015-05-11 17:20:37 -0700124 EVP_PKEY_free(ret->pkey);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800125 OPENSSL_free(ret);
126 return NULL;
127 }
128 }
129
130 return ret;
131}
132
133EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e) {
134 return evp_pkey_ctx_new(pkey, e, -1);
135}
136
137EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e) {
138 return evp_pkey_ctx_new(NULL, e, id);
139}
140
141void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx) {
142 if (ctx == NULL) {
143 return;
144 }
145 if (ctx->pmeth && ctx->pmeth->cleanup) {
146 ctx->pmeth->cleanup(ctx);
147 }
Adam Langleye9ada862015-05-11 17:20:37 -0700148 EVP_PKEY_free(ctx->pkey);
149 EVP_PKEY_free(ctx->peerkey);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800150 OPENSSL_free(ctx);
151}
152
Robert Sloana94fe052017-02-21 08:49:28 -0800153EVP_PKEY_CTX *EVP_PKEY_CTX_dup(EVP_PKEY_CTX *ctx) {
154 if (!ctx->pmeth || !ctx->pmeth->copy) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800155 return NULL;
156 }
157
Robert Sloana94fe052017-02-21 08:49:28 -0800158 EVP_PKEY_CTX *ret = OPENSSL_malloc(sizeof(EVP_PKEY_CTX));
159 if (!ret) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800160 return NULL;
161 }
162
Robert Sloana94fe052017-02-21 08:49:28 -0800163 OPENSSL_memset(ret, 0, sizeof(EVP_PKEY_CTX));
Adam Langleyd9e397b2015-01-22 14:27:53 -0800164
Robert Sloana94fe052017-02-21 08:49:28 -0800165 ret->pmeth = ctx->pmeth;
166 ret->engine = ctx->engine;
167 ret->operation = ctx->operation;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800168
Robert Sloana94fe052017-02-21 08:49:28 -0800169 if (ctx->pkey != NULL) {
170 EVP_PKEY_up_ref(ctx->pkey);
171 ret->pkey = ctx->pkey;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800172 }
173
Robert Sloana94fe052017-02-21 08:49:28 -0800174 if (ctx->peerkey != NULL) {
175 EVP_PKEY_up_ref(ctx->peerkey);
176 ret->peerkey = ctx->peerkey;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800177 }
178
Robert Sloana94fe052017-02-21 08:49:28 -0800179 if (ctx->pmeth->copy(ret, ctx) <= 0) {
180 ret->pmeth = NULL;
181 EVP_PKEY_CTX_free(ret);
182 OPENSSL_PUT_ERROR(EVP, ERR_LIB_EVP);
183 return NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800184 }
185
Robert Sloana94fe052017-02-21 08:49:28 -0800186 return ret;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800187}
188
189EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx) { return ctx->pkey; }
190
Adam Langleyd9e397b2015-01-22 14:27:53 -0800191int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype, int cmd,
192 int p1, void *p2) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800193 if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl) {
Kenny Rootb8494592015-09-25 02:29:14 +0000194 OPENSSL_PUT_ERROR(EVP, EVP_R_COMMAND_NOT_SUPPORTED);
Adam Langleye9ada862015-05-11 17:20:37 -0700195 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800196 }
197 if (keytype != -1 && ctx->pmeth->pkey_id != keytype) {
Robert Sloan4d1ac502017-02-06 08:36:14 -0800198 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
Adam Langleye9ada862015-05-11 17:20:37 -0700199 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800200 }
201
202 if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
Kenny Rootb8494592015-09-25 02:29:14 +0000203 OPENSSL_PUT_ERROR(EVP, EVP_R_NO_OPERATION_SET);
Adam Langleye9ada862015-05-11 17:20:37 -0700204 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800205 }
206
207 if (optype != -1 && !(ctx->operation & optype)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000208 OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_OPERATION);
Adam Langleye9ada862015-05-11 17:20:37 -0700209 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800210 }
211
Adam Langleye9ada862015-05-11 17:20:37 -0700212 return ctx->pmeth->ctrl(ctx, cmd, p1, p2);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800213}
214
215int EVP_PKEY_sign_init(EVP_PKEY_CTX *ctx) {
Robert Sloan572a4e22017-04-17 10:52:19 -0700216 if (ctx == NULL || ctx->pmeth == NULL ||
217 (ctx->pmeth->sign == NULL && ctx->pmeth->sign_message == NULL)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000218 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800219 return 0;
220 }
221
222 ctx->operation = EVP_PKEY_OP_SIGN;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800223 return 1;
224}
225
226int EVP_PKEY_sign(EVP_PKEY_CTX *ctx, uint8_t *sig, size_t *sig_len,
Robert Sloan572a4e22017-04-17 10:52:19 -0700227 const uint8_t *digest, size_t digest_len) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800228 if (!ctx || !ctx->pmeth || !ctx->pmeth->sign) {
Kenny Rootb8494592015-09-25 02:29:14 +0000229 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800230 return 0;
231 }
232 if (ctx->operation != EVP_PKEY_OP_SIGN) {
Kenny Rootb8494592015-09-25 02:29:14 +0000233 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATON_NOT_INITIALIZED);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800234 return 0;
235 }
Robert Sloan572a4e22017-04-17 10:52:19 -0700236 return ctx->pmeth->sign(ctx, sig, sig_len, digest, digest_len);
237}
238
Adam Langleyd9e397b2015-01-22 14:27:53 -0800239int EVP_PKEY_verify_init(EVP_PKEY_CTX *ctx) {
Robert Sloan572a4e22017-04-17 10:52:19 -0700240 if (ctx == NULL || ctx->pmeth == NULL ||
241 (ctx->pmeth->verify == NULL && ctx->pmeth->verify_message == NULL)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000242 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800243 return 0;
244 }
245 ctx->operation = EVP_PKEY_OP_VERIFY;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800246 return 1;
247}
248
249int EVP_PKEY_verify(EVP_PKEY_CTX *ctx, const uint8_t *sig, size_t sig_len,
Robert Sloan572a4e22017-04-17 10:52:19 -0700250 const uint8_t *digest, size_t digest_len) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800251 if (!ctx || !ctx->pmeth || !ctx->pmeth->verify) {
Kenny Rootb8494592015-09-25 02:29:14 +0000252 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800253 return 0;
254 }
255 if (ctx->operation != EVP_PKEY_OP_VERIFY) {
Kenny Rootb8494592015-09-25 02:29:14 +0000256 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATON_NOT_INITIALIZED);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800257 return 0;
258 }
Robert Sloan572a4e22017-04-17 10:52:19 -0700259 return ctx->pmeth->verify(ctx, sig, sig_len, digest, digest_len);
260}
261
Adam Langleyd9e397b2015-01-22 14:27:53 -0800262int EVP_PKEY_encrypt_init(EVP_PKEY_CTX *ctx) {
263 if (!ctx || !ctx->pmeth || !ctx->pmeth->encrypt) {
Kenny Rootb8494592015-09-25 02:29:14 +0000264 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800265 return 0;
266 }
267 ctx->operation = EVP_PKEY_OP_ENCRYPT;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800268 return 1;
269}
270
271int EVP_PKEY_encrypt(EVP_PKEY_CTX *ctx, uint8_t *out, size_t *outlen,
272 const uint8_t *in, size_t inlen) {
273 if (!ctx || !ctx->pmeth || !ctx->pmeth->encrypt) {
Kenny Rootb8494592015-09-25 02:29:14 +0000274 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800275 return 0;
276 }
277 if (ctx->operation != EVP_PKEY_OP_ENCRYPT) {
Kenny Rootb8494592015-09-25 02:29:14 +0000278 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATON_NOT_INITIALIZED);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800279 return 0;
280 }
281 return ctx->pmeth->encrypt(ctx, out, outlen, in, inlen);
282}
283
284int EVP_PKEY_decrypt_init(EVP_PKEY_CTX *ctx) {
285 if (!ctx || !ctx->pmeth || !ctx->pmeth->decrypt) {
Kenny Rootb8494592015-09-25 02:29:14 +0000286 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800287 return 0;
288 }
289 ctx->operation = EVP_PKEY_OP_DECRYPT;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800290 return 1;
291}
292
293int EVP_PKEY_decrypt(EVP_PKEY_CTX *ctx, uint8_t *out, size_t *outlen,
294 const uint8_t *in, size_t inlen) {
295 if (!ctx || !ctx->pmeth || !ctx->pmeth->decrypt) {
Kenny Rootb8494592015-09-25 02:29:14 +0000296 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800297 return 0;
298 }
299 if (ctx->operation != EVP_PKEY_OP_DECRYPT) {
Kenny Rootb8494592015-09-25 02:29:14 +0000300 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATON_NOT_INITIALIZED);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800301 return 0;
302 }
303 return ctx->pmeth->decrypt(ctx, out, outlen, in, inlen);
304}
305
David Benjamin4969cc92016-04-22 15:02:23 -0400306int EVP_PKEY_verify_recover_init(EVP_PKEY_CTX *ctx) {
307 if (!ctx || !ctx->pmeth || !ctx->pmeth->verify_recover) {
308 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
309 return 0;
310 }
311 ctx->operation = EVP_PKEY_OP_VERIFYRECOVER;
312 return 1;
313}
314
315int EVP_PKEY_verify_recover(EVP_PKEY_CTX *ctx, uint8_t *out, size_t *out_len,
316 const uint8_t *sig, size_t sig_len) {
317 if (!ctx || !ctx->pmeth || !ctx->pmeth->verify_recover) {
318 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
319 return 0;
320 }
321 if (ctx->operation != EVP_PKEY_OP_VERIFYRECOVER) {
322 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATON_NOT_INITIALIZED);
323 return 0;
324 }
325 return ctx->pmeth->verify_recover(ctx, out, out_len, sig, sig_len);
326}
327
Adam Langleyd9e397b2015-01-22 14:27:53 -0800328int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx) {
329 if (!ctx || !ctx->pmeth || !ctx->pmeth->derive) {
Kenny Rootb8494592015-09-25 02:29:14 +0000330 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800331 return 0;
332 }
333 ctx->operation = EVP_PKEY_OP_DERIVE;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800334 return 1;
335}
336
337int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer) {
338 int ret;
339 if (!ctx || !ctx->pmeth ||
340 !(ctx->pmeth->derive || ctx->pmeth->encrypt || ctx->pmeth->decrypt) ||
341 !ctx->pmeth->ctrl) {
Kenny Rootb8494592015-09-25 02:29:14 +0000342 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800343 return 0;
344 }
345 if (ctx->operation != EVP_PKEY_OP_DERIVE &&
346 ctx->operation != EVP_PKEY_OP_ENCRYPT &&
347 ctx->operation != EVP_PKEY_OP_DECRYPT) {
Kenny Rootb8494592015-09-25 02:29:14 +0000348 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATON_NOT_INITIALIZED);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800349 return 0;
350 }
351
352 ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 0, peer);
353
354 if (ret <= 0) {
355 return 0;
356 }
357
358 if (ret == 2) {
359 return 1;
360 }
361
362 if (!ctx->pkey) {
Kenny Rootb8494592015-09-25 02:29:14 +0000363 OPENSSL_PUT_ERROR(EVP, EVP_R_NO_KEY_SET);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800364 return 0;
365 }
366
367 if (ctx->pkey->type != peer->type) {
Kenny Rootb8494592015-09-25 02:29:14 +0000368 OPENSSL_PUT_ERROR(EVP, EVP_R_DIFFERENT_KEY_TYPES);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800369 return 0;
370 }
371
372 /* ran@cryptocom.ru: For clarity. The error is if parameters in peer are
373 * present (!missing) but don't match. EVP_PKEY_cmp_parameters may return
374 * 1 (match), 0 (don't match) and -2 (comparison is not defined). -1
375 * (different key types) is impossible here because it is checked earlier.
376 * -2 is OK for us here, as well as 1, so we can check for 0 only. */
377 if (!EVP_PKEY_missing_parameters(peer) &&
378 !EVP_PKEY_cmp_parameters(ctx->pkey, peer)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000379 OPENSSL_PUT_ERROR(EVP, EVP_R_DIFFERENT_PARAMETERS);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800380 return 0;
381 }
382
Adam Langleye9ada862015-05-11 17:20:37 -0700383 EVP_PKEY_free(ctx->peerkey);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800384 ctx->peerkey = peer;
385
386 ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 1, peer);
387
388 if (ret <= 0) {
389 ctx->peerkey = NULL;
390 return 0;
391 }
392
Adam Langleye9ada862015-05-11 17:20:37 -0700393 EVP_PKEY_up_ref(peer);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800394 return 1;
395}
396
397int EVP_PKEY_derive(EVP_PKEY_CTX *ctx, uint8_t *key, size_t *out_key_len) {
398 if (!ctx || !ctx->pmeth || !ctx->pmeth->derive) {
Kenny Rootb8494592015-09-25 02:29:14 +0000399 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800400 return 0;
401 }
402 if (ctx->operation != EVP_PKEY_OP_DERIVE) {
Kenny Rootb8494592015-09-25 02:29:14 +0000403 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATON_NOT_INITIALIZED);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800404 return 0;
405 }
406 return ctx->pmeth->derive(ctx, key, out_key_len);
407}
408
409int EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx) {
410 if (!ctx || !ctx->pmeth || !ctx->pmeth->keygen) {
Kenny Rootb8494592015-09-25 02:29:14 +0000411 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800412 return 0;
413 }
414 ctx->operation = EVP_PKEY_OP_KEYGEN;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800415 return 1;
416}
417
418int EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey) {
419 if (!ctx || !ctx->pmeth || !ctx->pmeth->keygen) {
Kenny Rootb8494592015-09-25 02:29:14 +0000420 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800421 return 0;
422 }
423 if (ctx->operation != EVP_PKEY_OP_KEYGEN) {
Kenny Rootb8494592015-09-25 02:29:14 +0000424 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATON_NOT_INITIALIZED);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800425 return 0;
426 }
427
428 if (!ppkey) {
429 return 0;
430 }
431
432 if (!*ppkey) {
433 *ppkey = EVP_PKEY_new();
434 if (!*ppkey) {
Kenny Rootb8494592015-09-25 02:29:14 +0000435 OPENSSL_PUT_ERROR(EVP, ERR_LIB_EVP);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800436 return 0;
437 }
438 }
439
440 if (!ctx->pmeth->keygen(ctx, *ppkey)) {
441 EVP_PKEY_free(*ppkey);
442 *ppkey = NULL;
443 return 0;
444 }
445 return 1;
446}