Library and command line tool to detect SHA-1 collision in a file

Overview

sha1collisiondetection

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

Copyright 2017 Marc Stevens [email protected]

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.

  • SHA1DC_NO_STANDARD_INCLUDES

Skips including standard headers. Use this if your project for whatever reason wishes to do its own header includes.

  • SHA1DC_CUSTOM_INCLUDE_SHA1_C

    Includes a custom header at the top of sha1.c. Usually this would be set in conjunction with SHA1DC_NO_STANDARD_INCLUDES to point to a header file which includes various standard headers.

  • SHA1DC_INIT_SAFE_HASH_DEFAULT

    Sets the default for safe_hash in SHA1DCInit(). Valid values are 0 and 1. If unset 1 is the default.

  • SHA1DC_CUSTOM_TRAILING_INCLUDE_SHA1_C

    Includes a custom trailer in sha1.c. Useful for any extra utility functions that make use of the functions already defined in sha1.c.

  • SHA1DC_CUSTOM_TRAILING_INCLUDE_SHA1_H

    Includes a custom trailer in sha1.h. Useful for defining the prototypes of the functions or code included by SHA1DC_CUSTOM_TRAILING_INCLUDE_SHA1_C.

  • SHA1DC_CUSTOM_INCLUDE_UBC_CHECK_C

    Includes a custom header at the top of ubc_check.c.

  • SHA1DC_CUSTOM_TRAILING_INCLUDE_UBC_CHECK_C

    Includes a custom trailer in ubc_check.c.

  • SHA1DC_CUSTOM_TRAILING_INCLUDE_UBC_CHECK_H

    Includes a custom trailer in ubc_check.H.

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

  • SHA1DC_FORCE_LITTLEENDIAN / SHA1DC_FORCE_BIGENDIAN

    Override the check for processor endianenss and force either Little-Endian or Big-Endian.

  • SHA1DC_FORCE_UNALIGNED_ACCESS

    Permit unaligned access. This will fail on e.g. SPARC processors, so it's only permitted on a whitelist of processors. If your CPU isn't detected as allowing this, and allows unaligned access, setting this may improve performance (or make it worse, if the kernel has to catch and emulate such access on its own).

Comments
  • commit 33a694a9ee1b broke all little endian platforms on *BSD

    commit 33a694a9ee1b broke all little endian platforms on *BSD

    Little endian platforms on all *BSD variants are broken since commit 33a694a9ee1b, because _BIG_ENDIAN is always defined even on little endian platforms.

    problem found by nonaka at NetBSD.org

    opened by n-soda 31
  • Add support for macOS

    Add support for macOS

    Compiling on macOS requires a few changes to the Makefile and some really trivial changes to some source files (only white space!) because Clang seems to be more strict to the standard.

    First issue was resolving compile errors regarding missing newline characters at the end of some source files as this is required by the standard (error: no newline at end of file).

    A slightly more complicated issue was Clang complaining about that "ISO C requires a translation unit to contain at least one declaration" which was caused by an empty source file because an #ifdef was false. This issue was resolved by only adding these source files when they are needed. Propably the defines (HAVE_MMX, HAVE_SSE, etc.) can be left out.

    Lastly a check was introduced to set compiler flags and file extensions required or specific for macOS.

    Additionally a few smaller issues in the Makefile where resolved:

    • the "bin" folder is not created (perhaps this was not noticed as the "clean" target does not remove the "bin" folder)
    • the "clean" taget now removes the "bin" folder to get a cleaner state

    I did not check if building still works on Linux. I suspect at least one change (line 137/158: change from ${LD} to ${CC}) could cause a little trouble. It would be nice if someone could check this out.

    opened by muellermartin 17
  • Makefile not portable

    Makefile not portable

    The default make that ships with the latest Xcode or Mac OS Yosemite does not honor the president going to the most specialized suffix rule: as a result the SIMD compile time flags don't get added and clang chokes trying to compile some of the avx356 code because -mavx2 doesn't get passed. See #4 for additional discussion.

    opened by zbeekman 13
  • Add support for macOS (rework)

    Add support for macOS (rework)

    This is a rework of PR #4 which was split into smaller PRs which where not related to macOS only. This PR waschanged to reflect the changes which were made in the mean time.

    Compiling on macOS requires following changes to the Makefile:

    Some checks were introduced to set compiler flags and file extensions required or specific for macOS.

    Additionally the order of general pattern rules was changed. The sets of patterns to match certain source files for different CPU features are based on suffixes in their filenames. make on Linux seems to pick the most specific rule whereas make shipped with Xcode does pick the first and therefore disregards the remaining rules. This was fixed by rearranging the rules from most specific to most general. This potentially fixes issue #10.

    I did not check if building still works on Linux. I suspect at least one change (line 137/158: change from ${LD} to ${CC}) could cause a little trouble. It would be nice if someone could check this out.

    opened by muellermartin 7
  • Fix BigEndian detection (hopefully permanently)

    Fix BigEndian detection (hopefully permanently)

    The 2.13.2 git release with the latest sha1collisiondetection master still broke on Solaris SPARC. These are two commits.

    The first one is a failed attempt (there seems to be no sun macro, despite what Oracle's docs say, maybe only the latest version?), it's left there for the git history.

    The second one rewrites the Big Endian detection completely based on my investigations into how gcc/glibc etc. and finally Solaris have exported specific macros indicating what the endianness is over time.

    This hopefully solves the Solaris bug (I've had reports from two SPARC users that it works), doesn't introduce any regressions, and makes the detection easier to understand going forward.

    opened by avar 6
  • AIX and xlc: no rule to make target 'dep_lib/ubc_check.d', needed by 'obj_lib/ubc_check.lo'

    AIX and xlc: no rule to make target 'dep_lib/ubc_check.d', needed by 'obj_lib/ubc_check.lo'

    Besides the gcc flags (see PR #41) there is also the issue that dependent files are not generated.

    [email protected]:[/data/prj/aixtools/git]git clone https://github.com/cr-marcstevens/sha1collisiondetection.git sha1test Cloning into 'sha1test'... remote: Counting objects: 864, done. remote: Total 864 (delta 0), reused 0 (delta 0), pack-reused 864 Receiving objects: 100% (864/864), 602.00 KiB | 552.00 KiB/s, done. Resolving deltas: 100% (560/560), done. [email protected]:[/data/prj/aixtools/git]cd sha1test [email protected]:[/data/prj/aixtools/git/sha1test]ls -l total 96 drwxr-sr-x 8 root felt 4096 Jul 30 06:38 .git -rw-r--r-- 1 root felt 422 Jul 30 06:38 .gitignore -rw-r--r-- 1 root felt 396 Jul 30 06:38 .travis.yml -rw-r--r-- 1 root felt 1270 Jul 30 06:38 LICENSE.txt -rw-r--r-- 1 root felt 5215 Jul 30 06:38 Makefile -rw-r--r-- 1 root felt 5786 Jul 30 06:38 README.md drwxr-sr-x 2 root felt 4096 Jul 30 06:38 lib drwxr-sr-x 2 root felt 4096 Jul 30 06:38 src drwxr-sr-x 2 root felt 4096 Jul 30 06:38 test drwxr-sr-x 4 root felt 4096 Jul 30 06:38 vs2015 [email protected]:[/data/prj/aixtools/git/sha1test]make mkdir -p dep_lib && cc -O2 -Wall -Werror -Wextra -pedantic -std=c90 -Ilib -M -MF dep_lib/sha1.d lib/sha1.c cc: 1501-210 (S) command option Wall contains an incorrect subargument mkdir -p dep_lib && cc -O2 -Wall -Werror -Wextra -pedantic -std=c90 -Ilib -M -MF dep_lib/ubc_check.d lib/ubc_check.c cc: 1501-210 (S) command option Wall contains an incorrect subargument mkdir -p dep_src && cc -O2 -Wall -Werror -Wextra -pedantic -std=c90 -Ilib -M -MF dep_src/main.d src/main.c cc: 1501-210 (S) command option Wall contains an incorrect subargument make: *** No rule to make target 'dep_lib/ubc_check.d', needed by 'obj_lib/ubc_check.lo'. Stop.

    opened by aixtools 5
  • How safe is

    How safe is "safe hash"?

    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.

    Please clarify strength of the "safe hash"? Why it's not vulnerable to SHA-1 weaknesses?

    opened by vt-alt 4
  • Detect endianess on HP-UX

    Detect endianess on HP-UX

    HP-UX is not properly detected and classified as little endian. Add test macro for HP-UX to make it big endian.

    Based on the discussion on the Git mailing list, here is the upstream patch for HP-UX detection.

    opened by michael-o 4
  • Big Endian Detection: Add a whitelist of always BE OSs

    Big Endian Detection: Add a whitelist of always BE OSs

    Hopefully fix the issue with AIX falling through this detection logic and being detected as Little Endian reported on the Git mailing list, see https://public-inbox.org/git/[email protected]/

    Attempt to solve this by extending the existing fallback we have for detecting always Big Endian processors to also detecting always Big Endian OSs. See the public-inbox link in the comment I'm adding for why this should work.

    I have not tested this myself, since I have no access to AIX. I'll be pointing Michael Felt to the relevant MR so he can test it before this is merged.

    opened by avar 4
  • Amend the lib/ code for easier inclusion in other programs

    Amend the lib/ code for easier inclusion in other programs

    This introduces no functional changes, but allows the lib/ code to be used as-is in other programs without patching these files directly.

    With these changes the git project can use this code without any modifications to the upstream code.

    This is used by a patch series I've just submitted as an RFC against git.git: https://public-inbox.org/git/[email protected]/T/#u

    Currently that patch series points at my fork, but it would be great if you could merge this (or if you'd like to have this done differently, I'm happy to change it), then projects like git could use your code as-is via either a submodule, or just by copying the code as-is with no customizations to the files themselves, only to the Makefile that makes use of them.

    opened by avar 4
  • [RESEARCH] How to make it widespread

    [RESEARCH] How to make it widespread

    Dear Marc Stevens and Dan Shumow, There are a variety of tools to calculate SHA1 that could benefit from including your library. But what’s the proper argumentation to move its authors to do that? For example, I asked RHash’s author to “reinforce SHA1 against collisions” which led to confusion.

    opened by sergeevabc 2
  • sha1.c: replace white/blacklist with allow/denylist

    sha1.c: replace white/blacklist with allow/denylist

    The words "whitelist" and "blacklist" have cultural implications that are not inclusive. The words "allowlist" and "denylist" are self-descriptive without requiring cultural implications to understand the words in context.

    A similar set of changes were sent to the Git project, but in those cases the words could be removed by rephrasing the sentences that used them, increasing clarity. In this example, the terms are used concretely enough that rephrasing would decrease clarity. Thus, a relatively mechanical replacement was applied.

    These changes only affect comment lines, nothing functional.


    I was redirected here after trying to make similar changes in the Git codebase [1], forgetting that this file is maintained in this repository.

    Thanks for considering this change!

    [1] https://lore.kernel.org/git/[email protected]

    opened by derrickstolee 0
  • Online file tester broken

    Online file tester broken

    I downloaded the sample PDFs and uploaded them to the file checker at https://shattered.io/ and it says that they're safe. Tesed on Chrome and Firefox. Wasn't sure where to post about the issue, so hopefully this works.

    opened by hoffers 0
  • Document the

    Document the "safe hash" algorithm used when a collision is detected

    It looks like the block where the collision is detected is re-compressed in some way, but I'm not enough of a C developer to be sure I understand what I'm reading. Is this documented anywhere?

    opened by jeremyBanks 1
  • Please provide the tools and data files to generate lib/ubc_check.[ch]

    Please provide the tools and data files to generate lib/ubc_check.[ch]

    These files contain the following note:

    // this file was generated by the 'parse_bitrel' program in the tools section
    // using the data files from directory 'tools/data/3565'
    

    However, that directory does not exist.

    opened by teythoon 1
Releases(stable-v1.0.3)
  • stable-v1.0.3(Mar 27, 2017)

  • stable-v1.0.2(Mar 5, 2017)

  • development-v1.0.1(Mar 4, 2017)

    This is a large cleanup version:

    • removed unused recompression functions and uses switch instead of a lookup table
    • non-api functions are now static and not exported anymore
    • compiles in c90 standard
    • removed SIMD for now: this was still WIP and still missed runtime selection code. Work on this has been moved to simd branch
    • Makefile rewrite:
      • Added install target
      • Support for libtool-less systems

    It is still marked as development for now, as naming might still change.

    Thanks to all who have contributed!

    Source code(tar.gz)
    Source code(zip)
Owner
Marc Stevens
Researcher Cryptology @ CWI, twitter: @realhashbreaker
Marc Stevens
FCracker is a command line tool designed to brute force encrypted files like zip, 7z, rar, pdf etc.

FCrack is a command-line tool designed to brute force encrypted files like zip, 7z, rar, pdf, gpg etc.

null 23 Dec 21, 2022
Distributed, Encrypted, Fractured File System - A custom distributed file system written in C with FUSE

A custom FUSE-based filesystem that distributes encrypted shards of data across machines on a local network, allowing those files to be accessible from any machine.

Charles Averill 14 Nov 2, 2022
CoinBrowser is a tool for Freqtrade where the program writes pairs into text file to be used with spesific exchange.

CoinBrowser CoinBrowser is a tool for Freqtrade where the program writes pairs into text file to be used with spesific exchange. Data for this program

null 25 Dec 14, 2022
A tool to decrypt Call of Duty: World War II's Fast File

A tool to decrypt Call of Duty: World War II's Fast File. This tool was made to allow people making HUDs in Call of Duty: Black Ops III's mod tools to aquire the assets needed to port HUDs from Call of Duty: World War II.

Philip 5 Sep 3, 2022
CVE-2021-3493 Ubuntu OverlayFS Local Privesc (Interactive Bash Shell & Execute Command Entered)

CVE-2021-3493 Ubuntu OverlayFS Local Privesc Description "Ubuntu specific issue in the overlayfs file system in the Linux kernel where it did not prop

3ND 31 Nov 9, 2022
x509cert is a tool and library for generating X.509 certificates and certificate requests.

x509cert is a tool and library for generating X.509 certificates and certificate requests. It is written in C99 and uses BearSSL to decode keys and compute signatures.

Michael Forney 10 Sep 5, 2022
Text-Crypt is a tool which encrypts and decrypts texts using a specific and certain key.

Text-Crypt is a tool which encrypts and decrypts texts using a specific and certain key. This tool uses Caesar Cypher Algorithm to encrypt and decrypt a given text.

AnonabdulJ 4 Dec 24, 2021
Parses existing Chia plotter log files and builds a .csv file containing all the important details

Chia Log Analysis Parses through Chia plotter log files and plops all the juicy details into a CSV file! Build See below for instructions if you prefe

Drew M Johnson 45 May 10, 2022
A useful tool for identifying the architecture, platform type, compiler, and operating system specifications by preprocessor feature support.

Platform-Detector Cross-Platform Information Detector It is a useful tool for identifying the architecture, platform type, compiler, and operating sys

Kambiz Asadzadeh 10 Jul 27, 2022
BlackDex is an Android unpack(dexdump) tool, it supports Android 5.0~12 and need not rely to any environment.

BlackDex is an Android unpack(dexdump) tool, it supports Android 5.0~12 and need not rely to any environment. BlackDex can run on any Android mobile phones or emulators, you can unpack APK File in several seconds.

null 4.3k Jan 8, 2023
A Powerful, Easy-to-Use, Compact, Cross-Platform and Installation-Free Crypto Tool. 一个强大,易用,小巧,跨平台且免安装的加密解密签名工具。

GpgFrontend GpgFrontend is a Powerful, Easy-to-Use, Compact, Cross-Platform, and Installation-Free OpenPGP Crypto Tool. By using GpgFrontend, you can

Saturn&Eric 203 Jan 7, 2023
Stegreg is a steganography tool made in C++ that encrypt and hide your data inside an image.

Stegreg Introduction Stegreg is a steganography tool made in C++ that encrypt and hide your data inside an image. Installation git clone https://githu

0UR4N05 29 Dec 11, 2022
This tool demonstrates the power of UAC bypasses and built-in features of Windows.

Auto-Elevate This tool demonstrates the power of UAC bypasses and built-in features of Windows. This utility auto-locates winlogon.exe, steals and imp

null 129 Dec 7, 2022
a header-file-only, SHA256 hash generator in C++

PicoSHA2 - a C++ SHA256 hash generator Copyright © 2017 okdshin Introduction PicoSHA2 is a tiny SHA256 hash generator for C++ with following propertie

Shintarou Okada 531 Dec 29, 2022
Transparent file encryption in git

git-crypt - transparent file encryption in git git-crypt enables transparent encryption and decryption of files in a git repository. Files which you c

Andrew Ayer 7k Dec 30, 2022
A collection of public domain/unlicense single-file cryptography

simple-crypto A collection of single-file public domain/unlicense cryptographic functions in different programming languages. Feel free to copy-paste

null 4 Sep 24, 2022
Rubicon - a New Custom Encryption Algorithm/Tool

Rubicon - a New Custom Encryption Algorithm/Tool Disclaimer DO NOT use this project for purposes other than legitimate red teaming/pentesting jobs

null 27 Dec 13, 2022
RdpCacheStitcher is a tool that supports forensic analysts in reconstructing useful images out of RDP cache bitmaps.

RdpCacheStitcher is a tool that supports forensic analysts in reconstructing useful images out of RDP cache bitmaps. Using raw RDP cache tile bitmaps extracted by tools like e.g. ANSSI's BMC-Tools as input, it provides a graphical user interface and several placement heuristics for stitching tiles together so that meaningful images or even full screenshots can be reconstructed.

Bundesamt für Sicherheit in der Informationstechnik 176 Jan 2, 2023
UnrealKey is a tool for automatically finding the AES-256 decryption keys for Unreal Engine 4 encrypted pak files.

UnrealKey UnrealKey is a tool for automatically finding the AES-256 decryption keys for Unreal Engine 4 encrypted pak files.

Devin Acker 39 Dec 17, 2022