New utility 'pkeyutl' a general purpose version of 'rsautl'.
[openssl.git] / crypto / evp / pmeth_fn.c
1 /* pmeth_fn.c */
2 /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
3  * project 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
13  *    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:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    licensing@OpenSSL.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This product includes cryptographic software written by Eric Young
54  * (eay@cryptsoft.com).  This product includes software written by Tim
55  * Hudson (tjh@cryptsoft.com).
56  *
57  */
58
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <openssl/objects.h>
62 #include "cryptlib.h"
63 #include <openssl/evp.h>
64 #include "evp_locl.h"
65
66 int EVP_PKEY_sign_init(EVP_PKEY_CTX *ctx)
67         {
68         int ret;
69         if (!ctx || !ctx->pmeth || !ctx->pmeth->sign_init)
70                 {
71                 EVPerr(EVP_F_EVP_PKEY_SIGN_INIT,
72                         EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
73                 return -2;
74                 }
75         ctx->operation = EVP_PKEY_OP_SIGN;
76         ret = ctx->pmeth->sign_init(ctx);
77         if (ret <= 0)
78                 ctx->operation = EVP_PKEY_OP_UNDEFINED;
79         return ret;
80         }
81
82 int EVP_PKEY_sign(EVP_PKEY_CTX *ctx,
83                         unsigned char *sig, int *siglen,
84                         unsigned char *tbs, int tbslen)
85         {
86         if (!ctx || !ctx->pmeth || !ctx->pmeth->sign)
87                 {
88                 EVPerr(EVP_F_EVP_PKEY_SIGN,
89                         EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
90                 return -2;
91                 }
92         if (ctx->operation != EVP_PKEY_OP_SIGN)
93                 {
94                 EVPerr(EVP_F_EVP_PKEY_SIGN, EVP_R_OPERATON_NOT_INITIALIZED);
95                 return -1;
96                 }
97         return ctx->pmeth->sign(ctx, sig, siglen, tbs, tbslen);
98         }
99
100 int EVP_PKEY_verify_init(EVP_PKEY_CTX *ctx)
101         {
102         int ret;
103         if (!ctx || !ctx->pmeth || !ctx->pmeth->verify_init)
104                 {
105                 EVPerr(EVP_F_EVP_PKEY_VERIFY_INIT,
106                         EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
107                 return -2;
108                 }
109         ctx->operation = EVP_PKEY_OP_VERIFY;
110         ret = ctx->pmeth->verify_init(ctx);
111         if (ret <= 0)
112                 ctx->operation = EVP_PKEY_OP_UNDEFINED;
113         return ret;
114         }
115
116 int EVP_PKEY_verify(EVP_PKEY_CTX *ctx,
117                         unsigned char *sig, int siglen,
118                         unsigned char *tbs, int tbslen)
119         {
120         if (!ctx || !ctx->pmeth || !ctx->pmeth->verify)
121                 {
122                 EVPerr(EVP_F_EVP_PKEY_VERIFY,
123                         EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
124                 return -2;
125                 }
126         if (ctx->operation != EVP_PKEY_OP_VERIFY)
127                 {
128                 EVPerr(EVP_F_EVP_PKEY_VERIFY, EVP_R_OPERATON_NOT_INITIALIZED);
129                 return -1;
130                 }
131         return ctx->pmeth->verify(ctx, sig, siglen, tbs, tbslen);
132         }
133
134 int EVP_PKEY_verify_recover_init(EVP_PKEY_CTX *ctx)
135         {
136         int ret;
137         if (!ctx || !ctx->pmeth || !ctx->pmeth->verify_recover_init)
138                 {
139                 EVPerr(EVP_F_EVP_PKEY_VERIFY_RECOVER_INIT,
140                         EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
141                 return -2;
142                 }
143         ctx->operation = EVP_PKEY_OP_VERIFYRECOVER;
144         ret = ctx->pmeth->verify_recover_init(ctx);
145         if (ret <= 0)
146                 ctx->operation = EVP_PKEY_OP_UNDEFINED;
147         return ret;
148         }
149
150 int EVP_PKEY_verify_recover(EVP_PKEY_CTX *ctx,
151                         unsigned char *rout, int *routlen,
152                         unsigned char *sig, int siglen)
153         {
154         if (!ctx || !ctx->pmeth || !ctx->pmeth->verify_recover)
155                 {
156                 EVPerr(EVP_F_EVP_PKEY_VERIFY_RECOVER,
157                         EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
158                 return -2;
159                 }
160         if (ctx->operation != EVP_PKEY_OP_VERIFYRECOVER)
161                 {
162                 EVPerr(EVP_F_EVP_PKEY_VERIFY_RECOVER, EVP_R_OPERATON_NOT_INITIALIZED);
163                 return -1;
164                 }
165         return ctx->pmeth->verify_recover(ctx, rout, routlen, sig, siglen);
166         }
167
168 int EVP_PKEY_encrypt_init(EVP_PKEY_CTX *ctx)
169         {
170         int ret;
171         if (!ctx || !ctx->pmeth || !ctx->pmeth->encrypt_init)
172                 {
173                 EVPerr(EVP_F_EVP_PKEY_ENCRYPT_INIT,
174                         EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
175                 return -2;
176                 }
177         ctx->operation = EVP_PKEY_OP_ENCRYPT;
178         ret = ctx->pmeth->encrypt_init(ctx);
179         if (ret <= 0)
180                 ctx->operation = EVP_PKEY_OP_UNDEFINED;
181         return ret;
182         }
183
184 int EVP_PKEY_encrypt(EVP_PKEY_CTX *ctx,
185                         unsigned char *out, int *outlen,
186                         unsigned char *in, int inlen)
187         {
188         if (!ctx || !ctx->pmeth || !ctx->pmeth->encrypt)
189                 {
190                 EVPerr(EVP_F_EVP_PKEY_ENCRYPT,
191                         EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
192                 return -2;
193                 }
194         if (ctx->operation != EVP_PKEY_OP_ENCRYPT)
195                 {
196                 EVPerr(EVP_F_EVP_PKEY_ENCRYPT, EVP_R_OPERATON_NOT_INITIALIZED);
197                 return -1;
198                 }
199         return ctx->pmeth->encrypt(ctx, out, outlen, in, inlen);
200         }
201
202 int EVP_PKEY_decrypt_init(EVP_PKEY_CTX *ctx)
203         {
204         int ret;
205         if (!ctx || !ctx->pmeth || !ctx->pmeth->decrypt_init)
206                 {
207                 EVPerr(EVP_F_EVP_PKEY_DECRYPT_INIT,
208                         EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
209                 return -2;
210                 }
211         ctx->operation = EVP_PKEY_OP_DECRYPT;
212         ret = ctx->pmeth->decrypt_init(ctx);
213         if (ret <= 0)
214                 ctx->operation = EVP_PKEY_OP_UNDEFINED;
215         return ret;
216         }
217
218 int EVP_PKEY_decrypt(EVP_PKEY_CTX *ctx,
219                         unsigned char *out, int *outlen,
220                         unsigned char *in, int inlen)
221         {
222         if (!ctx || !ctx->pmeth || !ctx->pmeth->decrypt)
223                 {
224                 EVPerr(EVP_F_EVP_PKEY_DECRYPT,
225                         EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
226                 return -2;
227                 }
228         if (ctx->operation != EVP_PKEY_OP_DECRYPT)
229                 {
230                 EVPerr(EVP_F_EVP_PKEY_DECRYPT, EVP_R_OPERATON_NOT_INITIALIZED);
231                 return -1;
232                 }
233         return ctx->pmeth->decrypt(ctx, out, outlen, in, inlen);
234         }
235