commit f2daaf5c7808fc8178427b2ad0bfcd8417ca6eaa from: Omar Polo date: Fri Jul 12 19:22:48 2024 UTC bump hash size and add an algo field to got_object_id commit - 7a968c0ed25628a1375e92fd6e10da7485f6ea06 commit + f2daaf5c7808fc8178427b2ad0bfcd8417ca6eaa blob - 2274686ae2246b9d7b4294fbd041ff854de59b8e blob + 84bb15514c656e502ae8af2671a92fe155d3d7ff --- include/got_object.h +++ include/got_object.h @@ -14,7 +14,8 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#define GOT_OBJECT_ID_HEX_MAXLEN SHA1_DIGEST_STRING_LENGTH +#define GOT_OBJECT_ID_MAXLEN SHA256_DIGEST_LENGTH +#define GOT_OBJECT_ID_HEX_MAXLEN SHA256_DIGEST_STRING_LENGTH enum got_hash_algorithm { GOT_HASH_SHA1, @@ -22,7 +23,8 @@ enum got_hash_algorithm { }; struct got_object_id { - u_int8_t hash[SHA1_DIGEST_LENGTH]; + uint8_t hash[GOT_OBJECT_ID_MAXLEN]; + enum got_hash_algorithm algo; }; struct got_blob_object; blob - 858b723ec18cc35148de23dbc170ed12cff46f72 blob + d91f248fe08f8bda24931834c38c29dfd8d08fd2 --- lib/hash.c +++ lib/hash.c @@ -46,7 +46,9 @@ int got_object_id_cmp(const struct got_object_id *id1, const struct got_object_id *id2) { - return memcmp(id1->hash, id2->hash, SHA1_DIGEST_LENGTH); + if (id1->algo != id2->algo) + abort(); + return memcmp(id1->hash, id2->hash, got_hash_digest_length(id1->algo)); } const struct got_error * @@ -170,7 +172,11 @@ got_parse_hash_digest(uint8_t *digest, const char *lin char * got_object_id_hex(struct got_object_id *id, char *buf, size_t len) { - return got_sha1_digest_to_str(id->hash, buf, len); + if (id->algo == GOT_HASH_SHA1) + return got_sha1_digest_to_str(id->hash, buf, len); + if (id->algo == GOT_HASH_SHA256) + return got_sha256_digest_to_str(id->hash, buf, len); + abort(); } int @@ -178,11 +184,7 @@ got_parse_object_id(struct got_object_id *id, const ch enum got_hash_algorithm algo) { memset(id, 0, sizeof(*id)); - - /* XXX: temporary until we grow got_object_id */ - if (algo != GOT_HASH_SHA1) - return 0; - + id->algo = algo; return got_parse_hash_digest(id->hash, line, algo); } @@ -220,10 +222,11 @@ void got_hash_final_object_id(struct got_hash *hash, struct got_object_id *id) { memset(id, 0, sizeof(*id)); + id->algo = hash->algo; if (hash->algo == GOT_HASH_SHA1) SHA1Final(id->hash, &hash->sha1_ctx); else - abort(); + SHA256Final(id->hash, &hash->sha256_ctx); } int blob - c26d62c16a4a7b6b393af5f80b4f7e30a369c8c6 blob + 19ffa25a90493a0b5593265ee761f8231c2f40f6 --- lib/object_open_io.c +++ lib/object_open_io.c @@ -565,7 +565,8 @@ open_tree(struct got_tree_object **tree, err = got_error(GOT_ERR_NO_SPACE); goto done; } - memcpy(te->id.hash, pe->id, SHA1_DIGEST_LENGTH); + memcpy(te->id.hash, pe->id, pe->idlen); + te->id.algo = pe->algo; te->mode = pe->mode; te->idx = i; } blob - 20b5ff506329860f970e4834b4b5ec6f9d3e38f4 blob + 13da36f4db4623f60a2b68897885e715fba89ba1 --- lib/object_parse.c +++ lib/object_parse.c @@ -255,7 +255,7 @@ got_object_read_raw(uint8_t **outbuf, off_t *size, siz *size = 0; *hdrlen = 0; - got_hash_init(&ctx, GOT_HASH_SHA1); + got_hash_init(&ctx, expected_id->algo); memset(&csum, 0, sizeof(csum)); csum.output_ctx = &ctx; @@ -710,7 +710,7 @@ got_object_read_commit(struct got_commit_object **comm struct got_hash ctx; struct got_object_id id; - got_hash_init(&ctx, GOT_HASH_SHA1); + got_hash_init(&ctx, expected_id->algo); memset(&csum, 0, sizeof(csum)); csum.output_ctx = &ctx; @@ -741,7 +741,7 @@ got_object_read_commit(struct got_commit_object **comm /* Skip object header. */ len -= obj->hdrlen; err = got_object_parse_commit(commit, p + obj->hdrlen, len, - GOT_HASH_SHA1); + expected_id->algo); done: free(p); if (obj) @@ -886,7 +886,7 @@ got_object_read_tree(struct got_parsed_tree_entry **en struct got_hash ctx; struct got_object_id id; - got_hash_init(&ctx, GOT_HASH_SHA1); + got_hash_init(&ctx, expected_id->algo); memset(&csum, 0, sizeof(csum)); csum.output_ctx = &ctx; @@ -912,7 +912,7 @@ got_object_read_tree(struct got_parsed_tree_entry **en /* Skip object header. */ len -= obj->hdrlen; err = got_object_parse_tree(entries, nentries, nentries_alloc, - *p + obj->hdrlen, len, GOT_HASH_SHA1); + *p + obj->hdrlen, len, expected_id->algo); done: if (obj) got_object_close(obj); @@ -1128,7 +1128,7 @@ got_object_read_tag(struct got_tag_object **tag, int f struct got_hash ctx; struct got_object_id id; - got_hash_init(&ctx, GOT_HASH_SHA1); + got_hash_init(&ctx, expected_id->algo); memset(&csum, 0, sizeof(csum)); csum.output_ctx = &ctx; @@ -1155,7 +1155,7 @@ got_object_read_tag(struct got_tag_object **tag, int f /* Skip object header. */ len -= obj->hdrlen; err = got_object_parse_tag(tag, p + obj->hdrlen, len, - GOT_HASH_SHA1); + expected_id->algo); done: free(p); if (obj) blob - 3e7306c44c58ef596f2c30790e4687ac2d89468e blob + 64aa579280aef2def21118ceff79ba3c010a6db7 --- lib/pack.c +++ lib/pack.c @@ -665,6 +665,7 @@ got_packidx_get_object_id(struct got_object_id *id, oid = packidx->hdr.sorted_ids + idx * digest_len; memcpy(id->hash, oid, digest_len); + id->algo = packidx->algo; return NULL; } @@ -713,6 +714,7 @@ got_packidx_match_id_str_prefix(struct got_object_id_q if (err) return err; memcpy(qid->id.hash, oid, digest_len); + qid->id.algo = packidx->algo; STAILQ_INSERT_TAIL(matched_ids, qid, entry); oid = packidx->hdr.sorted_ids + (++i) * digest_len; blob - 420df55900fae5048926e340c76737b0480284d2 blob + f709a6f1ece7b45517e779175f7eda6b5e6b513c --- lib/pack_index.c +++ lib/pack_index.c @@ -514,6 +514,7 @@ add_indexed_object(struct got_packidx *packidx, uint32 oid = packidx->hdr.sorted_ids + idx * digest_len; memcpy(oid, obj->id.hash, digest_len); + obj->id.algo = packidx->algo; packidx->hdr.crc32[idx] = htobe32(obj->crc); if (obj->off < GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) packidx->hdr.offsets[idx] = htobe32(obj->off); blob - ca34316533171eaaffebc13cf14010ae8e32d87c blob + 16b07ccae25800aac74814a8f28dc862b140de76 --- lib/privsep.c +++ lib/privsep.c @@ -1585,7 +1585,9 @@ recv_tree_entries(void *data, size_t datalen, struct g te_name = buf + sizeof(ite); memcpy(te->name, te_name, ite.namelen); te->name[ite.namelen] = '\0'; - memcpy(te->id.hash, ite.id, sizeof(te->id.sha1)); + memcpy(te->id.hash, ite.id, sizeof(te->id.hash)); + memcpy(te->id.hash, ite.id, got_hash_digest_length(ite.algo)); + te->id.algo = ite.algo; te->mode = ite.mode; te->idx = *nentries; (*nentries)++; blob - dca64c5b3e52082226f276cca9afe7e6a997004e blob + d39172980ea798fa07c161a3cecc055dfc76136c --- lib/repository.c +++ lib/repository.c @@ -1236,7 +1236,8 @@ got_repo_check_packidx_bloom_filter(struct got_reposit bf = get_packidx_bloom_filter(repo, path_packidx, strlen(path_packidx)); if (bf) - return bloom_check(bf->bloom, id->hash, sizeof(id->hash)); + return bloom_check(bf->bloom, id->hash, + got_hash_digest_length(id->algo)); /* No bloom filter means this pack index must be searched. */ return 1; blob - f70fd48fd09091b36d366da7f2e938a4cf92b691 blob + 66d664aa6393a67bea7096826a3412604ea954ef --- lib/repository_admin.c +++ lib/repository_admin.c @@ -573,6 +573,7 @@ got_repo_list_pack(FILE *packfile, struct got_object_i break; } oid = packidx->hdr.sorted_ids + i * digest_len; + id.algo = repo->algo; memcpy(id.hash, oid, digest_len); offset = got_packidx_get_object_offset(packidx, i); @@ -1288,6 +1289,7 @@ pack_is_redundant(int *redundant, struct got_repositor memset(&id, 0, sizeof(id)); memcpy(&id.hash, pid, digest_len); + id.algo = repo->algo; if (got_object_idset_contains(idset, &id)) continue; blob - b3be4749f4694271e276c0162905dccddf9cc21d blob + bace23fd1f0b2eb7a3e6033e9af5afea04a7d486 --- libexec/got-read-blob/got-read-blob.c +++ libexec/got-read-blob/got-read-blob.c @@ -81,10 +81,6 @@ main(int argc, char *argv[]) struct got_inflate_checksum csum; struct got_hash ctx; - got_hash_init(&ctx, GOT_HASH_SHA1); - memset(&csum, 0, sizeof(csum)); - csum.output_ctx = &ctx; - memset(&imsg, 0, sizeof(imsg)); memset(&imsg_outfd, 0, sizeof(imsg_outfd)); @@ -163,6 +159,10 @@ main(int argc, char *argv[]) } fd = -1; + got_hash_init(&ctx, expected_id.algo); + memset(&csum, 0, sizeof(csum)); + csum.output_ctx = &ctx; + if (obj->size + obj->hdrlen <= GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) { err = got_inflate_to_mem(&buf, &size, NULL, &csum, f); blob - f08ec067641ea4c9b844f9d5ff590d976d692e18 blob + 5e1ec6efa75876a9ed9e5f0aea1a6975639cbbfc --- libexec/got-read-pack/got-read-pack.c +++ libexec/got-read-pack/got-read-pack.c @@ -247,7 +247,7 @@ tree_request(struct imsg *imsg, struct imsgbuf *ibuf, return err; err = got_object_parse_tree(entries, nentries, nentries_alloc, - buf, len, GOT_HASH_SHA1); + buf, len, id.algo); if (err) goto done; @@ -424,7 +424,7 @@ tag_request(struct imsg *imsg, struct imsgbuf *ibuf, s goto done; obj->size = len; - err = got_object_parse_tag(&tag, buf, len, GOT_HASH_SHA1); + err = got_object_parse_tag(&tag, buf, len, id.algo); if (err) goto done; @@ -559,7 +559,8 @@ tree_path_changed(int *changed, uint8_t **buf1, size_t struct got_object_id id1, id2; int idx; - memcpy(id1.hash, pte1.id, SHA1_DIGEST_LENGTH); + memcpy(id1.hash, pte1.id, pte1.idlen); + id1.algo = pack->algo; idx = got_packidx_get_object_idx(packidx, &id1); if (idx == -1) { err = got_error_no_obj(&id1); @@ -576,6 +577,7 @@ tree_path_changed(int *changed, uint8_t **buf1, size_t remain1 = *len1; memcpy(id2.hash, pte2.id, SHA1_DIGEST_LENGTH); + id2.algo = pack->algo; idx = got_packidx_get_object_idx(packidx, &id2); if (idx == -1) { err = got_error_no_obj(&id2); @@ -1276,6 +1278,7 @@ enumerate_tree(int *have_all_entries, struct imsgbuf * err = got_object_qid_alloc_partial(&eqid); if (err) goto done; + eqid->id.algo = pte->algo; memcpy(eqid->id.hash, pte->id, sizeof(eqid->id.hash)); if (got_object_idset_contains(idset, &eqid->id)) { @@ -1469,7 +1472,7 @@ enumeration_request(struct imsg *imsg, struct imsgbuf goto done; obj->size = len; err = got_object_parse_tag(&tag, buf, len, - GOT_HASH_SHA1); + qid->id.algo); if (err) { free(buf); goto done;