cmac-aes256.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 CMAC 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/cmac.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. 0x6c, 0xde, 0x14, 0xf5, 0xd5, 0x2a, 0x4a, 0xdf,
  27. 0x12, 0x39, 0x1e, 0xbf, 0x36, 0xf9, 0x6a, 0x46,
  28. 0x48, 0xd0, 0xb6, 0x51, 0x89, 0xfc, 0x24, 0x85,
  29. 0xa8, 0x8d, 0xdf, 0x7e, 0x80, 0x14, 0xc8, 0xce,
  30. };
  31. static const unsigned char data[] =
  32. "To be, or not to be, that is the question,\n"
  33. "Whether tis nobler in the minde to suffer\n"
  34. "The ſlings and arrowes of outragious fortune,\n"
  35. "Or to take Armes again in a sea of troubles,\n"
  36. "And by opposing, end them, to die to sleep;\n"
  37. "No more, and by a sleep, to say we end\n"
  38. "The heart-ache, and the thousand natural shocks\n"
  39. "That flesh is heir to? tis a consumation\n"
  40. "Devoutly to be wished. To die to sleep,\n"
  41. "To sleepe, perchance to dreame, Aye, there's the rub,\n"
  42. "For in that sleep of death what dreams may come\n"
  43. "When we haue shuffled off this mortal coil\n"
  44. "Must give us pause. There's the respect\n"
  45. "That makes calamity of so long life:\n"
  46. "For who would bear the Ships and Scorns of time,\n"
  47. "The oppressor's wrong, the proud man's Contumely,\n"
  48. "The pangs of dispised love, the Law's delay,\n"
  49. ;
  50. /* The known value of the CMAC/AES256 MAC of the above soliloqy */
  51. static const unsigned char expected_output[] = {
  52. 0x67, 0x92, 0x32, 0x23, 0x50, 0x3d, 0xc5, 0xba,
  53. 0x78, 0xd4, 0x6d, 0x63, 0xf2, 0x2b, 0xe9, 0x56,
  54. };
  55. /*
  56. * A property query used for selecting the MAC implementation.
  57. */
  58. static const char *propq = NULL;
  59. int main(void)
  60. {
  61. int ret = EXIT_FAILURE;
  62. OSSL_LIB_CTX *library_context = NULL;
  63. EVP_MAC *mac = NULL;
  64. EVP_MAC_CTX *mctx = NULL;
  65. unsigned char *out = NULL;
  66. size_t out_len = 0;
  67. OSSL_PARAM params[4], *p = params;
  68. char cipher_name[] = "AES-256-CBC";
  69. library_context = OSSL_LIB_CTX_new();
  70. if (library_context == NULL) {
  71. fprintf(stderr, "OSSL_LIB_CTX_new() returned NULL\n");
  72. goto end;
  73. }
  74. /* Fetch the CMAC implementation */
  75. mac = EVP_MAC_fetch(library_context, "CMAC", propq);
  76. if (mac == NULL) {
  77. fprintf(stderr, "EVP_MAC_fetch() returned NULL\n");
  78. goto end;
  79. }
  80. /* Create a context for the CMAC operation */
  81. mctx = EVP_MAC_CTX_new(mac);
  82. if (mctx == NULL) {
  83. fprintf(stderr, "EVP_MAC_CTX_new() returned NULL\n");
  84. goto end;
  85. }
  86. /* The underlying cipher to be used */
  87. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER, cipher_name,
  88. sizeof(cipher_name));
  89. *p = OSSL_PARAM_construct_end();
  90. /* Initialise the CMAC operation */
  91. if (!EVP_MAC_init(mctx, key, sizeof(key), params)) {
  92. fprintf(stderr, "EVP_MAC_init() failed\n");
  93. goto end;
  94. }
  95. /* Make one or more calls to process the data to be authenticated */
  96. if (!EVP_MAC_update(mctx, data, sizeof(data))) {
  97. fprintf(stderr, "EVP_MAC_update() failed\n");
  98. goto end;
  99. }
  100. /* Make a call to the final with a NULL buffer to get the length of the MAC */
  101. if (!EVP_MAC_final(mctx, NULL, &out_len, 0)) {
  102. fprintf(stderr, "EVP_MAC_final() failed\n");
  103. goto end;
  104. }
  105. out = OPENSSL_malloc(out_len);
  106. if (out == NULL) {
  107. fprintf(stderr, "malloc failed\n");
  108. goto end;
  109. }
  110. /* Make one call to the final to get the MAC */
  111. if (!EVP_MAC_final(mctx, out, &out_len, out_len)) {
  112. fprintf(stderr, "EVP_MAC_final() failed\n");
  113. goto end;
  114. }
  115. printf("Generated MAC:\n");
  116. BIO_dump_indent_fp(stdout, out, out_len, 2);
  117. putchar('\n');
  118. if (out_len != sizeof(expected_output)) {
  119. fprintf(stderr, "Generated MAC has an unexpected length\n");
  120. goto end;
  121. }
  122. if (CRYPTO_memcmp(expected_output, out, sizeof(expected_output)) != 0) {
  123. fprintf(stderr, "Generated MAC does not match expected value\n");
  124. goto end;
  125. }
  126. ret = EXIT_SUCCESS;
  127. end:
  128. if (ret != EXIT_SUCCESS)
  129. ERR_print_errors_fp(stderr);
  130. /* OpenSSL free functions will ignore NULL arguments */
  131. OPENSSL_free(out);
  132. EVP_MAC_CTX_free(mctx);
  133. EVP_MAC_free(mac);
  134. OSSL_LIB_CTX_free(library_context);
  135. return ret;
  136. }