51c9b6ece20fb37169bcf393c18e4687a3dddfd2
[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 = OPENSSL_zalloc(sizeof(EVP_CIPHER));
20
21     if (cipher != NULL) {
22         cipher->nid = cipher_type;
23         cipher->block_size = block_size;
24         cipher->key_len = key_len;
25         cipher->lock = CRYPTO_THREAD_lock_new();
26         if (cipher->lock == NULL) {
27             OPENSSL_free(cipher);
28             return NULL;
29         }
30         cipher->refcnt = 1;
31     }
32     return cipher;
33 }
34
35 EVP_CIPHER *EVP_CIPHER_meth_dup(const EVP_CIPHER *cipher)
36 {
37     EVP_CIPHER *to = EVP_CIPHER_meth_new(cipher->nid, cipher->block_size,
38                                          cipher->key_len);
39
40     if (to != NULL) {
41         CRYPTO_RWLOCK *lock = to->lock;
42
43         memcpy(to, cipher, sizeof(*to));
44         to->lock = lock;
45     }
46     return to;
47 }
48
49 void EVP_CIPHER_meth_free(EVP_CIPHER *cipher)
50 {
51     if (cipher != NULL) {
52         int i;
53
54         CRYPTO_DOWN_REF(&cipher->refcnt, &i, cipher->lock);
55         if (i > 0)
56             return;
57         ossl_provider_free(cipher->prov);
58         OPENSSL_free(cipher->name);
59         CRYPTO_THREAD_lock_free(cipher->lock);
60         OPENSSL_free(cipher);
61     }
62 }
63
64 int EVP_CIPHER_up_ref(EVP_CIPHER *cipher)
65 {
66     int ref = 0;
67
68     CRYPTO_UP_REF(&cipher->refcnt, &ref, cipher->lock);
69     return 1;
70 }
71
72 int EVP_CIPHER_meth_set_iv_length(EVP_CIPHER *cipher, int iv_len)
73 {
74     cipher->iv_len = iv_len;
75     return 1;
76 }
77
78 int EVP_CIPHER_meth_set_flags(EVP_CIPHER *cipher, unsigned long flags)
79 {
80     cipher->flags = flags;
81     return 1;
82 }
83
84 int EVP_CIPHER_meth_set_impl_ctx_size(EVP_CIPHER *cipher, int ctx_size)
85 {
86     cipher->ctx_size = ctx_size;
87     return 1;
88 }
89
90 int EVP_CIPHER_meth_set_init(EVP_CIPHER *cipher,
91                              int (*init) (EVP_CIPHER_CTX *ctx,
92                                           const unsigned char *key,
93                                           const unsigned char *iv,
94                                           int enc))
95 {
96     cipher->init = init;
97     return 1;
98 }
99
100 int EVP_CIPHER_meth_set_do_cipher(EVP_CIPHER *cipher,
101                                   int (*do_cipher) (EVP_CIPHER_CTX *ctx,
102                                                     unsigned char *out,
103                                                     const unsigned char *in,
104                                                     size_t inl))
105 {
106     cipher->do_cipher = do_cipher;
107     return 1;
108 }
109
110 int EVP_CIPHER_meth_set_cleanup(EVP_CIPHER *cipher,
111                                 int (*cleanup) (EVP_CIPHER_CTX *))
112 {
113     cipher->cleanup = cleanup;
114     return 1;
115 }
116
117 int EVP_CIPHER_meth_set_set_asn1_params(EVP_CIPHER *cipher,
118                                         int (*set_asn1_parameters) (EVP_CIPHER_CTX *,
119                                                                     ASN1_TYPE *))
120 {
121     cipher->set_asn1_parameters = set_asn1_parameters;
122     return 1;
123 }
124
125 int EVP_CIPHER_meth_set_get_asn1_params(EVP_CIPHER *cipher,
126                                         int (*get_asn1_parameters) (EVP_CIPHER_CTX *,
127                                                                     ASN1_TYPE *))
128 {
129     cipher->get_asn1_parameters = get_asn1_parameters;
130     return 1;
131 }
132
133 int EVP_CIPHER_meth_set_ctrl(EVP_CIPHER *cipher,
134                              int (*ctrl) (EVP_CIPHER_CTX *, int type,
135                                           int arg, void *ptr))
136 {
137     cipher->ctrl = ctrl;
138     return 1;
139 }
140
141
142 int (*EVP_CIPHER_meth_get_init(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *ctx,
143                                                           const unsigned char *key,
144                                                           const unsigned char *iv,
145                                                           int enc)
146 {
147     return cipher->init;
148 }
149 int (*EVP_CIPHER_meth_get_do_cipher(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *ctx,
150                                                                unsigned char *out,
151                                                                const unsigned char *in,
152                                                                size_t inl)
153 {
154     return cipher->do_cipher;
155 }
156
157 int (*EVP_CIPHER_meth_get_cleanup(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *)
158 {
159     return cipher->cleanup;
160 }
161
162 int (*EVP_CIPHER_meth_get_set_asn1_params(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *,
163                                                                      ASN1_TYPE *)
164 {
165     return cipher->set_asn1_parameters;
166 }
167
168 int (*EVP_CIPHER_meth_get_get_asn1_params(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *,
169                                                                ASN1_TYPE *)
170 {
171     return cipher->get_asn1_parameters;
172 }
173
174 int (*EVP_CIPHER_meth_get_ctrl(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *,
175                                                           int type, int arg,
176                                                           void *ptr)
177 {
178     return cipher->ctrl;
179 }
180