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