descriptionLibrary and command line tool to detect SHA-1 collision in a file
homepage URLhttps://shattered.it/
repository URLhttps://github.com/cr-marcstevens/sha1collisiondetection.git
ownermackyle@gmail.com
last changeWed, 9 Dec 2020 08:03:52 +0000 (9 00:03 -0800)
last refreshSat, 4 May 2024 04:53:01 +0000 (4 06:53 +0200)
content tags
add:
README.md

sha1collisiondetection

Library and command line tool to detect SHA-1 collisions in files

Copyright 2017 Marc Stevens <marc@marc-stevens.nl>

Distributed under the MIT Software License.

See accompanying file LICENSE.txt or copy at https://opensource.org/licenses/MIT.

Developers

About

This library and command line tool were designed as near drop-in replacements for common SHA-1 libraries and sha1sum. They will compute the SHA-1 hash of any given file and additionally will detect cryptanalytic collision attacks against SHA-1 present in each file. It is very fast and takes less than twice the amount of time as regular SHA-1.

More specifically they will detect any cryptanalytic collision attack against SHA-1 using any of the top 32 SHA-1 disturbance vectors with probability 1:

    I(43,0), I(44,0), I(45,0), I(46,0), I(47,0), I(48,0), I(49,0), I(50,0), I(51,0), I(52,0),
    I(46,2), I(47,2), I(48,2), I(49,2), I(50,2), I(51,2),
    II(45,0), II(46,0), II(47,0), II(48,0), II(49,0), II(50,0), II(51,0), II(52,0), II(53,0), II(54,0), II(55,0), II(56,0),
    II(46,2), II(49,2), II(50,2), II(51,2)

The possibility of false positives can be neglected as the probability is smaller than 2^-90.

The library supports both an indicator flag that applications can check and act on, as well as a special safe-hash mode that returns the real SHA-1 hash when no collision was detected and a different safe hash when a collision was detected. Colliding files will have the same SHA-1 hash, but will have different unpredictable safe-hashes. This essentially enables protection of applications against SHA-1 collisions with no further changes in the application, e.g., digital signature forgeries based on SHA-1 collisions automatically become invalid.

For the theoretical explanation of collision detection see the award-winning paper on Counter-Cryptanalysis:

Counter-cryptanalysis, Marc Stevens, CRYPTO 2013, Lecture Notes in Computer Science, vol. 8042, Springer, 2013, pp. 129-146, https://marc-stevens.nl/research/papers/C13-S.pdf

Compiling

Run:

make

Command-line usage

There are two programs bin/sha1dcsum and bin/sha1dcsum_partialcoll. The first program bin/sha1dcsum will detect and warn for files that were generated with a cryptanalytic SHA-1 collision attack, like the one documented at https://shattered.io/ as well as the later derived attack https://sha-mbles.github.io/. The second program bin/sha1dcsum_partialcoll will detect and warn for files that were generated with a cryptanalytic collision attack against reduced-round SHA-1 (of which there are a few examples so far).

Examples:

bin/sha1dcsum test/sha1_reducedsha_coll.bin test/shattered-1.pdf
bin/sha1dcsum_partialcoll test/sha1reducedsha_coll.bin test/shattered-1.pdf
pipe_data | bin/sha1dcsum -

Library usage

See the documentation in lib/sha1.h. Here is a simple example code snippet:

#include <sha1dc/sha1.h>

SHA1_CTX ctx;
unsigned char hash[20];
SHA1DCInit(&ctx);

/** disable safe-hash mode (safe-hash mode is enabled by default) **/
// SHA1DCSetSafeHash(&ctx, 0);
/** disable use of unavoidable attack conditions to speed up detection (enabled by default) **/
// SHA1DCSetUseUBC(&ctx, 0);

SHA1DCUpdate(&ctx, buffer, (unsigned)(size));

int iscoll = SHA1DCFinal(hash,&ctx);
if (iscoll)
    printf("collision detected");
else
    printf("no collision detected");

Inclusion in other programs

In order to make it easier to include these sources in other project there are several preprocessor macros that the code uses. Rather than copy/pasting and customizing or specializing the code, first see if setting any of these defines appropriately will allow you to avoid modifying the code yourself.

This code will try to auto-detect certain things based on CPU/platform. Unless you're running on some really obscure CPU or porting to a new platform you should not need to tweak this. If you do please open an issue at https://github.com/cr-marcstevens/sha1collisiondetection

shortlog
2020-12-09 Dan ShumowMerge pull request #63 from timgates42/bugfix_typo_embeddedmaster
2020-12-06 Tim Gatesdocs: fix simple typo, embeded -> embedded
2020-01-09 cr-marcstevensDocumented SHA-mbles example colliding files
2019-05-09 Michael OsipovDetect endianess on HP-UX
2019-03-12 cr-marcstevensShorter version of force aligned access.
2019-03-12 cr-marcstevensAdd compiler option to force aligned access even for...
2018-08-02 Dan ShumowMerge pull request #45 from avar/aix-big-endian-detection
2018-08-01 Ævar Arnfjörð... Big Endian Detection: Add a whitelist of always BE OSs
2018-02-16 Ævar Arnfjörð... sha1c: fix a trivial spelling error
2017-07-01 Dan ShumowMerge pull request #37 from avar/fixup-pull-request-34
2017-07-01 SODA Noriyukiinclude <sys/types.h> explicitly if __unix__, to make...
2017-07-01 SODA NoriyukiBig Endian Detection: fix breakage on *BSD & newlib
2017-07-01 SODA Noriyukiuse #elif to make the nesting level of #if-#endif shallower
2017-06-27 Dan ShumowMerge pull request #36 from avar/bigend-detect-solaris...
2017-06-27 Ævar Arnfjörð... Big Endian detection: stop special-casing Solaris
2017-06-27 Ævar Arnfjörð... Rewrite Big Endian detection (again)
...
tags
7 years ago stable-v1.0.3
7 years ago stable-v1.0.2
7 years ago development-v1.0.1
heads
3 years ago master
6 years ago simd