From 7177c1d29fdb87a260a0510f815828cb02db99be Mon Sep 17 00:00:00 2001 From: Jeremy Apthorp Date: Wed, 19 Dec 2018 14:46:14 -0800 Subject: [PATCH] Add EC_KEY_key2buf for OpenSSL compatibility Change-Id: If45ef3a9bb757bd0c7f592f40ececaf4aa2f607d Reviewed-on: https://boringssl-review.googlesource.com/c/33824 Reviewed-by: Adam Langley Commit-Queue: Adam Langley --- crypto/fipsmodule/ec/ec_key.c | 27 +++++++++++++++++++++++++++ include/openssl/ec_key.h | 6 ++++++ 2 files changed, 33 insertions(+) diff --git a/crypto/fipsmodule/ec/ec_key.c b/crypto/fipsmodule/ec/ec_key.c index 632dc9b2..4bc12a07 100644 --- a/crypto/fipsmodule/ec/ec_key.c +++ b/crypto/fipsmodule/ec/ec_key.c @@ -394,6 +394,33 @@ err: return ok; } +size_t EC_KEY_key2buf(EC_KEY *key, point_conversion_form_t form, + unsigned char **out_buf, BN_CTX *ctx) { + if (key == NULL || key->pub_key == NULL || key->group == NULL) { + return 0; + } + + const size_t len = + EC_POINT_point2oct(key->group, key->pub_key, form, NULL, 0, ctx); + if (len == 0) { + return 0; + } + + uint8_t *buf = OPENSSL_malloc(len); + if (buf == NULL) { + return 0; + } + + if (EC_POINT_point2oct(key->group, key->pub_key, form, buf, len, ctx) != + len) { + OPENSSL_free(buf); + return 0; + } + + *out_buf = buf; + return len; +} + int EC_KEY_generate_key(EC_KEY *key) { if (key == NULL || key->group == NULL) { OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER); diff --git a/include/openssl/ec_key.h b/include/openssl/ec_key.h index 9bc78875..3b1a5666 100644 --- a/include/openssl/ec_key.h +++ b/include/openssl/ec_key.h @@ -177,6 +177,12 @@ OPENSSL_EXPORT int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key, BIGNUM *x, BIGNUM *y); +// EC_KEY_key2buf encodes the public key in |key| to an allocated octet string +// and sets |*out_buf| to point to it. It returns the length of the encoded +// octet string or zero if an error occurred. +OPENSSL_EXPORT size_t EC_KEY_key2buf(EC_KEY *key, point_conversion_form_t form, + unsigned char **out_buf, BN_CTX *ctx); + // Key generation.