]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/evp/cmeth_lib.c
Add "origin" field to EVP_CIPHER, EVP_MD
[thirdparty/openssl.git] / crypto / evp / cmeth_lib.c
1 /*
2 * Copyright 2015-2020 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 /*
11 * EVP _meth_ APIs are deprecated for public use, but still ok for
12 * internal use.
13 */
14 #include "internal/deprecated.h"
15
16 #include <string.h>
17
18 #include <openssl/evp.h>
19 #include "crypto/evp.h"
20 #include "internal/provider.h"
21 #include "evp_local.h"
22
23 EVP_CIPHER *EVP_CIPHER_meth_new(int cipher_type, int block_size, int key_len)
24 {
25 EVP_CIPHER *cipher = evp_cipher_new();
26
27 if (cipher != NULL) {
28 cipher->nid = cipher_type;
29 cipher->block_size = block_size;
30 cipher->key_len = key_len;
31 cipher->origin = EVP_ORIG_METH;
32 }
33 return cipher;
34 }
35
36 EVP_CIPHER *EVP_CIPHER_meth_dup(const EVP_CIPHER *cipher)
37 {
38 EVP_CIPHER *to = NULL;
39
40 /*
41 * Non-legacy EVP_CIPHERs can't be duplicated like this.
42 * Use EVP_CIPHER_up_ref() instead.
43 */
44 if (cipher->prov != NULL)
45 return NULL;
46
47 if ((to = EVP_CIPHER_meth_new(cipher->nid, cipher->block_size,
48 cipher->key_len)) != NULL) {
49 CRYPTO_RWLOCK *lock = to->lock;
50
51 memcpy(to, cipher, sizeof(*to));
52 to->lock = lock;
53 }
54 return to;
55 }
56
57 void EVP_CIPHER_meth_free(EVP_CIPHER *cipher)
58 {
59 if (cipher == NULL || cipher->origin != EVP_ORIG_METH)
60 return;
61
62 evp_cipher_free_int(cipher);
63 }
64
65 int EVP_CIPHER_meth_set_iv_length(EVP_CIPHER *cipher, int iv_len)
66 {
67 if (cipher->iv_len != 0)
68 return 0;
69
70 cipher->iv_len = iv_len;
71 return 1;
72 }
73
74 int EVP_CIPHER_meth_set_flags(EVP_CIPHER *cipher, unsigned long flags)
75 {
76 if (cipher->flags != 0)
77 return 0;
78
79 cipher->flags = flags;
80 return 1;
81 }
82
83 int EVP_CIPHER_meth_set_impl_ctx_size(EVP_CIPHER *cipher, int ctx_size)
84 {
85 if (cipher->ctx_size != 0)
86 return 0;
87
88 cipher->ctx_size = ctx_size;
89 return 1;
90 }
91
92 int EVP_CIPHER_meth_set_init(EVP_CIPHER *cipher,
93 int (*init) (EVP_CIPHER_CTX *ctx,
94 const unsigned char *key,
95 const unsigned char *iv,
96 int enc))
97 {
98 if (cipher->init != NULL)
99 return 0;
100
101 cipher->init = init;
102 return 1;
103 }
104
105 int EVP_CIPHER_meth_set_do_cipher(EVP_CIPHER *cipher,
106 int (*do_cipher) (EVP_CIPHER_CTX *ctx,
107 unsigned char *out,
108 const unsigned char *in,
109 size_t inl))
110 {
111 if (cipher->do_cipher != NULL)
112 return 0;
113
114 cipher->do_cipher = do_cipher;
115 return 1;
116 }
117
118 int EVP_CIPHER_meth_set_cleanup(EVP_CIPHER *cipher,
119 int (*cleanup) (EVP_CIPHER_CTX *))
120 {
121 if (cipher->cleanup != NULL)
122 return 0;
123
124 cipher->cleanup = cleanup;
125 return 1;
126 }
127
128 int EVP_CIPHER_meth_set_set_asn1_params(EVP_CIPHER *cipher,
129 int (*set_asn1_parameters) (EVP_CIPHER_CTX *,
130 ASN1_TYPE *))
131 {
132 if (cipher->set_asn1_parameters != NULL)
133 return 0;
134
135 cipher->set_asn1_parameters = set_asn1_parameters;
136 return 1;
137 }
138
139 int EVP_CIPHER_meth_set_get_asn1_params(EVP_CIPHER *cipher,
140 int (*get_asn1_parameters) (EVP_CIPHER_CTX *,
141 ASN1_TYPE *))
142 {
143 if (cipher->get_asn1_parameters != NULL)
144 return 0;
145
146 cipher->get_asn1_parameters = get_asn1_parameters;
147 return 1;
148 }
149
150 int EVP_CIPHER_meth_set_ctrl(EVP_CIPHER *cipher,
151 int (*ctrl) (EVP_CIPHER_CTX *, int type,
152 int arg, void *ptr))
153 {
154 if (cipher->ctrl != NULL)
155 return 0;
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