2016-05-17 18:51:04 +00:00
|
|
|
/*
|
2018-04-03 12:57:12 +00:00
|
|
|
* Copyright 2002-2018 The OpenSSL Project Authors. All Rights Reserved.
|
2017-06-15 14:16:46 +00:00
|
|
|
* Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
|
2016-05-17 18:51:04 +00:00
|
|
|
*
|
|
|
|
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
|
|
|
*/
|
|
|
|
|
2003-03-21 00:04:14 +00:00
|
|
|
#include <string.h>
|
2003-02-28 15:37:10 +00:00
|
|
|
#include <limits.h>
|
|
|
|
|
2015-05-14 14:56:48 +00:00
|
|
|
#include "internal/cryptlib.h"
|
2003-03-20 17:09:46 +00:00
|
|
|
|
2002-08-09 08:43:04 +00:00
|
|
|
#include <openssl/err.h>
|
2004-05-17 19:14:22 +00:00
|
|
|
#include <openssl/bn.h>
|
2015-10-22 17:47:11 +00:00
|
|
|
#include <openssl/objects.h>
|
|
|
|
#include <openssl/ec.h>
|
|
|
|
#include "ec_lcl.h"
|
2002-08-09 08:43:04 +00:00
|
|
|
|
2016-02-29 14:12:11 +00:00
|
|
|
int ossl_ecdh_compute_key(unsigned char **psec, size_t *pseclen,
|
|
|
|
const EC_POINT *pub_key, const EC_KEY *ecdh)
|
2016-02-28 17:47:06 +00:00
|
|
|
{
|
2016-02-28 17:48:48 +00:00
|
|
|
if (ecdh->group->meth->ecdh_compute_key == NULL) {
|
|
|
|
ECerr(EC_F_OSSL_ECDH_COMPUTE_KEY, EC_R_CURVE_DOES_NOT_SUPPORT_ECDH);
|
2016-06-17 12:55:15 +00:00
|
|
|
return 0;
|
2016-02-28 17:47:06 +00:00
|
|
|
}
|
|
|
|
|
2016-02-29 14:12:11 +00:00
|
|
|
return ecdh->group->meth->ecdh_compute_key(psec, pseclen, pub_key, ecdh);
|
2016-02-28 17:47:06 +00:00
|
|
|
}
|
|
|
|
|
2015-01-05 00:34:00 +00:00
|
|
|
/*-
|
|
|
|
* This implementation is based on the following primitives in the IEEE 1363 standard:
|
2002-08-09 08:43:04 +00:00
|
|
|
* - ECKAS-DH1
|
|
|
|
* - ECSVDP-DH
|
|
|
|
*/
|
2016-02-29 14:12:11 +00:00
|
|
|
int ecdh_simple_compute_key(unsigned char **pout, size_t *poutlen,
|
|
|
|
const EC_POINT *pub_key, const EC_KEY *ecdh)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
BN_CTX *ctx;
|
|
|
|
EC_POINT *tmp = NULL;
|
2018-03-06 19:00:24 +00:00
|
|
|
BIGNUM *x = NULL;
|
2015-01-22 03:40:55 +00:00
|
|
|
const BIGNUM *priv_key;
|
|
|
|
const EC_GROUP *group;
|
2016-02-29 14:12:11 +00:00
|
|
|
int ret = 0;
|
2015-01-22 03:40:55 +00:00
|
|
|
size_t buflen, len;
|
|
|
|
unsigned char *buf = NULL;
|
|
|
|
|
|
|
|
if ((ctx = BN_CTX_new()) == NULL)
|
|
|
|
goto err;
|
|
|
|
BN_CTX_start(ctx);
|
|
|
|
x = BN_CTX_get(ctx);
|
2018-03-06 19:00:24 +00:00
|
|
|
if (x == NULL) {
|
2017-06-13 16:08:40 +00:00
|
|
|
ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
2015-01-22 03:40:55 +00:00
|
|
|
|
|
|
|
priv_key = EC_KEY_get0_private_key(ecdh);
|
|
|
|
if (priv_key == NULL) {
|
2016-02-28 17:47:06 +00:00
|
|
|
ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, EC_R_NO_PRIVATE_VALUE);
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
group = EC_KEY_get0_group(ecdh);
|
|
|
|
|
|
|
|
if (EC_KEY_get_flags(ecdh) & EC_FLAG_COFACTOR_ECDH) {
|
2016-02-14 03:33:56 +00:00
|
|
|
if (!EC_GROUP_get_cofactor(group, x, NULL) ||
|
2015-01-22 03:40:55 +00:00
|
|
|
!BN_mul(x, x, priv_key, ctx)) {
|
2016-02-28 17:47:06 +00:00
|
|
|
ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
priv_key = x;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((tmp = EC_POINT_new(group)) == NULL) {
|
2016-02-28 17:47:06 +00:00
|
|
|
ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!EC_POINT_mul(group, tmp, NULL, pub_key, priv_key, ctx)) {
|
2016-02-28 17:47:06 +00:00
|
|
|
ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, EC_R_POINT_ARITHMETIC_FAILURE);
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) ==
|
|
|
|
NID_X9_62_prime_field) {
|
2018-03-06 19:00:24 +00:00
|
|
|
if (!EC_POINT_get_affine_coordinates_GFp(group, tmp, x, NULL, ctx)) {
|
2016-02-28 17:47:06 +00:00
|
|
|
ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, EC_R_POINT_ARITHMETIC_FAILURE);
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
2011-02-12 17:23:32 +00:00
|
|
|
#ifndef OPENSSL_NO_EC2M
|
2015-01-22 03:40:55 +00:00
|
|
|
else {
|
2018-03-06 19:00:24 +00:00
|
|
|
if (!EC_POINT_get_affine_coordinates_GF2m(group, tmp, x, NULL, ctx)) {
|
2016-02-28 17:47:06 +00:00
|
|
|
ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, EC_R_POINT_ARITHMETIC_FAILURE);
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
2011-02-12 17:23:32 +00:00
|
|
|
#endif
|
2002-08-09 08:43:04 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
buflen = (EC_GROUP_get_degree(group) + 7) / 8;
|
|
|
|
len = BN_num_bytes(x);
|
|
|
|
if (len > buflen) {
|
2016-02-28 17:47:06 +00:00
|
|
|
ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_INTERNAL_ERROR);
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if ((buf = OPENSSL_malloc(buflen)) == NULL) {
|
2016-02-28 17:47:06 +00:00
|
|
|
ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(buf, 0, buflen - len);
|
|
|
|
if (len != (size_t)BN_bn2bin(x, buf + buflen - len)) {
|
2016-02-28 17:47:06 +00:00
|
|
|
ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_BN_LIB);
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2016-02-29 14:12:11 +00:00
|
|
|
*pout = buf;
|
|
|
|
*poutlen = buflen;
|
|
|
|
buf = NULL;
|
|
|
|
|
|
|
|
ret = 1;
|
2015-01-22 03:40:55 +00:00
|
|
|
|
|
|
|
err:
|
2015-03-25 22:35:24 +00:00
|
|
|
EC_POINT_free(tmp);
|
2015-01-22 03:40:55 +00:00
|
|
|
if (ctx)
|
|
|
|
BN_CTX_end(ctx);
|
2015-05-01 01:37:06 +00:00
|
|
|
BN_CTX_free(ctx);
|
2015-05-01 14:02:07 +00:00
|
|
|
OPENSSL_free(buf);
|
2016-02-29 14:12:11 +00:00
|
|
|
return ret;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|