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