provider_util.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright 2019-2022 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/provider.h>
  10. #include <openssl/types.h>
  11. typedef struct {
  12. /*
  13. * References to the underlying cipher implementation. |cipher| caches
  14. * the cipher, always. |alloc_cipher| only holds a reference to an
  15. * explicitly fetched cipher.
  16. */
  17. const EVP_CIPHER *cipher; /* cipher */
  18. EVP_CIPHER *alloc_cipher; /* fetched cipher */
  19. /* Conditions for legacy EVP_CIPHER uses */
  20. ENGINE *engine; /* cipher engine */
  21. } PROV_CIPHER;
  22. typedef struct {
  23. /*
  24. * References to the underlying digest implementation. |md| caches
  25. * the digest, always. |alloc_md| only holds a reference to an explicitly
  26. * fetched digest.
  27. */
  28. const EVP_MD *md; /* digest */
  29. EVP_MD *alloc_md; /* fetched digest */
  30. /* Conditions for legacy EVP_MD uses */
  31. ENGINE *engine; /* digest engine */
  32. } PROV_DIGEST;
  33. /* Cipher functions */
  34. /*
  35. * Load a cipher from the specified parameters with the specified context.
  36. * The params "properties", "engine" and "cipher" are used to determine the
  37. * implementation used. If a provider cannot be found, it falls back to trying
  38. * non-provider based implementations.
  39. */
  40. int ossl_prov_cipher_load_from_params(PROV_CIPHER *pc,
  41. const OSSL_PARAM params[],
  42. OSSL_LIB_CTX *ctx);
  43. /* Reset the PROV_CIPHER fields and free any allocated cipher reference */
  44. void ossl_prov_cipher_reset(PROV_CIPHER *pc);
  45. /* Clone a PROV_CIPHER structure into a second */
  46. int ossl_prov_cipher_copy(PROV_CIPHER *dst, const PROV_CIPHER *src);
  47. /* Query the cipher and associated engine (if any) */
  48. const EVP_CIPHER *ossl_prov_cipher_cipher(const PROV_CIPHER *pc);
  49. ENGINE *ossl_prov_cipher_engine(const PROV_CIPHER *pc);
  50. /* Digest functions */
  51. /*
  52. * Fetch a digest from the specified libctx using the provided mdname and
  53. * propquery. Store the result in the PROV_DIGEST and return the fetched md.
  54. */
  55. const EVP_MD *ossl_prov_digest_fetch(PROV_DIGEST *pd, OSSL_LIB_CTX *libctx,
  56. const char *mdname, const char *propquery);
  57. /*
  58. * Load a digest from the specified parameters with the specified context.
  59. * The params "properties", "engine" and "digest" are used to determine the
  60. * implementation used. If a provider cannot be found, it falls back to trying
  61. * non-provider based implementations.
  62. */
  63. int ossl_prov_digest_load_from_params(PROV_DIGEST *pd,
  64. const OSSL_PARAM params[],
  65. OSSL_LIB_CTX *ctx);
  66. /* Reset the PROV_DIGEST fields and free any allocated digest reference */
  67. void ossl_prov_digest_reset(PROV_DIGEST *pd);
  68. /* Clone a PROV_DIGEST structure into a second */
  69. int ossl_prov_digest_copy(PROV_DIGEST *dst, const PROV_DIGEST *src);
  70. /* Query the digest and associated engine (if any) */
  71. const EVP_MD *ossl_prov_digest_md(const PROV_DIGEST *pd);
  72. ENGINE *ossl_prov_digest_engine(const PROV_DIGEST *pd);
  73. /*
  74. * Set the various parameters on an EVP_MAC_CTX from the supplied arguments.
  75. * If any of the supplied ciphername/mdname etc are NULL then the values
  76. * from the supplied params (if non NULL) are used instead.
  77. */
  78. int ossl_prov_set_macctx(EVP_MAC_CTX *macctx,
  79. const OSSL_PARAM params[],
  80. const char *ciphername,
  81. const char *mdname,
  82. const char *engine,
  83. const char *properties,
  84. const unsigned char *key,
  85. size_t keylen);
  86. /* MAC functions */
  87. /*
  88. * Load an EVP_MAC_CTX* from the specified parameters with the specified
  89. * library context.
  90. * The params "mac" and "properties" are used to determine the implementation
  91. * used, and the parameters "digest", "cipher", "engine" and "properties" are
  92. * passed to the MAC via the created MAC context if they are given.
  93. * If there is already a created MAC context, it will be replaced if the "mac"
  94. * parameter is found, otherwise it will simply be used as is, and passed the
  95. * parameters to pilfer as it sees fit.
  96. *
  97. * As an option, a MAC name may be explicitly given, and if it is, the "mac"
  98. * parameter will be ignored.
  99. * Similarly, as an option, a cipher name or a digest name may be explicitly
  100. * given, and if any of them is, the "digest" and "cipher" parameters are
  101. * ignored.
  102. */
  103. int ossl_prov_macctx_load_from_params(EVP_MAC_CTX **macctx,
  104. const OSSL_PARAM params[],
  105. const char *macname,
  106. const char *ciphername,
  107. const char *mdname,
  108. OSSL_LIB_CTX *ctx);
  109. typedef struct ag_capable_st {
  110. OSSL_ALGORITHM alg;
  111. int (*capable)(void);
  112. } OSSL_ALGORITHM_CAPABLE;
  113. /*
  114. * Dynamically select algorithms by calling a capable() method.
  115. * If this method is NULL or the method returns 1 then the algorithm is added.
  116. */
  117. void ossl_prov_cache_exported_algorithms(const OSSL_ALGORITHM_CAPABLE *in,
  118. OSSL_ALGORITHM *out);
  119. /* Duplicate a lump of memory safely */
  120. int ossl_prov_memdup(const void *src, size_t src_len,
  121. unsigned char **dest, size_t *dest_len);