EVP_MD_xof.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*-
  2. * Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <openssl/err.h>
  12. #include <openssl/evp.h>
  13. #include <openssl/core_names.h>
  14. /*
  15. * Example of using an extendable-output hash function (XOF). A XOF is a hash
  16. * function with configurable output length and which can generate an
  17. * arbitrarily large output.
  18. *
  19. * This example uses SHAKE256, an extendable output variant of SHA3 (Keccak).
  20. *
  21. * To generate different output lengths, you can pass a single integer argument
  22. * on the command line, which is the output size in bytes. By default, a 20-byte
  23. * output is generated and (for this length only) a known answer test is
  24. * performed.
  25. */
  26. /* Our input to the XOF hash function. */
  27. const char message[] = "This is a test message.";
  28. /* Expected output when an output length of 20 bytes is used. */
  29. static const char known_answer[] = {
  30. 0x52, 0x97, 0x93, 0x78, 0x27, 0x58, 0x7d, 0x62,
  31. 0x8b, 0x00, 0x25, 0xb5, 0xec, 0x39, 0x5e, 0x2d,
  32. 0x7f, 0x3e, 0xd4, 0x19
  33. };
  34. /*
  35. * A property query used for selecting the SHAKE256 implementation.
  36. */
  37. static const char *propq = NULL;
  38. int main(int argc, char **argv)
  39. {
  40. int ret = EXIT_FAILURE;
  41. OSSL_LIB_CTX *libctx = NULL;
  42. EVP_MD *md = NULL;
  43. EVP_MD_CTX *ctx = NULL;
  44. unsigned int digest_len = 20;
  45. int digest_len_i;
  46. unsigned char *digest = NULL;
  47. /* Allow digest length to be changed for demonstration purposes. */
  48. if (argc > 1) {
  49. digest_len_i = atoi(argv[1]);
  50. if (digest_len_i <= 0) {
  51. fprintf(stderr, "Specify a non-negative digest length\n");
  52. goto end;
  53. }
  54. digest_len = (unsigned int)digest_len_i;
  55. }
  56. /*
  57. * Retrieve desired algorithm. This must be a hash algorithm which supports
  58. * XOF.
  59. */
  60. md = EVP_MD_fetch(libctx, "SHAKE256", propq);
  61. if (md == NULL) {
  62. fprintf(stderr, "Failed to retrieve SHAKE256 algorithm\n");
  63. goto end;
  64. }
  65. /* Create context. */
  66. ctx = EVP_MD_CTX_new();
  67. if (ctx == NULL) {
  68. fprintf(stderr, "Failed to create digest context\n");
  69. goto end;
  70. }
  71. /* Initialize digest context. */
  72. if (EVP_DigestInit(ctx, md) == 0) {
  73. fprintf(stderr, "Failed to initialize digest\n");
  74. goto end;
  75. }
  76. /*
  77. * Feed our message into the digest function.
  78. * This may be called multiple times.
  79. */
  80. if (EVP_DigestUpdate(ctx, message, sizeof(message)) == 0) {
  81. fprintf(stderr, "Failed to hash input message\n");
  82. goto end;
  83. }
  84. /* Allocate enough memory for our digest length. */
  85. digest = OPENSSL_malloc(digest_len);
  86. if (digest == NULL) {
  87. fprintf(stderr, "Failed to allocate memory for digest\n");
  88. goto end;
  89. }
  90. /* Get computed digest. The digest will be of whatever length we specify. */
  91. if (EVP_DigestFinalXOF(ctx, digest, digest_len) == 0) {
  92. fprintf(stderr, "Failed to finalize hash\n");
  93. goto end;
  94. }
  95. printf("Output digest:\n");
  96. BIO_dump_indent_fp(stdout, digest, digest_len, 2);
  97. /* If digest length is 20 bytes, check it matches our known answer. */
  98. if (digest_len == 20) {
  99. /*
  100. * Always use a constant-time function such as CRYPTO_memcmp
  101. * when comparing cryptographic values. Do not use memcmp(3).
  102. */
  103. if (CRYPTO_memcmp(digest, known_answer, sizeof(known_answer)) != 0) {
  104. fprintf(stderr, "Output does not match expected result\n");
  105. goto end;
  106. }
  107. }
  108. ret = EXIT_SUCCESS;
  109. end:
  110. OPENSSL_free(digest);
  111. EVP_MD_CTX_free(ctx);
  112. EVP_MD_free(md);
  113. OSSL_LIB_CTX_free(libctx);
  114. return ret;
  115. }