summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortb <>2024-10-30 18:14:49 +0000
committertb <>2024-10-30 18:14:49 +0000
commit8c30fb7d85e73088455141bfbd758b3a43c931df (patch)
tree42ef944d850db6a3b6bd05aa9dd8d263df4e921a
parent198c8abda2dbade2e5be1aebfc5d0c25d2944274 (diff)
downloadopenbsd-8c30fb7d85e73088455141bfbd758b3a43c931df.tar.gz
openbsd-8c30fb7d85e73088455141bfbd758b3a43c931df.tar.bz2
openbsd-8c30fb7d85e73088455141bfbd758b3a43c931df.zip
Move public point <-> octets API to a new ec_convert.c
discussed with jsing
-rw-r--r--src/lib/libcrypto/Makefile3
-rw-r--r--src/lib/libcrypto/ec/ec_convert.c199
-rw-r--r--src/lib/libcrypto/ec/ec_oct.c131
3 files changed, 202 insertions, 131 deletions
diff --git a/src/lib/libcrypto/Makefile b/src/lib/libcrypto/Makefile
index 6a1d125950..652d74ee92 100644
--- a/src/lib/libcrypto/Makefile
+++ b/src/lib/libcrypto/Makefile
@@ -1,4 +1,4 @@
1# $OpenBSD: Makefile,v 1.214 2024/10/19 08:26:03 tb Exp $ 1# $OpenBSD: Makefile,v 1.215 2024/10/30 18:14:49 tb Exp $
2 2
3LIB= crypto 3LIB= crypto
4LIBREBUILD=y 4LIBREBUILD=y
@@ -282,6 +282,7 @@ SRCS+= dsa_prn.c
282SRCS+= ec_ameth.c 282SRCS+= ec_ameth.c
283SRCS+= ec_asn1.c 283SRCS+= ec_asn1.c
284SRCS+= ec_curve.c 284SRCS+= ec_curve.c
285SRCS+= ec_convert.c
285SRCS+= ec_err.c 286SRCS+= ec_err.c
286SRCS+= ec_key.c 287SRCS+= ec_key.c
287SRCS+= ec_kmeth.c 288SRCS+= ec_kmeth.c
diff --git a/src/lib/libcrypto/ec/ec_convert.c b/src/lib/libcrypto/ec/ec_convert.c
new file mode 100644
index 0000000000..fd0182f420
--- /dev/null
+++ b/src/lib/libcrypto/ec/ec_convert.c
@@ -0,0 +1,199 @@
1/* $OpenBSD: ec_convert.c,v 1.1 2024/10/30 18:14:49 tb Exp $ */
2/*
3 * Originally written by Bodo Moeller for the OpenSSL project.
4 */
5/* ====================================================================
6 * Copyright (c) 1998-2003 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * openssl-core@openssl.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58/* ====================================================================
59 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
60 * Binary polynomial ECC support in OpenSSL originally developed by
61 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
62 */
63
64#include <string.h>
65
66#include <openssl/asn1.h>
67#include <openssl/err.h>
68
69#include "asn1_local.h"
70#include "ec_local.h"
71
72int
73ec_point_to_octets(const EC_GROUP *group, const EC_POINT *point, int form,
74 unsigned char **out_buf, size_t *out_len, BN_CTX *ctx)
75{
76 unsigned char *buf = NULL;
77 size_t len = 0;
78 int ret = 0;
79
80 if (out_buf != NULL && *out_buf != NULL)
81 goto err;
82
83 *out_len = 0;
84
85 if ((len = EC_POINT_point2oct(group, point, form, NULL, 0, ctx)) == 0)
86 goto err;
87
88 if (out_buf == NULL)
89 goto done;
90
91 if ((buf = calloc(1, len)) == NULL)
92 goto err;
93 if (EC_POINT_point2oct(group, point, form, buf, len, ctx) != len)
94 goto err;
95
96 *out_buf = buf;
97 buf = NULL;
98
99 done:
100 *out_len = len;
101
102 ret = 1;
103
104 err:
105 freezero(buf, len);
106
107 return ret;
108}
109
110int
111ec_point_from_octets(const EC_GROUP *group, const unsigned char *buf, size_t buf_len,
112 EC_POINT **out_point, uint8_t *out_form, BN_CTX *ctx)
113{
114 EC_POINT *point;
115 int ret = 0;
116
117 if ((point = *out_point) == NULL)
118 point = EC_POINT_new(group);
119 if (point == NULL)
120 goto err;
121
122 if (!EC_POINT_oct2point(group, point, buf, buf_len, ctx))
123 goto err;
124
125 if (out_form != NULL)
126 *out_form = buf[0] & ~1U; /* XXX - EC_OCT_YBIT */
127
128 *out_point = point;
129 point = NULL;
130
131 ret = 1;
132
133 err:
134 if (*out_point != point)
135 EC_POINT_free(point);
136
137 return ret;
138}
139
140size_t
141EC_POINT_point2oct(const EC_GROUP *group, const EC_POINT *point,
142 point_conversion_form_t form, unsigned char *buf, size_t len,
143 BN_CTX *ctx_in)
144{
145 BN_CTX *ctx;
146 size_t ret = 0;
147
148 if ((ctx = ctx_in) == NULL)
149 ctx = BN_CTX_new();
150 if (ctx == NULL)
151 goto err;
152
153 if (group->meth->point2oct == NULL) {
154 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
155 goto err;
156 }
157 if (group->meth != point->meth) {
158 ECerror(EC_R_INCOMPATIBLE_OBJECTS);
159 goto err;
160 }
161 ret = group->meth->point2oct(group, point, form, buf, len, ctx);
162
163 err:
164 if (ctx != ctx_in)
165 BN_CTX_free(ctx);
166
167 return ret;
168}
169LCRYPTO_ALIAS(EC_POINT_point2oct);
170
171int
172EC_POINT_oct2point(const EC_GROUP *group, EC_POINT *point,
173 const unsigned char *buf, size_t len, BN_CTX *ctx_in)
174{
175 BN_CTX *ctx;
176 int ret = 0;
177
178 if ((ctx = ctx_in) == NULL)
179 ctx = BN_CTX_new();
180 if (ctx == NULL)
181 goto err;
182
183 if (group->meth->oct2point == NULL) {
184 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
185 goto err;
186 }
187 if (group->meth != point->meth) {
188 ECerror(EC_R_INCOMPATIBLE_OBJECTS);
189 goto err;
190 }
191 ret = group->meth->oct2point(group, point, buf, len, ctx);
192
193 err:
194 if (ctx != ctx_in)
195 BN_CTX_free(ctx);
196
197 return ret;
198}
199LCRYPTO_ALIAS(EC_POINT_oct2point);
diff --git a/src/lib/libcrypto/ec/ec_oct.c b/src/lib/libcrypto/ec/ec_oct.c
index 3277bf4dd5..7eb7d51910 100644
--- a/src/lib/libcrypto/ec/ec_oct.c
+++ b/src/lib/libcrypto/ec/ec_oct.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: ec_oct.c,v 1.19 2024/10/30 17:52:34 tb Exp $ */ 1/* $OpenBSD: ec_oct.c,v 1.20 2024/10/30 18:14:49 tb Exp $ */
2/* 2/*
3 * Originally written by Bodo Moeller for the OpenSSL project. 3 * Originally written by Bodo Moeller for the OpenSSL project.
4 */ 4 */
@@ -110,132 +110,3 @@ EC_POINT_set_compressed_coordinates_GFp(const EC_GROUP *group, EC_POINT *point,
110 return EC_POINT_set_compressed_coordinates(group, point, x, y_bit, ctx); 110 return EC_POINT_set_compressed_coordinates(group, point, x, y_bit, ctx);
111} 111}
112LCRYPTO_ALIAS(EC_POINT_set_compressed_coordinates_GFp); 112LCRYPTO_ALIAS(EC_POINT_set_compressed_coordinates_GFp);
113
114int
115ec_point_to_octets(const EC_GROUP *group, const EC_POINT *point, int form,
116 unsigned char **out_buf, size_t *out_len, BN_CTX *ctx)
117{
118 unsigned char *buf = NULL;
119 size_t len = 0;
120 int ret = 0;
121
122 if (out_buf != NULL && *out_buf != NULL)
123 goto err;
124
125 *out_len = 0;
126
127 if ((len = EC_POINT_point2oct(group, point, form, NULL, 0, ctx)) == 0)
128 goto err;
129
130 if (out_buf == NULL)
131 goto done;
132
133 if ((buf = calloc(1, len)) == NULL)
134 goto err;
135 if (EC_POINT_point2oct(group, point, form, buf, len, ctx) != len)
136 goto err;
137
138 *out_buf = buf;
139 buf = NULL;
140
141 done:
142 *out_len = len;
143
144 ret = 1;
145
146 err:
147 freezero(buf, len);
148
149 return ret;
150}
151
152int
153ec_point_from_octets(const EC_GROUP *group, const unsigned char *buf, size_t buf_len,
154 EC_POINT **out_point, uint8_t *out_form, BN_CTX *ctx)
155{
156 EC_POINT *point;
157 int ret = 0;
158
159 if ((point = *out_point) == NULL)
160 point = EC_POINT_new(group);
161 if (point == NULL)
162 goto err;
163
164 if (!EC_POINT_oct2point(group, point, buf, buf_len, ctx))
165 goto err;
166
167 if (out_form != NULL)
168 *out_form = buf[0] & ~1U; /* XXX - EC_OCT_YBIT */
169
170 *out_point = point;
171 point = NULL;
172
173 ret = 1;
174
175 err:
176 if (*out_point != point)
177 EC_POINT_free(point);
178
179 return ret;
180}
181
182size_t
183EC_POINT_point2oct(const EC_GROUP *group, const EC_POINT *point,
184 point_conversion_form_t form, unsigned char *buf, size_t len,
185 BN_CTX *ctx_in)
186{
187 BN_CTX *ctx;
188 size_t ret = 0;
189
190 if ((ctx = ctx_in) == NULL)
191 ctx = BN_CTX_new();
192 if (ctx == NULL)
193 goto err;
194
195 if (group->meth->point2oct == NULL) {
196 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
197 goto err;
198 }
199 if (group->meth != point->meth) {
200 ECerror(EC_R_INCOMPATIBLE_OBJECTS);
201 goto err;
202 }
203 ret = group->meth->point2oct(group, point, form, buf, len, ctx);
204
205 err:
206 if (ctx != ctx_in)
207 BN_CTX_free(ctx);
208
209 return ret;
210}
211LCRYPTO_ALIAS(EC_POINT_point2oct);
212
213int
214EC_POINT_oct2point(const EC_GROUP *group, EC_POINT *point,
215 const unsigned char *buf, size_t len, BN_CTX *ctx_in)
216{
217 BN_CTX *ctx;
218 int ret = 0;
219
220 if ((ctx = ctx_in) == NULL)
221 ctx = BN_CTX_new();
222 if (ctx == NULL)
223 goto err;
224
225 if (group->meth->oct2point == NULL) {
226 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
227 goto err;
228 }
229 if (group->meth != point->meth) {
230 ECerror(EC_R_INCOMPATIBLE_OBJECTS);
231 goto err;
232 }
233 ret = group->meth->oct2point(group, point, buf, len, ctx);
234
235 err:
236 if (ctx != ctx_in)
237 BN_CTX_free(ctx);
238
239 return ret;
240}
241LCRYPTO_ALIAS(EC_POINT_oct2point);