summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authortb <>2024-11-22 00:52:39 +0000
committertb <>2024-11-22 00:52:39 +0000
commitf92aaf708c114aa21014cf16e0e1788a766660ac (patch)
treee983798b6425eeebe1844ed01cc0d31d9bdb9a53 /src/lib
parent042cd1d37e41c4ec9a755da7534860a7b0cb140e (diff)
downloadopenbsd-f92aaf708c114aa21014cf16e0e1788a766660ac.tar.gz
openbsd-f92aaf708c114aa21014cf16e0e1788a766660ac.tar.bz2
openbsd-f92aaf708c114aa21014cf16e0e1788a766660ac.zip
Move wNAF[], wNAF_len[], wsize[] to the stack
Again, we know their sizes (always 2), so we can avoid allocating and freeing them. Also remove the extra "pivot" element. It's not needed. ok djm
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/libcrypto/ec/ec_mult.c38
1 files changed, 6 insertions, 32 deletions
diff --git a/src/lib/libcrypto/ec/ec_mult.c b/src/lib/libcrypto/ec/ec_mult.c
index 598694d80a..756abf1a3d 100644
--- a/src/lib/libcrypto/ec/ec_mult.c
+++ b/src/lib/libcrypto/ec/ec_mult.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: ec_mult.c,v 1.39 2024/11/22 00:15:38 tb Exp $ */ 1/* $OpenBSD: ec_mult.c,v 1.40 2024/11/22 00:52:39 tb Exp $ */
2/* 2/*
3 * Originally written by Bodo Moeller and Nils Larsch for the OpenSSL project. 3 * Originally written by Bodo Moeller and Nils Larsch for the OpenSSL project.
4 */ 4 */
@@ -227,6 +227,9 @@ int
227ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *m, 227ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *m,
228 const EC_POINT *point, const BIGNUM *n, BN_CTX *ctx) 228 const EC_POINT *point, const BIGNUM *n, BN_CTX *ctx)
229{ 229{
230 signed char *wNAF[2] = { 0 };
231 size_t wNAF_len[2] = { 0 };
232 size_t wsize[2] = { 0 };
230 const EC_POINT *generator = NULL; 233 const EC_POINT *generator = NULL;
231 EC_POINT *tmp = NULL; 234 EC_POINT *tmp = NULL;
232 EC_POINT **row[2] = { 0 }; 235 EC_POINT **row[2] = { 0 };
@@ -234,9 +237,6 @@ ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *m,
234 size_t i, j; 237 size_t i, j;
235 int k; 238 int k;
236 int r_is_inverted = 0; 239 int r_is_inverted = 0;
237 size_t *wsize = NULL; /* individual window sizes */
238 signed char **wNAF = NULL; /* individual wNAFs */
239 size_t *wNAF_len = NULL;
240 size_t max_len = 0; 240 size_t max_len = 0;
241 size_t num_val; 241 size_t num_val;
242 EC_POINT **val = NULL; /* precomputation */ 242 EC_POINT **val = NULL; /* precomputation */
@@ -259,23 +259,6 @@ ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *m,
259 259
260 totalnum = 2; 260 totalnum = 2;
261 261
262 /* includes space for pivot */
263 wNAF = reallocarray(NULL, (totalnum + 1), sizeof wNAF[0]);
264 if (wNAF == NULL) {
265 ECerror(ERR_R_MALLOC_FAILURE);
266 goto err;
267 }
268
269 wNAF[0] = NULL; /* preliminary pivot */
270
271 wsize = reallocarray(NULL, totalnum, sizeof wsize[0]);
272 wNAF_len = reallocarray(NULL, totalnum, sizeof wNAF_len[0]);
273
274 if (wsize == NULL || wNAF_len == NULL) {
275 ECerror(ERR_R_MALLOC_FAILURE);
276 goto err;
277 }
278
279 /* num_val will be the total number of temporarily precomputed points */ 262 /* num_val will be the total number of temporarily precomputed points */
280 num_val = 0; 263 num_val = 0;
281 264
@@ -285,7 +268,6 @@ ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *m,
285 bits = i < 1 ? BN_num_bits(n) : BN_num_bits(m); 268 bits = i < 1 ? BN_num_bits(n) : BN_num_bits(m);
286 wsize[i] = EC_window_bits_for_scalar_size(bits); 269 wsize[i] = EC_window_bits_for_scalar_size(bits);
287 num_val += (size_t) 1 << (wsize[i] - 1); 270 num_val += (size_t) 1 << (wsize[i] - 1);
288 wNAF[i + 1] = NULL; /* make sure we always have a pivot */
289 wNAF[i] = compute_wNAF(i < 1 ? n : m, wsize[i], &wNAF_len[i]); 271 wNAF[i] = compute_wNAF(i < 1 ? n : m, wsize[i], &wNAF_len[i]);
290 if (wNAF[i] == NULL) 272 if (wNAF[i] == NULL)
291 goto err; 273 goto err;
@@ -403,16 +385,8 @@ ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *m,
403 385
404 err: 386 err:
405 EC_POINT_free(tmp); 387 EC_POINT_free(tmp);
406 free(wsize); 388 free(wNAF[0]);
407 free(wNAF_len); 389 free(wNAF[1]);
408 if (wNAF != NULL) {
409 signed char **w;
410
411 for (w = wNAF; *w != NULL; w++)
412 free(*w);
413
414 free(wNAF);
415 }
416 if (val != NULL) { 390 if (val != NULL) {
417 for (v = val; *v != NULL; v++) 391 for (v = val; *v != NULL; v++)
418 EC_POINT_free(*v); 392 EC_POINT_free(*v);