static int cipher_test_enc(EVP_TEST *t, int enc,
                           size_t out_misalign, size_t inp_misalign, int frag)
{
    CIPHER_DATA *expected = t->data;
    unsigned char *in, *expected_out, *tmp = NULL;
    size_t in_len, out_len, donelen = 0;
    int ok = 0, tmplen, chunklen, tmpflen;
    EVP_CIPHER_CTX *ctx = NULL;
    t->err = "TEST_FAILURE";
    if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new()))
        goto err;
    EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
    if (enc) {
        in = expected->plaintext;
        in_len = expected->plaintext_len;
        expected_out = expected->ciphertext;
        out_len = expected->ciphertext_len;
    } else {
        in = expected->ciphertext;
        in_len = expected->ciphertext_len;
        expected_out = expected->plaintext;
        out_len = expected->plaintext_len;
    }
    if (inp_misalign == (size_t)-1) {
        tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH);
        if (!tmp)
            goto err;
        in = memcpy(tmp + out_misalign, in, in_len);
    } else {
        inp_misalign += 16 - ((out_misalign + in_len) & 15);
        tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
                             inp_misalign + in_len);
        if (!tmp)
            goto err;
        in = memcpy(tmp + out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
                    inp_misalign, in, in_len);
    }
    if (!EVP_CipherInit_ex(ctx, expected->cipher, NULL, NULL, NULL, enc)) {
        t->err = "CIPHERINIT_ERROR";
        goto err;
    }
    if (expected->iv) {
        if (expected->aead) {
            if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
                                     expected->iv_len, 0)) {
                t->err = "INVALID_IV_LENGTH";
                goto err;
            }
        } else if (expected->iv_len != (size_t)EVP_CIPHER_CTX_iv_length(ctx)) {
            t->err = "INVALID_IV_LENGTH";
            goto err;
        }
    }
    if (expected->aead) {
        unsigned char *tag;
        if (enc || expected->aead == EVP_CIPH_OCB_MODE) {
            t->err = "TAG_LENGTH_SET_ERROR";
            tag = NULL;
        } else {
            t->err = "TAG_SET_ERROR";
            tag = expected->tag;
        }
        if (tag || expected->aead != EVP_CIPH_GCM_MODE) {
            if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
                                     expected->tag_len, tag))
                goto err;
        }
    }
    if (!EVP_CIPHER_CTX_set_key_length(ctx, expected->key_len)) {
        t->err = "INVALID_KEY_LENGTH";
        goto err;
    }
    if (!EVP_CipherInit_ex(ctx, NULL, NULL, expected->key, expected->iv, -1)) {
        t->err = "KEY_SET_ERROR";
        goto err;
    }
    if (!enc && expected->aead == EVP_CIPH_OCB_MODE) {
        if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
                                 expected->tag_len, expected->tag)) {
            t->err = "TAG_SET_ERROR";
            goto err;
        }
    }
    if (expected->aead == EVP_CIPH_CCM_MODE) {
        if (!EVP_CipherUpdate(ctx, NULL, &tmplen, NULL, out_len)) {
            t->err = "CCM_PLAINTEXT_LENGTH_SET_ERROR";
            goto err;
        }
    }
    if (expected->aad) {
        t->err = "AAD_SET_ERROR";
        if (!frag) {
            if (!EVP_CipherUpdate(ctx, NULL, &chunklen, expected->aad,
                                  expected->aad_len))
                goto err;
        } else {
            if (expected->aad_len > 0) {
                if (!EVP_CipherUpdate(ctx, NULL, &chunklen, expected->aad, 1))
                    goto err;
                donelen++;
            }
            if (expected->aad_len > 2) {
                if (!EVP_CipherUpdate(ctx, NULL, &chunklen,
                                      expected->aad + donelen,
                                      expected->aad_len - 2))
                    goto err;
                donelen += expected->aad_len - 2;
            }
            if (expected->aad_len > 1
                    && !EVP_CipherUpdate(ctx, NULL, &chunklen,
                                         expected->aad + donelen, 1))
                goto err;
        }
    }
    EVP_CIPHER_CTX_set_padding(ctx, 0);
    t->err = "CIPHERUPDATE_ERROR";
    tmplen = 0;
    if (!frag) {
        if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &tmplen, in, in_len))
            goto err;
    } else {
        if (in_len > 0) {
            if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &chunklen, in, 1))
                goto err;
            tmplen += chunklen;
            in++;
            in_len--;
        }
        if (in_len > 1) {
            if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
                                  in, in_len - 1))
                goto err;
            tmplen += chunklen;
            in += in_len - 1;
            in_len = 1;
        }
        if (in_len > 0 ) {
            if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
                                  in, 1))
                goto err;
            tmplen += chunklen;
        }
    }
    if (!EVP_CipherFinal_ex(ctx, tmp + out_misalign + tmplen, &tmpflen)) {
        t->err = "CIPHERFINAL_ERROR";
        goto err;
    }
    if (!TEST_mem_eq(expected_out, out_len,
                     tmp + out_misalign, tmplen + tmpflen)) {
        t->err = "VALUE_MISMATCH";
        goto err;
    }
    if (enc && expected->aead) {
        unsigned char rtag[16];
        if (!TEST_size_t_le(expected->tag_len, sizeof(rtag))) {
            t->err = "TAG_LENGTH_INTERNAL_ERROR";
            goto err;
        }
        if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
                                 expected->tag_len, rtag)) {
            t->err = "TAG_RETRIEVE_ERROR";
            goto err;
        }
        if (!TEST_mem_eq(expected->tag, expected->tag_len,
                         rtag, expected->tag_len)) {
            t->err = "TAG_VALUE_MISMATCH";
            goto err;
        }
    }
    t->err = NULL;
    ok = 1;
 err:
    OPENSSL_free(tmp);
    EVP_CIPHER_CTX_free(ctx);
    return ok;
}
['static int cipher_test_run(EVP_TEST *t)\n{\n    CIPHER_DATA *cdat = t->data;\n    int rv, frag = 0;\n    size_t out_misalign, inp_misalign;\n    if (!cdat->key) {\n        t->err = "NO_KEY";\n        return 0;\n    }\n    if (!cdat->iv && EVP_CIPHER_iv_length(cdat->cipher)) {\n        if (EVP_CIPHER_mode(cdat->cipher) != EVP_CIPH_WRAP_MODE) {\n            t->err = "NO_IV";\n            return 0;\n        }\n    }\n    if (cdat->aead && !cdat->tag) {\n        t->err = "NO_TAG";\n        return 0;\n    }\n    for (out_misalign = 0; out_misalign <= 1;) {\n        static char aux_err[64];\n        t->aux_err = aux_err;\n        for (inp_misalign = (size_t)-1; inp_misalign != 2; inp_misalign++) {\n            if (inp_misalign == (size_t)-1) {\n                BIO_snprintf(aux_err, sizeof(aux_err),\n                             "%s in-place, %sfragmented",\n                             out_misalign ? "misaligned" : "aligned",\n                             frag ? "" : "not ");\n            } else {\n                BIO_snprintf(aux_err, sizeof(aux_err),\n                             "%s output and %s input, %sfragmented",\n                             out_misalign ? "misaligned" : "aligned",\n                             inp_misalign ? "misaligned" : "aligned",\n                             frag ? "" : "not ");\n            }\n            if (cdat->enc) {\n                rv = cipher_test_enc(t, 1, out_misalign, inp_misalign, frag);\n                if (rv != 1) {\n                    if (rv < 0)\n                        return 0;\n                    return 1;\n                }\n            }\n            if (cdat->enc != 1) {\n                rv = cipher_test_enc(t, 0, out_misalign, inp_misalign, frag);\n                if (rv != 1) {\n                    if (rv < 0)\n                        return 0;\n                    return 1;\n                }\n            }\n        }\n        if (out_misalign == 1 && frag == 0) {\n            if (cdat->aead == EVP_CIPH_CCM_MODE\n                    || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_XTS_MODE\n                    || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_WRAP_MODE)\n                break;\n            out_misalign = 0;\n            frag++;\n        } else {\n            out_misalign++;\n        }\n    }\n    t->aux_err = NULL;\n    return 1;\n}', 'static int cipher_test_enc(EVP_TEST *t, int enc,\n                           size_t out_misalign, size_t inp_misalign, int frag)\n{\n    CIPHER_DATA *expected = t->data;\n    unsigned char *in, *expected_out, *tmp = NULL;\n    size_t in_len, out_len, donelen = 0;\n    int ok = 0, tmplen, chunklen, tmpflen;\n    EVP_CIPHER_CTX *ctx = NULL;\n    t->err = "TEST_FAILURE";\n    if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new()))\n        goto err;\n    EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);\n    if (enc) {\n        in = expected->plaintext;\n        in_len = expected->plaintext_len;\n        expected_out = expected->ciphertext;\n        out_len = expected->ciphertext_len;\n    } else {\n        in = expected->ciphertext;\n        in_len = expected->ciphertext_len;\n        expected_out = expected->plaintext;\n        out_len = expected->plaintext_len;\n    }\n    if (inp_misalign == (size_t)-1) {\n        tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH);\n        if (!tmp)\n            goto err;\n        in = memcpy(tmp + out_misalign, in, in_len);\n    } else {\n        inp_misalign += 16 - ((out_misalign + in_len) & 15);\n        tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +\n                             inp_misalign + in_len);\n        if (!tmp)\n            goto err;\n        in = memcpy(tmp + out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +\n                    inp_misalign, in, in_len);\n    }\n    if (!EVP_CipherInit_ex(ctx, expected->cipher, NULL, NULL, NULL, enc)) {\n        t->err = "CIPHERINIT_ERROR";\n        goto err;\n    }\n    if (expected->iv) {\n        if (expected->aead) {\n            if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,\n                                     expected->iv_len, 0)) {\n                t->err = "INVALID_IV_LENGTH";\n                goto err;\n            }\n        } else if (expected->iv_len != (size_t)EVP_CIPHER_CTX_iv_length(ctx)) {\n            t->err = "INVALID_IV_LENGTH";\n            goto err;\n        }\n    }\n    if (expected->aead) {\n        unsigned char *tag;\n        if (enc || expected->aead == EVP_CIPH_OCB_MODE) {\n            t->err = "TAG_LENGTH_SET_ERROR";\n            tag = NULL;\n        } else {\n            t->err = "TAG_SET_ERROR";\n            tag = expected->tag;\n        }\n        if (tag || expected->aead != EVP_CIPH_GCM_MODE) {\n            if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,\n                                     expected->tag_len, tag))\n                goto err;\n        }\n    }\n    if (!EVP_CIPHER_CTX_set_key_length(ctx, expected->key_len)) {\n        t->err = "INVALID_KEY_LENGTH";\n        goto err;\n    }\n    if (!EVP_CipherInit_ex(ctx, NULL, NULL, expected->key, expected->iv, -1)) {\n        t->err = "KEY_SET_ERROR";\n        goto err;\n    }\n    if (!enc && expected->aead == EVP_CIPH_OCB_MODE) {\n        if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,\n                                 expected->tag_len, expected->tag)) {\n            t->err = "TAG_SET_ERROR";\n            goto err;\n        }\n    }\n    if (expected->aead == EVP_CIPH_CCM_MODE) {\n        if (!EVP_CipherUpdate(ctx, NULL, &tmplen, NULL, out_len)) {\n            t->err = "CCM_PLAINTEXT_LENGTH_SET_ERROR";\n            goto err;\n        }\n    }\n    if (expected->aad) {\n        t->err = "AAD_SET_ERROR";\n        if (!frag) {\n            if (!EVP_CipherUpdate(ctx, NULL, &chunklen, expected->aad,\n                                  expected->aad_len))\n                goto err;\n        } else {\n            if (expected->aad_len > 0) {\n                if (!EVP_CipherUpdate(ctx, NULL, &chunklen, expected->aad, 1))\n                    goto err;\n                donelen++;\n            }\n            if (expected->aad_len > 2) {\n                if (!EVP_CipherUpdate(ctx, NULL, &chunklen,\n                                      expected->aad + donelen,\n                                      expected->aad_len - 2))\n                    goto err;\n                donelen += expected->aad_len - 2;\n            }\n            if (expected->aad_len > 1\n                    && !EVP_CipherUpdate(ctx, NULL, &chunklen,\n                                         expected->aad + donelen, 1))\n                goto err;\n        }\n    }\n    EVP_CIPHER_CTX_set_padding(ctx, 0);\n    t->err = "CIPHERUPDATE_ERROR";\n    tmplen = 0;\n    if (!frag) {\n        if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &tmplen, in, in_len))\n            goto err;\n    } else {\n        if (in_len > 0) {\n            if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &chunklen, in, 1))\n                goto err;\n            tmplen += chunklen;\n            in++;\n            in_len--;\n        }\n        if (in_len > 1) {\n            if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,\n                                  in, in_len - 1))\n                goto err;\n            tmplen += chunklen;\n            in += in_len - 1;\n            in_len = 1;\n        }\n        if (in_len > 0 ) {\n            if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,\n                                  in, 1))\n                goto err;\n            tmplen += chunklen;\n        }\n    }\n    if (!EVP_CipherFinal_ex(ctx, tmp + out_misalign + tmplen, &tmpflen)) {\n        t->err = "CIPHERFINAL_ERROR";\n        goto err;\n    }\n    if (!TEST_mem_eq(expected_out, out_len,\n                     tmp + out_misalign, tmplen + tmpflen)) {\n        t->err = "VALUE_MISMATCH";\n        goto err;\n    }\n    if (enc && expected->aead) {\n        unsigned char rtag[16];\n        if (!TEST_size_t_le(expected->tag_len, sizeof(rtag))) {\n            t->err = "TAG_LENGTH_INTERNAL_ERROR";\n            goto err;\n        }\n        if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,\n                                 expected->tag_len, rtag)) {\n            t->err = "TAG_RETRIEVE_ERROR";\n            goto err;\n        }\n        if (!TEST_mem_eq(expected->tag, expected->tag_len,\n                         rtag, expected->tag_len)) {\n            t->err = "TAG_VALUE_MISMATCH";\n            goto err;\n        }\n    }\n    t->err = NULL;\n    ok = 1;\n err:\n    OPENSSL_free(tmp);\n    EVP_CIPHER_CTX_free(ctx);\n    return ok;\n}']