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 <davidben@google.com>
Reviewed-by: David Benjamin <davidben@google.com>
CQ-Verified: CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org>
diff --git a/crypto/cipher/aead.c b/crypto/cipher/aead.c
index b5ff48a..40b0bbf 100644
--- a/crypto/cipher/aead.c
+++ b/crypto/cipher/aead.c
@@ -18,6 +18,7 @@
#include <openssl/cipher.h>
#include <openssl/err.h>
+#include <openssl/mem.h>
#include "internal.h"
#include "../internal.h"
@@ -35,6 +36,24 @@
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 521e183..7515ba1 100644
--- a/include/openssl/aead.h
+++ b/include/openssl/aead.h
@@ -186,6 +186,16 @@
* 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 @@
internal::StackAllocated<EVP_AEAD_CTX, void, EVP_AEAD_CTX_zero,
EVP_AEAD_CTX_cleanup>;
+BORINGSSL_MAKE_DELETER(EVP_AEAD_CTX, EVP_AEAD_CTX_free)
+
} // namespace bssl
} // extern C++