diff options
author | tb <> | 2024-10-30 18:14:49 +0000 |
---|---|---|
committer | tb <> | 2024-10-30 18:14:49 +0000 |
commit | 8c30fb7d85e73088455141bfbd758b3a43c931df (patch) | |
tree | 42ef944d850db6a3b6bd05aa9dd8d263df4e921a /src/lib/libcrypto/ec/ec_oct.c | |
parent | 198c8abda2dbade2e5be1aebfc5d0c25d2944274 (diff) | |
download | openbsd-8c30fb7d85e73088455141bfbd758b3a43c931df.tar.gz openbsd-8c30fb7d85e73088455141bfbd758b3a43c931df.tar.bz2 openbsd-8c30fb7d85e73088455141bfbd758b3a43c931df.zip |
Move public point <-> octets API to a new ec_convert.c
discussed with jsing
Diffstat (limited to 'src/lib/libcrypto/ec/ec_oct.c')
-rw-r--r-- | src/lib/libcrypto/ec/ec_oct.c | 131 |
1 files changed, 1 insertions, 130 deletions
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 | } |
112 | LCRYPTO_ALIAS(EC_POINT_set_compressed_coordinates_GFp); | 112 | LCRYPTO_ALIAS(EC_POINT_set_compressed_coordinates_GFp); |
113 | |||
114 | int | ||
115 | ec_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 | |||
152 | int | ||
153 | ec_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 | |||
182 | size_t | ||
183 | EC_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 | } | ||
211 | LCRYPTO_ALIAS(EC_POINT_point2oct); | ||
212 | |||
213 | int | ||
214 | EC_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 | } | ||
241 | LCRYPTO_ALIAS(EC_POINT_oct2point); | ||