]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/evp/pmeth_fn.c
Copyright consolidation 05/10
[thirdparty/openssl.git] / crypto / evp / pmeth_fn.c
CommitLineData
0f113f3e 1/*
aa6bb135 2 * Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
f733a5ef 3 *
aa6bb135
RS
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
f733a5ef
DSH
8 */
9
10#include <stdio.h>
11#include <stdlib.h>
b39fc560 12#include "internal/cryptlib.h"
c20276e4 13#include <openssl/objects.h>
f733a5ef 14#include <openssl/evp.h>
27af42f9 15#include "internal/evp_int.h"
f733a5ef 16
b010b7c4 17#define M_check_autoarg(ctx, arg, arglen, err) \
0f113f3e
MC
18 if (ctx->pmeth->flags & EVP_PKEY_FLAG_AUTOARGLEN) \
19 { \
20 size_t pksize = (size_t)EVP_PKEY_size(ctx->pkey); \
21 if (!arg) \
22 { \
23 *arglen = pksize; \
24 return 1; \
25 } \
26 else if (*arglen < pksize) \
27 { \
28 EVPerr(err, EVP_R_BUFFER_TOO_SMALL); /*ckerr_ignore*/\
29 return 0; \
30 } \
31 }
b010b7c4 32
cd763898 33int EVP_PKEY_sign_init(EVP_PKEY_CTX *ctx)
0f113f3e
MC
34{
35 int ret;
36 if (!ctx || !ctx->pmeth || !ctx->pmeth->sign) {
37 EVPerr(EVP_F_EVP_PKEY_SIGN_INIT,
38 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
39 return -2;
40 }
41 ctx->operation = EVP_PKEY_OP_SIGN;
42 if (!ctx->pmeth->sign_init)
43 return 1;
44 ret = ctx->pmeth->sign_init(ctx);
45 if (ret <= 0)
46 ctx->operation = EVP_PKEY_OP_UNDEFINED;
47 return ret;
48}
f733a5ef
DSH
49
50int EVP_PKEY_sign(EVP_PKEY_CTX *ctx,
0f113f3e
MC
51 unsigned char *sig, size_t *siglen,
52 const unsigned char *tbs, size_t tbslen)
53{
54 if (!ctx || !ctx->pmeth || !ctx->pmeth->sign) {
55 EVPerr(EVP_F_EVP_PKEY_SIGN,
56 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
57 return -2;
58 }
59 if (ctx->operation != EVP_PKEY_OP_SIGN) {
60 EVPerr(EVP_F_EVP_PKEY_SIGN, EVP_R_OPERATON_NOT_INITIALIZED);
61 return -1;
62 }
63 M_check_autoarg(ctx, sig, siglen, EVP_F_EVP_PKEY_SIGN)
64 return ctx->pmeth->sign(ctx, sig, siglen, tbs, tbslen);
65}
f733a5ef 66
cd763898 67int EVP_PKEY_verify_init(EVP_PKEY_CTX *ctx)
0f113f3e
MC
68{
69 int ret;
70 if (!ctx || !ctx->pmeth || !ctx->pmeth->verify) {
71 EVPerr(EVP_F_EVP_PKEY_VERIFY_INIT,
72 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
73 return -2;
74 }
75 ctx->operation = EVP_PKEY_OP_VERIFY;
76 if (!ctx->pmeth->verify_init)
77 return 1;
78 ret = ctx->pmeth->verify_init(ctx);
79 if (ret <= 0)
80 ctx->operation = EVP_PKEY_OP_UNDEFINED;
81 return ret;
82}
f733a5ef
DSH
83
84int EVP_PKEY_verify(EVP_PKEY_CTX *ctx,
0f113f3e
MC
85 const unsigned char *sig, size_t siglen,
86 const unsigned char *tbs, size_t tbslen)
87{
88 if (!ctx || !ctx->pmeth || !ctx->pmeth->verify) {
89 EVPerr(EVP_F_EVP_PKEY_VERIFY,
90 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
91 return -2;
92 }
93 if (ctx->operation != EVP_PKEY_OP_VERIFY) {
94 EVPerr(EVP_F_EVP_PKEY_VERIFY, EVP_R_OPERATON_NOT_INITIALIZED);
95 return -1;
96 }
97 return ctx->pmeth->verify(ctx, sig, siglen, tbs, tbslen);
98}
f733a5ef 99
cd763898 100int EVP_PKEY_verify_recover_init(EVP_PKEY_CTX *ctx)
0f113f3e
MC
101{
102 int ret;
103 if (!ctx || !ctx->pmeth || !ctx->pmeth->verify_recover) {
104 EVPerr(EVP_F_EVP_PKEY_VERIFY_RECOVER_INIT,
105 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
106 return -2;
107 }
108 ctx->operation = EVP_PKEY_OP_VERIFYRECOVER;
109 if (!ctx->pmeth->verify_recover_init)
110 return 1;
111 ret = ctx->pmeth->verify_recover_init(ctx);
112 if (ret <= 0)
113 ctx->operation = EVP_PKEY_OP_UNDEFINED;
114 return ret;
115}
f733a5ef
DSH
116
117int EVP_PKEY_verify_recover(EVP_PKEY_CTX *ctx,
0f113f3e
MC
118 unsigned char *rout, size_t *routlen,
119 const unsigned char *sig, size_t siglen)
120{
121 if (!ctx || !ctx->pmeth || !ctx->pmeth->verify_recover) {
122 EVPerr(EVP_F_EVP_PKEY_VERIFY_RECOVER,
123 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
124 return -2;
125 }
126 if (ctx->operation != EVP_PKEY_OP_VERIFYRECOVER) {
127 EVPerr(EVP_F_EVP_PKEY_VERIFY_RECOVER, EVP_R_OPERATON_NOT_INITIALIZED);
128 return -1;
129 }
130 M_check_autoarg(ctx, rout, routlen, EVP_F_EVP_PKEY_VERIFY_RECOVER)
131 return ctx->pmeth->verify_recover(ctx, rout, routlen, sig, siglen);
132}
f733a5ef 133
cd763898 134int EVP_PKEY_encrypt_init(EVP_PKEY_CTX *ctx)
0f113f3e
MC
135{
136 int ret;
137 if (!ctx || !ctx->pmeth || !ctx->pmeth->encrypt) {
138 EVPerr(EVP_F_EVP_PKEY_ENCRYPT_INIT,
139 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
140 return -2;
141 }
142 ctx->operation = EVP_PKEY_OP_ENCRYPT;
143 if (!ctx->pmeth->encrypt_init)
144 return 1;
145 ret = ctx->pmeth->encrypt_init(ctx);
146 if (ret <= 0)
147 ctx->operation = EVP_PKEY_OP_UNDEFINED;
148 return ret;
149}
f733a5ef
DSH
150
151int EVP_PKEY_encrypt(EVP_PKEY_CTX *ctx,
0f113f3e
MC
152 unsigned char *out, size_t *outlen,
153 const unsigned char *in, size_t inlen)
154{
155 if (!ctx || !ctx->pmeth || !ctx->pmeth->encrypt) {
156 EVPerr(EVP_F_EVP_PKEY_ENCRYPT,
157 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
158 return -2;
159 }
160 if (ctx->operation != EVP_PKEY_OP_ENCRYPT) {
161 EVPerr(EVP_F_EVP_PKEY_ENCRYPT, EVP_R_OPERATON_NOT_INITIALIZED);
162 return -1;
163 }
164 M_check_autoarg(ctx, out, outlen, EVP_F_EVP_PKEY_ENCRYPT)
165 return ctx->pmeth->encrypt(ctx, out, outlen, in, inlen);
166}
f733a5ef 167
cd763898 168int EVP_PKEY_decrypt_init(EVP_PKEY_CTX *ctx)
0f113f3e
MC
169{
170 int ret;
171 if (!ctx || !ctx->pmeth || !ctx->pmeth->decrypt) {
172 EVPerr(EVP_F_EVP_PKEY_DECRYPT_INIT,
173 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
174 return -2;
175 }
176 ctx->operation = EVP_PKEY_OP_DECRYPT;
177 if (!ctx->pmeth->decrypt_init)
178 return 1;
179 ret = ctx->pmeth->decrypt_init(ctx);
180 if (ret <= 0)
181 ctx->operation = EVP_PKEY_OP_UNDEFINED;
182 return ret;
183}
f733a5ef
DSH
184
185int EVP_PKEY_decrypt(EVP_PKEY_CTX *ctx,
0f113f3e
MC
186 unsigned char *out, size_t *outlen,
187 const unsigned char *in, size_t inlen)
188{
189 if (!ctx || !ctx->pmeth || !ctx->pmeth->decrypt) {
190 EVPerr(EVP_F_EVP_PKEY_DECRYPT,
191 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
192 return -2;
193 }
194 if (ctx->operation != EVP_PKEY_OP_DECRYPT) {
195 EVPerr(EVP_F_EVP_PKEY_DECRYPT, EVP_R_OPERATON_NOT_INITIALIZED);
196 return -1;
197 }
198 M_check_autoarg(ctx, out, outlen, EVP_F_EVP_PKEY_DECRYPT)
199 return ctx->pmeth->decrypt(ctx, out, outlen, in, inlen);
200}
d87e6152
DSH
201
202int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx)
0f113f3e
MC
203{
204 int ret;
205 if (!ctx || !ctx->pmeth || !ctx->pmeth->derive) {
206 EVPerr(EVP_F_EVP_PKEY_DERIVE_INIT,
207 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
208 return -2;
209 }
210 ctx->operation = EVP_PKEY_OP_DERIVE;
211 if (!ctx->pmeth->derive_init)
212 return 1;
213 ret = ctx->pmeth->derive_init(ctx);
214 if (ret <= 0)
215 ctx->operation = EVP_PKEY_OP_UNDEFINED;
216 return ret;
217}
d87e6152 218
ffb1ac67 219int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer)
0f113f3e
MC
220{
221 int ret;
222 if (!ctx || !ctx->pmeth
223 || !(ctx->pmeth->derive || ctx->pmeth->encrypt || ctx->pmeth->decrypt)
224 || !ctx->pmeth->ctrl) {
225 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
226 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
227 return -2;
228 }
229 if (ctx->operation != EVP_PKEY_OP_DERIVE
230 && ctx->operation != EVP_PKEY_OP_ENCRYPT
231 && ctx->operation != EVP_PKEY_OP_DECRYPT) {
232 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
233 EVP_R_OPERATON_NOT_INITIALIZED);
234 return -1;
235 }
ffb1ac67 236
0f113f3e 237 ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 0, peer);
ffb1ac67 238
0f113f3e
MC
239 if (ret <= 0)
240 return ret;
ffb1ac67 241
0f113f3e
MC
242 if (ret == 2)
243 return 1;
ffb1ac67 244
0f113f3e
MC
245 if (!ctx->pkey) {
246 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, EVP_R_NO_KEY_SET);
247 return -1;
248 }
ffb1ac67 249
0f113f3e
MC
250 if (ctx->pkey->type != peer->type) {
251 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, EVP_R_DIFFERENT_KEY_TYPES);
252 return -1;
253 }
ffb1ac67 254
0f113f3e
MC
255 /*
256 * ran@cryptocom.ru: For clarity. The error is if parameters in peer are
257 * present (!missing) but don't match. EVP_PKEY_cmp_parameters may return
258 * 1 (match), 0 (don't match) and -2 (comparison is not defined). -1
259 * (different key types) is impossible here because it is checked earlier.
260 * -2 is OK for us here, as well as 1, so we can check for 0 only.
261 */
262 if (!EVP_PKEY_missing_parameters(peer) &&
263 !EVP_PKEY_cmp_parameters(ctx->pkey, peer)) {
264 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, EVP_R_DIFFERENT_PARAMETERS);
265 return -1;
266 }
ffb1ac67 267
c5ba2d99 268 EVP_PKEY_free(ctx->peerkey);
0f113f3e 269 ctx->peerkey = peer;
ffb1ac67 270
0f113f3e 271 ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 1, peer);
ffb1ac67 272
0f113f3e
MC
273 if (ret <= 0) {
274 ctx->peerkey = NULL;
275 return ret;
276 }
ffb1ac67 277
03273d61 278 EVP_PKEY_up_ref(peer);
0f113f3e
MC
279 return 1;
280}
ffb1ac67 281
eaff5a14 282int EVP_PKEY_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *pkeylen)
0f113f3e
MC
283{
284 if (!ctx || !ctx->pmeth || !ctx->pmeth->derive) {
285 EVPerr(EVP_F_EVP_PKEY_DERIVE,
286 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
287 return -2;
288 }
289 if (ctx->operation != EVP_PKEY_OP_DERIVE) {
290 EVPerr(EVP_F_EVP_PKEY_DERIVE, EVP_R_OPERATON_NOT_INITIALIZED);
291 return -1;
292 }
293 M_check_autoarg(ctx, key, pkeylen, EVP_F_EVP_PKEY_DERIVE)
294 return ctx->pmeth->derive(ctx, key, pkeylen);
295}