Disallow change EVP_MD properties once set
[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 = evp_cipher_new();
20
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 {
31     EVP_CIPHER *to = NULL;
32
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,
41                                   cipher->key_len)) == NULL) {
42         CRYPTO_RWLOCK *lock = to->lock;
43
44         memcpy(to, cipher, sizeof(*to));
45         to->lock = lock;
46     }
47     return to;
48 }
49
50 void EVP_CIPHER_meth_free(EVP_CIPHER *cipher)
51 {
52     EVP_CIPHER_free(cipher);
53 }
54
55 int EVP_CIPHER_meth_set_iv_length(EVP_CIPHER *cipher, int iv_len)
56 {
57     cipher->iv_len = iv_len;
58     return 1;
59 }
60
61 int EVP_CIPHER_meth_set_flags(EVP_CIPHER *cipher, unsigned long flags)
62 {
63     cipher->flags = flags;
64     return 1;
65 }
66
67 int EVP_CIPHER_meth_set_impl_ctx_size(EVP_CIPHER *cipher, int ctx_size)
68 {
69     cipher->ctx_size = ctx_size;
70     return 1;
71 }
72
73 int EVP_CIPHER_meth_set_init(EVP_CIPHER *cipher,
74                              int (*init) (EVP_CIPHER_CTX *ctx,
75                                           const unsigned char *key,
76                                           const unsigned char *iv,
77                                           int enc))
78 {
79     cipher->init = init;
80     return 1;
81 }
82
83 int EVP_CIPHER_meth_set_do_cipher(EVP_CIPHER *cipher,
84                                   int (*do_cipher) (EVP_CIPHER_CTX *ctx,
85                                                     unsigned char *out,
86                                                     const unsigned char *in,
87                                                     size_t inl))
88 {
89     cipher->do_cipher = do_cipher;
90     return 1;
91 }
92
93 int EVP_CIPHER_meth_set_cleanup(EVP_CIPHER *cipher,
94                                 int (*cleanup) (EVP_CIPHER_CTX *))
95 {
96     cipher->cleanup = cleanup;
97     return 1;
98 }
99
100 int EVP_CIPHER_meth_set_set_asn1_params(EVP_CIPHER *cipher,
101                                         int (*set_asn1_parameters) (EVP_CIPHER_CTX *,
102                                                                     ASN1_TYPE *))
103 {
104     cipher->set_asn1_parameters = set_asn1_parameters;
105     return 1;
106 }
107
108 int EVP_CIPHER_meth_set_get_asn1_params(EVP_CIPHER *cipher,
109                                         int (*get_asn1_parameters) (EVP_CIPHER_CTX *,
110                                                                     ASN1_TYPE *))
111 {
112     cipher->get_asn1_parameters = get_asn1_parameters;
113     return 1;
114 }
115
116 int EVP_CIPHER_meth_set_ctrl(EVP_CIPHER *cipher,
117                              int (*ctrl) (EVP_CIPHER_CTX *, int type,
118                                           int arg, void *ptr))
119 {
120     cipher->ctrl = ctrl;
121     return 1;
122 }
123
124
125 int (*EVP_CIPHER_meth_get_init(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *ctx,
126                                                           const unsigned char *key,
127                                                           const unsigned char *iv,
128                                                           int enc)
129 {
130     return cipher->init;
131 }
132 int (*EVP_CIPHER_meth_get_do_cipher(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *ctx,
133                                                                unsigned char *out,
134                                                                const unsigned char *in,
135                                                                size_t inl)
136 {
137     return cipher->do_cipher;
138 }
139
140 int (*EVP_CIPHER_meth_get_cleanup(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *)
141 {
142     return cipher->cleanup;
143 }
144
145 int (*EVP_CIPHER_meth_get_set_asn1_params(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *,
146                                                                      ASN1_TYPE *)
147 {
148     return cipher->set_asn1_parameters;
149 }
150
151 int (*EVP_CIPHER_meth_get_get_asn1_params(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *,
152                                                                ASN1_TYPE *)
153 {
154     return cipher->get_asn1_parameters;
155 }
156
157 int (*EVP_CIPHER_meth_get_ctrl(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *,
158                                                           int type, int arg,
159                                                           void *ptr)
160 {
161     return cipher->ctrl;
162 }
163