EVP_MAC.pod 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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_copy, 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. int EVP_MAC_CTX_copy(EVP_MAC_CTX *dest, 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_copy() makes a deep copy of the C<src> context to the
  59. C<dest> context.
  60. The C<dest> context I<must> have been created before calling this
  61. function.
  62. EVP_MAC_CTX_mac() returns the B<EVP_MAC> associated with the context
  63. C<ctx>.
  64. =head2 Computing functions
  65. EVP_MAC_init() sets up the underlying context with information given
  66. through diverse controls.
  67. This should be called before calling EVP_MAC_update() and
  68. EVP_MAC_final().
  69. EVP_MAC_reset() resets the computation for the given context.
  70. This may not be supported by the MAC implementation.
  71. EVP_MAC_update() adds C<datalen> bytes from C<data> to the MAC input.
  72. EVP_MAC_final() does the final computation and stores the result in
  73. the memory pointed at by C<out>, and sets its size in the B<size_t>
  74. the C<poutlen> points at.
  75. If C<out> is B<NULL>, then no computation is made.
  76. To figure out what the output length will be and allocate space for it
  77. dynamically, simply call with C<out> being B<NULL> and C<poutlen>
  78. pointing at a valid location, then allocate space and make a second
  79. call with C<out> pointing at the allocated space.
  80. EVP_MAC_ctrl() is used to manipulate or get information on aspects of
  81. the MAC which may vary depending on the MAC algorithm or its
  82. implementation.
  83. This includes the MAC key, and for MACs that use other algorithms to
  84. do their computation, this is also the way to tell it which one to
  85. use.
  86. This functions takes variable arguments, the exact expected arguments
  87. depend on C<cmd>.
  88. EVP_MAC_ctrl() can be called both before and after EVP_MAC_init(), but
  89. the effect will depend on what control is being use.
  90. See </CONTROLS> below for a description of standard controls.
  91. EVP_MAC_vctrl() is the variant of EVP_MAC_ctrl() that takes a
  92. C<va_list> argument instead of variadic arguments.
  93. EVP_MAC_ctrl_str() is an alternative to EVP_MAC_ctrl() to control the
  94. MAC implementation as E<lt> C<type>, C<value> E<gt> pairs.
  95. The MAC implementation documentation should specify what control type
  96. strings are accepted.
  97. EVP_MAC_str2ctrl() and EVP_MAC_hex2ctrl() are helper functions to
  98. control the MAC implementation with raw strings or with strings
  99. containing hexadecimal numbers.
  100. The latter are decoded into bitstrings that are sent on to
  101. EVP_MAC_ctrl().
  102. =head2 Information functions
  103. EVP_MAC_size() returns the MAC output size for the given context.
  104. EVP_MAC_nid() returns the numeric identity of the given MAC implementation.
  105. EVP_MAC_name() returns the name of the given MAC implementation.
  106. =head2 Object database functions
  107. EVP_get_macbyname() fetches a MAC implementation from the object
  108. database by name.
  109. EVP_get_macbynid() fetches a MAC implementation from the object
  110. database by numeric identity.
  111. EVP_get_macbyobj() fetches a MAC implementation from the object
  112. database by ASN.1 OBJECT (i.e. an encoded OID).
  113. =head1 CONTROLS
  114. The standard controls are:
  115. =over 4
  116. =item B<EVP_MAC_CTRL_SET_KEY>
  117. This control expects two arguments: C<unsigned char *key>, C<size_t keylen>
  118. These will set the MAC key from the given string of the given length.
  119. The string may be any bitstring, and can contain NUL bytes.
  120. For MACs that use an underlying computation algorithm, the algorithm
  121. I<must> be set first, see B<EVP_MAC_CTRL_SET_ENGINE>,
  122. B<EVP_MAC_CTRL_SET_MD> and B<EVP_MAC_CTRL_SET_CIPHER> below.
  123. =item B<EVP_MAC_CTRL_SET_IV>
  124. This control expects two arguments: C<unsigned char *key>, C<size_t keylen>
  125. Some MAC implementations require an IV, this control sets the IV.
  126. =item B<EVP_MAC_CTRL_SET_CUSTOM>
  127. This control expects two arguments: C<unsigned char *key>, C<size_t keylen>
  128. Some MAC implementations (KMAC) require an Customization String,
  129. this control sets the Customization String. The default value is "".
  130. =item B<EVP_MAC_CTRL_SET_XOF>
  131. This control expects one argument: C<int xof>
  132. This option is used by KMAC.
  133. =item B<EVP_MAC_CTRL_SET_FLAGS>
  134. This control expects one argument: C<unsigned long flags>
  135. These will set the MAC flags to the given numbers.
  136. Some MACs do not support this option.
  137. =item B<EVP_MAC_CTRL_SET_ENGINE>
  138. =item B<EVP_MAC_CTRL_SET_MD>
  139. =item B<EVP_MAC_CTRL_SET_CIPHER>
  140. For MAC implementations that use an underlying computation algorithm,
  141. these controls set what the algorithm should be, and the engine that
  142. implements the algorithm if needed.
  143. B<EVP_MAC_CTRL_SET_ENGINE> takes one argument: C<ENGINE *>
  144. B<EVP_MAC_CTRL_SET_MD> takes one argument: C<EVP_MD *>
  145. B<EVP_MAC_CTRL_SET_CIPHER> takes one argument: C<EVP_CIPHER *>
  146. =item B<EVP_MAC_CTRL_SET_SIZE>
  147. For MAC implementations that support it, set the output size that
  148. EVP_MAC_final() should produce.
  149. The allowed sizes vary between MAC implementations.
  150. =back
  151. All these control should be used before the calls to any of
  152. EVP_MAC_init(), EVP_MAC_update() and EVP_MAC_final() for a full
  153. computation.
  154. Anything else may give undefined results.
  155. =head1 NOTES
  156. EVP_get_macbynid(), EVP_get_macbyobj() and EVP_MAC_name() are
  157. implemented as a macro.
  158. =head1 RETURN VALUES
  159. EVP_MAC_CTX_new() and EVP_MAC_CTX_new_id() return a pointer to a newly
  160. created EVP_MAC_CTX, or NULL if allocation failed.
  161. EVP_MAC_CTX_free() returns nothing at all.
  162. EVP_MAC_CTX_copy(), EVP_MAC_reset(), EVP_MAC_init(), EVP_MAC_update(),
  163. 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_CMAC(7)>,
  250. L<EVP_MAC_GMAC(7)>,
  251. L<EVP_MAC_HMAC(7)>,
  252. L<EVP_MAC_KMAC(7)>,
  253. L<EVP_MAC_SIPHASH(7)>,
  254. L<EVP_MAC_POLY1305(7)>
  255. =head1 COPYRIGHT
  256. Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
  257. Licensed under the Apache License 2.0 (the "License"). You may not use
  258. this file except in compliance with the License. You can obtain a copy
  259. in the file LICENSE in the source distribution or at
  260. L<https://www.openssl.org/source/license.html>.
  261. =cut