EVP_MAC.pod 15.9 KB
Newer Older
R
Richard Levitte 已提交
1 2 3 4
=pod

=head1 NAME

5
EVP_MAC, EVP_MAC_fetch, EVP_MAC_up_ref, EVP_MAC_free,
6
EVP_MAC_is_a, EVP_MAC_number, EVP_MAC_name, EVP_MAC_names_do_all,
R
Richard Levitte 已提交
7
EVP_MAC_provider, EVP_MAC_get_params, EVP_MAC_gettable_params,
8 9
EVP_MAC_CTX, EVP_MAC_CTX_new, EVP_MAC_CTX_free, EVP_MAC_CTX_dup,
EVP_MAC_CTX_mac, EVP_MAC_CTX_get_params, EVP_MAC_CTX_set_params,
10
EVP_MAC_CTX_get_mac_size, EVP_MAC_init, EVP_MAC_update, EVP_MAC_final,
11
EVP_MAC_gettable_ctx_params, EVP_MAC_settable_ctx_params,
12
EVP_MAC_CTX_gettable_params, EVP_MAC_CTX_settable_params,
13
EVP_MAC_do_all_provided - EVP MAC routines
R
Richard Levitte 已提交
14 15 16 17 18 19 20 21

=head1 SYNOPSIS

 #include <openssl/evp.h>

 typedef struct evp_mac_st EVP_MAC;
 typedef struct evp_mac_ctx_st EVP_MAC_CTX;

22
 EVP_MAC *EVP_MAC_fetch(OSSL_LIB_CTX *libctx, const char *algorithm,
23 24 25
                        const char *properties);
 int EVP_MAC_up_ref(EVP_MAC *mac);
 void EVP_MAC_free(EVP_MAC *mac);
26
 int EVP_MAC_is_a(const EVP_MAC *mac, const char *name);
27
 int EVP_MAC_number(const EVP_MAC *mac);
28
 const char *EVP_MAC_name(const EVP_MAC *mac);
29 30 31
 int EVP_MAC_names_do_all(const EVP_MAC *mac,
                          void (*fn)(const char *name, void *data),
                          void *data);
R
Richard Levitte 已提交
32
 const OSSL_PROVIDER *EVP_MAC_provider(const EVP_MAC *mac);
33 34
 int EVP_MAC_get_params(EVP_MAC *mac, OSSL_PARAM params[]);

35 36 37 38 39 40
 EVP_MAC_CTX *EVP_MAC_CTX_new(EVP_MAC *mac);
 void EVP_MAC_CTX_free(EVP_MAC_CTX *ctx);
 EVP_MAC_CTX *EVP_MAC_CTX_dup(const EVP_MAC_CTX *src);
 EVP_MAC *EVP_MAC_CTX_mac(EVP_MAC_CTX *ctx);
 int EVP_MAC_CTX_get_params(EVP_MAC_CTX *ctx, OSSL_PARAM params[]);
 int EVP_MAC_CTX_set_params(EVP_MAC_CTX *ctx, const OSSL_PARAM params[]);
41

42
 size_t EVP_MAC_CTX_get_mac_size(EVP_MAC_CTX *ctx);
43 44
 int EVP_MAC_init(EVP_MAC_CTX *ctx, const unsigned char *key, size_t keylen,
                  const OSSL_PARAM params[]);
R
Richard Levitte 已提交
45
 int EVP_MAC_update(EVP_MAC_CTX *ctx, const unsigned char *data, size_t datalen);
46 47 48 49
 int EVP_MAC_final(EVP_MAC_CTX *ctx,
                   unsigned char *out, size_t *outl, size_t outsize);

 const OSSL_PARAM *EVP_MAC_gettable_params(const EVP_MAC *mac);
50 51
 const OSSL_PARAM *EVP_MAC_gettable_ctx_params(const EVP_MAC *mac);
 const OSSL_PARAM *EVP_MAC_settable_ctx_params(const EVP_MAC *mac);
52 53
 const OSSL_PARAM *EVP_MAC_CTX_gettable_params(EVP_MAC_CTX *ctx);
 const OSSL_PARAM *EVP_MAC_CTX_settable_params(EVP_MAC_CTX *ctx);
R
Richard Levitte 已提交
54

55
 void EVP_MAC_do_all_provided(OSSL_LIB_CTX *libctx,
56 57
                              void (*fn)(EVP_MAC *mac, void *arg),
                              void *arg);
R
Richard Levitte 已提交
58

R
Richard Levitte 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
=head1 DESCRIPTION

These types and functions help the application to calculate MACs of
different types and with different underlying algorithms if there are
any.

MACs are a bit complex insofar that some of them use other algorithms
for actual computation.  HMAC uses a digest, and CMAC uses a cipher.
Therefore, there are sometimes two contexts to keep track of, one for
the MAC algorithm itself and one for the underlying computation
algorithm if there is one.

To make things less ambiguous, this manual talks about a "context" or
"MAC context", which is to denote the MAC level context, and about a
"underlying context", or "computation context", which is to denote the
context for the underlying computation algorithm if there is one.

=head2 Types

B<EVP_MAC> is a type that holds the implementation of a MAC.

B<EVP_MAC_CTX> is a context type that holds internal MAC information
as well as a reference to a computation context, for those MACs that
rely on an underlying computation algorithm.

84 85 86 87 88 89
=head2 Algorithm implementation fetching

EVP_MAC_fetch() fetches an implementation of a MAC I<algorithm>, given
a library context I<libctx> and a set of I<properties>.
See L<provider(7)/Fetching algorithms> for further information.

90 91 92
See L<OSSL_PROVIDER-default(7)/Message Authentication Code (MAC)> for the list
of algorithms supported by the default provider.

93 94 95 96 97 98 99 100 101
The returned value must eventually be freed with
L<EVP_MAC_free(3)>.

EVP_MAC_up_ref() increments the reference count of an already fetched
MAC.

EVP_MAC_free() frees a fetched algorithm.
NULL is a valid parameter, for which this function is a no-op.

R
Richard Levitte 已提交
102 103
=head2 Context manipulation functions

104
EVP_MAC_CTX_new() creates a new context for the MAC type I<mac>.
R
Richard Levitte 已提交
105 106 107
The created context can then be used with most other functions
described here.

108
EVP_MAC_CTX_free() frees the contents of the context, including an
R
Richard Levitte 已提交
109
underlying context if there is one, as well as the context itself.
110
NULL is a valid parameter, for which this function is a no-op.
R
Richard Levitte 已提交
111

112
EVP_MAC_CTX_dup() duplicates the I<src> context and returns a newly allocated
113
context.
R
Richard Levitte 已提交
114

115
EVP_MAC_CTX_mac() returns the B<EVP_MAC> associated with the context
116
I<ctx>.
R
Richard Levitte 已提交
117 118 119 120

=head2 Computing functions

EVP_MAC_init() sets up the underlying context with information given
121 122 123 124 125
via the I<key> and I<params> arguments.  The MAC I<key> has a length of
I<keylen> and the parameters in I<params> are processed before setting
the key.  If I<key> is NULL, the key must be set via params either
as part of this call or separately using EVP_MAC_CTX_set_params().
This should be called before calling EVP_MAC_update() and EVP_MAC_final().
R
Richard Levitte 已提交
126

127
EVP_MAC_update() adds I<datalen> bytes from I<data> to the MAC input.
R
Richard Levitte 已提交
128 129

EVP_MAC_final() does the final computation and stores the result in
130 131
the memory pointed at by I<out> of size I<outsize>, and sets the number
of bytes written in I<*outl> at.
R
Richard Levitte 已提交
132
If I<out> is NULL or I<outsize> is too small, then no computation
133
is made.
R
Richard Levitte 已提交
134
To figure out what the output length will be and allocate space for it
R
Richard Levitte 已提交
135
dynamically, simply call with I<out> being NULL and I<outl>
R
Richard Levitte 已提交
136
pointing at a valid location, then allocate space and make a second
137 138 139 140 141 142 143 144 145
call with I<out> pointing at the allocated space.

EVP_MAC_get_params() retrieves details about the implementation
I<mac>.
The set of parameters given with I<params> determine exactly what
parameters should be retrieved.
Note that a parameter that is unknown in the underlying context is
simply ignored.

146
EVP_MAC_CTX_get_params() retrieves chosen parameters, given the
147 148 149 150 151 152
context I<ctx> and its underlying context.
The set of parameters given with I<params> determine exactly what
parameters should be retrieved.
Note that a parameter that is unknown in the underlying context is
simply ignored.

153
EVP_MAC_CTX_set_params() passes chosen parameters to the underlying
154 155 156 157 158 159 160 161
context, given a context I<ctx>.
The set of parameters given with I<params> determine exactly what
parameters are passed down.
Note that a parameter that is unknown in the underlying context is
simply ignored.
Also, what happens when a needed parameter isn't passed down is
defined by the implementation.

162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
EVP_MAC_gettable_params() returns an B<OSSL_PARAM> array that describes
the retrievable and settable parameters.  EVP_MAC_gettable_params()
returns parameters that can be used with EVP_MAC_get_params().
See L<OSSL_PARAM(3)> for the use of B<OSSL_PARAM> as a parameter descriptor.

EVP_MAC_gettable_ctx_params() and EVP_MAC_CTX_gettable_params()
return constant B<OSSL_PARAM> arrays that describe the retrievable
parameters that can be used with EVP_MAC_CTX_get_params().
EVP_MAC_gettable_ctx_params() returns the parameters that can be retrieved
from the algorithm, whereas EVP_MAC_CTX_gettable_params() returns
the parameters that can be retrieved in the context's current state.
See L<OSSL_PARAM(3)> for the use of B<OSSL_PARAM> as a parameter descriptor.

EVP_MAC_settable_ctx_params() and EVP_MAC_CTX_settable_params() return
constant B<OSSL_PARAM> arrays that describe the settable parameters that
can be used with EVP_MAC_CTX_set_params().  EVP_MAC_settable_ctx_params()
returns the parameters that can be retrieved from the algorithm,
whereas EVP_MAC_CTX_settable_params() returns the parameters that can
be retrieved in the context's current state.  See L<OSSL_PARAM(3)>
for the use of B<OSSL_PARAM> as a parameter descriptor.
R
Richard Levitte 已提交
182 183 184

=head2 Information functions

185
EVP_MAC_CTX_get_mac_size() returns the MAC output size for the given context.
R
Richard Levitte 已提交
186

187 188 189
EVP_MAC_is_a() checks if the given I<mac> is an implementation of an
algorithm that's identifiable with I<name>.

R
Richard Levitte 已提交
190 191 192
EVP_MAC_provider() returns the provider that holds the implementation
of the given I<mac>.

193
EVP_MAC_do_all_provided() traverses all MAC implemented by all activated
R
Richard Levitte 已提交
194 195 196 197
providers in the given library context I<libctx>, and for each of the
implementations, calls the given function I<fn> with the implementation method
and the given I<arg> as argument.

198 199 200
EVP_MAC_number() returns the internal dynamic number assigned to
I<mac>.

201 202 203 204
EVP_MAC_name() return the name of the given MAC.  For fetched MACs
with multiple names, only one of them is returned; it's
recommended to use EVP_MAC_names_do_all() instead.

205 206 207
EVP_MAC_names_do_all() traverses all names for I<mac>, and calls
I<fn> with each name and I<data>.

208
=head1 PARAMETERS
R
Richard Levitte 已提交
209

210 211 212 213 214 215 216
Parameters are identified by name as strings, and have an expected
data type and maximum size.
OpenSSL has a set of macros for parameter names it expects to see in
its own MAC implementations.
Here, we show all three, the OpenSSL macro for the parameter name, the
name in string form, and a type description.

217
The standard parameter names are:
R
Richard Levitte 已提交
218 219 220

=over 4

P
Pauli 已提交
221
=item "key" (B<OSSL_MAC_PARAM_KEY>) <octet string>
R
Richard Levitte 已提交
222

223
Its value is the MAC key as an array of bytes.
R
Richard Levitte 已提交
224 225

For MACs that use an underlying computation algorithm, the algorithm
226
must be set first, see parameter names "algorithm" below.
P
Pauli 已提交
227

P
Pauli 已提交
228
=item "iv" (B<OSSL_MAC_PARAM_IV>) <octet string>
P
Pauli 已提交
229

230
Some MAC implementations require an IV, this parameter sets the IV.
S
Shane Lontis 已提交
231

P
Pauli 已提交
232
=item "custom" (B<OSSL_MAC_PARAM_CUSTOM>) <octet string>
S
Shane Lontis 已提交
233

A
Antoine Salon 已提交
234
Some MAC implementations (KMAC, BLAKE2) accept a Customization String,
235 236
this parameter sets the Customization String. The default value is the
empty string.
S
Shane Lontis 已提交
237

P
Pauli 已提交
238
=item "salt" (B<OSSL_MAC_PARAM_SALT>) <octet string>
A
Antoine Salon 已提交
239 240 241

This option is used by BLAKE2 MAC.

P
Pauli 已提交
242
=item "xof" (B<OSSL_MAC_PARAM_XOF>) <integer>
S
Shane Lontis 已提交
243

244
It's a simple flag, the value 0 or 1 are expected.
S
Shane Lontis 已提交
245 246 247

This option is used by KMAC.

248
=item "digest-noinit" (B<OSSL_MAC_PARAM_DIGEST_NOINIT>) <integer>
R
Richard Levitte 已提交
249

250 251 252 253 254 255 256 257 258 259 260
A simple flag to set the MAC digest to not initialise the
implementation specific data. The value 0 or 1 is expected.

This option is used by HMAC.

=item "digest-oneshot" (B<OSSL_MAC_PARAM_DIGEST_ONESHOT>) <integer>

A simple flag to set the MAC digest to be a oneshot operation.
The value 0 or 1 is expected.

This option is used by HMAC.
R
Richard Levitte 已提交
261

P
Pauli 已提交
262
=item "properties" (B<OSSL_MAC_PARAM_PROPERTIES>) <UTF8 string>
R
Richard Levitte 已提交
263

P
Pauli 已提交
264
=item "digest" (B<OSSL_MAC_PARAM_DIGEST>) <UTF8 string>
R
Richard Levitte 已提交
265

P
Pauli 已提交
266
=item "cipher" (B<OSSL_MAC_PARAM_CIPHER>) <UTF8 string>
267

R
Richard Levitte 已提交
268
For MAC implementations that use an underlying computation cipher or
269
digest, these parameters set what the algorithm should be.
R
Richard Levitte 已提交
270

271
The value is always the name of the intended algorithm,
R
Richard Levitte 已提交
272
or the properties.
R
Richard Levitte 已提交
273

274 275 276
Note that not all algorithms may support all digests.
HMAC does not support variable output length digests such as SHAKE128
or SHAKE256.
R
Richard Levitte 已提交
277

P
Pauli 已提交
278
=item "size" (B<OSSL_MAC_PARAM_SIZE>) <unsigned integer>
R
Richard Levitte 已提交
279 280 281

For MAC implementations that support it, set the output size that
EVP_MAC_final() should produce.
282 283
The allowed sizes vary between MAC implementations, but must never exceed
what can be given with a B<size_t>.
R
Richard Levitte 已提交
284

285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
=item "tls-data-size" (B<OSSL_MAC_PARAM_TLS_DATA_SIZE>) <unsigned integer>

This parameter is only supported by HMAC. If set then special handling is
activated for calculating the MAC of a received mac-then-encrypt TLS record
where variable length record padding has been used (as in the case of CBC mode
ciphersuites). The value represents the total length of the record that is
having the MAC calculated including the received MAC and the record padding.

When used EVP_MAC_update must be called precisely twice. The first time with
the 13 bytes of TLS "header" data, and the second time with the entire record
including the MAC itself and any padding. The entire record length must equal
the value passed in the "tls-data-size" parameter. The length passed in the
B<datalen> parameter to EVP_MAC_update() should be equal to the length of the
record after the MAC and any padding has been removed.

R
Richard Levitte 已提交
300 301
=back

302
All these parameters should be used before the calls to any of
R
Richard Levitte 已提交
303 304 305 306
EVP_MAC_init(), EVP_MAC_update() and EVP_MAC_final() for a full
computation.
Anything else may give undefined results.

307 308 309 310 311 312 313
=head1 NOTES

The MAC life-cycle is described in L<life_cycle-mac(7)>.  In the future,
the transitions described there will be enforced.  When this is done, it will
not be considered a breaking change to the API.


314
=head1 RETURN VALUES
R
Richard Levitte 已提交
315

316 317
EVP_MAC_fetch() returns a pointer to a newly fetched EVP_MAC, or
NULL if allocation failed.
R
Richard Levitte 已提交
318

319 320
EVP_MAC_up_ref() returns 1 on success, 0 on error.

321 322 323
EVP_MAC_names_do_all() returns 1 if the callback was called for all names. A
return value of 0 means that the callback was not called for any names.

324 325
EVP_MAC_free() returns nothing at all.

326 327 328
EVP_MAC_is_a() returns 1 if the given method can be identified with
the given name, otherwise 0.

329 330
EVP_MAC_name() returns a name of the MAC, or NULL on error.

R
Richard Levitte 已提交
331 332 333
EVP_MAC_provider() returns a pointer to the provider for the MAC, or
NULL on error.

334
EVP_MAC_CTX_new() and EVP_MAC_CTX_dup() return a pointer to a newly
335
created EVP_MAC_CTX, or NULL if allocation failed.
R
Richard Levitte 已提交
336

337
EVP_MAC_CTX_free() returns nothing at all.
R
Richard Levitte 已提交
338

339
EVP_MAC_CTX_get_params() and EVP_MAC_CTX_set_params() return 1 on
340
success, 0 on error.
R
Richard Levitte 已提交
341

342 343
EVP_MAC_init(), EVP_MAC_update(), and EVP_MAC_final() return 1 on success, 0
on error.
R
Richard Levitte 已提交
344

345
EVP_MAC_CTX_get_mac_size() returns the expected output size, or 0 if it isn't set.
R
Richard Levitte 已提交
346 347
If it isn't set, a call to EVP_MAC_init() should get it set.

348
EVP_MAC_do_all_provided() returns nothing at all.
R
Richard Levitte 已提交
349

350
=head1 EXAMPLES
R
Richard Levitte 已提交
351 352 353 354 355 356 357 358 359

  #include <stdlib.h>
  #include <stdio.h>
  #include <string.h>
  #include <stdarg.h>
  #include <unistd.h>

  #include <openssl/evp.h>
  #include <openssl/err.h>
360
  #include <openssl/params.h>
R
Richard Levitte 已提交
361 362

  int main() {
363 364 365
      EVP_MAC *mac = EVP_MAC_fetch(NULL, getenv("MY_MAC"), NULL);
      const char *cipher = getenv("MY_MAC_CIPHER");
      const char *digest = getenv("MY_MAC_DIGEST");
R
Richard Levitte 已提交
366 367 368 369
      const char *key = getenv("MY_KEY");
      EVP_MAC_CTX *ctx = NULL;

      unsigned char buf[4096];
370
      size_t read_l;
R
Richard Levitte 已提交
371 372 373 374
      size_t final_l;

      size_t i;

375
      OSSL_PARAM params[3];
376 377 378 379
      size_t params_n = 0;

      if (cipher != NULL)
          params[params_n++] =
380
              OSSL_PARAM_construct_utf8_string("cipher", (char*)cipher, 0);
381 382
      if (digest != NULL)
          params[params_n++] =
383
              OSSL_PARAM_construct_utf8_string("digest", (char*)digest, 0);
384 385
      params[params_n] = OSSL_PARAM_construct_end();

R
Richard Levitte 已提交
386 387
      if (mac == NULL
          || key == NULL
388
          || (ctx = EVP_MAC_CTX_new(mac)) == NULL
389 390
          || !EVP_MAC_init(ctx, (const unsigned char *)key, strlen(key),
                           params))
R
Richard Levitte 已提交
391 392
          goto err;

393
      while ( (read_l = read(STDIN_FILENO, buf, sizeof(buf))) > 0) {
R
Richard Levitte 已提交
394 395 396 397
          if (!EVP_MAC_update(ctx, buf, read_l))
              goto err;
      }

398
      if (!EVP_MAC_final(ctx, buf, &final_l, sizeof(buf)))
R
Richard Levitte 已提交
399 400 401 402 403 404 405
          goto err;

      printf("Result: ");
      for (i = 0; i < final_l; i++)
          printf("%02X", buf[i]);
      printf("\n");

406
      EVP_MAC_CTX_free(ctx);
407
      EVP_MAC_free(mac);
R
Richard Levitte 已提交
408 409 410
      exit(0);

   err:
411
      EVP_MAC_CTX_free(ctx);
412
      EVP_MAC_free(mac);
R
Richard Levitte 已提交
413 414 415 416 417 418 419 420 421 422
      fprintf(stderr, "Something went wrong\n");
      ERR_print_errors_fp(stderr);
      exit (1);
  }

A run of this program, called with correct environment variables, can
look like this:

  $ MY_MAC=cmac MY_KEY=secret0123456789 MY_MAC_CIPHER=aes-128-cbc \
    LD_LIBRARY_PATH=. ./foo < foo.c
423
  Result: C5C06683CD9DDEF904D754505C560A4E
R
Richard Levitte 已提交
424 425 426 427 428 429

(in this example, that program was stored in F<foo.c> and compiled to
F<./foo>)

=head1 SEE ALSO

430 431
L<property(7)>
L<OSSL_PARAM(3)>,
R
Rich Salz 已提交
432 433 434 435 436 437
L<EVP_MAC-BLAKE2(7)>,
L<EVP_MAC-CMAC(7)>,
L<EVP_MAC-GMAC(7)>,
L<EVP_MAC-HMAC(7)>,
L<EVP_MAC-KMAC(7)>,
L<EVP_MAC-Siphash(7)>,
438 439 440
L<EVP_MAC-Poly1305(7)>,
L<provider-mac(7)>,
L<life_cycle-mac(7)>
R
Richard Levitte 已提交
441

442 443
=head1 HISTORY

444
These functions were added in OpenSSL 3.0.
445

R
Richard Levitte 已提交
446 447
=head1 COPYRIGHT

M
Matt Caswell 已提交
448
Copyright 2018-2021 The OpenSSL Project Authors. All Rights Reserved.
R
Richard Levitte 已提交
449

450
Licensed under the Apache License 2.0 (the "License").  You may not use
R
Richard Levitte 已提交
451 452 453 454 455
this file except in compliance with the License.  You can obtain a copy
in the file LICENSE in the source distribution or at
L<https://www.openssl.org/source/license.html>.

=cut