]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/ec/ecdh_ossl.c
Reorganize local header files
[thirdparty/openssl.git] / crypto / ec / ecdh_ossl.c
CommitLineData
4f22f405 1/*
c4d3c19b 2 * Copyright 2002-2018 The OpenSSL Project Authors. All Rights Reserved.
aa8f3d76 3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
4f22f405 4 *
a7f182b7 5 * Licensed under the Apache License 2.0 (the "License"). You may not use
4f22f405
RS
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
9 */
10
8b5bcef7 11#include <string.h>
176f31dd
BM
12#include <limits.h>
13
b39fc560 14#include "internal/cryptlib.h"
10a66ad3 15
e172d60d 16#include <openssl/err.h>
0f814687 17#include <openssl/bn.h>
647b2238
DSH
18#include <openssl/objects.h>
19#include <openssl/ec.h>
706457b7 20#include "ec_local.h"
e172d60d 21
e2285d87
DSH
22int ossl_ecdh_compute_key(unsigned char **psec, size_t *pseclen,
23 const EC_POINT *pub_key, const EC_KEY *ecdh)
77470e98 24{
9ff9bccc
DSH
25 if (ecdh->group->meth->ecdh_compute_key == NULL) {
26 ECerr(EC_F_OSSL_ECDH_COMPUTE_KEY, EC_R_CURVE_DOES_NOT_SUPPORT_ECDH);
65ea288d 27 return 0;
77470e98
DSH
28 }
29
e2285d87 30 return ecdh->group->meth->ecdh_compute_key(psec, pseclen, pub_key, ecdh);
77470e98
DSH
31}
32
3a83462d
MC
33/*-
34 * This implementation is based on the following primitives in the IEEE 1363 standard:
e172d60d
BM
35 * - ECKAS-DH1
36 * - ECSVDP-DH
e172d60d 37 */
e2285d87
DSH
38int ecdh_simple_compute_key(unsigned char **pout, size_t *poutlen,
39 const EC_POINT *pub_key, const EC_KEY *ecdh)
0f113f3e
MC
40{
41 BN_CTX *ctx;
42 EC_POINT *tmp = NULL;
ddc1caac 43 BIGNUM *x = NULL;
0f113f3e
MC
44 const BIGNUM *priv_key;
45 const EC_GROUP *group;
e2285d87 46 int ret = 0;
0f113f3e
MC
47 size_t buflen, len;
48 unsigned char *buf = NULL;
49
a9612d6c 50 if ((ctx = BN_CTX_new_ex(ecdh->libctx)) == NULL)
0f113f3e
MC
51 goto err;
52 BN_CTX_start(ctx);
53 x = BN_CTX_get(ctx);
ddc1caac 54 if (x == NULL) {
abea494c
BE
55 ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);
56 goto err;
57 }
0f113f3e
MC
58
59 priv_key = EC_KEY_get0_private_key(ecdh);
60 if (priv_key == NULL) {
7408f675 61 ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, EC_R_MISSING_PRIVATE_KEY);
0f113f3e
MC
62 goto err;
63 }
64
65 group = EC_KEY_get0_group(ecdh);
66
67 if (EC_KEY_get_flags(ecdh) & EC_FLAG_COFACTOR_ECDH) {
a773b52a 68 if (!EC_GROUP_get_cofactor(group, x, NULL) ||
0f113f3e 69 !BN_mul(x, x, priv_key, ctx)) {
77470e98 70 ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
71 goto err;
72 }
73 priv_key = x;
74 }
75
76 if ((tmp = EC_POINT_new(group)) == NULL) {
77470e98 77 ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
78 goto err;
79 }
80
81 if (!EC_POINT_mul(group, tmp, NULL, pub_key, priv_key, ctx)) {
77470e98 82 ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, EC_R_POINT_ARITHMETIC_FAILURE);
0f113f3e
MC
83 goto err;
84 }
85
9cc570d4
MC
86 if (!EC_POINT_get_affine_coordinates(group, tmp, x, NULL, ctx)) {
87 ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, EC_R_POINT_ARITHMETIC_FAILURE);
88 goto err;
0f113f3e 89 }
e172d60d 90
0f113f3e
MC
91 buflen = (EC_GROUP_get_degree(group) + 7) / 8;
92 len = BN_num_bytes(x);
93 if (len > buflen) {
77470e98 94 ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_INTERNAL_ERROR);
0f113f3e
MC
95 goto err;
96 }
97 if ((buf = OPENSSL_malloc(buflen)) == NULL) {
77470e98 98 ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
99 goto err;
100 }
101
102 memset(buf, 0, buflen - len);
103 if (len != (size_t)BN_bn2bin(x, buf + buflen - len)) {
77470e98 104 ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_BN_LIB);
0f113f3e
MC
105 goto err;
106 }
107
e2285d87
DSH
108 *pout = buf;
109 *poutlen = buflen;
110 buf = NULL;
111
112 ret = 1;
0f113f3e
MC
113
114 err:
1ff2c992 115 EC_POINT_clear_free(tmp);
ce1415ed 116 BN_CTX_end(ctx);
23a1d5e9 117 BN_CTX_free(ctx);
b548a1f1 118 OPENSSL_free(buf);
e2285d87 119 return ret;
0f113f3e 120}