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