EVP_MAC.pod 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. =pod
  2. =head1 NAME
  3. EVP_MAC, EVP_MAC_fetch, EVP_MAC_up_ref, EVP_MAC_free, EVP_MAC_is_a,
  4. EVP_MAC_get0_name, EVP_MAC_names_do_all, EVP_MAC_get0_description,
  5. EVP_MAC_get0_provider, EVP_MAC_get_params, EVP_MAC_gettable_params,
  6. EVP_MAC_CTX, EVP_MAC_CTX_new, EVP_MAC_CTX_free, EVP_MAC_CTX_dup,
  7. EVP_MAC_CTX_get0_mac, EVP_MAC_CTX_get_params, EVP_MAC_CTX_set_params,
  8. EVP_MAC_CTX_get_mac_size, EVP_MAC_CTX_get_block_size, EVP_Q_mac,
  9. EVP_MAC_init, EVP_MAC_update, EVP_MAC_final, EVP_MAC_finalXOF,
  10. EVP_MAC_gettable_ctx_params, EVP_MAC_settable_ctx_params,
  11. EVP_MAC_CTX_gettable_params, EVP_MAC_CTX_settable_params,
  12. EVP_MAC_do_all_provided - EVP MAC routines
  13. =head1 SYNOPSIS
  14. #include <openssl/evp.h>
  15. typedef struct evp_mac_st EVP_MAC;
  16. typedef struct evp_mac_ctx_st EVP_MAC_CTX;
  17. EVP_MAC *EVP_MAC_fetch(OSSL_LIB_CTX *libctx, const char *algorithm,
  18. const char *properties);
  19. int EVP_MAC_up_ref(EVP_MAC *mac);
  20. void EVP_MAC_free(EVP_MAC *mac);
  21. int EVP_MAC_is_a(const EVP_MAC *mac, const char *name);
  22. const char *EVP_MAC_get0_name(const EVP_MAC *mac);
  23. int EVP_MAC_names_do_all(const EVP_MAC *mac,
  24. void (*fn)(const char *name, void *data),
  25. void *data);
  26. const char *EVP_MAC_get0_description(const EVP_MAC *mac);
  27. const OSSL_PROVIDER *EVP_MAC_get0_provider(const EVP_MAC *mac);
  28. int EVP_MAC_get_params(EVP_MAC *mac, OSSL_PARAM params[]);
  29. EVP_MAC_CTX *EVP_MAC_CTX_new(EVP_MAC *mac);
  30. void EVP_MAC_CTX_free(EVP_MAC_CTX *ctx);
  31. EVP_MAC_CTX *EVP_MAC_CTX_dup(const EVP_MAC_CTX *src);
  32. EVP_MAC *EVP_MAC_CTX_get0_mac(EVP_MAC_CTX *ctx);
  33. int EVP_MAC_CTX_get_params(EVP_MAC_CTX *ctx, OSSL_PARAM params[]);
  34. int EVP_MAC_CTX_set_params(EVP_MAC_CTX *ctx, const OSSL_PARAM params[]);
  35. size_t EVP_MAC_CTX_get_mac_size(EVP_MAC_CTX *ctx);
  36. size_t EVP_MAC_CTX_get_block_size(EVP_MAC_CTX *ctx);
  37. unsigned char *EVP_Q_mac(OSSL_LIB_CTX *libctx, const char *name, const char *propq,
  38. const char *subalg, const OSSL_PARAM *params,
  39. const void *key, size_t keylen,
  40. const unsigned char *data, size_t datalen,
  41. unsigned char *out, size_t outsize, size_t *outlen);
  42. int EVP_MAC_init(EVP_MAC_CTX *ctx, const unsigned char *key, size_t keylen,
  43. const OSSL_PARAM params[]);
  44. int EVP_MAC_update(EVP_MAC_CTX *ctx, const unsigned char *data, size_t datalen);
  45. int EVP_MAC_final(EVP_MAC_CTX *ctx,
  46. unsigned char *out, size_t *outl, size_t outsize);
  47. int EVP_MAC_finalXOF(EVP_MAC_CTX *ctx, unsigned char *out, size_t outsize);
  48. const OSSL_PARAM *EVP_MAC_gettable_params(const EVP_MAC *mac);
  49. const OSSL_PARAM *EVP_MAC_gettable_ctx_params(const EVP_MAC *mac);
  50. const OSSL_PARAM *EVP_MAC_settable_ctx_params(const EVP_MAC *mac);
  51. const OSSL_PARAM *EVP_MAC_CTX_gettable_params(EVP_MAC_CTX *ctx);
  52. const OSSL_PARAM *EVP_MAC_CTX_settable_params(EVP_MAC_CTX *ctx);
  53. void EVP_MAC_do_all_provided(OSSL_LIB_CTX *libctx,
  54. void (*fn)(EVP_MAC *mac, void *arg),
  55. void *arg);
  56. =head1 DESCRIPTION
  57. These types and functions help the application to calculate MACs of
  58. different types and with different underlying algorithms if there are
  59. any.
  60. MACs are a bit complex insofar that some of them use other algorithms
  61. for actual computation. HMAC uses a digest, and CMAC uses a cipher.
  62. Therefore, there are sometimes two contexts to keep track of, one for
  63. the MAC algorithm itself and one for the underlying computation
  64. algorithm if there is one.
  65. To make things less ambiguous, this manual talks about a "context" or
  66. "MAC context", which is to denote the MAC level context, and about a
  67. "underlying context", or "computation context", which is to denote the
  68. context for the underlying computation algorithm if there is one.
  69. =head2 Types
  70. B<EVP_MAC> is a type that holds the implementation of a MAC.
  71. B<EVP_MAC_CTX> is a context type that holds internal MAC information
  72. as well as a reference to a computation context, for those MACs that
  73. rely on an underlying computation algorithm.
  74. =head2 Algorithm implementation fetching
  75. EVP_MAC_fetch() fetches an implementation of a MAC I<algorithm>, given
  76. a library context I<libctx> and a set of I<properties>.
  77. See L<crypto(7)/ALGORITHM FETCHING> for further information.
  78. See L<OSSL_PROVIDER-default(7)/Message Authentication Code (MAC)> for the list
  79. of algorithms supported by the default provider.
  80. The returned value must eventually be freed with
  81. L<EVP_MAC_free(3)>.
  82. EVP_MAC_up_ref() increments the reference count of an already fetched
  83. MAC.
  84. EVP_MAC_free() frees a fetched algorithm.
  85. NULL is a valid parameter, for which this function is a no-op.
  86. =head2 Context manipulation functions
  87. EVP_MAC_CTX_new() creates a new context for the MAC type I<mac>.
  88. The created context can then be used with most other functions
  89. described here.
  90. EVP_MAC_CTX_free() frees the contents of the context, including an
  91. underlying context if there is one, as well as the context itself.
  92. NULL is a valid parameter, for which this function is a no-op.
  93. EVP_MAC_CTX_dup() duplicates the I<src> context and returns a newly allocated
  94. context.
  95. EVP_MAC_CTX_get0_mac() returns the B<EVP_MAC> associated with the context
  96. I<ctx>.
  97. =head2 Computing functions
  98. EVP_Q_mac() computes the message authentication code
  99. of I<data> with length I<datalen>
  100. using the MAC algorithm I<name> and the key I<key> with length I<keylen>.
  101. The MAC algorithm is fetched using any given I<libctx> and property query
  102. string I<propq>. It takes parameters I<subalg> and further I<params>,
  103. both of which may be NULL if not needed.
  104. If I<out> is not NULL, it places the result in the memory pointed at by I<out>,
  105. but only if I<outsize> is sufficient (otherwise no computation is made).
  106. If I<out> is NULL, it allocates and uses a buffer of suitable length,
  107. which will be returned on success and must be freed by the caller.
  108. In either case, also on error,
  109. it assigns the number of bytes written to I<*outlen> unless I<outlen> is NULL.
  110. EVP_MAC_init() sets up the underlying context I<ctx> with information given
  111. via the I<key> and I<params> arguments. The MAC I<key> has a length of
  112. I<keylen> and the parameters in I<params> are processed before setting
  113. the key. If I<key> is NULL, the key must be set via I<params> either
  114. as part of this call or separately using EVP_MAC_CTX_set_params().
  115. Providing non-NULL I<params> to this function is equivalent to calling
  116. EVP_MAC_CTX_set_params() with those I<params> for the same I<ctx> beforehand.
  117. Note: There are additional requirements for some MAC algorithms during
  118. re-initalization (i.e. calling EVP_MAC_init() on an EVP_MAC after EVP_MAC_final()
  119. has been called on the same object). See the NOTES section below.
  120. EVP_MAC_init() should be called before EVP_MAC_update() and EVP_MAC_final().
  121. EVP_MAC_update() adds I<datalen> bytes from I<data> to the MAC input.
  122. EVP_MAC_final() does the final computation and stores the result in
  123. the memory pointed at by I<out> of size I<outsize>, and sets the number
  124. of bytes written in I<*outl> at.
  125. If I<out> is NULL or I<outsize> is too small, then no computation
  126. is made.
  127. To figure out what the output length will be and allocate space for it
  128. dynamically, simply call with I<out> being NULL and I<outl>
  129. pointing at a valid location, then allocate space and make a second
  130. call with I<out> pointing at the allocated space.
  131. EVP_MAC_finalXOF() does the final computation for an XOF based MAC and stores
  132. the result in the memory pointed at by I<out> of size I<outsize>.
  133. EVP_MAC_get_params() retrieves details about the implementation
  134. I<mac>.
  135. The set of parameters given with I<params> determine exactly what
  136. parameters should be retrieved.
  137. Note that a parameter that is unknown in the underlying context is
  138. simply ignored.
  139. EVP_MAC_CTX_get_params() retrieves chosen parameters, given the
  140. context I<ctx> and its underlying context.
  141. The set of parameters given with I<params> determine exactly what
  142. parameters should be retrieved.
  143. Note that a parameter that is unknown in the underlying context is
  144. simply ignored.
  145. EVP_MAC_CTX_set_params() passes chosen parameters to the underlying
  146. context, given a context I<ctx>.
  147. The set of parameters given with I<params> determine exactly what
  148. parameters are passed down.
  149. If I<params> are NULL, the underlying context should do nothing and return 1.
  150. Note that a parameter that is unknown in the underlying context is
  151. simply ignored.
  152. Also, what happens when a needed parameter isn't passed down is
  153. defined by the implementation.
  154. EVP_MAC_gettable_params() returns an L<OSSL_PARAM(3)> array that describes
  155. the retrievable and settable parameters. EVP_MAC_gettable_params()
  156. returns parameters that can be used with EVP_MAC_get_params().
  157. EVP_MAC_gettable_ctx_params() and EVP_MAC_CTX_gettable_params()
  158. return constant L<OSSL_PARAM(3)> arrays that describe the retrievable
  159. parameters that can be used with EVP_MAC_CTX_get_params().
  160. EVP_MAC_gettable_ctx_params() returns the parameters that can be retrieved
  161. from the algorithm, whereas EVP_MAC_CTX_gettable_params() returns
  162. the parameters that can be retrieved in the context's current state.
  163. EVP_MAC_settable_ctx_params() and EVP_MAC_CTX_settable_params() return
  164. constant L<OSSL_PARAM(3)> arrays that describe the settable parameters that
  165. can be used with EVP_MAC_CTX_set_params(). EVP_MAC_settable_ctx_params()
  166. returns the parameters that can be retrieved from the algorithm,
  167. whereas EVP_MAC_CTX_settable_params() returns the parameters that can
  168. be retrieved in the context's current state.
  169. =head2 Information functions
  170. EVP_MAC_CTX_get_mac_size() returns the MAC output size for the given context.
  171. EVP_MAC_CTX_get_block_size() returns the MAC block size for the given context.
  172. Not all MAC algorithms support this.
  173. EVP_MAC_is_a() checks if the given I<mac> is an implementation of an
  174. algorithm that's identifiable with I<name>.
  175. EVP_MAC_get0_provider() returns the provider that holds the implementation
  176. of the given I<mac>.
  177. EVP_MAC_do_all_provided() traverses all MAC implemented by all activated
  178. providers in the given library context I<libctx>, and for each of the
  179. implementations, calls the given function I<fn> with the implementation method
  180. and the given I<arg> as argument.
  181. EVP_MAC_get0_name() return the name of the given MAC. For fetched MACs
  182. with multiple names, only one of them is returned; it's
  183. recommended to use EVP_MAC_names_do_all() instead.
  184. EVP_MAC_names_do_all() traverses all names for I<mac>, and calls
  185. I<fn> with each name and I<data>.
  186. EVP_MAC_get0_description() returns a description of the I<mac>, meant
  187. for display and human consumption. The description is at the discretion
  188. of the mac implementation.
  189. =head1 PARAMETERS
  190. Parameters are identified by name as strings, and have an expected
  191. data type and maximum size.
  192. OpenSSL has a set of macros for parameter names it expects to see in
  193. its own MAC implementations.
  194. Here, we show all three, the OpenSSL macro for the parameter name, the
  195. name in string form, and a type description.
  196. The standard parameter names are:
  197. =over 4
  198. =item "key" (B<OSSL_MAC_PARAM_KEY>) <octet string>
  199. Its value is the MAC key as an array of bytes.
  200. For MACs that use an underlying computation algorithm, the algorithm
  201. must be set first, see parameter names "algorithm" below.
  202. =item "iv" (B<OSSL_MAC_PARAM_IV>) <octet string>
  203. Some MAC implementations (GMAC) require an IV, this parameter sets the IV.
  204. =item "custom" (B<OSSL_MAC_PARAM_CUSTOM>) <octet string>
  205. Some MAC implementations (KMAC, BLAKE2) accept a Customization String,
  206. this parameter sets the Customization String. The default value is the
  207. empty string.
  208. =item "salt" (B<OSSL_MAC_PARAM_SALT>) <octet string>
  209. This option is used by BLAKE2 MAC.
  210. =item "xof" (B<OSSL_MAC_PARAM_XOF>) <integer>
  211. It's a simple flag, the value 0 or 1 are expected.
  212. This option is used by KMAC.
  213. =item "digest-noinit" (B<OSSL_MAC_PARAM_DIGEST_NOINIT>) <integer>
  214. A simple flag to set the MAC digest to not initialise the
  215. implementation specific data. The value 0 or 1 is expected.
  216. This option is deprecated and will be removed in a future release.
  217. The option may be set, but is ignored.
  218. =item "digest-oneshot" (B<OSSL_MAC_PARAM_DIGEST_ONESHOT>) <integer>
  219. A simple flag to set the MAC digest to be a oneshot operation.
  220. The value 0 or 1 is expected.
  221. This option is deprecated and will be removed in a future release.
  222. The option may be set, but is ignored.
  223. =item "properties" (B<OSSL_MAC_PARAM_PROPERTIES>) <UTF8 string>
  224. =item "digest" (B<OSSL_MAC_PARAM_DIGEST>) <UTF8 string>
  225. =item "cipher" (B<OSSL_MAC_PARAM_CIPHER>) <UTF8 string>
  226. For MAC implementations that use an underlying computation cipher or
  227. digest, these parameters set what the algorithm should be.
  228. The value is always the name of the intended algorithm,
  229. or the properties.
  230. Note that not all algorithms may support all digests.
  231. HMAC does not support variable output length digests such as SHAKE128
  232. or SHAKE256.
  233. =item "size" (B<OSSL_MAC_PARAM_SIZE>) <unsigned integer>
  234. For MAC implementations that support it, set the output size that
  235. EVP_MAC_final() should produce.
  236. The allowed sizes vary between MAC implementations, but must never exceed
  237. what can be given with a B<size_t>.
  238. =item "tls-data-size" (B<OSSL_MAC_PARAM_TLS_DATA_SIZE>) <unsigned integer>
  239. This parameter is only supported by HMAC. If set then special handling is
  240. activated for calculating the MAC of a received mac-then-encrypt TLS record
  241. where variable length record padding has been used (as in the case of CBC mode
  242. ciphersuites). The value represents the total length of the record that is
  243. having the MAC calculated including the received MAC and the record padding.
  244. When used EVP_MAC_update must be called precisely twice. The first time with
  245. the 13 bytes of TLS "header" data, and the second time with the entire record
  246. including the MAC itself and any padding. The entire record length must equal
  247. the value passed in the "tls-data-size" parameter. The length passed in the
  248. B<datalen> parameter to EVP_MAC_update() should be equal to the length of the
  249. record after the MAC and any padding has been removed.
  250. =back
  251. All these parameters should be used before the calls to any of
  252. EVP_MAC_init(), EVP_MAC_update() and EVP_MAC_final() for a full
  253. computation.
  254. Anything else may give undefined results.
  255. =head1 NOTES
  256. The MAC life-cycle is described in L<life_cycle-mac(7)>. In the future,
  257. the transitions described there will be enforced. When this is done, it will
  258. not be considered a breaking change to the API.
  259. The usage of the parameter names "custom", "iv" and "salt" correspond to
  260. the names used in the standard where the algorithm was defined.
  261. Some MAC algorithms store internal state that cannot be extracted during
  262. re-initalization. For example GMAC cannot extract an B<IV> from the
  263. underlying CIPHER context, and so calling EVP_MAC_init() on an EVP_MAC object
  264. after EVP_MAC_final() has been called cannot reset its cipher state to what it
  265. was when the B<IV> was initially generated. For such instances, an
  266. B<OSSL_MAC_PARAM_IV> parameter must be passed with each call to EVP_MAC_init().
  267. =head1 RETURN VALUES
  268. EVP_MAC_fetch() returns a pointer to a newly fetched B<EVP_MAC>, or
  269. NULL if allocation failed.
  270. EVP_MAC_up_ref() returns 1 on success, 0 on error.
  271. EVP_MAC_names_do_all() returns 1 if the callback was called for all names. A
  272. return value of 0 means that the callback was not called for any names.
  273. EVP_MAC_free() returns nothing at all.
  274. EVP_MAC_is_a() returns 1 if the given method can be identified with
  275. the given name, otherwise 0.
  276. EVP_MAC_get0_name() returns a name of the MAC, or NULL on error.
  277. EVP_MAC_get0_provider() returns a pointer to the provider for the MAC, or
  278. NULL on error.
  279. EVP_MAC_CTX_new() and EVP_MAC_CTX_dup() return a pointer to a newly
  280. created EVP_MAC_CTX, or NULL if allocation failed.
  281. EVP_MAC_CTX_free() returns nothing at all.
  282. EVP_MAC_CTX_get_params() and EVP_MAC_CTX_set_params() return 1 on
  283. success, 0 on error.
  284. EVP_Q_mac() returns a pointer to the computed MAC value, or NULL on error.
  285. EVP_MAC_init(), EVP_MAC_update(), EVP_MAC_final(), and EVP_MAC_finalXOF()
  286. return 1 on success, 0 on error.
  287. EVP_MAC_CTX_get_mac_size() returns the expected output size, or 0 if it isn't
  288. set. If it isn't set, a call to EVP_MAC_init() will set it.
  289. EVP_MAC_CTX_get_block_size() returns the block size, or 0 if it isn't set.
  290. If it isn't set, a call to EVP_MAC_init() will set it.
  291. EVP_MAC_do_all_provided() returns nothing at all.
  292. =head1 EXAMPLES
  293. #include <stdlib.h>
  294. #include <stdio.h>
  295. #include <string.h>
  296. #include <stdarg.h>
  297. #include <unistd.h>
  298. #include <openssl/evp.h>
  299. #include <openssl/err.h>
  300. #include <openssl/params.h>
  301. int main() {
  302. EVP_MAC *mac = EVP_MAC_fetch(NULL, getenv("MY_MAC"), NULL);
  303. const char *cipher = getenv("MY_MAC_CIPHER");
  304. const char *digest = getenv("MY_MAC_DIGEST");
  305. const char *key = getenv("MY_KEY");
  306. EVP_MAC_CTX *ctx = NULL;
  307. unsigned char buf[4096];
  308. size_t read_l;
  309. size_t final_l;
  310. size_t i;
  311. OSSL_PARAM params[3];
  312. size_t params_n = 0;
  313. if (cipher != NULL)
  314. params[params_n++] =
  315. OSSL_PARAM_construct_utf8_string("cipher", (char*)cipher, 0);
  316. if (digest != NULL)
  317. params[params_n++] =
  318. OSSL_PARAM_construct_utf8_string("digest", (char*)digest, 0);
  319. params[params_n] = OSSL_PARAM_construct_end();
  320. if (mac == NULL
  321. || key == NULL
  322. || (ctx = EVP_MAC_CTX_new(mac)) == NULL
  323. || !EVP_MAC_init(ctx, (const unsigned char *)key, strlen(key),
  324. params))
  325. goto err;
  326. while ( (read_l = read(STDIN_FILENO, buf, sizeof(buf))) > 0) {
  327. if (!EVP_MAC_update(ctx, buf, read_l))
  328. goto err;
  329. }
  330. if (!EVP_MAC_final(ctx, buf, &final_l, sizeof(buf)))
  331. goto err;
  332. printf("Result: ");
  333. for (i = 0; i < final_l; i++)
  334. printf("%02X", buf[i]);
  335. printf("\n");
  336. EVP_MAC_CTX_free(ctx);
  337. EVP_MAC_free(mac);
  338. exit(0);
  339. err:
  340. EVP_MAC_CTX_free(ctx);
  341. EVP_MAC_free(mac);
  342. fprintf(stderr, "Something went wrong\n");
  343. ERR_print_errors_fp(stderr);
  344. exit (1);
  345. }
  346. A run of this program, called with correct environment variables, can
  347. look like this:
  348. $ MY_MAC=cmac MY_KEY=secret0123456789 MY_MAC_CIPHER=aes-128-cbc \
  349. LD_LIBRARY_PATH=. ./foo < foo.c
  350. Result: C5C06683CD9DDEF904D754505C560A4E
  351. (in this example, that program was stored in F<foo.c> and compiled to
  352. F<./foo>)
  353. =head1 SEE ALSO
  354. L<property(7)>
  355. L<OSSL_PARAM(3)>,
  356. L<EVP_MAC-BLAKE2(7)>,
  357. L<EVP_MAC-CMAC(7)>,
  358. L<EVP_MAC-GMAC(7)>,
  359. L<EVP_MAC-HMAC(7)>,
  360. L<EVP_MAC-KMAC(7)>,
  361. L<EVP_MAC-Siphash(7)>,
  362. L<EVP_MAC-Poly1305(7)>,
  363. L<provider-mac(7)>,
  364. L<life_cycle-mac(7)>
  365. =head1 HISTORY
  366. These functions were added in OpenSSL 3.0.
  367. =head1 COPYRIGHT
  368. Copyright 2018-2024 The OpenSSL Project Authors. All Rights Reserved.
  369. Licensed under the Apache License 2.0 (the "License"). You may not use
  370. this file except in compliance with the License. You can obtain a copy
  371. in the file LICENSE in the source distribution or at
  372. L<https://www.openssl.org/source/license.html>.
  373. =cut