diff options
| author | Denys Vlasenko <vda.linux@googlemail.com> | 2021-04-26 21:58:04 +0200 |
|---|---|---|
| committer | Denys Vlasenko <vda.linux@googlemail.com> | 2021-04-26 21:58:04 +0200 |
| commit | 9a40be433de31b8a7fea20b7ebce3dafbedaf504 (patch) | |
| tree | 932d58daca61e599ca79e71ebffb785146f6766d | |
| parent | 120401249a37a77cd2d4c71ad20a9a194bfea409 (diff) | |
| download | busybox-w32-9a40be433de31b8a7fea20b7ebce3dafbedaf504.tar.gz busybox-w32-9a40be433de31b8a7fea20b7ebce3dafbedaf504.tar.bz2 busybox-w32-9a40be433de31b8a7fea20b7ebce3dafbedaf504.zip | |
tls: get rid of constant-time add/sub operations
function old new delta
sp_256_sub_10 - 22 +22
static.sp_256_mont_reduce_10 176 178 +2
sp_256_mod_mul_norm_10 1440 1439 -1
sp_256_proj_point_dbl_10 453 446 -7
sp_256_ecc_mulmod_10 1229 1216 -13
static.sp_256_mont_sub_10 52 30 -22
static.sp_256_cond_sub_10 32 - -32
------------------------------------------------------------------------------
(add/remove: 1/1 grow/shrink: 1/4 up/down: 24/-75) Total: -51 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
| -rw-r--r-- | networking/tls_sp_c32.c | 58 |
1 files changed, 21 insertions, 37 deletions
diff --git a/networking/tls_sp_c32.c b/networking/tls_sp_c32.c index 8527e7864..72a3be537 100644 --- a/networking/tls_sp_c32.c +++ b/networking/tls_sp_c32.c | |||
| @@ -203,26 +203,12 @@ static void sp_256_add_10(sp_digit* r, const sp_digit* a, const sp_digit* b) | |||
| 203 | r[i] = a[i] + b[i]; | 203 | r[i] = a[i] + b[i]; |
| 204 | } | 204 | } |
| 205 | 205 | ||
| 206 | /* Conditionally add a and b using the mask m. | 206 | /* Sub b from a into r. (r = a - b) */ |
| 207 | * m is -1 to add and 0 when not. | 207 | static void sp_256_sub_10(sp_digit* r, const sp_digit* a, const sp_digit* b) |
| 208 | */ | ||
| 209 | static void sp_256_cond_add_10(sp_digit* r, const sp_digit* a, | ||
| 210 | const sp_digit* b, const sp_digit m) | ||
| 211 | { | ||
| 212 | int i; | ||
| 213 | for (i = 0; i < 10; i++) | ||
| 214 | r[i] = a[i] + (b[i] & m); | ||
| 215 | } | ||
| 216 | |||
| 217 | /* Conditionally subtract b from a using the mask m. | ||
| 218 | * m is -1 to subtract and 0 when not. | ||
| 219 | */ | ||
| 220 | static void sp_256_cond_sub_10(sp_digit* r, const sp_digit* a, | ||
| 221 | const sp_digit* b, const sp_digit m) | ||
| 222 | { | 208 | { |
| 223 | int i; | 209 | int i; |
| 224 | for (i = 0; i < 10; i++) | 210 | for (i = 0; i < 10; i++) |
| 225 | r[i] = a[i] - (b[i] & m); | 211 | r[i] = a[i] - b[i]; |
| 226 | } | 212 | } |
| 227 | 213 | ||
| 228 | /* Shift number left one bit. Bottom bit is lost. */ | 214 | /* Shift number left one bit. Bottom bit is lost. */ |
| @@ -352,7 +338,8 @@ static void sp_256_mul_add_10(sp_digit* r, const sp_digit* a, sp_digit b) | |||
| 352 | /* Divide the number by 2 mod the modulus (prime). (r = a / 2 % m) */ | 338 | /* Divide the number by 2 mod the modulus (prime). (r = a / 2 % m) */ |
| 353 | static void sp_256_div2_10(sp_digit* r, const sp_digit* a, const sp_digit* m) | 339 | static void sp_256_div2_10(sp_digit* r, const sp_digit* a, const sp_digit* m) |
| 354 | { | 340 | { |
| 355 | sp_256_cond_add_10(r, a, m, 0 - (a[0] & 1)); | 341 | if (a[0] & 1) |
| 342 | sp_256_add_10(r, a, m); | ||
| 356 | sp_256_norm_10(r); | 343 | sp_256_norm_10(r); |
| 357 | sp_256_rshift1_10(r, r); | 344 | sp_256_rshift1_10(r, r); |
| 358 | } | 345 | } |
| @@ -382,7 +369,8 @@ static void sp_256_mont_add_10(sp_digit* r, const sp_digit* a, const sp_digit* b | |||
| 382 | { | 369 | { |
| 383 | sp_256_add_10(r, a, b); | 370 | sp_256_add_10(r, a, b); |
| 384 | sp_256_norm_10(r); | 371 | sp_256_norm_10(r); |
| 385 | sp_256_cond_sub_10(r, r, m, 0 - ((r[9] >> 22) > 0)); | 372 | if ((r[9] >> 22) > 0) |
| 373 | sp_256_sub_10(r, r, m); | ||
| 386 | sp_256_norm_10(r); | 374 | sp_256_norm_10(r); |
| 387 | } | 375 | } |
| 388 | 376 | ||
| @@ -391,7 +379,8 @@ static void sp_256_mont_dbl_10(sp_digit* r, const sp_digit* a, const sp_digit* m | |||
| 391 | { | 379 | { |
| 392 | sp_256_add_10(r, a, a); | 380 | sp_256_add_10(r, a, a); |
| 393 | sp_256_norm_10(r); | 381 | sp_256_norm_10(r); |
| 394 | sp_256_cond_sub_10(r, r, m, 0 - ((r[9] >> 22) > 0)); | 382 | if ((r[9] >> 22) > 0) |
| 383 | sp_256_sub_10(r, r, m); | ||
| 395 | sp_256_norm_10(r); | 384 | sp_256_norm_10(r); |
| 396 | } | 385 | } |
| 397 | 386 | ||
| @@ -400,28 +389,23 @@ static void sp_256_mont_tpl_10(sp_digit* r, const sp_digit* a, const sp_digit* m | |||
| 400 | { | 389 | { |
| 401 | sp_256_add_10(r, a, a); | 390 | sp_256_add_10(r, a, a); |
| 402 | sp_256_norm_10(r); | 391 | sp_256_norm_10(r); |
| 403 | sp_256_cond_sub_10(r, r, m, 0 - ((r[9] >> 22) > 0)); | 392 | if ((r[9] >> 22) > 0) |
| 393 | sp_256_sub_10(r, r, m); | ||
| 404 | sp_256_norm_10(r); | 394 | sp_256_norm_10(r); |
| 405 | sp_256_add_10(r, r, a); | 395 | sp_256_add_10(r, r, a); |
| 406 | sp_256_norm_10(r); | 396 | sp_256_norm_10(r); |
| 407 | sp_256_cond_sub_10(r, r, m, 0 - ((r[9] >> 22) > 0)); | 397 | if ((r[9] >> 22) > 0) |
| 398 | sp_256_sub_10(r, r, m); | ||
| 408 | sp_256_norm_10(r); | 399 | sp_256_norm_10(r); |
| 409 | } | 400 | } |
| 410 | 401 | ||
| 411 | /* Sub b from a into r. (r = a - b) */ | ||
| 412 | static void sp_256_sub_10(sp_digit* r, const sp_digit* a, const sp_digit* b) | ||
| 413 | { | ||
| 414 | int i; | ||
| 415 | for (i = 0; i < 10; i++) | ||
| 416 | r[i] = a[i] - b[i]; | ||
| 417 | } | ||
| 418 | |||
| 419 | /* Subtract two Montgomery form numbers (r = a - b % m) */ | 402 | /* Subtract two Montgomery form numbers (r = a - b % m) */ |
| 420 | static void sp_256_mont_sub_10(sp_digit* r, const sp_digit* a, const sp_digit* b, | 403 | static void sp_256_mont_sub_10(sp_digit* r, const sp_digit* a, const sp_digit* b, |
| 421 | const sp_digit* m) | 404 | const sp_digit* m) |
| 422 | { | 405 | { |
| 423 | sp_256_sub_10(r, a, b); | 406 | sp_256_sub_10(r, a, b); |
| 424 | sp_256_cond_add_10(r, r, m, r[9] >> 22); | 407 | if (r[9] >> 22) |
| 408 | sp_256_add_10(r, r, m); | ||
| 425 | sp_256_norm_10(r); | 409 | sp_256_norm_10(r); |
| 426 | } | 410 | } |
| 427 | 411 | ||
| @@ -460,7 +444,8 @@ static void sp_256_mont_reduce_10(sp_digit* a, const sp_digit* m, sp_digit mp) | |||
| 460 | } | 444 | } |
| 461 | 445 | ||
| 462 | sp_256_mont_shift_10(a, a); | 446 | sp_256_mont_shift_10(a, a); |
| 463 | sp_256_cond_sub_10(a, a, m, 0 - ((a[9] >> 22) > 0)); | 447 | if ((a[9] >> 22) > 0) |
| 448 | sp_256_sub_10(a, a, m); | ||
| 464 | sp_256_norm_10(a); | 449 | sp_256_norm_10(a); |
| 465 | } | 450 | } |
| 466 | 451 | ||
| @@ -590,7 +575,6 @@ static void sp_256_map_10(sp_point* r, sp_point* p) | |||
| 590 | { | 575 | { |
| 591 | sp_digit t1[2*10]; | 576 | sp_digit t1[2*10]; |
| 592 | sp_digit t2[2*10]; | 577 | sp_digit t2[2*10]; |
| 593 | int32_t n; | ||
| 594 | 578 | ||
| 595 | sp_256_mont_inv_10(t1, p->z); | 579 | sp_256_mont_inv_10(t1, p->z); |
| 596 | 580 | ||
| @@ -602,8 +586,8 @@ static void sp_256_map_10(sp_point* r, sp_point* p) | |||
| 602 | memset(r->x + 10, 0, sizeof(r->x) / 2); | 586 | memset(r->x + 10, 0, sizeof(r->x) / 2); |
| 603 | sp_256_mont_reduce_10(r->x, p256_mod, p256_mp_mod); | 587 | sp_256_mont_reduce_10(r->x, p256_mod, p256_mp_mod); |
| 604 | /* Reduce x to less than modulus */ | 588 | /* Reduce x to less than modulus */ |
| 605 | n = sp_256_cmp_10(r->x, p256_mod); | 589 | if (sp_256_cmp_10(r->x, p256_mod) >= 0) |
| 606 | sp_256_cond_sub_10(r->x, r->x, p256_mod, 0 - (n >= 0)); | 590 | sp_256_sub_10(r->x, r->x, p256_mod); |
| 607 | sp_256_norm_10(r->x); | 591 | sp_256_norm_10(r->x); |
| 608 | 592 | ||
| 609 | /* y /= z^3 */ | 593 | /* y /= z^3 */ |
| @@ -611,8 +595,8 @@ static void sp_256_map_10(sp_point* r, sp_point* p) | |||
| 611 | memset(r->y + 10, 0, sizeof(r->y) / 2); | 595 | memset(r->y + 10, 0, sizeof(r->y) / 2); |
| 612 | sp_256_mont_reduce_10(r->y, p256_mod, p256_mp_mod); | 596 | sp_256_mont_reduce_10(r->y, p256_mod, p256_mp_mod); |
| 613 | /* Reduce y to less than modulus */ | 597 | /* Reduce y to less than modulus */ |
| 614 | n = sp_256_cmp_10(r->y, p256_mod); | 598 | if (sp_256_cmp_10(r->y, p256_mod) >= 0) |
| 615 | sp_256_cond_sub_10(r->y, r->y, p256_mod, 0 - (n >= 0)); | 599 | sp_256_sub_10(r->y, r->y, p256_mod); |
| 616 | sp_256_norm_10(r->y); | 600 | sp_256_norm_10(r->y); |
| 617 | 601 | ||
| 618 | memset(r->z, 0, sizeof(r->z)); | 602 | memset(r->z, 0, sizeof(r->z)); |
