/src/openssl/crypto/ec/ecdh_ossl.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2002-2018 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 | 0 | { |
25 | 0 | if (ecdh->group->meth->ecdh_compute_key == NULL) { |
26 | 0 | ECerr(EC_F_OSSL_ECDH_COMPUTE_KEY, EC_R_CURVE_DOES_NOT_SUPPORT_ECDH); |
27 | 0 | return 0; |
28 | 0 | } |
29 | 0 |
|
30 | 0 | return ecdh->group->meth->ecdh_compute_key(psec, pseclen, pub_key, ecdh); |
31 | 0 | } |
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 | 0 | { |
41 | 0 | BN_CTX *ctx; |
42 | 0 | EC_POINT *tmp = NULL; |
43 | 0 | BIGNUM *x = NULL; |
44 | 0 | const BIGNUM *priv_key; |
45 | 0 | const EC_GROUP *group; |
46 | 0 | int ret = 0; |
47 | 0 | size_t buflen, len; |
48 | 0 | unsigned char *buf = NULL; |
49 | 0 |
|
50 | 0 | if ((ctx = BN_CTX_new()) == NULL) |
51 | 0 | goto err; |
52 | 0 | BN_CTX_start(ctx); |
53 | 0 | x = BN_CTX_get(ctx); |
54 | 0 | if (x == NULL) { |
55 | 0 | ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_MALLOC_FAILURE); |
56 | 0 | goto err; |
57 | 0 | } |
58 | 0 |
|
59 | 0 | priv_key = EC_KEY_get0_private_key(ecdh); |
60 | 0 | if (priv_key == NULL) { |
61 | 0 | ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, EC_R_NO_PRIVATE_VALUE); |
62 | 0 | goto err; |
63 | 0 | } |
64 | 0 |
|
65 | 0 | group = EC_KEY_get0_group(ecdh); |
66 | 0 |
|
67 | 0 | if (EC_KEY_get_flags(ecdh) & EC_FLAG_COFACTOR_ECDH) { |
68 | 0 | if (!EC_GROUP_get_cofactor(group, x, NULL) || |
69 | 0 | !BN_mul(x, x, priv_key, ctx)) { |
70 | 0 | ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_MALLOC_FAILURE); |
71 | 0 | goto err; |
72 | 0 | } |
73 | 0 | priv_key = x; |
74 | 0 | } |
75 | 0 |
|
76 | 0 | if ((tmp = EC_POINT_new(group)) == NULL) { |
77 | 0 | ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_MALLOC_FAILURE); |
78 | 0 | goto err; |
79 | 0 | } |
80 | 0 |
|
81 | 0 | if (!EC_POINT_mul(group, tmp, NULL, pub_key, priv_key, ctx)) { |
82 | 0 | ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, EC_R_POINT_ARITHMETIC_FAILURE); |
83 | 0 | goto err; |
84 | 0 | } |
85 | 0 |
|
86 | 0 | if (!EC_POINT_get_affine_coordinates(group, tmp, x, NULL, ctx)) { |
87 | 0 | ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, EC_R_POINT_ARITHMETIC_FAILURE); |
88 | 0 | goto err; |
89 | 0 | } |
90 | 0 |
|
91 | 0 | buflen = (EC_GROUP_get_degree(group) + 7) / 8; |
92 | 0 | len = BN_num_bytes(x); |
93 | 0 | if (len > buflen) { |
94 | 0 | ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_INTERNAL_ERROR); |
95 | 0 | goto err; |
96 | 0 | } |
97 | 0 | if ((buf = OPENSSL_malloc(buflen)) == NULL) { |
98 | 0 | ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_MALLOC_FAILURE); |
99 | 0 | goto err; |
100 | 0 | } |
101 | 0 |
|
102 | 0 | memset(buf, 0, buflen - len); |
103 | 0 | if (len != (size_t)BN_bn2bin(x, buf + buflen - len)) { |
104 | 0 | ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_BN_LIB); |
105 | 0 | goto err; |
106 | 0 | } |
107 | 0 |
|
108 | 0 | *pout = buf; |
109 | 0 | *poutlen = buflen; |
110 | 0 | buf = NULL; |
111 | 0 |
|
112 | 0 | ret = 1; |
113 | 0 |
|
114 | 0 | err: |
115 | 0 | EC_POINT_free(tmp); |
116 | 0 | if (ctx) |
117 | 0 | BN_CTX_end(ctx); |
118 | 0 | BN_CTX_free(ctx); |
119 | 0 | OPENSSL_free(buf); |
120 | 0 | return ret; |
121 | 0 | } |