1EVP_MAC-KMAC(7ossl) OpenSSL EVP_MAC-KMAC(7ossl)
2
3
4
6 EVP_MAC-KMAC, EVP_MAC-KMAC128, EVP_MAC-KMAC256 - The KMAC EVP_MAC
7 implementations
8
10 Support for computing KMAC MACs through the EVP_MAC API.
11
12 Identity
13 These implementations are identified with one of these names and
14 properties, to be used with EVP_MAC_fetch():
15
16 "KMAC-128", "provider=default" or "provider=fips"
17 "KMAC-256", "provider=default" or "provider=fips"
18
19 Supported parameters
20 The general description of these parameters can be found in
21 "PARAMETERS" in EVP_MAC(3).
22
23 All these parameters can be set with EVP_MAC_CTX_set_params().
24 Furthermore, the "size" parameter can be retrieved with
25 EVP_MAC_CTX_get_params(), or with EVP_MAC_CTX_get_mac_size(). The
26 length of the "size" parameter should not exceed that of a size_t.
27 Likewise, the "block-size" parameter can be retrieved with
28 EVP_MAC_CTX_get_params(), or with EVP_MAC_CTX_get_block_size().
29
30 "key" (OSSL_MAC_PARAM_KEY) <octet string>
31 Sets the MAC key. Setting this parameter is identical to passing a
32 key to EVP_MAC_init(3). The length of the key (in bytes) must be
33 in the range 4...512.
34
35 "custom" (OSSL_MAC_PARAM_CUSTOM) <octet string>
36 Sets the custom value. It is an optional value with a length of at
37 most 512 bytes, and is empty by default.
38
39 "size" (OSSL_MAC_PARAM_SIZE) <unsigned integer>
40 Sets the MAC size. By default, it is 16 for "KMAC-128" and 32 for
41 "KMAC-256".
42
43 "block-size" (OSSL_MAC_PARAM_SIZE) <unsigned integer>
44 Gets the MAC block size. By default, it is 168 for "KMAC-128" and
45 136 for "KMAC-256".
46
47 "xof" (OSSL_MAC_PARAM_XOF) <integer>
48 The "xof" parameter value is expected to be 1 or 0. Use 1 to enable
49 XOF mode. The default value is 0.
50
51 The "custom" parameter must be set as part of or before the
52 EVP_MAC_init() call. The "xof" and "size" parameters can be set at any
53 time before EVP_MAC_final(). The "key" parameter is set as part of the
54 EVP_MAC_init() call, but can be set before it instead.
55
57 #include <openssl/evp.h>
58 #include <openssl/params.h>
59
60 static int do_kmac(const unsigned char *in, size_t in_len,
61 const unsigned char *key, size_t key_len,
62 const unsigned char *custom, size_t custom_len,
63 int xof_enabled, unsigned char *out, int out_len)
64 {
65 EVP_MAC_CTX *ctx = NULL;
66 EVP_MAC *mac = NULL;
67 OSSL_PARAM params[4], *p;
68 int ret = 0;
69 size_t l = 0;
70
71 mac = EVP_MAC_fetch(NULL, "KMAC-128", NULL);
72 if (mac == NULL)
73 goto err;
74 ctx = EVP_MAC_CTX_new(mac);
75 /* The mac can be freed after it is used by EVP_MAC_CTX_new */
76 EVP_MAC_free(mac);
77 if (ctx == NULL)
78 goto err;
79
80 /*
81 * Setup parameters required before calling EVP_MAC_init()
82 * The parameters OSSL_MAC_PARAM_XOF and OSSL_MAC_PARAM_SIZE may also be
83 * used at this point.
84 */
85 p = params;
86 *p++ = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
87 (void *)key, key_len);
88 if (custom != NULL && custom_len != 0)
89 *p++ = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_CUSTOM,
90 (void *)custom, custom_len);
91 *p = OSSL_PARAM_construct_end();
92 if (!EVP_MAC_CTX_set_params(ctx, params))
93 goto err;
94
95 if (!EVP_MAC_init(ctx))
96 goto err;
97
98 /*
99 * Note: the following optional parameters can be set any time
100 * before EVP_MAC_final().
101 */
102 p = params;
103 *p++ = OSSL_PARAM_construct_int(OSSL_MAC_PARAM_XOF, &xof_enabled);
104 *p++ = OSSL_PARAM_construct_int(OSSL_MAC_PARAM_SIZE, &out_len);
105 *p = OSSL_PARAM_construct_end();
106 if (!EVP_MAC_CTX_set_params(ctx, params))
107 goto err;
108
109 /* The update may be called multiple times here for streamed input */
110 if (!EVP_MAC_update(ctx, in, in_len))
111 goto err;
112 if (!EVP_MAC_final(ctx, out, &l, out_len))
113 goto err;
114 ret = 1;
115 err:
116 EVP_MAC_CTX_free(ctx);
117 return ret;
118 }
119
121 EVP_MAC_CTX_get_params(3), EVP_MAC_CTX_set_params(3), "PARAMETERS" in
122 EVP_MAC(3), OSSL_PARAM(3)
123
125 Copyright 2018-2022 The OpenSSL Project Authors. All Rights Reserved.
126
127 Licensed under the Apache License 2.0 (the "License"). You may not use
128 this file except in compliance with the License. You can obtain a copy
129 in the file LICENSE in the source distribution or at
130 <https://www.openssl.org/source/license.html>.
131
132
133
1343.1.1 2023-08-31 EVP_MAC-KMAC(7ossl)