+
+EVP_CIPHER *evp_cipher_new(void)
+{
+ EVP_CIPHER *cipher = OPENSSL_zalloc(sizeof(EVP_CIPHER));
+
+ if (cipher != NULL) {
+ cipher->lock = CRYPTO_THREAD_lock_new();
+ if (cipher->lock == NULL) {
+ OPENSSL_free(cipher);
+ return NULL;
+ }
+ cipher->refcnt = 1;
+ }
+ return cipher;
+}
+
+/*
+ * FIPS module note: since internal fetches will be entirely
+ * provider based, we know that none of its code depends on legacy
+ * NIDs or any functionality that use them.
+ */
+#ifndef FIPS_MODE
+/* TODO(3.x) get rid of the need for legacy NIDs */
+static void set_legacy_nid(const char *name, void *vlegacy_nid)
+{
+ int nid;
+ int *legacy_nid = vlegacy_nid;
+
+ if (*legacy_nid == -1) /* We found a clash already */
+ return;
+ if ((nid = OBJ_sn2nid(name)) == NID_undef
+ && (nid = OBJ_ln2nid(name)) == NID_undef)
+ return;
+ if (*legacy_nid != NID_undef && *legacy_nid != nid) {
+ *legacy_nid = -1;
+ return;
+ }
+ *legacy_nid = nid;
+}
+#endif
+
+static void *evp_cipher_from_dispatch(const int name_id,
+ const OSSL_DISPATCH *fns,
+ OSSL_PROVIDER *prov,
+ void *unused)
+{
+ EVP_CIPHER *cipher = NULL;
+ int fnciphcnt = 0, fnctxcnt = 0;
+
+ if ((cipher = evp_cipher_new()) == NULL) {
+ EVPerr(0, ERR_R_MALLOC_FAILURE);
+ return NULL;
+ }
+
+#ifndef FIPS_MODE
+ /* TODO(3.x) get rid of the need for legacy NIDs */
+ cipher->nid = NID_undef;
+ evp_doall_names(prov, name_id, set_legacy_nid, &cipher->nid);
+ if (cipher->nid == -1) {
+ ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
+ EVP_CIPHER_free(cipher);
+ return NULL;
+ }
+#endif
+
+ cipher->name_id = name_id;
+
+ for (; fns->function_id != 0; fns++) {
+ switch (fns->function_id) {
+ case OSSL_FUNC_CIPHER_NEWCTX:
+ if (cipher->newctx != NULL)
+ break;
+ cipher->newctx = OSSL_get_OP_cipher_newctx(fns);
+ fnctxcnt++;
+ break;
+ case OSSL_FUNC_CIPHER_ENCRYPT_INIT:
+ if (cipher->einit != NULL)
+ break;
+ cipher->einit = OSSL_get_OP_cipher_encrypt_init(fns);
+ fnciphcnt++;
+ break;
+ case OSSL_FUNC_CIPHER_DECRYPT_INIT:
+ if (cipher->dinit != NULL)
+ break;
+ cipher->dinit = OSSL_get_OP_cipher_decrypt_init(fns);
+ fnciphcnt++;
+ break;
+ case OSSL_FUNC_CIPHER_UPDATE:
+ if (cipher->cupdate != NULL)
+ break;
+ cipher->cupdate = OSSL_get_OP_cipher_update(fns);
+ fnciphcnt++;
+ break;
+ case OSSL_FUNC_CIPHER_FINAL:
+ if (cipher->cfinal != NULL)
+ break;
+ cipher->cfinal = OSSL_get_OP_cipher_final(fns);
+ fnciphcnt++;
+ break;
+ case OSSL_FUNC_CIPHER_CIPHER:
+ if (cipher->ccipher != NULL)
+ break;
+ cipher->ccipher = OSSL_get_OP_cipher_cipher(fns);
+ break;
+ case OSSL_FUNC_CIPHER_FREECTX:
+ if (cipher->freectx != NULL)
+ break;
+ cipher->freectx = OSSL_get_OP_cipher_freectx(fns);
+ fnctxcnt++;
+ break;
+ case OSSL_FUNC_CIPHER_DUPCTX:
+ if (cipher->dupctx != NULL)
+ break;
+ cipher->dupctx = OSSL_get_OP_cipher_dupctx(fns);
+ break;
+ case OSSL_FUNC_CIPHER_GET_PARAMS:
+ if (cipher->get_params != NULL)
+ break;
+ cipher->get_params = OSSL_get_OP_cipher_get_params(fns);
+ break;
+ case OSSL_FUNC_CIPHER_GET_CTX_PARAMS:
+ if (cipher->get_ctx_params != NULL)
+ break;
+ cipher->get_ctx_params = OSSL_get_OP_cipher_get_ctx_params(fns);
+ break;
+ case OSSL_FUNC_CIPHER_SET_CTX_PARAMS:
+ if (cipher->set_ctx_params != NULL)
+ break;
+ cipher->set_ctx_params = OSSL_get_OP_cipher_set_ctx_params(fns);
+ break;
+ case OSSL_FUNC_CIPHER_GETTABLE_PARAMS:
+ if (cipher->gettable_params != NULL)
+ break;
+ cipher->gettable_params = OSSL_get_OP_cipher_gettable_params(fns);
+ break;
+ case OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS:
+ if (cipher->gettable_ctx_params != NULL)
+ break;
+ cipher->gettable_ctx_params =
+ OSSL_get_OP_cipher_gettable_ctx_params(fns);
+ break;
+ case OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS:
+ if (cipher->settable_ctx_params != NULL)
+ break;
+ cipher->settable_ctx_params =
+ OSSL_get_OP_cipher_settable_ctx_params(fns);
+ break;
+ }
+ }
+ if ((fnciphcnt != 0 && fnciphcnt != 3 && fnciphcnt != 4)
+ || (fnciphcnt == 0 && cipher->ccipher == NULL)
+ || fnctxcnt != 2) {
+ /*
+ * In order to be a consistent set of functions we must have at least
+ * a complete set of "encrypt" functions, or a complete set of "decrypt"
+ * functions, or a single "cipher" function. In all cases we need both
+ * the "newctx" and "freectx" functions.
+ */
+ EVP_CIPHER_free(cipher);
+ EVPerr(EVP_F_EVP_CIPHER_FROM_DISPATCH, EVP_R_INVALID_PROVIDER_FUNCTIONS);
+ return NULL;
+ }
+ cipher->prov = prov;
+ if (prov != NULL)
+ ossl_provider_up_ref(prov);
+
+ return cipher;
+}
+
+static int evp_cipher_up_ref(void *cipher)
+{
+ return EVP_CIPHER_up_ref(cipher);
+}
+
+static void evp_cipher_free(void *cipher)
+{
+ EVP_CIPHER_free(cipher);
+}
+
+EVP_CIPHER *EVP_CIPHER_fetch(OPENSSL_CTX *ctx, const char *algorithm,
+ const char *properties)
+{
+ EVP_CIPHER *cipher =
+ evp_generic_fetch(ctx, OSSL_OP_CIPHER, algorithm, properties,
+ evp_cipher_from_dispatch, NULL, evp_cipher_up_ref,
+ evp_cipher_free);
+
+ return cipher;
+}
+
+int EVP_CIPHER_up_ref(EVP_CIPHER *cipher)
+{
+ int ref = 0;
+
+ CRYPTO_UP_REF(&cipher->refcnt, &ref, cipher->lock);
+ return 1;
+}
+
+void EVP_CIPHER_free(EVP_CIPHER *cipher)
+{
+ int i;
+
+ if (cipher == NULL)
+ return;
+
+ CRYPTO_DOWN_REF(&cipher->refcnt, &i, cipher->lock);
+ if (i > 0)
+ return;
+ ossl_provider_free(cipher->prov);
+ CRYPTO_THREAD_lock_free(cipher->lock);
+ OPENSSL_free(cipher);
+}
+
+void EVP_CIPHER_do_all_ex(OPENSSL_CTX *libctx,
+ void (*fn)(EVP_CIPHER *mac, void *arg),
+ void *arg)
+{
+ evp_generic_do_all(libctx, OSSL_OP_CIPHER,
+ (void (*)(void *, void *))fn, arg,
+ evp_cipher_from_dispatch, NULL, evp_cipher_free);
+}