static int test_badmod()
{
    BIGNUM *a = BN_new();
    BIGNUM *b = BN_new();
    BIGNUM *zero = BN_new();
    BN_MONT_CTX *mont = BN_MONT_CTX_new();
    int st = 0;

    if (a == NULL || b == NULL || zero == NULL || mont == NULL)
        goto err;
    BN_zero(zero);

    if (BN_div(a, b, BN_value_one(), zero, ctx)) {
        fprintf(stderr, "Division by zero succeeded!\n");
        goto err;
    }
    ERR_clear_error();

    if (BN_mod_mul(a, BN_value_one(), BN_value_one(), zero, ctx)) {
        fprintf(stderr, "BN_mod_mul with zero modulus succeeded!\n");
        goto err;
    }
    ERR_clear_error();

    if (BN_mod_exp(a, BN_value_one(), BN_value_one(), zero, ctx)) {
        fprintf(stderr, "BN_mod_exp with zero modulus succeeded!\n");
        goto err;
    }
    ERR_clear_error();

    if (BN_mod_exp_mont(a, BN_value_one(), BN_value_one(), zero, ctx, NULL)) {
        fprintf(stderr, "BN_mod_exp_mont with zero modulus succeeded!\n");
        goto err;
    }
    ERR_clear_error();

    if (BN_mod_exp_mont_consttime(a, BN_value_one(), BN_value_one(),
                                  zero, ctx, NULL)) {
        fprintf(stderr,
                "BN_mod_exp_mont_consttime with zero modulus succeeded!\n");
        goto err;
    }
    ERR_clear_error();

    if (BN_MONT_CTX_set(mont, zero, ctx)) {
        fprintf(stderr, "BN_MONT_CTX_set succeeded for zero modulus!\n");
        goto err;
    }
    ERR_clear_error();

    /* Some operations also may not be used with an even modulus. */
    if (!BN_set_word(b, 16))
        goto err;

    if (BN_MONT_CTX_set(mont, b, ctx)) {
        fprintf(stderr,
                "BN_MONT_CTX_set succeeded for even modulus!\n");
        goto err;
    }
    ERR_clear_error();

    if (BN_mod_exp_mont(a, BN_value_one(), BN_value_one(), b, ctx, NULL)) {
        fprintf(stderr,
                "BN_mod_exp_mont with even modulus succeeded!\n");
        goto err;
    }
    ERR_clear_error();

    if (BN_mod_exp_mont_consttime(a, BN_value_one(), BN_value_one(),
                                  b, ctx, NULL)) {
        fprintf(stderr,
                "BN_mod_exp_mont_consttime with even modulus succeeded!\n");
        goto err;
    }
    ERR_clear_error();

    st = 1;
err:
    BN_free(a);
    BN_free(b);
    BN_free(zero);
    BN_MONT_CTX_free(mont);
    return st;
}