siphash_meth.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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 <stdarg.h>
  10. #include <string.h>
  11. #include <openssl/evp.h>
  12. #include <openssl/err.h>
  13. #include "internal/siphash.h"
  14. #include "siphash_local.h"
  15. #include "internal/evp_int.h"
  16. /* local SIPHASH structure is actually a SIPHASH */
  17. struct evp_mac_impl_st {
  18. SIPHASH ctx;
  19. };
  20. static EVP_MAC_IMPL *siphash_new(void)
  21. {
  22. return OPENSSL_zalloc(sizeof(EVP_MAC_IMPL));
  23. }
  24. static void siphash_free(EVP_MAC_IMPL *sctx)
  25. {
  26. OPENSSL_free(sctx);
  27. }
  28. static int siphash_copy(EVP_MAC_IMPL *sdst, EVP_MAC_IMPL *ssrc)
  29. {
  30. *sdst = *ssrc;
  31. return 1;
  32. }
  33. static size_t siphash_size(EVP_MAC_IMPL *sctx)
  34. {
  35. return SipHash_hash_size(&sctx->ctx);
  36. }
  37. static int siphash_init(EVP_MAC_IMPL *sctx)
  38. {
  39. /* Not much to do here, actual initialization happens through controls */
  40. return 1;
  41. }
  42. static int siphash_update(EVP_MAC_IMPL *sctx, const unsigned char *data,
  43. size_t datalen)
  44. {
  45. SipHash_Update(&sctx->ctx, data, datalen);
  46. return 1;
  47. }
  48. static int siphash_final(EVP_MAC_IMPL *sctx, unsigned char *out)
  49. {
  50. size_t hlen = siphash_size(sctx);
  51. return SipHash_Final(&sctx->ctx, out, hlen);
  52. }
  53. static int siphash_ctrl(EVP_MAC_IMPL *sctx, int cmd, va_list args)
  54. {
  55. switch (cmd) {
  56. case EVP_MAC_CTRL_SET_SIZE:
  57. {
  58. size_t size = va_arg(args, size_t);
  59. return SipHash_set_hash_size(&sctx->ctx, size);
  60. }
  61. break;
  62. case EVP_MAC_CTRL_SET_KEY:
  63. {
  64. const unsigned char *key = va_arg(args, const unsigned char *);
  65. size_t keylen = va_arg(args, size_t);
  66. if (key == NULL || keylen != SIPHASH_KEY_SIZE)
  67. return 0;
  68. return SipHash_Init(&sctx->ctx, key, 0, 0);
  69. }
  70. break;
  71. default:
  72. return -2;
  73. }
  74. return 1;
  75. }
  76. static int siphash_ctrl_int(EVP_MAC_IMPL *sctx, int cmd, ...)
  77. {
  78. int rv;
  79. va_list args;
  80. va_start(args, cmd);
  81. rv = siphash_ctrl(sctx, cmd, args);
  82. va_end(args);
  83. return rv;
  84. }
  85. static int siphash_ctrl_str_cb(void *ctx, int cmd, void *buf, size_t buflen)
  86. {
  87. return siphash_ctrl_int(ctx, cmd, buf, buflen);
  88. }
  89. static int siphash_ctrl_str(EVP_MAC_IMPL *ctx,
  90. const char *type, const char *value)
  91. {
  92. if (value == NULL)
  93. return 0;
  94. if (strcmp(type, "digestsize") == 0) {
  95. size_t hash_size = atoi(value);
  96. return siphash_ctrl_int(ctx, EVP_MAC_CTRL_SET_SIZE, hash_size);
  97. }
  98. if (strcmp(type, "key") == 0)
  99. return EVP_str2ctrl(siphash_ctrl_str_cb, ctx, EVP_MAC_CTRL_SET_KEY,
  100. value);
  101. if (strcmp(type, "hexkey") == 0)
  102. return EVP_hex2ctrl(siphash_ctrl_str_cb, ctx, EVP_MAC_CTRL_SET_KEY,
  103. value);
  104. return -2;
  105. }
  106. const EVP_MAC siphash_meth = {
  107. EVP_MAC_SIPHASH,
  108. siphash_new,
  109. siphash_copy,
  110. siphash_free,
  111. siphash_size,
  112. siphash_init,
  113. siphash_update,
  114. siphash_final,
  115. siphash_ctrl,
  116. siphash_ctrl_str
  117. };