hmac-sha512.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. /*
  10. * Example of using EVP_MAC_ methods to calculate
  11. * a HMAC of static buffers
  12. */
  13. #include <string.h>
  14. #include <stdio.h>
  15. #include <openssl/crypto.h>
  16. #include <openssl/core_names.h>
  17. #include <openssl/err.h>
  18. #include <openssl/evp.h>
  19. #include <openssl/hmac.h>
  20. #include <openssl/params.h>
  21. /*
  22. * Hard coding the key into an application is very bad.
  23. * It is done here solely for educational purposes.
  24. */
  25. static unsigned char key[] = {
  26. 0x25, 0xfd, 0x12, 0x99, 0xdf, 0xad, 0x1a, 0x03,
  27. 0x0a, 0x81, 0x3c, 0x2d, 0xcc, 0x05, 0xd1, 0x5c,
  28. 0x17, 0x7a, 0x36, 0x73, 0x17, 0xef, 0x41, 0x75,
  29. 0x71, 0x18, 0xe0, 0x1a, 0xda, 0x99, 0xc3, 0x61,
  30. 0x38, 0xb5, 0xb1, 0xe0, 0x82, 0x2c, 0x70, 0xa4,
  31. 0xc0, 0x8e, 0x5e, 0xf9, 0x93, 0x9f, 0xcf, 0xf7,
  32. 0x32, 0x4d, 0x0c, 0xbd, 0x31, 0x12, 0x0f, 0x9a,
  33. 0x15, 0xee, 0x82, 0xdb, 0x8d, 0x29, 0x54, 0x14,
  34. };
  35. static const unsigned char data[] =
  36. "To be, or not to be, that is the question,\n"
  37. "Whether tis nobler in the minde to suffer\n"
  38. "The ſlings and arrowes of outragious fortune,\n"
  39. "Or to take Armes again in a sea of troubles,\n"
  40. "And by opposing, end them, to die to sleep;\n"
  41. "No more, and by a sleep, to say we end\n"
  42. "The heart-ache, and the thousand natural shocks\n"
  43. "That flesh is heir to? tis a consumation\n"
  44. "Devoutly to be wished. To die to sleep,\n"
  45. "To sleepe, perchance to dreame, Aye, there's the rub,\n"
  46. "For in that sleep of death what dreams may come\n"
  47. "When we haue shuffled off this mortal coil\n"
  48. "Must give us pause. There's the respect\n"
  49. "That makes calamity of so long life:\n"
  50. "For who would bear the Ships and Scorns of time,\n"
  51. "The oppressor's wrong, the proud man's Contumely,\n"
  52. "The pangs of dispised love, the Law's delay,\n"
  53. ;
  54. /* The known value of the HMAC/SHA3-512 MAC of the above soliloqy */
  55. static const unsigned char expected_output[] = {
  56. 0x3b, 0x77, 0x5f, 0xf1, 0x4f, 0x9e, 0xb9, 0x23,
  57. 0x8f, 0xdc, 0xa0, 0x68, 0x15, 0x7b, 0x8a, 0xf1,
  58. 0x96, 0x23, 0xaa, 0x3c, 0x1f, 0xe9, 0xdc, 0x89,
  59. 0x11, 0x7d, 0x58, 0x07, 0xe7, 0x96, 0x17, 0xe3,
  60. 0x44, 0x8b, 0x03, 0x37, 0x91, 0xc0, 0x6e, 0x06,
  61. 0x7c, 0x54, 0xe4, 0xa4, 0xcc, 0xd5, 0x16, 0xbb,
  62. 0x5e, 0x4d, 0x64, 0x7d, 0x88, 0x23, 0xc9, 0xb7,
  63. 0x25, 0xda, 0xbe, 0x4b, 0xe4, 0xd5, 0x34, 0x30,
  64. };
  65. /*
  66. * A property query used for selecting the MAC implementation.
  67. */
  68. static const char *propq = NULL;
  69. int main(void)
  70. {
  71. int ret = EXIT_FAILURE;
  72. OSSL_LIB_CTX *library_context = NULL;
  73. EVP_MAC *mac = NULL;
  74. EVP_MAC_CTX *mctx = NULL;
  75. EVP_MD_CTX *digest_context = NULL;
  76. unsigned char *out = NULL;
  77. size_t out_len = 0;
  78. OSSL_PARAM params[4], *p = params;
  79. char digest_name[] = "SHA3-512";
  80. library_context = OSSL_LIB_CTX_new();
  81. if (library_context == NULL) {
  82. fprintf(stderr, "OSSL_LIB_CTX_new() returned NULL\n");
  83. goto end;
  84. }
  85. /* Fetch the HMAC implementation */
  86. mac = EVP_MAC_fetch(library_context, "HMAC", propq);
  87. if (mac == NULL) {
  88. fprintf(stderr, "EVP_MAC_fetch() returned NULL\n");
  89. goto end;
  90. }
  91. /* Create a context for the HMAC operation */
  92. mctx = EVP_MAC_CTX_new(mac);
  93. if (mctx == NULL) {
  94. fprintf(stderr, "EVP_MAC_CTX_new() returned NULL\n");
  95. goto end;
  96. }
  97. /* The underlying digest to be used */
  98. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST, digest_name,
  99. sizeof(digest_name));
  100. *p = OSSL_PARAM_construct_end();
  101. /* Initialise the HMAC operation */
  102. if (!EVP_MAC_init(mctx, key, sizeof(key), params)) {
  103. fprintf(stderr, "EVP_MAC_init() failed\n");
  104. goto end;
  105. }
  106. /* Make one or more calls to process the data to be authenticated */
  107. if (!EVP_MAC_update(mctx, data, sizeof(data))) {
  108. fprintf(stderr, "EVP_MAC_update() failed\n");
  109. goto end;
  110. }
  111. /* Make a call to the final with a NULL buffer to get the length of the MAC */
  112. if (!EVP_MAC_final(mctx, NULL, &out_len, 0)) {
  113. fprintf(stderr, "EVP_MAC_final() failed\n");
  114. goto end;
  115. }
  116. out = OPENSSL_malloc(out_len);
  117. if (out == NULL) {
  118. fprintf(stderr, "malloc failed\n");
  119. goto end;
  120. }
  121. /* Make one call to the final to get the MAC */
  122. if (!EVP_MAC_final(mctx, out, &out_len, out_len)) {
  123. fprintf(stderr, "EVP_MAC_final() failed\n");
  124. goto end;
  125. }
  126. printf("Generated MAC:\n");
  127. BIO_dump_indent_fp(stdout, out, out_len, 2);
  128. putchar('\n');
  129. if (out_len != sizeof(expected_output)) {
  130. fprintf(stderr, "Generated MAC has an unexpected length\n");
  131. goto end;
  132. }
  133. if (CRYPTO_memcmp(expected_output, out, sizeof(expected_output)) != 0) {
  134. fprintf(stderr, "Generated MAC does not match expected value\n");
  135. goto end;
  136. }
  137. ret = EXIT_SUCCESS;
  138. end:
  139. if (ret != EXIT_SUCCESS)
  140. ERR_print_errors_fp(stderr);
  141. /* OpenSSL free functions will ignore NULL arguments */
  142. OPENSSL_free(out);
  143. EVP_MD_CTX_free(digest_context);
  144. EVP_MAC_CTX_free(mctx);
  145. EVP_MAC_free(mac);
  146. OSSL_LIB_CTX_free(library_context);
  147. return ret;
  148. }