Skip to content

Commit 6ac4e6f

Browse files
authored
Merge pull request #2 from iotanbo/bugfix-issue#1
Fixed issue #1
2 parents bfd0003 + c7c8e43 commit 6ac4e6f

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

src/compact_x25519.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,14 @@ void compact_x25519_shared(
2121
const uint8_t my_private_key[X25519_KEY_SIZE],
2222
const uint8_t their_public_key[X25519_KEY_SIZE]
2323
) {
24-
c25519_smult(shared_secret, their_public_key, my_private_key);
24+
// Ensure that supplied private key is clamped (fix issue #1).
25+
// Calling `c25519_prepare` multiple times for the same private key
26+
// is OK because it won't modify already clamped key.
27+
uint8_t clamped_private_key[X25519_KEY_SIZE];
28+
memcpy(clamped_private_key, my_private_key, X25519_KEY_SIZE);
29+
c25519_prepare(clamped_private_key);
30+
c25519_smult(shared_secret, their_public_key, clamped_private_key);
31+
compact_wipe(clamped_private_key, X25519_KEY_SIZE);
2532
}
2633

2734
#ifndef COMPACT_DISABLE_X25519_DERIVE

test/run-all.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,64 @@ static int testX25519(pcg32_random_t* rng) {
6161
return -1;
6262
}
6363
}
64+
65+
int testX25519WithRfc7748Vectors(void) {
66+
printf("Testing X25519 using rfc7748 test vectors: ");
67+
int r = 0;
68+
uint8_t alice_sec[X25519_KEY_SIZE] = {
69+
0x77, 0x07, 0x6d, 0x0a, 0x73, 0x18, 0xa5, 0x7d,
70+
0x3c, 0x16, 0xc1, 0x72, 0x51, 0xb2, 0x66, 0x45,
71+
0xdf, 0x4c, 0x2f, 0x87, 0xeb, 0xc0, 0x99, 0x2a,
72+
0xb1, 0x77, 0xfb, 0xa5, 0x1d, 0xb9, 0x2c, 0x2a
73+
};
74+
75+
uint8_t alice_pub[X25519_KEY_SIZE] = {
76+
0x85, 0x20, 0xf0, 0x09, 0x89, 0x30, 0xa7, 0x54,
77+
0x74, 0x8b, 0x7d, 0xdc, 0xb4, 0x3e, 0xf7, 0x5a,
78+
0x0d, 0xbf, 0x3a, 0x0d, 0x26, 0x38, 0x1a, 0xf4,
79+
0xeb, 0xa4, 0xa9, 0x8e, 0xaa, 0x9b, 0x4e, 0x6a
80+
};
81+
82+
uint8_t bob_sec[X25519_KEY_SIZE] = {
83+
0x5d, 0xab, 0x08, 0x7e, 0x62, 0x4a, 0x8a, 0x4b,
84+
0x79, 0xe1, 0x7f, 0x8b, 0x83, 0x80, 0x0e, 0xe6,
85+
0x6f, 0x3b, 0xb1, 0x29, 0x26, 0x18, 0xb6, 0xfd,
86+
0x1c, 0x2f, 0x8b, 0x27, 0xff, 0x88, 0xe0, 0xeb
87+
};
88+
89+
uint8_t bob_pub[X25519_KEY_SIZE] = {
90+
0xde, 0x9e, 0xdb, 0x7d, 0x7b, 0x7d, 0xc1, 0xb4,
91+
0xd3, 0x5b, 0x61, 0xc2, 0xec, 0xe4, 0x35, 0x37,
92+
0x3f, 0x83, 0x43, 0xc8, 0x5b, 0x78, 0x67, 0x4d,
93+
0xad, 0xfc, 0x7e, 0x14, 0x6f, 0x88, 0x2b, 0x4f
94+
};
95+
96+
uint8_t expected_shared[X25519_KEY_SIZE] = {
97+
0x4a, 0x5d, 0x9d, 0x5b, 0xa4, 0xce, 0x2d, 0xe1,
98+
0x72, 0x8e, 0x3b, 0xf4, 0x80, 0x35, 0x0f, 0x25,
99+
0xe0, 0x7e, 0x21, 0xc9, 0x47, 0xd1, 0x9e, 0x33,
100+
0x76, 0xf0, 0x9b, 0x3c, 0x1e, 0x16, 0x17, 0x42
101+
};
102+
103+
104+
uint8_t shared1[X25519_SHARED_SIZE];
105+
uint8_t shared2[X25519_SHARED_SIZE];
106+
107+
compact_x25519_shared(shared1, alice_sec, bob_pub);
108+
compact_x25519_shared(shared2, bob_sec, alice_pub);
109+
110+
if (memcmp(shared1, shared2, X25519_SHARED_SIZE) != 0) {
111+
printf("Fail\n");
112+
r--;
113+
}
114+
115+
if (memcmp(shared1, expected_shared, X25519_KEY_SIZE) != 0) {
116+
printf("Fail\n");
117+
r--;
118+
}
119+
if (r == 0) printf("Success\n");
120+
return r;
121+
}
64122
#endif
65123

66124
#ifndef COMPACT_DISABLE_ED25519
@@ -105,6 +163,7 @@ int main(void) {
105163

106164
int result = 0;
107165
#ifndef COMPACT_DISABLE_X25519
166+
testX25519WithRfc7748Vectors();
108167
for (int i = 0; i < 5; i++) {
109168
result += testX25519(&rng);
110169
}

0 commit comments

Comments
 (0)