Blame src/hash/sha1dc/sha1.h

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