Skip to content

Commit d5c3488

Browse files
committed
Merge branch 'bc/constant-memequal' into pu
* bc/constant-memequal: builtin/receive-pack: use constant-time comparison for HMAC value
2 parents 07b9d9a + edc6dcc commit d5c3488

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

builtin/receive-pack.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,12 +518,25 @@ static char *find_header(const char *msg, size_t len, const char *key,
518518
return NULL;
519519
}
520520

521+
/*
522+
* Return zero if a and b are equal up to n bytes and nonzero if they are not.
523+
* This operation is guaranteed to run in constant time to avoid leaking data.
524+
*/
525+
static int constant_memequal(const char *a, const char *b, size_t n)
526+
{
527+
int res = 0;
528+
for (size_t i = 0; i < n; i++)
529+
res |= a[i] ^ b[i];
530+
return res;
531+
}
532+
521533
static const char *check_nonce(const char *buf, size_t len)
522534
{
523535
char *nonce = find_header(buf, len, "nonce", NULL);
524536
timestamp_t stamp, ostamp;
525537
char *bohmac, *expect = NULL;
526538
const char *retval = NONCE_BAD;
539+
size_t noncelen;
527540

528541
if (!nonce) {
529542
retval = NONCE_MISSING;
@@ -565,8 +578,14 @@ static const char *check_nonce(const char *buf, size_t len)
565578
goto leave;
566579
}
567580

581+
noncelen = strlen(nonce);
568582
expect = prepare_push_cert_nonce(service_dir, stamp);
569-
if (strcmp(expect, nonce)) {
583+
if (noncelen != strlen(expect)) {
584+
/* This is not even the right size. */
585+
retval = NONCE_BAD;
586+
goto leave;
587+
}
588+
if (constant_memequal(expect, nonce, noncelen)) {
570589
/* Not what we would have signed earlier */
571590
retval = NONCE_BAD;
572591
goto leave;

0 commit comments

Comments
 (0)