interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / Documentation / technical / hash-function-transition.adoc

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