Commit 9e9aae5eab for openssl.org

commit 9e9aae5eabec596d82b8ae7dbb1d9997e5c406a0
Author: Daniel Kubec <kubec@openssl.foundation>
Date:   Thu Nov 19 03:09:18 2026 +0000

    EVP: EVP_MAC_CTX_set_params() with a key initialises the MAC context

    Commit 2f2950e ("EVP: Enforce the whole MAC life-cycle at the EVP
    layer") makes EVP_MAC_update() and EVP_MAC_final() fail
    with EVP_R_OPERATION_NOT_INITIALIZED on any context that was
    not initialised via EVP_MAC_init() / EVP_MAC_init_SKEY().

    Since the MAC life-cycle is enforced at the EVP layer, a context keyed
    only through EVP_MAC_CTX_set_params() was rejected by EVP_MAC_update()
    and EVP_MAC_final().  That broke the documented way of setting up
    the HMAC in a SSL_CTX_set_tlsext_ticket_key_evp_cb() callback.

    When the parameters contain OSSL_MAC_PARAM_KEY, the call is now
    equivalent to EVP_MAC_init() with a NULL key and the same parameters.
    It goes through the implementation's init function rather than only
    updating the state, because KMAC and BLAKE2 merely store a key given
    as a parameter and set up their working state in init.

    Resolves: https://github.com/openssl/openssl/issues/33790
    Fixes: 2f2950ef2cc8 "EVP: Enforce the whole MAC life-cycle at the EVP layer"
    References: a76ce2862bc6 "TLS: use EVP for HMAC throughout libssl."
    Assisted-by: Claude:claude-fable-5-2
    Co-authored-by: Paul Dale <paul.dale@oracle.com>
    Reviewed-by: Paul Dale <paul.dale@oracle.com>
    Reviewed-by: Viktor Dukhovni <viktor@openssl.org>
    Reviewed-by: Jakub Zelenka <jakub.zelenka@openssl.foundation>
    Reviewed-by: Eugene Syromiatnikov <esyr@openssl.org>
    Merge-date: Sun Nov 22 04:03:02 2026
    Merged-from: https://github.com/openssl/openssl/pull/33804

diff --git a/CHANGES.md b/CHANGES.md
index c4309cdb36..3484cd1a5b 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -104,10 +104,12 @@ OpenSSL 4.2
    life_cycle-mac(7).  `EVP_MAC_update()`, `EVP_MAC_final()` and
    `EVP_MAC_finalXOF()` fail with an error when called on an `EVP_MAC_CTX`
    that has not been initialised with `EVP_MAC_init()` or
-   `EVP_MAC_init_SKEY()`, including one whose key was only supplied through
-   `EVP_MAC_CTX_set_params()`, or that has already been finalised and not
-   been re-initialised since.  A failed `EVP_MAC_init()` or
-   `EVP_MAC_init_SKEY()` leaves the context uninitialised.
+   `EVP_MAC_init_SKEY()`, or that has already been finalised and not been
+   re-initialised since.  A failed `EVP_MAC_init()` or `EVP_MAC_init_SKEY()`
+   leaves the context uninitialised.  Supplying the key through
+   `EVP_MAC_CTX_set_params()` initialises the context too: with the
+   `OSSL_MAC_PARAM_KEY` parameter it is equivalent to `EVP_MAC_init()` with
+   a NULL key and the same parameters, for every MAC implementation.
    <!-- https://github.com/openssl/openssl/pull/33084-->

    *Daniel Kubec*
diff --git a/crypto/evp/evp_local.h b/crypto/evp/evp_local.h
index 346bcacec2..cd8c0f9c5c 100644
--- a/crypto/evp/evp_local.h
+++ b/crypto/evp/evp_local.h
@@ -80,8 +80,10 @@ struct evp_mac_ctx_st {
      * at EVP_MAC_STATE_NEW (zero), a successful EVP_MAC_init() or
      * EVP_MAC_init_SKEY() moves to INITIALISED, a failed one back to NEW,
      * EVP_MAC_update() to UPDATED and EVP_MAC_final()/EVP_MAC_finalXOF() to
-     * FINALISED.  EVP_MAC_update(), EVP_MAC_final() and EVP_MAC_finalXOF()
-     * are rejected in the NEW and FINALISED states.
+     * FINALISED.  EVP_MAC_CTX_set_params() counts as EVP_MAC_init() when it
+     * is given a key.  EVP_MAC_update(),
+     * EVP_MAC_final() and EVP_MAC_finalXOF() are rejected in the NEW and
+     * FINALISED states.
      */
     enum {
         EVP_MAC_STATE_NEW = 0,
diff --git a/crypto/evp/mac_lib.c b/crypto/evp/mac_lib.c
index 1b19538e3b..ee2b7a034f 100644
--- a/crypto/evp/mac_lib.c
+++ b/crypto/evp/mac_lib.c
@@ -251,6 +251,17 @@ int EVP_MAC_CTX_get_params(EVP_MAC_CTX *ctx, OSSL_PARAM params[])

 int EVP_MAC_CTX_set_params(EVP_MAC_CTX *ctx, const OSSL_PARAM params[])
 {
+    /*
+     * Supplying a key starts the MAC operation, as documented for the "key"
+     * parameter: it is the same as EVP_MAC_init() with a NULL key and these
+     * parameters.  Going through the implementation's init function, rather
+     * than just recording the new state here, matters for the implementations
+     * that only store a key given as a parameter and set up their working
+     * state from it in init (KMAC, BLAKE2).
+     */
+    if (ctx->meth->init != NULL
+        && OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY) != NULL)
+        return EVP_MAC_init(ctx, NULL, 0, params);
     if (ctx->meth->set_ctx_params != NULL)
         return ctx->meth->set_ctx_params(ctx->algctx, params);
     return 1;
diff --git a/doc/life-cycles/mac.dot b/doc/life-cycles/mac.dot
index fe277f8328..bc10d5195e 100644
--- a/doc/life-cycles/mac.dot
+++ b/doc/life-cycles/mac.dot
@@ -9,7 +9,7 @@ digraph mac {
     end [label=freed, color="#deeaee", style="filled"];

     begin -> newed [label="EVP_MAC_CTX_new"];
-    newed -> initialised [label="EVP_MAC_init"];
+    newed -> initialised [label="EVP_MAC_init\nEVP_MAC_CTX_set_params (key)"];
     initialised -> updated [label="EVP_MAC_update"];
     updated -> updated [label="EVP_MAC_update"];
     updated -> finaled [label="EVP_MAC_final"];
@@ -20,9 +20,9 @@ digraph mac {
                         fontcolor="#808080", color="#808080"];
     */
     finaled -> end [label="EVP_MAC_CTX_free"];
-    updated -> initialised [label="EVP_MAC_init", style=dashed,
-                            color="#034f84", fontcolor="#034f84"];
-    finaled -> initialised [label="EVP_MAC_init", style=dashed,
-                            color="#034f84", fontcolor="#034f84"];
+    updated -> initialised [label="EVP_MAC_init\nEVP_MAC_CTX_set_params (key)",
+                            style=dashed, color="#034f84", fontcolor="#034f84"];
+    finaled -> initialised [label="EVP_MAC_init\nEVP_MAC_CTX_set_params (key)",
+                            style=dashed, color="#034f84", fontcolor="#034f84"];
 }

diff --git a/doc/man3/EVP_MAC.pod b/doc/man3/EVP_MAC.pod
index 2a2ffb33b8..e428b569e3 100644
--- a/doc/man3/EVP_MAC.pod
+++ b/doc/man3/EVP_MAC.pod
@@ -150,7 +150,8 @@ Note: There are additional requirements for some MAC algorithms during
 re-initialization (i.e. calling EVP_MAC_init() on an EVP_MAC after EVP_MAC_final()
 has been called on the same object).  See the NOTES section below.

-EVP_MAC_init() must be called before EVP_MAC_update() and EVP_MAC_final().
+EVP_MAC_init() must be called before EVP_MAC_update() and EVP_MAC_final(),
+unless the key is supplied with EVP_MAC_CTX_set_params(), see below.

 EVP_MAC_init_SKEY() is similar to EVP_MAC_init() but it accepts an opaque
 B<EVP_SKEY> object as a key.
@@ -171,15 +172,15 @@ EVP_MAC_finalXOF() does the final computation for an XOF based MAC and stores
 the result in the memory pointed at by I<out> of size I<outsize>.

 EVP_MAC_update(), EVP_MAC_final() and EVP_MAC_finalXOF() require I<ctx> to
-have been initialised with EVP_MAC_init() or EVP_MAC_init_SKEY() and fail
-otherwise.  Once EVP_MAC_final() or EVP_MAC_finalXOF() has been called,
-I<ctx> is finalised and these three functions fail again until it is
-re-initialised.  A failed EVP_MAC_init() or EVP_MAC_init_SKEY() leaves
-I<ctx> uninitialised, whatever its state was before, as the implementation
-may have been partially re-initialised.  Calling EVP_MAC_final() with
-I<out> set to NULL to query the output length is possible in any state, and
-a copy made with EVP_MAC_CTX_dup() is in the same state as the original.
-Supplying a key with EVP_MAC_CTX_set_params() does not initialise I<ctx>.
+have been initialised with EVP_MAC_init() or EVP_MAC_init_SKEY(), or by
+supplying a key with EVP_MAC_CTX_set_params(), and fail otherwise.  Once
+EVP_MAC_final() or EVP_MAC_finalXOF() has been called, I<ctx> is finalised
+and these three functions fail again until it is re-initialised.  A failed
+EVP_MAC_init() or EVP_MAC_init_SKEY() leaves I<ctx> uninitialised, whatever
+its state was before, as the implementation may have been partially
+re-initialised.  Calling EVP_MAC_final() with I<out> set to NULL to query the
+output length is possible in any state, and a copy made with
+EVP_MAC_CTX_dup() is in the same state as the original.

 EVP_MAC_get_params() retrieves details about the implementation
 I<mac>.
@@ -204,6 +205,11 @@ Note that a parameter that is unknown in the underlying context is
 simply ignored.
 Also, what happens when a needed parameter isn't passed down is
 defined by the implementation.
+If I<params> contain the B<OSSL_MAC_PARAM_KEY> ("key") parameter, the call
+is equivalent to calling EVP_MAC_init() with a NULL I<key> and those
+I<params>: I<ctx> is initialised, or re-initialised with any input supplied
+so far being discarded, and a failure leaves I<ctx> uninitialised.
+Parameters without a key never change whether I<ctx> is initialised.

 EVP_MAC_gettable_params() returns an L<OSSL_PARAM(3)> array that describes
 the retrievable and settable parameters.  EVP_MAC_gettable_params()
@@ -268,9 +274,12 @@ The standard parameter names are:
 =item "key" (B<OSSL_MAC_PARAM_KEY>) <octet string>

 Its value is the MAC key as an array of bytes.
+Setting it with EVP_MAC_CTX_set_params() initialises the context, in the
+same way as passing the key to EVP_MAC_init() does.

 For MACs that use an underlying computation algorithm, the algorithm
-must be set first, see "cipher" and "digest" parameters below.
+must be set first, or along with the key, see "cipher" and "digest"
+parameters below.

 =item "iv" (B<OSSL_MAC_PARAM_IV>) <octet string>

@@ -510,6 +519,9 @@ The EVP_MAC_init_SKEY() function was added in OpenSSL 3.5.
 Since OpenSSL 4.2, EVP_MAC_update(), EVP_MAC_final() and EVP_MAC_finalXOF()
 fail on a context that has not been initialised, or that has been finalised
 and not re-initialised since. Previously results of such calls were undefined.
+Whether a key supplied with EVP_MAC_CTX_set_params() left the context usable
+without a call to EVP_MAC_init() was up to the implementation before
+OpenSSL 4.2; it now always initialises the context.

 =head1 COPYRIGHT

diff --git a/doc/man7/img/mac.png b/doc/man7/img/mac.png
index d3978767c9..56bcf8b7c3 100644
Binary files a/doc/man7/img/mac.png and b/doc/man7/img/mac.png differ
diff --git a/doc/man7/life_cycle-mac.pod b/doc/man7/life_cycle-mac.pod
index 6cdf7519ce..6b77054340 100644
--- a/doc/man7/life_cycle-mac.pod
+++ b/doc/man7/life_cycle-mac.pod
@@ -49,39 +49,40 @@ The usual life-cycle of a MAC is illustrated:

 =begin man

-                 +-------------------+
-                 |       start       |
-                 +-------------------+
-                   |
-                   | EVP_MAC_CTX_new
-                   v
-                 +-------------------+
-                 |       newed       |
-                 +-------------------+
-                   |
-                   | EVP_MAC_init
-                   v
-                 +-------------------+
-              +> |    initialised    | <+
-              |  +-------------------+  |
-              |    |                    |
-              |    | EVP_MAC_update     | EVP_MAC_init
-              |    v                    |
- EVP_MAC_init |  +-------------------+  |
-              |  |      updated      | -+
-              |  +-------------------+
-              |    |               |
-              |    | EVP_MAC_final | EVP_MAC_finalXOF
-              |    v               v
-              |  +-------------------+
-              +- |      finaled      |
-                 +-------------------+
-                   |
-                   | EVP_MAC_CTX_free
-                   v
-                 +-------------------+
-                 |       freed       |
-                 +-------------------+
+                                 +-------------------+
+                                 |       start       |
+                                 +-------------------+
+                                   |
+                                   | EVP_MAC_CTX_new
+                                   v
+                                 +-------------------+
+                                 |       newed       |
+                                 +-------------------+
+                                   |
+                                   | EVP_MAC_init
+                                   | EVP_MAC_CTX_set_params (key)
+                                   v
+                                 +-------------------+
+                              +> |    initialised    | <+
+                              |  +-------------------+  |
+                              |    |                    |
+                              |    | EVP_MAC_update     | EVP_MAC_init
+                              |    v                    | EVP_MAC_CTX_set_params (key)
+ EVP_MAC_init                 |  +-------------------+  |
+ EVP_MAC_CTX_set_params (key) |  |      updated      | -+
+                              |  +-------------------+
+                              |    |               |
+                              |    | EVP_MAC_final | EVP_MAC_finalXOF
+                              |    v               v
+                              |  +-------------------+
+                              +- |      finaled      |
+                                 +-------------------+
+                                   |
+                                   | EVP_MAC_CTX_free
+                                   v
+                                 +-------------------+
+                                 |       freed       |
+                                 +-------------------+

 =end man

@@ -106,6 +107,7 @@ This is the canonical list.
  EVP_MAC_CTX_free                freed   freed       freed         freed       freed
  EVP_MAC_CTX_get_params                  newed       initialised   updated     finaled
  EVP_MAC_CTX_set_params                  newed       initialised   updated     finaled
+ EVP_MAC_CTX_set_params (key)            initialised initialised   initialised initialised
  EVP_MAC_CTX_gettable_params             newed       initialised   updated     finaled
  EVP_MAC_CTX_settable_params             newed       initialised   updated     finaled

@@ -193,6 +195,13 @@ This is the canonical list.
     <td style="border:1px solid" align="center">updated</td>
     <td style="border:1px solid" align="center">finaled</td>
     <td style="border:1px solid" align="center"></td></tr>
+<tr><th style="border:1px solid" align="left">EVP_MAC_CTX_set_params (key)</th>
+    <td style="border:1px solid" align="center"></td>
+    <td style="border:1px solid" align="center">initialised</td>
+    <td style="border:1px solid" align="center">initialised</td>
+    <td style="border:1px solid" align="center">initialised</td>
+    <td style="border:1px solid" align="center">initialised</td>
+    <td style="border:1px solid" align="center"></td></tr>
 <tr><th style="border:1px solid" align="left">EVP_MAC_CTX_gettable_params</th>
     <td style="border:1px solid" align="center"></td>
     <td style="border:1px solid" align="center">newed</td>
@@ -219,6 +228,11 @@ B<newed> and B<finaled> states, see L<EVP_MAC(3)>.  Querying the output
 length by calling EVP_MAC_final() with a NULL output buffer is permitted in
 every state and does not change it.

+EVP_MAC_CTX_set_params() does not change the state, except when it is given
+the MAC key (B<OSSL_MAC_PARAM_KEY>).  It is then equivalent to EVP_MAC_init()
+with a NULL key and the same parameters, which is the "(key)" row of the
+table above.
+
 =head1 SEE ALSO

 L<provider-mac(7)>, L<EVP_MAC(3)>.
diff --git a/test/evp_extra_test.c b/test/evp_extra_test.c
index 822be5fc4c..d5dfcdb45a 100644
--- a/test/evp_extra_test.c
+++ b/test/evp_extra_test.c
@@ -2401,8 +2401,9 @@ static int test_evp_mac_life_cycle(int idx)
     const MAC_LIFE_CYCLE_TEST *t = &mac_life_cycle_tests[idx];
     int ret = 0;
     EVP_MAC *mac = NULL;
-    EVP_MAC_CTX *ctx = NULL, *dup = NULL, *pre = NULL;
+    EVP_MAC_CTX *ctx = NULL, *dup = NULL, *pre = NULL, *kctx = NULL;
     OSSL_PARAM init_params[3], reinit_params[2], bad_params[2], *p;
+    OSSL_PARAM key_params[4], bad_key_params[3];
     static const unsigned char data[] = "EVP_MAC life-cycle enforcement";
     unsigned char key[32], iv[12];
     unsigned char mac1[EVP_MAX_MD_SIZE], mac2[EVP_MAX_MD_SIZE];
@@ -2432,6 +2433,25 @@ static int test_evp_mac_life_cycle(int idx)
             (char *)"no-such-algorithm", 0);
         bad_params[1] = OSSL_PARAM_construct_end();
     }
+    /* The parameters of init_params together with the key */
+    p = key_params;
+    if (t->subalg != NULL)
+        *p++ = OSSL_PARAM_construct_utf8_string(t->subalg_param,
+            (char *)t->subalg, 0);
+    if (t->needs_iv)
+        *p++ = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_IV,
+            iv, sizeof(iv));
+    *p++ = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
+        key, t->keylen);
+    *p = OSSL_PARAM_construct_end();
+    /* A key that cannot be set: unknown sub-algorithm or bad key length */
+    p = bad_key_params;
+    if (t->subalg_param != NULL)
+        *p++ = OSSL_PARAM_construct_utf8_string(t->subalg_param,
+            (char *)"no-such-algorithm", 0);
+    *p++ = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY, key,
+        t->subalg_param != NULL ? t->keylen : t->bad_keylen);
+    *p = OSSL_PARAM_construct_end();

     ERR_clear_error();
     if (!TEST_ptr(mac = EVP_MAC_fetch(testctx, t->name, testpropq))
@@ -2606,8 +2626,66 @@ static int test_evp_mac_life_cycle(int idx)
         || !TEST_ulong_eq(ERR_peek_error(), 0))
         goto err;

+    /*
+     * EVP_MAC_CTX_set_params() does not initialise a new context unless it
+     * is given a key, in which case it is the same as EVP_MAC_init().
+     */
+    if (!TEST_ptr(kctx = EVP_MAC_CTX_new(mac))
+        || !TEST_true(EVP_MAC_CTX_set_params(kctx, init_params))
+        || !TEST_false(EVP_MAC_update(kctx, data, sizeof(data)))
+        || !TEST_err_r(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED))
+        goto err;
+    ERR_clear_error();
+    if (!TEST_true(EVP_MAC_CTX_set_params(kctx, key_params))
+        || !TEST_true(EVP_MAC_update(kctx, data, sizeof(data)))
+        || !TEST_true(EVP_MAC_final(kctx, mac2, &mac2len, sizeof(mac2)))
+        || !TEST_mem_eq(mac2, mac2len, mac1, mac1len)
+        || !TEST_ulong_eq(ERR_peek_error(), 0))
+        goto err;
+
+    /* It re-initialises a finalised context, and discards pending input */
+    if (!TEST_true(EVP_MAC_CTX_set_params(kctx, key_params))
+        || !TEST_true(EVP_MAC_update(kctx, iv, sizeof(iv)))
+        || !TEST_true(EVP_MAC_CTX_set_params(kctx, key_params))
+        || !TEST_true(EVP_MAC_update(kctx, data, sizeof(data)))
+        || !TEST_true(EVP_MAC_final(kctx, mac2, &mac2len, sizeof(mac2)))
+        || !TEST_mem_eq(mac2, mac2len, mac1, mac1len))
+        goto err;
+
+    /* Output can be produced straight after it (empty input) */
+    if (!TEST_true(EVP_MAC_CTX_set_params(kctx, key_params))
+        || !TEST_true(EVP_MAC_final(kctx, mac2, &mac2len, sizeof(mac2)))
+        || !TEST_size_t_gt(mac2len, 0)
+        || !TEST_ulong_eq(ERR_peek_error(), 0))
+        goto err;
+
+    /* Like a failed EVP_MAC_init(), a key that is rejected resets the state */
+    if (!TEST_true(EVP_MAC_CTX_set_params(kctx, key_params))
+        || !TEST_true(EVP_MAC_update(kctx, data, sizeof(data)))
+        || !TEST_false(EVP_MAC_CTX_set_params(kctx, bad_key_params)))
+        goto err;
+    ERR_clear_error();
+    if (!TEST_false(EVP_MAC_update(kctx, data, sizeof(data)))
+        || !TEST_err_r(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED)
+        || !TEST_false(EVP_MAC_final(kctx, mac2, &mac2len, sizeof(mac2)))
+        || !TEST_err_r(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED))
+        goto err;
+    ERR_clear_error();
+
+    /* Parameters without a key still leave the state alone afterwards */
+    if (!TEST_true(EVP_MAC_CTX_set_params(kctx, key_params))
+        || !TEST_true(EVP_MAC_update(kctx, data, sizeof(data)))
+        || !TEST_true(EVP_MAC_final(kctx, mac2, &mac2len, sizeof(mac2)))
+        || !TEST_mem_eq(mac2, mac2len, mac1, mac1len)
+        || !TEST_true(EVP_MAC_CTX_set_params(kctx, reinit_params))
+        || !TEST_false(EVP_MAC_update(kctx, data, sizeof(data)))
+        || !TEST_err_r(ERR_LIB_EVP, EVP_R_UPDATE_ERROR))
+        goto err;
+    ERR_clear_error();
+
     ret = 1;
 err:
+    EVP_MAC_CTX_free(kctx);
     EVP_MAC_CTX_free(pre);
     EVP_MAC_CTX_free(dup);
     EVP_MAC_CTX_free(ctx);
diff --git a/test/sslapitest.c b/test/sslapitest.c
index cec221ac44..40fd1123ae 100644
--- a/test/sslapitest.c
+++ b/test/sslapitest.c
@@ -12501,6 +12501,8 @@ static int test_ssl_get_shared_ciphers(int tst)
 static const char *appdata = "Hello World";
 static int gen_tick_called, dec_tick_called, tick_key_cb_called;
 static int tick_key_renew = 0;
+/* Key the ticket HMAC with EVP_MAC_CTX_set_params() only, as documented */
+static int tick_key_set_params = 0;
 static SSL_TICKET_RETURN tick_dec_ret = SSL_TICKET_RETURN_ABORT;

 static int gen_tick_cb(SSL *s, void *arg)
@@ -12603,7 +12605,7 @@ static int tick_key_evp_cb(SSL *s, unsigned char key_name[16],
 {
     const unsigned char tick_aes_key[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
     unsigned char tick_hmac_key[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
-    OSSL_PARAM params[2];
+    OSSL_PARAM params[3], *p = params;
     EVP_CIPHER *aes128cbc;
     int ret;

@@ -12618,13 +12620,18 @@ static int tick_key_evp_cb(SSL *s, unsigned char key_name[16],

     memset(iv, 0, AES_BLOCK_SIZE);
     memset(key_name, 0, 16);
-    params[0] = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
+    *p++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
         "SHA256", 0);
-    params[1] = OSSL_PARAM_construct_end();
+    if (tick_key_set_params)
+        *p++ = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
+            tick_hmac_key, sizeof(tick_hmac_key));
+    *p = OSSL_PARAM_construct_end();
     if (aes128cbc == NULL
         || !EVP_CipherInit_ex(ctx, aes128cbc, NULL, tick_aes_key, iv, enc)
-        || !EVP_MAC_init(hctx, tick_hmac_key, sizeof(tick_hmac_key),
-            params))
+        || !(tick_key_set_params
+                ? EVP_MAC_CTX_set_params(hctx, params)
+                : EVP_MAC_init(hctx, tick_hmac_key, sizeof(tick_hmac_key),
+                      params)))
         ret = -1;
     else
         ret = tick_key_renew ? 2 : 1;
@@ -12656,6 +12663,10 @@ static int tick_key_evp_cb(SSL *s, unsigned char key_name[16],
  * Test 17: TLSv1.3, ticket key callback, ticket, renewal
  * Test 18: TLSv1.2, ticket key callback, no ticket
  * Test 19: TLSv1.3, ticket key callback, no ticket
+ * Test 20: TLSv1.2, ticket key callback using set_params, ticket, no renewal
+ * Test 21: TLSv1.3, ticket key callback using set_params, ticket, no renewal
+ * Test 22: TLSv1.2, ticket key callback using set_params, ticket, renewal
+ * Test 23: TLSv1.3, ticket key callback using set_params, ticket, renewal
  */
 static int test_ticket_callbacks(int tst)
 {
@@ -12681,13 +12692,19 @@ static int test_ticket_callbacks(int tst)

     /* Which tests the ticket key callback should request renewal for */

-    if (tst == 10 || tst == 11 || tst == 16 || tst == 17)
+    if (tst == 10 || tst == 11 || tst == 16 || tst == 17 || tst >= 22)
         tick_key_renew = 1;
     else if (tst == 12 || tst == 13 || tst == 18 || tst == 19)
         tick_key_renew = -1; /* abort sending the ticket/0-length ticket */
     else
         tick_key_renew = 0;

+    /*
+     * Tests 20 to 23 only supply the HMAC key and digest through
+     * EVP_MAC_CTX_set_params(), without calling EVP_MAC_init()
+     */
+    tick_key_set_params = (tst >= 20);
+
     /* Which tests the decrypt ticket callback should request renewal for */
     switch (tst) {
     case 0:
@@ -12902,6 +12919,7 @@ static int clienthello_cb_server_random(const SSL_METHOD *smeth,
             tick_key_evp_cb_server_random)))
         goto end;
     tick_key_renew = 0;
+    tick_key_set_params = 0;
     tick_key_cb_called = 0;

     sni_cb_random_cnt = dec_tick_cb_random_cnt = tick_key_cb_random_cnt = 0;
@@ -20028,7 +20046,7 @@ int setup_tests(void)
 #endif
     ADD_ALL_TESTS(test_ssl_pending, 2);
     ADD_ALL_TESTS(test_ssl_get_shared_ciphers, OSSL_NELEM(shared_ciphers_data));
-    ADD_ALL_TESTS(test_ticket_callbacks, 20);
+    ADD_ALL_TESTS(test_ticket_callbacks, 24);
     ADD_ALL_TESTS(test_tls_clienthello_cb_server_random, 2);
 #ifndef OPENSSL_NO_DTLS
     ADD_ALL_TESTS(test_dtls_clienthello_cb_server_random, 2);