test/evp_test.c: exercise different combinations of data misalignment.

Reviewed-by: Emilia Käsper <emilia@openssl.org>
This commit is contained in:
Andy Polyakov 2016-04-23 19:14:21 +02:00
parent c0aa8c2748
commit 9a2d2fb338

View file

@ -251,7 +251,7 @@ struct evp_test {
/* start line of current test */
unsigned int start_line;
/* Error string for test */
const char *err;
const char *err, *aux_err;
/* Expected error value of test */
char *expected_err;
/* Number of tests */
@ -364,8 +364,13 @@ static int check_test_error(struct evp_test *t)
if (!t->err && !t->expected_err)
return 1;
if (t->err && !t->expected_err) {
if (t->aux_err != NULL) {
fprintf(stderr, "Test line %d(%s): unexpected error %s\n",
t->start_line, t->aux_err, t->err);
} else {
fprintf(stderr, "Test line %d: unexpected error %s\n",
t->start_line, t->err);
}
print_expected(t);
return 0;
}
@ -830,7 +835,8 @@ static int cipher_test_parse(struct evp_test *t, const char *keyword,
return 0;
}
static int cipher_test_enc(struct evp_test *t, int enc)
static int cipher_test_enc(struct evp_test *t, int enc,
size_t out_misalign, size_t inp_misalign)
{
struct cipher_data *cdat = t->data;
unsigned char *in, *out, *tmp = NULL;
@ -854,9 +860,21 @@ static int cipher_test_enc(struct evp_test *t, int enc)
out = cdat->plaintext;
out_len = cdat->plaintext_len;
}
tmp = OPENSSL_malloc(in_len + 2 * EVP_MAX_BLOCK_LENGTH);
inp_misalign += 16 - ((out_misalign + in_len) & 15);
/*
* 'tmp' will store both output and copy of input. We make the copy
* of input to specifically aligned part of 'tmp'. So we just
* figured out how much padding would ensure the required alignment,
* 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";
if (!EVP_CipherInit_ex(ctx, cdat->cipher, NULL, NULL, NULL, enc))
goto err;
@ -918,20 +936,20 @@ static int cipher_test_enc(struct evp_test *t, int enc)
}
EVP_CIPHER_CTX_set_padding(ctx, 0);
err = "CIPHERUPDATE_ERROR";
if (!EVP_CipherUpdate(ctx, tmp, &tmplen, in, in_len))
if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &tmplen, in, in_len))
goto err;
if (cdat->aead == EVP_CIPH_CCM_MODE)
tmpflen = 0;
else {
err = "CIPHERFINAL_ERROR";
if (!EVP_CipherFinal_ex(ctx, tmp + tmplen, &tmpflen))
if (!EVP_CipherFinal_ex(ctx, tmp + out_misalign + tmplen, &tmpflen))
goto err;
}
err = "LENGTH_MISMATCH";
if (out_len != (size_t)(tmplen + tmpflen))
goto err;
err = "VALUE_MISMATCH";
if (check_output(t, out, tmp, out_len))
if (check_output(t, out, tmp + out_misalign, out_len))
goto err;
if (enc && cdat->aead) {
unsigned char rtag[16];
@ -961,6 +979,8 @@ static int cipher_test_run(struct evp_test *t)
{
struct cipher_data *cdat = t->data;
int rv;
size_t out_misalign, inp_misalign;
if (!cdat->key) {
t->err = "NO_KEY";
return 0;
@ -976,8 +996,15 @@ static int cipher_test_run(struct evp_test *t)
t->err = "NO_TAG";
return 0;
}
for (out_misalign = 0; out_misalign <= 1; out_misalign++) {
static char aux_err[64];
t->aux_err = aux_err;
for (inp_misalign = 0; inp_misalign <= 1; inp_misalign++) {
BIO_snprintf(aux_err, sizeof(aux_err), "%s output and %s input",
out_misalign ? "misaligned" : "aligned",
inp_misalign ? "misaligned" : "aligned");
if (cdat->enc) {
rv = cipher_test_enc(t, 1);
rv = cipher_test_enc(t, 1, out_misalign, inp_misalign);
/* Not fatal errors: return */
if (rv != 1) {
if (rv < 0)
@ -986,7 +1013,7 @@ static int cipher_test_run(struct evp_test *t)
}
}
if (cdat->enc != 1) {
rv = cipher_test_enc(t, 0);
rv = cipher_test_enc(t, 0, out_misalign, inp_misalign);
/* Not fatal errors: return */
if (rv != 1) {
if (rv < 0)
@ -994,6 +1021,10 @@ static int cipher_test_run(struct evp_test *t)
return 1;
}
}
}
}
t->aux_err = NULL;
return 1;
}