int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
                      int cmd, int p1, void *p2)
{
    int ret;
    if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl) {
        EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
        return -2;
    }
    if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
        return -1;
    if (ctx->pmeth->digest_custom != NULL)
        goto doit;
    if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
        EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_NO_OPERATION_SET);
        return -1;
    }
    if ((optype != -1) && !(ctx->operation & optype)) {
        EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_INVALID_OPERATION);
        return -1;
    }
 doit:
    ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
    if (ret == -2)
        EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
    return ret;
}
['static int test_mdc2(void)\n{\n    int testresult = 0;\n    unsigned char md[MDC2_DIGEST_LENGTH];\n    EVP_MD_CTX *c;\n    static char text[] = "Now is the time for all ";\n    size_t tlen = strlen(text);\n# ifdef CHARSET_EBCDIC\n    ebcdic2ascii(text, text, tlen);\n# endif\n    c = EVP_MD_CTX_new();\n    if (!TEST_ptr(c)\n        || !TEST_true(EVP_DigestInit_ex(c, EVP_mdc2(), NULL))\n        || !TEST_true(EVP_DigestUpdate(c, (unsigned char *)text, tlen))\n        || !TEST_true(EVP_DigestFinal_ex(c, &(md[0]), NULL))\n        || !TEST_mem_eq(md, MDC2_DIGEST_LENGTH, pad1, MDC2_DIGEST_LENGTH)\n        || !TEST_true(EVP_DigestInit_ex(c, EVP_mdc2(), NULL)))\n        goto end;\n    ((MDC2_CTX *)EVP_MD_CTX_md_data(c))->pad_type = 2;\n    if (!TEST_true(EVP_DigestUpdate(c, (unsigned char *)text, tlen))\n        || !TEST_true(EVP_DigestFinal_ex(c, &(md[0]), NULL))\n        || !TEST_mem_eq(md, MDC2_DIGEST_LENGTH, pad2, MDC2_DIGEST_LENGTH))\n        goto end;\n    testresult = 1;\n end:\n    EVP_MD_CTX_free(c);\n    return testresult;\n}', 'int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *isize)\n{\n    int ret;\n    size_t size = 0;\n    if (ctx->digest == NULL || ctx->digest->prov == NULL)\n        goto legacy;\n    if (ctx->digest->dfinal == NULL) {\n        EVPerr(EVP_F_EVP_DIGESTFINAL_EX, EVP_R_FINAL_ERROR);\n        return 0;\n    }\n    ret = ctx->digest->dfinal(ctx->provctx, md, &size);\n    if (isize != NULL) {\n        if (size <= UINT_MAX) {\n            *isize = (int)size;\n        } else {\n            EVPerr(EVP_F_EVP_DIGESTFINAL_EX, EVP_R_FINAL_ERROR);\n            ret = 0;\n        }\n    }\n    EVP_MD_CTX_reset(ctx);\n    return ret;\n legacy:\n    OPENSSL_assert(ctx->digest->md_size <= EVP_MAX_MD_SIZE);\n    ret = ctx->digest->final(ctx, md);\n    if (isize != NULL)\n        *isize = ctx->digest->md_size;\n    if (ctx->digest->cleanup) {\n        ctx->digest->cleanup(ctx);\n        EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);\n    }\n    OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);\n    return ret;\n}', 'int EVP_MD_CTX_reset(EVP_MD_CTX *ctx)\n{\n    if (ctx == NULL)\n        return 1;\n    if (ctx->digest == NULL || ctx->digest->prov == NULL)\n        goto legacy;\n    if (ctx->provctx != NULL) {\n        if (ctx->digest->freectx != NULL)\n            ctx->digest->freectx(ctx->provctx);\n        ctx->provctx = NULL;\n        EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);\n    }\n    if (ctx->pctx != NULL)\n        goto legacy;\n    return 1;\n legacy:\n    if (ctx->digest && ctx->digest->cleanup\n        && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_CLEANED))\n        ctx->digest->cleanup(ctx);\n    if (ctx->digest && ctx->digest->ctx_size && ctx->md_data\n        && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_REUSE)) {\n        OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);\n    }\n    if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX))\n        EVP_PKEY_CTX_free(ctx->pctx);\n#ifndef OPENSSL_NO_ENGINE\n    ENGINE_finish(ctx->engine);\n#endif\n    OPENSSL_cleanse(ctx, sizeof(*ctx));\n    return 1;\n}', 'void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)\n{\n    if (ctx == NULL)\n        return;\n    if (ctx->pmeth && ctx->pmeth->cleanup)\n        ctx->pmeth->cleanup(ctx);\n    EVP_PKEY_free(ctx->pkey);\n    EVP_PKEY_free(ctx->peerkey);\n#ifndef OPENSSL_NO_ENGINE\n    ENGINE_finish(ctx->engine);\n#endif\n    OPENSSL_free(ctx);\n}', 'void CRYPTO_free(void *str, const char *file, int line)\n{\n    INCREMENT(free_count);\n    if (free_impl != NULL && free_impl != &CRYPTO_free) {\n        free_impl(str, file, line);\n        return;\n    }\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n    if (call_malloc_debug) {\n        CRYPTO_mem_debug_free(str, 0, file, line);\n        free(str);\n        CRYPTO_mem_debug_free(str, 1, file, line);\n    } else {\n        free(str);\n    }\n#else\n    free(str);\n#endif\n}', 'int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)\n{\n    EVP_MD *provmd;\n    ENGINE *tmpimpl = NULL;\n    EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);\n#ifndef OPENSSL_NO_ENGINE\n    if (ctx->engine && ctx->digest &&\n        (type == NULL || (type->type == ctx->digest->type)))\n        goto skip_to_init;\n    if (type != NULL && impl == NULL)\n        tmpimpl = ENGINE_get_digest_engine(type->type);\n#endif\n    if (ctx->engine != NULL\n            || impl != NULL\n            || tmpimpl != NULL\n            || ctx->pctx != NULL\n            || (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) != 0) {\n        if (ctx->digest == ctx->fetched_digest)\n            ctx->digest = NULL;\n        EVP_MD_meth_free(ctx->fetched_digest);\n        ctx->fetched_digest = NULL;\n        goto legacy;\n    }\n    if (type->prov == NULL) {\n        switch(type->type) {\n        case NID_sha256:\n            break;\n        default:\n            goto legacy;\n        }\n    }\n    if (ctx->digest != NULL && ctx->digest->ctx_size > 0) {\n        OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);\n        ctx->md_data = NULL;\n    }\n    if (type->prov == NULL) {\n        provmd = EVP_MD_fetch(NULL, OBJ_nid2sn(type->type), "");\n        if (provmd == NULL) {\n            EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);\n            return 0;\n        }\n        type = provmd;\n        EVP_MD_meth_free(ctx->fetched_digest);\n        ctx->fetched_digest = provmd;\n    }\n    ctx->digest = type;\n    if (ctx->provctx == NULL) {\n        ctx->provctx = ctx->digest->newctx();\n        if (ctx->provctx == NULL) {\n            EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);\n            return 0;\n        }\n    }\n    if (ctx->digest->dinit == NULL) {\n        EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);\n        return 0;\n    }\n    return ctx->digest->dinit(ctx->provctx);\n legacy:\n#ifndef OPENSSL_NO_ENGINE\n    if (type) {\n        ENGINE_finish(ctx->engine);\n        if (impl != NULL) {\n            if (!ENGINE_init(impl)) {\n                EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);\n                return 0;\n            }\n        } else {\n            impl = tmpimpl;\n        }\n        if (impl != NULL) {\n            const EVP_MD *d = ENGINE_get_digest(impl, type->type);\n            if (d == NULL) {\n                EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);\n                ENGINE_finish(impl);\n                return 0;\n            }\n            type = d;\n            ctx->engine = impl;\n        } else\n            ctx->engine = NULL;\n    } else {\n        if (!ctx->digest) {\n            EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_NO_DIGEST_SET);\n            return 0;\n        }\n        type = ctx->digest;\n    }\n#endif\n    if (ctx->digest != type) {\n        if (ctx->digest && ctx->digest->ctx_size) {\n            OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);\n            ctx->md_data = NULL;\n        }\n        ctx->digest = type;\n        if (!(ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) && type->ctx_size) {\n            ctx->update = type->update;\n            ctx->md_data = OPENSSL_zalloc(type->ctx_size);\n            if (ctx->md_data == NULL) {\n                EVPerr(EVP_F_EVP_DIGESTINIT_EX, ERR_R_MALLOC_FAILURE);\n                return 0;\n            }\n        }\n    }\n#ifndef OPENSSL_NO_ENGINE\n skip_to_init:\n#endif\n    if (ctx->pctx) {\n        int r;\n        r = EVP_PKEY_CTX_ctrl(ctx->pctx, -1, EVP_PKEY_OP_TYPE_SIG,\n                              EVP_PKEY_CTRL_DIGESTINIT, 0, ctx);\n        if (r <= 0 && (r != -2))\n            return 0;\n    }\n    if (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT)\n        return 1;\n    return ctx->digest->init(ctx);\n}', 'int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,\n                      int cmd, int p1, void *p2)\n{\n    int ret;\n    if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl) {\n        EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);\n        return -2;\n    }\n    if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))\n        return -1;\n    if (ctx->pmeth->digest_custom != NULL)\n        goto doit;\n    if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {\n        EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_NO_OPERATION_SET);\n        return -1;\n    }\n    if ((optype != -1) && !(ctx->operation & optype)) {\n        EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_INVALID_OPERATION);\n        return -1;\n    }\n doit:\n    ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);\n    if (ret == -2)\n        EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);\n    return ret;\n}']