1 /********************************************************************\ 2 * 3 * FILE: rmd160.h 4 * 5 * CONTENTS: Header file for a sample C-implementation of the 6 * RIPEMD-160 hash-function. 7 * TARGET: any computer with an ANSI C compiler 8 * 9 * AUTHOR: Antoon Bosselaers, ESAT-COSIC 10 * DATE: 1 March 1996 11 * VERSION: 1.0 12 * 13 * Copyright (c) Katholieke Universiteit Leuven 14 * 1996, All Rights Reserved 15 * 16 \********************************************************************/ 17 18 #ifndef RMD160H /* make sure this file is read only once */ 19 #define RMD160H 20 21 #include <limits.h> 22 23 /********************************************************************/ 24 25 /* typedef 8, 16 and 32 bit types, resp. */ 26 /* adapt these, if necessary, 27 for your operating system and compiler */ 28 typedef unsigned char byte; /* unsigned 8-bit integer */ 29 typedef unsigned short word; /* unsigned 16-bit integer */ 30 #if ULONG_MAX == 4294967295U 31 typedef unsigned long dword; /* unsigned 32-bit integer */ 32 #elif UINT_MAX == 4294967295U 33 typedef unsigned int dword; /* unsigned 32-bit integer */ 34 #endif 35 36 /********************************************************************/ 37 38 /* macro definitions */ 39 40 /* collect four bytes into one word: */ 41 #define BYTES_TO_DWORD(strptr) \ 42 (((dword) *((strptr)+3) << 24) | \ 43 ((dword) *((strptr)+2) << 16) | \ 44 ((dword) *((strptr)+1) << 8) | \ 45 ((dword) *(strptr))) 46 47 /* ROL(x, n) cyclically rotates x over n bits to the left */ 48 /* x must be of an unsigned 32 bits type and 0 <= n < 32. */ 49 #define ROL(x, n) (((x) << (n)) | ((x) >> (32-(n)))) 50 51 /* the three basic functions F(), G() and H() */ 52 #define F(x, y, z) ((x) ^ (y) ^ (z)) 53 #define G(x, y, z) (((x) & (y)) | (~(x) & (z))) 54 #define H(x, y, z) (((x) | ~(y)) ^ (z)) 55 #define I(x, y, z) (((x) & (z)) | ((y) & ~(z))) 56 #define J(x, y, z) ((x) ^ ((y) | ~(z))) 57 58 /* the eight basic operations FF() through III() */ 59 #define FF(a, b, c, d, e, x, s) {\ 60 (a) += F((b), (c), (d)) + (x);\ 61 (a) = ROL((a), (s)) + (e);\ 62 (c) = ROL((c), 10);\ 63 } 64 #define GG(a, b, c, d, e, x, s) {\ 65 (a) += G((b), (c), (d)) + (x) + 0x5a827999UL;\ 66 (a) = ROL((a), (s)) + (e);\ 67 (c) = ROL((c), 10);\ 68 } 69 #define HH(a, b, c, d, e, x, s) {\ 70 (a) += H((b), (c), (d)) + (x) + 0x6ed9eba1UL;\ 71 (a) = ROL((a), (s)) + (e);\ 72 (c) = ROL((c), 10);\ 73 } 74 #define II(a, b, c, d, e, x, s) {\ 75 (a) += I((b), (c), (d)) + (x) + 0x8f1bbcdcUL;\ 76 (a) = ROL((a), (s)) + (e);\ 77 (c) = ROL((c), 10);\ 78 } 79 #define JJ(a, b, c, d, e, x, s) {\ 80 (a) += J((b), (c), (d)) + (x) + 0xa953fd4eUL;\ 81 (a) = ROL((a), (s)) + (e);\ 82 (c) = ROL((c), 10);\ 83 } 84 #define FFF(a, b, c, d, e, x, s) {\ 85 (a) += F((b), (c), (d)) + (x);\ 86 (a) = ROL((a), (s)) + (e);\ 87 (c) = ROL((c), 10);\ 88 } 89 #define GGG(a, b, c, d, e, x, s) {\ 90 (a) += G((b), (c), (d)) + (x) + 0x7a6d76e9UL;\ 91 (a) = ROL((a), (s)) + (e);\ 92 (c) = ROL((c), 10);\ 93 } 94 #define HHH(a, b, c, d, e, x, s) {\ 95 (a) += H((b), (c), (d)) + (x) + 0x6d703ef3UL;\ 96 (a) = ROL((a), (s)) + (e);\ 97 (c) = ROL((c), 10);\ 98 } 99 #define III(a, b, c, d, e, x, s) {\ 100 (a) += I((b), (c), (d)) + (x) + 0x5c4dd124UL;\ 101 (a) = ROL((a), (s)) + (e);\ 102 (c) = ROL((c), 10);\ 103 } 104 #define JJJ(a, b, c, d, e, x, s) {\ 105 (a) += J((b), (c), (d)) + (x) + 0x50a28be6UL;\ 106 (a) = ROL((a), (s)) + (e);\ 107 (c) = ROL((c), 10);\ 108 } 109 110 /********************************************************************/ 111 112 /* function prototypes */ 113 114 void rmd160_init(dword *MDbuf); 115 /* 116 * initializes MDbuffer to "magic constants" 117 */ 118 119 void rmd160_compress(dword *MDbuf, dword *X); 120 /* 121 * the compression function. 122 * transforms MDbuf using message bytes X[0] through X[15] 123 */ 124 125 void rmd160_finish(dword *MDbuf, byte *strptr, dword lswlen, dword mswlen); 126 /* 127 * puts bytes from strptr into X and pad out; appends length 128 * and finally, compresses the last block(s) 129 * note: length in bits == 8 * (lswlen + 2^32 mswlen). 130 * note: there are (lswlen mod 64) bytes left in strptr. 131 */ 132 133 #endif /* RMD160H */ 134 135 /*********************** end of file rmd160.h ***********************/ 136 137