2
0

EVP_MAC.pod 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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 unterlying 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 B<OSSL_PARAM> 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. See L<OSSL_PARAM(3)> for the use of B<OSSL_PARAM> as a parameter descriptor.
  155. EVP_MAC_gettable_ctx_params() and EVP_MAC_CTX_gettable_params()
  156. return constant B<OSSL_PARAM> arrays that describe the retrievable
  157. parameters that can be used with EVP_MAC_CTX_get_params().
  158. EVP_MAC_gettable_ctx_params() returns the parameters that can be retrieved
  159. from the algorithm, whereas EVP_MAC_CTX_gettable_params() returns
  160. the parameters that can be retrieved in the context's current state.
  161. See L<OSSL_PARAM(3)> for the use of B<OSSL_PARAM> as a parameter descriptor.
  162. EVP_MAC_settable_ctx_params() and EVP_MAC_CTX_settable_params() return
  163. constant B<OSSL_PARAM> arrays that describe the settable parameters that
  164. can be used with EVP_MAC_CTX_set_params(). EVP_MAC_settable_ctx_params()
  165. returns the parameters that can be retrieved from the algorithm,
  166. whereas EVP_MAC_CTX_settable_params() returns the parameters that can
  167. be retrieved in the context's current state. See L<OSSL_PARAM(3)>
  168. for the use of B<OSSL_PARAM> as a parameter descriptor.
  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 used by HMAC.
  217. =item "digest-oneshot" (B<OSSL_MAC_PARAM_DIGEST_ONESHOT>) <integer>
  218. A simple flag to set the MAC digest to be a oneshot operation.
  219. The value 0 or 1 is expected.
  220. This option is used by HMAC.
  221. =item "properties" (B<OSSL_MAC_PARAM_PROPERTIES>) <UTF8 string>
  222. =item "digest" (B<OSSL_MAC_PARAM_DIGEST>) <UTF8 string>
  223. =item "cipher" (B<OSSL_MAC_PARAM_CIPHER>) <UTF8 string>
  224. For MAC implementations that use an underlying computation cipher or
  225. digest, these parameters set what the algorithm should be.
  226. The value is always the name of the intended algorithm,
  227. or the properties.
  228. Note that not all algorithms may support all digests.
  229. HMAC does not support variable output length digests such as SHAKE128
  230. or SHAKE256.
  231. =item "size" (B<OSSL_MAC_PARAM_SIZE>) <unsigned integer>
  232. For MAC implementations that support it, set the output size that
  233. EVP_MAC_final() should produce.
  234. The allowed sizes vary between MAC implementations, but must never exceed
  235. what can be given with a B<size_t>.
  236. =item "tls-data-size" (B<OSSL_MAC_PARAM_TLS_DATA_SIZE>) <unsigned integer>
  237. This parameter is only supported by HMAC. If set then special handling is
  238. activated for calculating the MAC of a received mac-then-encrypt TLS record
  239. where variable length record padding has been used (as in the case of CBC mode
  240. ciphersuites). The value represents the total length of the record that is
  241. having the MAC calculated including the received MAC and the record padding.
  242. When used EVP_MAC_update must be called precisely twice. The first time with
  243. the 13 bytes of TLS "header" data, and the second time with the entire record
  244. including the MAC itself and any padding. The entire record length must equal
  245. the value passed in the "tls-data-size" parameter. The length passed in the
  246. B<datalen> parameter to EVP_MAC_update() should be equal to the length of the
  247. record after the MAC and any padding has been removed.
  248. =back
  249. All these parameters should be used before the calls to any of
  250. EVP_MAC_init(), EVP_MAC_update() and EVP_MAC_final() for a full
  251. computation.
  252. Anything else may give undefined results.
  253. =head1 NOTES
  254. The MAC life-cycle is described in L<life_cycle-mac(7)>. In the future,
  255. the transitions described there will be enforced. When this is done, it will
  256. not be considered a breaking change to the API.
  257. The usage of the parameter names "custom", "iv" and "salt" correspond to
  258. the names used in the standard where the algorithm was defined.
  259. =head1 RETURN VALUES
  260. EVP_MAC_fetch() returns a pointer to a newly fetched B<EVP_MAC>, or
  261. NULL if allocation failed.
  262. EVP_MAC_up_ref() returns 1 on success, 0 on error.
  263. EVP_MAC_names_do_all() returns 1 if the callback was called for all names. A
  264. return value of 0 means that the callback was not called for any names.
  265. EVP_MAC_free() returns nothing at all.
  266. EVP_MAC_is_a() returns 1 if the given method can be identified with
  267. the given name, otherwise 0.
  268. EVP_MAC_get0_name() returns a name of the MAC, or NULL on error.
  269. EVP_MAC_get0_provider() returns a pointer to the provider for the MAC, or
  270. NULL on error.
  271. EVP_MAC_CTX_new() and EVP_MAC_CTX_dup() return a pointer to a newly
  272. created EVP_MAC_CTX, or NULL if allocation failed.
  273. EVP_MAC_CTX_free() returns nothing at all.
  274. EVP_MAC_CTX_get_params() and EVP_MAC_CTX_set_params() return 1 on
  275. success, 0 on error.
  276. EVP_Q_mac() returns a pointer to the computed MAC value, or NULL on error.
  277. EVP_MAC_init(), EVP_MAC_update(), EVP_MAC_final(), and EVP_MAC_finalXOF()
  278. return 1 on success, 0 on error.
  279. EVP_MAC_CTX_get_mac_size() returns the expected output size, or 0 if it isn't
  280. set. If it isn't set, a call to EVP_MAC_init() will set it.
  281. EVP_MAC_CTX_get_block_size() returns the block size, or 0 if it isn't set.
  282. If it isn't set, a call to EVP_MAC_init() will set it.
  283. EVP_MAC_do_all_provided() returns nothing at all.
  284. =head1 EXAMPLES
  285. #include <stdlib.h>
  286. #include <stdio.h>
  287. #include <string.h>
  288. #include <stdarg.h>
  289. #include <unistd.h>
  290. #include <openssl/evp.h>
  291. #include <openssl/err.h>
  292. #include <openssl/params.h>
  293. int main() {
  294. EVP_MAC *mac = EVP_MAC_fetch(NULL, getenv("MY_MAC"), NULL);
  295. const char *cipher = getenv("MY_MAC_CIPHER");
  296. const char *digest = getenv("MY_MAC_DIGEST");
  297. const char *key = getenv("MY_KEY");
  298. EVP_MAC_CTX *ctx = NULL;
  299. unsigned char buf[4096];
  300. size_t read_l;
  301. size_t final_l;
  302. size_t i;
  303. OSSL_PARAM params[3];
  304. size_t params_n = 0;
  305. if (cipher != NULL)
  306. params[params_n++] =
  307. OSSL_PARAM_construct_utf8_string("cipher", (char*)cipher, 0);
  308. if (digest != NULL)
  309. params[params_n++] =
  310. OSSL_PARAM_construct_utf8_string("digest", (char*)digest, 0);
  311. params[params_n] = OSSL_PARAM_construct_end();
  312. if (mac == NULL
  313. || key == NULL
  314. || (ctx = EVP_MAC_CTX_new(mac)) == NULL
  315. || !EVP_MAC_init(ctx, (const unsigned char *)key, strlen(key),
  316. params))
  317. goto err;
  318. while ( (read_l = read(STDIN_FILENO, buf, sizeof(buf))) > 0) {
  319. if (!EVP_MAC_update(ctx, buf, read_l))
  320. goto err;
  321. }
  322. if (!EVP_MAC_final(ctx, buf, &final_l, sizeof(buf)))
  323. goto err;
  324. printf("Result: ");
  325. for (i = 0; i < final_l; i++)
  326. printf("%02X", buf[i]);
  327. printf("\n");
  328. EVP_MAC_CTX_free(ctx);
  329. EVP_MAC_free(mac);
  330. exit(0);
  331. err:
  332. EVP_MAC_CTX_free(ctx);
  333. EVP_MAC_free(mac);
  334. fprintf(stderr, "Something went wrong\n");
  335. ERR_print_errors_fp(stderr);
  336. exit (1);
  337. }
  338. A run of this program, called with correct environment variables, can
  339. look like this:
  340. $ MY_MAC=cmac MY_KEY=secret0123456789 MY_MAC_CIPHER=aes-128-cbc \
  341. LD_LIBRARY_PATH=. ./foo < foo.c
  342. Result: C5C06683CD9DDEF904D754505C560A4E
  343. (in this example, that program was stored in F<foo.c> and compiled to
  344. F<./foo>)
  345. =head1 SEE ALSO
  346. L<property(7)>
  347. L<OSSL_PARAM(3)>,
  348. L<EVP_MAC-BLAKE2(7)>,
  349. L<EVP_MAC-CMAC(7)>,
  350. L<EVP_MAC-GMAC(7)>,
  351. L<EVP_MAC-HMAC(7)>,
  352. L<EVP_MAC-KMAC(7)>,
  353. L<EVP_MAC-Siphash(7)>,
  354. L<EVP_MAC-Poly1305(7)>,
  355. L<provider-mac(7)>,
  356. L<life_cycle-mac(7)>
  357. =head1 HISTORY
  358. These functions were added in OpenSSL 3.0.
  359. =head1 COPYRIGHT
  360. Copyright 2018-2021 The OpenSSL Project Authors. All Rights Reserved.
  361. Licensed under the Apache License 2.0 (the "License"). You may not use
  362. this file except in compliance with the License. You can obtain a copy
  363. in the file LICENSE in the source distribution or at
  364. L<https://www.openssl.org/source/license.html>.
  365. =cut