diff options
Diffstat (limited to 'src/lib/libcrypto/ecdh/ecdh.c')
-rw-r--r-- | src/lib/libcrypto/ecdh/ecdh.c | 272 |
1 files changed, 272 insertions, 0 deletions
diff --git a/src/lib/libcrypto/ecdh/ecdh.c b/src/lib/libcrypto/ecdh/ecdh.c new file mode 100644 index 0000000000..d2de3a09af --- /dev/null +++ b/src/lib/libcrypto/ecdh/ecdh.c | |||
@@ -0,0 +1,272 @@ | |||
1 | /* $OpenBSD: ecdh.c,v 1.1 2023/07/05 12:31:14 tb Exp $ */ | ||
2 | /* ==================================================================== | ||
3 | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. | ||
4 | * | ||
5 | * The Elliptic Curve Public-Key Crypto Library (ECC Code) included | ||
6 | * herein is developed by SUN MICROSYSTEMS, INC., and is contributed | ||
7 | * to the OpenSSL project. | ||
8 | * | ||
9 | * The ECC Code is licensed pursuant to the OpenSSL open source | ||
10 | * license provided below. | ||
11 | * | ||
12 | * The ECDH software is originally written by Douglas Stebila of | ||
13 | * Sun Microsystems Laboratories. | ||
14 | * | ||
15 | */ | ||
16 | /* ==================================================================== | ||
17 | * Copyright (c) 1998-2003 The OpenSSL Project. All rights reserved. | ||
18 | * | ||
19 | * Redistribution and use in source and binary forms, with or without | ||
20 | * modification, are permitted provided that the following conditions | ||
21 | * are met: | ||
22 | * | ||
23 | * 1. Redistributions of source code must retain the above copyright | ||
24 | * notice, this list of conditions and the following disclaimer. | ||
25 | * | ||
26 | * 2. Redistributions in binary form must reproduce the above copyright | ||
27 | * notice, this list of conditions and the following disclaimer in | ||
28 | * the documentation and/or other materials provided with the | ||
29 | * distribution. | ||
30 | * | ||
31 | * 3. All advertising materials mentioning features or use of this | ||
32 | * software must display the following acknowledgment: | ||
33 | * "This product includes software developed by the OpenSSL Project | ||
34 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" | ||
35 | * | ||
36 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | ||
37 | * endorse or promote products derived from this software without | ||
38 | * prior written permission. For written permission, please contact | ||
39 | * openssl-core@OpenSSL.org. | ||
40 | * | ||
41 | * 5. Products derived from this software may not be called "OpenSSL" | ||
42 | * nor may "OpenSSL" appear in their names without prior written | ||
43 | * permission of the OpenSSL Project. | ||
44 | * | ||
45 | * 6. Redistributions of any form whatsoever must retain the following | ||
46 | * acknowledgment: | ||
47 | * "This product includes software developed by the OpenSSL Project | ||
48 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" | ||
49 | * | ||
50 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | ||
51 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
52 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
53 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | ||
54 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
55 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
56 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
57 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
58 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
59 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
60 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | ||
61 | * OF THE POSSIBILITY OF SUCH DAMAGE. | ||
62 | * ==================================================================== | ||
63 | * | ||
64 | * This product includes cryptographic software written by Eric Young | ||
65 | * (eay@cryptsoft.com). This product includes software written by Tim | ||
66 | * Hudson (tjh@cryptsoft.com). | ||
67 | * | ||
68 | */ | ||
69 | |||
70 | #include <limits.h> | ||
71 | #include <stdlib.h> | ||
72 | #include <string.h> | ||
73 | |||
74 | #include <openssl/opensslconf.h> | ||
75 | |||
76 | #include <openssl/bn.h> | ||
77 | #include <openssl/ec.h> | ||
78 | #include <openssl/ecdh.h> | ||
79 | #include <openssl/err.h> | ||
80 | #include <openssl/evp.h> | ||
81 | |||
82 | #include "ec_local.h" | ||
83 | |||
84 | /* | ||
85 | * Key derivation function from X9.63/SECG. | ||
86 | */ | ||
87 | |||
88 | /* Way more than we will ever need */ | ||
89 | #define ECDH_KDF_MAX (1 << 30) | ||
90 | |||
91 | int | ||
92 | ecdh_KDF_X9_63(unsigned char *out, size_t outlen, const unsigned char *Z, | ||
93 | size_t Zlen, const unsigned char *sinfo, size_t sinfolen, const EVP_MD *md) | ||
94 | { | ||
95 | EVP_MD_CTX *mctx = NULL; | ||
96 | unsigned int i; | ||
97 | size_t mdlen; | ||
98 | unsigned char ctr[4]; | ||
99 | int rv = 0; | ||
100 | |||
101 | if (sinfolen > ECDH_KDF_MAX || outlen > ECDH_KDF_MAX || | ||
102 | Zlen > ECDH_KDF_MAX) | ||
103 | return 0; | ||
104 | mctx = EVP_MD_CTX_new(); | ||
105 | if (mctx == NULL) | ||
106 | return 0; | ||
107 | mdlen = EVP_MD_size(md); | ||
108 | for (i = 1;; i++) { | ||
109 | unsigned char mtmp[EVP_MAX_MD_SIZE]; | ||
110 | if (!EVP_DigestInit_ex(mctx, md, NULL)) | ||
111 | goto err; | ||
112 | ctr[3] = i & 0xFF; | ||
113 | ctr[2] = (i >> 8) & 0xFF; | ||
114 | ctr[1] = (i >> 16) & 0xFF; | ||
115 | ctr[0] = (i >> 24) & 0xFF; | ||
116 | if (!EVP_DigestUpdate(mctx, Z, Zlen)) | ||
117 | goto err; | ||
118 | if (!EVP_DigestUpdate(mctx, ctr, sizeof(ctr))) | ||
119 | goto err; | ||
120 | if (!EVP_DigestUpdate(mctx, sinfo, sinfolen)) | ||
121 | goto err; | ||
122 | if (outlen >= mdlen) { | ||
123 | if (!EVP_DigestFinal(mctx, out, NULL)) | ||
124 | goto err; | ||
125 | outlen -= mdlen; | ||
126 | if (outlen == 0) | ||
127 | break; | ||
128 | out += mdlen; | ||
129 | } else { | ||
130 | if (!EVP_DigestFinal(mctx, mtmp, NULL)) | ||
131 | goto err; | ||
132 | memcpy(out, mtmp, outlen); | ||
133 | explicit_bzero(mtmp, mdlen); | ||
134 | break; | ||
135 | } | ||
136 | } | ||
137 | rv = 1; | ||
138 | |||
139 | err: | ||
140 | EVP_MD_CTX_free(mctx); | ||
141 | |||
142 | return rv; | ||
143 | } | ||
144 | |||
145 | /* | ||
146 | * Based on the ECKAS-DH1 and ECSVDP-DH primitives in the IEEE 1363 standard. | ||
147 | */ | ||
148 | /* XXX - KDF handling moved to ECDH_compute_key(). See OpenSSL e2285d87. */ | ||
149 | int | ||
150 | ecdh_compute_key(void *out, size_t outlen, const EC_POINT *pub_key, EC_KEY *ecdh, | ||
151 | void *(*KDF)(const void *in, size_t inlen, void *out, size_t *outlen)) | ||
152 | { | ||
153 | BN_CTX *ctx; | ||
154 | BIGNUM *cofactor, *x; | ||
155 | const BIGNUM *priv_key; | ||
156 | const EC_GROUP *group; | ||
157 | EC_POINT *point = NULL; | ||
158 | unsigned char *buf = NULL; | ||
159 | int buflen; | ||
160 | int ret = -1; | ||
161 | |||
162 | if (outlen > INT_MAX) { | ||
163 | /* Sort of, anyway. */ | ||
164 | ECDHerror(ERR_R_MALLOC_FAILURE); | ||
165 | return -1; | ||
166 | } | ||
167 | |||
168 | if ((ctx = BN_CTX_new()) == NULL) | ||
169 | goto err; | ||
170 | |||
171 | BN_CTX_start(ctx); | ||
172 | |||
173 | if ((x = BN_CTX_get(ctx)) == NULL) | ||
174 | goto err; | ||
175 | if ((cofactor = BN_CTX_get(ctx)) == NULL) | ||
176 | goto err; | ||
177 | |||
178 | if ((group = EC_KEY_get0_group(ecdh)) == NULL) | ||
179 | goto err; | ||
180 | |||
181 | if (!EC_POINT_is_on_curve(group, pub_key, ctx)) | ||
182 | goto err; | ||
183 | |||
184 | if ((point = EC_POINT_new(group)) == NULL) { | ||
185 | ECDHerror(ERR_R_MALLOC_FAILURE); | ||
186 | goto err; | ||
187 | } | ||
188 | |||
189 | if ((priv_key = EC_KEY_get0_private_key(ecdh)) == NULL) { | ||
190 | ECDHerror(ECDH_R_NO_PRIVATE_VALUE); | ||
191 | goto err; | ||
192 | } | ||
193 | |||
194 | if ((EC_KEY_get_flags(ecdh) & EC_FLAG_COFACTOR_ECDH) != 0) { | ||
195 | if (!EC_GROUP_get_cofactor(group, cofactor, NULL)) { | ||
196 | ECDHerror(ERR_R_EC_LIB); | ||
197 | goto err; | ||
198 | } | ||
199 | if (!BN_mul(cofactor, cofactor, priv_key, ctx)) { | ||
200 | ECDHerror(ERR_R_BN_LIB); | ||
201 | goto err; | ||
202 | } | ||
203 | priv_key = cofactor; | ||
204 | } | ||
205 | |||
206 | if (!EC_POINT_mul(group, point, NULL, pub_key, priv_key, ctx)) { | ||
207 | ECDHerror(ECDH_R_POINT_ARITHMETIC_FAILURE); | ||
208 | goto err; | ||
209 | } | ||
210 | |||
211 | if (!EC_POINT_get_affine_coordinates(group, point, x, NULL, ctx)) { | ||
212 | ECDHerror(ECDH_R_POINT_ARITHMETIC_FAILURE); | ||
213 | goto err; | ||
214 | } | ||
215 | |||
216 | if ((buflen = ECDH_size(ecdh)) < BN_num_bytes(x)) { | ||
217 | ECDHerror(ERR_R_INTERNAL_ERROR); | ||
218 | goto err; | ||
219 | } | ||
220 | if (KDF == NULL && outlen < buflen) { | ||
221 | /* The resulting key would be truncated. */ | ||
222 | ECDHerror(ECDH_R_KEY_TRUNCATION); | ||
223 | goto err; | ||
224 | } | ||
225 | if ((buf = malloc(buflen)) == NULL) { | ||
226 | ECDHerror(ERR_R_MALLOC_FAILURE); | ||
227 | goto err; | ||
228 | } | ||
229 | if (BN_bn2binpad(x, buf, buflen) != buflen) { | ||
230 | ECDHerror(ERR_R_BN_LIB); | ||
231 | goto err; | ||
232 | } | ||
233 | |||
234 | if (KDF != NULL) { | ||
235 | if (KDF(buf, buflen, out, &outlen) == NULL) { | ||
236 | ECDHerror(ECDH_R_KDF_FAILED); | ||
237 | goto err; | ||
238 | } | ||
239 | } else { | ||
240 | memset(out, 0, outlen); | ||
241 | if (outlen > buflen) | ||
242 | outlen = buflen; | ||
243 | memcpy(out, buf, outlen); | ||
244 | } | ||
245 | |||
246 | ret = outlen; | ||
247 | err: | ||
248 | EC_POINT_free(point); | ||
249 | BN_CTX_end(ctx); | ||
250 | BN_CTX_free(ctx); | ||
251 | free(buf); | ||
252 | |||
253 | return ret; | ||
254 | } | ||
255 | |||
256 | int | ||
257 | ECDH_compute_key(void *out, size_t outlen, const EC_POINT *pub_key, | ||
258 | EC_KEY *eckey, | ||
259 | void *(*KDF)(const void *in, size_t inlen, void *out, size_t *outlen)) | ||
260 | { | ||
261 | if (eckey->meth->compute_key == NULL) { | ||
262 | ECerror(EC_R_NOT_IMPLEMENTED); | ||
263 | return 0; | ||
264 | } | ||
265 | return eckey->meth->compute_key(out, outlen, pub_key, eckey, KDF); | ||
266 | } | ||
267 | |||
268 | int | ||
269 | ECDH_size(const EC_KEY *d) | ||
270 | { | ||
271 | return (EC_GROUP_get_degree(EC_KEY_get0_group(d)) + 7) / 8; | ||
272 | } | ||