s2n_cipher.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. #include <openssl/evp.h>
  16. #if defined(OPENSSL_IS_BORINGSSL) || defined(OPENSSL_IS_AWSLC)
  17. #error #include <openssl/mem.h>
  18. #endif
  19. #include "crypto/s2n_cipher.h"
  20. #include "utils/s2n_safety.h"
  21. int s2n_session_key_alloc(struct s2n_session_key *key)
  22. {
  23. POSIX_ENSURE_EQ(key->evp_cipher_ctx, NULL);
  24. POSIX_ENSURE_REF(key->evp_cipher_ctx = EVP_CIPHER_CTX_new());
  25. #if defined(S2N_CIPHER_AEAD_API_AVAILABLE)
  26. POSIX_ENSURE_EQ(key->evp_aead_ctx, NULL);
  27. key->evp_aead_ctx = OPENSSL_malloc(sizeof(EVP_AEAD_CTX));
  28. if (key->evp_aead_ctx == NULL) {
  29. EVP_CIPHER_CTX_free(key->evp_cipher_ctx);
  30. S2N_ERROR_PRESERVE_ERRNO();
  31. }
  32. EVP_AEAD_CTX_zero(key->evp_aead_ctx);
  33. #endif
  34. return 0;
  35. }
  36. int s2n_session_key_free(struct s2n_session_key *key)
  37. {
  38. if (key->evp_cipher_ctx != NULL) {
  39. EVP_CIPHER_CTX_free(key->evp_cipher_ctx);
  40. key->evp_cipher_ctx = NULL;
  41. }
  42. #if defined(S2N_CIPHER_AEAD_API_AVAILABLE)
  43. if (key->evp_aead_ctx != NULL) {
  44. EVP_AEAD_CTX_free(key->evp_aead_ctx);
  45. key->evp_aead_ctx = NULL;
  46. }
  47. #endif
  48. return 0;
  49. }