Fixup EVP-MAC-KMAC documentation
[openssl.git] / doc / man7 / EVP_MAC-KMAC.pod
1 =pod
2
3 =head1 NAME
4
5 EVP_MAC-KMAC, EVP_MAC-KMAC128, EVP_MAC-KMAC256
6 - The KMAC EVP_MAC implementations
7
8 =head1 DESCRIPTION
9
10 Support for computing KMAC MACs through the B<EVP_MAC> API.
11
12 =head2 Identity
13
14 These implementations are identified with one of these names and
15 properties, 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
27 The general description of these parameters can be found in
28 L<EVP_MAC(3)/PARAMETERS>.
29
30 All these parameters can be set with EVP_MAC_CTX_set_params().
31 Furthermore, the "size" parameter can be retrieved with
32 EVP_MAC_CTX_get_params(), or with EVP_MAC_CTX_get_mac_size().
33 The length of the "size" parameter should not exceed that of a B<size_t>.
34
35 =over 4
36
37 =item "key" (B<OSSL_MAC_PARAM_KEY>) <octet string>
38
39 =item "custom" (B<OSSL_MAC_PARAM_CUSTOM>) <octet string>
40
41 =item "size" (B<OSSL_MAC_PARAM_SIZE>) <unsigned integer>
42
43 =item "xof" (B<OSSL_MAC_PARAM_XOF>) <integer>
44
45 The "xof" parameter value is expected to be 1 or 0. Use 1 to enable XOF mode.
46 The default value is 0.
47
48 =back
49
50 The "custom" and "key" parameters must be set before EVP_MAC_init().
51 The "xof" and "size" parameters can be set at any time before EVP_MAC_final().
52
53 =head1 EXAMPLES
54
55   #include <openssl/evp.h>
56   #include <openssl/params.h>
57
58   static int do_kmac(const unsigned char *in, size_t in_len,
59                      const unsigned char *key, size_t key_len,
60                      const unsigned char *custom, size_t custom_len,
61                      int xof_enabled, unsigned char *out, int out_len)
62   {
63       EVP_MAC_CTX *ctx = NULL;
64       EVP_MAC *mac = NULL;
65       OSSL_PARAM params[4], *p;
66       int ret = 0;
67       size_t l = 0;
68
69       mac = EVP_MAC_fetch(NULL, "KMAC-128", NULL);
70       if (mac == NULL)
71           goto err;
72       ctx = EVP_MAC_CTX_new(mac);
73       /* The mac can be freed after it is used by EVP_MAC_CTX_new */
74       EVP_MAC_free(mac);
75       if (ctx == NULL)
76           goto err;
77
78       /*
79        * Setup parameters required before calling EVP_MAC_init()
80        * The parameters OSSL_MAC_PARAM_XOF and OSSL_MAC_PARAM_SIZE may also be
81        * used at this point.
82        */
83       p = params;
84       *p++ = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
85                                                (void *)key, key_len);
86       if (custom != NULL && custom_len != 0)
87         *p++ = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_CUSTOM,
88                                                  (void *)custom, custom_len);
89       *p = OSSL_PARAM_construct_end();
90       if (!EVP_MAC_CTX_set_params(ctx, params))
91           goto err;
92
93       if (!EVP_MAC_init(ctx))
94           goto err;
95
96       /*
97        * Note: the following optional parameters can be set any time
98        * before EVP_MAC_final().
99        */
100       p = params;
101       *p++ = OSSL_PARAM_construct_int(OSSL_MAC_PARAM_XOF, &xof_enabled);
102       *p++ = OSSL_PARAM_construct_int(OSSL_MAC_PARAM_SIZE, &out_len);
103       *p = OSSL_PARAM_construct_end();
104       if (!EVP_MAC_CTX_set_params(ctx, params))
105           goto err;
106
107       /* The update may be called multiple times here for streamed input */
108       if (!EVP_MAC_update(ctx, in, in_len))
109           goto err;
110       if (!EVP_MAC_final(ctx, out, &l, out_len))
111           goto err;
112       ret = 1;
113   err:
114       EVP_MAC_CTX_free(ctx);
115       return ret;
116   }
117
118 =head1 SEE ALSO
119
120 L<EVP_MAC_CTX_get_params(3)>, L<EVP_MAC_CTX_set_params(3)>,
121 L<EVP_MAC(3)/PARAMETERS>, L<OSSL_PARAM(3)>
122
123 =head1 COPYRIGHT
124
125 Copyright 2018-2020 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 L<https://www.openssl.org/source/license.html>.
131
132 =cut