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>
brian m. carlson committed
Aug 18, 2019 at 20:04 UTC
fabec2c5c33b2623fe405c40ffcdeb838e97ed26
1 file changed
+21
-23
builtin/receive-pack.c
+21
-23
@@ -417,24 +417,22 @@ static int copy_to_sideband(int in, int out, void *arg)
417
return 0;
418
}
419
420
-#define HMAC_BLOCK_SIZE 64
421
-
422
-static void hmac_sha1(unsigned char *out,
420
+static void hmac(unsigned char *out,
421
const char *key_in, size_t key_len,
422
const char *text, size_t text_len)
423
{
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];
427
int i;
430
- git_SHA_CTX ctx;
428
+ git_hash_ctx ctx;
429
430
/* 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);
436
} else {
437
memcpy(key, key_in, key_len);
438
}
@@ -446,29 +444,29 @@ static void hmac_sha1(unsigned char *out,
444
}
445
446
/* 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);
451
452
/* 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);
457
}
458
459
static char *prepare_push_cert_nonce(const char *path, timestamp_t stamp)
460
{
461
struct strbuf buf = STRBUF_INIT;
464
- unsigned char sha1[GIT_SHA1_RAWSZ];
462
+ unsigned char hash[GIT_MAX_RAWSZ];
463
464
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));
466
strbuf_release(&buf);
467
468
/* 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));
470
return strbuf_detach(&buf, NULL);
471
}
472