siphash_prov.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright 2018-2020 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 <string.h>
  10. #include <openssl/core_dispatch.h>
  11. #include <openssl/core_names.h>
  12. #include <openssl/params.h>
  13. #include <openssl/evp.h>
  14. #include <openssl/err.h>
  15. #include "crypto/siphash.h"
  16. /*
  17. * TODO(3.0) when siphash has moved entirely to our providers, this
  18. * header should be moved to the provider include directory. For the
  19. * moment, crypto/siphash/siphash_ameth.c has us stuck.
  20. */
  21. #include "../../../crypto/siphash/siphash_local.h"
  22. #include "prov/providercommonerr.h"
  23. #include "prov/implementations.h"
  24. /*
  25. * Forward declaration of everything implemented here. This is not strictly
  26. * necessary for the compiler, but provides an assurance that the signatures
  27. * of the functions in the dispatch table are correct.
  28. */
  29. static OSSL_FUNC_mac_newctx_fn siphash_new;
  30. static OSSL_FUNC_mac_dupctx_fn siphash_dup;
  31. static OSSL_FUNC_mac_freectx_fn siphash_free;
  32. static OSSL_FUNC_mac_gettable_ctx_params_fn siphash_gettable_ctx_params;
  33. static OSSL_FUNC_mac_get_ctx_params_fn siphash_get_ctx_params;
  34. static OSSL_FUNC_mac_settable_ctx_params_fn siphash_settable_params;
  35. static OSSL_FUNC_mac_set_ctx_params_fn siphash_set_params;
  36. static OSSL_FUNC_mac_size_fn siphash_size;
  37. static OSSL_FUNC_mac_init_fn siphash_init;
  38. static OSSL_FUNC_mac_update_fn siphash_update;
  39. static OSSL_FUNC_mac_final_fn siphash_final;
  40. struct siphash_data_st {
  41. void *provctx;
  42. SIPHASH siphash; /* Siphash data */
  43. };
  44. static void *siphash_new(void *provctx)
  45. {
  46. struct siphash_data_st *ctx = OPENSSL_zalloc(sizeof(*ctx));
  47. if (ctx != NULL)
  48. ctx->provctx = provctx;
  49. return ctx;
  50. }
  51. static void siphash_free(void *vmacctx)
  52. {
  53. OPENSSL_free(vmacctx);
  54. }
  55. static void *siphash_dup(void *vsrc)
  56. {
  57. struct siphash_data_st *ssrc = vsrc;
  58. struct siphash_data_st *sdst = siphash_new(ssrc->provctx);
  59. if (sdst == NULL)
  60. return NULL;
  61. sdst->siphash = ssrc->siphash;
  62. return sdst;
  63. }
  64. static size_t siphash_size(void *vmacctx)
  65. {
  66. struct siphash_data_st *ctx = vmacctx;
  67. return SipHash_hash_size(&ctx->siphash);
  68. }
  69. static int siphash_init(void *vmacctx)
  70. {
  71. /* Not much to do here, actual initialization happens through controls */
  72. return 1;
  73. }
  74. static int siphash_update(void *vmacctx, const unsigned char *data,
  75. size_t datalen)
  76. {
  77. struct siphash_data_st *ctx = vmacctx;
  78. SipHash_Update(&ctx->siphash, data, datalen);
  79. return 1;
  80. }
  81. static int siphash_final(void *vmacctx, unsigned char *out, size_t *outl,
  82. size_t outsize)
  83. {
  84. struct siphash_data_st *ctx = vmacctx;
  85. size_t hlen = siphash_size(ctx);
  86. if (outsize < hlen)
  87. return 0;
  88. *outl = hlen;
  89. return SipHash_Final(&ctx->siphash, out, hlen);
  90. }
  91. static const OSSL_PARAM known_gettable_ctx_params[] = {
  92. OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
  93. OSSL_PARAM_END
  94. };
  95. static const OSSL_PARAM *siphash_gettable_ctx_params(ossl_unused void *provctx)
  96. {
  97. return known_gettable_ctx_params;
  98. }
  99. static int siphash_get_ctx_params(void *vmacctx, OSSL_PARAM params[])
  100. {
  101. OSSL_PARAM *p;
  102. if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL)
  103. return OSSL_PARAM_set_size_t(p, siphash_size(vmacctx));
  104. return 1;
  105. }
  106. static const OSSL_PARAM known_settable_ctx_params[] = {
  107. OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
  108. OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0),
  109. OSSL_PARAM_END
  110. };
  111. static const OSSL_PARAM *siphash_settable_params(void *provctx)
  112. {
  113. return known_settable_ctx_params;
  114. }
  115. static int siphash_set_params(void *vmacctx, const OSSL_PARAM *params)
  116. {
  117. struct siphash_data_st *ctx = vmacctx;
  118. const OSSL_PARAM *p = NULL;
  119. if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_SIZE)) != NULL) {
  120. size_t size;
  121. if (!OSSL_PARAM_get_size_t(p, &size)
  122. || !SipHash_set_hash_size(&ctx->siphash, size))
  123. return 0;
  124. }
  125. if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL)
  126. if (p->data_type != OSSL_PARAM_OCTET_STRING
  127. || p->data_size != SIPHASH_KEY_SIZE
  128. || !SipHash_Init(&ctx->siphash, p->data, 0, 0))
  129. return 0;
  130. return 1;
  131. }
  132. const OSSL_DISPATCH siphash_functions[] = {
  133. { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))siphash_new },
  134. { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))siphash_dup },
  135. { OSSL_FUNC_MAC_FREECTX, (void (*)(void))siphash_free },
  136. { OSSL_FUNC_MAC_INIT, (void (*)(void))siphash_init },
  137. { OSSL_FUNC_MAC_UPDATE, (void (*)(void))siphash_update },
  138. { OSSL_FUNC_MAC_FINAL, (void (*)(void))siphash_final },
  139. { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS,
  140. (void (*)(void))siphash_gettable_ctx_params },
  141. { OSSL_FUNC_MAC_GET_CTX_PARAMS, (void (*)(void))siphash_get_ctx_params },
  142. { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,
  143. (void (*)(void))siphash_settable_params },
  144. { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))siphash_set_params },
  145. { 0, NULL }
  146. };