Skip to content

Commit fabec2c

Browse files
bk2204gitster
authored andcommitted
builtin/receive-pack: switch to use the_hash_algo
The push cert code uses HMAC-SHA-1 to create a nonce. This is a secure use of SHA-1 which is not affected by its collision resistance (or lack thereof). However, it makes sense for us to use a better algorithm if one is available, one which may even be more performant. Futhermore, until we have specialized functions for computing the hex value of an arbitrary function, it simplifies the code greatly to use the same hash algorithm everywhere. Switch this code to use GIT_MAX_BLKSZ and the_hash_algo for computing the push cert nonce, and rename the hmac_sha1 function to simply "hmac". Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent f6af19a commit fabec2c

File tree

1 file changed

+21
-23
lines changed

1 file changed

+21
-23
lines changed

builtin/receive-pack.c

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -417,24 +417,22 @@ static int copy_to_sideband(int in, int out, void *arg)
417417
return 0;
418418
}
419419

420-
#define HMAC_BLOCK_SIZE 64
421-
422-
static void hmac_sha1(unsigned char *out,
420+
static void hmac(unsigned char *out,
423421
const char *key_in, size_t key_len,
424422
const char *text, size_t text_len)
425423
{
426-
unsigned char key[HMAC_BLOCK_SIZE];
427-
unsigned char k_ipad[HMAC_BLOCK_SIZE];
428-
unsigned char k_opad[HMAC_BLOCK_SIZE];
424+
unsigned char key[GIT_MAX_BLKSZ];
425+
unsigned char k_ipad[GIT_MAX_BLKSZ];
426+
unsigned char k_opad[GIT_MAX_BLKSZ];
429427
int i;
430-
git_SHA_CTX ctx;
428+
git_hash_ctx ctx;
431429

432430
/* RFC 2104 2. (1) */
433-
memset(key, '\0', HMAC_BLOCK_SIZE);
434-
if (HMAC_BLOCK_SIZE < key_len) {
435-
git_SHA1_Init(&ctx);
436-
git_SHA1_Update(&ctx, key_in, key_len);
437-
git_SHA1_Final(key, &ctx);
431+
memset(key, '\0', GIT_MAX_BLKSZ);
432+
if (the_hash_algo->blksz < key_len) {
433+
the_hash_algo->init_fn(&ctx);
434+
the_hash_algo->update_fn(&ctx, key_in, key_len);
435+
the_hash_algo->final_fn(key, &ctx);
438436
} else {
439437
memcpy(key, key_in, key_len);
440438
}
@@ -446,29 +444,29 @@ static void hmac_sha1(unsigned char *out,
446444
}
447445

448446
/* RFC 2104 2. (3) & (4) */
449-
git_SHA1_Init(&ctx);
450-
git_SHA1_Update(&ctx, k_ipad, sizeof(k_ipad));
451-
git_SHA1_Update(&ctx, text, text_len);
452-
git_SHA1_Final(out, &ctx);
447+
the_hash_algo->init_fn(&ctx);
448+
the_hash_algo->update_fn(&ctx, k_ipad, sizeof(k_ipad));
449+
the_hash_algo->update_fn(&ctx, text, text_len);
450+
the_hash_algo->final_fn(out, &ctx);
453451

454452
/* RFC 2104 2. (6) & (7) */
455-
git_SHA1_Init(&ctx);
456-
git_SHA1_Update(&ctx, k_opad, sizeof(k_opad));
457-
git_SHA1_Update(&ctx, out, GIT_SHA1_RAWSZ);
458-
git_SHA1_Final(out, &ctx);
453+
the_hash_algo->init_fn(&ctx);
454+
the_hash_algo->update_fn(&ctx, k_opad, sizeof(k_opad));
455+
the_hash_algo->update_fn(&ctx, out, the_hash_algo->rawsz);
456+
the_hash_algo->final_fn(out, &ctx);
459457
}
460458

461459
static char *prepare_push_cert_nonce(const char *path, timestamp_t stamp)
462460
{
463461
struct strbuf buf = STRBUF_INIT;
464-
unsigned char sha1[GIT_SHA1_RAWSZ];
462+
unsigned char hash[GIT_MAX_RAWSZ];
465463

466464
strbuf_addf(&buf, "%s:%"PRItime, path, stamp);
467-
hmac_sha1(sha1, buf.buf, buf.len, cert_nonce_seed, strlen(cert_nonce_seed));
465+
hmac(hash, buf.buf, buf.len, cert_nonce_seed, strlen(cert_nonce_seed));
468466
strbuf_release(&buf);
469467

470468
/* RFC 2104 5. HMAC-SHA1-80 */
471-
strbuf_addf(&buf, "%"PRItime"-%.*s", stamp, GIT_SHA1_HEXSZ, sha1_to_hex(sha1));
469+
strbuf_addf(&buf, "%"PRItime"-%.*s", stamp, (int)the_hash_algo->hexsz, sha1_to_hex(hash));
472470
return strbuf_detach(&buf, NULL);
473471
}
474472

0 commit comments

Comments
 (0)