summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/ecdh/ech_key.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/ecdh/ech_key.c')
-rw-r--r--src/lib/libcrypto/ecdh/ech_key.c210
1 files changed, 0 insertions, 210 deletions
diff --git a/src/lib/libcrypto/ecdh/ech_key.c b/src/lib/libcrypto/ecdh/ech_key.c
deleted file mode 100644
index 5efb49ba59..0000000000
--- a/src/lib/libcrypto/ecdh/ech_key.c
+++ /dev/null
@@ -1,210 +0,0 @@
1/* $OpenBSD: ech_key.c,v 1.33 2023/07/05 08:39:40 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
81#include "ec_local.h"
82
83/*
84 * Based on the ECKAS-DH1 and ECSVDP-DH primitives in the IEEE 1363 standard.
85 */
86/* XXX - KDF handling moved to ECDH_compute_key(). See OpenSSL e2285d87. */
87int
88ecdh_compute_key(void *out, size_t outlen, const EC_POINT *pub_key, EC_KEY *ecdh,
89 void *(*KDF)(const void *in, size_t inlen, void *out, size_t *outlen))
90{
91 BN_CTX *ctx;
92 BIGNUM *cofactor, *x;
93 const BIGNUM *priv_key;
94 const EC_GROUP *group;
95 EC_POINT *point = NULL;
96 unsigned char *buf = NULL;
97 int buflen;
98 int ret = -1;
99
100 if (outlen > INT_MAX) {
101 /* Sort of, anyway. */
102 ECDHerror(ERR_R_MALLOC_FAILURE);
103 return -1;
104 }
105
106 if ((ctx = BN_CTX_new()) == NULL)
107 goto err;
108
109 BN_CTX_start(ctx);
110
111 if ((x = BN_CTX_get(ctx)) == NULL)
112 goto err;
113 if ((cofactor = BN_CTX_get(ctx)) == NULL)
114 goto err;
115
116 if ((group = EC_KEY_get0_group(ecdh)) == NULL)
117 goto err;
118
119 if (!EC_POINT_is_on_curve(group, pub_key, ctx))
120 goto err;
121
122 if ((point = EC_POINT_new(group)) == NULL) {
123 ECDHerror(ERR_R_MALLOC_FAILURE);
124 goto err;
125 }
126
127 if ((priv_key = EC_KEY_get0_private_key(ecdh)) == NULL) {
128 ECDHerror(ECDH_R_NO_PRIVATE_VALUE);
129 goto err;
130 }
131
132 if ((EC_KEY_get_flags(ecdh) & EC_FLAG_COFACTOR_ECDH) != 0) {
133 if (!EC_GROUP_get_cofactor(group, cofactor, NULL)) {
134 ECDHerror(ERR_R_EC_LIB);
135 goto err;
136 }
137 if (!BN_mul(cofactor, cofactor, priv_key, ctx)) {
138 ECDHerror(ERR_R_BN_LIB);
139 goto err;
140 }
141 priv_key = cofactor;
142 }
143
144 if (!EC_POINT_mul(group, point, NULL, pub_key, priv_key, ctx)) {
145 ECDHerror(ECDH_R_POINT_ARITHMETIC_FAILURE);
146 goto err;
147 }
148
149 if (!EC_POINT_get_affine_coordinates(group, point, x, NULL, ctx)) {
150 ECDHerror(ECDH_R_POINT_ARITHMETIC_FAILURE);
151 goto err;
152 }
153
154 if ((buflen = ECDH_size(ecdh)) < BN_num_bytes(x)) {
155 ECDHerror(ERR_R_INTERNAL_ERROR);
156 goto err;
157 }
158 if (KDF == NULL && outlen < buflen) {
159 /* The resulting key would be truncated. */
160 ECDHerror(ECDH_R_KEY_TRUNCATION);
161 goto err;
162 }
163 if ((buf = malloc(buflen)) == NULL) {
164 ECDHerror(ERR_R_MALLOC_FAILURE);
165 goto err;
166 }
167 if (BN_bn2binpad(x, buf, buflen) != buflen) {
168 ECDHerror(ERR_R_BN_LIB);
169 goto err;
170 }
171
172 if (KDF != NULL) {
173 if (KDF(buf, buflen, out, &outlen) == NULL) {
174 ECDHerror(ECDH_R_KDF_FAILED);
175 goto err;
176 }
177 } else {
178 memset(out, 0, outlen);
179 if (outlen > buflen)
180 outlen = buflen;
181 memcpy(out, buf, outlen);
182 }
183
184 ret = outlen;
185 err:
186 EC_POINT_free(point);
187 BN_CTX_end(ctx);
188 BN_CTX_free(ctx);
189 free(buf);
190
191 return ret;
192}
193
194int
195ECDH_compute_key(void *out, size_t outlen, const EC_POINT *pub_key,
196 EC_KEY *eckey,
197 void *(*KDF)(const void *in, size_t inlen, void *out, size_t *outlen))
198{
199 if (eckey->meth->compute_key == NULL) {
200 ECerror(EC_R_NOT_IMPLEMENTED);
201 return 0;
202 }
203 return eckey->meth->compute_key(out, outlen, pub_key, eckey, KDF);
204}
205
206int
207ECDH_size(const EC_KEY *d)
208{
209 return (EC_GROUP_get_degree(EC_KEY_get0_group(d)) + 7) / 8;
210}