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>
This commit is contained in:
Adam Langley 2017-05-05 12:05:25 -07:00 committed by CQ bot account: commit-bot@chromium.org
parent a90044a463
commit 4249481a66
2 changed files with 31 additions and 0 deletions

View File

@ -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 @@ 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) {

View File

@ -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<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++