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