EVP_MAC-KMAC.pod 4.5 KB

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