Repository navigation
Commit 462ddce
crypto/ecies: improve concatKDF (#20836)
This removes a bunch of weird code around the counter overflow check in
concatKDF and makes it actually work for different hash output sizes.
The overflow check worked as follows: concatKDF applies the hash function N
times, where N is roundup(kdLen, hashsize) / hashsize. N should not
overflow 32 bits because that would lead to a repetition in the KDF output.
A couple issues with the overflow check:
- It used the hash.BlockSize, which is wrong because the
block size is about the input of the hash function. Luckily, all standard
hash functions have a block size that's greater than the output size, so
concatKDF didn't crash, it just generated too much key material.
- The check used big.Int to compare against 2^32-1.
- The calculation could still overflow before reaching the check.
The new code in concatKDF doesn't check for overflow. Instead, there is a
new check on ECIESParams which ensures that params.KeyLen is < 512. This
removes any possibility of overflow.
There are a couple of miscellaneous improvements bundled in with this
change:
- The key buffer is pre-allocated instead of appending the hash output
to an initially empty slice.
- The code that uses concatKDF to derive keys is now shared between Encrypt
and Decrypt.
- There was a redundant invocation of IsOnCurve in Decrypt. This is now removed
because elliptic.Unmarshal already checks whether the input is a valid curve
point since Go 1.5.
Co-authored-by: Felix Lange <fjl@twurst.com>1 parent f7b29ec commit 462ddce
3 files changed
Lines changed: 93 additions & 109 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
35 | 35 | | |
36 | 36 | | |
37 | 37 | | |
| 38 | + | |
38 | 39 | | |
39 | 40 | | |
40 | 41 | | |
| |||
44 | 45 | | |
45 | 46 | | |
46 | 47 | | |
47 | | - | |
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
| |||
138 | 138 | | |
139 | 139 | | |
140 | 140 | | |
141 | | - | |
142 | 141 | | |
143 | 142 | | |
144 | 143 | | |
145 | 144 | | |
146 | | - | |
147 | | - | |
148 | | - | |
149 | | - | |
150 | | - | |
151 | | - | |
152 | | - | |
153 | | - | |
154 | | - | |
155 | | - | |
156 | | - | |
157 | | - | |
158 | | - | |
159 | | - | |
160 | | - | |
161 | | - | |
162 | | - | |
163 | | - | |
164 | | - | |
165 | | - | |
166 | 145 | | |
167 | | - | |
168 | | - | |
169 | | - | |
170 | | - | |
171 | | - | |
172 | | - | |
173 | | - | |
174 | | - | |
175 | | - | |
176 | | - | |
177 | | - | |
178 | | - | |
179 | | - | |
180 | | - | |
181 | | - | |
182 | | - | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
183 | 153 | | |
184 | 154 | | |
185 | | - | |
186 | | - | |
187 | | - | |
| 155 | + | |
188 | 156 | | |
| 157 | + | |
| 158 | + | |
189 | 159 | | |
190 | | - | |
191 | | - | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
192 | 174 | | |
193 | 175 | | |
194 | 176 | | |
| |||
209 | 191 | | |
210 | 192 | | |
211 | 193 | | |
212 | | - | |
213 | 194 | | |
214 | 195 | | |
215 | 196 | | |
| |||
249 | 230 | | |
250 | 231 | | |
251 | 232 | | |
252 | | - | |
253 | | - | |
254 | | - | |
255 | | - | |
256 | | - | |
257 | | - | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
258 | 236 | | |
| 237 | + | |
259 | 238 | | |
260 | 239 | | |
261 | | - | |
| 240 | + | |
262 | 241 | | |
263 | 242 | | |
264 | | - | |
265 | 243 | | |
266 | 244 | | |
267 | | - | |
268 | | - | |
269 | | - | |
270 | | - | |
271 | | - | |
| 245 | + | |
272 | 246 | | |
273 | | - | |
274 | | - | |
275 | | - | |
276 | | - | |
277 | | - | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
278 | 250 | | |
279 | 251 | | |
280 | 252 | | |
281 | | - | |
| 253 | + | |
282 | 254 | | |
283 | 255 | | |
284 | 256 | | |
| |||
288 | 260 | | |
289 | 261 | | |
290 | 262 | | |
291 | | - | |
| 263 | + | |
292 | 264 | | |
293 | 265 | | |
294 | 266 | | |
295 | 267 | | |
296 | 268 | | |
297 | 269 | | |
298 | 270 | | |
299 | | - | |
300 | | - | |
301 | | - | |
302 | | - | |
303 | | - | |
304 | | - | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
305 | 274 | | |
| 275 | + | |
306 | 276 | | |
307 | 277 | | |
308 | 278 | | |
| |||
316 | 286 | | |
317 | 287 | | |
318 | 288 | | |
319 | | - | |
320 | | - | |
| 289 | + | |
321 | 290 | | |
322 | 291 | | |
323 | | - | |
324 | | - | |
| 292 | + | |
325 | 293 | | |
326 | 294 | | |
327 | 295 | | |
| |||
331 | 299 | | |
332 | 300 | | |
333 | 301 | | |
334 | | - | |
335 | | - | |
336 | | - | |
337 | | - | |
338 | | - | |
339 | | - | |
| 302 | + | |
340 | 303 | | |
341 | 304 | | |
342 | 305 | | |
343 | 306 | | |
344 | | - | |
| 307 | + | |
345 | 308 | | |
346 | | - | |
347 | | - | |
348 | | - | |
349 | | - | |
350 | | - | |
351 | | - | |
352 | | - | |
353 | | - | |
354 | | - | |
355 | | - | |
356 | | - | |
| 309 | + | |
357 | 310 | | |
358 | 311 | | |
359 | 312 | | |
360 | | - | |
361 | | - | |
| 313 | + | |
362 | 314 | | |
363 | 315 | | |
364 | | - | |
365 | | - | |
| 316 | + | |
366 | 317 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
42 | 42 | | |
43 | 43 | | |
44 | 44 | | |
45 | | - | |
46 | 45 | | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
56 | 62 | | |
57 | 63 | | |
58 | 64 | | |
| |||
293 | 299 | | |
294 | 300 | | |
295 | 301 | | |
296 | | - | |
297 | | - | |
| 302 | + | |
| 303 | + | |
298 | 304 | | |
299 | 305 | | |
300 | 306 | | |
| |||
401 | 407 | | |
402 | 408 | | |
403 | 409 | | |
404 | | - | |
| 410 | + | |
405 | 411 | | |
406 | 412 | | |
407 | 413 | | |
| |||
414 | 420 | | |
415 | 421 | | |
416 | 422 | | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
49 | 49 | | |
50 | 50 | | |
51 | 51 | | |
| 52 | + | |
52 | 53 | | |
53 | 54 | | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
54 | 60 | | |
55 | 61 | | |
56 | 62 | | |
| |||
115 | 121 | | |
116 | 122 | | |
117 | 123 | | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
0 commit comments