doc hash-function-transition: clarify how older gits die on NewHash
[alt-git.git] / Documentation / technical / hash-function-transition.txt
blob34396f13eca95ffcf4eba02582cce974d3c3e0a6
1 Git hash function transition
2 ============================
4 Objective
5 ---------
6 Migrate Git from SHA-1 to a stronger hash function.
8 Background
9 ----------
10 At its core, the Git version control system is a content addressable
11 filesystem. It uses the SHA-1 hash function to name content. For
12 example, files, directories, and revisions are referred to by hash
13 values unlike in other traditional version control systems where files
14 or versions are referred to via sequential numbers. The use of a hash
15 function to address its content delivers a few advantages:
17 * Integrity checking is easy. Bit flips, for example, are easily
18   detected, as the hash of corrupted content does not match its name.
19 * Lookup of objects is fast.
21 Using a cryptographically secure hash function brings additional
22 advantages:
24 * Object names can be signed and third parties can trust the hash to
25   address the signed object and all objects it references.
26 * Communication using Git protocol and out of band communication
27   methods have a short reliable string that can be used to reliably
28   address stored content.
30 Over time some flaws in SHA-1 have been discovered by security
31 researchers. https://shattered.io demonstrated a practical SHA-1 hash
32 collision. As a result, SHA-1 cannot be considered cryptographically
33 secure any more. This impacts the communication of hash values because
34 we cannot trust that a given hash value represents the known good
35 version of content that the speaker intended.
37 SHA-1 still possesses the other properties such as fast object lookup
38 and safe error checking, but other hash functions are equally suitable
39 that are believed to be cryptographically secure.
41 Goals
42 -----
43 Where NewHash is a strong 256-bit hash function to replace SHA-1 (see
44 "Selection of a New Hash", below):
46 1. The transition to NewHash can be done one local repository at a time.
47    a. Requiring no action by any other party.
48    b. A NewHash repository can communicate with SHA-1 Git servers
49       (push/fetch).
50    c. Users can use SHA-1 and NewHash identifiers for objects
51       interchangeably (see "Object names on the command line", below).
52    d. New signed objects make use of a stronger hash function than
53       SHA-1 for their security guarantees.
54 2. Allow a complete transition away from SHA-1.
55    a. Local metadata for SHA-1 compatibility can be removed from a
56       repository if compatibility with SHA-1 is no longer needed.
57 3. Maintainability throughout the process.
58    a. The object format is kept simple and consistent.
59    b. Creation of a generalized repository conversion tool.
61 Non-Goals
62 ---------
63 1. Add NewHash support to Git protocol. This is valuable and the
64    logical next step but it is out of scope for this initial design.
65 2. Transparently improving the security of existing SHA-1 signed
66    objects.
67 3. Intermixing objects using multiple hash functions in a single
68    repository.
69 4. Taking the opportunity to fix other bugs in Git's formats and
70    protocols.
71 5. Shallow clones and fetches into a NewHash repository. (This will
72    change when we add NewHash support to Git protocol.)
73 6. Skip fetching some submodules of a project into a NewHash
74    repository. (This also depends on NewHash support in Git
75    protocol.)
77 Overview
78 --------
79 We introduce a new repository format extension. Repositories with this
80 extension enabled use NewHash instead of SHA-1 to name their objects.
81 This affects both object names and object content --- both the names
82 of objects and all references to other objects within an object are
83 switched to the new hash function.
85 NewHash repositories cannot be read by older versions of Git.
87 Alongside the packfile, a NewHash repository stores a bidirectional
88 mapping between NewHash and SHA-1 object names. The mapping is generated
89 locally and can be verified using "git fsck". Object lookups use this
90 mapping to allow naming objects using either their SHA-1 and NewHash names
91 interchangeably.
93 "git cat-file" and "git hash-object" gain options to display an object
94 in its sha1 form and write an object given its sha1 form. This
95 requires all objects referenced by that object to be present in the
96 object database so that they can be named using the appropriate name
97 (using the bidirectional hash mapping).
99 Fetches from a SHA-1 based server convert the fetched objects into
100 NewHash form and record the mapping in the bidirectional mapping table
101 (see below for details). Pushes to a SHA-1 based server convert the
102 objects being pushed into sha1 form so the server does not have to be
103 aware of the hash function the client is using.
105 Detailed Design
106 ---------------
107 Repository format extension
108 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
109 A NewHash repository uses repository format version `1` (see
110 Documentation/technical/repository-version.txt) with extensions
111 `objectFormat` and `compatObjectFormat`:
113         [core]
114                 repositoryFormatVersion = 1
115         [extensions]
116                 objectFormat = newhash
117                 compatObjectFormat = sha1
119 The combination of setting `core.repositoryFormatVersion=1` and
120 populating `extensions.*` ensures that all versions of Git later than
121 `v0.99.9l` will die instead of trying to operate on the NewHash
122 repository, instead producing an error message.
124         # Between v0.99.9l and v2.7.0
125         $ git status
126         fatal: Expected git repo version <= 0, found 1
127         # After v2.7.0
128         $ git status
129         fatal: unknown repository extensions found:
130                 objectformat
131                 compatobjectformat
133 See the "Transition plan" section below for more details on these
134 repository extensions.
136 Object names
137 ~~~~~~~~~~~~
138 Objects can be named by their 40 hexadecimal digit sha1-name or 64
139 hexadecimal digit newhash-name, plus names derived from those (see
140 gitrevisions(7)).
142 The sha1-name of an object is the SHA-1 of the concatenation of its
143 type, length, a nul byte, and the object's sha1-content. This is the
144 traditional <sha1> used in Git to name objects.
146 The newhash-name of an object is the NewHash of the concatenation of its
147 type, length, a nul byte, and the object's newhash-content.
149 Object format
150 ~~~~~~~~~~~~~
151 The content as a byte sequence of a tag, commit, or tree object named
152 by sha1 and newhash differ because an object named by newhash-name refers to
153 other objects by their newhash-names and an object named by sha1-name
154 refers to other objects by their sha1-names.
156 The newhash-content of an object is the same as its sha1-content, except
157 that objects referenced by the object are named using their newhash-names
158 instead of sha1-names. Because a blob object does not refer to any
159 other object, its sha1-content and newhash-content are the same.
161 The format allows round-trip conversion between newhash-content and
162 sha1-content.
164 Object storage
165 ~~~~~~~~~~~~~~
166 Loose objects use zlib compression and packed objects use the packed
167 format described in Documentation/technical/pack-format.txt, just like
168 today. The content that is compressed and stored uses newhash-content
169 instead of sha1-content.
171 Pack index
172 ~~~~~~~~~~
173 Pack index (.idx) files use a new v3 format that supports multiple
174 hash functions. They have the following format (all integers are in
175 network byte order):
177 - A header appears at the beginning and consists of the following:
178   - The 4-byte pack index signature: '\377t0c'
179   - 4-byte version number: 3
180   - 4-byte length of the header section, including the signature and
181     version number
182   - 4-byte number of objects contained in the pack
183   - 4-byte number of object formats in this pack index: 2
184   - For each object format:
185     - 4-byte format identifier (e.g., 'sha1' for SHA-1)
186     - 4-byte length in bytes of shortened object names. This is the
187       shortest possible length needed to make names in the shortened
188       object name table unambiguous.
189     - 4-byte integer, recording where tables relating to this format
190       are stored in this index file, as an offset from the beginning.
191   - 4-byte offset to the trailer from the beginning of this file.
192   - Zero or more additional key/value pairs (4-byte key, 4-byte
193     value). Only one key is supported: 'PSRC'. See the "Loose objects
194     and unreachable objects" section for supported values and how this
195     is used.  All other keys are reserved. Readers must ignore
196     unrecognized keys.
197 - Zero or more NUL bytes. This can optionally be used to improve the
198   alignment of the full object name table below.
199 - Tables for the first object format:
200   - A sorted table of shortened object names.  These are prefixes of
201     the names of all objects in this pack file, packed together
202     without offset values to reduce the cache footprint of the binary
203     search for a specific object name.
205   - A table of full object names in pack order. This allows resolving
206     a reference to "the nth object in the pack file" (from a
207     reachability bitmap or from the next table of another object
208     format) to its object name.
210   - A table of 4-byte values mapping object name order to pack order.
211     For an object in the table of sorted shortened object names, the
212     value at the corresponding index in this table is the index in the
213     previous table for that same object.
215     This can be used to look up the object in reachability bitmaps or
216     to look up its name in another object format.
218   - A table of 4-byte CRC32 values of the packed object data, in the
219     order that the objects appear in the pack file. This is to allow
220     compressed data to be copied directly from pack to pack during
221     repacking without undetected data corruption.
223   - A table of 4-byte offset values. For an object in the table of
224     sorted shortened object names, the value at the corresponding
225     index in this table indicates where that object can be found in
226     the pack file. These are usually 31-bit pack file offsets, but
227     large offsets are encoded as an index into the next table with the
228     most significant bit set.
230   - A table of 8-byte offset entries (empty for pack files less than
231     2 GiB). Pack files are organized with heavily used objects toward
232     the front, so most object references should not need to refer to
233     this table.
234 - Zero or more NUL bytes.
235 - Tables for the second object format, with the same layout as above,
236   up to and not including the table of CRC32 values.
237 - Zero or more NUL bytes.
238 - The trailer consists of the following:
239   - A copy of the 20-byte NewHash checksum at the end of the
240     corresponding packfile.
242   - 20-byte NewHash checksum of all of the above.
244 Loose object index
245 ~~~~~~~~~~~~~~~~~~
246 A new file $GIT_OBJECT_DIR/loose-object-idx contains information about
247 all loose objects. Its format is
249   # loose-object-idx
250   (newhash-name SP sha1-name LF)*
252 where the object names are in hexadecimal format. The file is not
253 sorted.
255 The loose object index is protected against concurrent writes by a
256 lock file $GIT_OBJECT_DIR/loose-object-idx.lock. To add a new loose
257 object:
259 1. Write the loose object to a temporary file, like today.
260 2. Open loose-object-idx.lock with O_CREAT | O_EXCL to acquire the lock.
261 3. Rename the loose object into place.
262 4. Open loose-object-idx with O_APPEND and write the new object
263 5. Unlink loose-object-idx.lock to release the lock.
265 To remove entries (e.g. in "git pack-refs" or "git-prune"):
267 1. Open loose-object-idx.lock with O_CREAT | O_EXCL to acquire the
268    lock.
269 2. Write the new content to loose-object-idx.lock.
270 3. Unlink any loose objects being removed.
271 4. Rename to replace loose-object-idx, releasing the lock.
273 Translation table
274 ~~~~~~~~~~~~~~~~~
275 The index files support a bidirectional mapping between sha1-names
276 and newhash-names. The lookup proceeds similarly to ordinary object
277 lookups. For example, to convert a sha1-name to a newhash-name:
279  1. Look for the object in idx files. If a match is present in the
280     idx's sorted list of truncated sha1-names, then:
281     a. Read the corresponding entry in the sha1-name order to pack
282        name order mapping.
283     b. Read the corresponding entry in the full sha1-name table to
284        verify we found the right object. If it is, then
285     c. Read the corresponding entry in the full newhash-name table.
286        That is the object's newhash-name.
287  2. Check for a loose object. Read lines from loose-object-idx until
288     we find a match.
290 Step (1) takes the same amount of time as an ordinary object lookup:
291 O(number of packs * log(objects per pack)). Step (2) takes O(number of
292 loose objects) time. To maintain good performance it will be necessary
293 to keep the number of loose objects low. See the "Loose objects and
294 unreachable objects" section below for more details.
296 Since all operations that make new objects (e.g., "git commit") add
297 the new objects to the corresponding index, this mapping is possible
298 for all objects in the object store.
300 Reading an object's sha1-content
301 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
302 The sha1-content of an object can be read by converting all newhash-names
303 its newhash-content references to sha1-names using the translation table.
305 Fetch
306 ~~~~~
307 Fetching from a SHA-1 based server requires translating between SHA-1
308 and NewHash based representations on the fly.
310 SHA-1s named in the ref advertisement that are present on the client
311 can be translated to NewHash and looked up as local objects using the
312 translation table.
314 Negotiation proceeds as today. Any "have"s generated locally are
315 converted to SHA-1 before being sent to the server, and SHA-1s
316 mentioned by the server are converted to NewHash when looking them up
317 locally.
319 After negotiation, the server sends a packfile containing the
320 requested objects. We convert the packfile to NewHash format using
321 the following steps:
323 1. index-pack: inflate each object in the packfile and compute its
324    SHA-1. Objects can contain deltas in OBJ_REF_DELTA format against
325    objects the client has locally. These objects can be looked up
326    using the translation table and their sha1-content read as
327    described above to resolve the deltas.
328 2. topological sort: starting at the "want"s from the negotiation
329    phase, walk through objects in the pack and emit a list of them,
330    excluding blobs, in reverse topologically sorted order, with each
331    object coming later in the list than all objects it references.
332    (This list only contains objects reachable from the "wants". If the
333    pack from the server contained additional extraneous objects, then
334    they will be discarded.)
335 3. convert to newhash: open a new (newhash) packfile. Read the topologically
336    sorted list just generated. For each object, inflate its
337    sha1-content, convert to newhash-content, and write it to the newhash
338    pack. Record the new sha1<->newhash mapping entry for use in the idx.
339 4. sort: reorder entries in the new pack to match the order of objects
340    in the pack the server generated and include blobs. Write a newhash idx
341    file
342 5. clean up: remove the SHA-1 based pack file, index, and
343    topologically sorted list obtained from the server in steps 1
344    and 2.
346 Step 3 requires every object referenced by the new object to be in the
347 translation table. This is why the topological sort step is necessary.
349 As an optimization, step 1 could write a file describing what non-blob
350 objects each object it has inflated from the packfile references. This
351 makes the topological sort in step 2 possible without inflating the
352 objects in the packfile for a second time. The objects need to be
353 inflated again in step 3, for a total of two inflations.
355 Step 4 is probably necessary for good read-time performance. "git
356 pack-objects" on the server optimizes the pack file for good data
357 locality (see Documentation/technical/pack-heuristics.txt).
359 Details of this process are likely to change. It will take some
360 experimenting to get this to perform well.
362 Push
363 ~~~~
364 Push is simpler than fetch because the objects referenced by the
365 pushed objects are already in the translation table. The sha1-content
366 of each object being pushed can be read as described in the "Reading
367 an object's sha1-content" section to generate the pack written by git
368 send-pack.
370 Signed Commits
371 ~~~~~~~~~~~~~~
372 We add a new field "gpgsig-newhash" to the commit object format to allow
373 signing commits without relying on SHA-1. It is similar to the
374 existing "gpgsig" field. Its signed payload is the newhash-content of the
375 commit object with any "gpgsig" and "gpgsig-newhash" fields removed.
377 This means commits can be signed
378 1. using SHA-1 only, as in existing signed commit objects
379 2. using both SHA-1 and NewHash, by using both gpgsig-newhash and gpgsig
380    fields.
381 3. using only NewHash, by only using the gpgsig-newhash field.
383 Old versions of "git verify-commit" can verify the gpgsig signature in
384 cases (1) and (2) without modifications and view case (3) as an
385 ordinary unsigned commit.
387 Signed Tags
388 ~~~~~~~~~~~
389 We add a new field "gpgsig-newhash" to the tag object format to allow
390 signing tags without relying on SHA-1. Its signed payload is the
391 newhash-content of the tag with its gpgsig-newhash field and "-----BEGIN PGP
392 SIGNATURE-----" delimited in-body signature removed.
394 This means tags can be signed
395 1. using SHA-1 only, as in existing signed tag objects
396 2. using both SHA-1 and NewHash, by using gpgsig-newhash and an in-body
397    signature.
398 3. using only NewHash, by only using the gpgsig-newhash field.
400 Mergetag embedding
401 ~~~~~~~~~~~~~~~~~~
402 The mergetag field in the sha1-content of a commit contains the
403 sha1-content of a tag that was merged by that commit.
405 The mergetag field in the newhash-content of the same commit contains the
406 newhash-content of the same tag.
408 Submodules
409 ~~~~~~~~~~
410 To convert recorded submodule pointers, you need to have the converted
411 submodule repository in place. The translation table of the submodule
412 can be used to look up the new hash.
414 Loose objects and unreachable objects
415 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
416 Fast lookups in the loose-object-idx require that the number of loose
417 objects not grow too high.
419 "git gc --auto" currently waits for there to be 6700 loose objects
420 present before consolidating them into a packfile. We will need to
421 measure to find a more appropriate threshold for it to use.
423 "git gc --auto" currently waits for there to be 50 packs present
424 before combining packfiles. Packing loose objects more aggressively
425 may cause the number of pack files to grow too quickly. This can be
426 mitigated by using a strategy similar to Martin Fick's exponential
427 rolling garbage collection script:
428 https://gerrit-review.googlesource.com/c/gerrit/+/35215
430 "git gc" currently expels any unreachable objects it encounters in
431 pack files to loose objects in an attempt to prevent a race when
432 pruning them (in case another process is simultaneously writing a new
433 object that refers to the about-to-be-deleted object). This leads to
434 an explosion in the number of loose objects present and disk space
435 usage due to the objects in delta form being replaced with independent
436 loose objects.  Worse, the race is still present for loose objects.
438 Instead, "git gc" will need to move unreachable objects to a new
439 packfile marked as UNREACHABLE_GARBAGE (using the PSRC field; see
440 below). To avoid the race when writing new objects referring to an
441 about-to-be-deleted object, code paths that write new objects will
442 need to copy any objects from UNREACHABLE_GARBAGE packs that they
443 refer to to new, non-UNREACHABLE_GARBAGE packs (or loose objects).
444 UNREACHABLE_GARBAGE are then safe to delete if their creation time (as
445 indicated by the file's mtime) is long enough ago.
447 To avoid a proliferation of UNREACHABLE_GARBAGE packs, they can be
448 combined under certain circumstances. If "gc.garbageTtl" is set to
449 greater than one day, then packs created within a single calendar day,
450 UTC, can be coalesced together. The resulting packfile would have an
451 mtime before midnight on that day, so this makes the effective maximum
452 ttl the garbageTtl + 1 day. If "gc.garbageTtl" is less than one day,
453 then we divide the calendar day into intervals one-third of that ttl
454 in duration. Packs created within the same interval can be coalesced
455 together. The resulting packfile would have an mtime before the end of
456 the interval, so this makes the effective maximum ttl equal to the
457 garbageTtl * 4/3.
459 This rule comes from Thirumala Reddy Mutchukota's JGit change
460 https://git.eclipse.org/r/90465.
462 The UNREACHABLE_GARBAGE setting goes in the PSRC field of the pack
463 index. More generally, that field indicates where a pack came from:
465  - 1 (PACK_SOURCE_RECEIVE) for a pack received over the network
466  - 2 (PACK_SOURCE_AUTO) for a pack created by a lightweight
467    "gc --auto" operation
468  - 3 (PACK_SOURCE_GC) for a pack created by a full gc
469  - 4 (PACK_SOURCE_UNREACHABLE_GARBAGE) for potential garbage
470    discovered by gc
471  - 5 (PACK_SOURCE_INSERT) for locally created objects that were
472    written directly to a pack file, e.g. from "git add ."
474 This information can be useful for debugging and for "gc --auto" to
475 make appropriate choices about which packs to coalesce.
477 Caveats
478 -------
479 Invalid objects
480 ~~~~~~~~~~~~~~~
481 The conversion from sha1-content to newhash-content retains any
482 brokenness in the original object (e.g., tree entry modes encoded with
483 leading 0, tree objects whose paths are not sorted correctly, and
484 commit objects without an author or committer). This is a deliberate
485 feature of the design to allow the conversion to round-trip.
487 More profoundly broken objects (e.g., a commit with a truncated "tree"
488 header line) cannot be converted but were not usable by current Git
489 anyway.
491 Shallow clone and submodules
492 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
493 Because it requires all referenced objects to be available in the
494 locally generated translation table, this design does not support
495 shallow clone or unfetched submodules. Protocol improvements might
496 allow lifting this restriction.
498 Alternates
499 ~~~~~~~~~~
500 For the same reason, a newhash repository cannot borrow objects from a
501 sha1 repository using objects/info/alternates or
502 $GIT_ALTERNATE_OBJECT_REPOSITORIES.
504 git notes
505 ~~~~~~~~~
506 The "git notes" tool annotates objects using their sha1-name as key.
507 This design does not describe a way to migrate notes trees to use
508 newhash-names. That migration is expected to happen separately (for
509 example using a file at the root of the notes tree to describe which
510 hash it uses).
512 Server-side cost
513 ~~~~~~~~~~~~~~~~
514 Until Git protocol gains NewHash support, using NewHash based storage
515 on public-facing Git servers is strongly discouraged. Once Git
516 protocol gains NewHash support, NewHash based servers are likely not
517 to support SHA-1 compatibility, to avoid what may be a very expensive
518 hash reencode during clone and to encourage peers to modernize.
520 The design described here allows fetches by SHA-1 clients of a
521 personal NewHash repository because it's not much more difficult than
522 allowing pushes from that repository. This support needs to be guarded
523 by a configuration option --- servers like git.kernel.org that serve a
524 large number of clients would not be expected to bear that cost.
526 Meaning of signatures
527 ~~~~~~~~~~~~~~~~~~~~~
528 The signed payload for signed commits and tags does not explicitly
529 name the hash used to identify objects. If some day Git adopts a new
530 hash function with the same length as the current SHA-1 (40
531 hexadecimal digit) or NewHash (64 hexadecimal digit) objects then the
532 intent behind the PGP signed payload in an object signature is
533 unclear:
535         object e7e07d5a4fcc2a203d9873968ad3e6bd4d7419d7
536         type commit
537         tag v2.12.0
538         tagger Junio C Hamano <gitster@pobox.com> 1487962205 -0800
540         Git 2.12
542 Does this mean Git v2.12.0 is the commit with sha1-name
543 e7e07d5a4fcc2a203d9873968ad3e6bd4d7419d7 or the commit with
544 new-40-digit-hash-name e7e07d5a4fcc2a203d9873968ad3e6bd4d7419d7?
546 Fortunately NewHash and SHA-1 have different lengths. If Git starts
547 using another hash with the same length to name objects, then it will
548 need to change the format of signed payloads using that hash to
549 address this issue.
551 Object names on the command line
552 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
553 To support the transition (see Transition plan below), this design
554 supports four different modes of operation:
556  1. ("dark launch") Treat object names input by the user as SHA-1 and
557     convert any object names written to output to SHA-1, but store
558     objects using NewHash.  This allows users to test the code with no
559     visible behavior change except for performance.  This allows
560     allows running even tests that assume the SHA-1 hash function, to
561     sanity-check the behavior of the new mode.
563  2. ("early transition") Allow both SHA-1 and NewHash object names in
564     input. Any object names written to output use SHA-1. This allows
565     users to continue to make use of SHA-1 to communicate with peers
566     (e.g. by email) that have not migrated yet and prepares for mode 3.
568  3. ("late transition") Allow both SHA-1 and NewHash object names in
569     input. Any object names written to output use NewHash. In this
570     mode, users are using a more secure object naming method by
571     default.  The disruption is minimal as long as most of their peers
572     are in mode 2 or mode 3.
574  4. ("post-transition") Treat object names input by the user as
575     NewHash and write output using NewHash. This is safer than mode 3
576     because there is less risk that input is incorrectly interpreted
577     using the wrong hash function.
579 The mode is specified in configuration.
581 The user can also explicitly specify which format to use for a
582 particular revision specifier and for output, overriding the mode. For
583 example:
585 git --output-format=sha1 log abac87a^{sha1}..f787cac^{newhash}
587 Selection of a New Hash
588 -----------------------
589 In early 2005, around the time that Git was written,  Xiaoyun Wang,
590 Yiqun Lisa Yin, and Hongbo Yu announced an attack finding SHA-1
591 collisions in 2^69 operations. In August they published details.
592 Luckily, no practical demonstrations of a collision in full SHA-1 were
593 published until 10 years later, in 2017.
595 The hash function NewHash to replace SHA-1 should be stronger than
596 SHA-1 was: we would like it to be trustworthy and useful in practice
597 for at least 10 years.
599 Some other relevant properties:
601 1. A 256-bit hash (long enough to match common security practice; not
602    excessively long to hurt performance and disk usage).
604 2. High quality implementations should be widely available (e.g. in
605    OpenSSL).
607 3. The hash function's properties should match Git's needs (e.g. Git
608    requires collision and 2nd preimage resistance and does not require
609    length extension resistance).
611 4. As a tiebreaker, the hash should be fast to compute (fortunately
612    many contenders are faster than SHA-1).
614 Some hashes under consideration are SHA-256, SHA-512/256, SHA-256x16,
615 K12, and BLAKE2bp-256.
617 Transition plan
618 ---------------
619 Some initial steps can be implemented independently of one another:
620 - adding a hash function API (vtable)
621 - teaching fsck to tolerate the gpgsig-newhash field
622 - excluding gpgsig-* from the fields copied by "git commit --amend"
623 - annotating tests that depend on SHA-1 values with a SHA1 test
624   prerequisite
625 - using "struct object_id", GIT_MAX_RAWSZ, and GIT_MAX_HEXSZ
626   consistently instead of "unsigned char *" and the hardcoded
627   constants 20 and 40.
628 - introducing index v3
629 - adding support for the PSRC field and safer object pruning
632 The first user-visible change is the introduction of the objectFormat
633 extension (without compatObjectFormat). This requires:
634 - implementing the loose-object-idx
635 - teaching fsck about this mode of operation
636 - using the hash function API (vtable) when computing object names
637 - signing objects and verifying signatures
638 - rejecting attempts to fetch from or push to an incompatible
639   repository
641 Next comes introduction of compatObjectFormat:
642 - translating object names between object formats
643 - translating object content between object formats
644 - generating and verifying signatures in the compat format
645 - adding appropriate index entries when adding a new object to the
646   object store
647 - --output-format option
648 - ^{sha1} and ^{newhash} revision notation
649 - configuration to specify default input and output format (see
650   "Object names on the command line" above)
652 The next step is supporting fetches and pushes to SHA-1 repositories:
653 - allow pushes to a repository using the compat format
654 - generate a topologically sorted list of the SHA-1 names of fetched
655   objects
656 - convert the fetched packfile to newhash format and generate an idx
657   file
658 - re-sort to match the order of objects in the fetched packfile
660 The infrastructure supporting fetch also allows converting an existing
661 repository. In converted repositories and new clones, end users can
662 gain support for the new hash function without any visible change in
663 behavior (see "dark launch" in the "Object names on the command line"
664 section). In particular this allows users to verify NewHash signatures
665 on objects in the repository, and it should ensure the transition code
666 is stable in production in preparation for using it more widely.
668 Over time projects would encourage their users to adopt the "early
669 transition" and then "late transition" modes to take advantage of the
670 new, more futureproof NewHash object names.
672 When objectFormat and compatObjectFormat are both set, commands
673 generating signatures would generate both SHA-1 and NewHash signatures
674 by default to support both new and old users.
676 In projects using NewHash heavily, users could be encouraged to adopt
677 the "post-transition" mode to avoid accidentally making implicit use
678 of SHA-1 object names.
680 Once a critical mass of users have upgraded to a version of Git that
681 can verify NewHash signatures and have converted their existing
682 repositories to support verifying them, we can add support for a
683 setting to generate only NewHash signatures. This is expected to be at
684 least a year later.
686 That is also a good moment to advertise the ability to convert
687 repositories to use NewHash only, stripping out all SHA-1 related
688 metadata. This improves performance by eliminating translation
689 overhead and security by avoiding the possibility of accidentally
690 relying on the safety of SHA-1.
692 Updating Git's protocols to allow a server to specify which hash
693 functions it supports is also an important part of this transition. It
694 is not discussed in detail in this document but this transition plan
695 assumes it happens. :)
697 Alternatives considered
698 -----------------------
699 Upgrading everyone working on a particular project on a flag day
700 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
701 Projects like the Linux kernel are large and complex enough that
702 flipping the switch for all projects based on the repository at once
703 is infeasible.
705 Not only would all developers and server operators supporting
706 developers have to switch on the same flag day, but supporting tooling
707 (continuous integration, code review, bug trackers, etc) would have to
708 be adapted as well. This also makes it difficult to get early feedback
709 from some project participants testing before it is time for mass
710 adoption.
712 Using hash functions in parallel
713 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
714 (e.g. https://public-inbox.org/git/22708.8913.864049.452252@chiark.greenend.org.uk/ )
715 Objects newly created would be addressed by the new hash, but inside
716 such an object (e.g. commit) it is still possible to address objects
717 using the old hash function.
718 * You cannot trust its history (needed for bisectability) in the
719   future without further work
720 * Maintenance burden as the number of supported hash functions grows
721   (they will never go away, so they accumulate). In this proposal, by
722   comparison, converted objects lose all references to SHA-1.
724 Signed objects with multiple hashes
725 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
726 Instead of introducing the gpgsig-newhash field in commit and tag objects
727 for newhash-content based signatures, an earlier version of this design
728 added "hash newhash <newhash-name>" fields to strengthen the existing
729 sha1-content based signatures.
731 In other words, a single signature was used to attest to the object
732 content using both hash functions. This had some advantages:
733 * Using one signature instead of two speeds up the signing process.
734 * Having one signed payload with both hashes allows the signer to
735   attest to the sha1-name and newhash-name referring to the same object.
736 * All users consume the same signature. Broken signatures are likely
737   to be detected quickly using current versions of git.
739 However, it also came with disadvantages:
740 * Verifying a signed object requires access to the sha1-names of all
741   objects it references, even after the transition is complete and
742   translation table is no longer needed for anything else. To support
743   this, the design added fields such as "hash sha1 tree <sha1-name>"
744   and "hash sha1 parent <sha1-name>" to the newhash-content of a signed
745   commit, complicating the conversion process.
746 * Allowing signed objects without a sha1 (for after the transition is
747   complete) complicated the design further, requiring a "nohash sha1"
748   field to suppress including "hash sha1" fields in the newhash-content
749   and signed payload.
751 Lazily populated translation table
752 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
753 Some of the work of building the translation table could be deferred to
754 push time, but that would significantly complicate and slow down pushes.
755 Calculating the sha1-name at object creation time at the same time it is
756 being streamed to disk and having its newhash-name calculated should be
757 an acceptable cost.
759 Document History
760 ----------------
762 2017-03-03
763 bmwill@google.com, jonathantanmy@google.com, jrnieder@gmail.com,
764 sbeller@google.com
766 Initial version sent to
767 http://public-inbox.org/git/20170304011251.GA26789@aiede.mtv.corp.google.com
769 2017-03-03 jrnieder@gmail.com
770 Incorporated suggestions from jonathantanmy and sbeller:
771 * describe purpose of signed objects with each hash type
772 * redefine signed object verification using object content under the
773   first hash function
775 2017-03-06 jrnieder@gmail.com
776 * Use SHA3-256 instead of SHA2 (thanks, Linus and brian m. carlson).[1][2]
777 * Make sha3-based signatures a separate field, avoiding the need for
778   "hash" and "nohash" fields (thanks to peff[3]).
779 * Add a sorting phase to fetch (thanks to Junio for noticing the need
780   for this).
781 * Omit blobs from the topological sort during fetch (thanks to peff).
782 * Discuss alternates, git notes, and git servers in the caveats
783   section (thanks to Junio Hamano, brian m. carlson[4], and Shawn
784   Pearce).
785 * Clarify language throughout (thanks to various commenters,
786   especially Junio).
788 2017-09-27 jrnieder@gmail.com, sbeller@google.com
789 * use placeholder NewHash instead of SHA3-256
790 * describe criteria for picking a hash function.
791 * include a transition plan (thanks especially to Brandon Williams
792   for fleshing these ideas out)
793 * define the translation table (thanks, Shawn Pearce[5], Jonathan
794   Tan, and Masaya Suzuki)
795 * avoid loose object overhead by packing more aggressively in
796   "git gc --auto"
798 [1] http://public-inbox.org/git/CA+55aFzJtejiCjV0e43+9oR3QuJK2PiFiLQemytoLpyJWe6P9w@mail.gmail.com/
799 [2] http://public-inbox.org/git/CA+55aFz+gkAsDZ24zmePQuEs1XPS9BP_s8O7Q4wQ7LV7X5-oDA@mail.gmail.com/
800 [3] http://public-inbox.org/git/20170306084353.nrns455dvkdsfgo5@sigill.intra.peff.net/
801 [4] http://public-inbox.org/git/20170304224936.rqqtkdvfjgyezsht@genre.crustytoothpaste.net
802 [5] https://public-inbox.org/git/CAJo=hJtoX9=AyLHHpUJS7fueV9ciZ_MNpnEPHUz8Whui6g9F0A@mail.gmail.com/