+ /*
+ * If there are engines involved then we should use legacy handling for now.
+ */
+ if (ctx->engine != NULL
+#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
+ || tmpimpl != NULL
+#endif
+ || impl != NULL) {
+ if (ctx->cipher == ctx->fetched_cipher)
+ ctx->cipher = NULL;
+ EVP_CIPHER_meth_free(ctx->fetched_cipher);
+ ctx->fetched_cipher = NULL;
+ goto legacy;
+ }
+
+ tmpcipher = (cipher == NULL) ? ctx->cipher : cipher;
+
+ if (tmpcipher->prov == NULL) {
+ switch(tmpcipher->nid) {
+ case NID_aes_256_ecb:
+ case NID_aes_192_ecb:
+ case NID_aes_128_ecb:
+ case NID_aes_256_cbc:
+ case NID_aes_192_cbc:
+ case NID_aes_128_cbc:
+ case NID_aes_256_ofb128:
+ case NID_aes_192_ofb128:
+ case NID_aes_128_ofb128:
+ case NID_aes_256_cfb128:
+ case NID_aes_192_cfb128:
+ case NID_aes_128_cfb128:
+ case NID_aes_256_cfb1:
+ case NID_aes_192_cfb1:
+ case NID_aes_128_cfb1:
+ case NID_aes_256_cfb8:
+ case NID_aes_192_cfb8:
+ case NID_aes_128_cfb8:
+ case NID_aes_256_ctr:
+ case NID_aes_192_ctr:
+ case NID_aes_128_ctr:
+ break;
+ default:
+ goto legacy;
+ }
+ }
+
+ /*
+ * Ensure a context left lying around from last time is cleared
+ * (legacy code)
+ */
+ if (cipher != NULL && ctx->cipher != NULL) {
+ OPENSSL_clear_free(ctx->cipher_data, ctx->cipher->ctx_size);
+ ctx->cipher_data = NULL;
+ }
+
+
+ /* TODO(3.0): Start of non-legacy code below */
+
+ /* Ensure a context left lying around from last time is cleared */
+ if (cipher != NULL && ctx->cipher != NULL) {
+ unsigned long flags = ctx->flags;
+
+ EVP_CIPHER_CTX_reset(ctx);
+ /* Restore encrypt and flags */
+ ctx->encrypt = enc;
+ ctx->flags = flags;
+ }
+
+ if (cipher != NULL)
+ ctx->cipher = cipher;
+ else
+ cipher = ctx->cipher;
+
+ if (cipher->prov == NULL) {
+#ifdef FIPS_MODE
+ /* We only do explict fetches inside the FIPS module */
+ EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
+ return 0;
+#else
+ EVP_CIPHER *provciph =
+ EVP_CIPHER_fetch(NULL, OBJ_nid2sn(cipher->nid), "");
+
+ if (provciph == NULL) {
+ EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
+ return 0;
+ }
+ cipher = provciph;
+ EVP_CIPHER_meth_free(ctx->fetched_cipher);
+ ctx->fetched_cipher = provciph;
+#endif
+ }
+
+ ctx->cipher = cipher;
+ if (ctx->provctx == NULL) {
+ ctx->provctx = ctx->cipher->newctx(ossl_provider_ctx(cipher->prov));
+ if (ctx->provctx == NULL) {
+ EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
+ return 0;
+ }
+ }
+
+ if ((ctx->flags & EVP_CIPH_NO_PADDING) != 0) {
+ /*
+ * If this ctx was already set up for no padding then we need to tell
+ * the new cipher about it.
+ */
+ if (!EVP_CIPHER_CTX_set_padding(ctx, 0))
+ return 0;
+ }
+
+ switch (EVP_CIPHER_mode(ctx->cipher)) {
+ case EVP_CIPH_CFB_MODE:
+ case EVP_CIPH_OFB_MODE:
+ case EVP_CIPH_CBC_MODE:
+ /* For these modes we remember the original IV for later use */
+ if (!ossl_assert(EVP_CIPHER_CTX_iv_length(ctx) <= (int)sizeof(ctx->oiv))) {
+ EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
+ return 0;
+ }
+ if (iv != NULL)
+ memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
+ }
+
+ if (enc) {
+ if (ctx->cipher->einit == NULL) {
+ EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
+ return 0;
+ }
+
+ return ctx->cipher->einit(ctx->provctx,
+ key,
+ key == NULL ? 0
+ : EVP_CIPHER_CTX_key_length(ctx),
+ iv,
+ iv == NULL ? 0
+ : EVP_CIPHER_CTX_iv_length(ctx));
+ }
+
+ if (ctx->cipher->dinit == NULL) {
+ EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
+ return 0;
+ }
+
+ return ctx->cipher->dinit(ctx->provctx,
+ key,
+ key == NULL ? 0
+ : EVP_CIPHER_CTX_key_length(ctx),
+ iv,
+ iv == NULL ? 0
+ : EVP_CIPHER_CTX_iv_length(ctx));
+
+ /* TODO(3.0): Remove legacy code below */
+ legacy:
+
+ if (cipher != NULL) {
+ /*
+ * Ensure a context left lying around from last time is cleared (we
+ * previously attempted to avoid this if the same ENGINE and
+ * EVP_CIPHER could be used).
+ */
+ if (ctx->cipher) {
+ unsigned long flags = ctx->flags;
+ EVP_CIPHER_CTX_reset(ctx);
+ /* Restore encrypt and flags */
+ ctx->encrypt = enc;
+ ctx->flags = flags;
+ }
+#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
+ if (impl != NULL) {
+ if (!ENGINE_init(impl)) {
+ EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
+ return 0;
+ }
+ } else {
+ impl = tmpimpl;
+ }
+ if (impl != NULL) {
+ /* There's an ENGINE for this job ... (apparently) */
+ const EVP_CIPHER *c = ENGINE_get_cipher(impl, cipher->nid);
+
+ if (c == NULL) {
+ /*
+ * One positive side-effect of US's export control history,
+ * is that we should at least be able to avoid using US
+ * misspellings of "initialisation"?
+ */
+ EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
+ return 0;
+ }
+ /* We'll use the ENGINE's private cipher definition */
+ cipher = c;
+ /*
+ * Store the ENGINE functional reference so we know 'cipher' came
+ * from an ENGINE and we need to release it when done.
+ */
+ ctx->engine = impl;
+ } else {
+ ctx->engine = NULL;
+ }