s2n_evp.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License").
  5. * You may not use this file except in compliance with the License.
  6. * A copy of the License is located at
  7. *
  8. * http://aws.amazon.com/apache2.0
  9. *
  10. * or in the "license" file accompanying this file. This file is distributed
  11. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  12. * express or implied. See the License for the specific language governing
  13. * permissions and limitations under the License.
  14. */
  15. #pragma once
  16. #include <openssl/evp.h>
  17. #include <openssl/hmac.h>
  18. #include "crypto/s2n_openssl.h"
  19. #include "utils/s2n_result.h"
  20. struct s2n_evp_digest {
  21. const EVP_MD *md;
  22. EVP_MD_CTX *ctx;
  23. };
  24. struct s2n_evp_hmac_state {
  25. struct s2n_evp_digest evp_digest;
  26. union {
  27. HMAC_CTX *hmac_ctx;
  28. EVP_PKEY *evp_pkey;
  29. } ctx;
  30. };
  31. /* Define API's that change based on the OpenSSL Major Version. */
  32. #if S2N_OPENSSL_VERSION_AT_LEAST(1, 1, 0) && !defined(LIBRESSL_VERSION_NUMBER)
  33. #define S2N_EVP_MD_CTX_NEW() (EVP_MD_CTX_new())
  34. #define S2N_EVP_MD_CTX_RESET(md_ctx) (EVP_MD_CTX_reset(md_ctx))
  35. #define S2N_EVP_MD_CTX_FREE(md_ctx) (EVP_MD_CTX_free(md_ctx))
  36. #else
  37. #define S2N_EVP_MD_CTX_NEW() (EVP_MD_CTX_create())
  38. #define S2N_EVP_MD_CTX_RESET(md_ctx) (EVP_MD_CTX_cleanup(md_ctx))
  39. #define S2N_EVP_MD_CTX_FREE(md_ctx) (EVP_MD_CTX_destroy(md_ctx))
  40. #endif
  41. /* On some versions of OpenSSL, "EVP_PKEY_CTX_set_signature_md()" is just a macro that casts digest_alg to "void*",
  42. * which fails to compile when the "-Werror=cast-qual" compiler flag is enabled. So we work around this OpenSSL
  43. * issue by turning off this compiler check for this one function with a cast through.
  44. */
  45. #define S2N_EVP_PKEY_CTX_set_signature_md(ctx, md) \
  46. EVP_PKEY_CTX_set_signature_md(ctx, (EVP_MD *) (uintptr_t) md)
  47. int s2n_digest_allow_md5_for_fips(struct s2n_evp_digest *evp_digest);
  48. S2N_RESULT s2n_digest_is_md5_allowed_for_fips(struct s2n_evp_digest *evp_digest, bool *out);