Skip to content

Commit 897a2ca

Browse files
davidbenBoringssl LUCI CQ
authored and
Boringssl LUCI CQ
committed
Add convenience functions to malloc EVP_HPKE_CTX and EVP_HPKE_KEY.
Some callers want the value to be heap-allocated. It's a little annoying that this returns an empty value (if we only supported heap-allocated ones, I'd have merged init into new), but since we have multiple constructor functions, this is probably the least fuss. Change-Id: I42f586e39850954fb6743f8be50a7cfffa0755ba Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/48526 Commit-Queue: David Benjamin <[email protected]> Reviewed-by: Adam Langley <[email protected]>
1 parent 6191cc9 commit 897a2ca

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

crypto/hpke/hpke.c

+34
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,23 @@ void EVP_HPKE_KEY_cleanup(EVP_HPKE_KEY *key) {
235235
// future.
236236
}
237237

238+
EVP_HPKE_KEY *EVP_HPKE_KEY_new(void) {
239+
EVP_HPKE_KEY *key = OPENSSL_malloc(sizeof(EVP_HPKE_KEY));
240+
if (key == NULL) {
241+
OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE);
242+
return NULL;
243+
}
244+
EVP_HPKE_KEY_zero(key);
245+
return key;
246+
}
247+
248+
void EVP_HPKE_KEY_free(EVP_HPKE_KEY *key) {
249+
if (key != NULL) {
250+
EVP_HPKE_KEY_cleanup(key);
251+
OPENSSL_free(key);
252+
}
253+
}
254+
238255
int EVP_HPKE_KEY_copy(EVP_HPKE_KEY *dst, const EVP_HPKE_KEY *src) {
239256
// For now, |EVP_HPKE_KEY| is trivially copyable.
240257
OPENSSL_memcpy(dst, src, sizeof(EVP_HPKE_KEY));
@@ -431,6 +448,23 @@ void EVP_HPKE_CTX_cleanup(EVP_HPKE_CTX *ctx) {
431448
EVP_AEAD_CTX_cleanup(&ctx->aead_ctx);
432449
}
433450

451+
EVP_HPKE_CTX *EVP_HPKE_CTX_new(void) {
452+
EVP_HPKE_CTX *ctx = OPENSSL_malloc(sizeof(EVP_HPKE_CTX));
453+
if (ctx == NULL) {
454+
OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE);
455+
return NULL;
456+
}
457+
EVP_HPKE_CTX_zero(ctx);
458+
return ctx;
459+
}
460+
461+
void EVP_HPKE_CTX_free(EVP_HPKE_CTX *ctx) {
462+
if (ctx != NULL) {
463+
EVP_HPKE_CTX_cleanup(ctx);
464+
OPENSSL_free(ctx);
465+
}
466+
}
467+
434468
int EVP_HPKE_CTX_setup_sender(EVP_HPKE_CTX *ctx, uint8_t *out_enc,
435469
size_t *out_enc_len, size_t max_enc,
436470
const EVP_HPKE_KEM *kem, const EVP_HPKE_KDF *kdf,

include/openssl/hpke.h

+25
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,17 @@ OPENSSL_EXPORT void EVP_HPKE_KEY_zero(EVP_HPKE_KEY *key);
9393
// EVP_HPKE_KEY_cleanup releases memory referenced by |key|.
9494
OPENSSL_EXPORT void EVP_HPKE_KEY_cleanup(EVP_HPKE_KEY *key);
9595

96+
// EVP_HPKE_KEY_new returns a newly-allocated |EVP_HPKE_KEY|, or NULL on error.
97+
// The caller must call |EVP_HPKE_KEY_free| on the result to release it.
98+
//
99+
// This is a convenience function for callers that need a heap-allocated
100+
// |EVP_HPKE_KEY|.
101+
OPENSSL_EXPORT EVP_HPKE_KEY *EVP_HPKE_KEY_new(void);
102+
103+
// EVP_HPKE_KEY_free releases memory associated with |key|, which must have been
104+
// created with |EVP_HPKE_KEY_new|.
105+
OPENSSL_EXPORT void EVP_HPKE_KEY_free(EVP_HPKE_KEY *key);
106+
96107
// EVP_HPKE_KEY_copy sets |dst| to a copy of |src|. It returns one on success
97108
// and zero on error. On success, the caller must call |EVP_HPKE_KEY_cleanup| to
98109
// release |dst|. On failure, calling |EVP_HPKE_KEY_cleanup| is safe, but not
@@ -160,6 +171,17 @@ OPENSSL_EXPORT void EVP_HPKE_CTX_zero(EVP_HPKE_CTX *ctx);
160171
// |EVP_HPKE_CTX_setup_*| functions.
161172
OPENSSL_EXPORT void EVP_HPKE_CTX_cleanup(EVP_HPKE_CTX *ctx);
162173

174+
// EVP_HPKE_CTX_new returns a newly-allocated |EVP_HPKE_CTX|, or NULL on error.
175+
// The caller must call |EVP_HPKE_CTX_free| on the result to release it.
176+
//
177+
// This is a convenience function for callers that need a heap-allocated
178+
// |EVP_HPKE_CTX|.
179+
OPENSSL_EXPORT EVP_HPKE_CTX *EVP_HPKE_CTX_new(void);
180+
181+
// EVP_HPKE_CTX_free releases memory associated with |ctx|, which must have been
182+
// created with |EVP_HPKE_CTX_new|.
183+
OPENSSL_EXPORT void EVP_HPKE_CTX_free(EVP_HPKE_CTX *ctx);
184+
163185
// EVP_HPKE_MAX_ENC_LENGTH is the maximum length of "enc", the encapsulated
164186
// shared secret, for all supported KEMs in this library.
165187
#define EVP_HPKE_MAX_ENC_LENGTH 32
@@ -317,6 +339,9 @@ using ScopedEVP_HPKE_KEY =
317339
internal::StackAllocated<EVP_HPKE_KEY, void, EVP_HPKE_KEY_zero,
318340
EVP_HPKE_KEY_cleanup>;
319341

342+
BORINGSSL_MAKE_DELETER(EVP_HPKE_CTX, EVP_HPKE_CTX_free)
343+
BORINGSSL_MAKE_DELETER(EVP_HPKE_KEY, EVP_HPKE_KEY_free)
344+
320345
BSSL_NAMESPACE_END
321346

322347
} // extern C++

0 commit comments

Comments
 (0)