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 |
|
---|
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 | *
|
---|
20 | * The ECDH software is originally written by Douglas Stebila of
|
---|
21 | * Sun Microsystems Laboratories.
|
---|
22 | *
|
---|
23 | */
|
---|
24 |
|
---|
25 | #include <string.h>
|
---|
26 | #include <limits.h>
|
---|
27 |
|
---|
28 | #include "internal/cryptlib.h"
|
---|
29 |
|
---|
30 | #include <openssl/err.h>
|
---|
31 | #include <openssl/bn.h>
|
---|
32 | #include <openssl/objects.h>
|
---|
33 | #include <openssl/ec.h>
|
---|
34 | #include "ec_lcl.h"
|
---|
35 |
|
---|
36 | int ossl_ecdh_compute_key(unsigned char **psec, size_t *pseclen,
|
---|
37 | const EC_POINT *pub_key, const EC_KEY *ecdh)
|
---|
38 | {
|
---|
39 | if (ecdh->group->meth->ecdh_compute_key == NULL) {
|
---|
40 | ECerr(EC_F_OSSL_ECDH_COMPUTE_KEY, EC_R_CURVE_DOES_NOT_SUPPORT_ECDH);
|
---|
41 | return 0;
|
---|
42 | }
|
---|
43 |
|
---|
44 | return ecdh->group->meth->ecdh_compute_key(psec, pseclen, pub_key, ecdh);
|
---|
45 | }
|
---|
46 |
|
---|
47 | /*-
|
---|
48 | * This implementation is based on the following primitives in the IEEE 1363 standard:
|
---|
49 | * - ECKAS-DH1
|
---|
50 | * - ECSVDP-DH
|
---|
51 | */
|
---|
52 | int ecdh_simple_compute_key(unsigned char **pout, size_t *poutlen,
|
---|
53 | const EC_POINT *pub_key, const EC_KEY *ecdh)
|
---|
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;
|
---|
60 | int ret = 0;
|
---|
61 | size_t buflen, len;
|
---|
62 | unsigned char *buf = NULL;
|
---|
63 |
|
---|
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);
|
---|
69 |
|
---|
70 | priv_key = EC_KEY_get0_private_key(ecdh);
|
---|
71 | if (priv_key == NULL) {
|
---|
72 | ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, EC_R_NO_PRIVATE_VALUE);
|
---|
73 | goto err;
|
---|
74 | }
|
---|
75 |
|
---|
76 | group = EC_KEY_get0_group(ecdh);
|
---|
77 |
|
---|
78 | if (EC_KEY_get_flags(ecdh) & EC_FLAG_COFACTOR_ECDH) {
|
---|
79 | if (!EC_GROUP_get_cofactor(group, x, NULL) ||
|
---|
80 | !BN_mul(x, x, priv_key, ctx)) {
|
---|
81 | ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);
|
---|
82 | goto err;
|
---|
83 | }
|
---|
84 | priv_key = x;
|
---|
85 | }
|
---|
86 |
|
---|
87 | if ((tmp = EC_POINT_new(group)) == NULL) {
|
---|
88 | ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);
|
---|
89 | goto err;
|
---|
90 | }
|
---|
91 |
|
---|
92 | if (!EC_POINT_mul(group, tmp, NULL, pub_key, priv_key, ctx)) {
|
---|
93 | ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, EC_R_POINT_ARITHMETIC_FAILURE);
|
---|
94 | goto err;
|
---|
95 | }
|
---|
96 |
|
---|
97 | if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) ==
|
---|
98 | NID_X9_62_prime_field) {
|
---|
99 | if (!EC_POINT_get_affine_coordinates_GFp(group, tmp, x, y, ctx)) {
|
---|
100 | ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, EC_R_POINT_ARITHMETIC_FAILURE);
|
---|
101 | goto err;
|
---|
102 | }
|
---|
103 | }
|
---|
104 | #ifndef OPENSSL_NO_EC2M
|
---|
105 | else {
|
---|
106 | if (!EC_POINT_get_affine_coordinates_GF2m(group, tmp, x, y, ctx)) {
|
---|
107 | ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, EC_R_POINT_ARITHMETIC_FAILURE);
|
---|
108 | goto err;
|
---|
109 | }
|
---|
110 | }
|
---|
111 | #endif
|
---|
112 |
|
---|
113 | buflen = (EC_GROUP_get_degree(group) + 7) / 8;
|
---|
114 | len = BN_num_bytes(x);
|
---|
115 | if (len > buflen) {
|
---|
116 | ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_INTERNAL_ERROR);
|
---|
117 | goto err;
|
---|
118 | }
|
---|
119 | if ((buf = OPENSSL_malloc(buflen)) == NULL) {
|
---|
120 | ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);
|
---|
121 | goto err;
|
---|
122 | }
|
---|
123 |
|
---|
124 | memset(buf, 0, buflen - len);
|
---|
125 | if (len != (size_t)BN_bn2bin(x, buf + buflen - len)) {
|
---|
126 | ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_BN_LIB);
|
---|
127 | goto err;
|
---|
128 | }
|
---|
129 |
|
---|
130 | *pout = buf;
|
---|
131 | *poutlen = buflen;
|
---|
132 | buf = NULL;
|
---|
133 |
|
---|
134 | ret = 1;
|
---|
135 |
|
---|
136 | err:
|
---|
137 | EC_POINT_free(tmp);
|
---|
138 | if (ctx)
|
---|
139 | BN_CTX_end(ctx);
|
---|
140 | BN_CTX_free(ctx);
|
---|
141 | OPENSSL_free(buf);
|
---|
142 | return ret;
|
---|
143 | }
|
---|