BIO_f_md.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*-
  2. * Copyright 2019-2021 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 BIO_f_md:
  19. * echo "1ca984dcc913344370cf" | xxd -r -p | ./BIO_f_md
  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/bio.h>
  26. #include <openssl/evp.h>
  27. /*-
  28. * This demonstration will show how to digest data using
  29. * a BIO configured with a message digest
  30. * A message digest name may be passed as an argument.
  31. * The default digest is SHA3-512
  32. */
  33. int main(int argc, char * argv[])
  34. {
  35. int result = 1;
  36. OSSL_LIB_CTX *library_context = NULL;
  37. BIO *input = NULL;
  38. BIO *bio_digest = NULL;
  39. EVP_MD *md = NULL;
  40. unsigned char buffer[512];
  41. size_t readct, writect;
  42. size_t digest_size;
  43. char *digest_value=NULL;
  44. int j;
  45. input = BIO_new_fd(fileno(stdin), 1);
  46. if (input == NULL) {
  47. fprintf(stderr, "BIO_new_fd() for stdin returned NULL\n");
  48. goto cleanup;
  49. }
  50. library_context = OSSL_LIB_CTX_new();
  51. if (library_context == NULL) {
  52. fprintf(stderr, "OSSL_LIB_CTX_new() returned NULL\n");
  53. goto cleanup;
  54. }
  55. /*
  56. * Fetch a message digest by name
  57. * The algorithm name is case insensitive.
  58. * See providers(7) for details about algorithm fetching
  59. */
  60. md = EVP_MD_fetch(library_context, "SHA3-512", NULL);
  61. if (md == NULL) {
  62. fprintf(stderr, "EVP_MD_fetch did not find SHA3-512.\n");
  63. goto cleanup;
  64. }
  65. digest_size = EVP_MD_get_size(md);
  66. digest_value = OPENSSL_malloc(digest_size);
  67. if (digest_value == NULL) {
  68. fprintf(stderr, "Can't allocate %lu bytes for the digest value.\n", (unsigned long)digest_size);
  69. goto cleanup;
  70. }
  71. /* Make a bio that uses the digest */
  72. bio_digest = BIO_new(BIO_f_md());
  73. if (bio_digest == NULL) {
  74. fprintf(stderr, "BIO_new(BIO_f_md()) returned NULL\n");
  75. goto cleanup;
  76. }
  77. /* set our bio_digest BIO to digest data */
  78. if (BIO_set_md(bio_digest, md) != 1) {
  79. fprintf(stderr, "BIO_set_md failed.\n");
  80. goto cleanup;
  81. }
  82. /*-
  83. * We will use BIO chaining so that as we read, the digest gets updated
  84. * See the man page for BIO_push
  85. */
  86. BIO *reading = BIO_push(bio_digest, input);
  87. while (BIO_read(reading, buffer, sizeof(buffer)) > 0)
  88. ;
  89. /*-
  90. * BIO_gets must be used to calculate the final
  91. * digest value and then copy it to digest_value.
  92. */
  93. if (BIO_gets(bio_digest, digest_value, digest_size) != digest_size) {
  94. fprintf(stderr, "BIO_gets(bio_digest) failed\n");
  95. goto cleanup;
  96. }
  97. for (j = 0; j < digest_size; j++) {
  98. fprintf(stdout, "%02x", (unsigned char)digest_value[j]);
  99. }
  100. fprintf(stdout, "\n");
  101. result = 0;
  102. cleanup:
  103. if (result != 0)
  104. ERR_print_errors_fp(stderr);
  105. OPENSSL_free(digest_value);
  106. BIO_free(input);
  107. BIO_free(bio_digest);
  108. EVP_MD_free(md);
  109. OSSL_LIB_CTX_free(library_context);
  110. return result;
  111. }