summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/ec/ec_oct.c
diff options
context:
space:
mode:
authorjsing <>2023-04-11 18:58:20 +0000
committerjsing <>2023-04-11 18:58:20 +0000
commitb0ee26c7d2e2ba5f8d9159d9c269c93565c36841 (patch)
tree566c48c290ac86140f8df6c959b74661e1d596a7 /src/lib/libcrypto/ec/ec_oct.c
parent9253152f1f616a3508716fdac0238296418c2025 (diff)
downloadopenbsd-b0ee26c7d2e2ba5f8d9159d9c269c93565c36841.tar.gz
openbsd-b0ee26c7d2e2ba5f8d9159d9c269c93565c36841.tar.bz2
openbsd-b0ee26c7d2e2ba5f8d9159d9c269c93565c36841.zip
Handle BN_CTX at the EC API boundary.
The EC API allows callers to optionally pass in a BN_CTX, which means that any code needing a BN_CTX has to check if one was provided, allocate one if not, then free it again. Rather than doing this dance throughout the EC code, handle the BN_CTX existance at the EC API boundary. This means that lower level implementation code can simply assume that the BN_CTX is available. ok tb@
Diffstat (limited to 'src/lib/libcrypto/ec/ec_oct.c')
-rw-r--r--src/lib/libcrypto/ec/ec_oct.c70
1 files changed, 56 insertions, 14 deletions
diff --git a/src/lib/libcrypto/ec/ec_oct.c b/src/lib/libcrypto/ec/ec_oct.c
index ef17ec59a5..b1c9e6a634 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.10 2023/03/08 04:50:27 jsing Exp $ */ 1/* $OpenBSD: ec_oct.c,v 1.11 2023/04/11 18:58:20 jsing 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 */
@@ -72,18 +72,32 @@
72 72
73int 73int
74EC_POINT_set_compressed_coordinates(const EC_GROUP *group, EC_POINT *point, 74EC_POINT_set_compressed_coordinates(const EC_GROUP *group, EC_POINT *point,
75 const BIGNUM *x, int y_bit, BN_CTX *ctx) 75 const BIGNUM *x, int y_bit, BN_CTX *ctx_in)
76{ 76{
77 BN_CTX *ctx;
78 int ret = 0;
79
80 if ((ctx = ctx_in) == NULL)
81 ctx = BN_CTX_new();
82 if (ctx == NULL)
83 goto err;
84
77 if (group->meth->point_set_compressed_coordinates == NULL) { 85 if (group->meth->point_set_compressed_coordinates == NULL) {
78 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 86 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
79 return 0; 87 goto err;
80 } 88 }
81 if (group->meth != point->meth) { 89 if (group->meth != point->meth) {
82 ECerror(EC_R_INCOMPATIBLE_OBJECTS); 90 ECerror(EC_R_INCOMPATIBLE_OBJECTS);
83 return 0; 91 goto err;
84 } 92 }
85 return group->meth->point_set_compressed_coordinates(group, point, 93 ret = group->meth->point_set_compressed_coordinates(group, point,
86 x, y_bit, ctx); 94 x, y_bit, ctx);
95
96 err:
97 if (ctx != ctx_in)
98 BN_CTX_free(ctx);
99
100 return ret;
87} 101}
88 102
89int 103int
@@ -104,31 +118,59 @@ EC_POINT_set_compressed_coordinates_GF2m(const EC_GROUP *group, EC_POINT *point,
104 118
105size_t 119size_t
106EC_POINT_point2oct(const EC_GROUP *group, const EC_POINT *point, 120EC_POINT_point2oct(const EC_GROUP *group, const EC_POINT *point,
107 point_conversion_form_t form, 121 point_conversion_form_t form, unsigned char *buf, size_t len,
108 unsigned char *buf, size_t len, BN_CTX *ctx) 122 BN_CTX *ctx_in)
109{ 123{
124 BN_CTX *ctx;
125 int ret = 0;
126
127 if ((ctx = ctx_in) == NULL)
128 ctx = BN_CTX_new();
129 if (ctx == NULL)
130 goto err;
131
110 if (group->meth->point2oct == NULL) { 132 if (group->meth->point2oct == NULL) {
111 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 133 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
112 return 0; 134 goto err;
113 } 135 }
114 if (group->meth != point->meth) { 136 if (group->meth != point->meth) {
115 ECerror(EC_R_INCOMPATIBLE_OBJECTS); 137 ECerror(EC_R_INCOMPATIBLE_OBJECTS);
116 return 0; 138 goto err;
117 } 139 }
118 return group->meth->point2oct(group, point, form, buf, len, ctx); 140 ret = group->meth->point2oct(group, point, form, buf, len, ctx);
141
142 err:
143 if (ctx != ctx_in)
144 BN_CTX_free(ctx);
145
146 return ret;
119} 147}
120 148
121int 149int
122EC_POINT_oct2point(const EC_GROUP *group, EC_POINT *point, 150EC_POINT_oct2point(const EC_GROUP *group, EC_POINT *point,
123 const unsigned char *buf, size_t len, BN_CTX *ctx) 151 const unsigned char *buf, size_t len, BN_CTX *ctx_in)
124{ 152{
153 BN_CTX *ctx;
154 int ret = 0;
155
156 if ((ctx = ctx_in) == NULL)
157 ctx = BN_CTX_new();
158 if (ctx == NULL)
159 goto err;
160
125 if (group->meth->oct2point == NULL) { 161 if (group->meth->oct2point == NULL) {
126 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 162 ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
127 return 0; 163 goto err;
128 } 164 }
129 if (group->meth != point->meth) { 165 if (group->meth != point->meth) {
130 ECerror(EC_R_INCOMPATIBLE_OBJECTS); 166 ECerror(EC_R_INCOMPATIBLE_OBJECTS);
131 return 0; 167 goto err;
132 } 168 }
133 return group->meth->oct2point(group, point, buf, len, ctx); 169 ret = group->meth->oct2point(group, point, buf, len, ctx);
170
171 err:
172 if (ctx != ctx_in)
173 BN_CTX_free(ctx);
174
175 return ret;
134} 176}