From 9a2d2fb338d67d3001aebcb2aa8e11d0fd7d2f80 Mon Sep 17 00:00:00 2001 From: Andy Polyakov Date: Sat, 23 Apr 2016 19:14:21 +0200 Subject: [PATCH] test/evp_test.c: exercise different combinations of data misalignment. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Reviewed-by: Emilia Käsper --- test/evp_test.c | 79 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 55 insertions(+), 24 deletions(-) diff --git a/test/evp_test.c b/test/evp_test.c index 56c821bd9e..f8506d467d 100644 --- a/test/evp_test.c +++ b/test/evp_test.c @@ -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) { - fprintf(stderr, "Test line %d: unexpected error %s\n", - t->start_line, t->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,24 +996,35 @@ static int cipher_test_run(struct evp_test *t) t->err = "NO_TAG"; return 0; } - if (cdat->enc) { - rv = cipher_test_enc(t, 1); - /* Not fatal errors: return */ - if (rv != 1) { - if (rv < 0) - return 0; - return 1; - } - } - if (cdat->enc != 1) { - rv = cipher_test_enc(t, 0); - /* Not fatal errors: return */ - if (rv != 1) { - if (rv < 0) - return 0; - return 1; + 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, out_misalign, inp_misalign); + /* Not fatal errors: return */ + if (rv != 1) { + if (rv < 0) + return 0; + return 1; + } + } + if (cdat->enc != 1) { + rv = cipher_test_enc(t, 0, out_misalign, inp_misalign); + /* Not fatal errors: return */ + if (rv != 1) { + if (rv < 0) + return 0; + return 1; + } + } } } + t->aux_err = NULL; + return 1; } -- 2.25.1