2 * Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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 <openssl/objects.h>
13 #include <openssl/evp.h>
14 #include "internal/cryptlib.h"
15 #include "internal/evp_int.h"
18 int EVP_PKEY_sign_init(EVP_PKEY_CTX
*ctx
)
21 if (!ctx
|| !ctx
->pmeth
|| !ctx
->pmeth
->sign
) {
22 EVPerr(EVP_F_EVP_PKEY_SIGN_INIT
,
23 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
26 ctx
->operation
= EVP_PKEY_OP_SIGN
;
27 if (!ctx
->pmeth
->sign_init
)
29 ret
= ctx
->pmeth
->sign_init(ctx
);
31 ctx
->operation
= EVP_PKEY_OP_UNDEFINED
;
35 int EVP_PKEY_sign(EVP_PKEY_CTX
*ctx
,
36 unsigned char *sig
, size_t *siglen
,
37 const unsigned char *tbs
, size_t tbslen
)
39 if (!ctx
|| !ctx
->pmeth
|| !ctx
->pmeth
->sign
) {
40 EVPerr(EVP_F_EVP_PKEY_SIGN
,
41 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
44 if (ctx
->operation
!= EVP_PKEY_OP_SIGN
) {
45 EVPerr(EVP_F_EVP_PKEY_SIGN
, EVP_R_OPERATON_NOT_INITIALIZED
);
48 M_check_autoarg(ctx
, sig
, siglen
, EVP_F_EVP_PKEY_SIGN
)
49 return ctx
->pmeth
->sign(ctx
, sig
, siglen
, tbs
, tbslen
);
52 int EVP_PKEY_verify_init(EVP_PKEY_CTX
*ctx
)
55 if (!ctx
|| !ctx
->pmeth
|| !ctx
->pmeth
->verify
) {
56 EVPerr(EVP_F_EVP_PKEY_VERIFY_INIT
,
57 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
60 ctx
->operation
= EVP_PKEY_OP_VERIFY
;
61 if (!ctx
->pmeth
->verify_init
)
63 ret
= ctx
->pmeth
->verify_init(ctx
);
65 ctx
->operation
= EVP_PKEY_OP_UNDEFINED
;
69 int EVP_PKEY_verify(EVP_PKEY_CTX
*ctx
,
70 const unsigned char *sig
, size_t siglen
,
71 const unsigned char *tbs
, size_t tbslen
)
73 if (!ctx
|| !ctx
->pmeth
|| !ctx
->pmeth
->verify
) {
74 EVPerr(EVP_F_EVP_PKEY_VERIFY
,
75 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
78 if (ctx
->operation
!= EVP_PKEY_OP_VERIFY
) {
79 EVPerr(EVP_F_EVP_PKEY_VERIFY
, EVP_R_OPERATON_NOT_INITIALIZED
);
82 return ctx
->pmeth
->verify(ctx
, sig
, siglen
, tbs
, tbslen
);
85 int EVP_PKEY_verify_recover_init(EVP_PKEY_CTX
*ctx
)
88 if (!ctx
|| !ctx
->pmeth
|| !ctx
->pmeth
->verify_recover
) {
89 EVPerr(EVP_F_EVP_PKEY_VERIFY_RECOVER_INIT
,
90 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
93 ctx
->operation
= EVP_PKEY_OP_VERIFYRECOVER
;
94 if (!ctx
->pmeth
->verify_recover_init
)
96 ret
= ctx
->pmeth
->verify_recover_init(ctx
);
98 ctx
->operation
= EVP_PKEY_OP_UNDEFINED
;
102 int EVP_PKEY_verify_recover(EVP_PKEY_CTX
*ctx
,
103 unsigned char *rout
, size_t *routlen
,
104 const unsigned char *sig
, size_t siglen
)
106 if (!ctx
|| !ctx
->pmeth
|| !ctx
->pmeth
->verify_recover
) {
107 EVPerr(EVP_F_EVP_PKEY_VERIFY_RECOVER
,
108 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
111 if (ctx
->operation
!= EVP_PKEY_OP_VERIFYRECOVER
) {
112 EVPerr(EVP_F_EVP_PKEY_VERIFY_RECOVER
, EVP_R_OPERATON_NOT_INITIALIZED
);
115 M_check_autoarg(ctx
, rout
, routlen
, EVP_F_EVP_PKEY_VERIFY_RECOVER
)
116 return ctx
->pmeth
->verify_recover(ctx
, rout
, routlen
, sig
, siglen
);
119 int EVP_PKEY_encrypt_init(EVP_PKEY_CTX
*ctx
)
122 if (!ctx
|| !ctx
->pmeth
|| !ctx
->pmeth
->encrypt
) {
123 EVPerr(EVP_F_EVP_PKEY_ENCRYPT_INIT
,
124 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
127 ctx
->operation
= EVP_PKEY_OP_ENCRYPT
;
128 if (!ctx
->pmeth
->encrypt_init
)
130 ret
= ctx
->pmeth
->encrypt_init(ctx
);
132 ctx
->operation
= EVP_PKEY_OP_UNDEFINED
;
136 int EVP_PKEY_encrypt(EVP_PKEY_CTX
*ctx
,
137 unsigned char *out
, size_t *outlen
,
138 const unsigned char *in
, size_t inlen
)
140 if (!ctx
|| !ctx
->pmeth
|| !ctx
->pmeth
->encrypt
) {
141 EVPerr(EVP_F_EVP_PKEY_ENCRYPT
,
142 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
145 if (ctx
->operation
!= EVP_PKEY_OP_ENCRYPT
) {
146 EVPerr(EVP_F_EVP_PKEY_ENCRYPT
, EVP_R_OPERATON_NOT_INITIALIZED
);
149 M_check_autoarg(ctx
, out
, outlen
, EVP_F_EVP_PKEY_ENCRYPT
)
150 return ctx
->pmeth
->encrypt(ctx
, out
, outlen
, in
, inlen
);
153 int EVP_PKEY_decrypt_init(EVP_PKEY_CTX
*ctx
)
156 if (!ctx
|| !ctx
->pmeth
|| !ctx
->pmeth
->decrypt
) {
157 EVPerr(EVP_F_EVP_PKEY_DECRYPT_INIT
,
158 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
161 ctx
->operation
= EVP_PKEY_OP_DECRYPT
;
162 if (!ctx
->pmeth
->decrypt_init
)
164 ret
= ctx
->pmeth
->decrypt_init(ctx
);
166 ctx
->operation
= EVP_PKEY_OP_UNDEFINED
;
170 int EVP_PKEY_decrypt(EVP_PKEY_CTX
*ctx
,
171 unsigned char *out
, size_t *outlen
,
172 const unsigned char *in
, size_t inlen
)
174 if (!ctx
|| !ctx
->pmeth
|| !ctx
->pmeth
->decrypt
) {
175 EVPerr(EVP_F_EVP_PKEY_DECRYPT
,
176 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
179 if (ctx
->operation
!= EVP_PKEY_OP_DECRYPT
) {
180 EVPerr(EVP_F_EVP_PKEY_DECRYPT
, EVP_R_OPERATON_NOT_INITIALIZED
);
183 M_check_autoarg(ctx
, out
, outlen
, EVP_F_EVP_PKEY_DECRYPT
)
184 return ctx
->pmeth
->decrypt(ctx
, out
, outlen
, in
, inlen
);