EVP_MAC-KMAC.pod 3.8 KB

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