提交 fa9ad414 编写于 作者: P Pauli

Remove EXECUTE_TEST_NO_TEARDOWN.

Simplify the only test that uses this macro so it doesn't need it anymore.
Clean up the formatting a little.
Reviewed-by: NAndy Polyakov <appro@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/4034)
上级 5d010e3f
...@@ -90,7 +90,7 @@ static const unsigned char vector_64[64] = { ...@@ -90,7 +90,7 @@ static const unsigned char vector_64[64] = {
{ \ { \
sizeof(vector_##len), vector_##len \ sizeof(vector_##len), vector_##len \
} }
static const SIZED_DATA cts128_vectors[] = { static const SIZED_DATA aes_cts128_vectors[] = {
CTS128_TEST_VECTOR(17), CTS128_TEST_VECTOR(17),
CTS128_TEST_VECTOR(31), CTS128_TEST_VECTOR(31),
CTS128_TEST_VECTOR(32), CTS128_TEST_VECTOR(32),
...@@ -125,35 +125,26 @@ static AES_KEY *cts128_decrypt_key_schedule() ...@@ -125,35 +125,26 @@ static AES_KEY *cts128_decrypt_key_schedule()
typedef struct { typedef struct {
const char *case_name; const char *case_name;
int num; size_t (*last_blocks_correction)(const unsigned char *in,
size_t (*transform_output)(const unsigned char *in, unsigned char *out, unsigned char *out, size_t len);
size_t len);
size_t (*encrypt_block)(const unsigned char *in, size_t (*encrypt_block)(const unsigned char *in,
unsigned char *out, size_t len, unsigned char *out, size_t len,
const void *key, unsigned char ivec[16], const void *key, unsigned char ivec[16],
block128_f block); block128_f block);
size_t (*encrypt)(const unsigned char *in, unsigned char *out, size_t (*encrypt_stream)(const unsigned char *in, unsigned char *out,
size_t len, const void *key, size_t len, const void *key,
unsigned char ivec[16], cbc128_f cbc); unsigned char ivec[16], cbc128_f cbc);
size_t (*decrypt_block)(const unsigned char *in, size_t (*decrypt_block)(const unsigned char *in,
unsigned char *out, size_t len, unsigned char *out, size_t len,
const void *key, unsigned char ivec[16], const void *key, unsigned char ivec[16],
block128_f block); block128_f block);
size_t (*decrypt)(const unsigned char *in, unsigned char *out, size_t (*decrypt_stream)(const unsigned char *in, unsigned char *out,
size_t len, const void *key, size_t len, const void *key,
unsigned char ivec[16], cbc128_f cbc); unsigned char ivec[16], cbc128_f cbc);
} CTS128_FIXTURE; } CTS128_FIXTURE;
static size_t last_blocks_correction(const unsigned char *in,
static CTS128_FIXTURE setup_cts128(const char *const test_case_name) unsigned char *out, size_t len)
{
CTS128_FIXTURE fixture;
fixture.case_name = test_case_name;
return fixture;
}
static size_t transform_output(const unsigned char *in, unsigned char *out,
size_t len)
{ {
size_t tail; size_t tail;
...@@ -165,8 +156,8 @@ static size_t transform_output(const unsigned char *in, unsigned char *out, ...@@ -165,8 +156,8 @@ static size_t transform_output(const unsigned char *in, unsigned char *out,
return tail; return tail;
} }
static size_t transform_output_nist(const unsigned char *in, unsigned char *out, static size_t last_blocks_correction_nist(const unsigned char *in,
size_t len) unsigned char *out, size_t len)
{ {
size_t tail; size_t tail;
...@@ -183,90 +174,86 @@ static size_t transform_output_nist(const unsigned char *in, unsigned char *out, ...@@ -183,90 +174,86 @@ static size_t transform_output_nist(const unsigned char *in, unsigned char *out,
return tail; return tail;
} }
static int execute_cts128(CTS128_FIXTURE fixture) static int execute_cts128(const CTS128_FIXTURE *fixture, int num)
{ {
const unsigned char *test_iv = cts128_test_iv; const unsigned char *test_iv = cts128_test_iv;
size_t test_iv_len = sizeof(cts128_test_iv); size_t test_iv_len = sizeof(cts128_test_iv);
const unsigned char *orig_vector = cts128_vectors[fixture.num].data; const unsigned char *orig_vector = aes_cts128_vectors[num].data;
size_t len = cts128_vectors[fixture.num].size; size_t len = aes_cts128_vectors[num].size;
const unsigned char *test_input = cts128_test_input; const unsigned char *test_input = cts128_test_input;
const AES_KEY *encrypt_key_schedule = cts128_encrypt_key_schedule(); const AES_KEY *encrypt_key_schedule = cts128_encrypt_key_schedule();
const AES_KEY *decrypt_key_schedule = cts128_decrypt_key_schedule(); const AES_KEY *decrypt_key_schedule = cts128_decrypt_key_schedule();
unsigned char iv[16]; unsigned char iv[16];
/* The largest test inputs are = 64 bytes. */ /* The largest test inputs are = 64 bytes. */
unsigned char cleartext[64], ciphertext[64], vector[64]; unsigned char cleartext[64], ciphertext[64], vector[64];
size_t tail; size_t tail, size;
TEST_info("%s_vector_%lu", fixture.case_name, (unsigned long)len); TEST_info("%s_vector_%lu", fixture->case_name, (unsigned long)len);
tail = fixture.transform_output(orig_vector, vector, len); tail = fixture->last_blocks_correction(orig_vector, vector, len);
/* test block-based encryption */ /* test block-based encryption */
memcpy(iv, test_iv, test_iv_len); memcpy(iv, test_iv, test_iv_len);
fixture.encrypt_block(test_input, ciphertext, len, if (!TEST_size_t_eq(fixture->encrypt_block(test_input, ciphertext, len,
encrypt_key_schedule, iv, encrypt_key_schedule, iv,
(block128_f)AES_encrypt); (block128_f)AES_encrypt), len)
if (!TEST_mem_eq(ciphertext, len, vector, len)) || !TEST_mem_eq(ciphertext, len, vector, len)
return 0; || !TEST_mem_eq(iv, sizeof(iv), vector + len - tail, sizeof(iv)))
if (!TEST_mem_eq(iv, sizeof(iv), vector + len - tail, sizeof(iv)))
return 0; return 0;
/* test block-based decryption */ /* test block-based decryption */
memcpy(iv, test_iv, test_iv_len); memcpy(iv, test_iv, test_iv_len);
fixture.decrypt_block(ciphertext, cleartext, len, size = fixture->decrypt_block(ciphertext, cleartext, len,
decrypt_key_schedule, iv, decrypt_key_schedule, iv,
(block128_f)AES_decrypt); (block128_f)AES_decrypt);
if (!TEST_mem_eq(cleartext, len, test_input, len)) if (!TEST_true(len == size || len + 16 == size)
return 0; || !TEST_mem_eq(cleartext, len, test_input, len)
if (!TEST_mem_eq(iv, sizeof(iv), vector + len - tail, sizeof(iv))) || !TEST_mem_eq(iv, sizeof(iv), vector + len - tail, sizeof(iv)))
return 0; return 0;
/* test streamed encryption */ /* test streamed encryption */
memcpy(iv, test_iv, test_iv_len); memcpy(iv, test_iv, test_iv_len);
fixture.encrypt(test_input, ciphertext, len, encrypt_key_schedule, if (!TEST_size_t_eq(fixture->encrypt_stream(test_input, ciphertext, len,
iv, (cbc128_f) AES_cbc_encrypt); encrypt_key_schedule, iv,
if (!TEST_mem_eq(ciphertext, len, vector, len)) (cbc128_f) AES_cbc_encrypt),
return 0; len)
if (!TEST_mem_eq(iv, sizeof(iv), vector + len - tail, sizeof(iv))) || !TEST_mem_eq(ciphertext, len, vector, len)
|| !TEST_mem_eq(iv, sizeof(iv), vector + len - tail, sizeof(iv)))
return 0; return 0;
/* test streamed decryption */ /* test streamed decryption */
memcpy(iv, test_iv, test_iv_len); memcpy(iv, test_iv, test_iv_len);
fixture.decrypt(ciphertext, cleartext, len, decrypt_key_schedule, iv, if (!TEST_size_t_eq(fixture->decrypt_stream(ciphertext, cleartext, len,
(cbc128_f)AES_cbc_encrypt); decrypt_key_schedule, iv,
if (!TEST_mem_eq(cleartext, len, test_input, len)) (cbc128_f)AES_cbc_encrypt),
return 0; len)
if (!TEST_mem_eq(iv, sizeof(iv), vector + len - tail, sizeof(iv))) || !TEST_mem_eq(cleartext, len, test_input, len)
|| !TEST_mem_eq(iv, sizeof(iv), vector + len - tail, sizeof(iv)))
return 0; return 0;
return 1; return 1;
} }
static int test_cts128(int idx) static int test_aes_cts128(int idx)
{ {
SETUP_TEST_FIXTURE(CTS128_FIXTURE, setup_cts128); static const CTS128_FIXTURE fixture_cts128 = {
fixture.transform_output = transform_output; "aes_cts128", last_blocks_correction,
fixture.encrypt_block = CRYPTO_cts128_encrypt_block; CRYPTO_cts128_encrypt_block, CRYPTO_cts128_encrypt,
fixture.encrypt = CRYPTO_cts128_encrypt; CRYPTO_cts128_decrypt_block, CRYPTO_cts128_decrypt
fixture.decrypt_block = CRYPTO_cts128_decrypt_block; };
fixture.decrypt = CRYPTO_cts128_decrypt;
fixture.case_name = "cts128"; return execute_cts128(&fixture_cts128, idx);
fixture.num = idx;
EXECUTE_TEST_NO_TEARDOWN(execute_cts128);
} }
static int test_cts128_nist(int idx) static int test_aes_cts128_nist(int idx)
{ {
SETUP_TEST_FIXTURE(CTS128_FIXTURE, setup_cts128); static const CTS128_FIXTURE fixture_cts128_nist = {
fixture.transform_output = transform_output_nist; "aes_cts128_nist", last_blocks_correction_nist,
fixture.encrypt_block = CRYPTO_nistcts128_encrypt_block; CRYPTO_nistcts128_encrypt_block, CRYPTO_nistcts128_encrypt,
fixture.encrypt = CRYPTO_nistcts128_encrypt; CRYPTO_nistcts128_decrypt_block, CRYPTO_nistcts128_decrypt
fixture.decrypt_block = CRYPTO_nistcts128_decrypt_block; };
fixture.decrypt = CRYPTO_nistcts128_decrypt;
fixture.case_name = "cts128_nist"; return execute_cts128(&fixture_cts128_nist, idx);
fixture.num = idx;
EXECUTE_TEST_NO_TEARDOWN(execute_cts128);
} }
/* /*
...@@ -957,8 +944,8 @@ int setup_tests(void) ...@@ -957,8 +944,8 @@ int setup_tests(void)
return 1; return 1;
} }
ADD_ALL_TESTS(test_cts128, OSSL_NELEM(cts128_vectors)); ADD_ALL_TESTS(test_aes_cts128, OSSL_NELEM(aes_cts128_vectors));
ADD_ALL_TESTS(test_cts128_nist, OSSL_NELEM(cts128_vectors)); ADD_ALL_TESTS(test_aes_cts128_nist, OSSL_NELEM(aes_cts128_vectors));
ADD_ALL_TESTS(test_gcm128, OSSL_NELEM(gcm128_vectors)); ADD_ALL_TESTS(test_gcm128, OSSL_NELEM(gcm128_vectors));
return 1; return 1;
} }
......
...@@ -99,11 +99,6 @@ ...@@ -99,11 +99,6 @@
tear_down(fixture);\ tear_down(fixture);\
return result return result
/* Shorthand if tear_down does nothing. */
# define EXECUTE_TEST_NO_TEARDOWN(execute_func)\
result = execute_func(fixture);\
return result
/* /*
* TEST_CASE_NAME is defined as the name of the test case function where * TEST_CASE_NAME is defined as the name of the test case function where
* possible; otherwise we get by with the file name and line number. * possible; otherwise we get by with the file name and line number.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册