]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/ec/ecdh_ossl.c
Modify Sun copyright to follow OpenSSL style
[thirdparty/openssl.git] / crypto / ec / ecdh_ossl.c
1 /*
2 * Copyright 2002-2016 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
4 *
5 * Licensed under the OpenSSL license (the "License"). You may not use
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
11 #include <string.h>
12 #include <limits.h>
13
14 #include "internal/cryptlib.h"
15
16 #include <openssl/err.h>
17 #include <openssl/bn.h>
18 #include <openssl/objects.h>
19 #include <openssl/ec.h>
20 #include "ec_lcl.h"
21
22 int ossl_ecdh_compute_key(unsigned char **psec, size_t *pseclen,
23 const EC_POINT *pub_key, const EC_KEY *ecdh)
24 {
25 if (ecdh->group->meth->ecdh_compute_key == NULL) {
26 ECerr(EC_F_OSSL_ECDH_COMPUTE_KEY, EC_R_CURVE_DOES_NOT_SUPPORT_ECDH);
27 return 0;
28 }
29
30 return ecdh->group->meth->ecdh_compute_key(psec, pseclen, pub_key, ecdh);
31 }
32
33 /*-
34 * This implementation is based on the following primitives in the IEEE 1363 standard:
35 * - ECKAS-DH1
36 * - ECSVDP-DH
37 */
38 int ecdh_simple_compute_key(unsigned char **pout, size_t *poutlen,
39 const EC_POINT *pub_key, const EC_KEY *ecdh)
40 {
41 BN_CTX *ctx;
42 EC_POINT *tmp = NULL;
43 BIGNUM *x = NULL, *y = NULL;
44 const BIGNUM *priv_key;
45 const EC_GROUP *group;
46 int ret = 0;
47 size_t buflen, len;
48 unsigned char *buf = NULL;
49
50 if ((ctx = BN_CTX_new()) == NULL)
51 goto err;
52 BN_CTX_start(ctx);
53 x = BN_CTX_get(ctx);
54 y = BN_CTX_get(ctx);
55 if (y == NULL) {
56 ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);
57 goto err;
58 }
59
60 priv_key = EC_KEY_get0_private_key(ecdh);
61 if (priv_key == NULL) {
62 ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, EC_R_NO_PRIVATE_VALUE);
63 goto err;
64 }
65
66 group = EC_KEY_get0_group(ecdh);
67
68 if (EC_KEY_get_flags(ecdh) & EC_FLAG_COFACTOR_ECDH) {
69 if (!EC_GROUP_get_cofactor(group, x, NULL) ||
70 !BN_mul(x, x, priv_key, ctx)) {
71 ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);
72 goto err;
73 }
74 priv_key = x;
75 }
76
77 if ((tmp = EC_POINT_new(group)) == NULL) {
78 ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);
79 goto err;
80 }
81
82 if (!EC_POINT_mul(group, tmp, NULL, pub_key, priv_key, ctx)) {
83 ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, EC_R_POINT_ARITHMETIC_FAILURE);
84 goto err;
85 }
86
87 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) ==
88 NID_X9_62_prime_field) {
89 if (!EC_POINT_get_affine_coordinates_GFp(group, tmp, x, y, ctx)) {
90 ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, EC_R_POINT_ARITHMETIC_FAILURE);
91 goto err;
92 }
93 }
94 #ifndef OPENSSL_NO_EC2M
95 else {
96 if (!EC_POINT_get_affine_coordinates_GF2m(group, tmp, x, y, ctx)) {
97 ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, EC_R_POINT_ARITHMETIC_FAILURE);
98 goto err;
99 }
100 }
101 #endif
102
103 buflen = (EC_GROUP_get_degree(group) + 7) / 8;
104 len = BN_num_bytes(x);
105 if (len > buflen) {
106 ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_INTERNAL_ERROR);
107 goto err;
108 }
109 if ((buf = OPENSSL_malloc(buflen)) == NULL) {
110 ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);
111 goto err;
112 }
113
114 memset(buf, 0, buflen - len);
115 if (len != (size_t)BN_bn2bin(x, buf + buflen - len)) {
116 ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_BN_LIB);
117 goto err;
118 }
119
120 *pout = buf;
121 *poutlen = buflen;
122 buf = NULL;
123
124 ret = 1;
125
126 err:
127 EC_POINT_free(tmp);
128 if (ctx)
129 BN_CTX_end(ctx);
130 BN_CTX_free(ctx);
131 OPENSSL_free(buf);
132 return ret;
133 }