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