gmac.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright 2021-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. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <openssl/core_names.h>
  12. #include <openssl/evp.h>
  13. #include <openssl/params.h>
  14. #include <openssl/err.h>
  15. /*
  16. * Taken from NIST's GCM Test Vectors
  17. * http://csrc.nist.gov/groups/STM/cavp/
  18. */
  19. /*
  20. * Hard coding the key into an application is very bad.
  21. * It is done here solely for educational purposes.
  22. */
  23. static unsigned char key[] = {
  24. 0x77, 0xbe, 0x63, 0x70, 0x89, 0x71, 0xc4, 0xe2,
  25. 0x40, 0xd1, 0xcb, 0x79, 0xe8, 0xd7, 0x7f, 0xeb
  26. };
  27. /*
  28. * The initialisation vector (IV) is better not being hard coded too.
  29. * Repeating password/IV pairs compromises the integrity of GMAC.
  30. * The IV is not considered secret information and is safe to store with
  31. * an encrypted password.
  32. */
  33. static unsigned char iv[] = {
  34. 0xe0, 0xe0, 0x0f, 0x19, 0xfe, 0xd7, 0xba,
  35. 0x01, 0x36, 0xa7, 0x97, 0xf3
  36. };
  37. static unsigned char data[] = {
  38. 0x7a, 0x43, 0xec, 0x1d, 0x9c, 0x0a, 0x5a, 0x78,
  39. 0xa0, 0xb1, 0x65, 0x33, 0xa6, 0x21, 0x3c, 0xab
  40. };
  41. static const unsigned char expected_output[] = {
  42. 0x20, 0x9f, 0xcc, 0x8d, 0x36, 0x75, 0xed, 0x93,
  43. 0x8e, 0x9c, 0x71, 0x66, 0x70, 0x9d, 0xd9, 0x46
  44. };
  45. /*
  46. * A property query used for selecting the GMAC implementation and the
  47. * underlying GCM mode cipher.
  48. */
  49. static char *propq = NULL;
  50. int main(int argc, char **argv)
  51. {
  52. int ret = EXIT_FAILURE;
  53. EVP_MAC *mac = NULL;
  54. EVP_MAC_CTX *mctx = NULL;
  55. unsigned char out[16];
  56. OSSL_PARAM params[4], *p = params;
  57. OSSL_LIB_CTX *library_context = NULL;
  58. size_t out_len = 0;
  59. library_context = OSSL_LIB_CTX_new();
  60. if (library_context == NULL) {
  61. fprintf(stderr, "OSSL_LIB_CTX_new() returned NULL\n");
  62. goto end;
  63. }
  64. /* Fetch the GMAC implementation */
  65. mac = EVP_MAC_fetch(library_context, "GMAC", propq);
  66. if (mac == NULL) {
  67. fprintf(stderr, "EVP_MAC_fetch() returned NULL\n");
  68. goto end;
  69. }
  70. /* Create a context for the GMAC operation */
  71. mctx = EVP_MAC_CTX_new(mac);
  72. if (mctx == NULL) {
  73. fprintf(stderr, "EVP_MAC_CTX_new() returned NULL\n");
  74. goto end;
  75. }
  76. /* GMAC requires a GCM mode cipher to be specified */
  77. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
  78. "AES-128-GCM", 0);
  79. /*
  80. * If a non-default property query is required when fetching the GCM mode
  81. * cipher, it needs to be specified too.
  82. */
  83. if (propq != NULL)
  84. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_PROPERTIES,
  85. propq, 0);
  86. /* Set the initialisation vector (IV) */
  87. *p++ = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_IV,
  88. iv, sizeof(iv));
  89. *p = OSSL_PARAM_construct_end();
  90. /* Initialise the GMAC 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 one call to the final to get the MAC */
  101. if (!EVP_MAC_final(mctx, out, &out_len, sizeof(out))) {
  102. fprintf(stderr, "EVP_MAC_final() failed\n");
  103. goto end;
  104. }
  105. printf("Generated MAC:\n");
  106. BIO_dump_indent_fp(stdout, out, out_len, 2);
  107. putchar('\n');
  108. if (out_len != sizeof(expected_output)) {
  109. fprintf(stderr, "Generated MAC has an unexpected length\n");
  110. goto end;
  111. }
  112. if (CRYPTO_memcmp(expected_output, out, sizeof(expected_output)) != 0) {
  113. fprintf(stderr, "Generated MAC does not match expected value\n");
  114. goto end;
  115. }
  116. ret = EXIT_SUCCESS;
  117. end:
  118. EVP_MAC_CTX_free(mctx);
  119. EVP_MAC_free(mac);
  120. OSSL_LIB_CTX_free(library_context);
  121. if (ret != EXIT_SUCCESS)
  122. ERR_print_errors_fp(stderr);
  123. return ret;
  124. }