blob: 9ca2c55815a55c9d5187269065c306ff8a38db9a [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[] = {
Pete Bentley0c61efe2019-08-13 09:32:23 +010070 &rsa_pkey_meth,
71 &ec_pkey_meth,
72 &ed25519_pkey_meth,
73 &x25519_pkey_meth,
Adam Langleyd9e397b2015-01-22 14:27:53 -080074};
75
76static const EVP_PKEY_METHOD *evp_pkey_meth_find(int type) {
Pete Bentley0c61efe2019-08-13 09:32:23 +010077 for (size_t i = 0; i < sizeof(evp_methods)/sizeof(EVP_PKEY_METHOD*); i++) {
Adam Langleyd9e397b2015-01-22 14:27:53 -080078 if (evp_methods[i]->pkey_id == type) {
79 return evp_methods[i];
80 }
81 }
82
83 return NULL;
84}
85
86static EVP_PKEY_CTX *evp_pkey_ctx_new(EVP_PKEY *pkey, ENGINE *e, int id) {
87 EVP_PKEY_CTX *ret;
88 const EVP_PKEY_METHOD *pmeth;
89
90 if (id == -1) {
91 if (!pkey || !pkey->ameth) {
92 return NULL;
93 }
94 id = pkey->ameth->pkey_id;
95 }
96
97 pmeth = evp_pkey_meth_find(id);
98
99 if (pmeth == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +0000100 OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM);
David Benjamin4969cc92016-04-22 15:02:23 -0400101 ERR_add_error_dataf("algorithm %d", id);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800102 return NULL;
103 }
104
105 ret = OPENSSL_malloc(sizeof(EVP_PKEY_CTX));
106 if (!ret) {
Kenny Rootb8494592015-09-25 02:29:14 +0000107 OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800108 return NULL;
109 }
Robert Sloan69939df2017-01-09 10:53:07 -0800110 OPENSSL_memset(ret, 0, sizeof(EVP_PKEY_CTX));
Adam Langleyd9e397b2015-01-22 14:27:53 -0800111
112 ret->engine = e;
113 ret->pmeth = pmeth;
114 ret->operation = EVP_PKEY_OP_UNDEFINED;
115
116 if (pkey) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400117 EVP_PKEY_up_ref(pkey);
118 ret->pkey = pkey;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800119 }
120
121 if (pmeth->init) {
122 if (pmeth->init(ret) <= 0) {
Adam Langleye9ada862015-05-11 17:20:37 -0700123 EVP_PKEY_free(ret->pkey);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800124 OPENSSL_free(ret);
125 return NULL;
126 }
127 }
128
129 return ret;
130}
131
132EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e) {
133 return evp_pkey_ctx_new(pkey, e, -1);
134}
135
136EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e) {
137 return evp_pkey_ctx_new(NULL, e, id);
138}
139
140void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx) {
141 if (ctx == NULL) {
142 return;
143 }
144 if (ctx->pmeth && ctx->pmeth->cleanup) {
145 ctx->pmeth->cleanup(ctx);
146 }
Adam Langleye9ada862015-05-11 17:20:37 -0700147 EVP_PKEY_free(ctx->pkey);
148 EVP_PKEY_free(ctx->peerkey);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800149 OPENSSL_free(ctx);
150}
151
Robert Sloana94fe052017-02-21 08:49:28 -0800152EVP_PKEY_CTX *EVP_PKEY_CTX_dup(EVP_PKEY_CTX *ctx) {
153 if (!ctx->pmeth || !ctx->pmeth->copy) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800154 return NULL;
155 }
156
Robert Sloana94fe052017-02-21 08:49:28 -0800157 EVP_PKEY_CTX *ret = OPENSSL_malloc(sizeof(EVP_PKEY_CTX));
158 if (!ret) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800159 return NULL;
160 }
161
Robert Sloana94fe052017-02-21 08:49:28 -0800162 OPENSSL_memset(ret, 0, sizeof(EVP_PKEY_CTX));
Adam Langleyd9e397b2015-01-22 14:27:53 -0800163
Robert Sloana94fe052017-02-21 08:49:28 -0800164 ret->pmeth = ctx->pmeth;
165 ret->engine = ctx->engine;
166 ret->operation = ctx->operation;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800167
Robert Sloana94fe052017-02-21 08:49:28 -0800168 if (ctx->pkey != NULL) {
169 EVP_PKEY_up_ref(ctx->pkey);
170 ret->pkey = ctx->pkey;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800171 }
172
Robert Sloana94fe052017-02-21 08:49:28 -0800173 if (ctx->peerkey != NULL) {
174 EVP_PKEY_up_ref(ctx->peerkey);
175 ret->peerkey = ctx->peerkey;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800176 }
177
Robert Sloana94fe052017-02-21 08:49:28 -0800178 if (ctx->pmeth->copy(ret, ctx) <= 0) {
179 ret->pmeth = NULL;
180 EVP_PKEY_CTX_free(ret);
181 OPENSSL_PUT_ERROR(EVP, ERR_LIB_EVP);
182 return NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800183 }
184
Robert Sloana94fe052017-02-21 08:49:28 -0800185 return ret;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800186}
187
188EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx) { return ctx->pkey; }
189
Adam Langleyd9e397b2015-01-22 14:27:53 -0800190int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype, int cmd,
191 int p1, void *p2) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800192 if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl) {
Kenny Rootb8494592015-09-25 02:29:14 +0000193 OPENSSL_PUT_ERROR(EVP, EVP_R_COMMAND_NOT_SUPPORTED);
Adam Langleye9ada862015-05-11 17:20:37 -0700194 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800195 }
196 if (keytype != -1 && ctx->pmeth->pkey_id != keytype) {
Robert Sloan4d1ac502017-02-06 08:36:14 -0800197 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
Adam Langleye9ada862015-05-11 17:20:37 -0700198 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800199 }
200
201 if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
Kenny Rootb8494592015-09-25 02:29:14 +0000202 OPENSSL_PUT_ERROR(EVP, EVP_R_NO_OPERATION_SET);
Adam Langleye9ada862015-05-11 17:20:37 -0700203 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800204 }
205
206 if (optype != -1 && !(ctx->operation & optype)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000207 OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_OPERATION);
Adam Langleye9ada862015-05-11 17:20:37 -0700208 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800209 }
210
Adam Langleye9ada862015-05-11 17:20:37 -0700211 return ctx->pmeth->ctrl(ctx, cmd, p1, p2);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800212}
213
214int EVP_PKEY_sign_init(EVP_PKEY_CTX *ctx) {
Robert Sloan572a4e22017-04-17 10:52:19 -0700215 if (ctx == NULL || ctx->pmeth == NULL ||
216 (ctx->pmeth->sign == NULL && ctx->pmeth->sign_message == NULL)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000217 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800218 return 0;
219 }
220
221 ctx->operation = EVP_PKEY_OP_SIGN;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800222 return 1;
223}
224
225int EVP_PKEY_sign(EVP_PKEY_CTX *ctx, uint8_t *sig, size_t *sig_len,
Robert Sloan572a4e22017-04-17 10:52:19 -0700226 const uint8_t *digest, size_t digest_len) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800227 if (!ctx || !ctx->pmeth || !ctx->pmeth->sign) {
Kenny Rootb8494592015-09-25 02:29:14 +0000228 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800229 return 0;
230 }
231 if (ctx->operation != EVP_PKEY_OP_SIGN) {
Kenny Rootb8494592015-09-25 02:29:14 +0000232 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATON_NOT_INITIALIZED);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800233 return 0;
234 }
Robert Sloan572a4e22017-04-17 10:52:19 -0700235 return ctx->pmeth->sign(ctx, sig, sig_len, digest, digest_len);
236}
237
Adam Langleyd9e397b2015-01-22 14:27:53 -0800238int EVP_PKEY_verify_init(EVP_PKEY_CTX *ctx) {
Robert Sloan572a4e22017-04-17 10:52:19 -0700239 if (ctx == NULL || ctx->pmeth == NULL ||
240 (ctx->pmeth->verify == NULL && ctx->pmeth->verify_message == NULL)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000241 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800242 return 0;
243 }
244 ctx->operation = EVP_PKEY_OP_VERIFY;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800245 return 1;
246}
247
248int EVP_PKEY_verify(EVP_PKEY_CTX *ctx, const uint8_t *sig, size_t sig_len,
Robert Sloan572a4e22017-04-17 10:52:19 -0700249 const uint8_t *digest, size_t digest_len) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800250 if (!ctx || !ctx->pmeth || !ctx->pmeth->verify) {
Kenny Rootb8494592015-09-25 02:29:14 +0000251 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800252 return 0;
253 }
254 if (ctx->operation != EVP_PKEY_OP_VERIFY) {
Kenny Rootb8494592015-09-25 02:29:14 +0000255 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATON_NOT_INITIALIZED);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800256 return 0;
257 }
Robert Sloan572a4e22017-04-17 10:52:19 -0700258 return ctx->pmeth->verify(ctx, sig, sig_len, digest, digest_len);
259}
260
Adam Langleyd9e397b2015-01-22 14:27:53 -0800261int EVP_PKEY_encrypt_init(EVP_PKEY_CTX *ctx) {
262 if (!ctx || !ctx->pmeth || !ctx->pmeth->encrypt) {
Kenny Rootb8494592015-09-25 02:29:14 +0000263 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800264 return 0;
265 }
266 ctx->operation = EVP_PKEY_OP_ENCRYPT;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800267 return 1;
268}
269
270int EVP_PKEY_encrypt(EVP_PKEY_CTX *ctx, uint8_t *out, size_t *outlen,
271 const uint8_t *in, size_t inlen) {
272 if (!ctx || !ctx->pmeth || !ctx->pmeth->encrypt) {
Kenny Rootb8494592015-09-25 02:29:14 +0000273 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800274 return 0;
275 }
276 if (ctx->operation != EVP_PKEY_OP_ENCRYPT) {
Kenny Rootb8494592015-09-25 02:29:14 +0000277 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATON_NOT_INITIALIZED);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800278 return 0;
279 }
280 return ctx->pmeth->encrypt(ctx, out, outlen, in, inlen);
281}
282
283int EVP_PKEY_decrypt_init(EVP_PKEY_CTX *ctx) {
284 if (!ctx || !ctx->pmeth || !ctx->pmeth->decrypt) {
Kenny Rootb8494592015-09-25 02:29:14 +0000285 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800286 return 0;
287 }
288 ctx->operation = EVP_PKEY_OP_DECRYPT;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800289 return 1;
290}
291
292int EVP_PKEY_decrypt(EVP_PKEY_CTX *ctx, uint8_t *out, size_t *outlen,
293 const uint8_t *in, size_t inlen) {
294 if (!ctx || !ctx->pmeth || !ctx->pmeth->decrypt) {
Kenny Rootb8494592015-09-25 02:29:14 +0000295 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800296 return 0;
297 }
298 if (ctx->operation != EVP_PKEY_OP_DECRYPT) {
Kenny Rootb8494592015-09-25 02:29:14 +0000299 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATON_NOT_INITIALIZED);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800300 return 0;
301 }
302 return ctx->pmeth->decrypt(ctx, out, outlen, in, inlen);
303}
304
David Benjamin4969cc92016-04-22 15:02:23 -0400305int EVP_PKEY_verify_recover_init(EVP_PKEY_CTX *ctx) {
306 if (!ctx || !ctx->pmeth || !ctx->pmeth->verify_recover) {
307 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
308 return 0;
309 }
310 ctx->operation = EVP_PKEY_OP_VERIFYRECOVER;
311 return 1;
312}
313
314int EVP_PKEY_verify_recover(EVP_PKEY_CTX *ctx, uint8_t *out, size_t *out_len,
315 const uint8_t *sig, size_t sig_len) {
316 if (!ctx || !ctx->pmeth || !ctx->pmeth->verify_recover) {
317 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
318 return 0;
319 }
320 if (ctx->operation != EVP_PKEY_OP_VERIFYRECOVER) {
321 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATON_NOT_INITIALIZED);
322 return 0;
323 }
324 return ctx->pmeth->verify_recover(ctx, out, out_len, sig, sig_len);
325}
326
Adam Langleyd9e397b2015-01-22 14:27:53 -0800327int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx) {
328 if (!ctx || !ctx->pmeth || !ctx->pmeth->derive) {
Kenny Rootb8494592015-09-25 02:29:14 +0000329 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800330 return 0;
331 }
332 ctx->operation = EVP_PKEY_OP_DERIVE;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800333 return 1;
334}
335
336int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer) {
337 int ret;
338 if (!ctx || !ctx->pmeth ||
339 !(ctx->pmeth->derive || ctx->pmeth->encrypt || ctx->pmeth->decrypt) ||
340 !ctx->pmeth->ctrl) {
Kenny Rootb8494592015-09-25 02:29:14 +0000341 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800342 return 0;
343 }
344 if (ctx->operation != EVP_PKEY_OP_DERIVE &&
345 ctx->operation != EVP_PKEY_OP_ENCRYPT &&
346 ctx->operation != EVP_PKEY_OP_DECRYPT) {
Kenny Rootb8494592015-09-25 02:29:14 +0000347 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATON_NOT_INITIALIZED);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800348 return 0;
349 }
350
351 ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 0, peer);
352
353 if (ret <= 0) {
354 return 0;
355 }
356
357 if (ret == 2) {
358 return 1;
359 }
360
361 if (!ctx->pkey) {
Kenny Rootb8494592015-09-25 02:29:14 +0000362 OPENSSL_PUT_ERROR(EVP, EVP_R_NO_KEY_SET);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800363 return 0;
364 }
365
366 if (ctx->pkey->type != peer->type) {
Kenny Rootb8494592015-09-25 02:29:14 +0000367 OPENSSL_PUT_ERROR(EVP, EVP_R_DIFFERENT_KEY_TYPES);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800368 return 0;
369 }
370
Robert Sloan8f860b12017-08-28 07:37:06 -0700371 // ran@cryptocom.ru: For clarity. The error is if parameters in peer are
372 // present (!missing) but don't match. EVP_PKEY_cmp_parameters may return
373 // 1 (match), 0 (don't match) and -2 (comparison is not defined). -1
374 // (different key types) is impossible here because it is checked earlier.
375 // -2 is OK for us here, as well as 1, so we can check for 0 only.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800376 if (!EVP_PKEY_missing_parameters(peer) &&
377 !EVP_PKEY_cmp_parameters(ctx->pkey, peer)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000378 OPENSSL_PUT_ERROR(EVP, EVP_R_DIFFERENT_PARAMETERS);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800379 return 0;
380 }
381
Adam Langleye9ada862015-05-11 17:20:37 -0700382 EVP_PKEY_free(ctx->peerkey);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800383 ctx->peerkey = peer;
384
385 ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 1, peer);
386
387 if (ret <= 0) {
388 ctx->peerkey = NULL;
389 return 0;
390 }
391
Adam Langleye9ada862015-05-11 17:20:37 -0700392 EVP_PKEY_up_ref(peer);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800393 return 1;
394}
395
396int EVP_PKEY_derive(EVP_PKEY_CTX *ctx, uint8_t *key, size_t *out_key_len) {
397 if (!ctx || !ctx->pmeth || !ctx->pmeth->derive) {
Kenny Rootb8494592015-09-25 02:29:14 +0000398 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800399 return 0;
400 }
401 if (ctx->operation != EVP_PKEY_OP_DERIVE) {
Kenny Rootb8494592015-09-25 02:29:14 +0000402 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATON_NOT_INITIALIZED);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800403 return 0;
404 }
405 return ctx->pmeth->derive(ctx, key, out_key_len);
406}
407
408int EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx) {
409 if (!ctx || !ctx->pmeth || !ctx->pmeth->keygen) {
Kenny Rootb8494592015-09-25 02:29:14 +0000410 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800411 return 0;
412 }
413 ctx->operation = EVP_PKEY_OP_KEYGEN;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800414 return 1;
415}
416
Robert Sloan4c22c5f2019-03-01 15:53:37 -0800417int EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **out_pkey) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800418 if (!ctx || !ctx->pmeth || !ctx->pmeth->keygen) {
Kenny Rootb8494592015-09-25 02:29:14 +0000419 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800420 return 0;
421 }
422 if (ctx->operation != EVP_PKEY_OP_KEYGEN) {
Kenny Rootb8494592015-09-25 02:29:14 +0000423 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATON_NOT_INITIALIZED);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800424 return 0;
425 }
426
Robert Sloan4c22c5f2019-03-01 15:53:37 -0800427 if (!out_pkey) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800428 return 0;
429 }
430
Robert Sloan4c22c5f2019-03-01 15:53:37 -0800431 if (!*out_pkey) {
432 *out_pkey = EVP_PKEY_new();
433 if (!*out_pkey) {
Kenny Rootb8494592015-09-25 02:29:14 +0000434 OPENSSL_PUT_ERROR(EVP, ERR_LIB_EVP);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800435 return 0;
436 }
437 }
438
Robert Sloan4c22c5f2019-03-01 15:53:37 -0800439 if (!ctx->pmeth->keygen(ctx, *out_pkey)) {
440 EVP_PKEY_free(*out_pkey);
441 *out_pkey = NULL;
442 return 0;
443 }
444 return 1;
445}
446
447int EVP_PKEY_paramgen_init(EVP_PKEY_CTX *ctx) {
448 if (!ctx || !ctx->pmeth || !ctx->pmeth->paramgen) {
449 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
450 return 0;
451 }
452 ctx->operation = EVP_PKEY_OP_PARAMGEN;
453 return 1;
454}
455
456int EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **out_pkey) {
457 if (!ctx || !ctx->pmeth || !ctx->pmeth->paramgen) {
458 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
459 return 0;
460 }
461 if (ctx->operation != EVP_PKEY_OP_PARAMGEN) {
462 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATON_NOT_INITIALIZED);
463 return 0;
464 }
465
466 if (!out_pkey) {
467 return 0;
468 }
469
470 if (!*out_pkey) {
471 *out_pkey = EVP_PKEY_new();
472 if (!*out_pkey) {
473 OPENSSL_PUT_ERROR(EVP, ERR_LIB_EVP);
474 return 0;
475 }
476 }
477
478 if (!ctx->pmeth->paramgen(ctx, *out_pkey)) {
479 EVP_PKEY_free(*out_pkey);
480 *out_pkey = NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800481 return 0;
482 }
483 return 1;
484}