EVP_MAC.3ossl 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. .\" -*- mode: troff; coding: utf-8 -*-
  2. .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43)
  3. .\"
  4. .\" Standard preamble:
  5. .\" ========================================================================
  6. .de Sp \" Vertical space (when we can't use .PP)
  7. .if t .sp .5v
  8. .if n .sp
  9. ..
  10. .de Vb \" Begin verbatim text
  11. .ft CW
  12. .nf
  13. .ne \\$1
  14. ..
  15. .de Ve \" End verbatim text
  16. .ft R
  17. .fi
  18. ..
  19. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
  20. .ie n \{\
  21. . ds C` ""
  22. . ds C' ""
  23. 'br\}
  24. .el\{\
  25. . ds C`
  26. . ds C'
  27. 'br\}
  28. .\"
  29. .\" Escape single quotes in literal strings from groff's Unicode transform.
  30. .ie \n(.g .ds Aq \(aq
  31. .el .ds Aq '
  32. .\"
  33. .\" If the F register is >0, we'll generate index entries on stderr for
  34. .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
  35. .\" entries marked with X<> in POD. Of course, you'll have to process the
  36. .\" output yourself in some meaningful fashion.
  37. .\"
  38. .\" Avoid warning from groff about undefined register 'F'.
  39. .de IX
  40. ..
  41. .nr rF 0
  42. .if \n(.g .if rF .nr rF 1
  43. .if (\n(rF:(\n(.g==0)) \{\
  44. . if \nF \{\
  45. . de IX
  46. . tm Index:\\$1\t\\n%\t"\\$2"
  47. ..
  48. . if !\nF==2 \{\
  49. . nr % 0
  50. . nr F 2
  51. . \}
  52. . \}
  53. .\}
  54. .rr rF
  55. .\" ========================================================================
  56. .\"
  57. .IX Title "EVP_MAC 3ossl"
  58. .TH EVP_MAC 3ossl 2025-01-17 3.4.0 OpenSSL
  59. .\" For nroff, turn off justification. Always turn off hyphenation; it makes
  60. .\" way too many mistakes in technical documents.
  61. .if n .ad l
  62. .nh
  63. .SH NAME
  64. EVP_MAC, EVP_MAC_fetch, EVP_MAC_up_ref, EVP_MAC_free, EVP_MAC_is_a,
  65. EVP_MAC_get0_name, EVP_MAC_names_do_all, EVP_MAC_get0_description,
  66. EVP_MAC_get0_provider, EVP_MAC_get_params, EVP_MAC_gettable_params,
  67. EVP_MAC_CTX, EVP_MAC_CTX_new, EVP_MAC_CTX_free, EVP_MAC_CTX_dup,
  68. EVP_MAC_CTX_get0_mac, EVP_MAC_CTX_get_params, EVP_MAC_CTX_set_params,
  69. EVP_MAC_CTX_get_mac_size, EVP_MAC_CTX_get_block_size, EVP_Q_mac,
  70. EVP_MAC_init, EVP_MAC_update, EVP_MAC_final, EVP_MAC_finalXOF,
  71. EVP_MAC_gettable_ctx_params, EVP_MAC_settable_ctx_params,
  72. EVP_MAC_CTX_gettable_params, EVP_MAC_CTX_settable_params,
  73. EVP_MAC_do_all_provided \- EVP MAC routines
  74. .SH SYNOPSIS
  75. .IX Header "SYNOPSIS"
  76. .Vb 1
  77. \& #include <openssl/evp.h>
  78. \&
  79. \& typedef struct evp_mac_st EVP_MAC;
  80. \& typedef struct evp_mac_ctx_st EVP_MAC_CTX;
  81. \&
  82. \& EVP_MAC *EVP_MAC_fetch(OSSL_LIB_CTX *libctx, const char *algorithm,
  83. \& const char *properties);
  84. \& int EVP_MAC_up_ref(EVP_MAC *mac);
  85. \& void EVP_MAC_free(EVP_MAC *mac);
  86. \& int EVP_MAC_is_a(const EVP_MAC *mac, const char *name);
  87. \& const char *EVP_MAC_get0_name(const EVP_MAC *mac);
  88. \& int EVP_MAC_names_do_all(const EVP_MAC *mac,
  89. \& void (*fn)(const char *name, void *data),
  90. \& void *data);
  91. \& const char *EVP_MAC_get0_description(const EVP_MAC *mac);
  92. \& const OSSL_PROVIDER *EVP_MAC_get0_provider(const EVP_MAC *mac);
  93. \& int EVP_MAC_get_params(EVP_MAC *mac, OSSL_PARAM params[]);
  94. \&
  95. \& EVP_MAC_CTX *EVP_MAC_CTX_new(EVP_MAC *mac);
  96. \& void EVP_MAC_CTX_free(EVP_MAC_CTX *ctx);
  97. \& EVP_MAC_CTX *EVP_MAC_CTX_dup(const EVP_MAC_CTX *src);
  98. \& EVP_MAC *EVP_MAC_CTX_get0_mac(EVP_MAC_CTX *ctx);
  99. \& int EVP_MAC_CTX_get_params(EVP_MAC_CTX *ctx, OSSL_PARAM params[]);
  100. \& int EVP_MAC_CTX_set_params(EVP_MAC_CTX *ctx, const OSSL_PARAM params[]);
  101. \&
  102. \& size_t EVP_MAC_CTX_get_mac_size(EVP_MAC_CTX *ctx);
  103. \& size_t EVP_MAC_CTX_get_block_size(EVP_MAC_CTX *ctx);
  104. \& unsigned char *EVP_Q_mac(OSSL_LIB_CTX *libctx, const char *name, const char *propq,
  105. \& const char *subalg, const OSSL_PARAM *params,
  106. \& const void *key, size_t keylen,
  107. \& const unsigned char *data, size_t datalen,
  108. \& unsigned char *out, size_t outsize, size_t *outlen);
  109. \& int EVP_MAC_init(EVP_MAC_CTX *ctx, const unsigned char *key, size_t keylen,
  110. \& const OSSL_PARAM params[]);
  111. \& int EVP_MAC_update(EVP_MAC_CTX *ctx, const unsigned char *data, size_t datalen);
  112. \& int EVP_MAC_final(EVP_MAC_CTX *ctx,
  113. \& unsigned char *out, size_t *outl, size_t outsize);
  114. \& int EVP_MAC_finalXOF(EVP_MAC_CTX *ctx, unsigned char *out, size_t outsize);
  115. \&
  116. \& const OSSL_PARAM *EVP_MAC_gettable_params(const EVP_MAC *mac);
  117. \& const OSSL_PARAM *EVP_MAC_gettable_ctx_params(const EVP_MAC *mac);
  118. \& const OSSL_PARAM *EVP_MAC_settable_ctx_params(const EVP_MAC *mac);
  119. \& const OSSL_PARAM *EVP_MAC_CTX_gettable_params(EVP_MAC_CTX *ctx);
  120. \& const OSSL_PARAM *EVP_MAC_CTX_settable_params(EVP_MAC_CTX *ctx);
  121. \&
  122. \& void EVP_MAC_do_all_provided(OSSL_LIB_CTX *libctx,
  123. \& void (*fn)(EVP_MAC *mac, void *arg),
  124. \& void *arg);
  125. .Ve
  126. .SH DESCRIPTION
  127. .IX Header "DESCRIPTION"
  128. These types and functions help the application to calculate MACs of
  129. different types and with different underlying algorithms if there are
  130. any.
  131. .PP
  132. MACs are a bit complex insofar that some of them use other algorithms
  133. for actual computation. HMAC uses a digest, and CMAC uses a cipher.
  134. Therefore, there are sometimes two contexts to keep track of, one for
  135. the MAC algorithm itself and one for the underlying computation
  136. algorithm if there is one.
  137. .PP
  138. To make things less ambiguous, this manual talks about a "context" or
  139. "MAC context", which is to denote the MAC level context, and about a
  140. "underlying context", or "computation context", which is to denote the
  141. context for the underlying computation algorithm if there is one.
  142. .SS Types
  143. .IX Subsection "Types"
  144. \&\fBEVP_MAC\fR is a type that holds the implementation of a MAC.
  145. .PP
  146. \&\fBEVP_MAC_CTX\fR is a context type that holds internal MAC information
  147. as well as a reference to a computation context, for those MACs that
  148. rely on an underlying computation algorithm.
  149. .SS "Algorithm implementation fetching"
  150. .IX Subsection "Algorithm implementation fetching"
  151. \&\fBEVP_MAC_fetch()\fR fetches an implementation of a MAC \fIalgorithm\fR, given
  152. a library context \fIlibctx\fR and a set of \fIproperties\fR.
  153. See "ALGORITHM FETCHING" in \fBcrypto\fR\|(7) for further information.
  154. .PP
  155. See "Message Authentication Code (MAC)" in \fBOSSL_PROVIDER\-default\fR\|(7) for the list
  156. of algorithms supported by the default provider.
  157. .PP
  158. The returned value must eventually be freed with
  159. \&\fBEVP_MAC_free\fR\|(3).
  160. .PP
  161. \&\fBEVP_MAC_up_ref()\fR increments the reference count of an already fetched
  162. MAC.
  163. .PP
  164. \&\fBEVP_MAC_free()\fR frees a fetched algorithm.
  165. NULL is a valid parameter, for which this function is a no-op.
  166. .SS "Context manipulation functions"
  167. .IX Subsection "Context manipulation functions"
  168. \&\fBEVP_MAC_CTX_new()\fR creates a new context for the MAC type \fImac\fR.
  169. The created context can then be used with most other functions
  170. described here.
  171. .PP
  172. \&\fBEVP_MAC_CTX_free()\fR frees the contents of the context, including an
  173. underlying context if there is one, as well as the context itself.
  174. NULL is a valid parameter, for which this function is a no-op.
  175. .PP
  176. \&\fBEVP_MAC_CTX_dup()\fR duplicates the \fIsrc\fR context and returns a newly allocated
  177. context.
  178. .PP
  179. \&\fBEVP_MAC_CTX_get0_mac()\fR returns the \fBEVP_MAC\fR associated with the context
  180. \&\fIctx\fR.
  181. .SS "Computing functions"
  182. .IX Subsection "Computing functions"
  183. \&\fBEVP_Q_mac()\fR computes the message authentication code
  184. of \fIdata\fR with length \fIdatalen\fR
  185. using the MAC algorithm \fIname\fR and the key \fIkey\fR with length \fIkeylen\fR.
  186. The MAC algorithm is fetched using any given \fIlibctx\fR and property query
  187. string \fIpropq\fR. It takes parameters \fIsubalg\fR and further \fIparams\fR,
  188. both of which may be NULL if not needed.
  189. If \fIout\fR is not NULL, it places the result in the memory pointed at by \fIout\fR,
  190. but only if \fIoutsize\fR is sufficient (otherwise no computation is made).
  191. If \fIout\fR is NULL, it allocates and uses a buffer of suitable length,
  192. which will be returned on success and must be freed by the caller.
  193. In either case, also on error,
  194. it assigns the number of bytes written to \fI*outlen\fR unless \fIoutlen\fR is NULL.
  195. .PP
  196. \&\fBEVP_MAC_init()\fR sets up the underlying context \fIctx\fR with information given
  197. via the \fIkey\fR and \fIparams\fR arguments. The MAC \fIkey\fR has a length of
  198. \&\fIkeylen\fR and the parameters in \fIparams\fR are processed before setting
  199. the key. If \fIkey\fR is NULL, the key must be set via \fIparams\fR either
  200. as part of this call or separately using \fBEVP_MAC_CTX_set_params()\fR.
  201. Providing non-NULL \fIparams\fR to this function is equivalent to calling
  202. \&\fBEVP_MAC_CTX_set_params()\fR with those \fIparams\fR for the same \fIctx\fR beforehand.
  203. Note: There are additional requirements for some MAC algorithms during
  204. re-initalization (i.e. calling \fBEVP_MAC_init()\fR on an EVP_MAC after \fBEVP_MAC_final()\fR
  205. has been called on the same object). See the NOTES section below.
  206. .PP
  207. \&\fBEVP_MAC_init()\fR should be called before \fBEVP_MAC_update()\fR and \fBEVP_MAC_final()\fR.
  208. .PP
  209. \&\fBEVP_MAC_update()\fR adds \fIdatalen\fR bytes from \fIdata\fR to the MAC input.
  210. .PP
  211. \&\fBEVP_MAC_final()\fR does the final computation and stores the result in
  212. the memory pointed at by \fIout\fR of size \fIoutsize\fR, and sets the number
  213. of bytes written in \fI*outl\fR at.
  214. If \fIout\fR is NULL or \fIoutsize\fR is too small, then no computation
  215. is made.
  216. To figure out what the output length will be and allocate space for it
  217. dynamically, simply call with \fIout\fR being NULL and \fIoutl\fR
  218. pointing at a valid location, then allocate space and make a second
  219. call with \fIout\fR pointing at the allocated space.
  220. .PP
  221. \&\fBEVP_MAC_finalXOF()\fR does the final computation for an XOF based MAC and stores
  222. the result in the memory pointed at by \fIout\fR of size \fIoutsize\fR.
  223. .PP
  224. \&\fBEVP_MAC_get_params()\fR retrieves details about the implementation
  225. \&\fImac\fR.
  226. The set of parameters given with \fIparams\fR determine exactly what
  227. parameters should be retrieved.
  228. Note that a parameter that is unknown in the underlying context is
  229. simply ignored.
  230. .PP
  231. \&\fBEVP_MAC_CTX_get_params()\fR retrieves chosen parameters, given the
  232. context \fIctx\fR and its underlying context.
  233. The set of parameters given with \fIparams\fR determine exactly what
  234. parameters should be retrieved.
  235. Note that a parameter that is unknown in the underlying context is
  236. simply ignored.
  237. .PP
  238. \&\fBEVP_MAC_CTX_set_params()\fR passes chosen parameters to the underlying
  239. context, given a context \fIctx\fR.
  240. The set of parameters given with \fIparams\fR determine exactly what
  241. parameters are passed down.
  242. If \fIparams\fR are NULL, the underlying context should do nothing and return 1.
  243. Note that a parameter that is unknown in the underlying context is
  244. simply ignored.
  245. Also, what happens when a needed parameter isn't passed down is
  246. defined by the implementation.
  247. .PP
  248. \&\fBEVP_MAC_gettable_params()\fR returns an \fBOSSL_PARAM\fR\|(3) array that describes
  249. the retrievable and settable parameters. \fBEVP_MAC_gettable_params()\fR
  250. returns parameters that can be used with \fBEVP_MAC_get_params()\fR.
  251. .PP
  252. \&\fBEVP_MAC_gettable_ctx_params()\fR and \fBEVP_MAC_CTX_gettable_params()\fR
  253. return constant \fBOSSL_PARAM\fR\|(3) arrays that describe the retrievable
  254. parameters that can be used with \fBEVP_MAC_CTX_get_params()\fR.
  255. \&\fBEVP_MAC_gettable_ctx_params()\fR returns the parameters that can be retrieved
  256. from the algorithm, whereas \fBEVP_MAC_CTX_gettable_params()\fR returns
  257. the parameters that can be retrieved in the context's current state.
  258. .PP
  259. \&\fBEVP_MAC_settable_ctx_params()\fR and \fBEVP_MAC_CTX_settable_params()\fR return
  260. constant \fBOSSL_PARAM\fR\|(3) arrays that describe the settable parameters that
  261. can be used with \fBEVP_MAC_CTX_set_params()\fR. \fBEVP_MAC_settable_ctx_params()\fR
  262. returns the parameters that can be retrieved from the algorithm,
  263. whereas \fBEVP_MAC_CTX_settable_params()\fR returns the parameters that can
  264. be retrieved in the context's current state.
  265. .SS "Information functions"
  266. .IX Subsection "Information functions"
  267. \&\fBEVP_MAC_CTX_get_mac_size()\fR returns the MAC output size for the given context.
  268. .PP
  269. \&\fBEVP_MAC_CTX_get_block_size()\fR returns the MAC block size for the given context.
  270. Not all MAC algorithms support this.
  271. .PP
  272. \&\fBEVP_MAC_is_a()\fR checks if the given \fImac\fR is an implementation of an
  273. algorithm that's identifiable with \fIname\fR.
  274. .PP
  275. \&\fBEVP_MAC_get0_provider()\fR returns the provider that holds the implementation
  276. of the given \fImac\fR.
  277. .PP
  278. \&\fBEVP_MAC_do_all_provided()\fR traverses all MAC implemented by all activated
  279. providers in the given library context \fIlibctx\fR, and for each of the
  280. implementations, calls the given function \fIfn\fR with the implementation method
  281. and the given \fIarg\fR as argument.
  282. .PP
  283. \&\fBEVP_MAC_get0_name()\fR return the name of the given MAC. For fetched MACs
  284. with multiple names, only one of them is returned; it's
  285. recommended to use \fBEVP_MAC_names_do_all()\fR instead.
  286. .PP
  287. \&\fBEVP_MAC_names_do_all()\fR traverses all names for \fImac\fR, and calls
  288. \&\fIfn\fR with each name and \fIdata\fR.
  289. .PP
  290. \&\fBEVP_MAC_get0_description()\fR returns a description of the \fImac\fR, meant
  291. for display and human consumption. The description is at the discretion
  292. of the mac implementation.
  293. .SH PARAMETERS
  294. .IX Header "PARAMETERS"
  295. Parameters are identified by name as strings, and have an expected
  296. data type and maximum size.
  297. OpenSSL has a set of macros for parameter names it expects to see in
  298. its own MAC implementations.
  299. Here, we show all three, the OpenSSL macro for the parameter name, the
  300. name in string form, and a type description.
  301. .PP
  302. The standard parameter names are:
  303. .IP """key"" (\fBOSSL_MAC_PARAM_KEY\fR) <octet string>" 4
  304. .IX Item """key"" (OSSL_MAC_PARAM_KEY) <octet string>"
  305. Its value is the MAC key as an array of bytes.
  306. .Sp
  307. For MACs that use an underlying computation algorithm, the algorithm
  308. must be set first, see parameter names "algorithm" below.
  309. .IP """iv"" (\fBOSSL_MAC_PARAM_IV\fR) <octet string>" 4
  310. .IX Item """iv"" (OSSL_MAC_PARAM_IV) <octet string>"
  311. Some MAC implementations (GMAC) require an IV, this parameter sets the IV.
  312. .IP """custom"" (\fBOSSL_MAC_PARAM_CUSTOM\fR) <octet string>" 4
  313. .IX Item """custom"" (OSSL_MAC_PARAM_CUSTOM) <octet string>"
  314. Some MAC implementations (KMAC, BLAKE2) accept a Customization String,
  315. this parameter sets the Customization String. The default value is the
  316. empty string.
  317. .IP """salt"" (\fBOSSL_MAC_PARAM_SALT\fR) <octet string>" 4
  318. .IX Item """salt"" (OSSL_MAC_PARAM_SALT) <octet string>"
  319. This option is used by BLAKE2 MAC.
  320. .IP """xof"" (\fBOSSL_MAC_PARAM_XOF\fR) <integer>" 4
  321. .IX Item """xof"" (OSSL_MAC_PARAM_XOF) <integer>"
  322. It's a simple flag, the value 0 or 1 are expected.
  323. .Sp
  324. This option is used by KMAC.
  325. .IP """digest-noinit"" (\fBOSSL_MAC_PARAM_DIGEST_NOINIT\fR) <integer>" 4
  326. .IX Item """digest-noinit"" (OSSL_MAC_PARAM_DIGEST_NOINIT) <integer>"
  327. A simple flag to set the MAC digest to not initialise the
  328. implementation specific data. The value 0 or 1 is expected.
  329. .Sp
  330. This option is deprecated and will be removed in a future release.
  331. The option may be set, but is ignored.
  332. .IP """digest-oneshot"" (\fBOSSL_MAC_PARAM_DIGEST_ONESHOT\fR) <integer>" 4
  333. .IX Item """digest-oneshot"" (OSSL_MAC_PARAM_DIGEST_ONESHOT) <integer>"
  334. A simple flag to set the MAC digest to be a oneshot operation.
  335. The value 0 or 1 is expected.
  336. .Sp
  337. This option is deprecated and will be removed in a future release.
  338. The option may be set, but is ignored.
  339. .IP """properties"" (\fBOSSL_MAC_PARAM_PROPERTIES\fR) <UTF8 string>" 4
  340. .IX Item """properties"" (OSSL_MAC_PARAM_PROPERTIES) <UTF8 string>"
  341. .PD 0
  342. .IP """digest"" (\fBOSSL_MAC_PARAM_DIGEST\fR) <UTF8 string>" 4
  343. .IX Item """digest"" (OSSL_MAC_PARAM_DIGEST) <UTF8 string>"
  344. .IP """cipher"" (\fBOSSL_MAC_PARAM_CIPHER\fR) <UTF8 string>" 4
  345. .IX Item """cipher"" (OSSL_MAC_PARAM_CIPHER) <UTF8 string>"
  346. .PD
  347. For MAC implementations that use an underlying computation cipher or
  348. digest, these parameters set what the algorithm should be.
  349. .Sp
  350. The value is always the name of the intended algorithm,
  351. or the properties.
  352. .Sp
  353. Note that not all algorithms may support all digests.
  354. HMAC does not support variable output length digests such as SHAKE128
  355. or SHAKE256.
  356. .IP """size"" (\fBOSSL_MAC_PARAM_SIZE\fR) <unsigned integer>" 4
  357. .IX Item """size"" (OSSL_MAC_PARAM_SIZE) <unsigned integer>"
  358. For MAC implementations that support it, set the output size that
  359. \&\fBEVP_MAC_final()\fR should produce.
  360. The allowed sizes vary between MAC implementations, but must never exceed
  361. what can be given with a \fBsize_t\fR.
  362. .IP """tls-data-size"" (\fBOSSL_MAC_PARAM_TLS_DATA_SIZE\fR) <unsigned integer>" 4
  363. .IX Item """tls-data-size"" (OSSL_MAC_PARAM_TLS_DATA_SIZE) <unsigned integer>"
  364. This parameter is only supported by HMAC. If set then special handling is
  365. activated for calculating the MAC of a received mac-then-encrypt TLS record
  366. where variable length record padding has been used (as in the case of CBC mode
  367. ciphersuites). The value represents the total length of the record that is
  368. having the MAC calculated including the received MAC and the record padding.
  369. .Sp
  370. When used EVP_MAC_update must be called precisely twice. The first time with
  371. the 13 bytes of TLS "header" data, and the second time with the entire record
  372. including the MAC itself and any padding. The entire record length must equal
  373. the value passed in the "tls-data-size" parameter. The length passed in the
  374. \&\fBdatalen\fR parameter to \fBEVP_MAC_update()\fR should be equal to the length of the
  375. record after the MAC and any padding has been removed.
  376. .PP
  377. All these parameters should be used before the calls to any of
  378. \&\fBEVP_MAC_init()\fR, \fBEVP_MAC_update()\fR and \fBEVP_MAC_final()\fR for a full
  379. computation.
  380. Anything else may give undefined results.
  381. .SH NOTES
  382. .IX Header "NOTES"
  383. The MAC life-cycle is described in \fBlife_cycle\-mac\fR\|(7). In the future,
  384. the transitions described there will be enforced. When this is done, it will
  385. not be considered a breaking change to the API.
  386. .PP
  387. The usage of the parameter names "custom", "iv" and "salt" correspond to
  388. the names used in the standard where the algorithm was defined.
  389. .PP
  390. Some MAC algorithms store internal state that cannot be extracted during
  391. re-initalization. For example GMAC cannot extract an \fBIV\fR from the
  392. underlying CIPHER context, and so calling \fBEVP_MAC_init()\fR on an EVP_MAC object
  393. after \fBEVP_MAC_final()\fR has been called cannot reset its cipher state to what it
  394. was when the \fBIV\fR was initially generated. For such instances, an
  395. \&\fBOSSL_MAC_PARAM_IV\fR parameter must be passed with each call to \fBEVP_MAC_init()\fR.
  396. .SH "RETURN VALUES"
  397. .IX Header "RETURN VALUES"
  398. \&\fBEVP_MAC_fetch()\fR returns a pointer to a newly fetched \fBEVP_MAC\fR, or
  399. NULL if allocation failed.
  400. .PP
  401. \&\fBEVP_MAC_up_ref()\fR returns 1 on success, 0 on error.
  402. .PP
  403. \&\fBEVP_MAC_names_do_all()\fR returns 1 if the callback was called for all names. A
  404. return value of 0 means that the callback was not called for any names.
  405. .PP
  406. \&\fBEVP_MAC_free()\fR returns nothing at all.
  407. .PP
  408. \&\fBEVP_MAC_is_a()\fR returns 1 if the given method can be identified with
  409. the given name, otherwise 0.
  410. .PP
  411. \&\fBEVP_MAC_get0_name()\fR returns a name of the MAC, or NULL on error.
  412. .PP
  413. \&\fBEVP_MAC_get0_provider()\fR returns a pointer to the provider for the MAC, or
  414. NULL on error.
  415. .PP
  416. \&\fBEVP_MAC_CTX_new()\fR and \fBEVP_MAC_CTX_dup()\fR return a pointer to a newly
  417. created EVP_MAC_CTX, or NULL if allocation failed.
  418. .PP
  419. \&\fBEVP_MAC_CTX_free()\fR returns nothing at all.
  420. .PP
  421. \&\fBEVP_MAC_CTX_get_params()\fR and \fBEVP_MAC_CTX_set_params()\fR return 1 on
  422. success, 0 on error.
  423. .PP
  424. \&\fBEVP_Q_mac()\fR returns a pointer to the computed MAC value, or NULL on error.
  425. .PP
  426. \&\fBEVP_MAC_init()\fR, \fBEVP_MAC_update()\fR, \fBEVP_MAC_final()\fR, and \fBEVP_MAC_finalXOF()\fR
  427. return 1 on success, 0 on error.
  428. .PP
  429. \&\fBEVP_MAC_CTX_get_mac_size()\fR returns the expected output size, or 0 if it isn't
  430. set. If it isn't set, a call to \fBEVP_MAC_init()\fR will set it.
  431. .PP
  432. \&\fBEVP_MAC_CTX_get_block_size()\fR returns the block size, or 0 if it isn't set.
  433. If it isn't set, a call to \fBEVP_MAC_init()\fR will set it.
  434. .PP
  435. \&\fBEVP_MAC_do_all_provided()\fR returns nothing at all.
  436. .SH EXAMPLES
  437. .IX Header "EXAMPLES"
  438. .Vb 5
  439. \& #include <stdlib.h>
  440. \& #include <stdio.h>
  441. \& #include <string.h>
  442. \& #include <stdarg.h>
  443. \& #include <unistd.h>
  444. \&
  445. \& #include <openssl/evp.h>
  446. \& #include <openssl/err.h>
  447. \& #include <openssl/params.h>
  448. \&
  449. \& int main() {
  450. \& EVP_MAC *mac = EVP_MAC_fetch(NULL, getenv("MY_MAC"), NULL);
  451. \& const char *cipher = getenv("MY_MAC_CIPHER");
  452. \& const char *digest = getenv("MY_MAC_DIGEST");
  453. \& const char *key = getenv("MY_KEY");
  454. \& EVP_MAC_CTX *ctx = NULL;
  455. \&
  456. \& unsigned char buf[4096];
  457. \& size_t read_l;
  458. \& size_t final_l;
  459. \&
  460. \& size_t i;
  461. \&
  462. \& OSSL_PARAM params[3];
  463. \& size_t params_n = 0;
  464. \&
  465. \& if (cipher != NULL)
  466. \& params[params_n++] =
  467. \& OSSL_PARAM_construct_utf8_string("cipher", (char*)cipher, 0);
  468. \& if (digest != NULL)
  469. \& params[params_n++] =
  470. \& OSSL_PARAM_construct_utf8_string("digest", (char*)digest, 0);
  471. \& params[params_n] = OSSL_PARAM_construct_end();
  472. \&
  473. \& if (mac == NULL
  474. \& || key == NULL
  475. \& || (ctx = EVP_MAC_CTX_new(mac)) == NULL
  476. \& || !EVP_MAC_init(ctx, (const unsigned char *)key, strlen(key),
  477. \& params))
  478. \& goto err;
  479. \&
  480. \& while ( (read_l = read(STDIN_FILENO, buf, sizeof(buf))) > 0) {
  481. \& if (!EVP_MAC_update(ctx, buf, read_l))
  482. \& goto err;
  483. \& }
  484. \&
  485. \& if (!EVP_MAC_final(ctx, buf, &final_l, sizeof(buf)))
  486. \& goto err;
  487. \&
  488. \& printf("Result: ");
  489. \& for (i = 0; i < final_l; i++)
  490. \& printf("%02X", buf[i]);
  491. \& printf("\en");
  492. \&
  493. \& EVP_MAC_CTX_free(ctx);
  494. \& EVP_MAC_free(mac);
  495. \& exit(0);
  496. \&
  497. \& err:
  498. \& EVP_MAC_CTX_free(ctx);
  499. \& EVP_MAC_free(mac);
  500. \& fprintf(stderr, "Something went wrong\en");
  501. \& ERR_print_errors_fp(stderr);
  502. \& exit (1);
  503. \& }
  504. .Ve
  505. .PP
  506. A run of this program, called with correct environment variables, can
  507. look like this:
  508. .PP
  509. .Vb 3
  510. \& $ MY_MAC=cmac MY_KEY=secret0123456789 MY_MAC_CIPHER=aes\-128\-cbc \e
  511. \& LD_LIBRARY_PATH=. ./foo < foo.c
  512. \& Result: C5C06683CD9DDEF904D754505C560A4E
  513. .Ve
  514. .PP
  515. (in this example, that program was stored in \fIfoo.c\fR and compiled to
  516. \&\fI./foo\fR)
  517. .SH "SEE ALSO"
  518. .IX Header "SEE ALSO"
  519. \&\fBproperty\fR\|(7)
  520. \&\fBOSSL_PARAM\fR\|(3),
  521. \&\fBEVP_MAC\-BLAKE2\fR\|(7),
  522. \&\fBEVP_MAC\-CMAC\fR\|(7),
  523. \&\fBEVP_MAC\-GMAC\fR\|(7),
  524. \&\fBEVP_MAC\-HMAC\fR\|(7),
  525. \&\fBEVP_MAC\-KMAC\fR\|(7),
  526. \&\fBEVP_MAC\-Siphash\fR\|(7),
  527. \&\fBEVP_MAC\-Poly1305\fR\|(7),
  528. \&\fBprovider\-mac\fR\|(7),
  529. \&\fBlife_cycle\-mac\fR\|(7)
  530. .SH HISTORY
  531. .IX Header "HISTORY"
  532. These functions were added in OpenSSL 3.0.
  533. .SH COPYRIGHT
  534. .IX Header "COPYRIGHT"
  535. Copyright 2018\-2024 The OpenSSL Project Authors. All Rights Reserved.
  536. .PP
  537. Licensed under the Apache License 2.0 (the "License"). You may not use
  538. this file except in compliance with the License. You can obtain a copy
  539. in the file LICENSE in the source distribution or at
  540. <https://www.openssl.org/source/license.html>.