forked from lthn/blockchain
150 lines
5.2 KiB
C
150 lines
5.2 KiB
C
/********************************************************************\
|
|
*
|
|
* FILE: rmd160.h
|
|
*
|
|
* CONTENTS: Header file for a sample C-implementation of the
|
|
* RIPEMD-160 hash-function.
|
|
* TARGET: any computer with an ANSI C compiler
|
|
*
|
|
* AUTHOR: Antoon Bosselaers, ESAT-COSIC
|
|
* DATE: 1 March 1996
|
|
* VERSION: 1.0
|
|
*
|
|
* Copyright (c) 1996 Katholieke Universiteit Leuven
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person
|
|
* obtaining a copy of this software and associated documentation
|
|
* files (the "Software"), to deal in the Software without restriction,
|
|
* including without limitation the rights to use, copy, modify, merge,
|
|
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
* and to permit persons to whom the Software is furnished to do so,
|
|
* subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be
|
|
* included in all copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
*
|
|
\********************************************************************/
|
|
|
|
#ifndef RMD160H /* make sure this file is read only once */
|
|
#define RMD160H
|
|
|
|
/********************************************************************/
|
|
#include <stdint.h>
|
|
/* typedef 8 and 32 bit types, resp. */
|
|
/* adapt these, if necessary,
|
|
for your operating system and compiler */
|
|
typedef unsigned char byte;
|
|
typedef uint32_t dword;
|
|
|
|
/* if this line causes a compiler error,
|
|
adapt the defintion of dword above */
|
|
typedef int the_correct_size_was_chosen[sizeof(dword) == 4 ? 1 : -1];
|
|
|
|
/********************************************************************/
|
|
|
|
/* macro definitions */
|
|
|
|
/* collect four bytes into one word: */
|
|
#define BYTES_TO_DWORD(strptr) \
|
|
(((dword) *((strptr)+3) << 24) | \
|
|
((dword) *((strptr)+2) << 16) | \
|
|
((dword) *((strptr)+1) << 8) | \
|
|
((dword) *(strptr)))
|
|
|
|
/* ROL(x, n) cyclically rotates x over n bits to the left */
|
|
/* x must be of an unsigned 32 bits type and 0 <= n < 32. */
|
|
#define ROL(x, n) (((x) << (n)) | ((x) >> (32-(n))))
|
|
|
|
/* the five basic functions F(), G() and H() */
|
|
#define F(x, y, z) ((x) ^ (y) ^ (z))
|
|
#define G(x, y, z) (((x) & (y)) | (~(x) & (z)))
|
|
#define H(x, y, z) (((x) | ~(y)) ^ (z))
|
|
#define I(x, y, z) (((x) & (z)) | ((y) & ~(z)))
|
|
#define J(x, y, z) ((x) ^ ((y) | ~(z)))
|
|
|
|
/* the ten basic operations FF() through III() */
|
|
#define FF(a, b, c, d, e, x, s) {\
|
|
(a) += F((b), (c), (d)) + (x);\
|
|
(a) = ROL((a), (s)) + (e);\
|
|
(c) = ROL((c), 10);\
|
|
}
|
|
#define GG(a, b, c, d, e, x, s) {\
|
|
(a) += G((b), (c), (d)) + (x) + 0x5a827999UL;\
|
|
(a) = ROL((a), (s)) + (e);\
|
|
(c) = ROL((c), 10);\
|
|
}
|
|
#define HH(a, b, c, d, e, x, s) {\
|
|
(a) += H((b), (c), (d)) + (x) + 0x6ed9eba1UL;\
|
|
(a) = ROL((a), (s)) + (e);\
|
|
(c) = ROL((c), 10);\
|
|
}
|
|
#define II(a, b, c, d, e, x, s) {\
|
|
(a) += I((b), (c), (d)) + (x) + 0x8f1bbcdcUL;\
|
|
(a) = ROL((a), (s)) + (e);\
|
|
(c) = ROL((c), 10);\
|
|
}
|
|
#define JJ(a, b, c, d, e, x, s) {\
|
|
(a) += J((b), (c), (d)) + (x) + 0xa953fd4eUL;\
|
|
(a) = ROL((a), (s)) + (e);\
|
|
(c) = ROL((c), 10);\
|
|
}
|
|
#define FFF(a, b, c, d, e, x, s) {\
|
|
(a) += F((b), (c), (d)) + (x);\
|
|
(a) = ROL((a), (s)) + (e);\
|
|
(c) = ROL((c), 10);\
|
|
}
|
|
#define GGG(a, b, c, d, e, x, s) {\
|
|
(a) += G((b), (c), (d)) + (x) + 0x7a6d76e9UL;\
|
|
(a) = ROL((a), (s)) + (e);\
|
|
(c) = ROL((c), 10);\
|
|
}
|
|
#define HHH(a, b, c, d, e, x, s) {\
|
|
(a) += H((b), (c), (d)) + (x) + 0x6d703ef3UL;\
|
|
(a) = ROL((a), (s)) + (e);\
|
|
(c) = ROL((c), 10);\
|
|
}
|
|
#define III(a, b, c, d, e, x, s) {\
|
|
(a) += I((b), (c), (d)) + (x) + 0x5c4dd124UL;\
|
|
(a) = ROL((a), (s)) + (e);\
|
|
(c) = ROL((c), 10);\
|
|
}
|
|
#define JJJ(a, b, c, d, e, x, s) {\
|
|
(a) += J((b), (c), (d)) + (x) + 0x50a28be6UL;\
|
|
(a) = ROL((a), (s)) + (e);\
|
|
(c) = ROL((c), 10);\
|
|
}
|
|
|
|
/********************************************************************/
|
|
|
|
/* function prototypes */
|
|
|
|
void MDinit(dword *MDbuf);
|
|
/*
|
|
* initializes MDbuffer to "magic constants"
|
|
*/
|
|
|
|
void compress(dword *MDbuf, dword *X);
|
|
/*
|
|
* the compression function.
|
|
* transforms MDbuf using message bytes X[0] through X[15]
|
|
*/
|
|
|
|
void MDfinish(dword *MDbuf, byte *strptr, dword lswlen, dword mswlen);
|
|
/*
|
|
* puts bytes from strptr into X and pad out; appends length
|
|
* and finally, compresses the last block(s)
|
|
* note: length in bits == 8 * (lswlen + 2^32 mswlen).
|
|
* note: there are (lswlen mod 64) bytes left in strptr.
|
|
*/
|
|
|
|
#endif /* RMD160H */
|
|
|
|
/*********************** end of file rmd160.h ***********************/
|