summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/lib/libcrypto/ec/ec_key.c50
-rw-r--r--src/lib/libcrypto/ec/ec_kmeth.c3
-rw-r--r--src/lib/libcrypto/ec/ec_lib.c154
-rw-r--r--src/lib/libcrypto/ec/ec_local.h27
4 files changed, 4 insertions, 230 deletions
diff --git a/src/lib/libcrypto/ec/ec_key.c b/src/lib/libcrypto/ec/ec_key.c
index 2f9f05cc56..4127352523 100644
--- a/src/lib/libcrypto/ec/ec_key.c
+++ b/src/lib/libcrypto/ec/ec_key.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: ec_key.c,v 1.32 2023/03/27 10:25:02 tb Exp $ */ 1/* $OpenBSD: ec_key.c,v 1.33 2023/06/25 18:52:27 tb Exp $ */
2/* 2/*
3 * Written by Nils Larsch for the OpenSSL project. 3 * Written by Nils Larsch for the OpenSSL project.
4 */ 4 */
@@ -122,16 +122,12 @@ EC_KEY_free(EC_KEY *r)
122 EC_POINT_free(r->pub_key); 122 EC_POINT_free(r->pub_key);
123 BN_free(r->priv_key); 123 BN_free(r->priv_key);
124 124
125 EC_EX_DATA_free_all_data(&r->method_data);
126
127 freezero(r, sizeof(EC_KEY)); 125 freezero(r, sizeof(EC_KEY));
128} 126}
129 127
130EC_KEY * 128EC_KEY *
131EC_KEY_copy(EC_KEY *dest, const EC_KEY *src) 129EC_KEY_copy(EC_KEY *dest, const EC_KEY *src)
132{ 130{
133 EC_EXTRA_DATA *d;
134
135 if (dest == NULL || src == NULL) { 131 if (dest == NULL || src == NULL) {
136 ECerror(ERR_R_PASSED_NULL_PARAMETER); 132 ECerror(ERR_R_PASSED_NULL_PARAMETER);
137 return NULL; 133 return NULL;
@@ -175,18 +171,6 @@ EC_KEY_copy(EC_KEY *dest, const EC_KEY *src)
175 if (!bn_copy(dest->priv_key, src->priv_key)) 171 if (!bn_copy(dest->priv_key, src->priv_key))
176 return NULL; 172 return NULL;
177 } 173 }
178 /* copy method/extra data */
179 EC_EX_DATA_free_all_data(&dest->method_data);
180
181 for (d = src->method_data; d != NULL; d = d->next) {
182 void *t = d->dup_func(d->data);
183
184 if (t == NULL)
185 return 0;
186 if (!EC_EX_DATA_set_data(&dest->method_data, t, d->dup_func,
187 d->free_func, d->clear_free_func))
188 return 0;
189 }
190 174
191 /* copy the rest */ 175 /* copy the rest */
192 dest->enc_flag = src->enc_flag; 176 dest->enc_flag = src->enc_flag;
@@ -526,38 +510,6 @@ EC_KEY_set_conv_form(EC_KEY *key, point_conversion_form_t cform)
526 EC_GROUP_set_point_conversion_form(key->group, cform); 510 EC_GROUP_set_point_conversion_form(key->group, cform);
527} 511}
528 512
529void *
530EC_KEY_get_key_method_data(EC_KEY *key,
531 void *(*dup_func) (void *),
532 void (*free_func) (void *),
533 void (*clear_free_func) (void *))
534{
535 void *ret;
536
537 CRYPTO_r_lock(CRYPTO_LOCK_EC);
538 ret = EC_EX_DATA_get_data(key->method_data, dup_func, free_func, clear_free_func);
539 CRYPTO_r_unlock(CRYPTO_LOCK_EC);
540
541 return ret;
542}
543
544void *
545EC_KEY_insert_key_method_data(EC_KEY *key, void *data,
546 void *(*dup_func) (void *),
547 void (*free_func) (void *),
548 void (*clear_free_func) (void *))
549{
550 EC_EXTRA_DATA *ex_data;
551
552 CRYPTO_w_lock(CRYPTO_LOCK_EC);
553 ex_data = EC_EX_DATA_get_data(key->method_data, dup_func, free_func, clear_free_func);
554 if (ex_data == NULL)
555 EC_EX_DATA_set_data(&key->method_data, data, dup_func, free_func, clear_free_func);
556 CRYPTO_w_unlock(CRYPTO_LOCK_EC);
557
558 return ex_data;
559}
560
561void 513void
562EC_KEY_set_asn1_flag(EC_KEY *key, int flag) 514EC_KEY_set_asn1_flag(EC_KEY *key, int flag)
563{ 515{
diff --git a/src/lib/libcrypto/ec/ec_kmeth.c b/src/lib/libcrypto/ec/ec_kmeth.c
index 56fb437093..4e296cfa68 100644
--- a/src/lib/libcrypto/ec/ec_kmeth.c
+++ b/src/lib/libcrypto/ec/ec_kmeth.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: ec_kmeth.c,v 1.7 2022/11/26 16:08:52 tb Exp $ */ 1/* $OpenBSD: ec_kmeth.c,v 1.8 2023/06/25 18:52:27 tb Exp $ */
2/* 2/*
3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL 3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
4 * project. 4 * project.
@@ -168,7 +168,6 @@ EC_KEY_new_method(ENGINE *engine)
168 ret->enc_flag = 0; 168 ret->enc_flag = 0;
169 ret->conv_form = POINT_CONVERSION_UNCOMPRESSED; 169 ret->conv_form = POINT_CONVERSION_UNCOMPRESSED;
170 ret->references = 1; 170 ret->references = 1;
171 ret->method_data = NULL;
172 171
173 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data)) 172 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data))
174 goto err; 173 goto err;
diff --git a/src/lib/libcrypto/ec/ec_lib.c b/src/lib/libcrypto/ec/ec_lib.c
index cb581f6e1c..2e180e9661 100644
--- a/src/lib/libcrypto/ec/ec_lib.c
+++ b/src/lib/libcrypto/ec/ec_lib.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: ec_lib.c,v 1.60 2023/06/24 18:21:07 jsing Exp $ */ 1/* $OpenBSD: ec_lib.c,v 1.61 2023/06/25 18:52:27 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 */
@@ -651,158 +651,6 @@ ec_point_blind_coordinates(const EC_GROUP *group, EC_POINT *p, BN_CTX *ctx)
651 return group->meth->blind_coordinates(group, p, ctx); 651 return group->meth->blind_coordinates(group, p, ctx);
652} 652}
653 653
654/* this has 'package' visibility */
655int
656EC_EX_DATA_set_data(EC_EXTRA_DATA ** ex_data, void *data,
657 void *(*dup_func) (void *),
658 void (*free_func) (void *),
659 void (*clear_free_func) (void *))
660{
661 EC_EXTRA_DATA *d;
662
663 if (ex_data == NULL)
664 return 0;
665
666 for (d = *ex_data; d != NULL; d = d->next) {
667 if (d->dup_func == dup_func && d->free_func == free_func &&
668 d->clear_free_func == clear_free_func) {
669 ECerror(EC_R_SLOT_FULL);
670 return 0;
671 }
672 }
673
674 if (data == NULL)
675 /* no explicit entry needed */
676 return 1;
677
678 d = malloc(sizeof *d);
679 if (d == NULL)
680 return 0;
681
682 d->data = data;
683 d->dup_func = dup_func;
684 d->free_func = free_func;
685 d->clear_free_func = clear_free_func;
686
687 d->next = *ex_data;
688 *ex_data = d;
689
690 return 1;
691}
692
693/* this has 'package' visibility */
694void *
695EC_EX_DATA_get_data(const EC_EXTRA_DATA *ex_data,
696 void *(*dup_func) (void *),
697 void (*free_func) (void *),
698 void (*clear_free_func) (void *))
699{
700 const EC_EXTRA_DATA *d;
701
702 for (d = ex_data; d != NULL; d = d->next) {
703 if (d->dup_func == dup_func && d->free_func == free_func && d->clear_free_func == clear_free_func)
704 return d->data;
705 }
706
707 return NULL;
708}
709
710/* this has 'package' visibility */
711void
712EC_EX_DATA_free_data(EC_EXTRA_DATA ** ex_data,
713 void *(*dup_func) (void *),
714 void (*free_func) (void *),
715 void (*clear_free_func) (void *))
716{
717 EC_EXTRA_DATA **p;
718
719 if (ex_data == NULL)
720 return;
721
722 for (p = ex_data; *p != NULL; p = &((*p)->next)) {
723 if ((*p)->dup_func == dup_func &&
724 (*p)->free_func == free_func &&
725 (*p)->clear_free_func == clear_free_func) {
726 EC_EXTRA_DATA *next = (*p)->next;
727
728 (*p)->free_func((*p)->data);
729 free(*p);
730
731 *p = next;
732 return;
733 }
734 }
735}
736
737/* this has 'package' visibility */
738void
739EC_EX_DATA_clear_free_data(EC_EXTRA_DATA ** ex_data,
740 void *(*dup_func) (void *),
741 void (*free_func) (void *),
742 void (*clear_free_func) (void *))
743{
744 EC_EXTRA_DATA **p;
745
746 if (ex_data == NULL)
747 return;
748
749 for (p = ex_data; *p != NULL; p = &((*p)->next)) {
750 if ((*p)->dup_func == dup_func &&
751 (*p)->free_func == free_func &&
752 (*p)->clear_free_func == clear_free_func) {
753 EC_EXTRA_DATA *next = (*p)->next;
754
755 (*p)->clear_free_func((*p)->data);
756 free(*p);
757
758 *p = next;
759 return;
760 }
761 }
762}
763
764/* this has 'package' visibility */
765void
766EC_EX_DATA_free_all_data(EC_EXTRA_DATA ** ex_data)
767{
768 EC_EXTRA_DATA *d;
769
770 if (ex_data == NULL)
771 return;
772
773 d = *ex_data;
774 while (d) {
775 EC_EXTRA_DATA *next = d->next;
776
777 d->free_func(d->data);
778 free(d);
779
780 d = next;
781 }
782 *ex_data = NULL;
783}
784
785/* this has 'package' visibility */
786void
787EC_EX_DATA_clear_free_all_data(EC_EXTRA_DATA ** ex_data)
788{
789 EC_EXTRA_DATA *d;
790
791 if (ex_data == NULL)
792 return;
793
794 d = *ex_data;
795 while (d) {
796 EC_EXTRA_DATA *next = d->next;
797
798 d->clear_free_func(d->data);
799 free(d);
800
801 d = next;
802 }
803 *ex_data = NULL;
804}
805
806EC_POINT * 654EC_POINT *
807EC_POINT_new(const EC_GROUP *group) 655EC_POINT_new(const EC_GROUP *group)
808{ 656{
diff --git a/src/lib/libcrypto/ec/ec_local.h b/src/lib/libcrypto/ec/ec_local.h
index eb0d6a82a6..6913cb5683 100644
--- a/src/lib/libcrypto/ec/ec_local.h
+++ b/src/lib/libcrypto/ec/ec_local.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: ec_local.h,v 1.18 2023/06/25 07:50:37 tb Exp $ */ 1/* $OpenBSD: ec_local.h,v 1.19 2023/06/25 18:52:27 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 */
@@ -173,14 +173,6 @@ struct ec_method_st {
173 BN_CTX *ctx); 173 BN_CTX *ctx);
174} /* EC_METHOD */; 174} /* EC_METHOD */;
175 175
176typedef struct ec_extra_data_st {
177 struct ec_extra_data_st *next;
178 void *data;
179 void *(*dup_func)(void *);
180 void (*free_func)(void *);
181 void (*clear_free_func)(void *);
182} EC_EXTRA_DATA; /* used in EC_GROUP */
183
184struct ec_group_st { 176struct ec_group_st {
185 /* 177 /*
186 * Methods and members exposed via the public API. 178 * Methods and members exposed via the public API.
@@ -260,26 +252,9 @@ struct ec_key_st {
260 int references; 252 int references;
261 int flags; 253 int flags;
262 254
263 EC_EXTRA_DATA *method_data;
264 CRYPTO_EX_DATA ex_data; 255 CRYPTO_EX_DATA ex_data;
265} /* EC_KEY */; 256} /* EC_KEY */;
266 257
267/* Basically a 'mixin' for extra data, but available for EC_GROUPs/EC_KEYs only
268 * (with visibility limited to 'package' level for now).
269 * We use the function pointers as index for retrieval; this obviates
270 * global ex_data-style index tables.
271 */
272int EC_EX_DATA_set_data(EC_EXTRA_DATA **, void *data,
273 void *(*dup_func)(void *), void (*free_func)(void *), void (*clear_free_func)(void *));
274void *EC_EX_DATA_get_data(const EC_EXTRA_DATA *,
275 void *(*dup_func)(void *), void (*free_func)(void *), void (*clear_free_func)(void *));
276void EC_EX_DATA_free_data(EC_EXTRA_DATA **,
277 void *(*dup_func)(void *), void (*free_func)(void *), void (*clear_free_func)(void *));
278void EC_EX_DATA_clear_free_data(EC_EXTRA_DATA **,
279 void *(*dup_func)(void *), void (*free_func)(void *), void (*clear_free_func)(void *));
280void EC_EX_DATA_free_all_data(EC_EXTRA_DATA **);
281void EC_EX_DATA_clear_free_all_data(EC_EXTRA_DATA **);
282
283struct ec_point_st { 258struct ec_point_st {
284 const EC_METHOD *meth; 259 const EC_METHOD *meth;
285 260