Blame src/hash/sha1dc/sha1.h

Packit ae9e2a
/***
Packit ae9e2a
* Copyright 2017 Marc Stevens <marc@marc-stevens.nl>, Dan Shumow <danshu@microsoft.com>
Packit ae9e2a
* Distributed under the MIT Software License.
Packit ae9e2a
* See accompanying file LICENSE.txt or copy at
Packit ae9e2a
* https://opensource.org/licenses/MIT
Packit ae9e2a
***/
Packit ae9e2a
Packit ae9e2a
#ifndef SHA1DC_SHA1_H
Packit ae9e2a
#define SHA1DC_SHA1_H
Packit ae9e2a
Packit ae9e2a
#if defined(__cplusplus)
Packit ae9e2a
extern "C" {
Packit ae9e2a
#endif
Packit ae9e2a
Packit ae9e2a
#ifndef SHA1DC_NO_STANDARD_INCLUDES
Packit ae9e2a
#include <stdint.h>
Packit ae9e2a
#endif
Packit ae9e2a
Packit ae9e2a
/* sha-1 compression function that takes an already expanded message, and additionally store intermediate states */
Packit ae9e2a
/* only stores states ii (the state between step ii-1 and step ii) when DOSTORESTATEii is defined in ubc_check.h */
Packit ae9e2a
void sha1_compression_states(uint32_t[5], const uint32_t[16], uint32_t[80], uint32_t[80][5]);
Packit ae9e2a
Packit ae9e2a
/*
Packit ae9e2a
// 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]).
Packit ae9e2a
// Where 0 <= T < 80
Packit ae9e2a
//       me2 is an expanded message (the expansion of an original message block XOR'ed with a disturbance vector's message block difference.)
Packit ae9e2a
//       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.
Packit ae9e2a
// The function will return:
Packit ae9e2a
//       ihvin: The reconstructed input chaining value.
Packit ae9e2a
//       ihvout: The reconstructed output chaining value.
Packit ae9e2a
*/
Packit ae9e2a
typedef void(*sha1_recompression_type)(uint32_t*, uint32_t*, const uint32_t*, const uint32_t*);
Packit ae9e2a
Packit ae9e2a
/* A callback function type that can be set to be called when a collision block has been found: */
Packit ae9e2a
/* 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]) */
Packit ae9e2a
typedef void(*collision_block_callback)(uint64_t, const uint32_t*, const uint32_t*, const uint32_t*, const uint32_t*);
Packit ae9e2a
Packit ae9e2a
/* The SHA-1 context. */
Packit ae9e2a
typedef struct {
Packit ae9e2a
	uint64_t total;
Packit ae9e2a
	uint32_t ihv[5];
Packit ae9e2a
	unsigned char buffer[64];
Packit ae9e2a
	int found_collision;
Packit ae9e2a
	int safe_hash;
Packit ae9e2a
	int detect_coll;
Packit ae9e2a
	int ubc_check;
Packit ae9e2a
	int reduced_round_coll;
Packit ae9e2a
	collision_block_callback callback;
Packit ae9e2a
Packit ae9e2a
	uint32_t ihv1[5];
Packit ae9e2a
	uint32_t ihv2[5];
Packit ae9e2a
	uint32_t m1[80];
Packit ae9e2a
	uint32_t m2[80];
Packit ae9e2a
	uint32_t states[80][5];
Packit ae9e2a
} SHA1_CTX;
Packit ae9e2a
Packit ae9e2a
/* Initialize SHA-1 context. */
Packit ae9e2a
void SHA1DCInit(SHA1_CTX*);
Packit ae9e2a
Packit ae9e2a
/*
Packit ae9e2a
    Function to enable safe SHA-1 hashing:
Packit ae9e2a
    Collision attacks are thwarted by hashing a detected near-collision block 3 times.
Packit ae9e2a
    Think of it as extending SHA-1 from 80-steps to 240-steps for such blocks:
Packit ae9e2a
        The best collision attacks against SHA-1 have complexity about 2^60,
Packit ae9e2a
        thus for 240-steps an immediate lower-bound for the best cryptanalytic attacks would be 2^180.
Packit ae9e2a
        An attacker would be better off using a generic birthday search of complexity 2^80.
Packit ae9e2a
Packit ae9e2a
   Enabling safe SHA-1 hashing will result in the correct SHA-1 hash for messages where no collision attack was detected,
Packit ae9e2a
   but it will result in a different SHA-1 hash for messages where a collision attack was detected.
Packit ae9e2a
   This will automatically invalidate SHA-1 based digital signature forgeries.
Packit ae9e2a
   Enabled by default.
Packit ae9e2a
*/
Packit ae9e2a
void SHA1DCSetSafeHash(SHA1_CTX*, int);
Packit ae9e2a
Packit ae9e2a
/*
Packit ae9e2a
    Function to disable or enable the use of Unavoidable Bitconditions (provides a significant speed up).
Packit ae9e2a
    Enabled by default
Packit ae9e2a
 */
Packit ae9e2a
void SHA1DCSetUseUBC(SHA1_CTX*, int);
Packit ae9e2a
Packit ae9e2a
/*
Packit ae9e2a
    Function to disable or enable the use of Collision Detection.
Packit ae9e2a
    Enabled by default.
Packit ae9e2a
 */
Packit ae9e2a
void SHA1DCSetUseDetectColl(SHA1_CTX*, int);
Packit ae9e2a
Packit ae9e2a
/* function to disable or enable the detection of reduced-round SHA-1 collisions */
Packit ae9e2a
/* disabled by default */
Packit ae9e2a
void SHA1DCSetDetectReducedRoundCollision(SHA1_CTX*, int);
Packit ae9e2a
Packit ae9e2a
/* function to set a callback function, pass NULL to disable */
Packit ae9e2a
/* by default no callback set */
Packit ae9e2a
void SHA1DCSetCallback(SHA1_CTX*, collision_block_callback);
Packit ae9e2a
Packit ae9e2a
/* update SHA-1 context with buffer contents */
Packit ae9e2a
void SHA1DCUpdate(SHA1_CTX*, const char*, size_t);
Packit ae9e2a
Packit ae9e2a
/* obtain SHA-1 hash from SHA-1 context */
Packit ae9e2a
/* returns: 0 = no collision detected, otherwise = collision found => warn user for active attack */
Packit ae9e2a
int  SHA1DCFinal(unsigned char[20], SHA1_CTX*);
Packit ae9e2a
Packit ae9e2a
#if defined(__cplusplus)
Packit ae9e2a
}
Packit ae9e2a
#endif
Packit ae9e2a
Packit ae9e2a
#ifdef SHA1DC_CUSTOM_TRAILING_INCLUDE_SHA1_H
Packit ae9e2a
#include SHA1DC_CUSTOM_TRAILING_INCLUDE_SHA1_H
Packit ae9e2a
#endif
Packit ae9e2a
Packit ae9e2a
#endif