Skip to content

Commit b2dc366

Browse files
committed
Fail ecdh_gen_secret when the two sides derive different secrets
1 parent 095f519 commit b2dc366

1 file changed

Lines changed: 14 additions & 4 deletions

File tree

pk/ecdh_generate_secret/ecdh_gen_secret.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,11 @@ int do_ecc(void)
139139
if (ret == 0) {
140140
/* Use a constant-time compare (not XMEMCMP) since these buffers
141141
* hold secret key material. See const_time_memcmp() above. */
142-
if (const_time_memcmp(AliceSecret, BobSecret, secretLen))
142+
if (const_time_memcmp(AliceSecret, BobSecret, secretLen)) {
143143
printf("Failed to generate a common secret\n");
144+
ret = -1;
145+
goto all_three;
146+
}
144147
} else {
145148
goto all_three;
146149
}
@@ -202,8 +205,11 @@ int do_25519(void)
202205
if (ret == 0) {
203206
/* Use a constant-time compare (not XMEMCMP) since these buffers
204207
* hold secret key material. See const_time_memcmp() above. */
205-
if (const_time_memcmp(AliceSecret, BobSecret, secretLen))
208+
if (const_time_memcmp(AliceSecret, BobSecret, secretLen)) {
206209
printf("Failed to generate a common secret\n");
210+
ret = -1;
211+
goto all_three;
212+
}
207213
} else {
208214
goto all_three;
209215
}
@@ -265,8 +271,11 @@ int do_448(void)
265271
if (ret == 0) {
266272
/* Use a constant-time compare (not XMEMCMP) since these buffers
267273
* hold secret key material. See const_time_memcmp() above. */
268-
if (const_time_memcmp(AliceSecret, BobSecret, secretLen))
274+
if (const_time_memcmp(AliceSecret, BobSecret, secretLen)) {
269275
printf("Failed to generate a common secret\n");
276+
ret = -1;
277+
goto all_three;
278+
}
270279
} else {
271280
goto all_three;
272281
}
@@ -285,7 +294,8 @@ int do_448(void)
285294
printf("Configure wolfssl with --enable-curve448 and try again\n");
286295
ret = -1;
287296
#endif
288-
return ret;
297+
/* not ret: an exit code is masked to 8 bits, so -256 would read as 0 */
298+
return (ret == 0) ? 0 : 1;
289299
}
290300

291301
void print_secret(char* who, byte* s, int sLen)

0 commit comments

Comments
 (0)