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/evp_int.h"
17 #define M_check_autoarg(ctx, arg, arglen, err) \
18 if (ctx->pmeth->flags & EVP_PKEY_FLAG_AUTOARGLEN) \
20 size_t pksize = (size_t)EVP_PKEY_size(ctx->pkey); \
26 else if (*arglen < pksize) \
28 EVPerr(err, EVP_R_BUFFER_TOO_SMALL); /*ckerr_ignore*/\
33 int EVP_PKEY_sign_init(EVP_PKEY_CTX
*ctx
)
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
);
41 ctx
->operation
= EVP_PKEY_OP_SIGN
;
42 if (!ctx
->pmeth
->sign_init
)
44 ret
= ctx
->pmeth
->sign_init(ctx
);
46 ctx
->operation
= EVP_PKEY_OP_UNDEFINED
;
50 int EVP_PKEY_sign(EVP_PKEY_CTX
*ctx
,
51 unsigned char *sig
, size_t *siglen
,
52 const unsigned char *tbs
, size_t tbslen
)
54 if (!ctx
|| !ctx
->pmeth
|| !ctx
->pmeth
->sign
) {
55 EVPerr(EVP_F_EVP_PKEY_SIGN
,
56 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
59 if (ctx
->operation
!= EVP_PKEY_OP_SIGN
) {
60 EVPerr(EVP_F_EVP_PKEY_SIGN
, EVP_R_OPERATON_NOT_INITIALIZED
);
63 M_check_autoarg(ctx
, sig
, siglen
, EVP_F_EVP_PKEY_SIGN
)
64 return ctx
->pmeth
->sign(ctx
, sig
, siglen
, tbs
, tbslen
);
67 int EVP_PKEY_verify_init(EVP_PKEY_CTX
*ctx
)
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
);
75 ctx
->operation
= EVP_PKEY_OP_VERIFY
;
76 if (!ctx
->pmeth
->verify_init
)
78 ret
= ctx
->pmeth
->verify_init(ctx
);
80 ctx
->operation
= EVP_PKEY_OP_UNDEFINED
;
84 int EVP_PKEY_verify(EVP_PKEY_CTX
*ctx
,
85 const unsigned char *sig
, size_t siglen
,
86 const unsigned char *tbs
, size_t tbslen
)
88 if (!ctx
|| !ctx
->pmeth
|| !ctx
->pmeth
->verify
) {
89 EVPerr(EVP_F_EVP_PKEY_VERIFY
,
90 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
93 if (ctx
->operation
!= EVP_PKEY_OP_VERIFY
) {
94 EVPerr(EVP_F_EVP_PKEY_VERIFY
, EVP_R_OPERATON_NOT_INITIALIZED
);
97 return ctx
->pmeth
->verify(ctx
, sig
, siglen
, tbs
, tbslen
);
100 int EVP_PKEY_verify_recover_init(EVP_PKEY_CTX
*ctx
)
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
);
108 ctx
->operation
= EVP_PKEY_OP_VERIFYRECOVER
;
109 if (!ctx
->pmeth
->verify_recover_init
)
111 ret
= ctx
->pmeth
->verify_recover_init(ctx
);
113 ctx
->operation
= EVP_PKEY_OP_UNDEFINED
;
117 int EVP_PKEY_verify_recover(EVP_PKEY_CTX
*ctx
,
118 unsigned char *rout
, size_t *routlen
,
119 const unsigned char *sig
, size_t siglen
)
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
);
126 if (ctx
->operation
!= EVP_PKEY_OP_VERIFYRECOVER
) {
127 EVPerr(EVP_F_EVP_PKEY_VERIFY_RECOVER
, EVP_R_OPERATON_NOT_INITIALIZED
);
130 M_check_autoarg(ctx
, rout
, routlen
, EVP_F_EVP_PKEY_VERIFY_RECOVER
)
131 return ctx
->pmeth
->verify_recover(ctx
, rout
, routlen
, sig
, siglen
);
134 int EVP_PKEY_encrypt_init(EVP_PKEY_CTX
*ctx
)
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
);
142 ctx
->operation
= EVP_PKEY_OP_ENCRYPT
;
143 if (!ctx
->pmeth
->encrypt_init
)
145 ret
= ctx
->pmeth
->encrypt_init(ctx
);
147 ctx
->operation
= EVP_PKEY_OP_UNDEFINED
;
151 int EVP_PKEY_encrypt(EVP_PKEY_CTX
*ctx
,
152 unsigned char *out
, size_t *outlen
,
153 const unsigned char *in
, size_t inlen
)
155 if (!ctx
|| !ctx
->pmeth
|| !ctx
->pmeth
->encrypt
) {
156 EVPerr(EVP_F_EVP_PKEY_ENCRYPT
,
157 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
160 if (ctx
->operation
!= EVP_PKEY_OP_ENCRYPT
) {
161 EVPerr(EVP_F_EVP_PKEY_ENCRYPT
, EVP_R_OPERATON_NOT_INITIALIZED
);
164 M_check_autoarg(ctx
, out
, outlen
, EVP_F_EVP_PKEY_ENCRYPT
)
165 return ctx
->pmeth
->encrypt(ctx
, out
, outlen
, in
, inlen
);
168 int EVP_PKEY_decrypt_init(EVP_PKEY_CTX
*ctx
)
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
);
176 ctx
->operation
= EVP_PKEY_OP_DECRYPT
;
177 if (!ctx
->pmeth
->decrypt_init
)
179 ret
= ctx
->pmeth
->decrypt_init(ctx
);
181 ctx
->operation
= EVP_PKEY_OP_UNDEFINED
;
185 int EVP_PKEY_decrypt(EVP_PKEY_CTX
*ctx
,
186 unsigned char *out
, size_t *outlen
,
187 const unsigned char *in
, size_t inlen
)
189 if (!ctx
|| !ctx
->pmeth
|| !ctx
->pmeth
->decrypt
) {
190 EVPerr(EVP_F_EVP_PKEY_DECRYPT
,
191 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
194 if (ctx
->operation
!= EVP_PKEY_OP_DECRYPT
) {
195 EVPerr(EVP_F_EVP_PKEY_DECRYPT
, EVP_R_OPERATON_NOT_INITIALIZED
);
198 M_check_autoarg(ctx
, out
, outlen
, EVP_F_EVP_PKEY_DECRYPT
)
199 return ctx
->pmeth
->decrypt(ctx
, out
, outlen
, in
, inlen
);
202 int EVP_PKEY_derive_init(EVP_PKEY_CTX
*ctx
)
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
);
210 ctx
->operation
= EVP_PKEY_OP_DERIVE
;
211 if (!ctx
->pmeth
->derive_init
)
213 ret
= ctx
->pmeth
->derive_init(ctx
);
215 ctx
->operation
= EVP_PKEY_OP_UNDEFINED
;
219 int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX
*ctx
, EVP_PKEY
*peer
)
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
);
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
);
237 ret
= ctx
->pmeth
->ctrl(ctx
, EVP_PKEY_CTRL_PEER_KEY
, 0, peer
);
246 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER
, EVP_R_NO_KEY_SET
);
250 if (ctx
->pkey
->type
!= peer
->type
) {
251 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER
, EVP_R_DIFFERENT_KEY_TYPES
);
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.
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
);
268 EVP_PKEY_free(ctx
->peerkey
);
271 ret
= ctx
->pmeth
->ctrl(ctx
, EVP_PKEY_CTRL_PEER_KEY
, 1, peer
);
278 EVP_PKEY_up_ref(peer
);
282 int EVP_PKEY_derive(EVP_PKEY_CTX
*ctx
, unsigned char *key
, size_t *pkeylen
)
284 if (!ctx
|| !ctx
->pmeth
|| !ctx
->pmeth
->derive
) {
285 EVPerr(EVP_F_EVP_PKEY_DERIVE
,
286 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
289 if (ctx
->operation
!= EVP_PKEY_OP_DERIVE
) {
290 EVPerr(EVP_F_EVP_PKEY_DERIVE
, EVP_R_OPERATON_NOT_INITIALIZED
);
293 M_check_autoarg(ctx
, key
, pkeylen
, EVP_F_EVP_PKEY_DERIVE
)
294 return ctx
->pmeth
->derive(ctx
, key
, pkeylen
);