crypto/evp/pkey_kdf.c: Redo parameter processing
[openssl.git] / crypto / evp / cmeth_lib.c
1 /*
2  * Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 #include <string.h>
11
12 #include <openssl/evp.h>
13 #include "internal/evp_int.h"
14 #include "internal/provider.h"
15 #include "evp_locl.h"
16
17 EVP_CIPHER *EVP_CIPHER_meth_new(int cipher_type, int block_size, int key_len)
18 {
19     EVP_CIPHER *cipher = evp_cipher_new();
20
21     if (cipher != NULL) {
22         cipher->nid = cipher_type;
23         cipher->block_size = block_size;
24         cipher->key_len = key_len;
25     }
26     return cipher;
27 }
28
29 EVP_CIPHER *EVP_CIPHER_meth_dup(const EVP_CIPHER *cipher)
30 {
31     EVP_CIPHER *to = NULL;
32
33     /*
34      * Non-legacy EVP_CIPHERs can't be duplicated like this.
35      * Use EVP_CIPHER_up_ref() instead.
36      */
37     if (cipher->prov != NULL)
38         return NULL;
39
40     if ((to = EVP_CIPHER_meth_new(cipher->nid, cipher->block_size,
41                                   cipher->key_len)) != NULL) {
42         CRYPTO_RWLOCK *lock = to->lock;
43
44         memcpy(to, cipher, sizeof(*to));
45         to->lock = lock;
46     }
47     return to;
48 }
49
50 void EVP_CIPHER_meth_free(EVP_CIPHER *cipher)
51 {
52     EVP_CIPHER_free(cipher);
53 }
54
55 int EVP_CIPHER_meth_set_iv_length(EVP_CIPHER *cipher, int iv_len)
56 {
57     if (cipher->iv_len != 0)
58         return 0;
59
60     cipher->iv_len = iv_len;
61     return 1;
62 }
63
64 int EVP_CIPHER_meth_set_flags(EVP_CIPHER *cipher, unsigned long flags)
65 {
66     if (cipher->flags != 0)
67         return 0;
68
69     cipher->flags = flags;
70     return 1;
71 }
72
73 int EVP_CIPHER_meth_set_impl_ctx_size(EVP_CIPHER *cipher, int ctx_size)
74 {
75     if (cipher->ctx_size != 0)
76         return 0;
77
78     cipher->ctx_size = ctx_size;
79     return 1;
80 }
81
82 int EVP_CIPHER_meth_set_init(EVP_CIPHER *cipher,
83                              int (*init) (EVP_CIPHER_CTX *ctx,
84                                           const unsigned char *key,
85                                           const unsigned char *iv,
86                                           int enc))
87 {
88     if (cipher->init != NULL)
89         return 0;
90
91     cipher->init = init;
92     return 1;
93 }
94
95 int EVP_CIPHER_meth_set_do_cipher(EVP_CIPHER *cipher,
96                                   int (*do_cipher) (EVP_CIPHER_CTX *ctx,
97                                                     unsigned char *out,
98                                                     const unsigned char *in,
99                                                     size_t inl))
100 {
101     if (cipher->do_cipher != NULL)
102         return 0;
103
104     cipher->do_cipher = do_cipher;
105     return 1;
106 }
107
108 int EVP_CIPHER_meth_set_cleanup(EVP_CIPHER *cipher,
109                                 int (*cleanup) (EVP_CIPHER_CTX *))
110 {
111     if (cipher->cleanup != NULL)
112         return 0;
113
114     cipher->cleanup = cleanup;
115     return 1;
116 }
117
118 int EVP_CIPHER_meth_set_set_asn1_params(EVP_CIPHER *cipher,
119                                         int (*set_asn1_parameters) (EVP_CIPHER_CTX *,
120                                                                     ASN1_TYPE *))
121 {
122     if (cipher->set_asn1_parameters != NULL)
123         return 0;
124
125     cipher->set_asn1_parameters = set_asn1_parameters;
126     return 1;
127 }
128
129 int EVP_CIPHER_meth_set_get_asn1_params(EVP_CIPHER *cipher,
130                                         int (*get_asn1_parameters) (EVP_CIPHER_CTX *,
131                                                                     ASN1_TYPE *))
132 {
133     if (cipher->get_asn1_parameters != NULL)
134         return 0;
135
136     cipher->get_asn1_parameters = get_asn1_parameters;
137     return 1;
138 }
139
140 int EVP_CIPHER_meth_set_ctrl(EVP_CIPHER *cipher,
141                              int (*ctrl) (EVP_CIPHER_CTX *, int type,
142                                           int arg, void *ptr))
143 {
144     if (cipher->ctrl != NULL)
145         return 0;
146
147     cipher->ctrl = ctrl;
148     return 1;
149 }
150
151
152 int (*EVP_CIPHER_meth_get_init(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *ctx,
153                                                           const unsigned char *key,
154                                                           const unsigned char *iv,
155                                                           int enc)
156 {
157     return cipher->init;
158 }
159 int (*EVP_CIPHER_meth_get_do_cipher(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *ctx,
160                                                                unsigned char *out,
161                                                                const unsigned char *in,
162                                                                size_t inl)
163 {
164     return cipher->do_cipher;
165 }
166
167 int (*EVP_CIPHER_meth_get_cleanup(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *)
168 {
169     return cipher->cleanup;
170 }
171
172 int (*EVP_CIPHER_meth_get_set_asn1_params(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *,
173                                                                      ASN1_TYPE *)
174 {
175     return cipher->set_asn1_parameters;
176 }
177
178 int (*EVP_CIPHER_meth_get_get_asn1_params(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *,
179                                                                ASN1_TYPE *)
180 {
181     return cipher->get_asn1_parameters;
182 }
183
184 int (*EVP_CIPHER_meth_get_ctrl(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *,
185                                                           int type, int arg,
186                                                           void *ptr)
187 {
188     return cipher->ctrl;
189 }
190