]>
Commit | Line | Data |
---|---|---|
e79f8773 | 1 | /* |
aa6bb135 | 2 | * Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved. |
e79f8773 | 3 | * |
4a8b0c55 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
aa6bb135 RS |
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 | |
e79f8773 RL |
8 | */ |
9 | ||
10 | #include <string.h> | |
11 | ||
12 | #include <openssl/evp.h> | |
13 | #include "internal/evp_int.h" | |
df05f2ce | 14 | #include "internal/provider.h" |
e79f8773 RL |
15 | #include "evp_locl.h" |
16 | ||
17 | EVP_CIPHER *EVP_CIPHER_meth_new(int cipher_type, int block_size, int key_len) | |
18 | { | |
550f974a | 19 | EVP_CIPHER *cipher = evp_cipher_new(); |
43ecb9c3 | 20 | |
e79f8773 RL |
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 | { | |
550f974a | 31 | EVP_CIPHER *to = NULL; |
43ecb9c3 | 32 | |
550f974a RL |
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, | |
464ac47f | 41 | cipher->key_len)) != NULL) { |
df05f2ce MC |
42 | CRYPTO_RWLOCK *lock = to->lock; |
43 | ||
e79f8773 | 44 | memcpy(to, cipher, sizeof(*to)); |
df05f2ce MC |
45 | to->lock = lock; |
46 | } | |
e79f8773 RL |
47 | return to; |
48 | } | |
49 | ||
50 | void EVP_CIPHER_meth_free(EVP_CIPHER *cipher) | |
51 | { | |
550f974a | 52 | EVP_CIPHER_free(cipher); |
e79f8773 RL |
53 | } |
54 | ||
55 | int EVP_CIPHER_meth_set_iv_length(EVP_CIPHER *cipher, int iv_len) | |
56 | { | |
0220fc99 DB |
57 | if (cipher->iv_len != 0) |
58 | return 0; | |
59 | ||
e79f8773 RL |
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 | { | |
0220fc99 DB |
66 | if (cipher->flags != 0) |
67 | return 0; | |
68 | ||
e79f8773 RL |
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 | { | |
0220fc99 DB |
75 | if (cipher->ctx_size != 0) |
76 | return 0; | |
77 | ||
e79f8773 RL |
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 | { | |
0220fc99 DB |
88 | if (cipher->init != NULL) |
89 | return 0; | |
90 | ||
e79f8773 RL |
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 | { | |
0220fc99 DB |
101 | if (cipher->do_cipher != NULL) |
102 | return 0; | |
103 | ||
e79f8773 RL |
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 | { | |
0220fc99 DB |
111 | if (cipher->cleanup != NULL) |
112 | return 0; | |
113 | ||
e79f8773 RL |
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 | { | |
0220fc99 DB |
122 | if (cipher->set_asn1_parameters != NULL) |
123 | return 0; | |
124 | ||
e79f8773 RL |
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 | { | |
0220fc99 DB |
133 | if (cipher->get_asn1_parameters != NULL) |
134 | return 0; | |
135 | ||
e79f8773 RL |
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 | { | |
0220fc99 DB |
144 | if (cipher->ctrl != NULL) |
145 | return 0; | |
146 | ||
e79f8773 RL |
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 |