summaryrefslogtreecommitdiffstats
path: root/test/evp_extra_test.c
diff options
context:
space:
mode:
authorMatt Caswell <matt@openssl.org>2022-09-29 12:10:30 +0100
committerMatt Caswell <matt@openssl.org>2022-10-03 15:01:12 +0100
commit8c7d847e2e6ac6bfded210c19fd8461254bb2be3 (patch)
treeffbd6a30a3476e1cd4844008cd55f95612858e27 /test/evp_extra_test.c
parentf817a7439eaa705429cf699dd0485e665b0ffc49 (diff)
Test usage of a custom EVP_CIPHER
Test that a custom EVP_CIPHER gets used in EVP_CipherInit_ex() calls. Reviewed-by: Tomas Mraz <tomas@openssl.org> Reviewed-by: Paul Dale <pauli@openssl.org> (Merged from https://github.com/openssl/openssl/pull/19300)
Diffstat (limited to 'test/evp_extra_test.c')
-rw-r--r--test/evp_extra_test.c87
1 files changed, 85 insertions, 2 deletions
diff --git a/test/evp_extra_test.c b/test/evp_extra_test.c
index 9e7a41d0ac..ad4d28ee69 100644
--- a/test/evp_extra_test.c
+++ b/test/evp_extra_test.c
@@ -4290,7 +4290,7 @@ static int test_custom_md_meth(void)
* library context in this test.
*/
if (testctx != NULL)
- return 1;
+ return TEST_skip("Non-default libctx");
custom_md_init_called = custom_md_cleanup_called = 0;
@@ -4312,7 +4312,7 @@ static int test_custom_md_meth(void)
/*
* Initing our custom md and then initing another md should
* result in the init and cleanup functions of the custom md
- * from being called.
+ * being called.
*/
|| !TEST_true(EVP_DigestInit_ex(mdctx, tmp, NULL))
|| !TEST_true(EVP_DigestInit_ex(mdctx, EVP_sha256(), NULL))
@@ -4329,6 +4329,88 @@ static int test_custom_md_meth(void)
return testresult;
}
+typedef struct {
+ int data;
+} custom_ciph_ctx;
+
+static int custom_ciph_init_called = 0;
+static int custom_ciph_cleanup_called = 0;
+
+static int custom_ciph_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
+ const unsigned char *iv, int enc)
+{
+ custom_ciph_ctx *p = EVP_CIPHER_CTX_get_cipher_data(ctx);
+
+ if (p == NULL)
+ return 0;
+
+ custom_ciph_init_called++;
+ return 1;
+}
+
+static int custom_ciph_cleanup(EVP_CIPHER_CTX *ctx)
+{
+ custom_ciph_ctx *p = EVP_CIPHER_CTX_get_cipher_data(ctx);
+
+ if (p == NULL)
+ /* Nothing to do */
+ return 1;
+
+ custom_ciph_cleanup_called++;
+ return 1;
+}
+
+static int test_custom_ciph_meth(void)
+{
+ EVP_CIPHER_CTX *ciphctx = NULL;
+ EVP_CIPHER *tmp = NULL;
+ int testresult = 0;
+ int nid;
+
+ /*
+ * We are testing deprecated functions. We don't support a non-default
+ * library context in this test.
+ */
+ if (testctx != NULL)
+ return TEST_skip("Non-default libctx");
+
+ custom_ciph_init_called = custom_ciph_cleanup_called = 0;
+
+ nid = OBJ_create("1.3.6.1.4.1.16604.998866.2", "custom-ciph", "custom-ciph");
+ if (!TEST_int_ne(nid, NID_undef))
+ goto err;
+ tmp = EVP_CIPHER_meth_new(nid, 16, 16);
+ if (!TEST_ptr(tmp))
+ goto err;
+
+ if (!TEST_true(EVP_CIPHER_meth_set_init(tmp, custom_ciph_init))
+ || !TEST_true(EVP_CIPHER_meth_set_flags(tmp, EVP_CIPH_ALWAYS_CALL_INIT))
+ || !TEST_true(EVP_CIPHER_meth_set_cleanup(tmp, custom_ciph_cleanup))
+ || !TEST_true(EVP_CIPHER_meth_set_impl_ctx_size(tmp,
+ sizeof(custom_ciph_ctx))))
+ goto err;
+
+ ciphctx = EVP_CIPHER_CTX_new();
+ if (!TEST_ptr(ciphctx)
+ /*
+ * Initing our custom cipher and then initing another cipher
+ * should result in the init and cleanup functions of the custom
+ * cipher being called.
+ */
+ || !TEST_true(EVP_CipherInit_ex(ciphctx, tmp, NULL, NULL, NULL, 1))
+ || !TEST_true(EVP_CipherInit_ex(ciphctx, EVP_aes_128_cbc(), NULL,
+ NULL, NULL, 1))
+ || !TEST_int_eq(custom_ciph_init_called, 1)
+ || !TEST_int_eq(custom_ciph_cleanup_called, 1))
+ goto err;
+
+ testresult = 1;
+ err:
+ EVP_CIPHER_CTX_free(ciphctx);
+ EVP_CIPHER_meth_free(tmp);
+ return testresult;
+}
+
# ifndef OPENSSL_NO_DYNAMIC_ENGINE
/* Test we can create a signature keys with an associated ENGINE */
static int test_signatures_with_engine(int tst)
@@ -4627,6 +4709,7 @@ int setup_tests(void)
ADD_ALL_TESTS(test_custom_pmeth, 12);
ADD_TEST(test_evp_md_cipher_meth);
ADD_TEST(test_custom_md_meth);
+ ADD_TEST(test_custom_ciph_meth);
# ifndef OPENSSL_NO_DYNAMIC_ENGINE
/* Tests only support the default libctx */