From 4249481a66f12d701725df1ab538d71882ffaa7e Mon Sep 17 00:00:00 2001 From: Adam Langley Date: Fri, 5 May 2017 12:05:25 -0700 Subject: [PATCH] Add EVP_AEAD_CTX_[new|free] and UniquePtr support. EVP_AEAD_CTX is otherwise a pain to use from C++ when you need to keep it around. Change-Id: I1dff926b33a3246680be21b89b69dfb336d25cd5 Reviewed-on: https://boringssl-review.googlesource.com/15965 Commit-Queue: David Benjamin Reviewed-by: David Benjamin CQ-Verified: CQ bot account: commit-bot@chromium.org --- crypto/cipher/aead.c | 19 +++++++++++++++++++ include/openssl/aead.h | 12 ++++++++++++ 2 files changed, 31 insertions(+) diff --git a/crypto/cipher/aead.c b/crypto/cipher/aead.c index b5ff48a0..40b0bbf8 100644 --- a/crypto/cipher/aead.c +++ b/crypto/cipher/aead.c @@ -18,6 +18,7 @@ #include #include +#include #include "internal.h" #include "../internal.h" @@ -35,6 +36,24 @@ void EVP_AEAD_CTX_zero(EVP_AEAD_CTX *ctx) { OPENSSL_memset(ctx, 0, sizeof(EVP_AEAD_CTX)); } +EVP_AEAD_CTX *EVP_AEAD_CTX_new(const EVP_AEAD *aead, const uint8_t *key, + size_t key_len, size_t tag_len) { + EVP_AEAD_CTX *ctx = OPENSSL_malloc(sizeof(EVP_AEAD_CTX)); + EVP_AEAD_CTX_zero(ctx); + + if (EVP_AEAD_CTX_init(ctx, aead, key, key_len, tag_len, NULL)) { + return ctx; + } + + EVP_AEAD_CTX_free(ctx); + return NULL; +} + +void EVP_AEAD_CTX_free(EVP_AEAD_CTX *ctx) { + EVP_AEAD_CTX_cleanup(ctx); + OPENSSL_free(ctx); +} + int EVP_AEAD_CTX_init(EVP_AEAD_CTX *ctx, const EVP_AEAD *aead, const uint8_t *key, size_t key_len, size_t tag_len, ENGINE *impl) { diff --git a/include/openssl/aead.h b/include/openssl/aead.h index 521e1838..7515ba19 100644 --- a/include/openssl/aead.h +++ b/include/openssl/aead.h @@ -186,6 +186,16 @@ typedef struct evp_aead_ctx_st { * more uniform cleanup of |EVP_AEAD_CTX|. */ OPENSSL_EXPORT void EVP_AEAD_CTX_zero(EVP_AEAD_CTX *ctx); +/* EVP_AEAD_CTX_new allocates an |EVP_AEAD_CTX|, calls |EVP_AEAD_CTX_init| and + * returns the |EVP_AEAD_CTX|, or NULL on error. */ +OPENSSL_EXPORT EVP_AEAD_CTX *EVP_AEAD_CTX_new(const EVP_AEAD *aead, + const uint8_t *key, + size_t key_len, size_t tag_len); + +/* EVP_AEAD_CTX_free calls |EVP_AEAD_CTX_cleanup| and |OPENSSL_free| on + * |ctx|. */ +OPENSSL_EXPORT void EVP_AEAD_CTX_free(EVP_AEAD_CTX *ctx); + /* EVP_AEAD_CTX_init initializes |ctx| for the given AEAD algorithm. The |impl| * argument is ignored and should be NULL. Authentication tags may be truncated * by passing a size as |tag_len|. A |tag_len| of zero indicates the default @@ -334,6 +344,8 @@ using ScopedEVP_AEAD_CTX = internal::StackAllocated; +BORINGSSL_MAKE_DELETER(EVP_AEAD_CTX, EVP_AEAD_CTX_free) + } // namespace bssl } // extern C++