xref: /freebsd/crypto/openssl/doc/man3/EVP_MAC.pod (revision 6f1af0d7d2af54b339b5212434cd6d4fda628d80)
1b077aed3SPierre Pronchery=pod
2b077aed3SPierre Pronchery
3b077aed3SPierre Pronchery=head1 NAME
4b077aed3SPierre Pronchery
5b077aed3SPierre ProncheryEVP_MAC, EVP_MAC_fetch, EVP_MAC_up_ref, EVP_MAC_free, EVP_MAC_is_a,
6b077aed3SPierre ProncheryEVP_MAC_get0_name, EVP_MAC_names_do_all, EVP_MAC_get0_description,
7b077aed3SPierre ProncheryEVP_MAC_get0_provider, EVP_MAC_get_params, EVP_MAC_gettable_params,
8b077aed3SPierre ProncheryEVP_MAC_CTX, EVP_MAC_CTX_new, EVP_MAC_CTX_free, EVP_MAC_CTX_dup,
9b077aed3SPierre ProncheryEVP_MAC_CTX_get0_mac, EVP_MAC_CTX_get_params, EVP_MAC_CTX_set_params,
10b077aed3SPierre ProncheryEVP_MAC_CTX_get_mac_size, EVP_MAC_CTX_get_block_size, EVP_Q_mac,
11b077aed3SPierre ProncheryEVP_MAC_init, EVP_MAC_update, EVP_MAC_final, EVP_MAC_finalXOF,
12b077aed3SPierre ProncheryEVP_MAC_gettable_ctx_params, EVP_MAC_settable_ctx_params,
13b077aed3SPierre ProncheryEVP_MAC_CTX_gettable_params, EVP_MAC_CTX_settable_params,
14b077aed3SPierre ProncheryEVP_MAC_do_all_provided - EVP MAC routines
15b077aed3SPierre Pronchery
16b077aed3SPierre Pronchery=head1 SYNOPSIS
17b077aed3SPierre Pronchery
18b077aed3SPierre Pronchery #include <openssl/evp.h>
19b077aed3SPierre Pronchery
20b077aed3SPierre Pronchery typedef struct evp_mac_st EVP_MAC;
21b077aed3SPierre Pronchery typedef struct evp_mac_ctx_st EVP_MAC_CTX;
22b077aed3SPierre Pronchery
23b077aed3SPierre Pronchery EVP_MAC *EVP_MAC_fetch(OSSL_LIB_CTX *libctx, const char *algorithm,
24b077aed3SPierre Pronchery                        const char *properties);
25b077aed3SPierre Pronchery int EVP_MAC_up_ref(EVP_MAC *mac);
26b077aed3SPierre Pronchery void EVP_MAC_free(EVP_MAC *mac);
27b077aed3SPierre Pronchery int EVP_MAC_is_a(const EVP_MAC *mac, const char *name);
28b077aed3SPierre Pronchery const char *EVP_MAC_get0_name(const EVP_MAC *mac);
29b077aed3SPierre Pronchery int EVP_MAC_names_do_all(const EVP_MAC *mac,
30b077aed3SPierre Pronchery                          void (*fn)(const char *name, void *data),
31b077aed3SPierre Pronchery                          void *data);
32b077aed3SPierre Pronchery const char *EVP_MAC_get0_description(const EVP_MAC *mac);
33b077aed3SPierre Pronchery const OSSL_PROVIDER *EVP_MAC_get0_provider(const EVP_MAC *mac);
34b077aed3SPierre Pronchery int EVP_MAC_get_params(EVP_MAC *mac, OSSL_PARAM params[]);
35b077aed3SPierre Pronchery
36b077aed3SPierre Pronchery EVP_MAC_CTX *EVP_MAC_CTX_new(EVP_MAC *mac);
37b077aed3SPierre Pronchery void EVP_MAC_CTX_free(EVP_MAC_CTX *ctx);
38b077aed3SPierre Pronchery EVP_MAC_CTX *EVP_MAC_CTX_dup(const EVP_MAC_CTX *src);
39b077aed3SPierre Pronchery EVP_MAC *EVP_MAC_CTX_get0_mac(EVP_MAC_CTX *ctx);
40b077aed3SPierre Pronchery int EVP_MAC_CTX_get_params(EVP_MAC_CTX *ctx, OSSL_PARAM params[]);
41b077aed3SPierre Pronchery int EVP_MAC_CTX_set_params(EVP_MAC_CTX *ctx, const OSSL_PARAM params[]);
42b077aed3SPierre Pronchery
43b077aed3SPierre Pronchery size_t EVP_MAC_CTX_get_mac_size(EVP_MAC_CTX *ctx);
44b077aed3SPierre Pronchery size_t EVP_MAC_CTX_get_block_size(EVP_MAC_CTX *ctx);
45b077aed3SPierre Pronchery unsigned char *EVP_Q_mac(OSSL_LIB_CTX *libctx, const char *name, const char *propq,
46b077aed3SPierre Pronchery                          const char *subalg, const OSSL_PARAM *params,
47b077aed3SPierre Pronchery                          const void *key, size_t keylen,
48b077aed3SPierre Pronchery                          const unsigned char *data, size_t datalen,
49b077aed3SPierre Pronchery                          unsigned char *out, size_t outsize, size_t *outlen);
50b077aed3SPierre Pronchery int EVP_MAC_init(EVP_MAC_CTX *ctx, const unsigned char *key, size_t keylen,
51b077aed3SPierre Pronchery                  const OSSL_PARAM params[]);
52b077aed3SPierre Pronchery int EVP_MAC_update(EVP_MAC_CTX *ctx, const unsigned char *data, size_t datalen);
53b077aed3SPierre Pronchery int EVP_MAC_final(EVP_MAC_CTX *ctx,
54b077aed3SPierre Pronchery                   unsigned char *out, size_t *outl, size_t outsize);
55b077aed3SPierre Pronchery int EVP_MAC_finalXOF(EVP_MAC_CTX *ctx, unsigned char *out, size_t outsize);
56b077aed3SPierre Pronchery
57b077aed3SPierre Pronchery const OSSL_PARAM *EVP_MAC_gettable_params(const EVP_MAC *mac);
58b077aed3SPierre Pronchery const OSSL_PARAM *EVP_MAC_gettable_ctx_params(const EVP_MAC *mac);
59b077aed3SPierre Pronchery const OSSL_PARAM *EVP_MAC_settable_ctx_params(const EVP_MAC *mac);
60b077aed3SPierre Pronchery const OSSL_PARAM *EVP_MAC_CTX_gettable_params(EVP_MAC_CTX *ctx);
61b077aed3SPierre Pronchery const OSSL_PARAM *EVP_MAC_CTX_settable_params(EVP_MAC_CTX *ctx);
62b077aed3SPierre Pronchery
63b077aed3SPierre Pronchery void EVP_MAC_do_all_provided(OSSL_LIB_CTX *libctx,
64b077aed3SPierre Pronchery                              void (*fn)(EVP_MAC *mac, void *arg),
65b077aed3SPierre Pronchery                              void *arg);
66b077aed3SPierre Pronchery
67b077aed3SPierre Pronchery=head1 DESCRIPTION
68b077aed3SPierre Pronchery
69b077aed3SPierre ProncheryThese types and functions help the application to calculate MACs of
70b077aed3SPierre Proncherydifferent types and with different underlying algorithms if there are
71b077aed3SPierre Proncheryany.
72b077aed3SPierre Pronchery
73b077aed3SPierre ProncheryMACs are a bit complex insofar that some of them use other algorithms
74b077aed3SPierre Proncheryfor actual computation.  HMAC uses a digest, and CMAC uses a cipher.
75b077aed3SPierre ProncheryTherefore, there are sometimes two contexts to keep track of, one for
76b077aed3SPierre Proncherythe MAC algorithm itself and one for the underlying computation
77b077aed3SPierre Proncheryalgorithm if there is one.
78b077aed3SPierre Pronchery
79b077aed3SPierre ProncheryTo make things less ambiguous, this manual talks about a "context" or
80b077aed3SPierre Pronchery"MAC context", which is to denote the MAC level context, and about a
81b077aed3SPierre Pronchery"underlying context", or "computation context", which is to denote the
82b077aed3SPierre Proncherycontext for the underlying computation algorithm if there is one.
83b077aed3SPierre Pronchery
84b077aed3SPierre Pronchery=head2 Types
85b077aed3SPierre Pronchery
86b077aed3SPierre ProncheryB<EVP_MAC> is a type that holds the implementation of a MAC.
87b077aed3SPierre Pronchery
88b077aed3SPierre ProncheryB<EVP_MAC_CTX> is a context type that holds internal MAC information
89b077aed3SPierre Proncheryas well as a reference to a computation context, for those MACs that
90b077aed3SPierre Proncheryrely on an underlying computation algorithm.
91b077aed3SPierre Pronchery
92b077aed3SPierre Pronchery=head2 Algorithm implementation fetching
93b077aed3SPierre Pronchery
94b077aed3SPierre ProncheryEVP_MAC_fetch() fetches an implementation of a MAC I<algorithm>, given
95b077aed3SPierre Proncherya library context I<libctx> and a set of I<properties>.
96b077aed3SPierre ProncherySee L<crypto(7)/ALGORITHM FETCHING> for further information.
97b077aed3SPierre Pronchery
98b077aed3SPierre ProncherySee L<OSSL_PROVIDER-default(7)/Message Authentication Code (MAC)> for the list
99b077aed3SPierre Proncheryof algorithms supported by the default provider.
100b077aed3SPierre Pronchery
101b077aed3SPierre ProncheryThe returned value must eventually be freed with
102b077aed3SPierre ProncheryL<EVP_MAC_free(3)>.
103b077aed3SPierre Pronchery
104b077aed3SPierre ProncheryEVP_MAC_up_ref() increments the reference count of an already fetched
105b077aed3SPierre ProncheryMAC.
106b077aed3SPierre Pronchery
107b077aed3SPierre ProncheryEVP_MAC_free() frees a fetched algorithm.
108b077aed3SPierre ProncheryNULL is a valid parameter, for which this function is a no-op.
109b077aed3SPierre Pronchery
110b077aed3SPierre Pronchery=head2 Context manipulation functions
111b077aed3SPierre Pronchery
112b077aed3SPierre ProncheryEVP_MAC_CTX_new() creates a new context for the MAC type I<mac>.
113b077aed3SPierre ProncheryThe created context can then be used with most other functions
114b077aed3SPierre Proncherydescribed here.
115b077aed3SPierre Pronchery
116b077aed3SPierre ProncheryEVP_MAC_CTX_free() frees the contents of the context, including an
117b077aed3SPierre Proncheryunderlying context if there is one, as well as the context itself.
118b077aed3SPierre ProncheryNULL is a valid parameter, for which this function is a no-op.
119b077aed3SPierre Pronchery
120b077aed3SPierre ProncheryEVP_MAC_CTX_dup() duplicates the I<src> context and returns a newly allocated
121b077aed3SPierre Proncherycontext.
122b077aed3SPierre Pronchery
123b077aed3SPierre ProncheryEVP_MAC_CTX_get0_mac() returns the B<EVP_MAC> associated with the context
124b077aed3SPierre ProncheryI<ctx>.
125b077aed3SPierre Pronchery
126b077aed3SPierre Pronchery=head2 Computing functions
127b077aed3SPierre Pronchery
128b077aed3SPierre ProncheryEVP_Q_mac() computes the message authentication code
129b077aed3SPierre Proncheryof I<data> with length I<datalen>
130b077aed3SPierre Proncheryusing the MAC algorithm I<name> and the key I<key> with length I<keylen>.
131b077aed3SPierre ProncheryThe MAC algorithm is fetched using any given I<libctx> and property query
132b077aed3SPierre Proncherystring I<propq>. It takes parameters I<subalg> and further I<params>,
133b077aed3SPierre Proncheryboth of which may be NULL if not needed.
134b077aed3SPierre ProncheryIf I<out> is not NULL, it places the result in the memory pointed at by I<out>,
135b077aed3SPierre Proncherybut only if I<outsize> is sufficient (otherwise no computation is made).
136b077aed3SPierre ProncheryIf I<out> is NULL, it allocates and uses a buffer of suitable length,
137b077aed3SPierre Proncherywhich will be returned on success and must be freed by the caller.
138b077aed3SPierre ProncheryIn either case, also on error,
139b077aed3SPierre Proncheryit assigns the number of bytes written to I<*outlen> unless I<outlen> is NULL.
140b077aed3SPierre Pronchery
141b077aed3SPierre ProncheryEVP_MAC_init() sets up the underlying context I<ctx> with information given
142b077aed3SPierre Proncheryvia the I<key> and I<params> arguments.  The MAC I<key> has a length of
143b077aed3SPierre ProncheryI<keylen> and the parameters in I<params> are processed before setting
144b077aed3SPierre Proncherythe key.  If I<key> is NULL, the key must be set via I<params> either
145b077aed3SPierre Proncheryas part of this call or separately using EVP_MAC_CTX_set_params().
146b077aed3SPierre ProncheryProviding non-NULL I<params> to this function is equivalent to calling
147b077aed3SPierre ProncheryEVP_MAC_CTX_set_params() with those I<params> for the same I<ctx> beforehand.
148b077aed3SPierre Pronchery
149b077aed3SPierre ProncheryEVP_MAC_init() should be called before EVP_MAC_update() and EVP_MAC_final().
150b077aed3SPierre Pronchery
151b077aed3SPierre ProncheryEVP_MAC_update() adds I<datalen> bytes from I<data> to the MAC input.
152b077aed3SPierre Pronchery
153b077aed3SPierre ProncheryEVP_MAC_final() does the final computation and stores the result in
154b077aed3SPierre Proncherythe memory pointed at by I<out> of size I<outsize>, and sets the number
155b077aed3SPierre Proncheryof bytes written in I<*outl> at.
156b077aed3SPierre ProncheryIf I<out> is NULL or I<outsize> is too small, then no computation
157b077aed3SPierre Proncheryis made.
158b077aed3SPierre ProncheryTo figure out what the output length will be and allocate space for it
159b077aed3SPierre Proncherydynamically, simply call with I<out> being NULL and I<outl>
160b077aed3SPierre Proncherypointing at a valid location, then allocate space and make a second
161b077aed3SPierre Proncherycall with I<out> pointing at the allocated space.
162b077aed3SPierre Pronchery
163b077aed3SPierre ProncheryEVP_MAC_finalXOF() does the final computation for an XOF based MAC and stores
164b077aed3SPierre Proncherythe result in the memory pointed at by I<out> of size I<outsize>.
165b077aed3SPierre Pronchery
166b077aed3SPierre ProncheryEVP_MAC_get_params() retrieves details about the implementation
167b077aed3SPierre ProncheryI<mac>.
168b077aed3SPierre ProncheryThe set of parameters given with I<params> determine exactly what
169b077aed3SPierre Proncheryparameters should be retrieved.
170b077aed3SPierre ProncheryNote that a parameter that is unknown in the underlying context is
171b077aed3SPierre Proncherysimply ignored.
172b077aed3SPierre Pronchery
173b077aed3SPierre ProncheryEVP_MAC_CTX_get_params() retrieves chosen parameters, given the
174b077aed3SPierre Proncherycontext I<ctx> and its underlying context.
175b077aed3SPierre ProncheryThe set of parameters given with I<params> determine exactly what
176b077aed3SPierre Proncheryparameters should be retrieved.
177b077aed3SPierre ProncheryNote that a parameter that is unknown in the underlying context is
178b077aed3SPierre Proncherysimply ignored.
179b077aed3SPierre Pronchery
180b077aed3SPierre ProncheryEVP_MAC_CTX_set_params() passes chosen parameters to the underlying
181b077aed3SPierre Proncherycontext, given a context I<ctx>.
182b077aed3SPierre ProncheryThe set of parameters given with I<params> determine exactly what
183b077aed3SPierre Proncheryparameters are passed down.
184*6f1af0d7SPierre ProncheryIf I<params> are NULL, the underlying context should do nothing and return 1.
185b077aed3SPierre ProncheryNote that a parameter that is unknown in the underlying context is
186b077aed3SPierre Proncherysimply ignored.
187b077aed3SPierre ProncheryAlso, what happens when a needed parameter isn't passed down is
188b077aed3SPierre Proncherydefined by the implementation.
189b077aed3SPierre Pronchery
190b077aed3SPierre ProncheryEVP_MAC_gettable_params() returns an L<OSSL_PARAM(3)> array that describes
191b077aed3SPierre Proncherythe retrievable and settable parameters.  EVP_MAC_gettable_params()
192b077aed3SPierre Proncheryreturns parameters that can be used with EVP_MAC_get_params().
193b077aed3SPierre Pronchery
194b077aed3SPierre ProncheryEVP_MAC_gettable_ctx_params() and EVP_MAC_CTX_gettable_params()
195b077aed3SPierre Proncheryreturn constant L<OSSL_PARAM(3)> arrays that describe the retrievable
196b077aed3SPierre Proncheryparameters that can be used with EVP_MAC_CTX_get_params().
197b077aed3SPierre ProncheryEVP_MAC_gettable_ctx_params() returns the parameters that can be retrieved
198b077aed3SPierre Proncheryfrom the algorithm, whereas EVP_MAC_CTX_gettable_params() returns
199b077aed3SPierre Proncherythe parameters that can be retrieved in the context's current state.
200b077aed3SPierre Pronchery
201b077aed3SPierre ProncheryEVP_MAC_settable_ctx_params() and EVP_MAC_CTX_settable_params() return
202b077aed3SPierre Proncheryconstant L<OSSL_PARAM(3)> arrays that describe the settable parameters that
203b077aed3SPierre Proncherycan be used with EVP_MAC_CTX_set_params().  EVP_MAC_settable_ctx_params()
204b077aed3SPierre Proncheryreturns the parameters that can be retrieved from the algorithm,
205b077aed3SPierre Proncherywhereas EVP_MAC_CTX_settable_params() returns the parameters that can
206b077aed3SPierre Proncherybe retrieved in the context's current state.
207b077aed3SPierre Pronchery
208b077aed3SPierre Pronchery=head2 Information functions
209b077aed3SPierre Pronchery
210b077aed3SPierre ProncheryEVP_MAC_CTX_get_mac_size() returns the MAC output size for the given context.
211b077aed3SPierre Pronchery
212b077aed3SPierre ProncheryEVP_MAC_CTX_get_block_size() returns the MAC block size for the given context.
213b077aed3SPierre ProncheryNot all MAC algorithms support this.
214b077aed3SPierre Pronchery
215b077aed3SPierre ProncheryEVP_MAC_is_a() checks if the given I<mac> is an implementation of an
216b077aed3SPierre Proncheryalgorithm that's identifiable with I<name>.
217b077aed3SPierre Pronchery
218b077aed3SPierre ProncheryEVP_MAC_get0_provider() returns the provider that holds the implementation
219b077aed3SPierre Proncheryof the given I<mac>.
220b077aed3SPierre Pronchery
221b077aed3SPierre ProncheryEVP_MAC_do_all_provided() traverses all MAC implemented by all activated
222b077aed3SPierre Proncheryproviders in the given library context I<libctx>, and for each of the
223b077aed3SPierre Proncheryimplementations, calls the given function I<fn> with the implementation method
224b077aed3SPierre Proncheryand the given I<arg> as argument.
225b077aed3SPierre Pronchery
226b077aed3SPierre ProncheryEVP_MAC_get0_name() return the name of the given MAC.  For fetched MACs
227b077aed3SPierre Proncherywith multiple names, only one of them is returned; it's
228b077aed3SPierre Proncheryrecommended to use EVP_MAC_names_do_all() instead.
229b077aed3SPierre Pronchery
230b077aed3SPierre ProncheryEVP_MAC_names_do_all() traverses all names for I<mac>, and calls
231b077aed3SPierre ProncheryI<fn> with each name and I<data>.
232b077aed3SPierre Pronchery
233b077aed3SPierre ProncheryEVP_MAC_get0_description() returns a description of the I<mac>, meant
234b077aed3SPierre Proncheryfor display and human consumption.  The description is at the discretion
235b077aed3SPierre Proncheryof the mac implementation.
236b077aed3SPierre Pronchery
237b077aed3SPierre Pronchery=head1 PARAMETERS
238b077aed3SPierre Pronchery
239b077aed3SPierre ProncheryParameters are identified by name as strings, and have an expected
240b077aed3SPierre Proncherydata type and maximum size.
241b077aed3SPierre ProncheryOpenSSL has a set of macros for parameter names it expects to see in
242b077aed3SPierre Proncheryits own MAC implementations.
243b077aed3SPierre ProncheryHere, we show all three, the OpenSSL macro for the parameter name, the
244b077aed3SPierre Proncheryname in string form, and a type description.
245b077aed3SPierre Pronchery
246b077aed3SPierre ProncheryThe standard parameter names are:
247b077aed3SPierre Pronchery
248b077aed3SPierre Pronchery=over 4
249b077aed3SPierre Pronchery
250b077aed3SPierre Pronchery=item "key" (B<OSSL_MAC_PARAM_KEY>) <octet string>
251b077aed3SPierre Pronchery
252b077aed3SPierre ProncheryIts value is the MAC key as an array of bytes.
253b077aed3SPierre Pronchery
254b077aed3SPierre ProncheryFor MACs that use an underlying computation algorithm, the algorithm
255b077aed3SPierre Proncherymust be set first, see parameter names "algorithm" below.
256b077aed3SPierre Pronchery
257b077aed3SPierre Pronchery=item "iv" (B<OSSL_MAC_PARAM_IV>) <octet string>
258b077aed3SPierre Pronchery
259b077aed3SPierre ProncherySome MAC implementations (GMAC) require an IV, this parameter sets the IV.
260b077aed3SPierre Pronchery
261b077aed3SPierre Pronchery=item "custom" (B<OSSL_MAC_PARAM_CUSTOM>) <octet string>
262b077aed3SPierre Pronchery
263b077aed3SPierre ProncherySome MAC implementations (KMAC, BLAKE2) accept a Customization String,
264b077aed3SPierre Proncherythis parameter sets the Customization String. The default value is the
265b077aed3SPierre Proncheryempty string.
266b077aed3SPierre Pronchery
267b077aed3SPierre Pronchery=item "salt" (B<OSSL_MAC_PARAM_SALT>) <octet string>
268b077aed3SPierre Pronchery
269b077aed3SPierre ProncheryThis option is used by BLAKE2 MAC.
270b077aed3SPierre Pronchery
271b077aed3SPierre Pronchery=item "xof" (B<OSSL_MAC_PARAM_XOF>) <integer>
272b077aed3SPierre Pronchery
273b077aed3SPierre ProncheryIt's a simple flag, the value 0 or 1 are expected.
274b077aed3SPierre Pronchery
275b077aed3SPierre ProncheryThis option is used by KMAC.
276b077aed3SPierre Pronchery
277b077aed3SPierre Pronchery=item "digest-noinit" (B<OSSL_MAC_PARAM_DIGEST_NOINIT>) <integer>
278b077aed3SPierre Pronchery
279b077aed3SPierre ProncheryA simple flag to set the MAC digest to not initialise the
280b077aed3SPierre Proncheryimplementation specific data. The value 0 or 1 is expected.
281b077aed3SPierre Pronchery
282b077aed3SPierre ProncheryThis option is used by HMAC.
283b077aed3SPierre Pronchery
284b077aed3SPierre Pronchery=item "digest-oneshot" (B<OSSL_MAC_PARAM_DIGEST_ONESHOT>) <integer>
285b077aed3SPierre Pronchery
286b077aed3SPierre ProncheryA simple flag to set the MAC digest to be a oneshot operation.
287b077aed3SPierre ProncheryThe value 0 or 1 is expected.
288b077aed3SPierre Pronchery
289b077aed3SPierre ProncheryThis option is used by HMAC.
290b077aed3SPierre Pronchery
291b077aed3SPierre Pronchery=item "properties" (B<OSSL_MAC_PARAM_PROPERTIES>) <UTF8 string>
292b077aed3SPierre Pronchery
293b077aed3SPierre Pronchery=item "digest" (B<OSSL_MAC_PARAM_DIGEST>) <UTF8 string>
294b077aed3SPierre Pronchery
295b077aed3SPierre Pronchery=item "cipher" (B<OSSL_MAC_PARAM_CIPHER>) <UTF8 string>
296b077aed3SPierre Pronchery
297b077aed3SPierre ProncheryFor MAC implementations that use an underlying computation cipher or
298b077aed3SPierre Proncherydigest, these parameters set what the algorithm should be.
299b077aed3SPierre Pronchery
300b077aed3SPierre ProncheryThe value is always the name of the intended algorithm,
301b077aed3SPierre Proncheryor the properties.
302b077aed3SPierre Pronchery
303b077aed3SPierre ProncheryNote that not all algorithms may support all digests.
304b077aed3SPierre ProncheryHMAC does not support variable output length digests such as SHAKE128
305b077aed3SPierre Proncheryor SHAKE256.
306b077aed3SPierre Pronchery
307b077aed3SPierre Pronchery=item "size" (B<OSSL_MAC_PARAM_SIZE>) <unsigned integer>
308b077aed3SPierre Pronchery
309b077aed3SPierre ProncheryFor MAC implementations that support it, set the output size that
310b077aed3SPierre ProncheryEVP_MAC_final() should produce.
311b077aed3SPierre ProncheryThe allowed sizes vary between MAC implementations, but must never exceed
312b077aed3SPierre Proncherywhat can be given with a B<size_t>.
313b077aed3SPierre Pronchery
314b077aed3SPierre Pronchery=item "tls-data-size" (B<OSSL_MAC_PARAM_TLS_DATA_SIZE>) <unsigned integer>
315b077aed3SPierre Pronchery
316b077aed3SPierre ProncheryThis parameter is only supported by HMAC. If set then special handling is
317b077aed3SPierre Proncheryactivated for calculating the MAC of a received mac-then-encrypt TLS record
318b077aed3SPierre Proncherywhere variable length record padding has been used (as in the case of CBC mode
319b077aed3SPierre Proncheryciphersuites). The value represents the total length of the record that is
320b077aed3SPierre Proncheryhaving the MAC calculated including the received MAC and the record padding.
321b077aed3SPierre Pronchery
322b077aed3SPierre ProncheryWhen used EVP_MAC_update must be called precisely twice. The first time with
323b077aed3SPierre Proncherythe 13 bytes of TLS "header" data, and the second time with the entire record
324b077aed3SPierre Proncheryincluding the MAC itself and any padding. The entire record length must equal
325b077aed3SPierre Proncherythe value passed in the "tls-data-size" parameter. The length passed in the
326b077aed3SPierre ProncheryB<datalen> parameter to EVP_MAC_update() should be equal to the length of the
327b077aed3SPierre Proncheryrecord after the MAC and any padding has been removed.
328b077aed3SPierre Pronchery
329b077aed3SPierre Pronchery=back
330b077aed3SPierre Pronchery
331b077aed3SPierre ProncheryAll these parameters should be used before the calls to any of
332b077aed3SPierre ProncheryEVP_MAC_init(), EVP_MAC_update() and EVP_MAC_final() for a full
333b077aed3SPierre Proncherycomputation.
334b077aed3SPierre ProncheryAnything else may give undefined results.
335b077aed3SPierre Pronchery
336b077aed3SPierre Pronchery=head1 NOTES
337b077aed3SPierre Pronchery
338b077aed3SPierre ProncheryThe MAC life-cycle is described in L<life_cycle-mac(7)>.  In the future,
339b077aed3SPierre Proncherythe transitions described there will be enforced.  When this is done, it will
340b077aed3SPierre Proncherynot be considered a breaking change to the API.
341b077aed3SPierre Pronchery
342b077aed3SPierre ProncheryThe usage of the parameter names "custom", "iv" and "salt" correspond to
343b077aed3SPierre Proncherythe names used in the standard where the algorithm was defined.
344b077aed3SPierre Pronchery
345b077aed3SPierre Pronchery=head1 RETURN VALUES
346b077aed3SPierre Pronchery
347b077aed3SPierre ProncheryEVP_MAC_fetch() returns a pointer to a newly fetched B<EVP_MAC>, or
348b077aed3SPierre ProncheryNULL if allocation failed.
349b077aed3SPierre Pronchery
350b077aed3SPierre ProncheryEVP_MAC_up_ref() returns 1 on success, 0 on error.
351b077aed3SPierre Pronchery
352b077aed3SPierre ProncheryEVP_MAC_names_do_all() returns 1 if the callback was called for all names. A
353b077aed3SPierre Proncheryreturn value of 0 means that the callback was not called for any names.
354b077aed3SPierre Pronchery
355b077aed3SPierre ProncheryEVP_MAC_free() returns nothing at all.
356b077aed3SPierre Pronchery
357b077aed3SPierre ProncheryEVP_MAC_is_a() returns 1 if the given method can be identified with
358b077aed3SPierre Proncherythe given name, otherwise 0.
359b077aed3SPierre Pronchery
360b077aed3SPierre ProncheryEVP_MAC_get0_name() returns a name of the MAC, or NULL on error.
361b077aed3SPierre Pronchery
362b077aed3SPierre ProncheryEVP_MAC_get0_provider() returns a pointer to the provider for the MAC, or
363b077aed3SPierre ProncheryNULL on error.
364b077aed3SPierre Pronchery
365b077aed3SPierre ProncheryEVP_MAC_CTX_new() and EVP_MAC_CTX_dup() return a pointer to a newly
366b077aed3SPierre Proncherycreated EVP_MAC_CTX, or NULL if allocation failed.
367b077aed3SPierre Pronchery
368b077aed3SPierre ProncheryEVP_MAC_CTX_free() returns nothing at all.
369b077aed3SPierre Pronchery
370b077aed3SPierre ProncheryEVP_MAC_CTX_get_params() and EVP_MAC_CTX_set_params() return 1 on
371b077aed3SPierre Proncherysuccess, 0 on error.
372b077aed3SPierre Pronchery
373b077aed3SPierre ProncheryEVP_Q_mac() returns a pointer to the computed MAC value, or NULL on error.
374b077aed3SPierre Pronchery
375b077aed3SPierre ProncheryEVP_MAC_init(), EVP_MAC_update(), EVP_MAC_final(), and EVP_MAC_finalXOF()
376b077aed3SPierre Proncheryreturn 1 on success, 0 on error.
377b077aed3SPierre Pronchery
378b077aed3SPierre ProncheryEVP_MAC_CTX_get_mac_size() returns the expected output size, or 0 if it isn't
379b077aed3SPierre Proncheryset.  If it isn't set, a call to EVP_MAC_init() will set it.
380b077aed3SPierre Pronchery
381b077aed3SPierre ProncheryEVP_MAC_CTX_get_block_size() returns the block size, or 0 if it isn't set.
382b077aed3SPierre ProncheryIf it isn't set, a call to EVP_MAC_init() will set it.
383b077aed3SPierre Pronchery
384b077aed3SPierre ProncheryEVP_MAC_do_all_provided() returns nothing at all.
385b077aed3SPierre Pronchery
386b077aed3SPierre Pronchery=head1 EXAMPLES
387b077aed3SPierre Pronchery
388b077aed3SPierre Pronchery  #include <stdlib.h>
389b077aed3SPierre Pronchery  #include <stdio.h>
390b077aed3SPierre Pronchery  #include <string.h>
391b077aed3SPierre Pronchery  #include <stdarg.h>
392b077aed3SPierre Pronchery  #include <unistd.h>
393b077aed3SPierre Pronchery
394b077aed3SPierre Pronchery  #include <openssl/evp.h>
395b077aed3SPierre Pronchery  #include <openssl/err.h>
396b077aed3SPierre Pronchery  #include <openssl/params.h>
397b077aed3SPierre Pronchery
398b077aed3SPierre Pronchery  int main() {
399b077aed3SPierre Pronchery      EVP_MAC *mac = EVP_MAC_fetch(NULL, getenv("MY_MAC"), NULL);
400b077aed3SPierre Pronchery      const char *cipher = getenv("MY_MAC_CIPHER");
401b077aed3SPierre Pronchery      const char *digest = getenv("MY_MAC_DIGEST");
402b077aed3SPierre Pronchery      const char *key = getenv("MY_KEY");
403b077aed3SPierre Pronchery      EVP_MAC_CTX *ctx = NULL;
404b077aed3SPierre Pronchery
405b077aed3SPierre Pronchery      unsigned char buf[4096];
406b077aed3SPierre Pronchery      size_t read_l;
407b077aed3SPierre Pronchery      size_t final_l;
408b077aed3SPierre Pronchery
409b077aed3SPierre Pronchery      size_t i;
410b077aed3SPierre Pronchery
411b077aed3SPierre Pronchery      OSSL_PARAM params[3];
412b077aed3SPierre Pronchery      size_t params_n = 0;
413b077aed3SPierre Pronchery
414b077aed3SPierre Pronchery      if (cipher != NULL)
415b077aed3SPierre Pronchery          params[params_n++] =
416b077aed3SPierre Pronchery              OSSL_PARAM_construct_utf8_string("cipher", (char*)cipher, 0);
417b077aed3SPierre Pronchery      if (digest != NULL)
418b077aed3SPierre Pronchery          params[params_n++] =
419b077aed3SPierre Pronchery              OSSL_PARAM_construct_utf8_string("digest", (char*)digest, 0);
420b077aed3SPierre Pronchery      params[params_n] = OSSL_PARAM_construct_end();
421b077aed3SPierre Pronchery
422b077aed3SPierre Pronchery      if (mac == NULL
423b077aed3SPierre Pronchery          || key == NULL
424b077aed3SPierre Pronchery          || (ctx = EVP_MAC_CTX_new(mac)) == NULL
425b077aed3SPierre Pronchery          || !EVP_MAC_init(ctx, (const unsigned char *)key, strlen(key),
426b077aed3SPierre Pronchery                           params))
427b077aed3SPierre Pronchery          goto err;
428b077aed3SPierre Pronchery
429b077aed3SPierre Pronchery      while ( (read_l = read(STDIN_FILENO, buf, sizeof(buf))) > 0) {
430b077aed3SPierre Pronchery          if (!EVP_MAC_update(ctx, buf, read_l))
431b077aed3SPierre Pronchery              goto err;
432b077aed3SPierre Pronchery      }
433b077aed3SPierre Pronchery
434b077aed3SPierre Pronchery      if (!EVP_MAC_final(ctx, buf, &final_l, sizeof(buf)))
435b077aed3SPierre Pronchery          goto err;
436b077aed3SPierre Pronchery
437b077aed3SPierre Pronchery      printf("Result: ");
438b077aed3SPierre Pronchery      for (i = 0; i < final_l; i++)
439b077aed3SPierre Pronchery          printf("%02X", buf[i]);
440b077aed3SPierre Pronchery      printf("\n");
441b077aed3SPierre Pronchery
442b077aed3SPierre Pronchery      EVP_MAC_CTX_free(ctx);
443b077aed3SPierre Pronchery      EVP_MAC_free(mac);
444b077aed3SPierre Pronchery      exit(0);
445b077aed3SPierre Pronchery
446b077aed3SPierre Pronchery   err:
447b077aed3SPierre Pronchery      EVP_MAC_CTX_free(ctx);
448b077aed3SPierre Pronchery      EVP_MAC_free(mac);
449b077aed3SPierre Pronchery      fprintf(stderr, "Something went wrong\n");
450b077aed3SPierre Pronchery      ERR_print_errors_fp(stderr);
451b077aed3SPierre Pronchery      exit (1);
452b077aed3SPierre Pronchery  }
453b077aed3SPierre Pronchery
454b077aed3SPierre ProncheryA run of this program, called with correct environment variables, can
455b077aed3SPierre Proncherylook like this:
456b077aed3SPierre Pronchery
457b077aed3SPierre Pronchery  $ MY_MAC=cmac MY_KEY=secret0123456789 MY_MAC_CIPHER=aes-128-cbc \
458b077aed3SPierre Pronchery    LD_LIBRARY_PATH=. ./foo < foo.c
459b077aed3SPierre Pronchery  Result: C5C06683CD9DDEF904D754505C560A4E
460b077aed3SPierre Pronchery
461b077aed3SPierre Pronchery(in this example, that program was stored in F<foo.c> and compiled to
462b077aed3SPierre ProncheryF<./foo>)
463b077aed3SPierre Pronchery
464b077aed3SPierre Pronchery=head1 SEE ALSO
465b077aed3SPierre Pronchery
466b077aed3SPierre ProncheryL<property(7)>
467b077aed3SPierre ProncheryL<OSSL_PARAM(3)>,
468b077aed3SPierre ProncheryL<EVP_MAC-BLAKE2(7)>,
469b077aed3SPierre ProncheryL<EVP_MAC-CMAC(7)>,
470b077aed3SPierre ProncheryL<EVP_MAC-GMAC(7)>,
471b077aed3SPierre ProncheryL<EVP_MAC-HMAC(7)>,
472b077aed3SPierre ProncheryL<EVP_MAC-KMAC(7)>,
473b077aed3SPierre ProncheryL<EVP_MAC-Siphash(7)>,
474b077aed3SPierre ProncheryL<EVP_MAC-Poly1305(7)>,
475b077aed3SPierre ProncheryL<provider-mac(7)>,
476b077aed3SPierre ProncheryL<life_cycle-mac(7)>
477b077aed3SPierre Pronchery
478b077aed3SPierre Pronchery=head1 HISTORY
479b077aed3SPierre Pronchery
480b077aed3SPierre ProncheryThese functions were added in OpenSSL 3.0.
481b077aed3SPierre Pronchery
482b077aed3SPierre Pronchery=head1 COPYRIGHT
483b077aed3SPierre Pronchery
484*6f1af0d7SPierre ProncheryCopyright 2018-2023 The OpenSSL Project Authors. All Rights Reserved.
485b077aed3SPierre Pronchery
486b077aed3SPierre ProncheryLicensed under the Apache License 2.0 (the "License").  You may not use
487b077aed3SPierre Proncherythis file except in compliance with the License.  You can obtain a copy
488b077aed3SPierre Proncheryin the file LICENSE in the source distribution or at
489b077aed3SPierre ProncheryL<https://www.openssl.org/source/license.html>.
490b077aed3SPierre Pronchery
491b077aed3SPierre Pronchery=cut
492