summaryrefslogtreecommitdiff
path: root/crypto/ec/ec_kmeth.c
diff options
context:
space:
mode:
authorDr. Stephen Henson <steve@openssl.org>2016-02-29 14:12:11 +0000
committerDr. Stephen Henson <steve@openssl.org>2016-03-02 20:48:12 +0000
commite2285d878d28f55bffc731a34389a2ea91de779f (patch)
tree7a74382cf5836ef03bcacf64407c16c0924d63ef /crypto/ec/ec_kmeth.c
parent2ad9ef06a6aadeeb78a05ce18ace0ea5f300401b (diff)
downloadopenssl-new-e2285d878d28f55bffc731a34389a2ea91de779f.tar.gz
Handle KDF internally.
Handle KDF in ECDH_compute_key instead of requiring each implementation support it. This modifies the compute_key method: now it allocates and populates a buffer containing the shared secret. Reviewed-by: Rich Salz <rsalz@openssl.org>
Diffstat (limited to 'crypto/ec/ec_kmeth.c')
-rw-r--r--crypto/ec/ec_kmeth.c46
1 files changed, 28 insertions, 18 deletions
diff --git a/crypto/ec/ec_kmeth.c b/crypto/ec/ec_kmeth.c
index 1a15877461..fad74bf435 100644
--- a/crypto/ec/ec_kmeth.c
+++ b/crypto/ec/ec_kmeth.c
@@ -51,6 +51,7 @@
* ====================================================================
*/
+#include <string.h>
#include <openssl/ec.h>
#ifndef OPENSSL_NO_ENGINE
# include <openssl/engine.h>
@@ -165,10 +166,27 @@ int ECDH_compute_key(void *out, size_t outlen, const EC_POINT *pub_key,
void *(*KDF) (const void *in, size_t inlen, void *out,
size_t *outlen))
{
- if (eckey->meth->compute_key != NULL)
- return eckey->meth->compute_key(out, outlen, pub_key, eckey, KDF);
- ECerr(EC_F_ECDH_COMPUTE_KEY, EC_R_OPERATION_NOT_SUPPORTED);
- return 0;
+ unsigned char *sec = NULL;
+ size_t seclen;
+ if (eckey->meth->compute_key == NULL) {
+ ECerr(EC_F_ECDH_COMPUTE_KEY, EC_R_OPERATION_NOT_SUPPORTED);
+ return 0;
+ }
+ if (outlen > INT_MAX) {
+ ECerr(EC_F_ECDH_COMPUTE_KEY, EC_R_INVALID_OUTPUT_LENGTH);
+ return 0;
+ }
+ if (!eckey->meth->compute_key(&sec, &seclen, pub_key, eckey))
+ return 0;
+ if (KDF != NULL) {
+ KDF(sec, seclen, out, &outlen);
+ } else {
+ if (outlen > seclen)
+ outlen = seclen;
+ memcpy(out, sec, outlen);
+ }
+ OPENSSL_clear_free(sec, seclen);
+ return outlen;
}
EC_KEY_METHOD *EC_KEY_METHOD_new(const EC_KEY_METHOD *meth)
@@ -214,14 +232,10 @@ void EC_KEY_METHOD_set_keygen(EC_KEY_METHOD *meth,
}
void EC_KEY_METHOD_set_compute_key(EC_KEY_METHOD *meth,
- int (*ckey)(void *out,
- size_t outlen,
+ int (*ckey)(unsigned char **psec,
+ size_t *pseclen,
const EC_POINT *pub_key,
- const EC_KEY *ecdh,
- void *(*KDF) (const void *in,
- size_t inlen,
- void *out,
- size_t *outlen)))
+ const EC_KEY *ecdh))
{
meth->compute_key = ckey;
}
@@ -292,14 +306,10 @@ void EC_KEY_METHOD_get_keygen(EC_KEY_METHOD *meth,
}
void EC_KEY_METHOD_get_compute_key(EC_KEY_METHOD *meth,
- int (**pck)(void *out,
- size_t outlen,
+ int (**pck)(unsigned char **pout,
+ size_t *poutlen,
const EC_POINT *pub_key,
- const EC_KEY *ecdh,
- void *(*KDF) (const void *in,
- size_t inlen,
- void *out,
- size_t *outlen)))
+ const EC_KEY *ecdh))
{
if (pck != NULL)
*pck = meth->compute_key;