mercurial/thirdparty/sha1dc/README.md
author Pulkit Goyal <7895pulkit@gmail.com>
Sat, 08 Aug 2020 15:48:17 +0530
changeset 45391 e58e234096de
parent 44057 7dddc2251365
permissions -rw-r--r--
repository: introduce constant for treemanifest requirement and use it In future we will like to much cleaner logic around which requirement is for working copy and which can go in store. To start with that, we first need to de-clutter the requirement values spread around and replace them with constants. Differential Revision: https://phab.mercurial-scm.org/D8916
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
44057
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
     1
# sha1collisiondetection
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
     2
Library and command line tool to detect SHA-1 collisions in files
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
     3
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
     4
Copyright 2017 Marc Stevens <marc@marc-stevens.nl>
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
     5
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
     6
Distributed under the MIT Software License.
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
     7
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
     8
See accompanying file LICENSE.txt or copy at https://opensource.org/licenses/MIT.
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
     9
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    10
## Developers
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    11
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    12
- Marc Stevens, CWI Amsterdam (https://marc-stevens.nl)
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    13
- Dan Shumow, Microsoft Research (https://www.microsoft.com/en-us/research/people/danshu/)
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    14
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    15
## About
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    16
This library and command line tool were designed as near drop-in replacements for common SHA-1 libraries and sha1sum.
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    17
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.
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    18
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    19
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:
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    20
```
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    21
    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),
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    22
    I(46,2), I(47,2), I(48,2), I(49,2), I(50,2), I(51,2),
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    23
    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),
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    24
    II(46,2), II(49,2), II(50,2), II(51,2)
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    25
```
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    26
The possibility of false positives can be neglected as the probability is smaller than 2^-90.
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    27
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    28
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.
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    29
Colliding files will have the same SHA-1 hash, but will have different unpredictable safe-hashes.
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    30
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.
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    31
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    32
For the theoretical explanation of collision detection see the award-winning paper on _Counter-Cryptanalysis_:
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    33
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    34
Counter-cryptanalysis, Marc Stevens, CRYPTO 2013, Lecture Notes in Computer Science, vol. 8042, Springer, 2013, pp. 129-146,
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    35
https://marc-stevens.nl/research/papers/C13-S.pdf
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    36
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    37
## Compiling
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    38
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    39
Run:
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    40
```
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    41
make
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    42
```
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    43
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    44
## Command-line usage
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    45
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    46
There are two programs `bin/sha1dcsum` and `bin/sha1dcsum_partialcoll`.
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    47
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/.
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    48
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).
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    49
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    50
Examples:
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    51
```
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    52
bin/sha1dcsum test/sha1_reducedsha_coll.bin test/shattered-1.pdf
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    53
bin/sha1dcsum_partialcoll test/sha1reducedsha_coll.bin test/shattered-1.pdf
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    54
pipe_data | bin/sha1dcsum -
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    55
```
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    56
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    57
## Library usage
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    58
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    59
See the documentation in `lib/sha1.h`. Here is a simple example code snippet:
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    60
```
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    61
#include <sha1dc/sha1.h>
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    62
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    63
SHA1_CTX ctx;
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    64
unsigned char hash[20];
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    65
SHA1DCInit(&ctx);
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    66
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    67
/** disable safe-hash mode (safe-hash mode is enabled by default) **/
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    68
// SHA1DCSetSafeHash(&ctx, 0);
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    69
/** disable use of unavoidable attack conditions to speed up detection (enabled by default) **/
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    70
// SHA1DCSetUseUBC(&ctx, 0); 
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    71
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    72
SHA1DCUpdate(&ctx, buffer, (unsigned)(size));
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    73
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    74
int iscoll = SHA1DCFinal(hash,&ctx);
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    75
if (iscoll)
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    76
    printf("collision detected");
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    77
else
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    78
    printf("no collision detected");
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    79
```
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    80
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    81
## Inclusion in other programs
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    82
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    83
In order to make it easier to include these sources in other project
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    84
there are several preprocessor macros that the code uses. Rather than
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    85
copy/pasting and customizing or specializing the code, first see if
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    86
setting any of these defines appropriately will allow you to avoid
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    87
modifying the code yourself.
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    88
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    89
- SHA1DC_NO_STANDARD_INCLUDES
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    90
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    91
 Skips including standard headers. Use this if your project for
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    92
 whatever reason wishes to do its own header includes.
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    93
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    94
- SHA1DC_CUSTOM_INCLUDE_SHA1_C
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    95
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    96
  Includes a custom header at the top of sha1.c. Usually this would be
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    97
  set in conjunction with SHA1DC_NO_STANDARD_INCLUDES to point to a
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    98
  header file which includes various standard headers.
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
    99
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
   100
- SHA1DC_INIT_SAFE_HASH_DEFAULT
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
   101
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
   102
  Sets the default for safe_hash in SHA1DCInit(). Valid values are 0
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
   103
  and 1. If unset 1 is the default.
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
   104
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
   105
- SHA1DC_CUSTOM_TRAILING_INCLUDE_SHA1_C
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
   106
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
   107
  Includes a custom trailer in sha1.c. Useful for any extra utility
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
   108
  functions that make use of the functions already defined in sha1.c.
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
   109
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
   110
- SHA1DC_CUSTOM_TRAILING_INCLUDE_SHA1_H
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
   111
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
   112
  Includes a custom trailer in sha1.h. Useful for defining the
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
   113
  prototypes of the functions or code included by
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
   114
  SHA1DC_CUSTOM_TRAILING_INCLUDE_SHA1_C.
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
   115
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
   116
- SHA1DC_CUSTOM_INCLUDE_UBC_CHECK_C
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
   117
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
   118
  Includes a custom header at the top of ubc_check.c.
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
   119
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
   120
- SHA1DC_CUSTOM_TRAILING_INCLUDE_UBC_CHECK_C
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
   121
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
   122
  Includes a custom trailer in ubc_check.c.
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
   123
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
   124
- SHA1DC_CUSTOM_TRAILING_INCLUDE_UBC_CHECK_H
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
   125
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
   126
  Includes a custom trailer in ubc_check.H.
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
   127
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
   128
This code will try to auto-detect certain things based on
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
   129
CPU/platform. Unless you're running on some really obscure CPU or
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
   130
porting to a new platform you should not need to tweak this. If you do
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
   131
please open an issue at
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
   132
https://github.com/cr-marcstevens/sha1collisiondetection
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
   133
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
   134
- SHA1DC_FORCE_LITTLEENDIAN / SHA1DC_FORCE_BIGENDIAN
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
   135
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
   136
  Override the check for processor endianenss and force either
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
   137
  Little-Endian or Big-Endian.
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
   138
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
   139
- SHA1DC_FORCE_UNALIGNED_ACCESS
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
   140
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
   141
  Permit unaligned access. This will fail on e.g. SPARC processors, so
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
   142
  it's only permitted on a whitelist of processors. If your CPU isn't
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
   143
  detected as allowing this, and allows unaligned access, setting this
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
   144
  may improve performance (or make it worse, if the kernel has to
7dddc2251365 sha1dc: import latest version from github
Augie Fackler <augie@google.com>
parents:
diff changeset
   145
  catch and emulate such access on its own).