1*b077aed3SPierre Pronchery=pod 2*b077aed3SPierre Pronchery 3*b077aed3SPierre Pronchery=head1 NAME 4*b077aed3SPierre Pronchery 5*b077aed3SPierre ProncheryEVP_MAC-KMAC, EVP_MAC-KMAC128, EVP_MAC-KMAC256 6*b077aed3SPierre Pronchery- The KMAC EVP_MAC implementations 7*b077aed3SPierre Pronchery 8*b077aed3SPierre Pronchery=head1 DESCRIPTION 9*b077aed3SPierre Pronchery 10*b077aed3SPierre ProncherySupport for computing KMAC MACs through the B<EVP_MAC> API. 11*b077aed3SPierre Pronchery 12*b077aed3SPierre Pronchery=head2 Identity 13*b077aed3SPierre Pronchery 14*b077aed3SPierre ProncheryThese implementations are identified with one of these names and 15*b077aed3SPierre Proncheryproperties, to be used with EVP_MAC_fetch(): 16*b077aed3SPierre Pronchery 17*b077aed3SPierre Pronchery=over 4 18*b077aed3SPierre Pronchery 19*b077aed3SPierre Pronchery=item "KMAC-128", "provider=default" or "provider=fips" 20*b077aed3SPierre Pronchery 21*b077aed3SPierre Pronchery=item "KMAC-256", "provider=default" or "provider=fips" 22*b077aed3SPierre Pronchery 23*b077aed3SPierre Pronchery=back 24*b077aed3SPierre Pronchery 25*b077aed3SPierre Pronchery=head2 Supported parameters 26*b077aed3SPierre Pronchery 27*b077aed3SPierre ProncheryThe general description of these parameters can be found in 28*b077aed3SPierre ProncheryL<EVP_MAC(3)/PARAMETERS>. 29*b077aed3SPierre Pronchery 30*b077aed3SPierre ProncheryAll these parameters can be set with EVP_MAC_CTX_set_params(). 31*b077aed3SPierre ProncheryFurthermore, the "size" parameter can be retrieved with 32*b077aed3SPierre ProncheryEVP_MAC_CTX_get_params(), or with EVP_MAC_CTX_get_mac_size(). 33*b077aed3SPierre ProncheryThe length of the "size" parameter should not exceed that of a B<size_t>. 34*b077aed3SPierre ProncheryLikewise, the "block-size" parameter can be retrieved with 35*b077aed3SPierre ProncheryEVP_MAC_CTX_get_params(), or with EVP_MAC_CTX_get_block_size(). 36*b077aed3SPierre Pronchery 37*b077aed3SPierre Pronchery 38*b077aed3SPierre Pronchery=over 4 39*b077aed3SPierre Pronchery 40*b077aed3SPierre Pronchery=item "key" (B<OSSL_MAC_PARAM_KEY>) <octet string> 41*b077aed3SPierre Pronchery 42*b077aed3SPierre ProncherySets the MAC key. 43*b077aed3SPierre ProncherySetting this parameter is identical to passing a I<key> to L<EVP_MAC_init(3)>. 44*b077aed3SPierre ProncheryThe length of the key (in bytes) must be in the range 4...512. 45*b077aed3SPierre Pronchery 46*b077aed3SPierre Pronchery=item "custom" (B<OSSL_MAC_PARAM_CUSTOM>) <octet string> 47*b077aed3SPierre Pronchery 48*b077aed3SPierre ProncherySets the custom value. 49*b077aed3SPierre ProncheryIt is an optional value with a length of at most 512 bytes, and is empty by default. 50*b077aed3SPierre Pronchery 51*b077aed3SPierre Pronchery=item "size" (B<OSSL_MAC_PARAM_SIZE>) <unsigned integer> 52*b077aed3SPierre Pronchery 53*b077aed3SPierre ProncherySets the MAC size. 54*b077aed3SPierre ProncheryBy default, it is 16 for C<KMAC-128> and 32 for C<KMAC-256>. 55*b077aed3SPierre Pronchery 56*b077aed3SPierre Pronchery=item "block-size" (B<OSSL_MAC_PARAM_SIZE>) <unsigned integer> 57*b077aed3SPierre Pronchery 58*b077aed3SPierre ProncheryGets the MAC block size. 59*b077aed3SPierre ProncheryBy default, it is 168 for C<KMAC-128> and 136 for C<KMAC-256>. 60*b077aed3SPierre Pronchery 61*b077aed3SPierre Pronchery=item "xof" (B<OSSL_MAC_PARAM_XOF>) <integer> 62*b077aed3SPierre Pronchery 63*b077aed3SPierre ProncheryThe "xof" parameter value is expected to be 1 or 0. Use 1 to enable XOF mode. 64*b077aed3SPierre ProncheryThe default value is 0. 65*b077aed3SPierre Pronchery 66*b077aed3SPierre Pronchery=back 67*b077aed3SPierre Pronchery 68*b077aed3SPierre ProncheryThe "custom" parameter must be set as part of or before the EVP_MAC_init() call. 69*b077aed3SPierre ProncheryThe "xof" and "size" parameters can be set at any time before EVP_MAC_final(). 70*b077aed3SPierre ProncheryThe "key" parameter is set as part of the EVP_MAC_init() call, but can be 71*b077aed3SPierre Proncheryset before it instead. 72*b077aed3SPierre Pronchery 73*b077aed3SPierre Pronchery=head1 EXAMPLES 74*b077aed3SPierre Pronchery 75*b077aed3SPierre Pronchery #include <openssl/evp.h> 76*b077aed3SPierre Pronchery #include <openssl/params.h> 77*b077aed3SPierre Pronchery 78*b077aed3SPierre Pronchery static int do_kmac(const unsigned char *in, size_t in_len, 79*b077aed3SPierre Pronchery const unsigned char *key, size_t key_len, 80*b077aed3SPierre Pronchery const unsigned char *custom, size_t custom_len, 81*b077aed3SPierre Pronchery int xof_enabled, unsigned char *out, int out_len) 82*b077aed3SPierre Pronchery { 83*b077aed3SPierre Pronchery EVP_MAC_CTX *ctx = NULL; 84*b077aed3SPierre Pronchery EVP_MAC *mac = NULL; 85*b077aed3SPierre Pronchery OSSL_PARAM params[4], *p; 86*b077aed3SPierre Pronchery int ret = 0; 87*b077aed3SPierre Pronchery size_t l = 0; 88*b077aed3SPierre Pronchery 89*b077aed3SPierre Pronchery mac = EVP_MAC_fetch(NULL, "KMAC-128", NULL); 90*b077aed3SPierre Pronchery if (mac == NULL) 91*b077aed3SPierre Pronchery goto err; 92*b077aed3SPierre Pronchery ctx = EVP_MAC_CTX_new(mac); 93*b077aed3SPierre Pronchery /* The mac can be freed after it is used by EVP_MAC_CTX_new */ 94*b077aed3SPierre Pronchery EVP_MAC_free(mac); 95*b077aed3SPierre Pronchery if (ctx == NULL) 96*b077aed3SPierre Pronchery goto err; 97*b077aed3SPierre Pronchery 98*b077aed3SPierre Pronchery /* 99*b077aed3SPierre Pronchery * Setup parameters required before calling EVP_MAC_init() 100*b077aed3SPierre Pronchery * The parameters OSSL_MAC_PARAM_XOF and OSSL_MAC_PARAM_SIZE may also be 101*b077aed3SPierre Pronchery * used at this point. 102*b077aed3SPierre Pronchery */ 103*b077aed3SPierre Pronchery p = params; 104*b077aed3SPierre Pronchery *p++ = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY, 105*b077aed3SPierre Pronchery (void *)key, key_len); 106*b077aed3SPierre Pronchery if (custom != NULL && custom_len != 0) 107*b077aed3SPierre Pronchery *p++ = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_CUSTOM, 108*b077aed3SPierre Pronchery (void *)custom, custom_len); 109*b077aed3SPierre Pronchery *p = OSSL_PARAM_construct_end(); 110*b077aed3SPierre Pronchery if (!EVP_MAC_CTX_set_params(ctx, params)) 111*b077aed3SPierre Pronchery goto err; 112*b077aed3SPierre Pronchery 113*b077aed3SPierre Pronchery if (!EVP_MAC_init(ctx)) 114*b077aed3SPierre Pronchery goto err; 115*b077aed3SPierre Pronchery 116*b077aed3SPierre Pronchery /* 117*b077aed3SPierre Pronchery * Note: the following optional parameters can be set any time 118*b077aed3SPierre Pronchery * before EVP_MAC_final(). 119*b077aed3SPierre Pronchery */ 120*b077aed3SPierre Pronchery p = params; 121*b077aed3SPierre Pronchery *p++ = OSSL_PARAM_construct_int(OSSL_MAC_PARAM_XOF, &xof_enabled); 122*b077aed3SPierre Pronchery *p++ = OSSL_PARAM_construct_int(OSSL_MAC_PARAM_SIZE, &out_len); 123*b077aed3SPierre Pronchery *p = OSSL_PARAM_construct_end(); 124*b077aed3SPierre Pronchery if (!EVP_MAC_CTX_set_params(ctx, params)) 125*b077aed3SPierre Pronchery goto err; 126*b077aed3SPierre Pronchery 127*b077aed3SPierre Pronchery /* The update may be called multiple times here for streamed input */ 128*b077aed3SPierre Pronchery if (!EVP_MAC_update(ctx, in, in_len)) 129*b077aed3SPierre Pronchery goto err; 130*b077aed3SPierre Pronchery if (!EVP_MAC_final(ctx, out, &l, out_len)) 131*b077aed3SPierre Pronchery goto err; 132*b077aed3SPierre Pronchery ret = 1; 133*b077aed3SPierre Pronchery err: 134*b077aed3SPierre Pronchery EVP_MAC_CTX_free(ctx); 135*b077aed3SPierre Pronchery return ret; 136*b077aed3SPierre Pronchery } 137*b077aed3SPierre Pronchery 138*b077aed3SPierre Pronchery=head1 SEE ALSO 139*b077aed3SPierre Pronchery 140*b077aed3SPierre ProncheryL<EVP_MAC_CTX_get_params(3)>, L<EVP_MAC_CTX_set_params(3)>, 141*b077aed3SPierre ProncheryL<EVP_MAC(3)/PARAMETERS>, L<OSSL_PARAM(3)> 142*b077aed3SPierre Pronchery 143*b077aed3SPierre Pronchery=head1 COPYRIGHT 144*b077aed3SPierre Pronchery 145*b077aed3SPierre ProncheryCopyright 2018-2021 The OpenSSL Project Authors. All Rights Reserved. 146*b077aed3SPierre Pronchery 147*b077aed3SPierre ProncheryLicensed under the Apache License 2.0 (the "License"). You may not use 148*b077aed3SPierre Proncherythis file except in compliance with the License. You can obtain a copy 149*b077aed3SPierre Proncheryin the file LICENSE in the source distribution or at 150*b077aed3SPierre ProncheryL<https://www.openssl.org/source/license.html>. 151*b077aed3SPierre Pronchery 152*b077aed3SPierre Pronchery=cut 153