/src/openssl30/crypto/evp/evp_enc.c
| Line | Count | Source (jump to first uncovered line) | 
| 1 |  | /* | 
| 2 |  |  * Copyright 1995-2022 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 |  | /* We need to use some engine deprecated APIs */ | 
| 11 |  | #define OPENSSL_SUPPRESS_DEPRECATED | 
| 12 |  |  | 
| 13 |  | #include <stdio.h> | 
| 14 |  | #include <limits.h> | 
| 15 |  | #include <assert.h> | 
| 16 |  | #include <openssl/evp.h> | 
| 17 |  | #include <openssl/err.h> | 
| 18 |  | #include <openssl/rand.h> | 
| 19 |  | #ifndef FIPS_MODULE | 
| 20 |  | # include <openssl/engine.h> | 
| 21 |  | #endif | 
| 22 |  | #include <openssl/params.h> | 
| 23 |  | #include <openssl/core_names.h> | 
| 24 |  | #include "internal/cryptlib.h" | 
| 25 |  | #include "internal/provider.h" | 
| 26 |  | #include "internal/core.h" | 
| 27 |  | #include "crypto/evp.h" | 
| 28 |  | #include "evp_local.h" | 
| 29 |  |  | 
| 30 |  | int EVP_CIPHER_CTX_reset(EVP_CIPHER_CTX *ctx) | 
| 31 | 17.0k | { | 
| 32 | 17.0k |     if (ctx == NULL) | 
| 33 | 0 |         return 1; | 
| 34 |  |  | 
| 35 | 17.0k |     if (ctx->cipher == NULL || ctx->cipher->prov == NULL) | 
| 36 | 7.72k |         goto legacy; | 
| 37 |  |  | 
| 38 | 9.29k |     if (ctx->algctx != NULL) { | 
| 39 | 9.29k |         if (ctx->cipher->freectx != NULL) | 
| 40 | 9.29k |             ctx->cipher->freectx(ctx->algctx); | 
| 41 | 9.29k |         ctx->algctx = NULL; | 
| 42 | 9.29k |     } | 
| 43 | 9.29k |     if (ctx->fetched_cipher != NULL) | 
| 44 | 9.29k |         EVP_CIPHER_free(ctx->fetched_cipher); | 
| 45 | 9.29k |     memset(ctx, 0, sizeof(*ctx)); | 
| 46 | 9.29k |     ctx->iv_len = -1; | 
| 47 |  |  | 
| 48 | 9.29k |     return 1; | 
| 49 |  |  | 
| 50 |  |     /* Remove legacy code below when legacy support is removed. */ | 
| 51 | 7.72k |  legacy: | 
| 52 |  |  | 
| 53 | 7.72k |     if (ctx->cipher != NULL) { | 
| 54 | 0 |         if (ctx->cipher->cleanup && !ctx->cipher->cleanup(ctx)) | 
| 55 | 0 |             return 0; | 
| 56 |  |         /* Cleanse cipher context data */ | 
| 57 | 0 |         if (ctx->cipher_data && ctx->cipher->ctx_size) | 
| 58 | 0 |             OPENSSL_cleanse(ctx->cipher_data, ctx->cipher->ctx_size); | 
| 59 | 0 |     } | 
| 60 | 7.72k |     OPENSSL_free(ctx->cipher_data); | 
| 61 | 7.72k | #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE) | 
| 62 | 7.72k |     ENGINE_finish(ctx->engine); | 
| 63 | 7.72k | #endif | 
| 64 | 7.72k |     memset(ctx, 0, sizeof(*ctx)); | 
| 65 | 7.72k |     ctx->iv_len = -1; | 
| 66 | 7.72k |     return 1; | 
| 67 | 7.72k | } | 
| 68 |  |  | 
| 69 |  | EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void) | 
| 70 | 15.6k | { | 
| 71 | 15.6k |     return OPENSSL_zalloc(sizeof(EVP_CIPHER_CTX)); | 
| 72 | 15.6k | } | 
| 73 |  |  | 
| 74 |  | void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx) | 
| 75 | 90.9k | { | 
| 76 | 90.9k |     if (ctx == NULL) | 
| 77 | 75.4k |         return; | 
| 78 | 15.5k |     EVP_CIPHER_CTX_reset(ctx); | 
| 79 | 15.5k |     OPENSSL_free(ctx); | 
| 80 | 15.5k | } | 
| 81 |  |  | 
| 82 |  | static int evp_cipher_init_internal(EVP_CIPHER_CTX *ctx, | 
| 83 |  |                                     const EVP_CIPHER *cipher, | 
| 84 |  |                                     ENGINE *impl, const unsigned char *key, | 
| 85 |  |                                     const unsigned char *iv, int enc, | 
| 86 |  |                                     const OSSL_PARAM params[]) | 
| 87 | 111k | { | 
| 88 | 111k |     int n; | 
| 89 | 111k | #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE) | 
| 90 | 111k |     ENGINE *tmpimpl = NULL; | 
| 91 | 111k | #endif | 
| 92 |  |  | 
| 93 | 111k |     ctx->iv_len = -1; | 
| 94 |  |  | 
| 95 |  |     /* | 
| 96 |  |      * enc == 1 means we are encrypting. | 
| 97 |  |      * enc == 0 means we are decrypting. | 
| 98 |  |      * enc == -1 means, use the previously initialised value for encrypt/decrypt | 
| 99 |  |      */ | 
| 100 | 111k |     if (enc == -1) { | 
| 101 | 49.6k |         enc = ctx->encrypt; | 
| 102 | 62.1k |     } else { | 
| 103 | 62.1k |         if (enc) | 
| 104 | 10.6k |             enc = 1; | 
| 105 | 62.1k |         ctx->encrypt = enc; | 
| 106 | 62.1k |     } | 
| 107 |  |  | 
| 108 | 111k |     if (cipher == NULL && ctx->cipher == NULL) { | 
| 109 | 0 |         ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET); | 
| 110 | 0 |         return 0; | 
| 111 | 0 |     } | 
| 112 |  |  | 
| 113 |  |     /* Code below to be removed when legacy support is dropped. */ | 
| 114 |  |  | 
| 115 | 111k | #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE) | 
| 116 |  |     /* | 
| 117 |  |      * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so | 
| 118 |  |      * this context may already have an ENGINE! Try to avoid releasing the | 
| 119 |  |      * previous handle, re-querying for an ENGINE, and having a | 
| 120 |  |      * reinitialisation, when it may all be unnecessary. | 
| 121 |  |      */ | 
| 122 | 111k |     if (ctx->engine && ctx->cipher | 
| 123 | 111k |         && (cipher == NULL || cipher->nid == ctx->cipher->nid)) | 
| 124 | 0 |         goto skip_to_init; | 
| 125 |  |  | 
| 126 | 111k |     if (cipher != NULL && impl == NULL) { | 
| 127 |  |          /* Ask if an ENGINE is reserved for this job */ | 
| 128 | 9.29k |         tmpimpl = ENGINE_get_cipher_engine(cipher->nid); | 
| 129 | 9.29k |     } | 
| 130 | 111k | #endif | 
| 131 |  |  | 
| 132 |  |     /* | 
| 133 |  |      * If there are engines involved then we should use legacy handling for now. | 
| 134 |  |      */ | 
| 135 | 111k |     if (ctx->engine != NULL | 
| 136 | 111k | #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE) | 
| 137 | 111k |             || tmpimpl != NULL | 
| 138 | 111k | #endif | 
| 139 | 111k |             || impl != NULL | 
| 140 | 111k |             || (cipher != NULL && cipher->origin == EVP_ORIG_METH) | 
| 141 | 111k |             || (cipher == NULL && ctx->cipher != NULL | 
| 142 | 111k |                                && ctx->cipher->origin == EVP_ORIG_METH)) { | 
| 143 | 0 |         if (ctx->cipher == ctx->fetched_cipher) | 
| 144 | 0 |             ctx->cipher = NULL; | 
| 145 | 0 |         EVP_CIPHER_free(ctx->fetched_cipher); | 
| 146 | 0 |         ctx->fetched_cipher = NULL; | 
| 147 | 0 |         goto legacy; | 
| 148 | 0 |     } | 
| 149 |  |     /* | 
| 150 |  |      * Ensure a context left lying around from last time is cleared | 
| 151 |  |      * (legacy code) | 
| 152 |  |      */ | 
| 153 | 111k |     if (cipher != NULL && ctx->cipher != NULL) { | 
| 154 | 0 |         if (ctx->cipher->cleanup != NULL && !ctx->cipher->cleanup(ctx)) | 
| 155 | 0 |             return 0; | 
| 156 | 0 |         OPENSSL_clear_free(ctx->cipher_data, ctx->cipher->ctx_size); | 
| 157 | 0 |         ctx->cipher_data = NULL; | 
| 158 | 0 |     } | 
| 159 |  |  | 
| 160 |  |     /* Start of non-legacy code below */ | 
| 161 |  |  | 
| 162 |  |     /* Ensure a context left lying around from last time is cleared */ | 
| 163 | 111k |     if (cipher != NULL && ctx->cipher != NULL) { | 
| 164 | 0 |         unsigned long flags = ctx->flags; | 
| 165 |  | 
 | 
| 166 | 0 |         EVP_CIPHER_CTX_reset(ctx); | 
| 167 |  |         /* Restore encrypt and flags */ | 
| 168 | 0 |         ctx->encrypt = enc; | 
| 169 | 0 |         ctx->flags = flags; | 
| 170 | 0 |     } | 
| 171 |  |  | 
| 172 | 111k |     if (cipher == NULL) | 
| 173 | 102k |         cipher = ctx->cipher; | 
| 174 |  |  | 
| 175 | 111k |     if (cipher->prov == NULL) { | 
| 176 |  | #ifdef FIPS_MODULE | 
| 177 |  |         /* We only do explicit fetches inside the FIPS module */ | 
| 178 |  |         ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); | 
| 179 |  |         return 0; | 
| 180 |  | #else | 
| 181 | 0 |         EVP_CIPHER *provciph = | 
| 182 | 0 |             EVP_CIPHER_fetch(NULL, | 
| 183 | 0 |                              cipher->nid == NID_undef ? "NULL" | 
| 184 | 0 |                                                       : OBJ_nid2sn(cipher->nid), | 
| 185 | 0 |                              ""); | 
| 186 |  | 
 | 
| 187 | 0 |         if (provciph == NULL) | 
| 188 | 0 |             return 0; | 
| 189 | 0 |         cipher = provciph; | 
| 190 | 0 |         EVP_CIPHER_free(ctx->fetched_cipher); | 
| 191 | 0 |         ctx->fetched_cipher = provciph; | 
| 192 | 0 | #endif | 
| 193 | 0 |     } | 
| 194 |  |  | 
| 195 | 111k |     if (cipher->prov != NULL) { | 
| 196 | 111k |         if (!EVP_CIPHER_up_ref((EVP_CIPHER *)cipher)) { | 
| 197 | 0 |             ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); | 
| 198 | 0 |             return 0; | 
| 199 | 0 |         } | 
| 200 | 111k |         EVP_CIPHER_free(ctx->fetched_cipher); | 
| 201 | 111k |         ctx->fetched_cipher = (EVP_CIPHER *)cipher; | 
| 202 | 111k |     } | 
| 203 | 111k |     ctx->cipher = cipher; | 
| 204 | 111k |     if (ctx->algctx == NULL) { | 
| 205 | 9.29k |         ctx->algctx = ctx->cipher->newctx(ossl_provider_ctx(cipher->prov)); | 
| 206 | 9.29k |         if (ctx->algctx == NULL) { | 
| 207 | 0 |             ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); | 
| 208 | 0 |             return 0; | 
| 209 | 0 |         } | 
| 210 | 9.29k |     } | 
| 211 |  |  | 
| 212 | 111k |     if ((ctx->flags & EVP_CIPH_NO_PADDING) != 0) { | 
| 213 |  |         /* | 
| 214 |  |          * If this ctx was already set up for no padding then we need to tell | 
| 215 |  |          * the new cipher about it. | 
| 216 |  |          */ | 
| 217 | 0 |         if (!EVP_CIPHER_CTX_set_padding(ctx, 0)) | 
| 218 | 0 |             return 0; | 
| 219 | 0 |     } | 
| 220 |  |  | 
| 221 | 111k |     if (enc) { | 
| 222 | 59.7k |         if (ctx->cipher->einit == NULL) { | 
| 223 | 0 |             ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); | 
| 224 | 0 |             return 0; | 
| 225 | 0 |         } | 
| 226 |  |  | 
| 227 | 59.7k |         return ctx->cipher->einit(ctx->algctx, | 
| 228 | 59.7k |                                   key, | 
| 229 | 59.7k |                                   key == NULL ? 0 | 
| 230 | 59.7k |                                               : EVP_CIPHER_CTX_get_key_length(ctx), | 
| 231 | 59.7k |                                   iv, | 
| 232 | 59.7k |                                   iv == NULL ? 0 | 
| 233 | 59.7k |                                              : EVP_CIPHER_CTX_get_iv_length(ctx), | 
| 234 | 59.7k |                                   params); | 
| 235 | 59.7k |     } | 
| 236 |  |  | 
| 237 | 52.1k |     if (ctx->cipher->dinit == NULL) { | 
| 238 | 0 |         ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); | 
| 239 | 0 |         return 0; | 
| 240 | 0 |     } | 
| 241 |  |  | 
| 242 | 52.1k |     return ctx->cipher->dinit(ctx->algctx, | 
| 243 | 52.1k |                               key, | 
| 244 | 52.1k |                               key == NULL ? 0 | 
| 245 | 52.1k |                                           : EVP_CIPHER_CTX_get_key_length(ctx), | 
| 246 | 52.1k |                               iv, | 
| 247 | 52.1k |                               iv == NULL ? 0 | 
| 248 | 52.1k |                                          : EVP_CIPHER_CTX_get_iv_length(ctx), | 
| 249 | 52.1k |                                   params); | 
| 250 |  |  | 
| 251 |  |     /* Code below to be removed when legacy support is dropped. */ | 
| 252 | 0 |  legacy: | 
| 253 |  | 
 | 
| 254 | 0 |     if (cipher != NULL) { | 
| 255 |  |         /* | 
| 256 |  |          * Ensure a context left lying around from last time is cleared (we | 
| 257 |  |          * previously attempted to avoid this if the same ENGINE and | 
| 258 |  |          * EVP_CIPHER could be used). | 
| 259 |  |          */ | 
| 260 | 0 |         if (ctx->cipher) { | 
| 261 | 0 |             unsigned long flags = ctx->flags; | 
| 262 | 0 |             EVP_CIPHER_CTX_reset(ctx); | 
| 263 |  |             /* Restore encrypt and flags */ | 
| 264 | 0 |             ctx->encrypt = enc; | 
| 265 | 0 |             ctx->flags = flags; | 
| 266 | 0 |         } | 
| 267 | 0 | #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE) | 
| 268 | 0 |         if (impl != NULL) { | 
| 269 | 0 |             if (!ENGINE_init(impl)) { | 
| 270 | 0 |                 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); | 
| 271 | 0 |                 return 0; | 
| 272 | 0 |             } | 
| 273 | 0 |         } else { | 
| 274 | 0 |             impl = tmpimpl; | 
| 275 | 0 |         } | 
| 276 | 0 |         if (impl != NULL) { | 
| 277 |  |             /* There's an ENGINE for this job ... (apparently) */ | 
| 278 | 0 |             const EVP_CIPHER *c = ENGINE_get_cipher(impl, cipher->nid); | 
| 279 |  | 
 | 
| 280 | 0 |             if (c == NULL) { | 
| 281 |  |                 /* | 
| 282 |  |                  * One positive side-effect of US's export control history, | 
| 283 |  |                  * is that we should at least be able to avoid using US | 
| 284 |  |                  * misspellings of "initialisation"? | 
| 285 |  |                  */ | 
| 286 | 0 |                 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); | 
| 287 | 0 |                 return 0; | 
| 288 | 0 |             } | 
| 289 |  |             /* We'll use the ENGINE's private cipher definition */ | 
| 290 | 0 |             cipher = c; | 
| 291 |  |             /* | 
| 292 |  |              * Store the ENGINE functional reference so we know 'cipher' came | 
| 293 |  |              * from an ENGINE and we need to release it when done. | 
| 294 |  |              */ | 
| 295 | 0 |             ctx->engine = impl; | 
| 296 | 0 |         } else { | 
| 297 | 0 |             ctx->engine = NULL; | 
| 298 | 0 |         } | 
| 299 | 0 | #endif | 
| 300 |  |  | 
| 301 | 0 |         ctx->cipher = cipher; | 
| 302 | 0 |         if (ctx->cipher->ctx_size) { | 
| 303 | 0 |             ctx->cipher_data = OPENSSL_zalloc(ctx->cipher->ctx_size); | 
| 304 | 0 |             if (ctx->cipher_data == NULL) { | 
| 305 | 0 |                 ctx->cipher = NULL; | 
| 306 | 0 |                 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE); | 
| 307 | 0 |                 return 0; | 
| 308 | 0 |             } | 
| 309 | 0 |         } else { | 
| 310 | 0 |             ctx->cipher_data = NULL; | 
| 311 | 0 |         } | 
| 312 | 0 |         ctx->key_len = cipher->key_len; | 
| 313 |  |         /* Preserve wrap enable flag, zero everything else */ | 
| 314 | 0 |         ctx->flags &= EVP_CIPHER_CTX_FLAG_WRAP_ALLOW; | 
| 315 | 0 |         if (ctx->cipher->flags & EVP_CIPH_CTRL_INIT) { | 
| 316 | 0 |             if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_INIT, 0, NULL) <= 0) { | 
| 317 | 0 |                 ctx->cipher = NULL; | 
| 318 | 0 |                 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); | 
| 319 | 0 |                 return 0; | 
| 320 | 0 |             } | 
| 321 | 0 |         } | 
| 322 | 0 |     } | 
| 323 | 0 | #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE) | 
| 324 | 0 |  skip_to_init: | 
| 325 | 0 | #endif | 
| 326 | 0 |     if (ctx->cipher == NULL) | 
| 327 | 0 |         return 0; | 
| 328 |  |  | 
| 329 |  |     /* we assume block size is a power of 2 in *cryptUpdate */ | 
| 330 | 0 |     OPENSSL_assert(ctx->cipher->block_size == 1 | 
| 331 | 0 |                    || ctx->cipher->block_size == 8 | 
| 332 | 0 |                    || ctx->cipher->block_size == 16); | 
| 333 |  | 
 | 
| 334 | 0 |     if (!(ctx->flags & EVP_CIPHER_CTX_FLAG_WRAP_ALLOW) | 
| 335 | 0 |         && EVP_CIPHER_CTX_get_mode(ctx) == EVP_CIPH_WRAP_MODE) { | 
| 336 | 0 |         ERR_raise(ERR_LIB_EVP, EVP_R_WRAP_MODE_NOT_ALLOWED); | 
| 337 | 0 |         return 0; | 
| 338 | 0 |     } | 
| 339 |  |  | 
| 340 | 0 |     if ((EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(ctx)) | 
| 341 | 0 |                 & EVP_CIPH_CUSTOM_IV) == 0) { | 
| 342 | 0 |         switch (EVP_CIPHER_CTX_get_mode(ctx)) { | 
| 343 |  |  | 
| 344 | 0 |         case EVP_CIPH_STREAM_CIPHER: | 
| 345 | 0 |         case EVP_CIPH_ECB_MODE: | 
| 346 | 0 |             break; | 
| 347 |  |  | 
| 348 | 0 |         case EVP_CIPH_CFB_MODE: | 
| 349 | 0 |         case EVP_CIPH_OFB_MODE: | 
| 350 |  | 
 | 
| 351 | 0 |             ctx->num = 0; | 
| 352 |  |             /* fall-through */ | 
| 353 |  | 
 | 
| 354 | 0 |         case EVP_CIPH_CBC_MODE: | 
| 355 | 0 |             n = EVP_CIPHER_CTX_get_iv_length(ctx); | 
| 356 | 0 |             if (n < 0 || n > (int)sizeof(ctx->iv)) { | 
| 357 | 0 |                 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_IV_LENGTH); | 
| 358 | 0 |                 return 0; | 
| 359 | 0 |             } | 
| 360 | 0 |             if (iv != NULL) | 
| 361 | 0 |                 memcpy(ctx->oiv, iv, n); | 
| 362 | 0 |             memcpy(ctx->iv, ctx->oiv, n); | 
| 363 | 0 |             break; | 
| 364 |  |  | 
| 365 | 0 |         case EVP_CIPH_CTR_MODE: | 
| 366 | 0 |             ctx->num = 0; | 
| 367 |  |             /* Don't reuse IV for CTR mode */ | 
| 368 | 0 |             if (iv != NULL) { | 
| 369 | 0 |                 n = EVP_CIPHER_CTX_get_iv_length(ctx); | 
| 370 | 0 |                 if (n <= 0 || n > (int)sizeof(ctx->iv)) { | 
| 371 | 0 |                     ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_IV_LENGTH); | 
| 372 | 0 |                     return 0; | 
| 373 | 0 |                 } | 
| 374 | 0 |                 memcpy(ctx->iv, iv, n); | 
| 375 | 0 |             } | 
| 376 | 0 |             break; | 
| 377 |  |  | 
| 378 | 0 |         default: | 
| 379 | 0 |             return 0; | 
| 380 | 0 |         } | 
| 381 | 0 |     } | 
| 382 |  |  | 
| 383 | 0 |     if (key != NULL || (ctx->cipher->flags & EVP_CIPH_ALWAYS_CALL_INIT)) { | 
| 384 | 0 |         if (!ctx->cipher->init(ctx, key, iv, enc)) | 
| 385 | 0 |             return 0; | 
| 386 | 0 |     } | 
| 387 | 0 |     ctx->buf_len = 0; | 
| 388 | 0 |     ctx->final_used = 0; | 
| 389 | 0 |     ctx->block_mask = ctx->cipher->block_size - 1; | 
| 390 | 0 |     return 1; | 
| 391 | 0 | } | 
| 392 |  |  | 
| 393 |  | int EVP_CipherInit_ex2(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, | 
| 394 |  |                        const unsigned char *key, const unsigned char *iv, | 
| 395 |  |                        int enc, const OSSL_PARAM params[]) | 
| 396 | 0 | { | 
| 397 | 0 |     return evp_cipher_init_internal(ctx, cipher, NULL, key, iv, enc, params); | 
| 398 | 0 | } | 
| 399 |  |  | 
| 400 |  | int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, | 
| 401 |  |                    const unsigned char *key, const unsigned char *iv, int enc) | 
| 402 | 0 | { | 
| 403 | 0 |     if (cipher != NULL) | 
| 404 | 0 |         EVP_CIPHER_CTX_reset(ctx); | 
| 405 | 0 |     return evp_cipher_init_internal(ctx, cipher, NULL, key, iv, enc, NULL); | 
| 406 | 0 | } | 
| 407 |  |  | 
| 408 |  | int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, | 
| 409 |  |                       ENGINE *impl, const unsigned char *key, | 
| 410 |  |                       const unsigned char *iv, int enc) | 
| 411 | 111k | { | 
| 412 | 111k |     return evp_cipher_init_internal(ctx, cipher, impl, key, iv, enc, NULL); | 
| 413 | 111k | } | 
| 414 |  |  | 
| 415 |  | int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl, | 
| 416 |  |                      const unsigned char *in, int inl) | 
| 417 | 357k | { | 
| 418 | 357k |     if (ctx->encrypt) | 
| 419 | 212k |         return EVP_EncryptUpdate(ctx, out, outl, in, inl); | 
| 420 | 145k |     else | 
| 421 | 145k |         return EVP_DecryptUpdate(ctx, out, outl, in, inl); | 
| 422 | 357k | } | 
| 423 |  |  | 
| 424 |  | int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl) | 
| 425 | 64.9k | { | 
| 426 | 64.9k |     if (ctx->encrypt) | 
| 427 | 6.79k |         return EVP_EncryptFinal_ex(ctx, out, outl); | 
| 428 | 58.1k |     else | 
| 429 | 58.1k |         return EVP_DecryptFinal_ex(ctx, out, outl); | 
| 430 | 64.9k | } | 
| 431 |  |  | 
| 432 |  | int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl) | 
| 433 | 0 | { | 
| 434 | 0 |     if (ctx->encrypt) | 
| 435 | 0 |         return EVP_EncryptFinal(ctx, out, outl); | 
| 436 | 0 |     else | 
| 437 | 0 |         return EVP_DecryptFinal(ctx, out, outl); | 
| 438 | 0 | } | 
| 439 |  |  | 
| 440 |  | int EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, | 
| 441 |  |                     const unsigned char *key, const unsigned char *iv) | 
| 442 | 0 | { | 
| 443 | 0 |     return EVP_CipherInit(ctx, cipher, key, iv, 1); | 
| 444 | 0 | } | 
| 445 |  |  | 
| 446 |  | int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, | 
| 447 |  |                        ENGINE *impl, const unsigned char *key, | 
| 448 |  |                        const unsigned char *iv) | 
| 449 | 6 | { | 
| 450 | 6 |     return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 1); | 
| 451 | 6 | } | 
| 452 |  |  | 
| 453 |  | int EVP_EncryptInit_ex2(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, | 
| 454 |  |                         const unsigned char *key, const unsigned char *iv, | 
| 455 |  |                         const OSSL_PARAM params[]) | 
| 456 | 0 | { | 
| 457 | 0 |     return EVP_CipherInit_ex2(ctx, cipher, key, iv, 1, params); | 
| 458 | 0 | } | 
| 459 |  |  | 
| 460 |  | int EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, | 
| 461 |  |                     const unsigned char *key, const unsigned char *iv) | 
| 462 | 0 | { | 
| 463 | 0 |     return EVP_CipherInit(ctx, cipher, key, iv, 0); | 
| 464 | 0 | } | 
| 465 |  |  | 
| 466 |  | int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, | 
| 467 |  |                        ENGINE *impl, const unsigned char *key, | 
| 468 |  |                        const unsigned char *iv) | 
| 469 | 669 | { | 
| 470 | 669 |     return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 0); | 
| 471 | 669 | } | 
| 472 |  |  | 
| 473 |  | int EVP_DecryptInit_ex2(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, | 
| 474 |  |                         const unsigned char *key, const unsigned char *iv, | 
| 475 |  |                         const OSSL_PARAM params[]) | 
| 476 | 0 | { | 
| 477 | 0 |     return EVP_CipherInit_ex2(ctx, cipher, key, iv, 0, params); | 
| 478 | 0 | } | 
| 479 |  |  | 
| 480 |  | /* | 
| 481 |  |  * According to the letter of standard difference between pointers | 
| 482 |  |  * is specified to be valid only within same object. This makes | 
| 483 |  |  * it formally challenging to determine if input and output buffers | 
| 484 |  |  * are not partially overlapping with standard pointer arithmetic. | 
| 485 |  |  */ | 
| 486 |  | #ifdef PTRDIFF_T | 
| 487 |  | # undef PTRDIFF_T | 
| 488 |  | #endif | 
| 489 |  | #if defined(OPENSSL_SYS_VMS) && __INITIAL_POINTER_SIZE==64 | 
| 490 |  | /* | 
| 491 |  |  * Then we have VMS that distinguishes itself by adhering to | 
| 492 |  |  * sizeof(size_t)==4 even in 64-bit builds, which means that | 
| 493 |  |  * difference between two pointers might be truncated to 32 bits. | 
| 494 |  |  * In the context one can even wonder how comparison for | 
| 495 |  |  * equality is implemented. To be on the safe side we adhere to | 
| 496 |  |  * PTRDIFF_T even for comparison for equality. | 
| 497 |  |  */ | 
| 498 |  | # define PTRDIFF_T uint64_t | 
| 499 |  | #else | 
| 500 | 0 | # define PTRDIFF_T size_t | 
| 501 |  | #endif | 
| 502 |  |  | 
| 503 |  | int ossl_is_partially_overlapping(const void *ptr1, const void *ptr2, int len) | 
| 504 | 0 | { | 
| 505 | 0 |     PTRDIFF_T diff = (PTRDIFF_T)ptr1-(PTRDIFF_T)ptr2; | 
| 506 |  |     /* | 
| 507 |  |      * Check for partially overlapping buffers. [Binary logical | 
| 508 |  |      * operations are used instead of boolean to minimize number | 
| 509 |  |      * of conditional branches.] | 
| 510 |  |      */ | 
| 511 | 0 |     int overlapped = (len > 0) & (diff != 0) & ((diff < (PTRDIFF_T)len) | | 
| 512 | 0 |                                                 (diff > (0 - (PTRDIFF_T)len))); | 
| 513 |  | 
 | 
| 514 | 0 |     return overlapped; | 
| 515 | 0 | } | 
| 516 |  |  | 
| 517 |  | static int evp_EncryptDecryptUpdate(EVP_CIPHER_CTX *ctx, | 
| 518 |  |                                     unsigned char *out, int *outl, | 
| 519 |  |                                     const unsigned char *in, int inl) | 
| 520 |  | { | 
| 521 |  |     int i, j, bl, cmpl = inl; | 
| 522 |  |  | 
| 523 |  |     if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS)) | 
| 524 |  |         cmpl = (cmpl + 7) / 8; | 
| 525 |  |  | 
| 526 |  |     bl = ctx->cipher->block_size; | 
| 527 |  |  | 
| 528 |  |     if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) { | 
| 529 |  |         /* If block size > 1 then the cipher will have to do this check */ | 
| 530 |  |         if (bl == 1 && ossl_is_partially_overlapping(out, in, cmpl)) { | 
| 531 |  |             ERR_raise(ERR_LIB_EVP, EVP_R_PARTIALLY_OVERLAPPING); | 
| 532 |  |             return 0; | 
| 533 |  |         } | 
| 534 |  |  | 
| 535 |  |         i = ctx->cipher->do_cipher(ctx, out, in, inl); | 
| 536 |  |         if (i < 0) | 
| 537 |  |             return 0; | 
| 538 |  |         else | 
| 539 |  |             *outl = i; | 
| 540 |  |         return 1; | 
| 541 |  |     } | 
| 542 |  |  | 
| 543 |  |     if (inl <= 0) { | 
| 544 |  |         *outl = 0; | 
| 545 |  |         return inl == 0; | 
| 546 |  |     } | 
| 547 |  |     if (ossl_is_partially_overlapping(out + ctx->buf_len, in, cmpl)) { | 
| 548 |  |         ERR_raise(ERR_LIB_EVP, EVP_R_PARTIALLY_OVERLAPPING); | 
| 549 |  |         return 0; | 
| 550 |  |     } | 
| 551 |  |  | 
| 552 |  |     if (ctx->buf_len == 0 && (inl & (ctx->block_mask)) == 0) { | 
| 553 |  |         if (ctx->cipher->do_cipher(ctx, out, in, inl)) { | 
| 554 |  |             *outl = inl; | 
| 555 |  |             return 1; | 
| 556 |  |         } else { | 
| 557 |  |             *outl = 0; | 
| 558 |  |             return 0; | 
| 559 |  |         } | 
| 560 |  |     } | 
| 561 |  |     i = ctx->buf_len; | 
| 562 |  |     OPENSSL_assert(bl <= (int)sizeof(ctx->buf)); | 
| 563 |  |     if (i != 0) { | 
| 564 |  |         if (bl - i > inl) { | 
| 565 |  |             memcpy(&(ctx->buf[i]), in, inl); | 
| 566 |  |             ctx->buf_len += inl; | 
| 567 |  |             *outl = 0; | 
| 568 |  |             return 1; | 
| 569 |  |         } else { | 
| 570 |  |             j = bl - i; | 
| 571 |  |  | 
| 572 |  |             /* | 
| 573 |  |              * Once we've processed the first j bytes from in, the amount of | 
| 574 |  |              * data left that is a multiple of the block length is: | 
| 575 |  |              * (inl - j) & ~(bl - 1) | 
| 576 |  |              * We must ensure that this amount of data, plus the one block that | 
| 577 |  |              * we process from ctx->buf does not exceed INT_MAX | 
| 578 |  |              */ | 
| 579 |  |             if (((inl - j) & ~(bl - 1)) > INT_MAX - bl) { | 
| 580 |  |                 ERR_raise(ERR_LIB_EVP, EVP_R_OUTPUT_WOULD_OVERFLOW); | 
| 581 |  |                 return 0; | 
| 582 |  |             } | 
| 583 |  |             memcpy(&(ctx->buf[i]), in, j); | 
| 584 |  |             inl -= j; | 
| 585 |  |             in += j; | 
| 586 |  |             if (!ctx->cipher->do_cipher(ctx, out, ctx->buf, bl)) | 
| 587 |  |                 return 0; | 
| 588 |  |             out += bl; | 
| 589 |  |             *outl = bl; | 
| 590 |  |         } | 
| 591 |  |     } else | 
| 592 |  |         *outl = 0; | 
| 593 |  |     i = inl & (bl - 1); | 
| 594 |  |     inl -= i; | 
| 595 |  |     if (inl > 0) { | 
| 596 |  |         if (!ctx->cipher->do_cipher(ctx, out, in, inl)) | 
| 597 |  |             return 0; | 
| 598 |  |         *outl += inl; | 
| 599 |  |     } | 
| 600 |  |  | 
| 601 |  |     if (i != 0) | 
| 602 |  |         memcpy(ctx->buf, &(in[inl]), i); | 
| 603 |  |     ctx->buf_len = i; | 
| 604 |  |     return 1; | 
| 605 |  | } | 
| 606 |  |  | 
| 607 |  |  | 
| 608 |  | int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl, | 
| 609 |  |                       const unsigned char *in, int inl) | 
| 610 | 22.9k | { | 
| 611 | 22.9k |     int ret; | 
| 612 | 22.9k |     size_t soutl, inl_ = (size_t)inl; | 
| 613 | 22.9k |     int blocksize; | 
| 614 |  |  | 
| 615 | 22.9k |     if (outl != NULL) { | 
| 616 | 22.9k |         *outl = 0; | 
| 617 | 22.9k |     } else { | 
| 618 | 0 |         ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER); | 
| 619 | 0 |         return 0; | 
| 620 | 0 |     } | 
| 621 |  |  | 
| 622 |  |     /* Prevent accidental use of decryption context when encrypting */ | 
| 623 | 22.9k |     if (!ctx->encrypt) { | 
| 624 | 0 |         ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION); | 
| 625 | 0 |         return 0; | 
| 626 | 0 |     } | 
| 627 |  |  | 
| 628 | 22.9k |     if (ctx->cipher == NULL) { | 
| 629 | 0 |         ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET); | 
| 630 | 0 |         return 0; | 
| 631 | 0 |     } | 
| 632 |  |  | 
| 633 | 22.9k |     if (ctx->cipher->prov == NULL) | 
| 634 | 0 |         goto legacy; | 
| 635 |  |  | 
| 636 | 22.9k |     blocksize = ctx->cipher->block_size; | 
| 637 |  |  | 
| 638 | 22.9k |     if (ctx->cipher->cupdate == NULL  || blocksize < 1) { | 
| 639 | 0 |         ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR); | 
| 640 | 0 |         return 0; | 
| 641 | 0 |     } | 
| 642 |  |  | 
| 643 | 22.9k |     ret = ctx->cipher->cupdate(ctx->algctx, out, &soutl, | 
| 644 | 22.9k |                                inl_ + (size_t)(blocksize == 1 ? 0 : blocksize), | 
| 645 | 22.9k |                                in, inl_); | 
| 646 |  |  | 
| 647 | 22.9k |     if (ret) { | 
| 648 | 22.9k |         if (soutl > INT_MAX) { | 
| 649 | 0 |             ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR); | 
| 650 | 0 |             return 0; | 
| 651 | 0 |         } | 
| 652 | 22.9k |         *outl = soutl; | 
| 653 | 22.9k |     } | 
| 654 |  |  | 
| 655 | 22.9k |     return ret; | 
| 656 |  |  | 
| 657 |  |     /* Code below to be removed when legacy support is dropped. */ | 
| 658 | 0 |  legacy: | 
| 659 |  | 
 | 
| 660 | 0 |     return evp_EncryptDecryptUpdate(ctx, out, outl, in, inl); | 
| 661 | 22.9k | } | 
| 662 |  |  | 
| 663 |  | int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl) | 
| 664 | 6 | { | 
| 665 | 6 |     int ret; | 
| 666 | 6 |     ret = EVP_EncryptFinal_ex(ctx, out, outl); | 
| 667 | 6 |     return ret; | 
| 668 | 6 | } | 
| 669 |  |  | 
| 670 |  | int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl) | 
| 671 | 4.41k | { | 
| 672 | 4.41k |     int n, ret; | 
| 673 | 4.41k |     unsigned int i, b, bl; | 
| 674 | 4.41k |     size_t soutl; | 
| 675 | 4.41k |     int blocksize; | 
| 676 |  |  | 
| 677 | 4.41k |     if (outl != NULL) { | 
| 678 | 4.41k |         *outl = 0; | 
| 679 | 4.41k |     } else { | 
| 680 | 0 |         ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER); | 
| 681 | 0 |         return 0; | 
| 682 | 0 |     } | 
| 683 |  |  | 
| 684 |  |     /* Prevent accidental use of decryption context when encrypting */ | 
| 685 | 4.41k |     if (!ctx->encrypt) { | 
| 686 | 0 |         ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION); | 
| 687 | 0 |         return 0; | 
| 688 | 0 |     } | 
| 689 |  |  | 
| 690 | 4.41k |     if (ctx->cipher == NULL) { | 
| 691 | 0 |         ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET); | 
| 692 | 0 |         return 0; | 
| 693 | 0 |     } | 
| 694 | 4.41k |     if (ctx->cipher->prov == NULL) | 
| 695 | 0 |         goto legacy; | 
| 696 |  |  | 
| 697 | 4.41k |     blocksize = EVP_CIPHER_CTX_get_block_size(ctx); | 
| 698 |  |  | 
| 699 | 4.41k |     if (blocksize < 1 || ctx->cipher->cfinal == NULL) { | 
| 700 | 0 |         ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR); | 
| 701 | 0 |         return 0; | 
| 702 | 0 |     } | 
| 703 |  |  | 
| 704 | 4.41k |     ret = ctx->cipher->cfinal(ctx->algctx, out, &soutl, | 
| 705 | 4.41k |                               blocksize == 1 ? 0 : blocksize); | 
| 706 |  |  | 
| 707 | 4.41k |     if (ret) { | 
| 708 | 4.41k |         if (soutl > INT_MAX) { | 
| 709 | 0 |             ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR); | 
| 710 | 0 |             return 0; | 
| 711 | 0 |         } | 
| 712 | 4.41k |         *outl = soutl; | 
| 713 | 4.41k |     } | 
| 714 |  |  | 
| 715 | 4.41k |     return ret; | 
| 716 |  |  | 
| 717 |  |     /* Code below to be removed when legacy support is dropped. */ | 
| 718 | 0 |  legacy: | 
| 719 |  | 
 | 
| 720 | 0 |     if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) { | 
| 721 | 0 |         ret = ctx->cipher->do_cipher(ctx, out, NULL, 0); | 
| 722 | 0 |         if (ret < 0) | 
| 723 | 0 |             return 0; | 
| 724 | 0 |         else | 
| 725 | 0 |             *outl = ret; | 
| 726 | 0 |         return 1; | 
| 727 | 0 |     } | 
| 728 |  |  | 
| 729 | 0 |     b = ctx->cipher->block_size; | 
| 730 | 0 |     OPENSSL_assert(b <= sizeof(ctx->buf)); | 
| 731 | 0 |     if (b == 1) { | 
| 732 | 0 |         *outl = 0; | 
| 733 | 0 |         return 1; | 
| 734 | 0 |     } | 
| 735 | 0 |     bl = ctx->buf_len; | 
| 736 | 0 |     if (ctx->flags & EVP_CIPH_NO_PADDING) { | 
| 737 | 0 |         if (bl) { | 
| 738 | 0 |             ERR_raise(ERR_LIB_EVP, EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH); | 
| 739 | 0 |             return 0; | 
| 740 | 0 |         } | 
| 741 | 0 |         *outl = 0; | 
| 742 | 0 |         return 1; | 
| 743 | 0 |     } | 
| 744 |  |  | 
| 745 | 0 |     n = b - bl; | 
| 746 | 0 |     for (i = bl; i < b; i++) | 
| 747 | 0 |         ctx->buf[i] = n; | 
| 748 | 0 |     ret = ctx->cipher->do_cipher(ctx, out, ctx->buf, b); | 
| 749 |  | 
 | 
| 750 | 0 |     if (ret) | 
| 751 | 0 |         *outl = b; | 
| 752 |  | 
 | 
| 753 | 0 |     return ret; | 
| 754 | 0 | } | 
| 755 |  |  | 
| 756 |  | int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl, | 
| 757 |  |                       const unsigned char *in, int inl) | 
| 758 | 51.7k | { | 
| 759 | 51.7k |     int fix_len, cmpl = inl, ret; | 
| 760 | 51.7k |     unsigned int b; | 
| 761 | 51.7k |     size_t soutl, inl_ = (size_t)inl; | 
| 762 | 51.7k |     int blocksize; | 
| 763 |  |  | 
| 764 | 51.7k |     if (outl != NULL) { | 
| 765 | 51.7k |         *outl = 0; | 
| 766 | 51.7k |     } else { | 
| 767 | 0 |         ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER); | 
| 768 | 0 |         return 0; | 
| 769 | 0 |     } | 
| 770 |  |  | 
| 771 |  |     /* Prevent accidental use of encryption context when decrypting */ | 
| 772 | 51.7k |     if (ctx->encrypt) { | 
| 773 | 0 |         ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION); | 
| 774 | 0 |         return 0; | 
| 775 | 0 |     } | 
| 776 |  |  | 
| 777 | 51.7k |     if (ctx->cipher == NULL) { | 
| 778 | 0 |         ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET); | 
| 779 | 0 |         return 0; | 
| 780 | 0 |     } | 
| 781 | 51.7k |     if (ctx->cipher->prov == NULL) | 
| 782 | 0 |         goto legacy; | 
| 783 |  |  | 
| 784 | 51.7k |     blocksize = EVP_CIPHER_CTX_get_block_size(ctx); | 
| 785 |  |  | 
| 786 | 51.7k |     if (ctx->cipher->cupdate == NULL || blocksize < 1) { | 
| 787 | 0 |         ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR); | 
| 788 | 0 |         return 0; | 
| 789 | 0 |     } | 
| 790 | 51.7k |     ret = ctx->cipher->cupdate(ctx->algctx, out, &soutl, | 
| 791 | 51.7k |                                inl_ + (size_t)(blocksize == 1 ? 0 : blocksize), | 
| 792 | 51.7k |                                in, inl_); | 
| 793 |  |  | 
| 794 | 51.7k |     if (ret) { | 
| 795 | 51.4k |         if (soutl > INT_MAX) { | 
| 796 | 0 |             ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR); | 
| 797 | 0 |             return 0; | 
| 798 | 0 |         } | 
| 799 | 51.4k |         *outl = soutl; | 
| 800 | 51.4k |     } | 
| 801 |  |  | 
| 802 | 51.7k |     return ret; | 
| 803 |  |  | 
| 804 |  |     /* Code below to be removed when legacy support is dropped. */ | 
| 805 | 0 |  legacy: | 
| 806 |  | 
 | 
| 807 | 0 |     b = ctx->cipher->block_size; | 
| 808 |  | 
 | 
| 809 | 0 |     if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS)) | 
| 810 | 0 |         cmpl = (cmpl + 7) / 8; | 
| 811 |  | 
 | 
| 812 | 0 |     if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) { | 
| 813 | 0 |         if (b == 1 && ossl_is_partially_overlapping(out, in, cmpl)) { | 
| 814 | 0 |             ERR_raise(ERR_LIB_EVP, EVP_R_PARTIALLY_OVERLAPPING); | 
| 815 | 0 |             return 0; | 
| 816 | 0 |         } | 
| 817 |  |  | 
| 818 | 0 |         fix_len = ctx->cipher->do_cipher(ctx, out, in, inl); | 
| 819 | 0 |         if (fix_len < 0) { | 
| 820 | 0 |             *outl = 0; | 
| 821 | 0 |             return 0; | 
| 822 | 0 |         } else | 
| 823 | 0 |             *outl = fix_len; | 
| 824 | 0 |         return 1; | 
| 825 | 0 |     } | 
| 826 |  |  | 
| 827 | 0 |     if (inl <= 0) { | 
| 828 | 0 |         *outl = 0; | 
| 829 | 0 |         return inl == 0; | 
| 830 | 0 |     } | 
| 831 |  |  | 
| 832 | 0 |     if (ctx->flags & EVP_CIPH_NO_PADDING) | 
| 833 | 0 |         return evp_EncryptDecryptUpdate(ctx, out, outl, in, inl); | 
| 834 |  |  | 
| 835 | 0 |     OPENSSL_assert(b <= sizeof(ctx->final)); | 
| 836 |  | 
 | 
| 837 | 0 |     if (ctx->final_used) { | 
| 838 |  |         /* see comment about PTRDIFF_T comparison above */ | 
| 839 | 0 |         if (((PTRDIFF_T)out == (PTRDIFF_T)in) | 
| 840 | 0 |             || ossl_is_partially_overlapping(out, in, b)) { | 
| 841 | 0 |             ERR_raise(ERR_LIB_EVP, EVP_R_PARTIALLY_OVERLAPPING); | 
| 842 | 0 |             return 0; | 
| 843 | 0 |         } | 
| 844 |  |         /* | 
| 845 |  |          * final_used is only ever set if buf_len is 0. Therefore the maximum | 
| 846 |  |          * length output we will ever see from evp_EncryptDecryptUpdate is | 
| 847 |  |          * the maximum multiple of the block length that is <= inl, or just: | 
| 848 |  |          * inl & ~(b - 1) | 
| 849 |  |          * Since final_used has been set then the final output length is: | 
| 850 |  |          * (inl & ~(b - 1)) + b | 
| 851 |  |          * This must never exceed INT_MAX | 
| 852 |  |          */ | 
| 853 | 0 |         if ((inl & ~(b - 1)) > INT_MAX - b) { | 
| 854 | 0 |             ERR_raise(ERR_LIB_EVP, EVP_R_OUTPUT_WOULD_OVERFLOW); | 
| 855 | 0 |             return 0; | 
| 856 | 0 |         } | 
| 857 | 0 |         memcpy(out, ctx->final, b); | 
| 858 | 0 |         out += b; | 
| 859 | 0 |         fix_len = 1; | 
| 860 | 0 |     } else | 
| 861 | 0 |         fix_len = 0; | 
| 862 |  |  | 
| 863 | 0 |     if (!evp_EncryptDecryptUpdate(ctx, out, outl, in, inl)) | 
| 864 | 0 |         return 0; | 
| 865 |  |  | 
| 866 |  |     /* | 
| 867 |  |      * if we have 'decrypted' a multiple of block size, make sure we have a | 
| 868 |  |      * copy of this last block | 
| 869 |  |      */ | 
| 870 | 0 |     if (b > 1 && !ctx->buf_len) { | 
| 871 | 0 |         *outl -= b; | 
| 872 | 0 |         ctx->final_used = 1; | 
| 873 | 0 |         memcpy(ctx->final, &out[*outl], b); | 
| 874 | 0 |     } else | 
| 875 | 0 |         ctx->final_used = 0; | 
| 876 |  | 
 | 
| 877 | 0 |     if (fix_len) | 
| 878 | 0 |         *outl += b; | 
| 879 |  | 
 | 
| 880 | 0 |     return 1; | 
| 881 | 0 | } | 
| 882 |  |  | 
| 883 |  | int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl) | 
| 884 | 557 | { | 
| 885 | 557 |     int ret; | 
| 886 | 557 |     ret = EVP_DecryptFinal_ex(ctx, out, outl); | 
| 887 | 557 |     return ret; | 
| 888 | 557 | } | 
| 889 |  |  | 
| 890 |  | int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl) | 
| 891 | 47.1k | { | 
| 892 | 47.1k |     int i, n; | 
| 893 | 47.1k |     unsigned int b; | 
| 894 | 47.1k |     size_t soutl; | 
| 895 | 47.1k |     int ret; | 
| 896 | 47.1k |     int blocksize; | 
| 897 |  |  | 
| 898 | 47.1k |     if (outl != NULL) { | 
| 899 | 47.1k |         *outl = 0; | 
| 900 | 47.1k |     } else { | 
| 901 | 0 |         ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER); | 
| 902 | 0 |         return 0; | 
| 903 | 0 |     } | 
| 904 |  |  | 
| 905 |  |     /* Prevent accidental use of encryption context when decrypting */ | 
| 906 | 47.1k |     if (ctx->encrypt) { | 
| 907 | 0 |         ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION); | 
| 908 | 0 |         return 0; | 
| 909 | 0 |     } | 
| 910 |  |  | 
| 911 | 47.1k |     if (ctx->cipher == NULL) { | 
| 912 | 0 |         ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET); | 
| 913 | 0 |         return 0; | 
| 914 | 0 |     } | 
| 915 |  |  | 
| 916 | 47.1k |     if (ctx->cipher->prov == NULL) | 
| 917 | 0 |         goto legacy; | 
| 918 |  |  | 
| 919 | 47.1k |     blocksize = EVP_CIPHER_CTX_get_block_size(ctx); | 
| 920 |  |  | 
| 921 | 47.1k |     if (blocksize < 1 || ctx->cipher->cfinal == NULL) { | 
| 922 | 0 |         ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR); | 
| 923 | 0 |         return 0; | 
| 924 | 0 |     } | 
| 925 |  |  | 
| 926 | 47.1k |     ret = ctx->cipher->cfinal(ctx->algctx, out, &soutl, | 
| 927 | 47.1k |                               blocksize == 1 ? 0 : blocksize); | 
| 928 |  |  | 
| 929 | 47.1k |     if (ret) { | 
| 930 | 724 |         if (soutl > INT_MAX) { | 
| 931 | 0 |             ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR); | 
| 932 | 0 |             return 0; | 
| 933 | 0 |         } | 
| 934 | 724 |         *outl = soutl; | 
| 935 | 724 |     } | 
| 936 |  |  | 
| 937 | 47.1k |     return ret; | 
| 938 |  |  | 
| 939 |  |     /* Code below to be removed when legacy support is dropped. */ | 
| 940 | 0 |  legacy: | 
| 941 |  | 
 | 
| 942 | 0 |     *outl = 0; | 
| 943 | 0 |     if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) { | 
| 944 | 0 |         i = ctx->cipher->do_cipher(ctx, out, NULL, 0); | 
| 945 | 0 |         if (i < 0) | 
| 946 | 0 |             return 0; | 
| 947 | 0 |         else | 
| 948 | 0 |             *outl = i; | 
| 949 | 0 |         return 1; | 
| 950 | 0 |     } | 
| 951 |  |  | 
| 952 | 0 |     b = ctx->cipher->block_size; | 
| 953 | 0 |     if (ctx->flags & EVP_CIPH_NO_PADDING) { | 
| 954 | 0 |         if (ctx->buf_len) { | 
| 955 | 0 |             ERR_raise(ERR_LIB_EVP, EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH); | 
| 956 | 0 |             return 0; | 
| 957 | 0 |         } | 
| 958 | 0 |         *outl = 0; | 
| 959 | 0 |         return 1; | 
| 960 | 0 |     } | 
| 961 | 0 |     if (b > 1) { | 
| 962 | 0 |         if (ctx->buf_len || !ctx->final_used) { | 
| 963 | 0 |             ERR_raise(ERR_LIB_EVP, EVP_R_WRONG_FINAL_BLOCK_LENGTH); | 
| 964 | 0 |             return 0; | 
| 965 | 0 |         } | 
| 966 | 0 |         OPENSSL_assert(b <= sizeof(ctx->final)); | 
| 967 |  |  | 
| 968 |  |         /* | 
| 969 |  |          * The following assumes that the ciphertext has been authenticated. | 
| 970 |  |          * Otherwise it provides a padding oracle. | 
| 971 |  |          */ | 
| 972 | 0 |         n = ctx->final[b - 1]; | 
| 973 | 0 |         if (n == 0 || n > (int)b) { | 
| 974 | 0 |             ERR_raise(ERR_LIB_EVP, EVP_R_BAD_DECRYPT); | 
| 975 | 0 |             return 0; | 
| 976 | 0 |         } | 
| 977 | 0 |         for (i = 0; i < n; i++) { | 
| 978 | 0 |             if (ctx->final[--b] != n) { | 
| 979 | 0 |                 ERR_raise(ERR_LIB_EVP, EVP_R_BAD_DECRYPT); | 
| 980 | 0 |                 return 0; | 
| 981 | 0 |             } | 
| 982 | 0 |         } | 
| 983 | 0 |         n = ctx->cipher->block_size - n; | 
| 984 | 0 |         for (i = 0; i < n; i++) | 
| 985 | 0 |             out[i] = ctx->final[i]; | 
| 986 | 0 |         *outl = n; | 
| 987 | 0 |     } else | 
| 988 | 0 |         *outl = 0; | 
| 989 | 0 |     return 1; | 
| 990 | 0 | } | 
| 991 |  |  | 
| 992 |  | int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, int keylen) | 
| 993 | 0 | { | 
| 994 | 0 |     if (c->cipher->prov != NULL) { | 
| 995 | 0 |         int ok; | 
| 996 | 0 |         OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; | 
| 997 | 0 |         size_t len = keylen; | 
| 998 |  | 
 | 
| 999 | 0 |         if (EVP_CIPHER_CTX_get_key_length(c) == keylen) | 
| 1000 | 0 |             return 1; | 
| 1001 |  |  | 
| 1002 |  |         /* Check the cipher actually understands this parameter */ | 
| 1003 | 0 |         if (OSSL_PARAM_locate_const(EVP_CIPHER_settable_ctx_params(c->cipher), | 
| 1004 | 0 |                                     OSSL_CIPHER_PARAM_KEYLEN) == NULL) { | 
| 1005 | 0 |             ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH); | 
| 1006 | 0 |             return 0; | 
| 1007 | 0 |         } | 
| 1008 |  |  | 
| 1009 | 0 |         params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &len); | 
| 1010 | 0 |         ok = evp_do_ciph_ctx_setparams(c->cipher, c->algctx, params); | 
| 1011 |  | 
 | 
| 1012 | 0 |         return ok > 0 ? 1 : 0; | 
| 1013 | 0 |     } | 
| 1014 |  |  | 
| 1015 |  |     /* Code below to be removed when legacy support is dropped. */ | 
| 1016 |  |  | 
| 1017 |  |     /* | 
| 1018 |  |      * Note there have never been any built-in ciphers that define this flag | 
| 1019 |  |      * since it was first introduced. | 
| 1020 |  |      */ | 
| 1021 | 0 |     if (c->cipher->flags & EVP_CIPH_CUSTOM_KEY_LENGTH) | 
| 1022 | 0 |         return EVP_CIPHER_CTX_ctrl(c, EVP_CTRL_SET_KEY_LENGTH, keylen, NULL); | 
| 1023 | 0 |     if (EVP_CIPHER_CTX_get_key_length(c) == keylen) | 
| 1024 | 0 |         return 1; | 
| 1025 | 0 |     if ((keylen > 0) && (c->cipher->flags & EVP_CIPH_VARIABLE_LENGTH)) { | 
| 1026 | 0 |         c->key_len = keylen; | 
| 1027 | 0 |         return 1; | 
| 1028 | 0 |     } | 
| 1029 | 0 |     ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH); | 
| 1030 | 0 |     return 0; | 
| 1031 | 0 | } | 
| 1032 |  |  | 
| 1033 |  | int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *ctx, int pad) | 
| 1034 | 0 | { | 
| 1035 | 0 |     int ok; | 
| 1036 | 0 |     OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; | 
| 1037 | 0 |     unsigned int pd = pad; | 
| 1038 |  | 
 | 
| 1039 | 0 |     if (pad) | 
| 1040 | 0 |         ctx->flags &= ~EVP_CIPH_NO_PADDING; | 
| 1041 | 0 |     else | 
| 1042 | 0 |         ctx->flags |= EVP_CIPH_NO_PADDING; | 
| 1043 |  | 
 | 
| 1044 | 0 |     if (ctx->cipher != NULL && ctx->cipher->prov == NULL) | 
| 1045 | 0 |         return 1; | 
| 1046 | 0 |     params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_PADDING, &pd); | 
| 1047 | 0 |     ok = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params); | 
| 1048 |  | 
 | 
| 1049 | 0 |     return ok != 0; | 
| 1050 | 0 | } | 
| 1051 |  |  | 
| 1052 |  | int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr) | 
| 1053 | 31.4k | { | 
| 1054 | 31.4k |     int ret = EVP_CTRL_RET_UNSUPPORTED; | 
| 1055 | 31.4k |     int set_params = 1; | 
| 1056 | 31.4k |     size_t sz = arg; | 
| 1057 | 31.4k |     unsigned int i; | 
| 1058 | 31.4k |     OSSL_PARAM params[4] = { | 
| 1059 | 31.4k |         OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END | 
| 1060 | 31.4k |     }; | 
| 1061 |  |  | 
| 1062 | 31.4k |     if (ctx == NULL || ctx->cipher == NULL) { | 
| 1063 | 0 |         ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET); | 
| 1064 | 0 |         return 0; | 
| 1065 | 0 |     } | 
| 1066 |  |  | 
| 1067 | 31.4k |     if (ctx->cipher->prov == NULL) | 
| 1068 | 0 |         goto legacy; | 
| 1069 |  |  | 
| 1070 | 31.4k |     switch (type) { | 
| 1071 | 0 |     case EVP_CTRL_SET_KEY_LENGTH: | 
| 1072 | 0 |         params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &sz); | 
| 1073 | 0 |         break; | 
| 1074 | 0 |     case EVP_CTRL_RAND_KEY:      /* Used by DES */ | 
| 1075 | 0 |         set_params = 0; | 
| 1076 | 0 |         params[0] = | 
| 1077 | 0 |             OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_RANDOM_KEY, | 
| 1078 | 0 |                                               ptr, sz); | 
| 1079 | 0 |         break; | 
| 1080 |  |  | 
| 1081 | 0 |     case EVP_CTRL_INIT: | 
| 1082 |  |         /* | 
| 1083 |  |          * EVP_CTRL_INIT is purely legacy, no provider counterpart. | 
| 1084 |  |          * As a matter of fact, this should be dead code, but some caller | 
| 1085 |  |          * might still do a direct control call with this command, so... | 
| 1086 |  |          * Legacy methods return 1 except for exceptional circumstances, so | 
| 1087 |  |          * we do the same here to not be disruptive. | 
| 1088 |  |          */ | 
| 1089 | 0 |         return 1; | 
| 1090 | 0 |     case EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS: /* Used by DASYNC */ | 
| 1091 | 0 |     default: | 
| 1092 | 0 |         goto end; | 
| 1093 | 1.55k |     case EVP_CTRL_AEAD_SET_IVLEN: | 
| 1094 | 1.55k |         if (arg < 0) | 
| 1095 | 0 |             return 0; | 
| 1096 | 1.55k |         params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &sz); | 
| 1097 | 1.55k |         ctx->iv_len = -1; | 
| 1098 | 1.55k |         break; | 
| 1099 | 0 |     case EVP_CTRL_CCM_SET_L: | 
| 1100 | 0 |         if (arg < 2 || arg > 8) | 
| 1101 | 0 |             return 0; | 
| 1102 | 0 |         sz = 15 - arg; | 
| 1103 | 0 |         params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &sz); | 
| 1104 | 0 |         ctx->iv_len = -1; | 
| 1105 | 0 |         break; | 
| 1106 | 516 |     case EVP_CTRL_AEAD_SET_IV_FIXED: | 
| 1107 | 516 |         params[0] = OSSL_PARAM_construct_octet_string( | 
| 1108 | 516 |                         OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED, ptr, sz); | 
| 1109 | 516 |         break; | 
| 1110 | 0 |     case EVP_CTRL_GCM_IV_GEN: | 
| 1111 | 0 |         set_params = 0; | 
| 1112 | 0 |         if (arg < 0) | 
| 1113 | 0 |             sz = 0; /* special case that uses the iv length */ | 
| 1114 | 0 |         params[0] = OSSL_PARAM_construct_octet_string( | 
| 1115 | 0 |                         OSSL_CIPHER_PARAM_AEAD_TLS1_GET_IV_GEN, ptr, sz); | 
| 1116 | 0 |         break; | 
| 1117 | 0 |     case EVP_CTRL_GCM_SET_IV_INV: | 
| 1118 | 0 |         if (arg < 0) | 
| 1119 | 0 |             return 0; | 
| 1120 | 0 |         params[0] = OSSL_PARAM_construct_octet_string( | 
| 1121 | 0 |                         OSSL_CIPHER_PARAM_AEAD_TLS1_SET_IV_INV, ptr, sz); | 
| 1122 | 0 |         break; | 
| 1123 | 0 |     case EVP_CTRL_GET_RC5_ROUNDS: | 
| 1124 | 0 |         set_params = 0; /* Fall thru */ | 
| 1125 | 0 |     case EVP_CTRL_SET_RC5_ROUNDS: | 
| 1126 | 0 |         if (arg < 0) | 
| 1127 | 0 |             return 0; | 
| 1128 | 0 |         i = (unsigned int)arg; | 
| 1129 | 0 |         params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_ROUNDS, &i); | 
| 1130 | 0 |         break; | 
| 1131 | 0 |     case EVP_CTRL_SET_SPEED: | 
| 1132 | 0 |         if (arg < 0) | 
| 1133 | 0 |             return 0; | 
| 1134 | 0 |         i = (unsigned int)arg; | 
| 1135 | 0 |         params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_SPEED, &i); | 
| 1136 | 0 |         break; | 
| 1137 | 2.14k |     case EVP_CTRL_AEAD_GET_TAG: | 
| 1138 | 2.14k |         set_params = 0; /* Fall thru */ | 
| 1139 | 27.6k |     case EVP_CTRL_AEAD_SET_TAG: | 
| 1140 | 27.6k |         params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG, | 
| 1141 | 27.6k |                                                       ptr, sz); | 
| 1142 | 27.6k |         break; | 
| 1143 | 1.19k |     case EVP_CTRL_AEAD_TLS1_AAD: | 
| 1144 |  |         /* This one does a set and a get - since it returns a size */ | 
| 1145 | 1.19k |         params[0] = | 
| 1146 | 1.19k |             OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD, | 
| 1147 | 1.19k |                                               ptr, sz); | 
| 1148 | 1.19k |         ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params); | 
| 1149 | 1.19k |         if (ret <= 0) | 
| 1150 | 17 |             goto end; | 
| 1151 | 1.17k |         params[0] = | 
| 1152 | 1.17k |             OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD, &sz); | 
| 1153 | 1.17k |         ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params); | 
| 1154 | 1.17k |         if (ret <= 0) | 
| 1155 | 0 |             goto end; | 
| 1156 | 1.17k |         return sz; | 
| 1157 | 0 | #ifndef OPENSSL_NO_RC2 | 
| 1158 | 0 |     case EVP_CTRL_GET_RC2_KEY_BITS: | 
| 1159 | 0 |         set_params = 0; /* Fall thru */ | 
| 1160 | 0 |     case EVP_CTRL_SET_RC2_KEY_BITS: | 
| 1161 | 0 |         params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_RC2_KEYBITS, &sz); | 
| 1162 | 0 |         break; | 
| 1163 | 0 | #endif /* OPENSSL_NO_RC2 */ | 
| 1164 | 0 | #if !defined(OPENSSL_NO_MULTIBLOCK) | 
| 1165 | 0 |     case EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE: | 
| 1166 | 0 |         params[0] = OSSL_PARAM_construct_size_t( | 
| 1167 | 0 |                 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_SEND_FRAGMENT, &sz); | 
| 1168 | 0 |         ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params); | 
| 1169 | 0 |         if (ret <= 0) | 
| 1170 | 0 |             return 0; | 
| 1171 |  |  | 
| 1172 | 0 |         params[0] = OSSL_PARAM_construct_size_t( | 
| 1173 | 0 |                 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_BUFSIZE, &sz); | 
| 1174 | 0 |         params[1] = OSSL_PARAM_construct_end(); | 
| 1175 | 0 |         ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params); | 
| 1176 | 0 |         if (ret <= 0) | 
| 1177 | 0 |             return 0; | 
| 1178 | 0 |         return sz; | 
| 1179 | 0 |     case EVP_CTRL_TLS1_1_MULTIBLOCK_AAD: { | 
| 1180 | 0 |         EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *p = | 
| 1181 | 0 |             (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *)ptr; | 
| 1182 |  | 
 | 
| 1183 | 0 |         if (arg < (int)sizeof(EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM)) | 
| 1184 | 0 |             return 0; | 
| 1185 |  |  | 
| 1186 | 0 |         params[0] = OSSL_PARAM_construct_octet_string( | 
| 1187 | 0 |                 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_AAD, (void*)p->inp, p->len); | 
| 1188 | 0 |         params[1] = OSSL_PARAM_construct_uint( | 
| 1189 | 0 |                 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE, &p->interleave); | 
| 1190 | 0 |         ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params); | 
| 1191 | 0 |         if (ret <= 0) | 
| 1192 | 0 |             return ret; | 
| 1193 |  |         /* Retrieve the return values changed by the set */ | 
| 1194 | 0 |         params[0] = OSSL_PARAM_construct_size_t( | 
| 1195 | 0 |                 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_AAD_PACKLEN, &sz); | 
| 1196 | 0 |         params[1] = OSSL_PARAM_construct_uint( | 
| 1197 | 0 |                 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE, &p->interleave); | 
| 1198 | 0 |         params[2] = OSSL_PARAM_construct_end(); | 
| 1199 | 0 |         ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params); | 
| 1200 | 0 |         if (ret <= 0) | 
| 1201 | 0 |             return 0; | 
| 1202 | 0 |         return sz; | 
| 1203 | 0 |     } | 
| 1204 | 0 |     case EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT: { | 
| 1205 | 0 |         EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *p = | 
| 1206 | 0 |             (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *)ptr; | 
| 1207 |  | 
 | 
| 1208 | 0 |         params[0] = OSSL_PARAM_construct_octet_string( | 
| 1209 | 0 |                         OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC, p->out, p->len); | 
| 1210 |  | 
 | 
| 1211 | 0 |         params[1] = OSSL_PARAM_construct_octet_string( | 
| 1212 | 0 |                 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC_IN, (void*)p->inp, | 
| 1213 | 0 |                 p->len); | 
| 1214 | 0 |         params[2] = OSSL_PARAM_construct_uint( | 
| 1215 | 0 |                 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE, &p->interleave); | 
| 1216 | 0 |         ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params); | 
| 1217 | 0 |         if (ret <= 0) | 
| 1218 | 0 |             return ret; | 
| 1219 | 0 |         params[0] = OSSL_PARAM_construct_size_t( | 
| 1220 | 0 |                         OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC_LEN, &sz); | 
| 1221 | 0 |         params[1] = OSSL_PARAM_construct_end(); | 
| 1222 | 0 |         ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params); | 
| 1223 | 0 |         if (ret <= 0) | 
| 1224 | 0 |             return 0; | 
| 1225 | 0 |         return sz; | 
| 1226 | 0 |     } | 
| 1227 | 0 | #endif /* OPENSSL_NO_MULTIBLOCK */ | 
| 1228 | 497 |     case EVP_CTRL_AEAD_SET_MAC_KEY: | 
| 1229 | 497 |         if (arg < 0) | 
| 1230 | 0 |             return -1; | 
| 1231 | 497 |         params[0] = OSSL_PARAM_construct_octet_string( | 
| 1232 | 497 |                 OSSL_CIPHER_PARAM_AEAD_MAC_KEY, ptr, sz); | 
| 1233 | 497 |         break; | 
| 1234 | 31.4k |     } | 
| 1235 |  |  | 
| 1236 | 30.2k |     if (set_params) | 
| 1237 | 28.0k |         ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params); | 
| 1238 | 2.14k |     else | 
| 1239 | 2.14k |         ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params); | 
| 1240 | 30.2k |     goto end; | 
| 1241 |  |  | 
| 1242 |  |     /* Code below to be removed when legacy support is dropped. */ | 
| 1243 | 0 | legacy: | 
| 1244 | 0 |     if (ctx->cipher->ctrl == NULL) { | 
| 1245 | 0 |         ERR_raise(ERR_LIB_EVP, EVP_R_CTRL_NOT_IMPLEMENTED); | 
| 1246 | 0 |         return 0; | 
| 1247 | 0 |     } | 
| 1248 |  |  | 
| 1249 | 0 |     ret = ctx->cipher->ctrl(ctx, type, arg, ptr); | 
| 1250 |  | 
 | 
| 1251 | 30.2k |  end: | 
| 1252 | 30.2k |     if (ret == EVP_CTRL_RET_UNSUPPORTED) { | 
| 1253 | 0 |         ERR_raise(ERR_LIB_EVP, EVP_R_CTRL_OPERATION_NOT_IMPLEMENTED); | 
| 1254 | 0 |         return 0; | 
| 1255 | 0 |     } | 
| 1256 | 30.2k |     return ret; | 
| 1257 | 30.2k | } | 
| 1258 |  |  | 
| 1259 |  | int EVP_CIPHER_get_params(EVP_CIPHER *cipher, OSSL_PARAM params[]) | 
| 1260 | 0 | { | 
| 1261 | 0 |     if (cipher != NULL && cipher->get_params != NULL) | 
| 1262 | 0 |         return cipher->get_params(params); | 
| 1263 | 0 |     return 0; | 
| 1264 | 0 | } | 
| 1265 |  |  | 
| 1266 |  | int EVP_CIPHER_CTX_set_params(EVP_CIPHER_CTX *ctx, const OSSL_PARAM params[]) | 
| 1267 | 2.31k | { | 
| 1268 | 2.31k |     if (ctx->cipher != NULL && ctx->cipher->set_ctx_params != NULL) { | 
| 1269 | 2.31k |         ctx->iv_len = -1; | 
| 1270 | 2.31k |         return ctx->cipher->set_ctx_params(ctx->algctx, params); | 
| 1271 | 2.31k |     } | 
| 1272 | 0 |     return 0; | 
| 1273 | 2.31k | } | 
| 1274 |  |  | 
| 1275 |  | int EVP_CIPHER_CTX_get_params(EVP_CIPHER_CTX *ctx, OSSL_PARAM params[]) | 
| 1276 | 28.0k | { | 
| 1277 | 28.0k |     if (ctx->cipher != NULL && ctx->cipher->get_ctx_params != NULL) | 
| 1278 | 28.0k |         return ctx->cipher->get_ctx_params(ctx->algctx, params); | 
| 1279 | 0 |     return 0; | 
| 1280 | 28.0k | } | 
| 1281 |  |  | 
| 1282 |  | const OSSL_PARAM *EVP_CIPHER_gettable_params(const EVP_CIPHER *cipher) | 
| 1283 | 0 | { | 
| 1284 | 0 |     if (cipher != NULL && cipher->gettable_params != NULL) | 
| 1285 | 0 |         return cipher->gettable_params( | 
| 1286 | 0 |                    ossl_provider_ctx(EVP_CIPHER_get0_provider(cipher))); | 
| 1287 | 0 |     return NULL; | 
| 1288 | 0 | } | 
| 1289 |  |  | 
| 1290 |  | const OSSL_PARAM *EVP_CIPHER_settable_ctx_params(const EVP_CIPHER *cipher) | 
| 1291 | 0 | { | 
| 1292 | 0 |     void *provctx; | 
| 1293 |  | 
 | 
| 1294 | 0 |     if (cipher != NULL && cipher->settable_ctx_params != NULL) { | 
| 1295 | 0 |         provctx = ossl_provider_ctx(EVP_CIPHER_get0_provider(cipher)); | 
| 1296 | 0 |         return cipher->settable_ctx_params(NULL, provctx); | 
| 1297 | 0 |     } | 
| 1298 | 0 |     return NULL; | 
| 1299 | 0 | } | 
| 1300 |  |  | 
| 1301 |  | const OSSL_PARAM *EVP_CIPHER_gettable_ctx_params(const EVP_CIPHER *cipher) | 
| 1302 | 762 | { | 
| 1303 | 762 |     void *provctx; | 
| 1304 |  |  | 
| 1305 | 762 |     if (cipher != NULL && cipher->gettable_ctx_params != NULL) { | 
| 1306 | 762 |         provctx = ossl_provider_ctx(EVP_CIPHER_get0_provider(cipher)); | 
| 1307 | 762 |         return cipher->gettable_ctx_params(NULL, provctx); | 
| 1308 | 762 |     } | 
| 1309 | 0 |     return NULL; | 
| 1310 | 762 | } | 
| 1311 |  |  | 
| 1312 |  | const OSSL_PARAM *EVP_CIPHER_CTX_settable_params(EVP_CIPHER_CTX *cctx) | 
| 1313 | 0 | { | 
| 1314 | 0 |     void *alg; | 
| 1315 |  | 
 | 
| 1316 | 0 |     if (cctx != NULL && cctx->cipher->settable_ctx_params != NULL) { | 
| 1317 | 0 |         alg = ossl_provider_ctx(EVP_CIPHER_get0_provider(cctx->cipher)); | 
| 1318 | 0 |         return cctx->cipher->settable_ctx_params(cctx->algctx, alg); | 
| 1319 | 0 |     } | 
| 1320 | 0 |     return NULL; | 
| 1321 | 0 | } | 
| 1322 |  |  | 
| 1323 |  | const OSSL_PARAM *EVP_CIPHER_CTX_gettable_params(EVP_CIPHER_CTX *cctx) | 
| 1324 | 0 | { | 
| 1325 | 0 |     void *provctx; | 
| 1326 |  | 
 | 
| 1327 | 0 |     if (cctx != NULL && cctx->cipher->gettable_ctx_params != NULL) { | 
| 1328 | 0 |         provctx = ossl_provider_ctx(EVP_CIPHER_get0_provider(cctx->cipher)); | 
| 1329 | 0 |         return cctx->cipher->gettable_ctx_params(cctx->algctx, provctx); | 
| 1330 | 0 |     } | 
| 1331 | 0 |     return NULL; | 
| 1332 | 0 | } | 
| 1333 |  |  | 
| 1334 |  | #ifndef FIPS_MODULE | 
| 1335 |  | static OSSL_LIB_CTX *EVP_CIPHER_CTX_get_libctx(EVP_CIPHER_CTX *ctx) | 
| 1336 | 0 | { | 
| 1337 | 0 |     const EVP_CIPHER *cipher = ctx->cipher; | 
| 1338 | 0 |     const OSSL_PROVIDER *prov; | 
| 1339 |  | 
 | 
| 1340 | 0 |     if (cipher == NULL) | 
| 1341 | 0 |         return NULL; | 
| 1342 |  |  | 
| 1343 | 0 |     prov = EVP_CIPHER_get0_provider(cipher); | 
| 1344 | 0 |     return ossl_provider_libctx(prov); | 
| 1345 | 0 | } | 
| 1346 |  | #endif | 
| 1347 |  |  | 
| 1348 |  | int EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, unsigned char *key) | 
| 1349 | 0 | { | 
| 1350 | 0 |     if (ctx->cipher->flags & EVP_CIPH_RAND_KEY) | 
| 1351 | 0 |         return EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_RAND_KEY, 0, key); | 
| 1352 |  |  | 
| 1353 |  | #ifdef FIPS_MODULE | 
| 1354 |  |     return 0; | 
| 1355 |  | #else | 
| 1356 | 0 |     { | 
| 1357 | 0 |         int kl; | 
| 1358 | 0 |         OSSL_LIB_CTX *libctx = EVP_CIPHER_CTX_get_libctx(ctx); | 
| 1359 |  | 
 | 
| 1360 | 0 |         kl = EVP_CIPHER_CTX_get_key_length(ctx); | 
| 1361 | 0 |         if (kl <= 0 || RAND_priv_bytes_ex(libctx, key, kl, 0) <= 0) | 
| 1362 | 0 |             return 0; | 
| 1363 | 0 |         return 1; | 
| 1364 | 0 |     } | 
| 1365 | 0 | #endif /* FIPS_MODULE */ | 
| 1366 | 0 | } | 
| 1367 |  |  | 
| 1368 |  | int EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in) | 
| 1369 | 0 | { | 
| 1370 | 0 |     if ((in == NULL) || (in->cipher == NULL)) { | 
| 1371 | 0 |         ERR_raise(ERR_LIB_EVP, EVP_R_INPUT_NOT_INITIALIZED); | 
| 1372 | 0 |         return 0; | 
| 1373 | 0 |     } | 
| 1374 |  |  | 
| 1375 | 0 |     if (in->cipher->prov == NULL) | 
| 1376 | 0 |         goto legacy; | 
| 1377 |  |  | 
| 1378 | 0 |     if (in->cipher->dupctx == NULL) { | 
| 1379 | 0 |         ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX); | 
| 1380 | 0 |         return 0; | 
| 1381 | 0 |     } | 
| 1382 |  |  | 
| 1383 | 0 |     EVP_CIPHER_CTX_reset(out); | 
| 1384 |  | 
 | 
| 1385 | 0 |     *out = *in; | 
| 1386 | 0 |     out->algctx = NULL; | 
| 1387 |  | 
 | 
| 1388 | 0 |     if (in->fetched_cipher != NULL && !EVP_CIPHER_up_ref(in->fetched_cipher)) { | 
| 1389 | 0 |         out->fetched_cipher = NULL; | 
| 1390 | 0 |         return 0; | 
| 1391 | 0 |     } | 
| 1392 |  |  | 
| 1393 | 0 |     out->algctx = in->cipher->dupctx(in->algctx); | 
| 1394 | 0 |     if (out->algctx == NULL) { | 
| 1395 | 0 |         ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX); | 
| 1396 | 0 |         return 0; | 
| 1397 | 0 |     } | 
| 1398 |  |  | 
| 1399 | 0 |     return 1; | 
| 1400 |  |  | 
| 1401 |  |     /* Code below to be removed when legacy support is dropped. */ | 
| 1402 | 0 |  legacy: | 
| 1403 |  | 
 | 
| 1404 | 0 | #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE) | 
| 1405 |  |     /* Make sure it's safe to copy a cipher context using an ENGINE */ | 
| 1406 | 0 |     if (in->engine && !ENGINE_init(in->engine)) { | 
| 1407 | 0 |         ERR_raise(ERR_LIB_EVP, ERR_R_ENGINE_LIB); | 
| 1408 | 0 |         return 0; | 
| 1409 | 0 |     } | 
| 1410 | 0 | #endif | 
| 1411 |  |  | 
| 1412 | 0 |     EVP_CIPHER_CTX_reset(out); | 
| 1413 | 0 |     memcpy(out, in, sizeof(*out)); | 
| 1414 |  | 
 | 
| 1415 | 0 |     if (in->cipher_data && in->cipher->ctx_size) { | 
| 1416 | 0 |         out->cipher_data = OPENSSL_malloc(in->cipher->ctx_size); | 
| 1417 | 0 |         if (out->cipher_data == NULL) { | 
| 1418 | 0 |             out->cipher = NULL; | 
| 1419 | 0 |             ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE); | 
| 1420 | 0 |             return 0; | 
| 1421 | 0 |         } | 
| 1422 | 0 |         memcpy(out->cipher_data, in->cipher_data, in->cipher->ctx_size); | 
| 1423 | 0 |     } | 
| 1424 |  |  | 
| 1425 | 0 |     if (in->cipher->flags & EVP_CIPH_CUSTOM_COPY) | 
| 1426 | 0 |         if (!in->cipher->ctrl((EVP_CIPHER_CTX *)in, EVP_CTRL_COPY, 0, out)) { | 
| 1427 | 0 |             out->cipher = NULL; | 
| 1428 | 0 |             ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); | 
| 1429 | 0 |             return 0; | 
| 1430 | 0 |         } | 
| 1431 | 0 |     return 1; | 
| 1432 | 0 | } | 
| 1433 |  |  | 
| 1434 |  | EVP_CIPHER *evp_cipher_new(void) | 
| 1435 | 372 | { | 
| 1436 | 372 |     EVP_CIPHER *cipher = OPENSSL_zalloc(sizeof(EVP_CIPHER)); | 
| 1437 |  |  | 
| 1438 | 372 |     if (cipher != NULL) { | 
| 1439 | 372 |         cipher->lock = CRYPTO_THREAD_lock_new(); | 
| 1440 | 372 |         if (cipher->lock == NULL) { | 
| 1441 | 0 |             OPENSSL_free(cipher); | 
| 1442 | 0 |             return NULL; | 
| 1443 | 0 |         } | 
| 1444 | 372 |         cipher->refcnt = 1; | 
| 1445 | 372 |     } | 
| 1446 | 372 |     return cipher; | 
| 1447 | 372 | } | 
| 1448 |  |  | 
| 1449 |  | /* | 
| 1450 |  |  * FIPS module note: since internal fetches will be entirely | 
| 1451 |  |  * provider based, we know that none of its code depends on legacy | 
| 1452 |  |  * NIDs or any functionality that use them. | 
| 1453 |  |  */ | 
| 1454 |  | #ifndef FIPS_MODULE | 
| 1455 |  | /* After removal of legacy support get rid of the need for legacy NIDs */ | 
| 1456 |  | static void set_legacy_nid(const char *name, void *vlegacy_nid) | 
| 1457 | 1.43k | { | 
| 1458 | 1.43k |     int nid; | 
| 1459 | 1.43k |     int *legacy_nid = vlegacy_nid; | 
| 1460 |  |     /* | 
| 1461 |  |      * We use lowest level function to get the associated method, because | 
| 1462 |  |      * higher level functions such as EVP_get_cipherbyname() have changed | 
| 1463 |  |      * to look at providers too. | 
| 1464 |  |      */ | 
| 1465 | 1.43k |     const void *legacy_method = OBJ_NAME_get(name, OBJ_NAME_TYPE_CIPHER_METH); | 
| 1466 |  |  | 
| 1467 | 1.43k |     if (*legacy_nid == -1)       /* We found a clash already */ | 
| 1468 | 0 |         return; | 
| 1469 | 1.43k |     if (legacy_method == NULL) | 
| 1470 | 636 |         return; | 
| 1471 | 795 |     nid = EVP_CIPHER_get_nid(legacy_method); | 
| 1472 | 795 |     if (*legacy_nid != NID_undef && *legacy_nid != nid) { | 
| 1473 | 0 |         *legacy_nid = -1; | 
| 1474 | 0 |         return; | 
| 1475 | 0 |     } | 
| 1476 | 795 |     *legacy_nid = nid; | 
| 1477 | 795 | } | 
| 1478 |  | #endif | 
| 1479 |  |  | 
| 1480 |  | static void *evp_cipher_from_algorithm(const int name_id, | 
| 1481 |  |                                        const OSSL_ALGORITHM *algodef, | 
| 1482 |  |                                        OSSL_PROVIDER *prov) | 
| 1483 | 762 | { | 
| 1484 | 762 |     const OSSL_DISPATCH *fns = algodef->implementation; | 
| 1485 | 762 |     EVP_CIPHER *cipher = NULL; | 
| 1486 | 762 |     int fnciphcnt = 0, fnctxcnt = 0; | 
| 1487 |  |  | 
| 1488 | 762 |     if ((cipher = evp_cipher_new()) == NULL) { | 
| 1489 | 0 |         ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE); | 
| 1490 | 0 |         return NULL; | 
| 1491 | 0 |     } | 
| 1492 |  |  | 
| 1493 | 762 | #ifndef FIPS_MODULE | 
| 1494 | 762 |     cipher->nid = NID_undef; | 
| 1495 | 762 |     if (!evp_names_do_all(prov, name_id, set_legacy_nid, &cipher->nid) | 
| 1496 | 762 |             || cipher->nid == -1) { | 
| 1497 | 0 |         ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR); | 
| 1498 | 0 |         EVP_CIPHER_free(cipher); | 
| 1499 | 0 |         return NULL; | 
| 1500 | 0 |     } | 
| 1501 | 762 | #endif | 
| 1502 |  |  | 
| 1503 | 762 |     cipher->name_id = name_id; | 
| 1504 | 762 |     if ((cipher->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL) { | 
| 1505 | 0 |         EVP_CIPHER_free(cipher); | 
| 1506 | 0 |         return NULL; | 
| 1507 | 0 |     } | 
| 1508 | 762 |     cipher->description = algodef->algorithm_description; | 
| 1509 |  |  | 
| 1510 | 11.2k |     for (; fns->function_id != 0; fns++) { | 
| 1511 | 10.5k |         switch (fns->function_id) { | 
| 1512 | 762 |         case OSSL_FUNC_CIPHER_NEWCTX: | 
| 1513 | 762 |             if (cipher->newctx != NULL) | 
| 1514 | 0 |                 break; | 
| 1515 | 762 |             cipher->newctx = OSSL_FUNC_cipher_newctx(fns); | 
| 1516 | 762 |             fnctxcnt++; | 
| 1517 | 762 |             break; | 
| 1518 | 762 |         case OSSL_FUNC_CIPHER_ENCRYPT_INIT: | 
| 1519 | 762 |             if (cipher->einit != NULL) | 
| 1520 | 0 |                 break; | 
| 1521 | 762 |             cipher->einit = OSSL_FUNC_cipher_encrypt_init(fns); | 
| 1522 | 762 |             fnciphcnt++; | 
| 1523 | 762 |             break; | 
| 1524 | 762 |         case OSSL_FUNC_CIPHER_DECRYPT_INIT: | 
| 1525 | 762 |             if (cipher->dinit != NULL) | 
| 1526 | 0 |                 break; | 
| 1527 | 762 |             cipher->dinit = OSSL_FUNC_cipher_decrypt_init(fns); | 
| 1528 | 762 |             fnciphcnt++; | 
| 1529 | 762 |             break; | 
| 1530 | 762 |         case OSSL_FUNC_CIPHER_UPDATE: | 
| 1531 | 762 |             if (cipher->cupdate != NULL) | 
| 1532 | 0 |                 break; | 
| 1533 | 762 |             cipher->cupdate = OSSL_FUNC_cipher_update(fns); | 
| 1534 | 762 |             fnciphcnt++; | 
| 1535 | 762 |             break; | 
| 1536 | 762 |         case OSSL_FUNC_CIPHER_FINAL: | 
| 1537 | 762 |             if (cipher->cfinal != NULL) | 
| 1538 | 0 |                 break; | 
| 1539 | 762 |             cipher->cfinal = OSSL_FUNC_cipher_final(fns); | 
| 1540 | 762 |             fnciphcnt++; | 
| 1541 | 762 |             break; | 
| 1542 | 690 |         case OSSL_FUNC_CIPHER_CIPHER: | 
| 1543 | 690 |             if (cipher->ccipher != NULL) | 
| 1544 | 0 |                 break; | 
| 1545 | 690 |             cipher->ccipher = OSSL_FUNC_cipher_cipher(fns); | 
| 1546 | 690 |             break; | 
| 1547 | 762 |         case OSSL_FUNC_CIPHER_FREECTX: | 
| 1548 | 762 |             if (cipher->freectx != NULL) | 
| 1549 | 0 |                 break; | 
| 1550 | 762 |             cipher->freectx = OSSL_FUNC_cipher_freectx(fns); | 
| 1551 | 762 |             fnctxcnt++; | 
| 1552 | 762 |             break; | 
| 1553 | 669 |         case OSSL_FUNC_CIPHER_DUPCTX: | 
| 1554 | 669 |             if (cipher->dupctx != NULL) | 
| 1555 | 0 |                 break; | 
| 1556 | 669 |             cipher->dupctx = OSSL_FUNC_cipher_dupctx(fns); | 
| 1557 | 669 |             break; | 
| 1558 | 762 |         case OSSL_FUNC_CIPHER_GET_PARAMS: | 
| 1559 | 762 |             if (cipher->get_params != NULL) | 
| 1560 | 0 |                 break; | 
| 1561 | 762 |             cipher->get_params = OSSL_FUNC_cipher_get_params(fns); | 
| 1562 | 762 |             break; | 
| 1563 | 762 |         case OSSL_FUNC_CIPHER_GET_CTX_PARAMS: | 
| 1564 | 762 |             if (cipher->get_ctx_params != NULL) | 
| 1565 | 0 |                 break; | 
| 1566 | 762 |             cipher->get_ctx_params = OSSL_FUNC_cipher_get_ctx_params(fns); | 
| 1567 | 762 |             break; | 
| 1568 | 762 |         case OSSL_FUNC_CIPHER_SET_CTX_PARAMS: | 
| 1569 | 762 |             if (cipher->set_ctx_params != NULL) | 
| 1570 | 0 |                 break; | 
| 1571 | 762 |             cipher->set_ctx_params = OSSL_FUNC_cipher_set_ctx_params(fns); | 
| 1572 | 762 |             break; | 
| 1573 | 762 |         case OSSL_FUNC_CIPHER_GETTABLE_PARAMS: | 
| 1574 | 762 |             if (cipher->gettable_params != NULL) | 
| 1575 | 0 |                 break; | 
| 1576 | 762 |             cipher->gettable_params = OSSL_FUNC_cipher_gettable_params(fns); | 
| 1577 | 762 |             break; | 
| 1578 | 762 |         case OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS: | 
| 1579 | 762 |             if (cipher->gettable_ctx_params != NULL) | 
| 1580 | 0 |                 break; | 
| 1581 | 762 |             cipher->gettable_ctx_params = | 
| 1582 | 762 |                 OSSL_FUNC_cipher_gettable_ctx_params(fns); | 
| 1583 | 762 |             break; | 
| 1584 | 762 |         case OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS: | 
| 1585 | 762 |             if (cipher->settable_ctx_params != NULL) | 
| 1586 | 0 |                 break; | 
| 1587 | 762 |             cipher->settable_ctx_params = | 
| 1588 | 762 |                 OSSL_FUNC_cipher_settable_ctx_params(fns); | 
| 1589 | 762 |             break; | 
| 1590 | 10.5k |         } | 
| 1591 | 10.5k |     } | 
| 1592 | 762 |     if ((fnciphcnt != 0 && fnciphcnt != 3 && fnciphcnt != 4) | 
| 1593 | 762 |             || (fnciphcnt == 0 && cipher->ccipher == NULL) | 
| 1594 | 762 |             || fnctxcnt != 2) { | 
| 1595 |  |         /* | 
| 1596 |  |          * In order to be a consistent set of functions we must have at least | 
| 1597 |  |          * a complete set of "encrypt" functions, or a complete set of "decrypt" | 
| 1598 |  |          * functions, or a single "cipher" function. In all cases we need both | 
| 1599 |  |          * the "newctx" and "freectx" functions. | 
| 1600 |  |          */ | 
| 1601 | 0 |         EVP_CIPHER_free(cipher); | 
| 1602 | 0 |         ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS); | 
| 1603 | 0 |         return NULL; | 
| 1604 | 0 |     } | 
| 1605 | 762 |     cipher->prov = prov; | 
| 1606 | 762 |     if (prov != NULL) | 
| 1607 | 762 |         ossl_provider_up_ref(prov); | 
| 1608 |  |  | 
| 1609 | 762 |     if (!evp_cipher_cache_constants(cipher)) { | 
| 1610 | 0 |         EVP_CIPHER_free(cipher); | 
| 1611 | 0 |         ERR_raise(ERR_LIB_EVP, EVP_R_CACHE_CONSTANTS_FAILED); | 
| 1612 | 0 |         cipher = NULL; | 
| 1613 | 0 |     } | 
| 1614 |  |  | 
| 1615 | 762 |     return cipher; | 
| 1616 | 762 | } | 
| 1617 |  |  | 
| 1618 |  | static int evp_cipher_up_ref(void *cipher) | 
| 1619 | 344k | { | 
| 1620 | 344k |     return EVP_CIPHER_up_ref(cipher); | 
| 1621 | 344k | } | 
| 1622 |  |  | 
| 1623 |  | static void evp_cipher_free(void *cipher) | 
| 1624 | 1.59k | { | 
| 1625 | 1.59k |     EVP_CIPHER_free(cipher); | 
| 1626 | 1.59k | } | 
| 1627 |  |  | 
| 1628 |  | EVP_CIPHER *EVP_CIPHER_fetch(OSSL_LIB_CTX *ctx, const char *algorithm, | 
| 1629 |  |                              const char *properties) | 
| 1630 | 562k | { | 
| 1631 | 562k |     EVP_CIPHER *cipher = | 
| 1632 | 562k |         evp_generic_fetch(ctx, OSSL_OP_CIPHER, algorithm, properties, | 
| 1633 | 562k |                           evp_cipher_from_algorithm, evp_cipher_up_ref, | 
| 1634 | 562k |                           evp_cipher_free); | 
| 1635 |  |  | 
| 1636 | 562k |     return cipher; | 
| 1637 | 562k | } | 
| 1638 |  |  | 
| 1639 |  | int EVP_CIPHER_up_ref(EVP_CIPHER *cipher) | 
| 1640 | 460k | { | 
| 1641 | 460k |     int ref = 0; | 
| 1642 |  |  | 
| 1643 | 460k |     if (cipher->origin == EVP_ORIG_DYNAMIC) | 
| 1644 | 460k |         CRYPTO_UP_REF(&cipher->refcnt, &ref, cipher->lock); | 
| 1645 | 460k |     return 1; | 
| 1646 | 460k | } | 
| 1647 |  |  | 
| 1648 |  | void evp_cipher_free_int(EVP_CIPHER *cipher) | 
| 1649 | 762 | { | 
| 1650 | 762 |     OPENSSL_free(cipher->type_name); | 
| 1651 | 762 |     ossl_provider_free(cipher->prov); | 
| 1652 | 762 |     CRYPTO_THREAD_lock_free(cipher->lock); | 
| 1653 | 762 |     OPENSSL_free(cipher); | 
| 1654 | 762 | } | 
| 1655 |  |  | 
| 1656 |  | void EVP_CIPHER_free(EVP_CIPHER *cipher) | 
| 1657 | 597k | { | 
| 1658 | 597k |     int i; | 
| 1659 |  |  | 
| 1660 | 597k |     if (cipher == NULL || cipher->origin != EVP_ORIG_DYNAMIC) | 
| 1661 | 135k |         return; | 
| 1662 |  |  | 
| 1663 | 461k |     CRYPTO_DOWN_REF(&cipher->refcnt, &i, cipher->lock); | 
| 1664 | 461k |     if (i > 0) | 
| 1665 | 460k |         return; | 
| 1666 | 762 |     evp_cipher_free_int(cipher); | 
| 1667 | 762 | } | 
| 1668 |  |  | 
| 1669 |  | void EVP_CIPHER_do_all_provided(OSSL_LIB_CTX *libctx, | 
| 1670 |  |                                 void (*fn)(EVP_CIPHER *mac, void *arg), | 
| 1671 |  |                                 void *arg) | 
| 1672 | 0 | { | 
| 1673 | 0 |     evp_generic_do_all(libctx, OSSL_OP_CIPHER, | 
| 1674 | 0 |                        (void (*)(void *, void *))fn, arg, | 
| 1675 | 0 |                        evp_cipher_from_algorithm, evp_cipher_up_ref, | 
| 1676 | 0 |                        evp_cipher_free); | 
| 1677 | 0 | } |