xref: /freebsd/crypto/openssl/doc/man3/EVP_MAC.pod (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
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.
148*e0c4386eSCy SchubertNote: There are additional requirements for some MAC algorithms during
149*e0c4386eSCy Schubertre-initalization (i.e. calling EVP_MAC_init() on an EVP_MAC after EVP_MAC_final()
150*e0c4386eSCy Schuberthas been called on the same object).  See the NOTES section below.
151b077aed3SPierre Pronchery
152b077aed3SPierre ProncheryEVP_MAC_init() should be called before EVP_MAC_update() and EVP_MAC_final().
153b077aed3SPierre Pronchery
154b077aed3SPierre ProncheryEVP_MAC_update() adds I<datalen> bytes from I<data> to the MAC input.
155b077aed3SPierre Pronchery
156b077aed3SPierre ProncheryEVP_MAC_final() does the final computation and stores the result in
157b077aed3SPierre Proncherythe memory pointed at by I<out> of size I<outsize>, and sets the number
158b077aed3SPierre Proncheryof bytes written in I<*outl> at.
159b077aed3SPierre ProncheryIf I<out> is NULL or I<outsize> is too small, then no computation
160b077aed3SPierre Proncheryis made.
161b077aed3SPierre ProncheryTo figure out what the output length will be and allocate space for it
162b077aed3SPierre Proncherydynamically, simply call with I<out> being NULL and I<outl>
163b077aed3SPierre Proncherypointing at a valid location, then allocate space and make a second
164b077aed3SPierre Proncherycall with I<out> pointing at the allocated space.
165b077aed3SPierre Pronchery
166b077aed3SPierre ProncheryEVP_MAC_finalXOF() does the final computation for an XOF based MAC and stores
167b077aed3SPierre Proncherythe result in the memory pointed at by I<out> of size I<outsize>.
168b077aed3SPierre Pronchery
169b077aed3SPierre ProncheryEVP_MAC_get_params() retrieves details about the implementation
170b077aed3SPierre ProncheryI<mac>.
171b077aed3SPierre ProncheryThe set of parameters given with I<params> determine exactly what
172b077aed3SPierre Proncheryparameters should be retrieved.
173b077aed3SPierre ProncheryNote that a parameter that is unknown in the underlying context is
174b077aed3SPierre Proncherysimply ignored.
175b077aed3SPierre Pronchery
176b077aed3SPierre ProncheryEVP_MAC_CTX_get_params() retrieves chosen parameters, given the
177b077aed3SPierre Proncherycontext I<ctx> and its underlying context.
178b077aed3SPierre ProncheryThe set of parameters given with I<params> determine exactly what
179b077aed3SPierre Proncheryparameters should be retrieved.
180b077aed3SPierre ProncheryNote that a parameter that is unknown in the underlying context is
181b077aed3SPierre Proncherysimply ignored.
182b077aed3SPierre Pronchery
183b077aed3SPierre ProncheryEVP_MAC_CTX_set_params() passes chosen parameters to the underlying
184b077aed3SPierre Proncherycontext, given a context I<ctx>.
185b077aed3SPierre ProncheryThe set of parameters given with I<params> determine exactly what
186b077aed3SPierre Proncheryparameters are passed down.
1876f1af0d7SPierre ProncheryIf I<params> are NULL, the underlying context should do nothing and return 1.
188b077aed3SPierre ProncheryNote that a parameter that is unknown in the underlying context is
189b077aed3SPierre Proncherysimply ignored.
190b077aed3SPierre ProncheryAlso, what happens when a needed parameter isn't passed down is
191b077aed3SPierre Proncherydefined by the implementation.
192b077aed3SPierre Pronchery
193b077aed3SPierre ProncheryEVP_MAC_gettable_params() returns an L<OSSL_PARAM(3)> array that describes
194b077aed3SPierre Proncherythe retrievable and settable parameters.  EVP_MAC_gettable_params()
195b077aed3SPierre Proncheryreturns parameters that can be used with EVP_MAC_get_params().
196b077aed3SPierre Pronchery
197b077aed3SPierre ProncheryEVP_MAC_gettable_ctx_params() and EVP_MAC_CTX_gettable_params()
198b077aed3SPierre Proncheryreturn constant L<OSSL_PARAM(3)> arrays that describe the retrievable
199b077aed3SPierre Proncheryparameters that can be used with EVP_MAC_CTX_get_params().
200b077aed3SPierre ProncheryEVP_MAC_gettable_ctx_params() returns the parameters that can be retrieved
201b077aed3SPierre Proncheryfrom the algorithm, whereas EVP_MAC_CTX_gettable_params() returns
202b077aed3SPierre Proncherythe parameters that can be retrieved in the context's current state.
203b077aed3SPierre Pronchery
204b077aed3SPierre ProncheryEVP_MAC_settable_ctx_params() and EVP_MAC_CTX_settable_params() return
205b077aed3SPierre Proncheryconstant L<OSSL_PARAM(3)> arrays that describe the settable parameters that
206b077aed3SPierre Proncherycan be used with EVP_MAC_CTX_set_params().  EVP_MAC_settable_ctx_params()
207b077aed3SPierre Proncheryreturns the parameters that can be retrieved from the algorithm,
208b077aed3SPierre Proncherywhereas EVP_MAC_CTX_settable_params() returns the parameters that can
209b077aed3SPierre Proncherybe retrieved in the context's current state.
210b077aed3SPierre Pronchery
211b077aed3SPierre Pronchery=head2 Information functions
212b077aed3SPierre Pronchery
213b077aed3SPierre ProncheryEVP_MAC_CTX_get_mac_size() returns the MAC output size for the given context.
214b077aed3SPierre Pronchery
215b077aed3SPierre ProncheryEVP_MAC_CTX_get_block_size() returns the MAC block size for the given context.
216b077aed3SPierre ProncheryNot all MAC algorithms support this.
217b077aed3SPierre Pronchery
218b077aed3SPierre ProncheryEVP_MAC_is_a() checks if the given I<mac> is an implementation of an
219b077aed3SPierre Proncheryalgorithm that's identifiable with I<name>.
220b077aed3SPierre Pronchery
221b077aed3SPierre ProncheryEVP_MAC_get0_provider() returns the provider that holds the implementation
222b077aed3SPierre Proncheryof the given I<mac>.
223b077aed3SPierre Pronchery
224b077aed3SPierre ProncheryEVP_MAC_do_all_provided() traverses all MAC implemented by all activated
225b077aed3SPierre Proncheryproviders in the given library context I<libctx>, and for each of the
226b077aed3SPierre Proncheryimplementations, calls the given function I<fn> with the implementation method
227b077aed3SPierre Proncheryand the given I<arg> as argument.
228b077aed3SPierre Pronchery
229b077aed3SPierre ProncheryEVP_MAC_get0_name() return the name of the given MAC.  For fetched MACs
230b077aed3SPierre Proncherywith multiple names, only one of them is returned; it's
231b077aed3SPierre Proncheryrecommended to use EVP_MAC_names_do_all() instead.
232b077aed3SPierre Pronchery
233b077aed3SPierre ProncheryEVP_MAC_names_do_all() traverses all names for I<mac>, and calls
234b077aed3SPierre ProncheryI<fn> with each name and I<data>.
235b077aed3SPierre Pronchery
236b077aed3SPierre ProncheryEVP_MAC_get0_description() returns a description of the I<mac>, meant
237b077aed3SPierre Proncheryfor display and human consumption.  The description is at the discretion
238b077aed3SPierre Proncheryof the mac implementation.
239b077aed3SPierre Pronchery
240b077aed3SPierre Pronchery=head1 PARAMETERS
241b077aed3SPierre Pronchery
242b077aed3SPierre ProncheryParameters are identified by name as strings, and have an expected
243b077aed3SPierre Proncherydata type and maximum size.
244b077aed3SPierre ProncheryOpenSSL has a set of macros for parameter names it expects to see in
245b077aed3SPierre Proncheryits own MAC implementations.
246b077aed3SPierre ProncheryHere, we show all three, the OpenSSL macro for the parameter name, the
247b077aed3SPierre Proncheryname in string form, and a type description.
248b077aed3SPierre Pronchery
249b077aed3SPierre ProncheryThe standard parameter names are:
250b077aed3SPierre Pronchery
251b077aed3SPierre Pronchery=over 4
252b077aed3SPierre Pronchery
253b077aed3SPierre Pronchery=item "key" (B<OSSL_MAC_PARAM_KEY>) <octet string>
254b077aed3SPierre Pronchery
255b077aed3SPierre ProncheryIts value is the MAC key as an array of bytes.
256b077aed3SPierre Pronchery
257b077aed3SPierre ProncheryFor MACs that use an underlying computation algorithm, the algorithm
258b077aed3SPierre Proncherymust be set first, see parameter names "algorithm" below.
259b077aed3SPierre Pronchery
260b077aed3SPierre Pronchery=item "iv" (B<OSSL_MAC_PARAM_IV>) <octet string>
261b077aed3SPierre Pronchery
262b077aed3SPierre ProncherySome MAC implementations (GMAC) require an IV, this parameter sets the IV.
263b077aed3SPierre Pronchery
264b077aed3SPierre Pronchery=item "custom" (B<OSSL_MAC_PARAM_CUSTOM>) <octet string>
265b077aed3SPierre Pronchery
266b077aed3SPierre ProncherySome MAC implementations (KMAC, BLAKE2) accept a Customization String,
267b077aed3SPierre Proncherythis parameter sets the Customization String. The default value is the
268b077aed3SPierre Proncheryempty string.
269b077aed3SPierre Pronchery
270b077aed3SPierre Pronchery=item "salt" (B<OSSL_MAC_PARAM_SALT>) <octet string>
271b077aed3SPierre Pronchery
272b077aed3SPierre ProncheryThis option is used by BLAKE2 MAC.
273b077aed3SPierre Pronchery
274b077aed3SPierre Pronchery=item "xof" (B<OSSL_MAC_PARAM_XOF>) <integer>
275b077aed3SPierre Pronchery
276b077aed3SPierre ProncheryIt's a simple flag, the value 0 or 1 are expected.
277b077aed3SPierre Pronchery
278b077aed3SPierre ProncheryThis option is used by KMAC.
279b077aed3SPierre Pronchery
280b077aed3SPierre Pronchery=item "digest-noinit" (B<OSSL_MAC_PARAM_DIGEST_NOINIT>) <integer>
281b077aed3SPierre Pronchery
282b077aed3SPierre ProncheryA simple flag to set the MAC digest to not initialise the
283b077aed3SPierre Proncheryimplementation specific data. The value 0 or 1 is expected.
284b077aed3SPierre Pronchery
285b077aed3SPierre ProncheryThis option is used by HMAC.
286b077aed3SPierre Pronchery
287b077aed3SPierre Pronchery=item "digest-oneshot" (B<OSSL_MAC_PARAM_DIGEST_ONESHOT>) <integer>
288b077aed3SPierre Pronchery
289b077aed3SPierre ProncheryA simple flag to set the MAC digest to be a oneshot operation.
290b077aed3SPierre ProncheryThe value 0 or 1 is expected.
291b077aed3SPierre Pronchery
292b077aed3SPierre ProncheryThis option is used by HMAC.
293b077aed3SPierre Pronchery
294b077aed3SPierre Pronchery=item "properties" (B<OSSL_MAC_PARAM_PROPERTIES>) <UTF8 string>
295b077aed3SPierre Pronchery
296b077aed3SPierre Pronchery=item "digest" (B<OSSL_MAC_PARAM_DIGEST>) <UTF8 string>
297b077aed3SPierre Pronchery
298b077aed3SPierre Pronchery=item "cipher" (B<OSSL_MAC_PARAM_CIPHER>) <UTF8 string>
299b077aed3SPierre Pronchery
300b077aed3SPierre ProncheryFor MAC implementations that use an underlying computation cipher or
301b077aed3SPierre Proncherydigest, these parameters set what the algorithm should be.
302b077aed3SPierre Pronchery
303b077aed3SPierre ProncheryThe value is always the name of the intended algorithm,
304b077aed3SPierre Proncheryor the properties.
305b077aed3SPierre Pronchery
306b077aed3SPierre ProncheryNote that not all algorithms may support all digests.
307b077aed3SPierre ProncheryHMAC does not support variable output length digests such as SHAKE128
308b077aed3SPierre Proncheryor SHAKE256.
309b077aed3SPierre Pronchery
310b077aed3SPierre Pronchery=item "size" (B<OSSL_MAC_PARAM_SIZE>) <unsigned integer>
311b077aed3SPierre Pronchery
312b077aed3SPierre ProncheryFor MAC implementations that support it, set the output size that
313b077aed3SPierre ProncheryEVP_MAC_final() should produce.
314b077aed3SPierre ProncheryThe allowed sizes vary between MAC implementations, but must never exceed
315b077aed3SPierre Proncherywhat can be given with a B<size_t>.
316b077aed3SPierre Pronchery
317b077aed3SPierre Pronchery=item "tls-data-size" (B<OSSL_MAC_PARAM_TLS_DATA_SIZE>) <unsigned integer>
318b077aed3SPierre Pronchery
319b077aed3SPierre ProncheryThis parameter is only supported by HMAC. If set then special handling is
320b077aed3SPierre Proncheryactivated for calculating the MAC of a received mac-then-encrypt TLS record
321b077aed3SPierre Proncherywhere variable length record padding has been used (as in the case of CBC mode
322b077aed3SPierre Proncheryciphersuites). The value represents the total length of the record that is
323b077aed3SPierre Proncheryhaving the MAC calculated including the received MAC and the record padding.
324b077aed3SPierre Pronchery
325b077aed3SPierre ProncheryWhen used EVP_MAC_update must be called precisely twice. The first time with
326b077aed3SPierre Proncherythe 13 bytes of TLS "header" data, and the second time with the entire record
327b077aed3SPierre Proncheryincluding the MAC itself and any padding. The entire record length must equal
328b077aed3SPierre Proncherythe value passed in the "tls-data-size" parameter. The length passed in the
329b077aed3SPierre ProncheryB<datalen> parameter to EVP_MAC_update() should be equal to the length of the
330b077aed3SPierre Proncheryrecord after the MAC and any padding has been removed.
331b077aed3SPierre Pronchery
332b077aed3SPierre Pronchery=back
333b077aed3SPierre Pronchery
334b077aed3SPierre ProncheryAll these parameters should be used before the calls to any of
335b077aed3SPierre ProncheryEVP_MAC_init(), EVP_MAC_update() and EVP_MAC_final() for a full
336b077aed3SPierre Proncherycomputation.
337b077aed3SPierre ProncheryAnything else may give undefined results.
338b077aed3SPierre Pronchery
339b077aed3SPierre Pronchery=head1 NOTES
340b077aed3SPierre Pronchery
341b077aed3SPierre ProncheryThe MAC life-cycle is described in L<life_cycle-mac(7)>.  In the future,
342b077aed3SPierre Proncherythe transitions described there will be enforced.  When this is done, it will
343b077aed3SPierre Proncherynot be considered a breaking change to the API.
344b077aed3SPierre Pronchery
345b077aed3SPierre ProncheryThe usage of the parameter names "custom", "iv" and "salt" correspond to
346b077aed3SPierre Proncherythe names used in the standard where the algorithm was defined.
347b077aed3SPierre Pronchery
348*e0c4386eSCy SchubertSome MAC algorithms store internal state that cannot be extracted during
349*e0c4386eSCy Schubertre-initalization.  For example GMAC cannot extract an B<IV> from the
350*e0c4386eSCy Schubertunderlying CIPHER context, and so calling EVP_MAC_init() on an EVP_MAC object
351*e0c4386eSCy Schubertafter EVP_MAC_final() has been called cannot reset its cipher state to what it
352*e0c4386eSCy Schubertwas when the B<IV> was initially generated.  For such instances, an
353*e0c4386eSCy SchubertB<OSSL_MAC_PARAM_IV> parameter must be passed with each call to EVP_MAC_init().
354*e0c4386eSCy Schubert
355b077aed3SPierre Pronchery=head1 RETURN VALUES
356b077aed3SPierre Pronchery
357b077aed3SPierre ProncheryEVP_MAC_fetch() returns a pointer to a newly fetched B<EVP_MAC>, or
358b077aed3SPierre ProncheryNULL if allocation failed.
359b077aed3SPierre Pronchery
360b077aed3SPierre ProncheryEVP_MAC_up_ref() returns 1 on success, 0 on error.
361b077aed3SPierre Pronchery
362b077aed3SPierre ProncheryEVP_MAC_names_do_all() returns 1 if the callback was called for all names. A
363b077aed3SPierre Proncheryreturn value of 0 means that the callback was not called for any names.
364b077aed3SPierre Pronchery
365b077aed3SPierre ProncheryEVP_MAC_free() returns nothing at all.
366b077aed3SPierre Pronchery
367b077aed3SPierre ProncheryEVP_MAC_is_a() returns 1 if the given method can be identified with
368b077aed3SPierre Proncherythe given name, otherwise 0.
369b077aed3SPierre Pronchery
370b077aed3SPierre ProncheryEVP_MAC_get0_name() returns a name of the MAC, or NULL on error.
371b077aed3SPierre Pronchery
372b077aed3SPierre ProncheryEVP_MAC_get0_provider() returns a pointer to the provider for the MAC, or
373b077aed3SPierre ProncheryNULL on error.
374b077aed3SPierre Pronchery
375b077aed3SPierre ProncheryEVP_MAC_CTX_new() and EVP_MAC_CTX_dup() return a pointer to a newly
376b077aed3SPierre Proncherycreated EVP_MAC_CTX, or NULL if allocation failed.
377b077aed3SPierre Pronchery
378b077aed3SPierre ProncheryEVP_MAC_CTX_free() returns nothing at all.
379b077aed3SPierre Pronchery
380b077aed3SPierre ProncheryEVP_MAC_CTX_get_params() and EVP_MAC_CTX_set_params() return 1 on
381b077aed3SPierre Proncherysuccess, 0 on error.
382b077aed3SPierre Pronchery
383b077aed3SPierre ProncheryEVP_Q_mac() returns a pointer to the computed MAC value, or NULL on error.
384b077aed3SPierre Pronchery
385b077aed3SPierre ProncheryEVP_MAC_init(), EVP_MAC_update(), EVP_MAC_final(), and EVP_MAC_finalXOF()
386b077aed3SPierre Proncheryreturn 1 on success, 0 on error.
387b077aed3SPierre Pronchery
388b077aed3SPierre ProncheryEVP_MAC_CTX_get_mac_size() returns the expected output size, or 0 if it isn't
389b077aed3SPierre Proncheryset.  If it isn't set, a call to EVP_MAC_init() will set it.
390b077aed3SPierre Pronchery
391b077aed3SPierre ProncheryEVP_MAC_CTX_get_block_size() returns the block size, or 0 if it isn't set.
392b077aed3SPierre ProncheryIf it isn't set, a call to EVP_MAC_init() will set it.
393b077aed3SPierre Pronchery
394b077aed3SPierre ProncheryEVP_MAC_do_all_provided() returns nothing at all.
395b077aed3SPierre Pronchery
396b077aed3SPierre Pronchery=head1 EXAMPLES
397b077aed3SPierre Pronchery
398b077aed3SPierre Pronchery  #include <stdlib.h>
399b077aed3SPierre Pronchery  #include <stdio.h>
400b077aed3SPierre Pronchery  #include <string.h>
401b077aed3SPierre Pronchery  #include <stdarg.h>
402b077aed3SPierre Pronchery  #include <unistd.h>
403b077aed3SPierre Pronchery
404b077aed3SPierre Pronchery  #include <openssl/evp.h>
405b077aed3SPierre Pronchery  #include <openssl/err.h>
406b077aed3SPierre Pronchery  #include <openssl/params.h>
407b077aed3SPierre Pronchery
408b077aed3SPierre Pronchery  int main() {
409b077aed3SPierre Pronchery      EVP_MAC *mac = EVP_MAC_fetch(NULL, getenv("MY_MAC"), NULL);
410b077aed3SPierre Pronchery      const char *cipher = getenv("MY_MAC_CIPHER");
411b077aed3SPierre Pronchery      const char *digest = getenv("MY_MAC_DIGEST");
412b077aed3SPierre Pronchery      const char *key = getenv("MY_KEY");
413b077aed3SPierre Pronchery      EVP_MAC_CTX *ctx = NULL;
414b077aed3SPierre Pronchery
415b077aed3SPierre Pronchery      unsigned char buf[4096];
416b077aed3SPierre Pronchery      size_t read_l;
417b077aed3SPierre Pronchery      size_t final_l;
418b077aed3SPierre Pronchery
419b077aed3SPierre Pronchery      size_t i;
420b077aed3SPierre Pronchery
421b077aed3SPierre Pronchery      OSSL_PARAM params[3];
422b077aed3SPierre Pronchery      size_t params_n = 0;
423b077aed3SPierre Pronchery
424b077aed3SPierre Pronchery      if (cipher != NULL)
425b077aed3SPierre Pronchery          params[params_n++] =
426b077aed3SPierre Pronchery              OSSL_PARAM_construct_utf8_string("cipher", (char*)cipher, 0);
427b077aed3SPierre Pronchery      if (digest != NULL)
428b077aed3SPierre Pronchery          params[params_n++] =
429b077aed3SPierre Pronchery              OSSL_PARAM_construct_utf8_string("digest", (char*)digest, 0);
430b077aed3SPierre Pronchery      params[params_n] = OSSL_PARAM_construct_end();
431b077aed3SPierre Pronchery
432b077aed3SPierre Pronchery      if (mac == NULL
433b077aed3SPierre Pronchery          || key == NULL
434b077aed3SPierre Pronchery          || (ctx = EVP_MAC_CTX_new(mac)) == NULL
435b077aed3SPierre Pronchery          || !EVP_MAC_init(ctx, (const unsigned char *)key, strlen(key),
436b077aed3SPierre Pronchery                           params))
437b077aed3SPierre Pronchery          goto err;
438b077aed3SPierre Pronchery
439b077aed3SPierre Pronchery      while ( (read_l = read(STDIN_FILENO, buf, sizeof(buf))) > 0) {
440b077aed3SPierre Pronchery          if (!EVP_MAC_update(ctx, buf, read_l))
441b077aed3SPierre Pronchery              goto err;
442b077aed3SPierre Pronchery      }
443b077aed3SPierre Pronchery
444b077aed3SPierre Pronchery      if (!EVP_MAC_final(ctx, buf, &final_l, sizeof(buf)))
445b077aed3SPierre Pronchery          goto err;
446b077aed3SPierre Pronchery
447b077aed3SPierre Pronchery      printf("Result: ");
448b077aed3SPierre Pronchery      for (i = 0; i < final_l; i++)
449b077aed3SPierre Pronchery          printf("%02X", buf[i]);
450b077aed3SPierre Pronchery      printf("\n");
451b077aed3SPierre Pronchery
452b077aed3SPierre Pronchery      EVP_MAC_CTX_free(ctx);
453b077aed3SPierre Pronchery      EVP_MAC_free(mac);
454b077aed3SPierre Pronchery      exit(0);
455b077aed3SPierre Pronchery
456b077aed3SPierre Pronchery   err:
457b077aed3SPierre Pronchery      EVP_MAC_CTX_free(ctx);
458b077aed3SPierre Pronchery      EVP_MAC_free(mac);
459b077aed3SPierre Pronchery      fprintf(stderr, "Something went wrong\n");
460b077aed3SPierre Pronchery      ERR_print_errors_fp(stderr);
461b077aed3SPierre Pronchery      exit (1);
462b077aed3SPierre Pronchery  }
463b077aed3SPierre Pronchery
464b077aed3SPierre ProncheryA run of this program, called with correct environment variables, can
465b077aed3SPierre Proncherylook like this:
466b077aed3SPierre Pronchery
467b077aed3SPierre Pronchery  $ MY_MAC=cmac MY_KEY=secret0123456789 MY_MAC_CIPHER=aes-128-cbc \
468b077aed3SPierre Pronchery    LD_LIBRARY_PATH=. ./foo < foo.c
469b077aed3SPierre Pronchery  Result: C5C06683CD9DDEF904D754505C560A4E
470b077aed3SPierre Pronchery
471b077aed3SPierre Pronchery(in this example, that program was stored in F<foo.c> and compiled to
472b077aed3SPierre ProncheryF<./foo>)
473b077aed3SPierre Pronchery
474b077aed3SPierre Pronchery=head1 SEE ALSO
475b077aed3SPierre Pronchery
476b077aed3SPierre ProncheryL<property(7)>
477b077aed3SPierre ProncheryL<OSSL_PARAM(3)>,
478b077aed3SPierre ProncheryL<EVP_MAC-BLAKE2(7)>,
479b077aed3SPierre ProncheryL<EVP_MAC-CMAC(7)>,
480b077aed3SPierre ProncheryL<EVP_MAC-GMAC(7)>,
481b077aed3SPierre ProncheryL<EVP_MAC-HMAC(7)>,
482b077aed3SPierre ProncheryL<EVP_MAC-KMAC(7)>,
483b077aed3SPierre ProncheryL<EVP_MAC-Siphash(7)>,
484b077aed3SPierre ProncheryL<EVP_MAC-Poly1305(7)>,
485b077aed3SPierre ProncheryL<provider-mac(7)>,
486b077aed3SPierre ProncheryL<life_cycle-mac(7)>
487b077aed3SPierre Pronchery
488b077aed3SPierre Pronchery=head1 HISTORY
489b077aed3SPierre Pronchery
490b077aed3SPierre ProncheryThese functions were added in OpenSSL 3.0.
491b077aed3SPierre Pronchery
492b077aed3SPierre Pronchery=head1 COPYRIGHT
493b077aed3SPierre Pronchery
494*e0c4386eSCy SchubertCopyright 2018-2024 The OpenSSL Project Authors. All Rights Reserved.
495b077aed3SPierre Pronchery
496b077aed3SPierre ProncheryLicensed under the Apache License 2.0 (the "License").  You may not use
497b077aed3SPierre Proncherythis file except in compliance with the License.  You can obtain a copy
498b077aed3SPierre Proncheryin the file LICENSE in the source distribution or at
499b077aed3SPierre ProncheryL<https://www.openssl.org/source/license.html>.
500b077aed3SPierre Pronchery
501b077aed3SPierre Pronchery=cut
502