bn/asm/x86_64-mont5.pl: unify gather procedure in hardly used path
[openssl.git] / crypto / evp / cmeth_lib.c
1 /*
2  * Written by Richard Levitte (levitte@openssl.org) for the OpenSSL project
3  * 2015.
4  */
5 /* ====================================================================
6  * Copyright (c) 2015 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 <string.h>
60
61 #include <openssl/evp.h>
62 #include "internal/evp_int.h"
63 #include "evp_locl.h"
64
65 EVP_CIPHER *EVP_CIPHER_meth_new(int cipher_type, int block_size, int key_len)
66 {
67     EVP_CIPHER *cipher = OPENSSL_zalloc(sizeof(EVP_CIPHER));
68
69     if (cipher != NULL) {
70         cipher->nid = cipher_type;
71         cipher->block_size = block_size;
72         cipher->key_len = key_len;
73     }
74     return cipher;
75 }
76
77 EVP_CIPHER *EVP_CIPHER_meth_dup(const EVP_CIPHER *cipher)
78 {
79     EVP_CIPHER *to = EVP_CIPHER_meth_new(cipher->nid, cipher->block_size,
80                                          cipher->key_len);
81
82     if (to != NULL)
83         memcpy(to, cipher, sizeof(*to));
84     return to;
85 }
86
87 void EVP_CIPHER_meth_free(EVP_CIPHER *cipher)
88 {
89     OPENSSL_free(cipher);
90 }
91
92 int EVP_CIPHER_meth_set_iv_length(EVP_CIPHER *cipher, int iv_len)
93 {
94     cipher->iv_len = iv_len;
95     return 1;
96 }
97
98 int EVP_CIPHER_meth_set_flags(EVP_CIPHER *cipher, unsigned long flags)
99 {
100     cipher->flags = flags;
101     return 1;
102 }
103
104 int EVP_CIPHER_meth_set_impl_ctx_size(EVP_CIPHER *cipher, int ctx_size)
105 {
106     cipher->ctx_size = ctx_size;
107     return 1;
108 }
109
110 int EVP_CIPHER_meth_set_init(EVP_CIPHER *cipher,
111                              int (*init) (EVP_CIPHER_CTX *ctx,
112                                           const unsigned char *key,
113                                           const unsigned char *iv,
114                                           int enc))
115 {
116     cipher->init = init;
117     return 1;
118 }
119
120 int EVP_CIPHER_meth_set_do_cipher(EVP_CIPHER *cipher,
121                                   int (*do_cipher) (EVP_CIPHER_CTX *ctx,
122                                                     unsigned char *out,
123                                                     const unsigned char *in,
124                                                     size_t inl))
125 {
126     cipher->do_cipher = do_cipher;
127     return 1;
128 }
129
130 int EVP_CIPHER_meth_set_cleanup(EVP_CIPHER *cipher,
131                                 int (*cleanup) (EVP_CIPHER_CTX *))
132 {
133     cipher->cleanup = cleanup;
134     return 1;
135 }
136
137 int EVP_CIPHER_meth_set_set_asn1_params(EVP_CIPHER *cipher,
138                                         int (*set_asn1_parameters) (EVP_CIPHER_CTX *,
139                                                                     ASN1_TYPE *))
140 {
141     cipher->set_asn1_parameters = set_asn1_parameters;
142     return 1;
143 }
144
145 int EVP_CIPHER_meth_set_get_asn1_params(EVP_CIPHER *cipher,
146                                         int (*get_asn1_parameters) (EVP_CIPHER_CTX *,
147                                                                     ASN1_TYPE *))
148 {
149     cipher->get_asn1_parameters = get_asn1_parameters;
150     return 1;
151 }
152
153 int EVP_CIPHER_meth_set_ctrl(EVP_CIPHER *cipher,
154                              int (*ctrl) (EVP_CIPHER_CTX *, int type,
155                                           int arg, void *ptr))
156 {
157     cipher->ctrl = ctrl;
158     return 1;
159 }
160
161
162 int (*EVP_CIPHER_meth_get_init(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *ctx,
163                                                           const unsigned char *key,
164                                                           const unsigned char *iv,
165                                                           int enc)
166 {
167     return cipher->init;
168 }
169 int (*EVP_CIPHER_meth_get_do_cipher(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *ctx,
170                                                                unsigned char *out,
171                                                                const unsigned char *in,
172                                                                size_t inl)
173 {
174     return cipher->do_cipher;
175 }
176
177 int (*EVP_CIPHER_meth_get_cleanup(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *)
178 {
179     return cipher->cleanup;
180 }
181
182 int (*EVP_CIPHER_meth_get_set_asn1_params(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *,
183                                                                      ASN1_TYPE *)
184 {
185     return cipher->set_asn1_parameters;
186 }
187
188 int (*EVP_CIPHER_meth_get_get_asn1_params(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *,
189                                                                ASN1_TYPE *)
190 {
191     return cipher->get_asn1_parameters;
192 }
193
194 int (*EVP_CIPHER_meth_get_ctrl(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *,
195                                                           int type, int arg,
196                                                           void *ptr)
197 {
198     return cipher->ctrl;
199 }
200