EVP_MD_stdin.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*-
  2. * Copyright 2019-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. /*-
  10. * Example of using EVP_MD_fetch and EVP_Digest* methods to calculate
  11. * a digest of static buffers
  12. * You can find SHA3 test vectors from NIST here:
  13. * https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/documents/sha3/sha-3bytetestvectors.zip
  14. * For example, contains these lines:
  15. Len = 80
  16. Msg = 1ca984dcc913344370cf
  17. MD = 6915ea0eeffb99b9b246a0e34daf3947852684c3d618260119a22835659e4f23d4eb66a15d0affb8e93771578f5e8f25b7a5f2a55f511fb8b96325ba2cd14816
  18. * use xxd convert the hex message string to binary input for EVP_MD_stdin:
  19. * echo "1ca984dcc913344370cf" | xxd -r -p | ./EVP_MD_stdin
  20. * and then verify the output matches MD above.
  21. */
  22. #include <string.h>
  23. #include <stdio.h>
  24. #include <openssl/err.h>
  25. #include <openssl/evp.h>
  26. /*-
  27. * This demonstration will show how to digest data using
  28. * a BIO created to read from stdin
  29. */
  30. int demonstrate_digest(BIO *input)
  31. {
  32. OSSL_LIB_CTX *library_context = NULL;
  33. int ret = 0;
  34. const char *option_properties = NULL;
  35. EVP_MD *message_digest = NULL;
  36. EVP_MD_CTX *digest_context = NULL;
  37. unsigned int digest_length;
  38. unsigned char *digest_value = NULL;
  39. unsigned char buffer[512];
  40. int ii;
  41. library_context = OSSL_LIB_CTX_new();
  42. if (library_context == NULL) {
  43. fprintf(stderr, "OSSL_LIB_CTX_new() returned NULL\n");
  44. goto cleanup;
  45. }
  46. /*
  47. * Fetch a message digest by name
  48. * The algorithm name is case insensitive.
  49. * See providers(7) for details about algorithm fetching
  50. */
  51. message_digest = EVP_MD_fetch(library_context,
  52. "SHA3-512", option_properties);
  53. if (message_digest == NULL) {
  54. fprintf(stderr, "EVP_MD_fetch could not find SHA3-512.");
  55. ERR_print_errors_fp(stderr);
  56. OSSL_LIB_CTX_free(library_context);
  57. return 0;
  58. }
  59. /* Determine the length of the fetched digest type */
  60. digest_length = EVP_MD_get_size(message_digest);
  61. if (digest_length <= 0) {
  62. fprintf(stderr, "EVP_MD_get_size returned invalid size.\n");
  63. goto cleanup;
  64. }
  65. digest_value = OPENSSL_malloc(digest_length);
  66. if (digest_value == NULL) {
  67. fprintf(stderr, "No memory.\n");
  68. goto cleanup;
  69. }
  70. /*
  71. * Make a message digest context to hold temporary state
  72. * during digest creation
  73. */
  74. digest_context = EVP_MD_CTX_new();
  75. if (digest_context == NULL) {
  76. fprintf(stderr, "EVP_MD_CTX_new failed.\n");
  77. ERR_print_errors_fp(stderr);
  78. goto cleanup;
  79. }
  80. /*
  81. * Initialize the message digest context to use the fetched
  82. * digest provider
  83. */
  84. if (EVP_DigestInit(digest_context, message_digest) != 1) {
  85. fprintf(stderr, "EVP_DigestInit failed.\n");
  86. ERR_print_errors_fp(stderr);
  87. goto cleanup;
  88. }
  89. while ((ii = BIO_read(input, buffer, sizeof(buffer))) > 0) {
  90. if (EVP_DigestUpdate(digest_context, buffer, ii) != 1) {
  91. fprintf(stderr, "EVP_DigestUpdate() failed.\n");
  92. goto cleanup;
  93. }
  94. }
  95. if (EVP_DigestFinal(digest_context, digest_value, &digest_length) != 1) {
  96. fprintf(stderr, "EVP_DigestFinal() failed.\n");
  97. goto cleanup;
  98. }
  99. ret = 1;
  100. for (ii=0; ii<digest_length; ii++) {
  101. fprintf(stdout, "%02x", digest_value[ii]);
  102. }
  103. fprintf(stdout, "\n");
  104. cleanup:
  105. if (ret != 1)
  106. ERR_print_errors_fp(stderr);
  107. /* OpenSSL free functions will ignore NULL arguments */
  108. EVP_MD_CTX_free(digest_context);
  109. OPENSSL_free(digest_value);
  110. EVP_MD_free(message_digest);
  111. OSSL_LIB_CTX_free(library_context);
  112. return ret;
  113. }
  114. int main(void)
  115. {
  116. int ret = EXIT_FAILURE;
  117. BIO *input = BIO_new_fd(fileno(stdin), 1);
  118. if (input != NULL) {
  119. ret = (demonstrate_digest(input) ? EXIT_SUCCESS : EXIT_FAILURE);
  120. BIO_free(input);
  121. }
  122. if (ret != EXIT_SUCCESS)
  123. ERR_print_errors_fp(stderr);
  124. return ret;
  125. }