EVP_MAC.pod 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. =pod
  2. =head1 NAME
  3. EVP_MAC, EVP_MAC_CTX, EVP_MAC_CTX_new, EVP_MAC_CTX_new_id, EVP_MAC_CTX_free,
  4. EVP_MAC_CTX_dup, EVP_MAC_CTX_mac, EVP_MAC_size, EVP_MAC_init, EVP_MAC_update,
  5. EVP_MAC_final, EVP_MAC_ctrl, EVP_MAC_vctrl, EVP_MAC_ctrl_str,
  6. EVP_MAC_str2ctrl, EVP_MAC_hex2ctrl, EVP_MAC_nid, EVP_MAC_name,
  7. EVP_get_macbyname, EVP_get_macbynid, EVP_get_macbyobj - EVP MAC routines
  8. =head1 SYNOPSIS
  9. #include <openssl/evp.h>
  10. typedef struct evp_mac_st EVP_MAC;
  11. typedef struct evp_mac_ctx_st EVP_MAC_CTX;
  12. EVP_MAC_CTX *EVP_MAC_CTX_new(const EVP_MAC *mac);
  13. EVP_MAC_CTX *EVP_MAC_CTX_new_id(int nid);
  14. void EVP_MAC_CTX_free(EVP_MAC_CTX *ctx);
  15. EVP_MAC_CTX *EVP_MAC_CTX_dup(const EVP_MAC_CTX *src);
  16. const EVP_MAC *EVP_MAC_CTX_mac(EVP_MAC_CTX *ctx);
  17. size_t EVP_MAC_size(EVP_MAC_CTX *ctx);
  18. int EVP_MAC_init(EVP_MAC_CTX *ctx);
  19. int EVP_MAC_update(EVP_MAC_CTX *ctx, const unsigned char *data, size_t datalen);
  20. int EVP_MAC_final(EVP_MAC_CTX *ctx, unsigned char *out, size_t *poutlen);
  21. int EVP_MAC_ctrl(EVP_MAC_CTX *ctx, int cmd, ...);
  22. int EVP_MAC_vctrl(EVP_MAC_CTX *ctx, int cmd, va_list args);
  23. int EVP_MAC_ctrl_str(EVP_MAC_CTX *ctx, const char *type, const char *value);
  24. int EVP_MAC_str2ctrl(EVP_MAC_CTX *ctx, int cmd, const char *value);
  25. int EVP_MAC_hex2ctrl(EVP_MAC_CTX *ctx, int cmd, const char *value);
  26. int EVP_MAC_nid(const EVP_MAC *mac);
  27. const char *EVP_MAC_name(const EVP_MAC *mac);
  28. const EVP_MAC *EVP_get_macbyname(const char *name);
  29. const EVP_MAC *EVP_get_macbynid(int nid);
  30. const EVP_MAC *EVP_get_macbyobj(const ASN1_OBJECT *o);
  31. =head1 DESCRIPTION
  32. These types and functions help the application to calculate MACs of
  33. different types and with different underlying algorithms if there are
  34. any.
  35. MACs are a bit complex insofar that some of them use other algorithms
  36. for actual computation. HMAC uses a digest, and CMAC uses a cipher.
  37. Therefore, there are sometimes two contexts to keep track of, one for
  38. the MAC algorithm itself and one for the underlying computation
  39. algorithm if there is one.
  40. To make things less ambiguous, this manual talks about a "context" or
  41. "MAC context", which is to denote the MAC level context, and about a
  42. "underlying context", or "computation context", which is to denote the
  43. context for the underlying computation algorithm if there is one.
  44. =head2 Types
  45. B<EVP_MAC> is a type that holds the implementation of a MAC.
  46. B<EVP_MAC_CTX> is a context type that holds internal MAC information
  47. as well as a reference to a computation context, for those MACs that
  48. rely on an underlying computation algorithm.
  49. =head2 Context manipulation functions
  50. EVP_MAC_CTX_new() creates a new context for the MAC type C<mac>.
  51. EVP_MAC_CTX_new_id() creates a new context for the numerical MAC
  52. identity <nid>.
  53. The created context can then be used with most other functions
  54. described here.
  55. EVP_MAC_CTX_free() frees the contents of the context, including an
  56. underlying context if there is one, as well as the context itself.
  57. B<NULL> is a valid parameter, for which this function is a no-op.
  58. EVP_MAC_CTX_dup() duplicates the C<src> context and returns a newly allocated
  59. context.
  60. EVP_MAC_CTX_mac() returns the B<EVP_MAC> associated with the context
  61. C<ctx>.
  62. =head2 Computing functions
  63. EVP_MAC_init() sets up the underlying context with information given
  64. through diverse controls.
  65. This should be called before calling EVP_MAC_update() and
  66. EVP_MAC_final().
  67. EVP_MAC_update() adds C<datalen> bytes from C<data> to the MAC input.
  68. EVP_MAC_final() does the final computation and stores the result in
  69. the memory pointed at by C<out>, and sets its size in the B<size_t>
  70. the C<poutlen> points at.
  71. If C<out> is B<NULL>, then no computation is made.
  72. To figure out what the output length will be and allocate space for it
  73. dynamically, simply call with C<out> being B<NULL> and C<poutlen>
  74. pointing at a valid location, then allocate space and make a second
  75. call with C<out> pointing at the allocated space.
  76. EVP_MAC_ctrl() is used to manipulate or get information on aspects of
  77. the MAC which may vary depending on the MAC algorithm or its
  78. implementation.
  79. This includes the MAC key, and for MACs that use other algorithms to
  80. do their computation, this is also the way to tell it which one to
  81. use.
  82. This functions takes variable arguments, the exact expected arguments
  83. depend on C<cmd>.
  84. EVP_MAC_ctrl() can be called both before and after EVP_MAC_init(), but
  85. the effect will depend on what control is being use.
  86. See L</CONTROLS> below for a description of standard controls.
  87. EVP_MAC_vctrl() is the variant of EVP_MAC_ctrl() that takes a
  88. C<va_list> argument instead of variadic arguments.
  89. EVP_MAC_ctrl_str() is an alternative to EVP_MAC_ctrl() to control the
  90. MAC implementation as E<lt> C<type>, C<value> E<gt> pairs.
  91. The MAC implementation documentation should specify what control type
  92. strings are accepted.
  93. EVP_MAC_str2ctrl() and EVP_MAC_hex2ctrl() are helper functions to
  94. control the MAC implementation with raw strings or with strings
  95. containing hexadecimal numbers.
  96. The latter are decoded into bitstrings that are sent on to
  97. EVP_MAC_ctrl().
  98. =head2 Information functions
  99. EVP_MAC_size() returns the MAC output size for the given context.
  100. EVP_MAC_nid() returns the numeric identity of the given MAC implementation.
  101. EVP_MAC_name() returns the name of the given MAC implementation.
  102. =head2 Object database functions
  103. EVP_get_macbyname() fetches a MAC implementation from the object
  104. database by name.
  105. EVP_get_macbynid() fetches a MAC implementation from the object
  106. database by numeric identity.
  107. EVP_get_macbyobj() fetches a MAC implementation from the object
  108. database by ASN.1 OBJECT (i.e. an encoded OID).
  109. =head1 CONTROLS
  110. The standard controls are:
  111. =over 4
  112. =item B<EVP_MAC_CTRL_SET_KEY>
  113. This control expects two arguments: C<unsigned char *key>, C<size_t keylen>
  114. These will set the MAC key from the given string of the given length.
  115. The string may be any bitstring, and can contain NUL bytes.
  116. For MACs that use an underlying computation algorithm, the algorithm
  117. I<must> be set first, see B<EVP_MAC_CTRL_SET_ENGINE>,
  118. B<EVP_MAC_CTRL_SET_MD> and B<EVP_MAC_CTRL_SET_CIPHER> below.
  119. =item B<EVP_MAC_CTRL_SET_IV>
  120. This control expects two arguments: C<unsigned char *key>, C<size_t keylen>
  121. Some MAC implementations require an IV, this control sets the IV.
  122. =item B<EVP_MAC_CTRL_SET_CUSTOM>
  123. This control expects two arguments: C<unsigned char *custom>, C<size_t customlen>
  124. Some MAC implementations (KMAC, BLAKE2) accept a Customization String,
  125. this control sets the Customization String. The default value is "".
  126. =item B<EVP_MAC_CTRL_SET_SALT>
  127. This control expects two arguments: C<unsigned char *salt>, C<size_t saltlen>
  128. This option is used by BLAKE2 MAC.
  129. =item B<EVP_MAC_CTRL_SET_XOF>
  130. This control expects one argument: C<int xof>
  131. This option is used by KMAC.
  132. =item B<EVP_MAC_CTRL_SET_FLAGS>
  133. This control expects one argument: C<unsigned long flags>
  134. These will set the MAC flags to the given numbers.
  135. Some MACs do not support this option.
  136. =item B<EVP_MAC_CTRL_SET_ENGINE>
  137. =item B<EVP_MAC_CTRL_SET_MD>
  138. =item B<EVP_MAC_CTRL_SET_CIPHER>
  139. For MAC implementations that use an underlying computation algorithm,
  140. these controls set what the algorithm should be, and the engine that
  141. implements the algorithm if needed.
  142. Note that not all algorithms may support all digests. HMAC does not support
  143. variable output length digests such as SHAKE128 or SHAKE256.
  144. B<EVP_MAC_CTRL_SET_ENGINE> takes one argument: C<ENGINE *>
  145. B<EVP_MAC_CTRL_SET_MD> takes one argument: C<EVP_MD *>
  146. B<EVP_MAC_CTRL_SET_CIPHER> takes one argument: C<EVP_CIPHER *>
  147. =item B<EVP_MAC_CTRL_SET_SIZE>
  148. For MAC implementations that support it, set the output size that
  149. EVP_MAC_final() should produce.
  150. The allowed sizes vary between MAC implementations.
  151. =back
  152. All these control should be used before the calls to any of
  153. EVP_MAC_init(), EVP_MAC_update() and EVP_MAC_final() for a full
  154. computation.
  155. Anything else may give undefined results.
  156. =head1 NOTES
  157. EVP_get_macbynid(), EVP_get_macbyobj() and EVP_MAC_name() are
  158. implemented as a macro.
  159. =head1 RETURN VALUES
  160. EVP_MAC_CTX_new(), EVP_MAC_CTX_new_id() and EVP_MAC_CTX_dup() return a pointer
  161. to a newly created EVP_MAC_CTX, or NULL if allocation failed.
  162. EVP_MAC_CTX_free() returns nothing at all.
  163. EVP_MAC_init(), EVP_MAC_update(), and EVP_MAC_final() return 1 on success, 0 on error.
  164. EVP_MAC_ctrl(), EVP_MAC_ctrl_str(), EVP_MAC_str2ctrl() and
  165. EVP_MAC_hex2ctrl() return 1 on success and 0 or a negative value on
  166. error.
  167. In particular, the value -2 indicates that the given control type
  168. isn't supported by the MAC implementation.
  169. EVP_MAC_size() returns the expected output size, or 0 if it isn't
  170. set.
  171. If it isn't set, a call to EVP_MAC_init() should get it set.
  172. EVP_MAC_nid() returns the numeric identity for the given C<mac>.
  173. EVP_MAC_name() returns the name for the given C<mac>, if it has been
  174. added to the object database.
  175. EVP_add_mac() returns 1 if the given C<mac> was successfully added to
  176. the object database, otherwise 0.
  177. EVP_get_macbyname(), EVP_get_macbynid() and EVP_get_macbyobj() return
  178. the request MAC implementation, if it exists in the object database,
  179. otherwise B<NULL>.
  180. =head1 EXAMPLE
  181. #include <stdlib.h>
  182. #include <stdio.h>
  183. #include <string.h>
  184. #include <stdarg.h>
  185. #include <unistd.h>
  186. #include <openssl/evp.h>
  187. #include <openssl/err.h>
  188. int ctrl_ign_unsupported(EVP_MAC_CTX *ctx, int cmd, ...)
  189. {
  190. va_list args;
  191. int rv;
  192. va_start(args, cmd);
  193. rv = EVP_MAC_vctrl(ctx, cmd, args);
  194. va_end(args);
  195. if (rv == -2)
  196. rv = 1; /* Ignore unsupported, pretend it worked fine */
  197. return rv;
  198. }
  199. int main() {
  200. const EVP_MAC *mac =
  201. EVP_get_macbyname(getenv("MY_MAC"));
  202. const EVP_CIPHER *cipher =
  203. EVP_get_cipherbyname(getenv("MY_MAC_CIPHER"));
  204. const EVP_MD *digest =
  205. EVP_get_digestbyname(getenv("MY_MAC_DIGEST"));
  206. const char *key = getenv("MY_KEY");
  207. EVP_MAC_CTX *ctx = NULL;
  208. unsigned char buf[4096];
  209. ssize_t read_l;
  210. size_t final_l;
  211. size_t i;
  212. if (mac == NULL
  213. || key == NULL
  214. || (ctx = EVP_MAC_CTX_new(mac)) == NULL
  215. || (cipher != NULL
  216. && !ctrl_ign_unsupported(ctx, EVP_MAC_CTRL_SET_CIPHER, cipher))
  217. || (digest != NULL
  218. && !ctrl_ign_unsupported(ctx, EVP_MAC_CTRL_SET_MD, digest))
  219. || EVP_MAC_ctrl(ctx, EVP_MAC_CTRL_SET_KEY, key, strlen(key)) <= 0)
  220. goto err;
  221. if (!EVP_MAC_init(ctx))
  222. goto err;
  223. while ( (read_l = read(STDIN_FILENO, buf, sizeof(buf))) < 0) {
  224. if (!EVP_MAC_update(ctx, buf, read_l))
  225. goto err;
  226. }
  227. if (!EVP_MAC_final(ctx, buf, &final_l))
  228. goto err;
  229. printf("Result: ");
  230. for (i = 0; i < final_l; i++)
  231. printf("%02X", buf[i]);
  232. printf("\n");
  233. EVP_MAC_CTX_free(ctx);
  234. exit(0);
  235. err:
  236. EVP_MAC_CTX_free(ctx);
  237. fprintf(stderr, "Something went wrong\n");
  238. ERR_print_errors_fp(stderr);
  239. exit (1);
  240. }
  241. A run of this program, called with correct environment variables, can
  242. look like this:
  243. $ MY_MAC=cmac MY_KEY=secret0123456789 MY_MAC_CIPHER=aes-128-cbc \
  244. LD_LIBRARY_PATH=. ./foo < foo.c
  245. Result: ECCAAFF041B22A2299EB90A1B53B6D45
  246. (in this example, that program was stored in F<foo.c> and compiled to
  247. F<./foo>)
  248. =head1 SEE ALSO
  249. L<EVP_MAC_BLAKE2(7)>,
  250. L<EVP_MAC_CMAC(7)>,
  251. L<EVP_MAC_GMAC(7)>,
  252. L<EVP_MAC_HMAC(7)>,
  253. L<EVP_MAC_KMAC(7)>,
  254. L<EVP_MAC_SIPHASH(7)>,
  255. L<EVP_MAC_POLY1305(7)>
  256. =head1 HISTORY
  257. These functions were added in OpenSSL 3.0.0.
  258. =head1 COPYRIGHT
  259. Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
  260. Licensed under the Apache License 2.0 (the "License"). You may not use
  261. this file except in compliance with the License. You can obtain a copy
  262. in the file LICENSE in the source distribution or at
  263. L<https://www.openssl.org/source/license.html>.
  264. =cut