siphash.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 the test vector from the paper "SipHash: a fast short-input PRF".
  17. * https://www.aumasson.jp/siphash/siphash.pdf
  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. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  25. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
  26. };
  27. static unsigned char data[] = {
  28. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  29. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e
  30. };
  31. static const unsigned char expected_output[] = {
  32. 0xe5, 0x45, 0xbe, 0x49, 0x61, 0xca, 0x29, 0xa1
  33. };
  34. /*
  35. * A property query used for selecting the SIPHASH implementation.
  36. */
  37. static char *propq = NULL;
  38. int main(int argc, char **argv)
  39. {
  40. int ret = EXIT_FAILURE;
  41. EVP_MAC *mac = NULL;
  42. EVP_MAC_CTX *mctx = NULL;
  43. unsigned char out[8];
  44. OSSL_PARAM params[4], *p = params;
  45. OSSL_LIB_CTX *library_context = NULL;
  46. unsigned int digest_len = 8, c_rounds = 2, d_rounds = 4;
  47. size_t out_len = 0;
  48. library_context = OSSL_LIB_CTX_new();
  49. if (library_context == NULL) {
  50. fprintf(stderr, "OSSL_LIB_CTX_new() returned NULL\n");
  51. goto end;
  52. }
  53. /* Fetch the SipHash implementation */
  54. mac = EVP_MAC_fetch(library_context, "SIPHASH", propq);
  55. if (mac == NULL) {
  56. fprintf(stderr, "EVP_MAC_fetch() returned NULL\n");
  57. goto end;
  58. }
  59. /* Create a context for the SipHash operation */
  60. mctx = EVP_MAC_CTX_new(mac);
  61. if (mctx == NULL) {
  62. fprintf(stderr, "EVP_MAC_CTX_new() returned NULL\n");
  63. goto end;
  64. }
  65. /* SipHash can support either 8 or 16-byte digests. */
  66. *p++ = OSSL_PARAM_construct_uint(OSSL_MAC_PARAM_SIZE, &digest_len);
  67. /*
  68. * The number of C-rounds and D-rounds is configurable. Standard SipHash
  69. * uses values of 2 and 4 respectively. The following lines are unnecessary
  70. * as they set the default, but demonstrate how to change these values.
  71. */
  72. *p++ = OSSL_PARAM_construct_uint(OSSL_MAC_PARAM_C_ROUNDS, &c_rounds);
  73. *p++ = OSSL_PARAM_construct_uint(OSSL_MAC_PARAM_D_ROUNDS, &d_rounds);
  74. *p = OSSL_PARAM_construct_end();
  75. /* Initialise the SIPHASH operation */
  76. if (!EVP_MAC_init(mctx, key, sizeof(key), params)) {
  77. fprintf(stderr, "EVP_MAC_init() failed\n");
  78. goto end;
  79. }
  80. /* Make one or more calls to process the data to be authenticated */
  81. if (!EVP_MAC_update(mctx, data, sizeof(data))) {
  82. fprintf(stderr, "EVP_MAC_update() failed\n");
  83. goto end;
  84. }
  85. /* Make one call to the final to get the MAC */
  86. if (!EVP_MAC_final(mctx, out, &out_len, sizeof(out))) {
  87. fprintf(stderr, "EVP_MAC_final() failed\n");
  88. goto end;
  89. }
  90. printf("Generated MAC:\n");
  91. BIO_dump_indent_fp(stdout, out, out_len, 2);
  92. putchar('\n');
  93. if (out_len != sizeof(expected_output)) {
  94. fprintf(stderr, "Generated MAC has an unexpected length\n");
  95. goto end;
  96. }
  97. if (CRYPTO_memcmp(expected_output, out, sizeof(expected_output)) != 0) {
  98. fprintf(stderr, "Generated MAC does not match expected value\n");
  99. goto end;
  100. }
  101. ret = EXIT_SUCCESS;
  102. end:
  103. EVP_MAC_CTX_free(mctx);
  104. EVP_MAC_free(mac);
  105. OSSL_LIB_CTX_free(library_context);
  106. if (ret != EXIT_SUCCESS)
  107. ERR_print_errors_fp(stderr);
  108. return ret;
  109. }