summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authortb <>2024-11-23 07:33:26 +0000
committertb <>2024-11-23 07:33:26 +0000
commitb694ac34c67a61603d51be4ed12f815e39102235 (patch)
tree80f88736a01aafffad950471a699ca386e50a175 /src
parent73c530b2be0c7fa5339689b03437a1961065e117 (diff)
downloadopenbsd-b694ac34c67a61603d51be4ed12f815e39102235.tar.gz
openbsd-b694ac34c67a61603d51be4ed12f815e39102235.tar.bz2
openbsd-b694ac34c67a61603d51be4ed12f815e39102235.zip
Further simplify after dropping wNAF modification
We can now turn the for loop into a proper for loop for which there is obviously no out of bounds access. The length can be determined up front and it's easier to explain what's going on, so expand a few comments. ok jsing
Diffstat (limited to 'src')
-rw-r--r--src/lib/libcrypto/ec/ec_mult.c36
1 files changed, 13 insertions, 23 deletions
diff --git a/src/lib/libcrypto/ec/ec_mult.c b/src/lib/libcrypto/ec/ec_mult.c
index b7a9e346cf..382174a367 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.48 2024/11/23 07:28:57 tb Exp $ */ 1/* $OpenBSD: ec_mult.c,v 1.49 2024/11/23 07:33:26 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 */
@@ -98,24 +98,12 @@ ec_compute_wNAF(const BIGNUM *bn, signed char **out_wNAF, size_t *out_wNAF_len,
98 size_t *out_len) 98 size_t *out_len)
99{ 99{
100 signed char *wNAF = NULL; 100 signed char *wNAF = NULL;
101 size_t wNAF_len = 1, len = 1; 101 size_t i, wNAF_len, len;
102 int digit, bit, next, sign, wbits, window; 102 int digit, bit, next, sign, wbits, window;
103 size_t i;
104 int ret = 0; 103 int ret = 0;
105 104
106 if (BN_is_zero(bn)) { 105 wNAF_len = BN_num_bits(bn) + 1;
107 if ((wNAF = calloc(1, 1)) == NULL) { 106 if ((wNAF = calloc(1, wNAF_len)) == NULL) {
108 ECerror(ERR_R_MALLOC_FAILURE);
109 goto err;
110 }
111
112 goto done;
113 }
114
115 sign = BN_is_negative(bn) ? -1 : 1;
116
117 wNAF_len = BN_num_bits(bn);
118 if ((wNAF = calloc(1, wNAF_len + 1)) == NULL) {
119 ECerror(ERR_R_MALLOC_FAILURE); 107 ECerror(ERR_R_MALLOC_FAILURE);
120 goto err; 108 goto err;
121 } 109 }
@@ -123,6 +111,11 @@ ec_compute_wNAF(const BIGNUM *bn, signed char **out_wNAF, size_t *out_wNAF_len,
123 wbits = ec_window_bits(bn); 111 wbits = ec_window_bits(bn);
124 len = 1 << (wbits - 1); 112 len = 1 << (wbits - 1);
125 113
114 if (BN_is_zero(bn))
115 goto done;
116
117 sign = BN_is_negative(bn) ? -1 : 1;
118
126 bit = 1 << wbits; 119 bit = 1 << wbits;
127 next = bit << 1; 120 next = bit << 1;
128 121
@@ -134,15 +127,14 @@ ec_compute_wNAF(const BIGNUM *bn, signed char **out_wNAF, size_t *out_wNAF_len,
134 } 127 }
135 128
136 /* Instead of bn >>= 1 in each iteration, slide window to the left. */ 129 /* Instead of bn >>= 1 in each iteration, slide window to the left. */
137 for (i = 0; i + wbits + 1 < wNAF_len || window != 0; i++) { 130 for (i = 0; i < wNAF_len; i++) {
138 digit = 0; 131 digit = 0;
139 132
140 /* 133 /*
141 * If window is odd, the i-th wNAF digit is window (mods 2^w), 134 * If window is odd, the i-th wNAF digit is window (mods 2^w),
142 * where mods is the signed modulo in (-2^w-1, 2^w-1]. In the 135 * where mods is the signed modulo in (-2^w-1, 2^w-1]. Subtract
143 * last iterations the digits are grouped slightly differently. 136 * the digit from window, so window is 0 or next, and add the
144 * Subtract the digit from window, so window is 0, next, or bit, 137 * digit to the wNAF digits.
145 * and add the digit to the wNAF digits.
146 */ 138 */
147 if ((window & 1) != 0) { 139 if ((window & 1) != 0) {
148 digit = window; 140 digit = window;
@@ -158,8 +150,6 @@ ec_compute_wNAF(const BIGNUM *bn, signed char **out_wNAF, size_t *out_wNAF_len,
158 window += bit * BN_is_bit_set(bn, i + wbits + 1); 150 window += bit * BN_is_bit_set(bn, i + wbits + 1);
159 } 151 }
160 152
161 wNAF_len = i;
162
163 done: 153 done:
164 *out_wNAF = wNAF; 154 *out_wNAF = wNAF;
165 wNAF = NULL; 155 wNAF = NULL;