xref: /openssl/crypto/evp/cmeth_lib.c (revision da1c088f)
1 /*
2  * Copyright 2015-2023 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 
EVP_CIPHER_meth_new(int cipher_type,int block_size,int key_len)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 
EVP_CIPHER_meth_dup(const EVP_CIPHER * cipher)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_REF_COUNT refcnt = to->refcnt;
50 
51         memcpy(to, cipher, sizeof(*to));
52         to->refcnt = refcnt;
53         to->origin = EVP_ORIG_METH;
54     }
55     return to;
56 }
57 
EVP_CIPHER_meth_free(EVP_CIPHER * cipher)58 void EVP_CIPHER_meth_free(EVP_CIPHER *cipher)
59 {
60     if (cipher == NULL || cipher->origin != EVP_ORIG_METH)
61        return;
62 
63     evp_cipher_free_int(cipher);
64 }
65 
EVP_CIPHER_meth_set_iv_length(EVP_CIPHER * cipher,int iv_len)66 int EVP_CIPHER_meth_set_iv_length(EVP_CIPHER *cipher, int iv_len)
67 {
68     if (cipher->iv_len != 0)
69         return 0;
70 
71     cipher->iv_len = iv_len;
72     return 1;
73 }
74 
EVP_CIPHER_meth_set_flags(EVP_CIPHER * cipher,unsigned long flags)75 int EVP_CIPHER_meth_set_flags(EVP_CIPHER *cipher, unsigned long flags)
76 {
77     if (cipher->flags != 0)
78         return 0;
79 
80     cipher->flags = flags;
81     return 1;
82 }
83 
EVP_CIPHER_meth_set_impl_ctx_size(EVP_CIPHER * cipher,int ctx_size)84 int EVP_CIPHER_meth_set_impl_ctx_size(EVP_CIPHER *cipher, int ctx_size)
85 {
86     if (cipher->ctx_size != 0)
87         return 0;
88 
89     cipher->ctx_size = ctx_size;
90     return 1;
91 }
92 
EVP_CIPHER_meth_set_init(EVP_CIPHER * cipher,int (* init)(EVP_CIPHER_CTX * ctx,const unsigned char * key,const unsigned char * iv,int enc))93 int EVP_CIPHER_meth_set_init(EVP_CIPHER *cipher,
94                              int (*init) (EVP_CIPHER_CTX *ctx,
95                                           const unsigned char *key,
96                                           const unsigned char *iv,
97                                           int enc))
98 {
99     if (cipher->init != NULL)
100         return 0;
101 
102     cipher->init = init;
103     return 1;
104 }
105 
EVP_CIPHER_meth_set_do_cipher(EVP_CIPHER * cipher,int (* do_cipher)(EVP_CIPHER_CTX * ctx,unsigned char * out,const unsigned char * in,size_t inl))106 int EVP_CIPHER_meth_set_do_cipher(EVP_CIPHER *cipher,
107                                   int (*do_cipher) (EVP_CIPHER_CTX *ctx,
108                                                     unsigned char *out,
109                                                     const unsigned char *in,
110                                                     size_t inl))
111 {
112     if (cipher->do_cipher != NULL)
113         return 0;
114 
115     cipher->do_cipher = do_cipher;
116     return 1;
117 }
118 
EVP_CIPHER_meth_set_cleanup(EVP_CIPHER * cipher,int (* cleanup)(EVP_CIPHER_CTX *))119 int EVP_CIPHER_meth_set_cleanup(EVP_CIPHER *cipher,
120                                 int (*cleanup) (EVP_CIPHER_CTX *))
121 {
122     if (cipher->cleanup != NULL)
123         return 0;
124 
125     cipher->cleanup = cleanup;
126     return 1;
127 }
128 
EVP_CIPHER_meth_set_set_asn1_params(EVP_CIPHER * cipher,int (* set_asn1_parameters)(EVP_CIPHER_CTX *,ASN1_TYPE *))129 int EVP_CIPHER_meth_set_set_asn1_params(EVP_CIPHER *cipher,
130                                         int (*set_asn1_parameters) (EVP_CIPHER_CTX *,
131                                                                     ASN1_TYPE *))
132 {
133     if (cipher->set_asn1_parameters != NULL)
134         return 0;
135 
136     cipher->set_asn1_parameters = set_asn1_parameters;
137     return 1;
138 }
139 
EVP_CIPHER_meth_set_get_asn1_params(EVP_CIPHER * cipher,int (* get_asn1_parameters)(EVP_CIPHER_CTX *,ASN1_TYPE *))140 int EVP_CIPHER_meth_set_get_asn1_params(EVP_CIPHER *cipher,
141                                         int (*get_asn1_parameters) (EVP_CIPHER_CTX *,
142                                                                     ASN1_TYPE *))
143 {
144     if (cipher->get_asn1_parameters != NULL)
145         return 0;
146 
147     cipher->get_asn1_parameters = get_asn1_parameters;
148     return 1;
149 }
150 
EVP_CIPHER_meth_set_ctrl(EVP_CIPHER * cipher,int (* ctrl)(EVP_CIPHER_CTX *,int type,int arg,void * ptr))151 int EVP_CIPHER_meth_set_ctrl(EVP_CIPHER *cipher,
152                              int (*ctrl) (EVP_CIPHER_CTX *, int type,
153                                           int arg, void *ptr))
154 {
155     if (cipher->ctrl != NULL)
156         return 0;
157 
158     cipher->ctrl = ctrl;
159     return 1;
160 }
161 
162 
EVP_CIPHER_meth_get_init(const EVP_CIPHER * cipher)163 int (*EVP_CIPHER_meth_get_init(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *ctx,
164                                                           const unsigned char *key,
165                                                           const unsigned char *iv,
166                                                           int enc)
167 {
168     return cipher->init;
169 }
EVP_CIPHER_meth_get_do_cipher(const EVP_CIPHER * cipher)170 int (*EVP_CIPHER_meth_get_do_cipher(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *ctx,
171                                                                unsigned char *out,
172                                                                const unsigned char *in,
173                                                                size_t inl)
174 {
175     return cipher->do_cipher;
176 }
177 
EVP_CIPHER_meth_get_cleanup(const EVP_CIPHER * cipher)178 int (*EVP_CIPHER_meth_get_cleanup(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *)
179 {
180     return cipher->cleanup;
181 }
182 
EVP_CIPHER_meth_get_set_asn1_params(const EVP_CIPHER * cipher)183 int (*EVP_CIPHER_meth_get_set_asn1_params(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *,
184                                                                      ASN1_TYPE *)
185 {
186     return cipher->set_asn1_parameters;
187 }
188 
EVP_CIPHER_meth_get_get_asn1_params(const EVP_CIPHER * cipher)189 int (*EVP_CIPHER_meth_get_get_asn1_params(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *,
190                                                                ASN1_TYPE *)
191 {
192     return cipher->get_asn1_parameters;
193 }
194 
EVP_CIPHER_meth_get_ctrl(const EVP_CIPHER * cipher)195 int (*EVP_CIPHER_meth_get_ctrl(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *,
196                                                           int type, int arg,
197                                                           void *ptr)
198 {
199     return cipher->ctrl;
200 }
201 
202