Complete key derivation support.
[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)
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         if (!ctx->pmeth->sign_init)
77                 return 1;
78         ret = ctx->pmeth->sign_init(ctx);
79         if (ret <= 0)
80                 ctx->operation = EVP_PKEY_OP_UNDEFINED;
81         return ret;
82         }
83
84 int EVP_PKEY_sign(EVP_PKEY_CTX *ctx,
85                         unsigned char *sig, int *siglen,
86                         const unsigned char *tbs, int tbslen)
87         {
88         if (!ctx || !ctx->pmeth || !ctx->pmeth->sign)
89                 {
90                 EVPerr(EVP_F_EVP_PKEY_SIGN,
91                         EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
92                 return -2;
93                 }
94         if (ctx->operation != EVP_PKEY_OP_SIGN)
95                 {
96                 EVPerr(EVP_F_EVP_PKEY_SIGN, EVP_R_OPERATON_NOT_INITIALIZED);
97                 return -1;
98                 }
99         return ctx->pmeth->sign(ctx, sig, siglen, tbs, tbslen);
100         }
101
102 int EVP_PKEY_verify_init(EVP_PKEY_CTX *ctx)
103         {
104         int ret;
105         if (!ctx || !ctx->pmeth || !ctx->pmeth->verify)
106                 {
107                 EVPerr(EVP_F_EVP_PKEY_VERIFY_INIT,
108                         EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
109                 return -2;
110                 }
111         ctx->operation = EVP_PKEY_OP_VERIFY;
112         if (!ctx->pmeth->verify_init)
113                 return 1;
114         ret = ctx->pmeth->verify_init(ctx);
115         if (ret <= 0)
116                 ctx->operation = EVP_PKEY_OP_UNDEFINED;
117         return ret;
118         }
119
120 int EVP_PKEY_verify(EVP_PKEY_CTX *ctx,
121                         const unsigned char *sig, int siglen,
122                         const unsigned char *tbs, int tbslen)
123         {
124         if (!ctx || !ctx->pmeth || !ctx->pmeth->verify)
125                 {
126                 EVPerr(EVP_F_EVP_PKEY_VERIFY,
127                         EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
128                 return -2;
129                 }
130         if (ctx->operation != EVP_PKEY_OP_VERIFY)
131                 {
132                 EVPerr(EVP_F_EVP_PKEY_VERIFY, EVP_R_OPERATON_NOT_INITIALIZED);
133                 return -1;
134                 }
135         return ctx->pmeth->verify(ctx, sig, siglen, tbs, tbslen);
136         }
137
138 int EVP_PKEY_verify_recover_init(EVP_PKEY_CTX *ctx)
139         {
140         int ret;
141         if (!ctx || !ctx->pmeth || !ctx->pmeth->verify_recover)
142                 {
143                 EVPerr(EVP_F_EVP_PKEY_VERIFY_RECOVER_INIT,
144                         EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
145                 return -2;
146                 }
147         ctx->operation = EVP_PKEY_OP_VERIFYRECOVER;
148         if (!ctx->pmeth->verify_recover_init)
149                 return 1;
150         ret = ctx->pmeth->verify_recover_init(ctx);
151         if (ret <= 0)
152                 ctx->operation = EVP_PKEY_OP_UNDEFINED;
153         return ret;
154         }
155
156 int EVP_PKEY_verify_recover(EVP_PKEY_CTX *ctx,
157                         unsigned char *rout, int *routlen,
158                         const unsigned char *sig, int siglen)
159         {
160         if (!ctx || !ctx->pmeth || !ctx->pmeth->verify_recover)
161                 {
162                 EVPerr(EVP_F_EVP_PKEY_VERIFY_RECOVER,
163                         EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
164                 return -2;
165                 }
166         if (ctx->operation != EVP_PKEY_OP_VERIFYRECOVER)
167                 {
168                 EVPerr(EVP_F_EVP_PKEY_VERIFY_RECOVER, EVP_R_OPERATON_NOT_INITIALIZED);
169                 return -1;
170                 }
171         return ctx->pmeth->verify_recover(ctx, rout, routlen, sig, siglen);
172         }
173
174 int EVP_PKEY_encrypt_init(EVP_PKEY_CTX *ctx)
175         {
176         int ret;
177         if (!ctx || !ctx->pmeth || !ctx->pmeth->encrypt)
178                 {
179                 EVPerr(EVP_F_EVP_PKEY_ENCRYPT_INIT,
180                         EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
181                 return -2;
182                 }
183         ctx->operation = EVP_PKEY_OP_ENCRYPT;
184         if (!ctx->pmeth->encrypt_init)
185                 return 1;
186         ret = ctx->pmeth->encrypt_init(ctx);
187         if (ret <= 0)
188                 ctx->operation = EVP_PKEY_OP_UNDEFINED;
189         return ret;
190         }
191
192 int EVP_PKEY_encrypt(EVP_PKEY_CTX *ctx,
193                         unsigned char *out, int *outlen,
194                         const unsigned char *in, int inlen)
195         {
196         if (!ctx || !ctx->pmeth || !ctx->pmeth->encrypt)
197                 {
198                 EVPerr(EVP_F_EVP_PKEY_ENCRYPT,
199                         EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
200                 return -2;
201                 }
202         if (ctx->operation != EVP_PKEY_OP_ENCRYPT)
203                 {
204                 EVPerr(EVP_F_EVP_PKEY_ENCRYPT, EVP_R_OPERATON_NOT_INITIALIZED);
205                 return -1;
206                 }
207         return ctx->pmeth->encrypt(ctx, out, outlen, in, inlen);
208         }
209
210 int EVP_PKEY_decrypt_init(EVP_PKEY_CTX *ctx)
211         {
212         int ret;
213         if (!ctx || !ctx->pmeth || !ctx->pmeth->decrypt)
214                 {
215                 EVPerr(EVP_F_EVP_PKEY_DECRYPT_INIT,
216                         EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
217                 return -2;
218                 }
219         ctx->operation = EVP_PKEY_OP_DECRYPT;
220         if (!ctx->pmeth->decrypt_init)
221                 return 1;
222         ret = ctx->pmeth->decrypt_init(ctx);
223         if (ret <= 0)
224                 ctx->operation = EVP_PKEY_OP_UNDEFINED;
225         return ret;
226         }
227
228 int EVP_PKEY_decrypt(EVP_PKEY_CTX *ctx,
229                         unsigned char *out, int *outlen,
230                         const unsigned char *in, int inlen)
231         {
232         if (!ctx || !ctx->pmeth || !ctx->pmeth->decrypt)
233                 {
234                 EVPerr(EVP_F_EVP_PKEY_DECRYPT,
235                         EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
236                 return -2;
237                 }
238         if (ctx->operation != EVP_PKEY_OP_DECRYPT)
239                 {
240                 EVPerr(EVP_F_EVP_PKEY_DECRYPT, EVP_R_OPERATON_NOT_INITIALIZED);
241                 return -1;
242                 }
243         return ctx->pmeth->decrypt(ctx, out, outlen, in, inlen);
244         }
245
246
247 int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx)
248         {
249         int ret;
250         if (!ctx || !ctx->pmeth || !ctx->pmeth->derive)
251                 {
252                 EVPerr(EVP_F_EVP_PKEY_DERIVE_INIT,
253                         EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
254                 return -2;
255                 }
256         ctx->operation = EVP_PKEY_OP_DERIVE;
257         if (!ctx->pmeth->derive_init)
258                 return 1;
259         ret = ctx->pmeth->derive_init(ctx);
260         if (ret <= 0)
261                 ctx->operation = EVP_PKEY_OP_UNDEFINED;
262         return ret;
263         }
264
265 int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer)
266         {
267         int ret;
268         if (!ctx || !ctx->pmeth || !ctx->pmeth->derive || !ctx->pmeth->ctrl)
269                 {
270                 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
271                         EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
272                 return -2;
273                 }
274         if (ctx->operation != EVP_PKEY_OP_DERIVE)
275                 {
276                 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
277                                         EVP_R_OPERATON_NOT_INITIALIZED);
278                 return -1;
279                 }
280
281         ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 0, peer);
282
283         if (ret <= 0)
284                 return ret;
285
286         if (ret == 2)
287                 return 1;
288
289         if (!ctx->pkey)
290                 {
291                 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, EVP_R_NO_KEY_SET);
292                 return -1;
293                 }
294
295         if (ctx->pkey->type != peer->type)
296                 {
297                 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
298                                                 EVP_R_DIFFERENT_KEY_TYPES);
299                 return -1;
300                 }
301
302         if (!EVP_PKEY_missing_parameters(peer) &&
303                 !EVP_PKEY_cmp_parameters(ctx->pkey, peer))
304                 {
305                 EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
306                                                 EVP_R_DIFFERENT_PARAMETERS);
307                 return -1;
308                 }
309
310         ctx->peerkey = peer;
311
312         ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 1, peer);
313
314         if (ret <= 0)
315                 {
316                 ctx->peerkey = NULL;
317                 return ret;
318                 }
319
320         CRYPTO_add(&peer->references,1,CRYPTO_LOCK_EVP_PKEY);
321         return 1;
322         }
323
324
325 int EVP_PKEY_derive(EVP_PKEY_CTX *ctx, unsigned char *key, int *pkeylen)
326         {
327         if (!ctx || !ctx->pmeth || !ctx->pmeth->derive)
328                 {
329                 EVPerr(EVP_F_EVP_PKEY_DERIVE,
330                         EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
331                 return -2;
332                 }
333         if (ctx->operation != EVP_PKEY_OP_DERIVE)
334                 {
335                 EVPerr(EVP_F_EVP_PKEY_DERIVE, EVP_R_OPERATON_NOT_INITIALIZED);
336                 return -1;
337                 }
338         return ctx->pmeth->derive(ctx, key, pkeylen);
339         }
340