EVP_MAC.pod 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. =pod
  2. =head1 NAME
  3. EVP_MAC, EVP_MAC_fetch, EVP_MAC_up_ref, EVP_MAC_free,
  4. EVP_MAC_is_a, EVP_MAC_number, EVP_MAC_names_do_all,
  5. EVP_MAC_provider, EVP_MAC_get_params, EVP_MAC_gettable_params,
  6. EVP_MAC_CTX, EVP_MAC_new_ctx, EVP_MAC_free_ctx, EVP_MAC_dup_ctx,
  7. EVP_MAC_get_ctx_mac, EVP_MAC_get_ctx_params, EVP_MAC_set_ctx_params,
  8. EVP_MAC_size, EVP_MAC_init, EVP_MAC_update, EVP_MAC_final,
  9. EVP_MAC_gettable_ctx_params, EVP_MAC_settable_ctx_params,
  10. EVP_MAC_do_all_provided - EVP MAC routines
  11. =head1 SYNOPSIS
  12. #include <openssl/evp.h>
  13. typedef struct evp_mac_st EVP_MAC;
  14. typedef struct evp_mac_ctx_st EVP_MAC_CTX;
  15. EVP_MAC *EVP_MAC_fetch(OPENSSL_CTX *libctx, const char *algorithm,
  16. const char *properties);
  17. int EVP_MAC_up_ref(EVP_MAC *mac);
  18. void EVP_MAC_free(EVP_MAC *mac);
  19. int EVP_MAC_is_a(const EVP_MAC *mac, const char *name);
  20. int EVP_MAC_number(const EVP_MAC *mac);
  21. void EVP_MAC_names_do_all(const EVP_MAC *mac,
  22. void (*fn)(const char *name, void *data),
  23. void *data);
  24. const OSSL_PROVIDER *EVP_MAC_provider(const EVP_MAC *mac);
  25. int EVP_MAC_get_params(EVP_MAC *mac, OSSL_PARAM params[]);
  26. EVP_MAC_CTX *EVP_MAC_new_ctx(EVP_MAC *mac);
  27. void EVP_MAC_free_ctx(EVP_MAC_CTX *ctx);
  28. EVP_MAC_CTX *EVP_MAC_dup_ctx(const EVP_MAC_CTX *src);
  29. EVP_MAC *EVP_MAC_get_ctx_mac(EVP_MAC_CTX *ctx);
  30. int EVP_MAC_get_ctx_params(EVP_MAC_CTX *ctx, OSSL_PARAM params[]);
  31. int EVP_MAC_set_ctx_params(EVP_MAC_CTX *ctx, const OSSL_PARAM params[]);
  32. size_t EVP_MAC_size(EVP_MAC_CTX *ctx);
  33. int EVP_MAC_init(EVP_MAC_CTX *ctx);
  34. int EVP_MAC_update(EVP_MAC_CTX *ctx, const unsigned char *data, size_t datalen);
  35. int EVP_MAC_final(EVP_MAC_CTX *ctx,
  36. unsigned char *out, size_t *outl, size_t outsize);
  37. const OSSL_PARAM *EVP_MAC_gettable_params(const EVP_MAC *mac);
  38. const OSSL_PARAM *EVP_MAC_gettable_ctx_params(const EVP_MAC *mac);
  39. const OSSL_PARAM *EVP_MAC_settable_ctx_params(const EVP_MAC *mac);
  40. void EVP_MAC_do_all_provided(OPENSSL_CTX *libctx,
  41. void (*fn)(EVP_MAC *mac, void *arg),
  42. void *arg);
  43. =head1 DESCRIPTION
  44. These types and functions help the application to calculate MACs of
  45. different types and with different underlying algorithms if there are
  46. any.
  47. MACs are a bit complex insofar that some of them use other algorithms
  48. for actual computation. HMAC uses a digest, and CMAC uses a cipher.
  49. Therefore, there are sometimes two contexts to keep track of, one for
  50. the MAC algorithm itself and one for the underlying computation
  51. algorithm if there is one.
  52. To make things less ambiguous, this manual talks about a "context" or
  53. "MAC context", which is to denote the MAC level context, and about a
  54. "underlying context", or "computation context", which is to denote the
  55. context for the underlying computation algorithm if there is one.
  56. =head2 Types
  57. B<EVP_MAC> is a type that holds the implementation of a MAC.
  58. B<EVP_MAC_CTX> is a context type that holds internal MAC information
  59. as well as a reference to a computation context, for those MACs that
  60. rely on an underlying computation algorithm.
  61. =head2 Algorithm implementation fetching
  62. EVP_MAC_fetch() fetches an implementation of a MAC I<algorithm>, given
  63. a library context I<libctx> and a set of I<properties>.
  64. See L<provider(7)/Fetching algorithms> for further information.
  65. See L<OSSL_PROVIDER-default(7)/Message Authentication Code (MAC)> for the list
  66. of algorithms supported by the default provider.
  67. The returned value must eventually be freed with
  68. L<EVP_MAC_free(3)>.
  69. EVP_MAC_up_ref() increments the reference count of an already fetched
  70. MAC.
  71. EVP_MAC_free() frees a fetched algorithm.
  72. NULL is a valid parameter, for which this function is a no-op.
  73. =head2 Context manipulation functions
  74. EVP_MAC_new_ctx() creates a new context for the MAC type I<mac>.
  75. The created context can then be used with most other functions
  76. described here.
  77. EVP_MAC_free_ctx() frees the contents of the context, including an
  78. underlying context if there is one, as well as the context itself.
  79. NULL is a valid parameter, for which this function is a no-op.
  80. EVP_MAC_dup_ctx() duplicates the I<src> context and returns a newly allocated
  81. context.
  82. EVP_MAC_get_ctx_mac() returns the B<EVP_MAC> associated with the context
  83. I<ctx>.
  84. =head2 Computing functions
  85. EVP_MAC_init() sets up the underlying context with information given
  86. through diverse controls.
  87. This should be called before calling EVP_MAC_update() and
  88. EVP_MAC_final().
  89. EVP_MAC_update() adds I<datalen> bytes from I<data> to the MAC input.
  90. EVP_MAC_final() does the final computation and stores the result in
  91. the memory pointed at by I<out> of size I<outsize>, and sets the number
  92. of bytes written in I<*outl> at.
  93. If I<out> is NULL or I<outsize> is too small, then no computation
  94. is made.
  95. To figure out what the output length will be and allocate space for it
  96. dynamically, simply call with I<out> being NULL and I<outl>
  97. pointing at a valid location, then allocate space and make a second
  98. call with I<out> pointing at the allocated space.
  99. EVP_MAC_get_params() retrieves details about the implementation
  100. I<mac>.
  101. The set of parameters given with I<params> determine exactly what
  102. parameters should be retrieved.
  103. Note that a parameter that is unknown in the underlying context is
  104. simply ignored.
  105. EVP_MAC_get_ctx_params() retrieves chosen parameters, given the
  106. context I<ctx> and its underlying context.
  107. The set of parameters given with I<params> determine exactly what
  108. parameters should be retrieved.
  109. Note that a parameter that is unknown in the underlying context is
  110. simply ignored.
  111. EVP_MAC_set_ctx_params() passes chosen parameters to the underlying
  112. context, given a context I<ctx>.
  113. The set of parameters given with I<params> determine exactly what
  114. parameters are passed down.
  115. Note that a parameter that is unknown in the underlying context is
  116. simply ignored.
  117. Also, what happens when a needed parameter isn't passed down is
  118. defined by the implementation.
  119. EVP_MAC_gettable_params(), EVP_MAC_gettable_ctx_params() and
  120. EVP_MAC_settable_ctx_params() get a constant B<OSSL_PARAM> array that
  121. describes the retrievable and settable parameters, i.e. parameters that
  122. can be used with EVP_MAC_get_params(), EVP_MAC_get_ctx_params()
  123. and EVP_MAC_set_ctx_params(), respectively.
  124. See L<OSSL_PARAM(3)> for the use of B<OSSL_PARAM> as parameter descriptor.
  125. =head2 Information functions
  126. EVP_MAC_size() returns the MAC output size for the given context.
  127. EVP_MAC_is_a() checks if the given I<mac> is an implementation of an
  128. algorithm that's identifiable with I<name>.
  129. EVP_MAC_provider() returns the provider that holds the implementation
  130. of the given I<mac>.
  131. EVP_MAC_do_all_provided() traverses all MAC implemented by all activated
  132. providers in the given library context I<libctx>, and for each of the
  133. implementations, calls the given function I<fn> with the implementation method
  134. and the given I<arg> as argument.
  135. EVP_MAC_number() returns the internal dynamic number assigned to
  136. I<mac>.
  137. EVP_MAC_names_do_all() traverses all names for I<mac>, and calls
  138. I<fn> with each name and I<data>.
  139. =head1 PARAMETERS
  140. Parameters are identified by name as strings, and have an expected
  141. data type and maximum size.
  142. OpenSSL has a set of macros for parameter names it expects to see in
  143. its own MAC implementations.
  144. Here, we show all three, the OpenSSL macro for the parameter name, the
  145. name in string form, and a type description.
  146. The standard parameter names are:
  147. =over 4
  148. =item "key" (B<OSSL_MAC_PARAM_KEY>) <octet string>
  149. Its value is the MAC key as an array of bytes.
  150. For MACs that use an underlying computation algorithm, the algorithm
  151. must be set first, see parameter names "algorithm" below.
  152. =item "iv" (B<OSSL_MAC_PARAM_IV>) <octet string>
  153. Some MAC implementations require an IV, this parameter sets the IV.
  154. =item "custom" (B<OSSL_MAC_PARAM_CUSTOM>) <octet string>
  155. Some MAC implementations (KMAC, BLAKE2) accept a Customization String,
  156. this parameter sets the Customization String. The default value is the
  157. empty string.
  158. =item "salt" (B<OSSL_MAC_PARAM_SALT>) <octet string>
  159. This option is used by BLAKE2 MAC.
  160. =item "xof" (B<OSSL_MAC_PARAM_XOF>) <integer>
  161. It's a simple flag, the value 0 or 1 are expected.
  162. This option is used by KMAC.
  163. =item "flags" (B<OSSL_MAC_PARAM_FLAGS>) <integer>
  164. These will set the MAC flags to the given numbers.
  165. Some MACs do not support this option.
  166. =item "properties" (B<OSSL_MAC_PARAM_PROPERTIES>) <UTF8 string>
  167. =item "digest" (B<OSSL_MAC_PARAM_DIGEST>) <UTF8 string>
  168. =item "cipher" (B<OSSL_MAC_PARAM_CIPHER>) <UTF8 string>
  169. For MAC implementations that use an underlying computation cipher or
  170. digest, these parameters set what the algorithm should be.
  171. The value is always the name of the intended algorithm,
  172. or the properties.
  173. Note that not all algorithms may support all digests.
  174. HMAC does not support variable output length digests such as SHAKE128
  175. or SHAKE256.
  176. =item "size" (B<OSSL_MAC_PARAM_SIZE>) <unsigned integer>
  177. For MAC implementations that support it, set the output size that
  178. EVP_MAC_final() should produce.
  179. The allowed sizes vary between MAC implementations, but must never exceed
  180. what can be given with a B<size_t>.
  181. =back
  182. All these parameters should be used before the calls to any of
  183. EVP_MAC_init(), EVP_MAC_update() and EVP_MAC_final() for a full
  184. computation.
  185. Anything else may give undefined results.
  186. =head1 RETURN VALUES
  187. EVP_MAC_fetch() returns a pointer to a newly fetched EVP_MAC, or
  188. NULL if allocation failed.
  189. EVP_MAC_up_ref() returns 1 on success, 0 on error.
  190. EVP_MAC_free() returns nothing at all.
  191. EVP_MAC_is_a() returns 1 if the given method can be identified with
  192. the given name, otherwise 0.
  193. EVP_MAC_provider() returns a pointer to the provider for the MAC, or
  194. NULL on error.
  195. EVP_MAC_new_ctx() and EVP_MAC_dup_ctx() return a pointer to a newly
  196. created EVP_MAC_CTX, or NULL if allocation failed.
  197. EVP_MAC_free_ctx() returns nothing at all.
  198. EVP_MAC_get_ctx_params() and EVP_MAC_set_ctx_params() return 1 on
  199. success, 0 on error.
  200. EVP_MAC_init(), EVP_MAC_update(), and EVP_MAC_final() return 1 on success, 0
  201. on error.
  202. EVP_MAC_size() returns the expected output size, or 0 if it isn't
  203. set.
  204. If it isn't set, a call to EVP_MAC_init() should get it set.
  205. EVP_MAC_do_all_provided() returns nothing at all.
  206. =head1 EXAMPLES
  207. #include <stdlib.h>
  208. #include <stdio.h>
  209. #include <string.h>
  210. #include <stdarg.h>
  211. #include <unistd.h>
  212. #include <openssl/evp.h>
  213. #include <openssl/err.h>
  214. #include <openssl/params.h>
  215. int main() {
  216. EVP_MAC *mac = EVP_MAC_fetch(NULL, getenv("MY_MAC"), NULL);
  217. const char *cipher = getenv("MY_MAC_CIPHER");
  218. const char *digest = getenv("MY_MAC_DIGEST");
  219. const char *key = getenv("MY_KEY");
  220. EVP_MAC_CTX *ctx = NULL;
  221. unsigned char buf[4096];
  222. ssize_t read_l;
  223. size_t final_l;
  224. size_t i;
  225. OSSL_PARAM params[4];
  226. size_t params_n = 0;
  227. if (cipher != NULL)
  228. params[params_n++] =
  229. OSSL_PARAM_construct_utf8_string("cipher", cipher, 0;
  230. if (digest != NULL)
  231. params[params_n++] =
  232. OSSL_PARAM_construct_utf8_string("digest", digest, 0);
  233. params[params_n++] =
  234. OSSL_PARAM_construct_octet_string("key", key, strlen(key));
  235. params[params_n] = OSSL_PARAM_construct_end();
  236. if (mac == NULL
  237. || key == NULL
  238. || (ctx = EVP_MAC_new_ctx(mac)) == NULL
  239. || EVP_MAC_set_ctx_params(ctx, params) <= 0)
  240. goto err;
  241. if (!EVP_MAC_init(ctx))
  242. goto err;
  243. while ( (read_l = read(STDIN_FILENO, buf, sizeof(buf))) > 0) {
  244. if (!EVP_MAC_update(ctx, buf, read_l))
  245. goto err;
  246. }
  247. if (!EVP_MAC_final(ctx, buf, &final_l))
  248. goto err;
  249. printf("Result: ");
  250. for (i = 0; i < final_l; i++)
  251. printf("%02X", buf[i]);
  252. printf("\n");
  253. EVP_MAC_free_ctx(ctx);
  254. EVP_MAC_free(mac);
  255. exit(0);
  256. err:
  257. EVP_MAC_free_ctx(ctx);
  258. EVP_MAC_free(mac);
  259. fprintf(stderr, "Something went wrong\n");
  260. ERR_print_errors_fp(stderr);
  261. exit (1);
  262. }
  263. A run of this program, called with correct environment variables, can
  264. look like this:
  265. $ MY_MAC=cmac MY_KEY=secret0123456789 MY_MAC_CIPHER=aes-128-cbc \
  266. LD_LIBRARY_PATH=. ./foo < foo.c
  267. Result: C5C06683CD9DDEF904D754505C560A4E
  268. (in this example, that program was stored in F<foo.c> and compiled to
  269. F<./foo>)
  270. =head1 SEE ALSO
  271. L<property(7)>
  272. L<OSSL_PARAM(3)>,
  273. L<EVP_MAC-BLAKE2(7)>,
  274. L<EVP_MAC-CMAC(7)>,
  275. L<EVP_MAC-GMAC(7)>,
  276. L<EVP_MAC-HMAC(7)>,
  277. L<EVP_MAC-KMAC(7)>,
  278. L<EVP_MAC-Siphash(7)>,
  279. L<EVP_MAC-Poly1305(7)>
  280. =head1 HISTORY
  281. These functions were added in OpenSSL 3.0.
  282. =head1 COPYRIGHT
  283. Copyright 2018-2020 The OpenSSL Project Authors. All Rights Reserved.
  284. Licensed under the Apache License 2.0 (the "License"). You may not use
  285. this file except in compliance with the License. You can obtain a copy
  286. in the file LICENSE in the source distribution or at
  287. L<https://www.openssl.org/source/license.html>.
  288. =cut