Deleted Added
full compact
pmeth_fn.c (238405) pmeth_fn.c (280297)
1/* pmeth_fn.c */
1/* pmeth_fn.c */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2006.
2/*
3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
4 * 2006.
4 */
5/* ====================================================================
6 * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
5 */
6/* ====================================================================
7 * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:

--- 37 unchanged lines hidden (view full) ---

59#include <stdio.h>
60#include <stdlib.h>
61#include "cryptlib.h"
62#include <openssl/objects.h>
63#include <openssl/evp.h>
64#include "evp_locl.h"
65
66#define M_check_autoarg(ctx, arg, arglen, err) \
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:

--- 37 unchanged lines hidden (view full) ---

60#include <stdio.h>
61#include <stdlib.h>
62#include "cryptlib.h"
63#include <openssl/objects.h>
64#include <openssl/evp.h>
65#include "evp_locl.h"
66
67#define M_check_autoarg(ctx, arg, arglen, err) \
67 if (ctx->pmeth->flags & EVP_PKEY_FLAG_AUTOARGLEN) \
68 { \
69 size_t pksize = (size_t)EVP_PKEY_size(ctx->pkey); \
70 if (!arg) \
71 { \
72 *arglen = pksize; \
73 return 1; \
74 } \
75 else if (*arglen < pksize) \
76 { \
77 EVPerr(err, EVP_R_BUFFER_TOO_SMALL); /*ckerr_ignore*/\
78 return 0; \
79 } \
80 }
68 if (ctx->pmeth->flags & EVP_PKEY_FLAG_AUTOARGLEN) \
69 { \
70 size_t pksize = (size_t)EVP_PKEY_size(ctx->pkey); \
71 if (!arg) \
72 { \
73 *arglen = pksize; \
74 return 1; \
75 } \
76 else if (*arglen < pksize) \
77 { \
78 EVPerr(err, EVP_R_BUFFER_TOO_SMALL); /*ckerr_ignore*/\
79 return 0; \
80 } \
81 }
81
82int EVP_PKEY_sign_init(EVP_PKEY_CTX *ctx)
82
83int EVP_PKEY_sign_init(EVP_PKEY_CTX *ctx)
83 {
84 int ret;
85 if (!ctx || !ctx->pmeth || !ctx->pmeth->sign)
86 {
87 EVPerr(EVP_F_EVP_PKEY_SIGN_INIT,
88 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
89 return -2;
90 }
91 ctx->operation = EVP_PKEY_OP_SIGN;
92 if (!ctx->pmeth->sign_init)
93 return 1;
94 ret = ctx->pmeth->sign_init(ctx);
95 if (ret <= 0)
96 ctx->operation = EVP_PKEY_OP_UNDEFINED;
97 return ret;
98 }
84{
85 int ret;
86 if (!ctx || !ctx->pmeth || !ctx->pmeth->sign) {
87 EVPerr(EVP_F_EVP_PKEY_SIGN_INIT,
88 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
89 return -2;
90 }
91 ctx->operation = EVP_PKEY_OP_SIGN;
92 if (!ctx->pmeth->sign_init)
93 return 1;
94 ret = ctx->pmeth->sign_init(ctx);
95 if (ret <= 0)
96 ctx->operation = EVP_PKEY_OP_UNDEFINED;
97 return ret;
98}
99
100int EVP_PKEY_sign(EVP_PKEY_CTX *ctx,
99
100int EVP_PKEY_sign(EVP_PKEY_CTX *ctx,
101 unsigned char *sig, size_t *siglen,
102 const unsigned char *tbs, size_t tbslen)
103 {
104 if (!ctx || !ctx->pmeth || !ctx->pmeth->sign)
105 {
106 EVPerr(EVP_F_EVP_PKEY_SIGN,
107 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
108 return -2;
109 }
110 if (ctx->operation != EVP_PKEY_OP_SIGN)
111 {
112 EVPerr(EVP_F_EVP_PKEY_SIGN, EVP_R_OPERATON_NOT_INITIALIZED);
113 return -1;
114 }
115 M_check_autoarg(ctx, sig, siglen, EVP_F_EVP_PKEY_SIGN)
116 return ctx->pmeth->sign(ctx, sig, siglen, tbs, tbslen);
117 }
101 unsigned char *sig, size_t *siglen,
102 const unsigned char *tbs, size_t tbslen)
103{
104 if (!ctx || !ctx->pmeth || !ctx->pmeth->sign) {
105 EVPerr(EVP_F_EVP_PKEY_SIGN,
106 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
107 return -2;
108 }
109 if (ctx->operation != EVP_PKEY_OP_SIGN) {
110 EVPerr(EVP_F_EVP_PKEY_SIGN, EVP_R_OPERATON_NOT_INITIALIZED);
111 return -1;
112 }
113 M_check_autoarg(ctx, sig, siglen, EVP_F_EVP_PKEY_SIGN)
114 return ctx->pmeth->sign(ctx, sig, siglen, tbs, tbslen);
115}
118
119int EVP_PKEY_verify_init(EVP_PKEY_CTX *ctx)
116
117int EVP_PKEY_verify_init(EVP_PKEY_CTX *ctx)
120 {
121 int ret;
122 if (!ctx || !ctx->pmeth || !ctx->pmeth->verify)
123 {
124 EVPerr(EVP_F_EVP_PKEY_VERIFY_INIT,
125 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
126 return -2;
127 }
128 ctx->operation = EVP_PKEY_OP_VERIFY;
129 if (!ctx->pmeth->verify_init)
130 return 1;
131 ret = ctx->pmeth->verify_init(ctx);
132 if (ret <= 0)
133 ctx->operation = EVP_PKEY_OP_UNDEFINED;
134 return ret;
135 }
118{
119 int ret;
120 if (!ctx || !ctx->pmeth || !ctx->pmeth->verify) {
121 EVPerr(EVP_F_EVP_PKEY_VERIFY_INIT,
122 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
123 return -2;
124 }
125 ctx->operation = EVP_PKEY_OP_VERIFY;
126 if (!ctx->pmeth->verify_init)
127 return 1;
128 ret = ctx->pmeth->verify_init(ctx);
129 if (ret <= 0)
130 ctx->operation = EVP_PKEY_OP_UNDEFINED;
131 return ret;
132}
136
137int EVP_PKEY_verify(EVP_PKEY_CTX *ctx,
133
134int EVP_PKEY_verify(EVP_PKEY_CTX *ctx,
138 const unsigned char *sig, size_t siglen,
139 const unsigned char *tbs, size_t tbslen)
140 {
141 if (!ctx || !ctx->pmeth || !ctx->pmeth->verify)
142 {
143 EVPerr(EVP_F_EVP_PKEY_VERIFY,
144 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
145 return -2;
146 }
147 if (ctx->operation != EVP_PKEY_OP_VERIFY)
148 {
149 EVPerr(EVP_F_EVP_PKEY_VERIFY, EVP_R_OPERATON_NOT_INITIALIZED);
150 return -1;
151 }
152 return ctx->pmeth->verify(ctx, sig, siglen, tbs, tbslen);
153 }
135 const unsigned char *sig, size_t siglen,
136 const unsigned char *tbs, size_t tbslen)
137{
138 if (!ctx || !ctx->pmeth || !ctx->pmeth->verify) {
139 EVPerr(EVP_F_EVP_PKEY_VERIFY,
140 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
141 return -2;
142 }
143 if (ctx->operation != EVP_PKEY_OP_VERIFY) {
144 EVPerr(EVP_F_EVP_PKEY_VERIFY, EVP_R_OPERATON_NOT_INITIALIZED);
145 return -1;
146 }
147 return ctx->pmeth->verify(ctx, sig, siglen, tbs, tbslen);
148}
154
155int EVP_PKEY_verify_recover_init(EVP_PKEY_CTX *ctx)
149
150int EVP_PKEY_verify_recover_init(EVP_PKEY_CTX *ctx)
156 {
157 int ret;
158 if (!ctx || !ctx->pmeth || !ctx->pmeth->verify_recover)
159 {
160 EVPerr(EVP_F_EVP_PKEY_VERIFY_RECOVER_INIT,
161 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
162 return -2;
163 }
164 ctx->operation = EVP_PKEY_OP_VERIFYRECOVER;
165 if (!ctx->pmeth->verify_recover_init)
166 return 1;
167 ret = ctx->pmeth->verify_recover_init(ctx);
168 if (ret <= 0)
169 ctx->operation = EVP_PKEY_OP_UNDEFINED;
170 return ret;
171 }
151{
152 int ret;
153 if (!ctx || !ctx->pmeth || !ctx->pmeth->verify_recover) {
154 EVPerr(EVP_F_EVP_PKEY_VERIFY_RECOVER_INIT,
155 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
156 return -2;
157 }
158 ctx->operation = EVP_PKEY_OP_VERIFYRECOVER;
159 if (!ctx->pmeth->verify_recover_init)
160 return 1;
161 ret = ctx->pmeth->verify_recover_init(ctx);
162 if (ret <= 0)
163 ctx->operation = EVP_PKEY_OP_UNDEFINED;
164 return ret;
165}
172
173int EVP_PKEY_verify_recover(EVP_PKEY_CTX *ctx,
166
167int EVP_PKEY_verify_recover(EVP_PKEY_CTX *ctx,
174 unsigned char *rout, size_t *routlen,
175 const unsigned char *sig, size_t siglen)
176 {
177 if (!ctx || !ctx->pmeth || !ctx->pmeth->verify_recover)
178 {
179 EVPerr(EVP_F_EVP_PKEY_VERIFY_RECOVER,
180 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
181 return -2;
182 }
183 if (ctx->operation != EVP_PKEY_OP_VERIFYRECOVER)
184 {
185 EVPerr(EVP_F_EVP_PKEY_VERIFY_RECOVER, EVP_R_OPERATON_NOT_INITIALIZED);
186 return -1;
187 }
188 M_check_autoarg(ctx, rout, routlen, EVP_F_EVP_PKEY_VERIFY_RECOVER)
189 return ctx->pmeth->verify_recover(ctx, rout, routlen, sig, siglen);
190 }
168 unsigned char *rout, size_t *routlen,
169 const unsigned char *sig, size_t siglen)
170{
171 if (!ctx || !ctx->pmeth || !ctx->pmeth->verify_recover) {
172 EVPerr(EVP_F_EVP_PKEY_VERIFY_RECOVER,
173 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
174 return -2;
175 }
176 if (ctx->operation != EVP_PKEY_OP_VERIFYRECOVER) {
177 EVPerr(EVP_F_EVP_PKEY_VERIFY_RECOVER, EVP_R_OPERATON_NOT_INITIALIZED);
178 return -1;
179 }
180 M_check_autoarg(ctx, rout, routlen, EVP_F_EVP_PKEY_VERIFY_RECOVER)
181 return ctx->pmeth->verify_recover(ctx, rout, routlen, sig, siglen);
182}
191
192int EVP_PKEY_encrypt_init(EVP_PKEY_CTX *ctx)
183
184int EVP_PKEY_encrypt_init(EVP_PKEY_CTX *ctx)
193 {
194 int ret;
195 if (!ctx || !ctx->pmeth || !ctx->pmeth->encrypt)
196 {
197 EVPerr(EVP_F_EVP_PKEY_ENCRYPT_INIT,
198 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
199 return -2;
200 }
201 ctx->operation = EVP_PKEY_OP_ENCRYPT;
202 if (!ctx->pmeth->encrypt_init)
203 return 1;
204 ret = ctx->pmeth->encrypt_init(ctx);
205 if (ret <= 0)
206 ctx->operation = EVP_PKEY_OP_UNDEFINED;
207 return ret;
208 }
185{
186 int ret;
187 if (!ctx || !ctx->pmeth || !ctx->pmeth->encrypt) {
188 EVPerr(EVP_F_EVP_PKEY_ENCRYPT_INIT,
189 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
190 return -2;
191 }
192 ctx->operation = EVP_PKEY_OP_ENCRYPT;
193 if (!ctx->pmeth->encrypt_init)
194 return 1;
195 ret = ctx->pmeth->encrypt_init(ctx);
196 if (ret <= 0)
197 ctx->operation = EVP_PKEY_OP_UNDEFINED;
198 return ret;
199}
209
210int EVP_PKEY_encrypt(EVP_PKEY_CTX *ctx,
200
201int EVP_PKEY_encrypt(EVP_PKEY_CTX *ctx,
211 unsigned char *out, size_t *outlen,
212 const unsigned char *in, size_t inlen)
213 {
214 if (!ctx || !ctx->pmeth || !ctx->pmeth->encrypt)
215 {
216 EVPerr(EVP_F_EVP_PKEY_ENCRYPT,
217 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
218 return -2;
219 }
220 if (ctx->operation != EVP_PKEY_OP_ENCRYPT)
221 {
222 EVPerr(EVP_F_EVP_PKEY_ENCRYPT, EVP_R_OPERATON_NOT_INITIALIZED);
223 return -1;
224 }
225 M_check_autoarg(ctx, out, outlen, EVP_F_EVP_PKEY_ENCRYPT)
226 return ctx->pmeth->encrypt(ctx, out, outlen, in, inlen);
227 }
202 unsigned char *out, size_t *outlen,
203 const unsigned char *in, size_t inlen)
204{
205 if (!ctx || !ctx->pmeth || !ctx->pmeth->encrypt) {
206 EVPerr(EVP_F_EVP_PKEY_ENCRYPT,
207 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
208 return -2;
209 }
210 if (ctx->operation != EVP_PKEY_OP_ENCRYPT) {
211 EVPerr(EVP_F_EVP_PKEY_ENCRYPT, EVP_R_OPERATON_NOT_INITIALIZED);
212 return -1;
213 }
214 M_check_autoarg(ctx, out, outlen, EVP_F_EVP_PKEY_ENCRYPT)
215 return ctx->pmeth->encrypt(ctx, out, outlen, in, inlen);
216}
228
229int EVP_PKEY_decrypt_init(EVP_PKEY_CTX *ctx)
217
218int EVP_PKEY_decrypt_init(EVP_PKEY_CTX *ctx)
230 {
231 int ret;
232 if (!ctx || !ctx->pmeth || !ctx->pmeth->decrypt)
233 {
234 EVPerr(EVP_F_EVP_PKEY_DECRYPT_INIT,
235 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
236 return -2;
237 }
238 ctx->operation = EVP_PKEY_OP_DECRYPT;
239 if (!ctx->pmeth->decrypt_init)
240 return 1;
241 ret = ctx->pmeth->decrypt_init(ctx);
242 if (ret <= 0)
243 ctx->operation = EVP_PKEY_OP_UNDEFINED;
244 return ret;
245 }
219{
220 int ret;
221 if (!ctx || !ctx->pmeth || !ctx->pmeth->decrypt) {
222 EVPerr(EVP_F_EVP_PKEY_DECRYPT_INIT,
223 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
224 return -2;
225 }
226 ctx->operation = EVP_PKEY_OP_DECRYPT;
227 if (!ctx->pmeth->decrypt_init)
228 return 1;
229 ret = ctx->pmeth->decrypt_init(ctx);
230 if (ret <= 0)
231 ctx->operation = EVP_PKEY_OP_UNDEFINED;
232 return ret;
233}
246
247int EVP_PKEY_decrypt(EVP_PKEY_CTX *ctx,
234
235int EVP_PKEY_decrypt(EVP_PKEY_CTX *ctx,
248 unsigned char *out, size_t *outlen,
249 const unsigned char *in, size_t inlen)
250 {
251 if (!ctx || !ctx->pmeth || !ctx->pmeth->decrypt)
252 {
253 EVPerr(EVP_F_EVP_PKEY_DECRYPT,
254 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
255 return -2;
256 }
257 if (ctx->operation != EVP_PKEY_OP_DECRYPT)
258 {
259 EVPerr(EVP_F_EVP_PKEY_DECRYPT, EVP_R_OPERATON_NOT_INITIALIZED);
260 return -1;
261 }
262 M_check_autoarg(ctx, out, outlen, EVP_F_EVP_PKEY_DECRYPT)
263 return ctx->pmeth->decrypt(ctx, out, outlen, in, inlen);
264 }
236 unsigned char *out, size_t *outlen,
237 const unsigned char *in, size_t inlen)
238{
239 if (!ctx || !ctx->pmeth || !ctx->pmeth->decrypt) {
240 EVPerr(EVP_F_EVP_PKEY_DECRYPT,
241 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
242 return -2;
243 }
244 if (ctx->operation != EVP_PKEY_OP_DECRYPT) {
245 EVPerr(EVP_F_EVP_PKEY_DECRYPT, EVP_R_OPERATON_NOT_INITIALIZED);
246 return -1;
247 }
248 M_check_autoarg(ctx, out, outlen, EVP_F_EVP_PKEY_DECRYPT)
249 return ctx->pmeth->decrypt(ctx, out, outlen, in, inlen);
250}
265
251
266
267int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx)
252int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx)
268 {
269 int ret;
270 if (!ctx || !ctx->pmeth || !ctx->pmeth->derive)
271 {
272 EVPerr(EVP_F_EVP_PKEY_DERIVE_INIT,
273 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
274 return -2;
275 }
276 ctx->operation = EVP_PKEY_OP_DERIVE;
277 if (!ctx->pmeth->derive_init)
278 return 1;
279 ret = ctx->pmeth->derive_init(ctx);
280 if (ret <= 0)
281 ctx->operation = EVP_PKEY_OP_UNDEFINED;
282 return ret;
283 }
253{
254 int ret;
255 if (!ctx || !ctx->pmeth || !ctx->pmeth->derive) {
256 EVPerr(EVP_F_EVP_PKEY_DERIVE_INIT,
257 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
258 return -2;
259 }
260 ctx->operation = EVP_PKEY_OP_DERIVE;
261 if (!ctx->pmeth->derive_init)
262 return 1;
263 ret = ctx->pmeth->derive_init(ctx);
264 if (ret <= 0)
265 ctx->operation = EVP_PKEY_OP_UNDEFINED;
266 return ret;
267}
284
285int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer)
268
269int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer)
286 {
287 int ret;
288 if (!ctx || !ctx->pmeth || !(ctx->pmeth->derive||ctx->pmeth->encrypt||ctx->pmeth->decrypt) || !ctx->pmeth->ctrl)
289 {
290 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
291 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
292 return -2;
293 }
294 if (ctx->operation != EVP_PKEY_OP_DERIVE && ctx->operation != EVP_PKEY_OP_ENCRYPT && ctx->operation != EVP_PKEY_OP_DECRYPT)
295 {
296 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
297 EVP_R_OPERATON_NOT_INITIALIZED);
298 return -1;
299 }
270{
271 int ret;
272 if (!ctx || !ctx->pmeth
273 || !(ctx->pmeth->derive || ctx->pmeth->encrypt || ctx->pmeth->decrypt)
274 || !ctx->pmeth->ctrl) {
275 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
276 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
277 return -2;
278 }
279 if (ctx->operation != EVP_PKEY_OP_DERIVE
280 && ctx->operation != EVP_PKEY_OP_ENCRYPT
281 && ctx->operation != EVP_PKEY_OP_DECRYPT) {
282 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
283 EVP_R_OPERATON_NOT_INITIALIZED);
284 return -1;
285 }
300
286
301 ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 0, peer);
287 ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 0, peer);
302
288
303 if (ret <= 0)
304 return ret;
289 if (ret <= 0)
290 return ret;
305
291
306 if (ret == 2)
307 return 1;
292 if (ret == 2)
293 return 1;
308
294
309 if (!ctx->pkey)
310 {
311 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, EVP_R_NO_KEY_SET);
312 return -1;
313 }
295 if (!ctx->pkey) {
296 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, EVP_R_NO_KEY_SET);
297 return -1;
298 }
314
299
315 if (ctx->pkey->type != peer->type)
316 {
317 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
318 EVP_R_DIFFERENT_KEY_TYPES);
319 return -1;
320 }
300 if (ctx->pkey->type != peer->type) {
301 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, EVP_R_DIFFERENT_KEY_TYPES);
302 return -1;
303 }
321
304
322 /* ran@cryptocom.ru: For clarity. The error is if parameters in peer are
323 * present (!missing) but don't match. EVP_PKEY_cmp_parameters may return
324 * 1 (match), 0 (don't match) and -2 (comparison is not defined). -1
325 * (different key types) is impossible here because it is checked earlier.
326 * -2 is OK for us here, as well as 1, so we can check for 0 only. */
327 if (!EVP_PKEY_missing_parameters(peer) &&
328 !EVP_PKEY_cmp_parameters(ctx->pkey, peer))
329 {
330 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
331 EVP_R_DIFFERENT_PARAMETERS);
332 return -1;
333 }
305 /*
306 * ran@cryptocom.ru: For clarity. The error is if parameters in peer are
307 * present (!missing) but don't match. EVP_PKEY_cmp_parameters may return
308 * 1 (match), 0 (don't match) and -2 (comparison is not defined). -1
309 * (different key types) is impossible here because it is checked earlier.
310 * -2 is OK for us here, as well as 1, so we can check for 0 only.
311 */
312 if (!EVP_PKEY_missing_parameters(peer) &&
313 !EVP_PKEY_cmp_parameters(ctx->pkey, peer)) {
314 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, EVP_R_DIFFERENT_PARAMETERS);
315 return -1;
316 }
334
317
335 if (ctx->peerkey)
336 EVP_PKEY_free(ctx->peerkey);
337 ctx->peerkey = peer;
318 if (ctx->peerkey)
319 EVP_PKEY_free(ctx->peerkey);
320 ctx->peerkey = peer;
338
321
339 ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 1, peer);
322 ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 1, peer);
340
323
341 if (ret <= 0)
342 {
343 ctx->peerkey = NULL;
344 return ret;
345 }
324 if (ret <= 0) {
325 ctx->peerkey = NULL;
326 return ret;
327 }
346
328
347 CRYPTO_add(&peer->references,1,CRYPTO_LOCK_EVP_PKEY);
348 return 1;
349 }
329 CRYPTO_add(&peer->references, 1, CRYPTO_LOCK_EVP_PKEY);
330 return 1;
331}
350
332
351
352int EVP_PKEY_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *pkeylen)
333int EVP_PKEY_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *pkeylen)
353 {
354 if (!ctx || !ctx->pmeth || !ctx->pmeth->derive)
355 {
356 EVPerr(EVP_F_EVP_PKEY_DERIVE,
357 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
358 return -2;
359 }
360 if (ctx->operation != EVP_PKEY_OP_DERIVE)
361 {
362 EVPerr(EVP_F_EVP_PKEY_DERIVE, EVP_R_OPERATON_NOT_INITIALIZED);
363 return -1;
364 }
365 M_check_autoarg(ctx, key, pkeylen, EVP_F_EVP_PKEY_DERIVE)
366 return ctx->pmeth->derive(ctx, key, pkeylen);
367 }
368
334{
335 if (!ctx || !ctx->pmeth || !ctx->pmeth->derive) {
336 EVPerr(EVP_F_EVP_PKEY_DERIVE,
337 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
338 return -2;
339 }
340 if (ctx->operation != EVP_PKEY_OP_DERIVE) {
341 EVPerr(EVP_F_EVP_PKEY_DERIVE, EVP_R_OPERATON_NOT_INITIALIZED);
342 return -1;
343 }
344 M_check_autoarg(ctx, key, pkeylen, EVP_F_EVP_PKEY_DERIVE)
345 return ctx->pmeth->derive(ctx, key, pkeylen);
346}