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>
This commit is contained in:
brian m. carlson 2019-08-18 20:04:05 +00:00 committed by Junio C Hamano
parent f6af19a9ad
commit fabec2c5c3

View File

@ -417,24 +417,22 @@ static int copy_to_sideband(int in, int out, void *arg)
return 0; return 0;
} }
#define HMAC_BLOCK_SIZE 64 static void hmac(unsigned char *out,
static void hmac_sha1(unsigned char *out,
const char *key_in, size_t key_len, const char *key_in, size_t key_len,
const char *text, size_t text_len) const char *text, size_t text_len)
{ {
unsigned char key[HMAC_BLOCK_SIZE]; unsigned char key[GIT_MAX_BLKSZ];
unsigned char k_ipad[HMAC_BLOCK_SIZE]; unsigned char k_ipad[GIT_MAX_BLKSZ];
unsigned char k_opad[HMAC_BLOCK_SIZE]; unsigned char k_opad[GIT_MAX_BLKSZ];
int i; int i;
git_SHA_CTX ctx; git_hash_ctx ctx;
/* RFC 2104 2. (1) */ /* RFC 2104 2. (1) */
memset(key, '\0', HMAC_BLOCK_SIZE); memset(key, '\0', GIT_MAX_BLKSZ);
if (HMAC_BLOCK_SIZE < key_len) { if (the_hash_algo->blksz < key_len) {
git_SHA1_Init(&ctx); the_hash_algo->init_fn(&ctx);
git_SHA1_Update(&ctx, key_in, key_len); the_hash_algo->update_fn(&ctx, key_in, key_len);
git_SHA1_Final(key, &ctx); the_hash_algo->final_fn(key, &ctx);
} else { } else {
memcpy(key, key_in, key_len); memcpy(key, key_in, key_len);
} }
@ -446,29 +444,29 @@ static void hmac_sha1(unsigned char *out,
} }
/* RFC 2104 2. (3) & (4) */ /* RFC 2104 2. (3) & (4) */
git_SHA1_Init(&ctx); the_hash_algo->init_fn(&ctx);
git_SHA1_Update(&ctx, k_ipad, sizeof(k_ipad)); the_hash_algo->update_fn(&ctx, k_ipad, sizeof(k_ipad));
git_SHA1_Update(&ctx, text, text_len); the_hash_algo->update_fn(&ctx, text, text_len);
git_SHA1_Final(out, &ctx); the_hash_algo->final_fn(out, &ctx);
/* RFC 2104 2. (6) & (7) */ /* RFC 2104 2. (6) & (7) */
git_SHA1_Init(&ctx); the_hash_algo->init_fn(&ctx);
git_SHA1_Update(&ctx, k_opad, sizeof(k_opad)); the_hash_algo->update_fn(&ctx, k_opad, sizeof(k_opad));
git_SHA1_Update(&ctx, out, GIT_SHA1_RAWSZ); the_hash_algo->update_fn(&ctx, out, the_hash_algo->rawsz);
git_SHA1_Final(out, &ctx); the_hash_algo->final_fn(out, &ctx);
} }
static char *prepare_push_cert_nonce(const char *path, timestamp_t stamp) static char *prepare_push_cert_nonce(const char *path, timestamp_t stamp)
{ {
struct strbuf buf = STRBUF_INIT; struct strbuf buf = STRBUF_INIT;
unsigned char sha1[GIT_SHA1_RAWSZ]; unsigned char hash[GIT_MAX_RAWSZ];
strbuf_addf(&buf, "%s:%"PRItime, path, stamp); strbuf_addf(&buf, "%s:%"PRItime, path, stamp);
hmac_sha1(sha1, buf.buf, buf.len, cert_nonce_seed, strlen(cert_nonce_seed)); hmac(hash, buf.buf, buf.len, cert_nonce_seed, strlen(cert_nonce_seed));
strbuf_release(&buf); strbuf_release(&buf);
/* RFC 2104 5. HMAC-SHA1-80 */ /* RFC 2104 5. HMAC-SHA1-80 */
strbuf_addf(&buf, "%"PRItime"-%.*s", stamp, GIT_SHA1_HEXSZ, sha1_to_hex(sha1)); strbuf_addf(&buf, "%"PRItime"-%.*s", stamp, (int)the_hash_algo->hexsz, sha1_to_hex(hash));
return strbuf_detach(&buf, NULL); return strbuf_detach(&buf, NULL);
} }