Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cc: fix ed25519 signatures malleability #632

Merged
merged 4 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/cryptoconditions/src/include/ed25519/src/verify.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
#include "ge.h"
#include "sc.h"

/* L = 2^252+27742317777372353535851937790883648493 in little-endian form */
const unsigned char curve25519_order[32] = {
0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58,
0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10
};

static int consttime_equal(const unsigned char *x, const unsigned char *y) {
unsigned char r = 0;

Expand Down Expand Up @@ -59,6 +67,21 @@ int ed25519_verify(const unsigned char *signature, const unsigned char *message,
return 0;
}

/* make sure 0 <= s < L, as per RFC 8032, section 5.1.7 to prevent signature
* malleability. Due to the three-bit check above (forces s < 2^253) there
* is not that much room, but adding L once works with most signatures */
for (int i = 31; ; i--)
{
if (signature[i+32] < curve25519_order[i])
{
break;
}
else if (signature[i+32] > curve25519_order[i] || i == 0)
{
return 0;
}
}

sha512_init(&hash);
sha512_update(&hash, signature, 32);
sha512_update(&hash, public_key, 32);
Expand Down
47 changes: 47 additions & 0 deletions src/cryptoconditions/src/include/ed25519/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@
#include "src/ge.h"
#include "src/sc.h"

void print_signature(unsigned char signature[64], char *msg) {
printf("%s\n", msg);
for (int i = 0; i < 64; i++) {
printf("%02x", signature[i]);
if ((i + 1) % 8 != 0) {
printf(" ");
} else {
printf("\n");
}
}
}

int main() {
unsigned char public_key[32], private_key[64], seed[32], scalar[32];
Expand Down Expand Up @@ -146,5 +157,41 @@ int main() {

printf("%fus per shared secret\n", ((double) ((end - start) * 1000)) / CLOCKS_PER_SEC / i * 1000);

/* ed25519 signature malleability test */

printf("testing signature malleability\n");

const unsigned char curve25519_order[32] = {
0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58,
0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10
};

ed25519_create_seed(seed);
ed25519_create_keypair(public_key, private_key, seed);
ed25519_sign(signature, message, message_len, public_key, private_key);

print_signature(signature, "Signature:");
if (ed25519_verify(signature, message, message_len, public_key)) {
printf("valid signature\n");
} else {
printf("invalid signature\n");
}

// add L to S, which starts at sig[32]
unsigned int s = 0;
for (size_t i = 0; i < 32; i++) {
s = signature[32 + i] + curve25519_order[i] + (s >> 8);
signature[32 + i] = s & 0xff;
}

print_signature(signature,"Modified signature:");
if (ed25519_verify(signature, message, message_len, public_key)) {
printf("valid signature\n");
} else {
printf("invalid signature\n");
}

return 0;
}
2 changes: 1 addition & 1 deletion src/cryptoconditions/test-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
base58==0.2.5
secp256k1
pytest
pytest>=8.0.0
Loading