1 #include <string.h>
2 #include <stdio.h>
3 
4 #include "rmd160.h"
5 #include "wrap_160.h"
6 
7 #ifdef USE_RMD160CRYPT_HEADER
RIPEMD160_init(Crypt__RIPEMD160 ripemd160)8 void RIPEMD160_init(Crypt__RIPEMD160 ripemd160)
9 #else
10 void RIPEMD160_init(RIPEMD160 ripemd160)
11 #endif
12 {
13   MDinit(ripemd160->MDbuf);
14   ripemd160->local = (dword) 0;
15   ripemd160->count_lo = (dword) 0;
16   ripemd160->count_hi = (dword) 0;
17 }
18 
19 #ifdef USE_RMD160CRYPT_HEADER
RIPEMD160_update(Crypt__RIPEMD160 ripemd160,byte * strptr,dword len)20 void RIPEMD160_update(Crypt__RIPEMD160 ripemd160, byte *strptr, dword len)
21 #else
22 void RIPEMD160_update(RIPEMD160 ripemd160, byte *strptr, dword len)
23 #endif
24 {
25   dword
26     i;
27   byte *
28     ptr;
29 
30   if (ripemd160->count_lo + len < ripemd160->count_lo) {
31     ripemd160->count_hi++;
32   }
33   ripemd160->count_lo += len;
34 
35   if (ripemd160->local > 0) {
36     i = RIPEMD160_BLOCKSIZE - ripemd160->local;
37     if (i > len) {
38       i = len;
39     }
40     memcpy(ripemd160->data + ripemd160->local, strptr, i);
41     len -= i;
42     strptr += i;
43     ripemd160->local += i;
44     if (ripemd160->local == RIPEMD160_BLOCKSIZE) {
45       memset(ripemd160->X, 0, RIPEMD160_BLOCKSIZE);
46       ptr = ripemd160->data;
47       for (i=0; i<RIPEMD160_BLOCKSIZE; i++) {
48 	/* byte i goes into word X[i div 4] at pos.  8*(i mod 4)  */
49 	ripemd160->X[i>>2] |= (dword) *ptr++ << (8 * (i&3));
50       }
51       rmd160_compress(ripemd160->MDbuf, ripemd160->X);
52     } else {
53       return;
54     }
55   }
56   while (len >= RIPEMD160_BLOCKSIZE) {
57     memset(ripemd160->X, 0, RIPEMD160_BLOCKSIZE);
58     for (i=0; i<RIPEMD160_BLOCKSIZE; i++) {
59       /* byte i goes into word X[i div 4] at pos.  8*(i mod 4)  */
60       ripemd160->X[i>>2] |= (dword) *strptr++ << (8 * (i&3));
61     }
62     len -= RIPEMD160_BLOCKSIZE;
63     rmd160_compress(ripemd160->MDbuf, ripemd160->X);
64   }
65   memcpy(ripemd160->data, strptr, len);
66   ripemd160->local = len;
67 }
68 
69 #ifdef USE_RMD160CRYPT_HEADER
RIPEMD160_final(Crypt__RIPEMD160 ripemd160)70 void RIPEMD160_final(Crypt__RIPEMD160 ripemd160)
71 #else
72 void RIPEMD160_final(RIPEMD160 ripemd160)
73 #endif
74 {
75   if (ripemd160->local != ripemd160->count_lo % 64) {
76     printf("local != count %% 64\n");
77   }
78 
79   MDfinish(ripemd160->MDbuf,
80 	   ripemd160->data,
81 	   (dword) ripemd160->count_lo,
82 	   (dword) ripemd160->count_hi);
83 }
84 
85 /* The HMAC_RIPEMD160 transform looks like:
86 
87    RIPEMD160(K XOR opad, RIPEMD160(K XOR ipad, text))
88 
89    where K is an n byte key
90    ipad is the byte 0x36 repeated 64 times
91    opad is the byte 0x5c repeated 64 times
92    and text is the data being protected
93 */
94 
95 #ifdef USE_MALICIOUS_MAC
96 #ifdef USE_RMD160CRYPT_HEADER
RIPEMD160_HMAC(Crypt__RIPEMD160 ripemd160,byte * input,dword len,byte * key,dword keylen)97 void RIPEMD160_HMAC(Crypt__RIPEMD160 ripemd160,
98 		    byte *input,        /* pointer to data stream */
99 		    dword len,           /* length of data stream */
100 		    byte *key,   /* pointer to authentication key */
101 		    dword keylen) /* length of authentication key */
102 #else
103 void RIPEMD160_HMAC(RIPEMD160 ripemd160,
104 		    byte *input,        /* pointer to data stream */
105 		    dword len,           /* length of data stream */
106 		    byte *key,   /* pointer to authentication key */
107 		    dword keylen) /* length of authentication key */
108 #endif
109 {
110   byte
111     k_ipad[65],  /* inner padding - key XORd with ipad */
112     k_opad[65];  /* outer padding - key XORd with opad */
113   byte
114     tk[RMD160_DIGESTSIZE];
115   dword
116     i;
117 
118   /* if key is longer than 64 bytes reset it to key=RIPEMD160(key) */
119   if (keylen > 64) {
120     RIPEMD160_INFO
121       tctx;
122 
123     RIPEMD160_init(&tctx);
124     RIPEMD160_update(&tctx, key, keylen);
125     RIPEMD160_final(&tctx);
126 
127     key = tctx.MDbuf;
128     keylen = RMD160_DIGESTSIZE;
129   }
130 
131   /* start out by storing key in pads */
132   memset(k_ipad, 0x36, sizeof(k_ipad));
133   memset(k_opad, 0x5c, sizeof(k_opad));
134 
135   /* XOR key with ipad and opad values */
136   for (i=0; i<keylen; i++) {
137     k_ipad[i] ^= key[i];
138     k_opad[i] ^= key[i];
139   }
140 
141   /* perform inner RIPEMD-160 */
142   RIPEMD160_init(ripemd160);                  /* init ripemd160 for 1st pass */
143   RIPEMD160_update(ripemd160, k_ipad, 64);           /* start with inner pad */
144   RIPEMD160_update(ripemd160, input, len);          /* then text of datagram */
145   RIPEMD160_final(ripemd160);                          /* finish up 1st pass */
146   memcpy(digest, ripemd160->MDbuf, RMD160_DIGESTSIZE);
147 
148   /* perform outer RIPEMD-160 */
149   RIPEMD160_init(ripemd160);                  /* init ripemd160 for 2nd pass */
150   RIPEMD160_update(ripemd160, k_opad, 64);           /* start with outer pad */
151   RIPEMD160_update(ripemd160, digest, RMD160_DIGESTSIZE);
152   RIPEMD160_final(ripemd160);                          /* finish up 2nd pass */
153   memcpy(digest, ripemd160->MDbuf, RMD160_DIGESTSIZE);
154 
155   /* clean up secret keys */
156   memset(k_ipad, 0x00, sizeof(k_ipad));
157   memset(k_opad, 0x00, sizeof(k_opad));
158 }
159 #endif
160 /* ************************************************************************* */
161 
162 /* #define MAINTEST */
163 
164 #ifdef MAINTEST
165 #include <stdio.h>
166 #include <stdlib.h>
167 
print_hash(RIPEMD160 ripemd160)168 void print_hash(RIPEMD160 ripemd160)
169 {
170   byte hashcode[RMDsize/8];
171   int i;
172 
173   for (i=0; i<RMDsize/8; i+=4) {
174     hashcode[i]   =  ripemd160->MDbuf[i>>2];
175     hashcode[i+1] = (ripemd160->MDbuf[i>>2] >>  8);
176     hashcode[i+2] = (ripemd160->MDbuf[i>>2] >> 16);
177     hashcode[i+3] = (ripemd160->MDbuf[i>>2] >> 24);
178   }
179   printf("hashcode: ");
180   for (i=0; i<RMDsize/8; i++)
181     printf("%02x", hashcode[i]);
182   printf("\n");
183 }
184 
main(void)185 int main (void)
186 {
187   RIPEMD160_INFO ripemd160_info;
188   byte a[1000001];
189 
190   int i;
191   long L;
192 
193   RIPEMD160_init(&ripemd160_info);
194 
195   /*
196     RIPEMD160_update(&ripemd160_info, (byte *) "a", (dword) strlen("a"));
197     RIPEMD160_update(&ripemd160_info, (byte *) "b", (dword) strlen("b"));
198     RIPEMD160_update(&ripemd160_info, (byte *) "c", (dword) strlen("c"));
199   */
200 
201   /*
202     memset(a, 'a', 1000000);
203     a[1000000] = 0;
204     RIPEMD160_update(&ripemd160_info, (byte *) a, (dword) 1000000);
205   */
206 
207 
208   for (L = 0; L<1000000; L++) {
209     RIPEMD160_update(&ripemd160_info, (byte *) "a", (dword) strlen("a"));
210   }
211 
212   /*
213     for (i = 0; i<8; i++) {
214     RIPEMD160_update(&ripemd160_info_info,
215     (byte *) "1234567890",
216     (dword) strlen("1234567890"));
217     }
218   */
219 
220   RIPEMD160_final(&ripemd160_info);
221 
222   print_hash(&ripemd160_info);
223 
224   return(0);
225 }
226 #endif
227