]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/ec/ecdh_ossl.c
Declare a new x509v3 extension: x509ExtAdmission
[thirdparty/openssl.git] / crypto / ec / ecdh_ossl.c
CommitLineData
4f22f405
RS
1/*
2 * Copyright 2002-2016 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
e172d60d
BM
10/* ====================================================================
11 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
12 *
13 * The Elliptic Curve Public-Key Crypto Library (ECC Code) included
14 * herein is developed by SUN MICROSYSTEMS, INC., and is contributed
15 * to the OpenSSL project.
16 *
17 * The ECC Code is licensed pursuant to the OpenSSL open source
18 * license provided below.
19 *
e172d60d
BM
20 * The ECDH software is originally written by Douglas Stebila of
21 * Sun Microsystems Laboratories.
22 *
23 */
e172d60d 24
8b5bcef7 25#include <string.h>
176f31dd
BM
26#include <limits.h>
27
b39fc560 28#include "internal/cryptlib.h"
10a66ad3 29
e172d60d 30#include <openssl/err.h>
0f814687 31#include <openssl/bn.h>
647b2238
DSH
32#include <openssl/objects.h>
33#include <openssl/ec.h>
34#include "ec_lcl.h"
e172d60d 35
e2285d87
DSH
36int ossl_ecdh_compute_key(unsigned char **psec, size_t *pseclen,
37 const EC_POINT *pub_key, const EC_KEY *ecdh)
77470e98 38{
9ff9bccc
DSH
39 if (ecdh->group->meth->ecdh_compute_key == NULL) {
40 ECerr(EC_F_OSSL_ECDH_COMPUTE_KEY, EC_R_CURVE_DOES_NOT_SUPPORT_ECDH);
65ea288d 41 return 0;
77470e98
DSH
42 }
43
e2285d87 44 return ecdh->group->meth->ecdh_compute_key(psec, pseclen, pub_key, ecdh);
77470e98
DSH
45}
46
3a83462d
MC
47/*-
48 * This implementation is based on the following primitives in the IEEE 1363 standard:
e172d60d
BM
49 * - ECKAS-DH1
50 * - ECSVDP-DH
e172d60d 51 */
e2285d87
DSH
52int ecdh_simple_compute_key(unsigned char **pout, size_t *poutlen,
53 const EC_POINT *pub_key, const EC_KEY *ecdh)
0f113f3e
MC
54{
55 BN_CTX *ctx;
56 EC_POINT *tmp = NULL;
57 BIGNUM *x = NULL, *y = NULL;
58 const BIGNUM *priv_key;
59 const EC_GROUP *group;
e2285d87 60 int ret = 0;
0f113f3e
MC
61 size_t buflen, len;
62 unsigned char *buf = NULL;
63
0f113f3e
MC
64 if ((ctx = BN_CTX_new()) == NULL)
65 goto err;
66 BN_CTX_start(ctx);
67 x = BN_CTX_get(ctx);
68 y = BN_CTX_get(ctx);
abea494c
BE
69 if (y == NULL) {
70 ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);
71 goto err;
72 }
0f113f3e
MC
73
74 priv_key = EC_KEY_get0_private_key(ecdh);
75 if (priv_key == NULL) {
77470e98 76 ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, EC_R_NO_PRIVATE_VALUE);
0f113f3e
MC
77 goto err;
78 }
79
80 group = EC_KEY_get0_group(ecdh);
81
82 if (EC_KEY_get_flags(ecdh) & EC_FLAG_COFACTOR_ECDH) {
a773b52a 83 if (!EC_GROUP_get_cofactor(group, x, NULL) ||
0f113f3e 84 !BN_mul(x, x, priv_key, ctx)) {
77470e98 85 ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
86 goto err;
87 }
88 priv_key = x;
89 }
90
91 if ((tmp = EC_POINT_new(group)) == NULL) {
77470e98 92 ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
93 goto err;
94 }
95
96 if (!EC_POINT_mul(group, tmp, NULL, pub_key, priv_key, ctx)) {
77470e98 97 ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, EC_R_POINT_ARITHMETIC_FAILURE);
0f113f3e
MC
98 goto err;
99 }
100
101 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) ==
102 NID_X9_62_prime_field) {
103 if (!EC_POINT_get_affine_coordinates_GFp(group, tmp, x, y, ctx)) {
77470e98 104 ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, EC_R_POINT_ARITHMETIC_FAILURE);
0f113f3e
MC
105 goto err;
106 }
107 }
b3310161 108#ifndef OPENSSL_NO_EC2M
0f113f3e
MC
109 else {
110 if (!EC_POINT_get_affine_coordinates_GF2m(group, tmp, x, y, ctx)) {
77470e98 111 ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, EC_R_POINT_ARITHMETIC_FAILURE);
0f113f3e
MC
112 goto err;
113 }
114 }
b3310161 115#endif
e172d60d 116
0f113f3e
MC
117 buflen = (EC_GROUP_get_degree(group) + 7) / 8;
118 len = BN_num_bytes(x);
119 if (len > buflen) {
77470e98 120 ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_INTERNAL_ERROR);
0f113f3e
MC
121 goto err;
122 }
123 if ((buf = OPENSSL_malloc(buflen)) == NULL) {
77470e98 124 ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
125 goto err;
126 }
127
128 memset(buf, 0, buflen - len);
129 if (len != (size_t)BN_bn2bin(x, buf + buflen - len)) {
77470e98 130 ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_BN_LIB);
0f113f3e
MC
131 goto err;
132 }
133
e2285d87
DSH
134 *pout = buf;
135 *poutlen = buflen;
136 buf = NULL;
137
138 ret = 1;
0f113f3e
MC
139
140 err:
8fdc3734 141 EC_POINT_free(tmp);
0f113f3e
MC
142 if (ctx)
143 BN_CTX_end(ctx);
23a1d5e9 144 BN_CTX_free(ctx);
b548a1f1 145 OPENSSL_free(buf);
e2285d87 146 return ret;
0f113f3e 147}