]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/evp/cmeth_lib.c
40aca34e077b2a3419465997bcaf96ff355afe8a
[thirdparty/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 CRYPTO_THREAD_lock_free(cipher->lock);
59 OPENSSL_free(cipher);
60 }
61 }
62
63 int EVP_CIPHER_up_ref(EVP_CIPHER *cipher)
64 {
65 int ref = 0;
66
67 CRYPTO_UP_REF(&cipher->refcnt, &ref, cipher->lock);
68 return 1;
69 }
70
71 int EVP_CIPHER_meth_set_iv_length(EVP_CIPHER *cipher, int iv_len)
72 {
73 cipher->iv_len = iv_len;
74 return 1;
75 }
76
77 int EVP_CIPHER_meth_set_flags(EVP_CIPHER *cipher, unsigned long flags)
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 cipher->ctx_size = ctx_size;
86 return 1;
87 }
88
89 int EVP_CIPHER_meth_set_init(EVP_CIPHER *cipher,
90 int (*init) (EVP_CIPHER_CTX *ctx,
91 const unsigned char *key,
92 const unsigned char *iv,
93 int enc))
94 {
95 cipher->init = init;
96 return 1;
97 }
98
99 int EVP_CIPHER_meth_set_do_cipher(EVP_CIPHER *cipher,
100 int (*do_cipher) (EVP_CIPHER_CTX *ctx,
101 unsigned char *out,
102 const unsigned char *in,
103 size_t inl))
104 {
105 cipher->do_cipher = do_cipher;
106 return 1;
107 }
108
109 int EVP_CIPHER_meth_set_cleanup(EVP_CIPHER *cipher,
110 int (*cleanup) (EVP_CIPHER_CTX *))
111 {
112 cipher->cleanup = cleanup;
113 return 1;
114 }
115
116 int EVP_CIPHER_meth_set_set_asn1_params(EVP_CIPHER *cipher,
117 int (*set_asn1_parameters) (EVP_CIPHER_CTX *,
118 ASN1_TYPE *))
119 {
120 cipher->set_asn1_parameters = set_asn1_parameters;
121 return 1;
122 }
123
124 int EVP_CIPHER_meth_set_get_asn1_params(EVP_CIPHER *cipher,
125 int (*get_asn1_parameters) (EVP_CIPHER_CTX *,
126 ASN1_TYPE *))
127 {
128 cipher->get_asn1_parameters = get_asn1_parameters;
129 return 1;
130 }
131
132 int EVP_CIPHER_meth_set_ctrl(EVP_CIPHER *cipher,
133 int (*ctrl) (EVP_CIPHER_CTX *, int type,
134 int arg, void *ptr))
135 {
136 cipher->ctrl = ctrl;
137 return 1;
138 }
139
140
141 int (*EVP_CIPHER_meth_get_init(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *ctx,
142 const unsigned char *key,
143 const unsigned char *iv,
144 int enc)
145 {
146 return cipher->init;
147 }
148 int (*EVP_CIPHER_meth_get_do_cipher(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *ctx,
149 unsigned char *out,
150 const unsigned char *in,
151 size_t inl)
152 {
153 return cipher->do_cipher;
154 }
155
156 int (*EVP_CIPHER_meth_get_cleanup(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *)
157 {
158 return cipher->cleanup;
159 }
160
161 int (*EVP_CIPHER_meth_get_set_asn1_params(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *,
162 ASN1_TYPE *)
163 {
164 return cipher->set_asn1_parameters;
165 }
166
167 int (*EVP_CIPHER_meth_get_get_asn1_params(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *,
168 ASN1_TYPE *)
169 {
170 return cipher->get_asn1_parameters;
171 }
172
173 int (*EVP_CIPHER_meth_get_ctrl(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *,
174 int type, int arg,
175 void *ptr)
176 {
177 return cipher->ctrl;
178 }
179