provider_seeding.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * Copyright 2020-2021 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 <openssl/core_dispatch.h>
  10. #include "prov/seeding.h"
  11. static OSSL_FUNC_get_entropy_fn *c_get_entropy = NULL;
  12. static OSSL_FUNC_cleanup_entropy_fn *c_cleanup_entropy = NULL;
  13. static OSSL_FUNC_get_nonce_fn *c_get_nonce = NULL;
  14. static OSSL_FUNC_cleanup_nonce_fn *c_cleanup_nonce = NULL;
  15. int ossl_prov_seeding_from_dispatch(const OSSL_DISPATCH *fns)
  16. {
  17. for (; fns->function_id != 0; fns++) {
  18. /*
  19. * We do not support the scenario of an application linked against
  20. * multiple versions of libcrypto (e.g. one static and one dynamic), but
  21. * sharing a single fips.so. We do a simple sanity check here.
  22. */
  23. #define set_func(c, f) \
  24. do { if (c == NULL) c = f; else if (c != f) return 0; } while (0)
  25. switch (fns->function_id) {
  26. case OSSL_FUNC_GET_ENTROPY:
  27. set_func(c_get_entropy, OSSL_FUNC_get_entropy(fns));
  28. break;
  29. case OSSL_FUNC_CLEANUP_ENTROPY:
  30. set_func(c_cleanup_entropy, OSSL_FUNC_cleanup_entropy(fns));
  31. break;
  32. case OSSL_FUNC_GET_NONCE:
  33. set_func(c_get_nonce, OSSL_FUNC_get_nonce(fns));
  34. break;
  35. case OSSL_FUNC_CLEANUP_NONCE:
  36. set_func(c_cleanup_nonce, OSSL_FUNC_cleanup_nonce(fns));
  37. break;
  38. }
  39. #undef set_func
  40. }
  41. return 1;
  42. }
  43. size_t ossl_prov_get_entropy(PROV_CTX *prov_ctx, unsigned char **pout,
  44. int entropy, size_t min_len, size_t max_len)
  45. {
  46. if (c_get_entropy == NULL)
  47. return 0;
  48. return c_get_entropy(ossl_prov_ctx_get0_handle(prov_ctx),
  49. pout, entropy, min_len, max_len);
  50. }
  51. void ossl_prov_cleanup_entropy(PROV_CTX *prov_ctx, unsigned char *buf,
  52. size_t len)
  53. {
  54. if (c_cleanup_entropy != NULL)
  55. c_cleanup_entropy(ossl_prov_ctx_get0_handle(prov_ctx), buf, len);
  56. }
  57. size_t ossl_prov_get_nonce(PROV_CTX *prov_ctx, unsigned char **pout,
  58. size_t min_len, size_t max_len,
  59. const void *salt,size_t salt_len)
  60. {
  61. if (c_get_nonce == NULL)
  62. return 0;
  63. return c_get_nonce(ossl_prov_ctx_get0_handle(prov_ctx), pout,
  64. min_len, max_len, salt, salt_len);
  65. }
  66. void ossl_prov_cleanup_nonce(PROV_CTX *prov_ctx, unsigned char *buf, size_t len)
  67. {
  68. if (c_cleanup_nonce != NULL)
  69. c_cleanup_nonce(ossl_prov_ctx_get0_handle(prov_ctx), buf, len);
  70. }