EVP_MAC.pod 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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. EVP_MAC_init() should be called before EVP_MAC_update() and EVP_MAC_final().
  118. EVP_MAC_update() adds I<datalen> bytes from I<data> to the MAC input.
  119. EVP_MAC_final() does the final computation and stores the result in
  120. the memory pointed at by I<out> of size I<outsize>, and sets the number
  121. of bytes written in I<*outl> at.
  122. If I<out> is NULL or I<outsize> is too small, then no computation
  123. is made.
  124. To figure out what the output length will be and allocate space for it
  125. dynamically, simply call with I<out> being NULL and I<outl>
  126. pointing at a valid location, then allocate space and make a second
  127. call with I<out> pointing at the allocated space.
  128. EVP_MAC_finalXOF() does the final computation for an XOF based MAC and stores
  129. the result in the memory pointed at by I<out> of size I<outsize>.
  130. EVP_MAC_get_params() retrieves details about the implementation
  131. I<mac>.
  132. The set of parameters given with I<params> determine exactly what
  133. parameters should be retrieved.
  134. Note that a parameter that is unknown in the underlying context is
  135. simply ignored.
  136. EVP_MAC_CTX_get_params() retrieves chosen parameters, given the
  137. context I<ctx> and its underlying context.
  138. The set of parameters given with I<params> determine exactly what
  139. parameters should be retrieved.
  140. Note that a parameter that is unknown in the underlying context is
  141. simply ignored.
  142. EVP_MAC_CTX_set_params() passes chosen parameters to the underlying
  143. context, given a context I<ctx>.
  144. The set of parameters given with I<params> determine exactly what
  145. parameters are passed down.
  146. If I<params> are NULL, the underlying context should do nothing and return 1.
  147. Note that a parameter that is unknown in the underlying context is
  148. simply ignored.
  149. Also, what happens when a needed parameter isn't passed down is
  150. defined by the implementation.
  151. EVP_MAC_gettable_params() returns an L<OSSL_PARAM(3)> array that describes
  152. the retrievable and settable parameters. EVP_MAC_gettable_params()
  153. returns parameters that can be used with EVP_MAC_get_params().
  154. EVP_MAC_gettable_ctx_params() and EVP_MAC_CTX_gettable_params()
  155. return constant L<OSSL_PARAM(3)> arrays that describe the retrievable
  156. parameters that can be used with EVP_MAC_CTX_get_params().
  157. EVP_MAC_gettable_ctx_params() returns the parameters that can be retrieved
  158. from the algorithm, whereas EVP_MAC_CTX_gettable_params() returns
  159. the parameters that can be retrieved in the context's current state.
  160. EVP_MAC_settable_ctx_params() and EVP_MAC_CTX_settable_params() return
  161. constant L<OSSL_PARAM(3)> arrays that describe the settable parameters that
  162. can be used with EVP_MAC_CTX_set_params(). EVP_MAC_settable_ctx_params()
  163. returns the parameters that can be retrieved from the algorithm,
  164. whereas EVP_MAC_CTX_settable_params() returns the parameters that can
  165. be retrieved in the context's current state.
  166. =head2 Information functions
  167. EVP_MAC_CTX_get_mac_size() returns the MAC output size for the given context.
  168. EVP_MAC_CTX_get_block_size() returns the MAC block size for the given context.
  169. Not all MAC algorithms support this.
  170. EVP_MAC_is_a() checks if the given I<mac> is an implementation of an
  171. algorithm that's identifiable with I<name>.
  172. EVP_MAC_get0_provider() returns the provider that holds the implementation
  173. of the given I<mac>.
  174. EVP_MAC_do_all_provided() traverses all MAC implemented by all activated
  175. providers in the given library context I<libctx>, and for each of the
  176. implementations, calls the given function I<fn> with the implementation method
  177. and the given I<arg> as argument.
  178. EVP_MAC_get0_name() return the name of the given MAC. For fetched MACs
  179. with multiple names, only one of them is returned; it's
  180. recommended to use EVP_MAC_names_do_all() instead.
  181. EVP_MAC_names_do_all() traverses all names for I<mac>, and calls
  182. I<fn> with each name and I<data>.
  183. EVP_MAC_get0_description() returns a description of the I<mac>, meant
  184. for display and human consumption. The description is at the discretion
  185. of the mac implementation.
  186. =head1 PARAMETERS
  187. Parameters are identified by name as strings, and have an expected
  188. data type and maximum size.
  189. OpenSSL has a set of macros for parameter names it expects to see in
  190. its own MAC implementations.
  191. Here, we show all three, the OpenSSL macro for the parameter name, the
  192. name in string form, and a type description.
  193. The standard parameter names are:
  194. =over 4
  195. =item "key" (B<OSSL_MAC_PARAM_KEY>) <octet string>
  196. Its value is the MAC key as an array of bytes.
  197. For MACs that use an underlying computation algorithm, the algorithm
  198. must be set first, see parameter names "algorithm" below.
  199. =item "iv" (B<OSSL_MAC_PARAM_IV>) <octet string>
  200. Some MAC implementations (GMAC) require an IV, this parameter sets the IV.
  201. =item "custom" (B<OSSL_MAC_PARAM_CUSTOM>) <octet string>
  202. Some MAC implementations (KMAC, BLAKE2) accept a Customization String,
  203. this parameter sets the Customization String. The default value is the
  204. empty string.
  205. =item "salt" (B<OSSL_MAC_PARAM_SALT>) <octet string>
  206. This option is used by BLAKE2 MAC.
  207. =item "xof" (B<OSSL_MAC_PARAM_XOF>) <integer>
  208. It's a simple flag, the value 0 or 1 are expected.
  209. This option is used by KMAC.
  210. =item "digest-noinit" (B<OSSL_MAC_PARAM_DIGEST_NOINIT>) <integer>
  211. A simple flag to set the MAC digest to not initialise the
  212. implementation specific data. The value 0 or 1 is expected.
  213. This option is used by HMAC.
  214. =item "digest-oneshot" (B<OSSL_MAC_PARAM_DIGEST_ONESHOT>) <integer>
  215. A simple flag to set the MAC digest to be a oneshot operation.
  216. The value 0 or 1 is expected.
  217. This option is used by HMAC.
  218. =item "properties" (B<OSSL_MAC_PARAM_PROPERTIES>) <UTF8 string>
  219. =item "digest" (B<OSSL_MAC_PARAM_DIGEST>) <UTF8 string>
  220. =item "cipher" (B<OSSL_MAC_PARAM_CIPHER>) <UTF8 string>
  221. For MAC implementations that use an underlying computation cipher or
  222. digest, these parameters set what the algorithm should be.
  223. The value is always the name of the intended algorithm,
  224. or the properties.
  225. Note that not all algorithms may support all digests.
  226. HMAC does not support variable output length digests such as SHAKE128
  227. or SHAKE256.
  228. =item "size" (B<OSSL_MAC_PARAM_SIZE>) <unsigned integer>
  229. For MAC implementations that support it, set the output size that
  230. EVP_MAC_final() should produce.
  231. The allowed sizes vary between MAC implementations, but must never exceed
  232. what can be given with a B<size_t>.
  233. =item "tls-data-size" (B<OSSL_MAC_PARAM_TLS_DATA_SIZE>) <unsigned integer>
  234. This parameter is only supported by HMAC. If set then special handling is
  235. activated for calculating the MAC of a received mac-then-encrypt TLS record
  236. where variable length record padding has been used (as in the case of CBC mode
  237. ciphersuites). The value represents the total length of the record that is
  238. having the MAC calculated including the received MAC and the record padding.
  239. When used EVP_MAC_update must be called precisely twice. The first time with
  240. the 13 bytes of TLS "header" data, and the second time with the entire record
  241. including the MAC itself and any padding. The entire record length must equal
  242. the value passed in the "tls-data-size" parameter. The length passed in the
  243. B<datalen> parameter to EVP_MAC_update() should be equal to the length of the
  244. record after the MAC and any padding has been removed.
  245. =back
  246. All these parameters should be used before the calls to any of
  247. EVP_MAC_init(), EVP_MAC_update() and EVP_MAC_final() for a full
  248. computation.
  249. Anything else may give undefined results.
  250. =head1 NOTES
  251. The MAC life-cycle is described in L<life_cycle-mac(7)>. In the future,
  252. the transitions described there will be enforced. When this is done, it will
  253. not be considered a breaking change to the API.
  254. The usage of the parameter names "custom", "iv" and "salt" correspond to
  255. the names used in the standard where the algorithm was defined.
  256. =head1 RETURN VALUES
  257. EVP_MAC_fetch() returns a pointer to a newly fetched B<EVP_MAC>, or
  258. NULL if allocation failed.
  259. EVP_MAC_up_ref() returns 1 on success, 0 on error.
  260. EVP_MAC_names_do_all() returns 1 if the callback was called for all names. A
  261. return value of 0 means that the callback was not called for any names.
  262. EVP_MAC_free() returns nothing at all.
  263. EVP_MAC_is_a() returns 1 if the given method can be identified with
  264. the given name, otherwise 0.
  265. EVP_MAC_get0_name() returns a name of the MAC, or NULL on error.
  266. EVP_MAC_get0_provider() returns a pointer to the provider for the MAC, or
  267. NULL on error.
  268. EVP_MAC_CTX_new() and EVP_MAC_CTX_dup() return a pointer to a newly
  269. created EVP_MAC_CTX, or NULL if allocation failed.
  270. EVP_MAC_CTX_free() returns nothing at all.
  271. EVP_MAC_CTX_get_params() and EVP_MAC_CTX_set_params() return 1 on
  272. success, 0 on error.
  273. EVP_Q_mac() returns a pointer to the computed MAC value, or NULL on error.
  274. EVP_MAC_init(), EVP_MAC_update(), EVP_MAC_final(), and EVP_MAC_finalXOF()
  275. return 1 on success, 0 on error.
  276. EVP_MAC_CTX_get_mac_size() returns the expected output size, or 0 if it isn't
  277. set. If it isn't set, a call to EVP_MAC_init() will set it.
  278. EVP_MAC_CTX_get_block_size() returns the block size, or 0 if it isn't set.
  279. If it isn't set, a call to EVP_MAC_init() will set it.
  280. EVP_MAC_do_all_provided() returns nothing at all.
  281. =head1 EXAMPLES
  282. #include <stdlib.h>
  283. #include <stdio.h>
  284. #include <string.h>
  285. #include <stdarg.h>
  286. #include <unistd.h>
  287. #include <openssl/evp.h>
  288. #include <openssl/err.h>
  289. #include <openssl/params.h>
  290. int main() {
  291. EVP_MAC *mac = EVP_MAC_fetch(NULL, getenv("MY_MAC"), NULL);
  292. const char *cipher = getenv("MY_MAC_CIPHER");
  293. const char *digest = getenv("MY_MAC_DIGEST");
  294. const char *key = getenv("MY_KEY");
  295. EVP_MAC_CTX *ctx = NULL;
  296. unsigned char buf[4096];
  297. size_t read_l;
  298. size_t final_l;
  299. size_t i;
  300. OSSL_PARAM params[3];
  301. size_t params_n = 0;
  302. if (cipher != NULL)
  303. params[params_n++] =
  304. OSSL_PARAM_construct_utf8_string("cipher", (char*)cipher, 0);
  305. if (digest != NULL)
  306. params[params_n++] =
  307. OSSL_PARAM_construct_utf8_string("digest", (char*)digest, 0);
  308. params[params_n] = OSSL_PARAM_construct_end();
  309. if (mac == NULL
  310. || key == NULL
  311. || (ctx = EVP_MAC_CTX_new(mac)) == NULL
  312. || !EVP_MAC_init(ctx, (const unsigned char *)key, strlen(key),
  313. params))
  314. goto err;
  315. while ( (read_l = read(STDIN_FILENO, buf, sizeof(buf))) > 0) {
  316. if (!EVP_MAC_update(ctx, buf, read_l))
  317. goto err;
  318. }
  319. if (!EVP_MAC_final(ctx, buf, &final_l, sizeof(buf)))
  320. goto err;
  321. printf("Result: ");
  322. for (i = 0; i < final_l; i++)
  323. printf("%02X", buf[i]);
  324. printf("\n");
  325. EVP_MAC_CTX_free(ctx);
  326. EVP_MAC_free(mac);
  327. exit(0);
  328. err:
  329. EVP_MAC_CTX_free(ctx);
  330. EVP_MAC_free(mac);
  331. fprintf(stderr, "Something went wrong\n");
  332. ERR_print_errors_fp(stderr);
  333. exit (1);
  334. }
  335. A run of this program, called with correct environment variables, can
  336. look like this:
  337. $ MY_MAC=cmac MY_KEY=secret0123456789 MY_MAC_CIPHER=aes-128-cbc \
  338. LD_LIBRARY_PATH=. ./foo < foo.c
  339. Result: C5C06683CD9DDEF904D754505C560A4E
  340. (in this example, that program was stored in F<foo.c> and compiled to
  341. F<./foo>)
  342. =head1 SEE ALSO
  343. L<property(7)>
  344. L<OSSL_PARAM(3)>,
  345. L<EVP_MAC-BLAKE2(7)>,
  346. L<EVP_MAC-CMAC(7)>,
  347. L<EVP_MAC-GMAC(7)>,
  348. L<EVP_MAC-HMAC(7)>,
  349. L<EVP_MAC-KMAC(7)>,
  350. L<EVP_MAC-Siphash(7)>,
  351. L<EVP_MAC-Poly1305(7)>,
  352. L<provider-mac(7)>,
  353. L<life_cycle-mac(7)>
  354. =head1 HISTORY
  355. These functions were added in OpenSSL 3.0.
  356. =head1 COPYRIGHT
  357. Copyright 2018-2023 The OpenSSL Project Authors. All Rights Reserved.
  358. Licensed under the Apache License 2.0 (the "License"). You may not use
  359. this file except in compliance with the License. You can obtain a copy
  360. in the file LICENSE in the source distribution or at
  361. L<https://www.openssl.org/source/license.html>.
  362. =cut