diff options
| author | tb <> | 2022-11-17 22:45:48 +0000 |
|---|---|---|
| committer | tb <> | 2022-11-17 22:45:48 +0000 |
| commit | 542b98969d50d39beed5e732bb18f840e947086d (patch) | |
| tree | af60f9da0cf40bd050a5dc9364e90692c58a8466 /src | |
| parent | f980b05434026f1410857753670f5346b3af9690 (diff) | |
| download | openbsd-542b98969d50d39beed5e732bb18f840e947086d.tar.gz openbsd-542b98969d50d39beed5e732bb18f840e947086d.tar.bz2 openbsd-542b98969d50d39beed5e732bb18f840e947086d.zip | |
Avoid a few unnecessary contortions
Turns out that after ~40 years of practice I still can't do addition
with carry correctly :S
Diffstat (limited to 'src')
| -rw-r--r-- | src/regress/lib/libcrypto/curve25519/ed25519test.c | 47 |
1 files changed, 12 insertions, 35 deletions
diff --git a/src/regress/lib/libcrypto/curve25519/ed25519test.c b/src/regress/lib/libcrypto/curve25519/ed25519test.c index 4239997837..da14070c2f 100644 --- a/src/regress/lib/libcrypto/curve25519/ed25519test.c +++ b/src/regress/lib/libcrypto/curve25519/ed25519test.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: ed25519test.c,v 1.5 2022/11/17 21:19:43 tb Exp $ */ | 1 | /* $OpenBSD: ed25519test.c,v 1.6 2022/11/17 22:45:48 tb Exp $ */ |
| 2 | /* | 2 | /* |
| 3 | * Copyright (c) 2019, 2022 Theo Buehler <tb@openbsd.org> | 3 | * Copyright (c) 2019, 2022 Theo Buehler <tb@openbsd.org> |
| 4 | * | 4 | * |
| @@ -387,22 +387,6 @@ dump_info(const uint8_t *message, size_t message_len, const uint8_t *public_key, | |||
| 387 | } | 387 | } |
| 388 | } | 388 | } |
| 389 | 389 | ||
| 390 | static void | ||
| 391 | dump_once(const char *description, const uint8_t *message, size_t message_len, | ||
| 392 | const uint8_t *public_key, const uint8_t *private_key, | ||
| 393 | const uint8_t *signature) | ||
| 394 | { | ||
| 395 | static int dumped = 0; | ||
| 396 | |||
| 397 | if (dumped) | ||
| 398 | return; | ||
| 399 | |||
| 400 | fprintf(stderr, "%s\n", description); | ||
| 401 | dump_info(message, message_len, public_key, private_key, signature); | ||
| 402 | |||
| 403 | dumped = 1; | ||
| 404 | } | ||
| 405 | |||
| 406 | /* | 390 | /* |
| 407 | * Little-endian representation of the order of edwards25519, | 391 | * Little-endian representation of the order of edwards25519, |
| 408 | * see https://www.rfc-editor.org/rfc/rfc7748#section-4.1 | 392 | * see https://www.rfc-editor.org/rfc/rfc7748#section-4.1 |
| @@ -421,18 +405,22 @@ static const uint8_t order[] = { | |||
| 421 | static void | 405 | static void |
| 422 | modify_signature(uint8_t *signature) | 406 | modify_signature(uint8_t *signature) |
| 423 | { | 407 | { |
| 408 | uint16_t sum; | ||
| 424 | uint8_t *upper_half = &signature[32]; | 409 | uint8_t *upper_half = &signature[32]; |
| 410 | uint16_t carry = 0; | ||
| 425 | size_t i; | 411 | size_t i; |
| 426 | 412 | ||
| 427 | for (i = 0; i < sizeof(order); i++) { | 413 | for (i = 0; i < sizeof(order); i++) { |
| 428 | if (i < sizeof(order) - 1 && 0xff - order[i] < upper_half[i]) | 414 | sum = carry + order[i] + upper_half[i]; |
| 429 | upper_half[i + 1] += 1; | 415 | carry = (sum > 0xff); |
| 430 | upper_half[i] += order[i]; | 416 | upper_half[i] = sum & 0xff; |
| 431 | } | 417 | } |
| 418 | |||
| 419 | /* carry == 0 since 0 <= upper_half < order and 2 * order < 2^256. */ | ||
| 432 | } | 420 | } |
| 433 | 421 | ||
| 434 | static int | 422 | static int |
| 435 | test_signature_malleability(void) | 423 | test_ED25519_signature_malleability(void) |
| 436 | { | 424 | { |
| 437 | uint8_t public_key[ED25519_PUBLIC_KEY_LENGTH]; | 425 | uint8_t public_key[ED25519_PUBLIC_KEY_LENGTH]; |
| 438 | uint8_t private_key[ED25519_PRIVATE_KEY_LENGTH]; | 426 | uint8_t private_key[ED25519_PRIVATE_KEY_LENGTH]; |
| @@ -461,8 +449,9 @@ test_signature_malleability(void) | |||
| 461 | modify_signature(signature); | 449 | modify_signature(signature); |
| 462 | 450 | ||
| 463 | if (ED25519_verify(message, sizeof(message), signature, public_key)) { | 451 | if (ED25519_verify(message, sizeof(message), signature, public_key)) { |
| 464 | dump_once("Verified with modified signature", message, | 452 | fprintf(stderr, "Verified with modified signature\n"); |
| 465 | sizeof(message), public_key, private_key, signature); | 453 | dump_info(message, sizeof(message), public_key, private_key, |
| 454 | signature); | ||
| 466 | goto err; | 455 | goto err; |
| 467 | } | 456 | } |
| 468 | 457 | ||
| @@ -472,18 +461,6 @@ test_signature_malleability(void) | |||
| 472 | return failed; | 461 | return failed; |
| 473 | } | 462 | } |
| 474 | 463 | ||
| 475 | static int | ||
| 476 | test_ED25519_signature_malleability(void) | ||
| 477 | { | ||
| 478 | int i; | ||
| 479 | int failed = 0; | ||
| 480 | |||
| 481 | for (i = 0; i < 128; i++) | ||
| 482 | failed |= test_signature_malleability(); | ||
| 483 | |||
| 484 | return failed; | ||
| 485 | } | ||
| 486 | |||
| 487 | int | 464 | int |
| 488 | main(int argc, char *argv[]) | 465 | main(int argc, char *argv[]) |
| 489 | { | 466 | { |
