EVP_MD_demo.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*-
  2. * Copyright 2021-2022 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. */
  13. #include <string.h>
  14. #include <stdio.h>
  15. #include <openssl/err.h>
  16. #include <openssl/evp.h>
  17. /*-
  18. * This demonstration will show how to digest data using
  19. * the soliloqy from Hamlet scene 1 act 3
  20. * The soliloqy is split into two parts to demonstrate using EVP_DigestUpdate
  21. * more than once.
  22. */
  23. const char * hamlet_1 =
  24. "To be, or not to be, that is the question,\n"
  25. "Whether tis nobler in the minde to suffer\n"
  26. "The ſlings and arrowes of outragious fortune,\n"
  27. "Or to take Armes again in a sea of troubles,\n"
  28. "And by opposing, end them, to die to sleep;\n"
  29. "No more, and by a sleep, to say we end\n"
  30. "The heart-ache, and the thousand natural shocks\n"
  31. "That flesh is heir to? tis a consumation\n"
  32. "Devoutly to be wished. To die to sleep,\n"
  33. "To sleepe, perchance to dreame, Aye, there's the rub,\n"
  34. "For in that sleep of death what dreams may come\n"
  35. "When we haue shuffled off this mortal coil\n"
  36. "Must give us pause. There's the respect\n"
  37. "That makes calamity of so long life:\n"
  38. "For who would bear the Ships and Scorns of time,\n"
  39. "The oppressor's wrong, the proud man's Contumely,\n"
  40. "The pangs of dispised love, the Law's delay,\n"
  41. ;
  42. const char * hamlet_2 =
  43. "The insolence of Office, and the spurns\n"
  44. "That patient merit of the'unworthy takes,\n"
  45. "When he himself might his Quietas make\n"
  46. "With a bare bodkin? Who would fardels bear,\n"
  47. "To grunt and sweat under a weary life,\n"
  48. "But that the dread of something after death,\n"
  49. "The undiscovered country, from whose bourn\n"
  50. "No traveller returns, puzzles the will,\n"
  51. "And makes us rather bear those ills we have,\n"
  52. "Then fly to others we know not of?\n"
  53. "Thus conscience does make cowards of us all,\n"
  54. "And thus the native hue of Resolution\n"
  55. "Is sickled o'er with the pale cast of Thought,\n"
  56. "And enterprises of great pith and moment,\n"
  57. "With this regard their currents turn awry,\n"
  58. "And lose the name of Action. Soft you now,\n"
  59. "The fair Ophelia? Nymph in thy Orisons\n"
  60. "Be all my sins remember'd.\n"
  61. ;
  62. /* The known value of the SHA3-512 digest of the above soliloqy */
  63. const unsigned char known_answer[] = {
  64. 0xbb, 0x69, 0xf8, 0x09, 0x9c, 0x2e, 0x00, 0x3d,
  65. 0xa4, 0x29, 0x5f, 0x59, 0x4b, 0x89, 0xe4, 0xd9,
  66. 0xdb, 0xa2, 0xe5, 0xaf, 0xa5, 0x87, 0x73, 0x9d,
  67. 0x83, 0x72, 0xcf, 0xea, 0x84, 0x66, 0xc1, 0xf9,
  68. 0xc9, 0x78, 0xef, 0xba, 0x3d, 0xe9, 0xc1, 0xff,
  69. 0xa3, 0x75, 0xc7, 0x58, 0x74, 0x8e, 0x9c, 0x1d,
  70. 0x14, 0xd9, 0xdd, 0xd1, 0xfd, 0x24, 0x30, 0xd6,
  71. 0x81, 0xca, 0x8f, 0x78, 0x29, 0x19, 0x9a, 0xfe,
  72. };
  73. int demonstrate_digest(void)
  74. {
  75. OSSL_LIB_CTX *library_context;
  76. int result = 0;
  77. const char *option_properties = NULL;
  78. EVP_MD *message_digest = NULL;
  79. EVP_MD_CTX *digest_context = NULL;
  80. unsigned int digest_length;
  81. unsigned char *digest_value = NULL;
  82. int j;
  83. library_context = OSSL_LIB_CTX_new();
  84. if (library_context == NULL) {
  85. fprintf(stderr, "OSSL_LIB_CTX_new() returned NULL\n");
  86. goto cleanup;
  87. }
  88. /*
  89. * Fetch a message digest by name
  90. * The algorithm name is case insensitive.
  91. * See providers(7) for details about algorithm fetching
  92. */
  93. message_digest = EVP_MD_fetch(library_context,
  94. "SHA3-512", option_properties);
  95. if (message_digest == NULL) {
  96. fprintf(stderr, "EVP_MD_fetch could not find SHA3-512.");
  97. goto cleanup;
  98. }
  99. /* Determine the length of the fetched digest type */
  100. digest_length = EVP_MD_get_size(message_digest);
  101. if (digest_length <= 0) {
  102. fprintf(stderr, "EVP_MD_get_size returned invalid size.\n");
  103. goto cleanup;
  104. }
  105. digest_value = OPENSSL_malloc(digest_length);
  106. if (digest_value == NULL) {
  107. fprintf(stderr, "No memory.\n");
  108. goto cleanup;
  109. }
  110. /*
  111. * Make a message digest context to hold temporary state
  112. * during digest creation
  113. */
  114. digest_context = EVP_MD_CTX_new();
  115. if (digest_context == NULL) {
  116. fprintf(stderr, "EVP_MD_CTX_new failed.\n");
  117. goto cleanup;
  118. }
  119. /*
  120. * Initialize the message digest context to use the fetched
  121. * digest provider
  122. */
  123. if (EVP_DigestInit(digest_context, message_digest) != 1) {
  124. fprintf(stderr, "EVP_DigestInit failed.\n");
  125. goto cleanup;
  126. }
  127. /* Digest parts one and two of the soliloqy */
  128. if (EVP_DigestUpdate(digest_context, hamlet_1, strlen(hamlet_1)) != 1) {
  129. fprintf(stderr, "EVP_DigestUpdate(hamlet_1) failed.\n");
  130. goto cleanup;
  131. }
  132. if (EVP_DigestUpdate(digest_context, hamlet_2, strlen(hamlet_2)) != 1) {
  133. fprintf(stderr, "EVP_DigestUpdate(hamlet_2) failed.\n");
  134. goto cleanup;
  135. }
  136. if (EVP_DigestFinal(digest_context, digest_value, &digest_length) != 1) {
  137. fprintf(stderr, "EVP_DigestFinal() failed.\n");
  138. goto cleanup;
  139. }
  140. for (j=0; j<digest_length; j++) {
  141. fprintf(stdout, "%02x", digest_value[j]);
  142. }
  143. fprintf(stdout, "\n");
  144. /* Check digest_value against the known answer */
  145. if ((size_t)digest_length != sizeof(known_answer)) {
  146. fprintf(stdout, "Digest length(%d) not equal to known answer length(%lu).\n",
  147. digest_length, sizeof(known_answer));
  148. } else if (memcmp(digest_value, known_answer, digest_length) != 0) {
  149. for (j=0; j<sizeof(known_answer); j++) {
  150. fprintf(stdout, "%02x", known_answer[j] );
  151. }
  152. fprintf(stdout, "\nDigest does not match known answer\n");
  153. } else {
  154. fprintf(stdout, "Digest computed properly.\n");
  155. result = 1;
  156. }
  157. cleanup:
  158. if (result != 1)
  159. ERR_print_errors_fp(stderr);
  160. /* OpenSSL free functions will ignore NULL arguments */
  161. EVP_MD_CTX_free(digest_context);
  162. OPENSSL_free(digest_value);
  163. EVP_MD_free(message_digest);
  164. OSSL_LIB_CTX_free(library_context);
  165. return result;
  166. }
  167. int main(void)
  168. {
  169. return demonstrate_digest() == 0;
  170. }