test/evp_test.c: exercise in-place encryption.

Reviewed-by: Emilia Käsper <emilia@openssl.org>
This commit is contained in:
Andy Polyakov 2016-05-09 14:18:14 +02:00
parent 6133b4edbd
commit ff715da492

View file

@ -860,21 +860,31 @@ static int cipher_test_enc(struct evp_test *t, int enc,
out = cdat->plaintext; out = cdat->plaintext;
out_len = cdat->plaintext_len; out_len = cdat->plaintext_len;
} }
inp_misalign += 16 - ((out_misalign + in_len) & 15); if (inp_misalign == (size_t)-1) {
/* /*
* 'tmp' will store both output and copy of input. We make the copy * Exercise in-place encryption
* of input to specifically aligned part of 'tmp'. So we just */
* figured out how much padding would ensure the required alignment, tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH);
* now we allocate extended buffer and finally copy the input just if (!tmp)
* past inp_misalign in expression below. Output will be written goto err;
* past out_misalign... in = memcpy(tmp + out_misalign, in, in_len);
*/ } else {
tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH + inp_misalign += 16 - ((out_misalign + in_len) & 15);
inp_misalign + in_len); /*
if (!tmp) * 'tmp' will store both output and copy of input. We make the copy
goto err; * of input to specifically aligned part of 'tmp'. So we just
in = memcpy(tmp + out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH + * figured out how much padding would ensure the required alignment,
inp_misalign, in, in_len); * now we allocate extended buffer and finally copy the input just
* past inp_misalign in expression below. Output will be written
* past out_misalign...
*/
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);
}
err = "CIPHERINIT_ERROR"; err = "CIPHERINIT_ERROR";
if (!EVP_CipherInit_ex(ctx, cdat->cipher, NULL, NULL, NULL, enc)) if (!EVP_CipherInit_ex(ctx, cdat->cipher, NULL, NULL, NULL, enc))
goto err; goto err;
@ -999,10 +1009,16 @@ static int cipher_test_run(struct evp_test *t)
for (out_misalign = 0; out_misalign <= 1; out_misalign++) { for (out_misalign = 0; out_misalign <= 1; out_misalign++) {
static char aux_err[64]; static char aux_err[64];
t->aux_err = aux_err; t->aux_err = aux_err;
for (inp_misalign = 0; inp_misalign <= 1; inp_misalign++) { for (inp_misalign = (size_t)-1; inp_misalign != 2; inp_misalign++) {
BIO_snprintf(aux_err, sizeof(aux_err), "%s output and %s input", if (inp_misalign == (size_t)-1) {
out_misalign ? "misaligned" : "aligned", /* kludge: inp_misalign == -1 means "exercise in-place" */
inp_misalign ? "misaligned" : "aligned"); BIO_snprintf(aux_err, sizeof(aux_err), "%s in-place",
out_misalign ? "misaligned" : "aligned");
} else {
BIO_snprintf(aux_err, sizeof(aux_err), "%s output and %s input",
out_misalign ? "misaligned" : "aligned",
inp_misalign ? "misaligned" : "aligned");
}
if (cdat->enc) { if (cdat->enc) {
rv = cipher_test_enc(t, 1, out_misalign, inp_misalign); rv = cipher_test_enc(t, 1, out_misalign, inp_misalign);
/* Not fatal errors: return */ /* Not fatal errors: return */