Coverage Report

Created: 2025-06-13 06:56

/src/openssl/crypto/evp/cmeth_lib.c
Line
Count
Source (jump to first uncovered line)
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
23
EVP_CIPHER *EVP_CIPHER_meth_new(int cipher_type, int block_size, int key_len)
24
0
{
25
0
    EVP_CIPHER *cipher = evp_cipher_new();
26
27
0
    if (cipher != NULL) {
28
0
        cipher->nid = cipher_type;
29
0
        cipher->block_size = block_size;
30
0
        cipher->key_len = key_len;
31
0
        cipher->origin = EVP_ORIG_METH;
32
0
    }
33
0
    return cipher;
34
0
}
35
36
EVP_CIPHER *EVP_CIPHER_meth_dup(const EVP_CIPHER *cipher)
37
0
{
38
0
    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
0
    if (cipher->prov != NULL)
45
0
        return NULL;
46
47
0
    if ((to = EVP_CIPHER_meth_new(cipher->nid, cipher->block_size,
48
0
                                  cipher->key_len)) != NULL) {
49
0
        CRYPTO_REF_COUNT refcnt = to->refcnt;
50
51
0
        memcpy(to, cipher, sizeof(*to));
52
0
        to->refcnt = refcnt;
53
0
        to->origin = EVP_ORIG_METH;
54
0
    }
55
0
    return to;
56
0
}
57
58
void EVP_CIPHER_meth_free(EVP_CIPHER *cipher)
59
0
{
60
0
    if (cipher == NULL || cipher->origin != EVP_ORIG_METH)
61
0
       return;
62
63
0
    evp_cipher_free_int(cipher);
64
0
}
65
66
int EVP_CIPHER_meth_set_iv_length(EVP_CIPHER *cipher, int iv_len)
67
0
{
68
0
    if (cipher->iv_len != 0)
69
0
        return 0;
70
71
0
    cipher->iv_len = iv_len;
72
0
    return 1;
73
0
}
74
75
int EVP_CIPHER_meth_set_flags(EVP_CIPHER *cipher, unsigned long flags)
76
0
{
77
0
    if (cipher->flags != 0)
78
0
        return 0;
79
80
0
    cipher->flags = flags;
81
0
    return 1;
82
0
}
83
84
int EVP_CIPHER_meth_set_impl_ctx_size(EVP_CIPHER *cipher, int ctx_size)
85
0
{
86
0
    if (cipher->ctx_size != 0)
87
0
        return 0;
88
89
0
    cipher->ctx_size = ctx_size;
90
0
    return 1;
91
0
}
92
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
0
{
99
0
    if (cipher->init != NULL)
100
0
        return 0;
101
102
0
    cipher->init = init;
103
0
    return 1;
104
0
}
105
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
0
{
112
0
    if (cipher->do_cipher != NULL)
113
0
        return 0;
114
115
0
    cipher->do_cipher = do_cipher;
116
0
    return 1;
117
0
}
118
119
int EVP_CIPHER_meth_set_cleanup(EVP_CIPHER *cipher,
120
                                int (*cleanup) (EVP_CIPHER_CTX *))
121
0
{
122
0
    if (cipher->cleanup != NULL)
123
0
        return 0;
124
125
0
    cipher->cleanup = cleanup;
126
0
    return 1;
127
0
}
128
129
int EVP_CIPHER_meth_set_set_asn1_params(EVP_CIPHER *cipher,
130
                                        int (*set_asn1_parameters) (EVP_CIPHER_CTX *,
131
                                                                    ASN1_TYPE *))
132
0
{
133
0
    if (cipher->set_asn1_parameters != NULL)
134
0
        return 0;
135
136
0
    cipher->set_asn1_parameters = set_asn1_parameters;
137
0
    return 1;
138
0
}
139
140
int EVP_CIPHER_meth_set_get_asn1_params(EVP_CIPHER *cipher,
141
                                        int (*get_asn1_parameters) (EVP_CIPHER_CTX *,
142
                                                                    ASN1_TYPE *))
143
0
{
144
0
    if (cipher->get_asn1_parameters != NULL)
145
0
        return 0;
146
147
0
    cipher->get_asn1_parameters = get_asn1_parameters;
148
0
    return 1;
149
0
}
150
151
int EVP_CIPHER_meth_set_ctrl(EVP_CIPHER *cipher,
152
                             int (*ctrl) (EVP_CIPHER_CTX *, int type,
153
                                          int arg, void *ptr))
154
0
{
155
0
    if (cipher->ctrl != NULL)
156
0
        return 0;
157
158
0
    cipher->ctrl = ctrl;
159
0
    return 1;
160
0
}
161
162
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
0
{
168
0
    return cipher->init;
169
0
}
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
0
{
175
0
    return cipher->do_cipher;
176
0
}
177
178
int (*EVP_CIPHER_meth_get_cleanup(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *)
179
0
{
180
0
    return cipher->cleanup;
181
0
}
182
183
int (*EVP_CIPHER_meth_get_set_asn1_params(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *,
184
                                                                     ASN1_TYPE *)
185
0
{
186
0
    return cipher->set_asn1_parameters;
187
0
}
188
189
int (*EVP_CIPHER_meth_get_get_asn1_params(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *,
190
                                                               ASN1_TYPE *)
191
0
{
192
0
    return cipher->get_asn1_parameters;
193
0
}
194
195
int (*EVP_CIPHER_meth_get_ctrl(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *,
196
                                                          int type, int arg,
197
                                                          void *ptr)
198
0
{
199
0
    return cipher->ctrl;
200
0
}
201