sha1.h 4.02 KB
Newer Older
1 2 3 4 5 6 7
/***
* Copyright 2017 Marc Stevens <marc@marc-stevens.nl>, Dan Shumow <danshu@microsoft.com>
* Distributed under the MIT Software License.
* See accompanying file LICENSE.txt or copy at
* https://opensource.org/licenses/MIT
***/

8 9 10
#ifndef SHA1DC_SHA1_H
#define SHA1DC_SHA1_H

11 12 13 14
#if defined(__cplusplus)
extern "C" {
#endif

15
#ifndef SHA1DC_NO_STANDARD_INCLUDES
16
#include <stdint.h>
17
#endif
18

19
/* sha-1 compression function that takes an already expanded message, and additionally store intermediate states */
20 21
/* only stores states ii (the state between step ii-1 and step ii) when DOSTORESTATEii is defined in ubc_check.h */
void sha1_compression_states(uint32_t[5], const uint32_t[16], uint32_t[80], uint32_t[80][5]);
22

23
/*
24 25 26 27 28 29 30
// Function type for sha1_recompression_step_T (uint32_t ihvin[5], uint32_t ihvout[5], const uint32_t me2[80], const uint32_t state[5]).
// Where 0 <= T < 80
//       me2 is an expanded message (the expansion of an original message block XOR'ed with a disturbance vector's message block difference.)
//       state is the internal state (a,b,c,d,e) before step T of the SHA-1 compression function while processing the original message block.
// The function will return:
//       ihvin: The reconstructed input chaining value.
//       ihvout: The reconstructed output chaining value.
31
*/
32 33
typedef void(*sha1_recompression_type)(uint32_t*, uint32_t*, const uint32_t*, const uint32_t*);

34
/* A callback function type that can be set to be called when a collision block has been found: */
35
/* void collision_block_callback(uint64_t byteoffset, const uint32_t ihvin1[5], const uint32_t ihvin2[5], const uint32_t m1[80], const uint32_t m2[80]) */
36 37
typedef void(*collision_block_callback)(uint64_t, const uint32_t*, const uint32_t*, const uint32_t*, const uint32_t*);

38
/* The SHA-1 context. */
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
typedef struct {
	uint64_t total;
	uint32_t ihv[5];
	unsigned char buffer[64];
	int found_collision;
	int safe_hash;
	int detect_coll;
	int ubc_check;
	int reduced_round_coll;
	collision_block_callback callback;

	uint32_t ihv1[5];
	uint32_t ihv2[5];
	uint32_t m1[80];
	uint32_t m2[80];
	uint32_t states[80][5];
} SHA1_CTX;

57
/* Initialize SHA-1 context. */
58
void SHA1DCInit(SHA1_CTX*);
59

60
/*
61 62 63 64 65 66 67 68 69 70 71
    Function to enable safe SHA-1 hashing:
    Collision attacks are thwarted by hashing a detected near-collision block 3 times.
    Think of it as extending SHA-1 from 80-steps to 240-steps for such blocks:
        The best collision attacks against SHA-1 have complexity about 2^60,
        thus for 240-steps an immediate lower-bound for the best cryptanalytic attacks would be 2^180.
        An attacker would be better off using a generic birthday search of complexity 2^80.

   Enabling safe SHA-1 hashing will result in the correct SHA-1 hash for messages where no collision attack was detected,
   but it will result in a different SHA-1 hash for messages where a collision attack was detected.
   This will automatically invalidate SHA-1 based digital signature forgeries.
   Enabled by default.
72
*/
73 74
void SHA1DCSetSafeHash(SHA1_CTX*, int);

75 76 77 78
/*
    Function to disable or enable the use of Unavoidable Bitconditions (provides a significant speed up).
    Enabled by default
 */
79 80
void SHA1DCSetUseUBC(SHA1_CTX*, int);

81 82 83 84
/*
    Function to disable or enable the use of Collision Detection.
    Enabled by default.
 */
85
void SHA1DCSetUseDetectColl(SHA1_CTX*, int);
86

87 88
/* function to disable or enable the detection of reduced-round SHA-1 collisions */
/* disabled by default */
89 90
void SHA1DCSetDetectReducedRoundCollision(SHA1_CTX*, int);

91 92
/* function to set a callback function, pass NULL to disable */
/* by default no callback set */
93 94
void SHA1DCSetCallback(SHA1_CTX*, collision_block_callback);

95 96
/* update SHA-1 context with buffer contents */
void SHA1DCUpdate(SHA1_CTX*, const char*, size_t);
97

98 99
/* obtain SHA-1 hash from SHA-1 context */
/* returns: 0 = no collision detected, otherwise = collision found => warn user for active attack */
100
int  SHA1DCFinal(unsigned char[20], SHA1_CTX*);
101 102 103 104

#if defined(__cplusplus)
}
#endif
105 106 107 108 109 110

#ifdef SHA1DC_CUSTOM_TRAILING_INCLUDE_SHA1_H
#include SHA1DC_CUSTOM_TRAILING_INCLUDE_SHA1_H
#endif

#endif