2
0

EVP_MAC-KMAC.pod 4.4 KB

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