diff options
author | tedu <> | 2014-05-06 03:56:27 +0000 |
---|---|---|
committer | tedu <> | 2014-05-06 03:56:27 +0000 |
commit | 2518b24aa4315d557b967bff48dfc9efed909569 (patch) | |
tree | ce2ee4fdddbbe61dd0ccb045a1604a3d92a86a00 /src/lib/libcrypto/ec/ec_key.c | |
parent | 0539604f5771dae2c3ecffa8122b5651ff283719 (diff) | |
download | openbsd-2518b24aa4315d557b967bff48dfc9efed909569.tar.gz openbsd-2518b24aa4315d557b967bff48dfc9efed909569.tar.bz2 openbsd-2518b24aa4315d557b967bff48dfc9efed909569.zip |
knf approximation
Diffstat (limited to '')
-rw-r--r-- | src/lib/libcrypto/ec/ec_key.c | 434 |
1 files changed, 219 insertions, 215 deletions
diff --git a/src/lib/libcrypto/ec/ec_key.c b/src/lib/libcrypto/ec/ec_key.c index 4f0559591e..1d727ec77d 100644 --- a/src/lib/libcrypto/ec/ec_key.c +++ b/src/lib/libcrypto/ec/ec_key.c | |||
@@ -10,7 +10,7 @@ | |||
10 | * are met: | 10 | * are met: |
11 | * | 11 | * |
12 | * 1. Redistributions of source code must retain the above copyright | 12 | * 1. Redistributions of source code must retain the above copyright |
13 | * notice, this list of conditions and the following disclaimer. | 13 | * notice, this list of conditions and the following disclaimer. |
14 | * | 14 | * |
15 | * 2. Redistributions in binary form must reproduce the above copyright | 15 | * 2. Redistributions in binary form must reproduce the above copyright |
16 | * notice, this list of conditions and the following disclaimer in | 16 | * notice, this list of conditions and the following disclaimer in |
@@ -57,7 +57,7 @@ | |||
57 | */ | 57 | */ |
58 | /* ==================================================================== | 58 | /* ==================================================================== |
59 | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. | 59 | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. |
60 | * Portions originally developed by SUN MICROSYSTEMS, INC., and | 60 | * Portions originally developed by SUN MICROSYSTEMS, INC., and |
61 | * contributed to the OpenSSL project. | 61 | * contributed to the OpenSSL project. |
62 | */ | 62 | */ |
63 | 63 | ||
@@ -65,78 +65,79 @@ | |||
65 | #include "ec_lcl.h" | 65 | #include "ec_lcl.h" |
66 | #include <openssl/err.h> | 66 | #include <openssl/err.h> |
67 | 67 | ||
68 | EC_KEY *EC_KEY_new(void) | 68 | EC_KEY * |
69 | { | 69 | EC_KEY_new(void) |
70 | { | ||
70 | EC_KEY *ret; | 71 | EC_KEY *ret; |
71 | 72 | ||
72 | ret=(EC_KEY *)malloc(sizeof(EC_KEY)); | 73 | ret = (EC_KEY *) malloc(sizeof(EC_KEY)); |
73 | if (ret == NULL) | 74 | if (ret == NULL) { |
74 | { | ||
75 | ECerr(EC_F_EC_KEY_NEW, ERR_R_MALLOC_FAILURE); | 75 | ECerr(EC_F_EC_KEY_NEW, ERR_R_MALLOC_FAILURE); |
76 | return(NULL); | 76 | return (NULL); |
77 | } | 77 | } |
78 | 78 | ret->version = 1; | |
79 | ret->version = 1; | ||
80 | ret->flags = 0; | 79 | ret->flags = 0; |
81 | ret->group = NULL; | 80 | ret->group = NULL; |
82 | ret->pub_key = NULL; | 81 | ret->pub_key = NULL; |
83 | ret->priv_key= NULL; | 82 | ret->priv_key = NULL; |
84 | ret->enc_flag= 0; | 83 | ret->enc_flag = 0; |
85 | ret->conv_form = POINT_CONVERSION_UNCOMPRESSED; | 84 | ret->conv_form = POINT_CONVERSION_UNCOMPRESSED; |
86 | ret->references= 1; | 85 | ret->references = 1; |
87 | ret->method_data = NULL; | 86 | ret->method_data = NULL; |
88 | return(ret); | 87 | return (ret); |
89 | } | 88 | } |
90 | 89 | ||
91 | EC_KEY *EC_KEY_new_by_curve_name(int nid) | 90 | EC_KEY * |
92 | { | 91 | EC_KEY_new_by_curve_name(int nid) |
92 | { | ||
93 | EC_KEY *ret = EC_KEY_new(); | 93 | EC_KEY *ret = EC_KEY_new(); |
94 | if (ret == NULL) | 94 | if (ret == NULL) |
95 | return NULL; | 95 | return NULL; |
96 | ret->group = EC_GROUP_new_by_curve_name(nid); | 96 | ret->group = EC_GROUP_new_by_curve_name(nid); |
97 | if (ret->group == NULL) | 97 | if (ret->group == NULL) { |
98 | { | ||
99 | EC_KEY_free(ret); | 98 | EC_KEY_free(ret); |
100 | return NULL; | 99 | return NULL; |
101 | } | ||
102 | return ret; | ||
103 | } | 100 | } |
101 | return ret; | ||
102 | } | ||
104 | 103 | ||
105 | void EC_KEY_free(EC_KEY *r) | 104 | void |
106 | { | 105 | EC_KEY_free(EC_KEY * r) |
106 | { | ||
107 | int i; | 107 | int i; |
108 | 108 | ||
109 | if (r == NULL) return; | 109 | if (r == NULL) |
110 | return; | ||
110 | 111 | ||
111 | i=CRYPTO_add(&r->references,-1,CRYPTO_LOCK_EC); | 112 | i = CRYPTO_add(&r->references, -1, CRYPTO_LOCK_EC); |
112 | if (i > 0) return; | 113 | if (i > 0) |
114 | return; | ||
113 | 115 | ||
114 | if (r->group != NULL) | 116 | if (r->group != NULL) |
115 | EC_GROUP_free(r->group); | 117 | EC_GROUP_free(r->group); |
116 | if (r->pub_key != NULL) | 118 | if (r->pub_key != NULL) |
117 | EC_POINT_free(r->pub_key); | 119 | EC_POINT_free(r->pub_key); |
118 | if (r->priv_key != NULL) | 120 | if (r->priv_key != NULL) |
119 | BN_clear_free(r->priv_key); | 121 | BN_clear_free(r->priv_key); |
120 | 122 | ||
121 | EC_EX_DATA_free_all_data(&r->method_data); | 123 | EC_EX_DATA_free_all_data(&r->method_data); |
122 | 124 | ||
123 | OPENSSL_cleanse((void *)r, sizeof(EC_KEY)); | 125 | OPENSSL_cleanse((void *) r, sizeof(EC_KEY)); |
124 | 126 | ||
125 | free(r); | 127 | free(r); |
126 | } | 128 | } |
127 | 129 | ||
128 | EC_KEY *EC_KEY_copy(EC_KEY *dest, const EC_KEY *src) | 130 | EC_KEY * |
129 | { | 131 | EC_KEY_copy(EC_KEY * dest, const EC_KEY * src) |
132 | { | ||
130 | EC_EXTRA_DATA *d; | 133 | EC_EXTRA_DATA *d; |
131 | 134 | ||
132 | if (dest == NULL || src == NULL) | 135 | if (dest == NULL || src == NULL) { |
133 | { | ||
134 | ECerr(EC_F_EC_KEY_COPY, ERR_R_PASSED_NULL_PARAMETER); | 136 | ECerr(EC_F_EC_KEY_COPY, ERR_R_PASSED_NULL_PARAMETER); |
135 | return NULL; | 137 | return NULL; |
136 | } | 138 | } |
137 | /* copy the parameters */ | 139 | /* copy the parameters */ |
138 | if (src->group) | 140 | if (src->group) { |
139 | { | ||
140 | const EC_METHOD *meth = EC_GROUP_method_of(src->group); | 141 | const EC_METHOD *meth = EC_GROUP_method_of(src->group); |
141 | /* clear the old group */ | 142 | /* clear the old group */ |
142 | if (dest->group) | 143 | if (dest->group) |
@@ -146,10 +147,9 @@ EC_KEY *EC_KEY_copy(EC_KEY *dest, const EC_KEY *src) | |||
146 | return NULL; | 147 | return NULL; |
147 | if (!EC_GROUP_copy(dest->group, src->group)) | 148 | if (!EC_GROUP_copy(dest->group, src->group)) |
148 | return NULL; | 149 | return NULL; |
149 | } | 150 | } |
150 | /* copy the public key */ | 151 | /* copy the public key */ |
151 | if (src->pub_key && src->group) | 152 | if (src->pub_key && src->group) { |
152 | { | ||
153 | if (dest->pub_key) | 153 | if (dest->pub_key) |
154 | EC_POINT_free(dest->pub_key); | 154 | EC_POINT_free(dest->pub_key); |
155 | dest->pub_key = EC_POINT_new(src->group); | 155 | dest->pub_key = EC_POINT_new(src->group); |
@@ -157,83 +157,81 @@ EC_KEY *EC_KEY_copy(EC_KEY *dest, const EC_KEY *src) | |||
157 | return NULL; | 157 | return NULL; |
158 | if (!EC_POINT_copy(dest->pub_key, src->pub_key)) | 158 | if (!EC_POINT_copy(dest->pub_key, src->pub_key)) |
159 | return NULL; | 159 | return NULL; |
160 | } | 160 | } |
161 | /* copy the private key */ | 161 | /* copy the private key */ |
162 | if (src->priv_key) | 162 | if (src->priv_key) { |
163 | { | 163 | if (dest->priv_key == NULL) { |
164 | if (dest->priv_key == NULL) | ||
165 | { | ||
166 | dest->priv_key = BN_new(); | 164 | dest->priv_key = BN_new(); |
167 | if (dest->priv_key == NULL) | 165 | if (dest->priv_key == NULL) |
168 | return NULL; | 166 | return NULL; |
169 | } | 167 | } |
170 | if (!BN_copy(dest->priv_key, src->priv_key)) | 168 | if (!BN_copy(dest->priv_key, src->priv_key)) |
171 | return NULL; | 169 | return NULL; |
172 | } | 170 | } |
173 | /* copy method/extra data */ | 171 | /* copy method/extra data */ |
174 | EC_EX_DATA_free_all_data(&dest->method_data); | 172 | EC_EX_DATA_free_all_data(&dest->method_data); |
175 | 173 | ||
176 | for (d = src->method_data; d != NULL; d = d->next) | 174 | for (d = src->method_data; d != NULL; d = d->next) { |
177 | { | ||
178 | void *t = d->dup_func(d->data); | 175 | void *t = d->dup_func(d->data); |
179 | 176 | ||
180 | if (t == NULL) | 177 | if (t == NULL) |
181 | return 0; | 178 | return 0; |
182 | if (!EC_EX_DATA_set_data(&dest->method_data, t, d->dup_func, d->free_func, d->clear_free_func)) | 179 | if (!EC_EX_DATA_set_data(&dest->method_data, t, d->dup_func, |
180 | d->free_func, d->clear_free_func)) | ||
183 | return 0; | 181 | return 0; |
184 | } | 182 | } |
185 | 183 | ||
186 | /* copy the rest */ | 184 | /* copy the rest */ |
187 | dest->enc_flag = src->enc_flag; | 185 | dest->enc_flag = src->enc_flag; |
188 | dest->conv_form = src->conv_form; | 186 | dest->conv_form = src->conv_form; |
189 | dest->version = src->version; | 187 | dest->version = src->version; |
190 | dest->flags = src->flags; | 188 | dest->flags = src->flags; |
191 | 189 | ||
192 | return dest; | 190 | return dest; |
193 | } | 191 | } |
194 | 192 | ||
195 | EC_KEY *EC_KEY_dup(const EC_KEY *ec_key) | 193 | EC_KEY * |
196 | { | 194 | EC_KEY_dup(const EC_KEY * ec_key) |
195 | { | ||
197 | EC_KEY *ret = EC_KEY_new(); | 196 | EC_KEY *ret = EC_KEY_new(); |
198 | if (ret == NULL) | 197 | if (ret == NULL) |
199 | return NULL; | 198 | return NULL; |
200 | if (EC_KEY_copy(ret, ec_key) == NULL) | 199 | if (EC_KEY_copy(ret, ec_key) == NULL) { |
201 | { | ||
202 | EC_KEY_free(ret); | 200 | EC_KEY_free(ret); |
203 | return NULL; | 201 | return NULL; |
204 | } | ||
205 | return ret; | ||
206 | } | 202 | } |
203 | return ret; | ||
204 | } | ||
207 | 205 | ||
208 | int EC_KEY_up_ref(EC_KEY *r) | 206 | int |
209 | { | 207 | EC_KEY_up_ref(EC_KEY * r) |
208 | { | ||
210 | int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_EC); | 209 | int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_EC); |
211 | return ((i > 1) ? 1 : 0); | 210 | return ((i > 1) ? 1 : 0); |
212 | } | 211 | } |
213 | 212 | ||
214 | int EC_KEY_generate_key(EC_KEY *eckey) | 213 | int |
215 | { | 214 | EC_KEY_generate_key(EC_KEY * eckey) |
216 | int ok = 0; | 215 | { |
217 | BN_CTX *ctx = NULL; | 216 | int ok = 0; |
218 | BIGNUM *priv_key = NULL, *order = NULL; | 217 | BN_CTX *ctx = NULL; |
218 | BIGNUM *priv_key = NULL, *order = NULL; | ||
219 | EC_POINT *pub_key = NULL; | 219 | EC_POINT *pub_key = NULL; |
220 | 220 | ||
221 | if (!eckey || !eckey->group) | 221 | if (!eckey || !eckey->group) { |
222 | { | ||
223 | ECerr(EC_F_EC_KEY_GENERATE_KEY, ERR_R_PASSED_NULL_PARAMETER); | 222 | ECerr(EC_F_EC_KEY_GENERATE_KEY, ERR_R_PASSED_NULL_PARAMETER); |
224 | return 0; | 223 | return 0; |
225 | } | 224 | } |
226 | 225 | if ((order = BN_new()) == NULL) | |
227 | if ((order = BN_new()) == NULL) goto err; | 226 | goto err; |
228 | if ((ctx = BN_CTX_new()) == NULL) goto err; | 227 | if ((ctx = BN_CTX_new()) == NULL) |
228 | goto err; | ||
229 | 229 | ||
230 | if (eckey->priv_key == NULL) | 230 | if (eckey->priv_key == NULL) { |
231 | { | ||
232 | priv_key = BN_new(); | 231 | priv_key = BN_new(); |
233 | if (priv_key == NULL) | 232 | if (priv_key == NULL) |
234 | goto err; | 233 | goto err; |
235 | } | 234 | } else |
236 | else | ||
237 | priv_key = eckey->priv_key; | 235 | priv_key = eckey->priv_key; |
238 | 236 | ||
239 | if (!EC_GROUP_get_order(eckey->group, order, ctx)) | 237 | if (!EC_GROUP_get_order(eckey->group, order, ctx)) |
@@ -244,127 +242,115 @@ int EC_KEY_generate_key(EC_KEY *eckey) | |||
244 | goto err; | 242 | goto err; |
245 | while (BN_is_zero(priv_key)); | 243 | while (BN_is_zero(priv_key)); |
246 | 244 | ||
247 | if (eckey->pub_key == NULL) | 245 | if (eckey->pub_key == NULL) { |
248 | { | ||
249 | pub_key = EC_POINT_new(eckey->group); | 246 | pub_key = EC_POINT_new(eckey->group); |
250 | if (pub_key == NULL) | 247 | if (pub_key == NULL) |
251 | goto err; | 248 | goto err; |
252 | } | 249 | } else |
253 | else | ||
254 | pub_key = eckey->pub_key; | 250 | pub_key = eckey->pub_key; |
255 | 251 | ||
256 | if (!EC_POINT_mul(eckey->group, pub_key, priv_key, NULL, NULL, ctx)) | 252 | if (!EC_POINT_mul(eckey->group, pub_key, priv_key, NULL, NULL, ctx)) |
257 | goto err; | 253 | goto err; |
258 | 254 | ||
259 | eckey->priv_key = priv_key; | 255 | eckey->priv_key = priv_key; |
260 | eckey->pub_key = pub_key; | 256 | eckey->pub_key = pub_key; |
261 | 257 | ||
262 | ok=1; | 258 | ok = 1; |
263 | 259 | ||
264 | err: | 260 | err: |
265 | if (order) | 261 | if (order) |
266 | BN_free(order); | 262 | BN_free(order); |
267 | if (pub_key != NULL && eckey->pub_key == NULL) | 263 | if (pub_key != NULL && eckey->pub_key == NULL) |
268 | EC_POINT_free(pub_key); | 264 | EC_POINT_free(pub_key); |
269 | if (priv_key != NULL && eckey->priv_key == NULL) | 265 | if (priv_key != NULL && eckey->priv_key == NULL) |
270 | BN_free(priv_key); | 266 | BN_free(priv_key); |
271 | if (ctx != NULL) | 267 | if (ctx != NULL) |
272 | BN_CTX_free(ctx); | 268 | BN_CTX_free(ctx); |
273 | return(ok); | 269 | return (ok); |
274 | } | 270 | } |
275 | 271 | ||
276 | int EC_KEY_check_key(const EC_KEY *eckey) | 272 | int |
277 | { | 273 | EC_KEY_check_key(const EC_KEY * eckey) |
278 | int ok = 0; | 274 | { |
279 | BN_CTX *ctx = NULL; | 275 | int ok = 0; |
280 | const BIGNUM *order = NULL; | 276 | BN_CTX *ctx = NULL; |
277 | const BIGNUM *order = NULL; | ||
281 | EC_POINT *point = NULL; | 278 | EC_POINT *point = NULL; |
282 | 279 | ||
283 | if (!eckey || !eckey->group || !eckey->pub_key) | 280 | if (!eckey || !eckey->group || !eckey->pub_key) { |
284 | { | ||
285 | ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_PASSED_NULL_PARAMETER); | 281 | ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_PASSED_NULL_PARAMETER); |
286 | return 0; | 282 | return 0; |
287 | } | 283 | } |
288 | 284 | if (EC_POINT_is_at_infinity(eckey->group, eckey->pub_key)) { | |
289 | if (EC_POINT_is_at_infinity(eckey->group, eckey->pub_key)) | ||
290 | { | ||
291 | ECerr(EC_F_EC_KEY_CHECK_KEY, EC_R_POINT_AT_INFINITY); | 285 | ECerr(EC_F_EC_KEY_CHECK_KEY, EC_R_POINT_AT_INFINITY); |
292 | goto err; | 286 | goto err; |
293 | } | 287 | } |
294 | |||
295 | if ((ctx = BN_CTX_new()) == NULL) | 288 | if ((ctx = BN_CTX_new()) == NULL) |
296 | goto err; | 289 | goto err; |
297 | if ((point = EC_POINT_new(eckey->group)) == NULL) | 290 | if ((point = EC_POINT_new(eckey->group)) == NULL) |
298 | goto err; | 291 | goto err; |
299 | 292 | ||
300 | /* testing whether the pub_key is on the elliptic curve */ | 293 | /* testing whether the pub_key is on the elliptic curve */ |
301 | if (!EC_POINT_is_on_curve(eckey->group, eckey->pub_key, ctx)) | 294 | if (!EC_POINT_is_on_curve(eckey->group, eckey->pub_key, ctx)) { |
302 | { | ||
303 | ECerr(EC_F_EC_KEY_CHECK_KEY, EC_R_POINT_IS_NOT_ON_CURVE); | 295 | ECerr(EC_F_EC_KEY_CHECK_KEY, EC_R_POINT_IS_NOT_ON_CURVE); |
304 | goto err; | 296 | goto err; |
305 | } | 297 | } |
306 | /* testing whether pub_key * order is the point at infinity */ | 298 | /* testing whether pub_key * order is the point at infinity */ |
307 | order = &eckey->group->order; | 299 | order = &eckey->group->order; |
308 | if (BN_is_zero(order)) | 300 | if (BN_is_zero(order)) { |
309 | { | ||
310 | ECerr(EC_F_EC_KEY_CHECK_KEY, EC_R_INVALID_GROUP_ORDER); | 301 | ECerr(EC_F_EC_KEY_CHECK_KEY, EC_R_INVALID_GROUP_ORDER); |
311 | goto err; | 302 | goto err; |
312 | } | 303 | } |
313 | if (!EC_POINT_mul(eckey->group, point, NULL, eckey->pub_key, order, ctx)) | 304 | if (!EC_POINT_mul(eckey->group, point, NULL, eckey->pub_key, order, ctx)) { |
314 | { | ||
315 | ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_EC_LIB); | 305 | ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_EC_LIB); |
316 | goto err; | 306 | goto err; |
317 | } | 307 | } |
318 | if (!EC_POINT_is_at_infinity(eckey->group, point)) | 308 | if (!EC_POINT_is_at_infinity(eckey->group, point)) { |
319 | { | ||
320 | ECerr(EC_F_EC_KEY_CHECK_KEY, EC_R_WRONG_ORDER); | 309 | ECerr(EC_F_EC_KEY_CHECK_KEY, EC_R_WRONG_ORDER); |
321 | goto err; | 310 | goto err; |
322 | } | 311 | } |
323 | /* in case the priv_key is present : | 312 | /* |
324 | * check if generator * priv_key == pub_key | 313 | * in case the priv_key is present : check if generator * priv_key == |
314 | * pub_key | ||
325 | */ | 315 | */ |
326 | if (eckey->priv_key) | 316 | if (eckey->priv_key) { |
327 | { | 317 | if (BN_cmp(eckey->priv_key, order) >= 0) { |
328 | if (BN_cmp(eckey->priv_key, order) >= 0) | ||
329 | { | ||
330 | ECerr(EC_F_EC_KEY_CHECK_KEY, EC_R_WRONG_ORDER); | 318 | ECerr(EC_F_EC_KEY_CHECK_KEY, EC_R_WRONG_ORDER); |
331 | goto err; | 319 | goto err; |
332 | } | 320 | } |
333 | if (!EC_POINT_mul(eckey->group, point, eckey->priv_key, | 321 | if (!EC_POINT_mul(eckey->group, point, eckey->priv_key, |
334 | NULL, NULL, ctx)) | 322 | NULL, NULL, ctx)) { |
335 | { | ||
336 | ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_EC_LIB); | 323 | ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_EC_LIB); |
337 | goto err; | 324 | goto err; |
338 | } | 325 | } |
339 | if (EC_POINT_cmp(eckey->group, point, eckey->pub_key, | 326 | if (EC_POINT_cmp(eckey->group, point, eckey->pub_key, |
340 | ctx) != 0) | 327 | ctx) != 0) { |
341 | { | ||
342 | ECerr(EC_F_EC_KEY_CHECK_KEY, EC_R_INVALID_PRIVATE_KEY); | 328 | ECerr(EC_F_EC_KEY_CHECK_KEY, EC_R_INVALID_PRIVATE_KEY); |
343 | goto err; | 329 | goto err; |
344 | } | ||
345 | } | 330 | } |
331 | } | ||
346 | ok = 1; | 332 | ok = 1; |
347 | err: | 333 | err: |
348 | if (ctx != NULL) | 334 | if (ctx != NULL) |
349 | BN_CTX_free(ctx); | 335 | BN_CTX_free(ctx); |
350 | if (point != NULL) | 336 | if (point != NULL) |
351 | EC_POINT_free(point); | 337 | EC_POINT_free(point); |
352 | return(ok); | 338 | return (ok); |
353 | } | 339 | } |
354 | 340 | ||
355 | int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key, BIGNUM *x, BIGNUM *y) | 341 | int |
356 | { | 342 | EC_KEY_set_public_key_affine_coordinates(EC_KEY * key, BIGNUM * x, BIGNUM * y) |
343 | { | ||
357 | BN_CTX *ctx = NULL; | 344 | BN_CTX *ctx = NULL; |
358 | BIGNUM *tx, *ty; | 345 | BIGNUM *tx, *ty; |
359 | EC_POINT *point = NULL; | 346 | EC_POINT *point = NULL; |
360 | int ok = 0, tmp_nid, is_char_two = 0; | 347 | int ok = 0, tmp_nid, is_char_two = 0; |
361 | 348 | ||
362 | if (!key || !key->group || !x || !y) | 349 | if (!key || !key->group || !x || !y) { |
363 | { | ||
364 | ECerr(EC_F_EC_KEY_SET_PUBLIC_KEY_AFFINE_COORDINATES, | 350 | ECerr(EC_F_EC_KEY_SET_PUBLIC_KEY_AFFINE_COORDINATES, |
365 | ERR_R_PASSED_NULL_PARAMETER); | 351 | ERR_R_PASSED_NULL_PARAMETER); |
366 | return 0; | 352 | return 0; |
367 | } | 353 | } |
368 | ctx = BN_CTX_new(); | 354 | ctx = BN_CTX_new(); |
369 | if (!ctx) | 355 | if (!ctx) |
370 | goto err; | 356 | goto err; |
@@ -376,41 +362,38 @@ int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key, BIGNUM *x, BIGNUM *y) | |||
376 | 362 | ||
377 | tmp_nid = EC_METHOD_get_field_type(EC_GROUP_method_of(key->group)); | 363 | tmp_nid = EC_METHOD_get_field_type(EC_GROUP_method_of(key->group)); |
378 | 364 | ||
379 | if (tmp_nid == NID_X9_62_characteristic_two_field) | 365 | if (tmp_nid == NID_X9_62_characteristic_two_field) |
380 | is_char_two = 1; | 366 | is_char_two = 1; |
381 | 367 | ||
382 | tx = BN_CTX_get(ctx); | 368 | tx = BN_CTX_get(ctx); |
383 | ty = BN_CTX_get(ctx); | 369 | ty = BN_CTX_get(ctx); |
384 | #ifndef OPENSSL_NO_EC2M | 370 | #ifndef OPENSSL_NO_EC2M |
385 | if (is_char_two) | 371 | if (is_char_two) { |
386 | { | ||
387 | if (!EC_POINT_set_affine_coordinates_GF2m(key->group, point, | 372 | if (!EC_POINT_set_affine_coordinates_GF2m(key->group, point, |
388 | x, y, ctx)) | 373 | x, y, ctx)) |
389 | goto err; | 374 | goto err; |
390 | if (!EC_POINT_get_affine_coordinates_GF2m(key->group, point, | 375 | if (!EC_POINT_get_affine_coordinates_GF2m(key->group, point, |
391 | tx, ty, ctx)) | 376 | tx, ty, ctx)) |
392 | goto err; | 377 | goto err; |
393 | } | 378 | } else |
394 | else | ||
395 | #endif | 379 | #endif |
396 | { | 380 | { |
397 | if (!EC_POINT_set_affine_coordinates_GFp(key->group, point, | 381 | if (!EC_POINT_set_affine_coordinates_GFp(key->group, point, |
398 | x, y, ctx)) | 382 | x, y, ctx)) |
399 | goto err; | 383 | goto err; |
400 | if (!EC_POINT_get_affine_coordinates_GFp(key->group, point, | 384 | if (!EC_POINT_get_affine_coordinates_GFp(key->group, point, |
401 | tx, ty, ctx)) | 385 | tx, ty, ctx)) |
402 | goto err; | 386 | goto err; |
403 | } | 387 | } |
404 | /* Check if retrieved coordinates match originals: if not values | 388 | /* |
405 | * are out of range. | 389 | * Check if retrieved coordinates match originals: if not values are |
390 | * out of range. | ||
406 | */ | 391 | */ |
407 | if (BN_cmp(x, tx) || BN_cmp(y, ty)) | 392 | if (BN_cmp(x, tx) || BN_cmp(y, ty)) { |
408 | { | ||
409 | ECerr(EC_F_EC_KEY_SET_PUBLIC_KEY_AFFINE_COORDINATES, | 393 | ECerr(EC_F_EC_KEY_SET_PUBLIC_KEY_AFFINE_COORDINATES, |
410 | EC_R_COORDINATES_OUT_OF_RANGE); | 394 | EC_R_COORDINATES_OUT_OF_RANGE); |
411 | goto err; | 395 | goto err; |
412 | } | 396 | } |
413 | |||
414 | if (!EC_KEY_set_public_key(key, point)) | 397 | if (!EC_KEY_set_public_key(key, point)) |
415 | goto err; | 398 | goto err; |
416 | 399 | ||
@@ -419,79 +402,92 @@ int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key, BIGNUM *x, BIGNUM *y) | |||
419 | 402 | ||
420 | ok = 1; | 403 | ok = 1; |
421 | 404 | ||
422 | err: | 405 | err: |
423 | if (ctx) | 406 | if (ctx) |
424 | BN_CTX_free(ctx); | 407 | BN_CTX_free(ctx); |
425 | if (point) | 408 | if (point) |
426 | EC_POINT_free(point); | 409 | EC_POINT_free(point); |
427 | return ok; | 410 | return ok; |
428 | 411 | ||
429 | } | 412 | } |
430 | 413 | ||
431 | const EC_GROUP *EC_KEY_get0_group(const EC_KEY *key) | 414 | const EC_GROUP * |
432 | { | 415 | EC_KEY_get0_group(const EC_KEY * key) |
416 | { | ||
433 | return key->group; | 417 | return key->group; |
434 | } | 418 | } |
435 | 419 | ||
436 | int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group) | 420 | int |
437 | { | 421 | EC_KEY_set_group(EC_KEY * key, const EC_GROUP * group) |
422 | { | ||
438 | if (key->group != NULL) | 423 | if (key->group != NULL) |
439 | EC_GROUP_free(key->group); | 424 | EC_GROUP_free(key->group); |
440 | key->group = EC_GROUP_dup(group); | 425 | key->group = EC_GROUP_dup(group); |
441 | return (key->group == NULL) ? 0 : 1; | 426 | return (key->group == NULL) ? 0 : 1; |
442 | } | 427 | } |
443 | 428 | ||
444 | const BIGNUM *EC_KEY_get0_private_key(const EC_KEY *key) | 429 | const BIGNUM * |
445 | { | 430 | EC_KEY_get0_private_key(const EC_KEY * key) |
431 | { | ||
446 | return key->priv_key; | 432 | return key->priv_key; |
447 | } | 433 | } |
448 | 434 | ||
449 | int EC_KEY_set_private_key(EC_KEY *key, const BIGNUM *priv_key) | 435 | int |
450 | { | 436 | EC_KEY_set_private_key(EC_KEY * key, const BIGNUM * priv_key) |
437 | { | ||
451 | if (key->priv_key) | 438 | if (key->priv_key) |
452 | BN_clear_free(key->priv_key); | 439 | BN_clear_free(key->priv_key); |
453 | key->priv_key = BN_dup(priv_key); | 440 | key->priv_key = BN_dup(priv_key); |
454 | return (key->priv_key == NULL) ? 0 : 1; | 441 | return (key->priv_key == NULL) ? 0 : 1; |
455 | } | 442 | } |
456 | 443 | ||
457 | const EC_POINT *EC_KEY_get0_public_key(const EC_KEY *key) | 444 | const EC_POINT * |
458 | { | 445 | EC_KEY_get0_public_key(const EC_KEY * key) |
446 | { | ||
459 | return key->pub_key; | 447 | return key->pub_key; |
460 | } | 448 | } |
461 | 449 | ||
462 | int EC_KEY_set_public_key(EC_KEY *key, const EC_POINT *pub_key) | 450 | int |
463 | { | 451 | EC_KEY_set_public_key(EC_KEY * key, const EC_POINT * pub_key) |
452 | { | ||
464 | if (key->pub_key != NULL) | 453 | if (key->pub_key != NULL) |
465 | EC_POINT_free(key->pub_key); | 454 | EC_POINT_free(key->pub_key); |
466 | key->pub_key = EC_POINT_dup(pub_key, key->group); | 455 | key->pub_key = EC_POINT_dup(pub_key, key->group); |
467 | return (key->pub_key == NULL) ? 0 : 1; | 456 | return (key->pub_key == NULL) ? 0 : 1; |
468 | } | 457 | } |
469 | 458 | ||
470 | unsigned int EC_KEY_get_enc_flags(const EC_KEY *key) | 459 | unsigned int |
471 | { | 460 | EC_KEY_get_enc_flags(const EC_KEY * key) |
461 | { | ||
472 | return key->enc_flag; | 462 | return key->enc_flag; |
473 | } | 463 | } |
474 | 464 | ||
475 | void EC_KEY_set_enc_flags(EC_KEY *key, unsigned int flags) | 465 | void |
476 | { | 466 | EC_KEY_set_enc_flags(EC_KEY * key, unsigned int flags) |
467 | { | ||
477 | key->enc_flag = flags; | 468 | key->enc_flag = flags; |
478 | } | 469 | } |
479 | 470 | ||
480 | point_conversion_form_t EC_KEY_get_conv_form(const EC_KEY *key) | 471 | point_conversion_form_t |
481 | { | 472 | EC_KEY_get_conv_form(const EC_KEY * key) |
473 | { | ||
482 | return key->conv_form; | 474 | return key->conv_form; |
483 | } | 475 | } |
484 | 476 | ||
485 | void EC_KEY_set_conv_form(EC_KEY *key, point_conversion_form_t cform) | 477 | void |
486 | { | 478 | EC_KEY_set_conv_form(EC_KEY * key, point_conversion_form_t cform) |
479 | { | ||
487 | key->conv_form = cform; | 480 | key->conv_form = cform; |
488 | if (key->group != NULL) | 481 | if (key->group != NULL) |
489 | EC_GROUP_set_point_conversion_form(key->group, cform); | 482 | EC_GROUP_set_point_conversion_form(key->group, cform); |
490 | } | 483 | } |
491 | 484 | ||
492 | void *EC_KEY_get_key_method_data(EC_KEY *key, | 485 | void * |
493 | void *(*dup_func)(void *), void (*free_func)(void *), void (*clear_free_func)(void *)) | 486 | EC_KEY_get_key_method_data(EC_KEY *key, |
494 | { | 487 | void *(*dup_func) (void *), |
488 | void (*free_func) (void *), | ||
489 | void (*clear_free_func) (void *)) | ||
490 | { | ||
495 | void *ret; | 491 | void *ret; |
496 | 492 | ||
497 | CRYPTO_r_lock(CRYPTO_LOCK_EC); | 493 | CRYPTO_r_lock(CRYPTO_LOCK_EC); |
@@ -499,11 +495,14 @@ void *EC_KEY_get_key_method_data(EC_KEY *key, | |||
499 | CRYPTO_r_unlock(CRYPTO_LOCK_EC); | 495 | CRYPTO_r_unlock(CRYPTO_LOCK_EC); |
500 | 496 | ||
501 | return ret; | 497 | return ret; |
502 | } | 498 | } |
503 | 499 | ||
504 | void *EC_KEY_insert_key_method_data(EC_KEY *key, void *data, | 500 | void * |
505 | void *(*dup_func)(void *), void (*free_func)(void *), void (*clear_free_func)(void *)) | 501 | EC_KEY_insert_key_method_data(EC_KEY * key, void *data, |
506 | { | 502 | void *(*dup_func) (void *), |
503 | void (*free_func) (void *), | ||
504 | void (*clear_free_func) (void *)) | ||
505 | { | ||
507 | EC_EXTRA_DATA *ex_data; | 506 | EC_EXTRA_DATA *ex_data; |
508 | 507 | ||
509 | CRYPTO_w_lock(CRYPTO_LOCK_EC); | 508 | CRYPTO_w_lock(CRYPTO_LOCK_EC); |
@@ -513,32 +512,37 @@ void *EC_KEY_insert_key_method_data(EC_KEY *key, void *data, | |||
513 | CRYPTO_w_unlock(CRYPTO_LOCK_EC); | 512 | CRYPTO_w_unlock(CRYPTO_LOCK_EC); |
514 | 513 | ||
515 | return ex_data; | 514 | return ex_data; |
516 | } | 515 | } |
517 | 516 | ||
518 | void EC_KEY_set_asn1_flag(EC_KEY *key, int flag) | 517 | void |
519 | { | 518 | EC_KEY_set_asn1_flag(EC_KEY * key, int flag) |
519 | { | ||
520 | if (key->group != NULL) | 520 | if (key->group != NULL) |
521 | EC_GROUP_set_asn1_flag(key->group, flag); | 521 | EC_GROUP_set_asn1_flag(key->group, flag); |
522 | } | 522 | } |
523 | 523 | ||
524 | int EC_KEY_precompute_mult(EC_KEY *key, BN_CTX *ctx) | 524 | int |
525 | { | 525 | EC_KEY_precompute_mult(EC_KEY * key, BN_CTX * ctx) |
526 | { | ||
526 | if (key->group == NULL) | 527 | if (key->group == NULL) |
527 | return 0; | 528 | return 0; |
528 | return EC_GROUP_precompute_mult(key->group, ctx); | 529 | return EC_GROUP_precompute_mult(key->group, ctx); |
529 | } | 530 | } |
530 | 531 | ||
531 | int EC_KEY_get_flags(const EC_KEY *key) | 532 | int |
532 | { | 533 | EC_KEY_get_flags(const EC_KEY * key) |
534 | { | ||
533 | return key->flags; | 535 | return key->flags; |
534 | } | 536 | } |
535 | 537 | ||
536 | void EC_KEY_set_flags(EC_KEY *key, int flags) | 538 | void |
537 | { | 539 | EC_KEY_set_flags(EC_KEY * key, int flags) |
540 | { | ||
538 | key->flags |= flags; | 541 | key->flags |= flags; |
539 | } | 542 | } |
540 | 543 | ||
541 | void EC_KEY_clear_flags(EC_KEY *key, int flags) | 544 | void |
542 | { | 545 | EC_KEY_clear_flags(EC_KEY * key, int flags) |
546 | { | ||
543 | key->flags &= ~flags; | 547 | key->flags &= ~flags; |
544 | } | 548 | } |