• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2  * All rights reserved.
3  *
4  * This package is an SSL implementation written
5  * by Eric Young (eay@cryptsoft.com).
6  * The implementation was written so as to conform with Netscapes SSL.
7  *
8  * This library is free for commercial and non-commercial use as long as
9  * the following conditions are aheared to.  The following conditions
10  * apply to all code found in this distribution, be it the RC4, RSA,
11  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
12  * included with this distribution is covered by the same copyright terms
13  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14  *
15  * Copyright remains Eric Young's, and as such any Copyright notices in
16  * the code are not to be removed.
17  * If this package is used in a product, Eric Young should be given attribution
18  * as the author of the parts of the library used.
19  * This can be in the form of a textual message at program startup or
20  * in documentation (online or textual) provided with the package.
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions
24  * are met:
25  * 1. Redistributions of source code must retain the copyright
26  *    notice, this list of conditions and the following disclaimer.
27  * 2. Redistributions in binary form must reproduce the above copyright
28  *    notice, this list of conditions and the following disclaimer in the
29  *    documentation and/or other materials provided with the distribution.
30  * 3. All advertising materials mentioning features or use of this software
31  *    must display the following acknowledgement:
32  *    "This product includes cryptographic software written by
33  *     Eric Young (eay@cryptsoft.com)"
34  *    The word 'cryptographic' can be left out if the rouines from the library
35  *    being used are not cryptographic related :-).
36  * 4. If you include any Windows specific code (or a derivative thereof) from
37  *    the apps directory (application code) you must include an acknowledgement:
38  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39  *
40  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  *
52  * The licence and distribution terms for any publically available version or
53  * derivative of this code cannot be changed.  i.e. this code cannot simply be
54  * copied and put under another distribution licence
55  * [including the GNU Public Licence.] */
56 
57 #include <openssl/ripemd.h>
58 
59 #include <string.h>
60 
61 #include "internal.h"
62 
63 
RIPEMD160_Init(RIPEMD160_CTX * ctx)64 int RIPEMD160_Init(RIPEMD160_CTX *ctx) {
65   OPENSSL_memset(ctx, 0, sizeof(*ctx));
66   ctx->h[0] = RIPEMD160_A;
67   ctx->h[1] = RIPEMD160_B;
68   ctx->h[2] = RIPEMD160_C;
69   ctx->h[3] = RIPEMD160_D;
70   ctx->h[4] = RIPEMD160_E;
71   return 1;
72 }
73 
ripemd160_block_data_order(uint32_t h[5],const uint8_t * data,size_t num)74 static void ripemd160_block_data_order(uint32_t h[5], const uint8_t *data,
75                                        size_t num) {
76   uint32_t A, B, C, D, E;
77   uint32_t a, b, c, d, e, l;
78   uint32_t XX0, XX1, XX2, XX3, XX4, XX5, XX6, XX7, XX8, XX9, XX10, XX11, XX12,
79       XX13, XX14, XX15;
80 #define X(i) XX##i
81 
82   for (; num--;) {
83     A = h[0];
84     B = h[1];
85     C = h[2];
86     D = h[3];
87     E = h[4];
88 
89     HOST_c2l(data, l);
90     X(0) = l;
91     HOST_c2l(data, l);
92     X(1) = l;
93     RIP1(A, B, C, D, E, WL00, SL00);
94     HOST_c2l(data, l);
95     X(2) = l;
96     RIP1(E, A, B, C, D, WL01, SL01);
97     HOST_c2l(data, l);
98     X(3) = l;
99     RIP1(D, E, A, B, C, WL02, SL02);
100     HOST_c2l(data, l);
101     X(4) = l;
102     RIP1(C, D, E, A, B, WL03, SL03);
103     HOST_c2l(data, l);
104     X(5) = l;
105     RIP1(B, C, D, E, A, WL04, SL04);
106     HOST_c2l(data, l);
107     X(6) = l;
108     RIP1(A, B, C, D, E, WL05, SL05);
109     HOST_c2l(data, l);
110     X(7) = l;
111     RIP1(E, A, B, C, D, WL06, SL06);
112     HOST_c2l(data, l);
113     X(8) = l;
114     RIP1(D, E, A, B, C, WL07, SL07);
115     HOST_c2l(data, l);
116     X(9) = l;
117     RIP1(C, D, E, A, B, WL08, SL08);
118     HOST_c2l(data, l);
119     X(10) = l;
120     RIP1(B, C, D, E, A, WL09, SL09);
121     HOST_c2l(data, l);
122     X(11) = l;
123     RIP1(A, B, C, D, E, WL10, SL10);
124     HOST_c2l(data, l);
125     X(12) = l;
126     RIP1(E, A, B, C, D, WL11, SL11);
127     HOST_c2l(data, l);
128     X(13) = l;
129     RIP1(D, E, A, B, C, WL12, SL12);
130     HOST_c2l(data, l);
131     X(14) = l;
132     RIP1(C, D, E, A, B, WL13, SL13);
133     HOST_c2l(data, l);
134     X(15) = l;
135     RIP1(B, C, D, E, A, WL14, SL14);
136     RIP1(A, B, C, D, E, WL15, SL15);
137 
138     RIP2(E, A, B, C, D, WL16, SL16, KL1);
139     RIP2(D, E, A, B, C, WL17, SL17, KL1);
140     RIP2(C, D, E, A, B, WL18, SL18, KL1);
141     RIP2(B, C, D, E, A, WL19, SL19, KL1);
142     RIP2(A, B, C, D, E, WL20, SL20, KL1);
143     RIP2(E, A, B, C, D, WL21, SL21, KL1);
144     RIP2(D, E, A, B, C, WL22, SL22, KL1);
145     RIP2(C, D, E, A, B, WL23, SL23, KL1);
146     RIP2(B, C, D, E, A, WL24, SL24, KL1);
147     RIP2(A, B, C, D, E, WL25, SL25, KL1);
148     RIP2(E, A, B, C, D, WL26, SL26, KL1);
149     RIP2(D, E, A, B, C, WL27, SL27, KL1);
150     RIP2(C, D, E, A, B, WL28, SL28, KL1);
151     RIP2(B, C, D, E, A, WL29, SL29, KL1);
152     RIP2(A, B, C, D, E, WL30, SL30, KL1);
153     RIP2(E, A, B, C, D, WL31, SL31, KL1);
154 
155     RIP3(D, E, A, B, C, WL32, SL32, KL2);
156     RIP3(C, D, E, A, B, WL33, SL33, KL2);
157     RIP3(B, C, D, E, A, WL34, SL34, KL2);
158     RIP3(A, B, C, D, E, WL35, SL35, KL2);
159     RIP3(E, A, B, C, D, WL36, SL36, KL2);
160     RIP3(D, E, A, B, C, WL37, SL37, KL2);
161     RIP3(C, D, E, A, B, WL38, SL38, KL2);
162     RIP3(B, C, D, E, A, WL39, SL39, KL2);
163     RIP3(A, B, C, D, E, WL40, SL40, KL2);
164     RIP3(E, A, B, C, D, WL41, SL41, KL2);
165     RIP3(D, E, A, B, C, WL42, SL42, KL2);
166     RIP3(C, D, E, A, B, WL43, SL43, KL2);
167     RIP3(B, C, D, E, A, WL44, SL44, KL2);
168     RIP3(A, B, C, D, E, WL45, SL45, KL2);
169     RIP3(E, A, B, C, D, WL46, SL46, KL2);
170     RIP3(D, E, A, B, C, WL47, SL47, KL2);
171 
172     RIP4(C, D, E, A, B, WL48, SL48, KL3);
173     RIP4(B, C, D, E, A, WL49, SL49, KL3);
174     RIP4(A, B, C, D, E, WL50, SL50, KL3);
175     RIP4(E, A, B, C, D, WL51, SL51, KL3);
176     RIP4(D, E, A, B, C, WL52, SL52, KL3);
177     RIP4(C, D, E, A, B, WL53, SL53, KL3);
178     RIP4(B, C, D, E, A, WL54, SL54, KL3);
179     RIP4(A, B, C, D, E, WL55, SL55, KL3);
180     RIP4(E, A, B, C, D, WL56, SL56, KL3);
181     RIP4(D, E, A, B, C, WL57, SL57, KL3);
182     RIP4(C, D, E, A, B, WL58, SL58, KL3);
183     RIP4(B, C, D, E, A, WL59, SL59, KL3);
184     RIP4(A, B, C, D, E, WL60, SL60, KL3);
185     RIP4(E, A, B, C, D, WL61, SL61, KL3);
186     RIP4(D, E, A, B, C, WL62, SL62, KL3);
187     RIP4(C, D, E, A, B, WL63, SL63, KL3);
188 
189     RIP5(B, C, D, E, A, WL64, SL64, KL4);
190     RIP5(A, B, C, D, E, WL65, SL65, KL4);
191     RIP5(E, A, B, C, D, WL66, SL66, KL4);
192     RIP5(D, E, A, B, C, WL67, SL67, KL4);
193     RIP5(C, D, E, A, B, WL68, SL68, KL4);
194     RIP5(B, C, D, E, A, WL69, SL69, KL4);
195     RIP5(A, B, C, D, E, WL70, SL70, KL4);
196     RIP5(E, A, B, C, D, WL71, SL71, KL4);
197     RIP5(D, E, A, B, C, WL72, SL72, KL4);
198     RIP5(C, D, E, A, B, WL73, SL73, KL4);
199     RIP5(B, C, D, E, A, WL74, SL74, KL4);
200     RIP5(A, B, C, D, E, WL75, SL75, KL4);
201     RIP5(E, A, B, C, D, WL76, SL76, KL4);
202     RIP5(D, E, A, B, C, WL77, SL77, KL4);
203     RIP5(C, D, E, A, B, WL78, SL78, KL4);
204     RIP5(B, C, D, E, A, WL79, SL79, KL4);
205 
206     a = A;
207     b = B;
208     c = C;
209     d = D;
210     e = E;
211     /* Do other half */
212     A = h[0];
213     B = h[1];
214     C = h[2];
215     D = h[3];
216     E = h[4];
217 
218     RIP5(A, B, C, D, E, WR00, SR00, KR0);
219     RIP5(E, A, B, C, D, WR01, SR01, KR0);
220     RIP5(D, E, A, B, C, WR02, SR02, KR0);
221     RIP5(C, D, E, A, B, WR03, SR03, KR0);
222     RIP5(B, C, D, E, A, WR04, SR04, KR0);
223     RIP5(A, B, C, D, E, WR05, SR05, KR0);
224     RIP5(E, A, B, C, D, WR06, SR06, KR0);
225     RIP5(D, E, A, B, C, WR07, SR07, KR0);
226     RIP5(C, D, E, A, B, WR08, SR08, KR0);
227     RIP5(B, C, D, E, A, WR09, SR09, KR0);
228     RIP5(A, B, C, D, E, WR10, SR10, KR0);
229     RIP5(E, A, B, C, D, WR11, SR11, KR0);
230     RIP5(D, E, A, B, C, WR12, SR12, KR0);
231     RIP5(C, D, E, A, B, WR13, SR13, KR0);
232     RIP5(B, C, D, E, A, WR14, SR14, KR0);
233     RIP5(A, B, C, D, E, WR15, SR15, KR0);
234 
235     RIP4(E, A, B, C, D, WR16, SR16, KR1);
236     RIP4(D, E, A, B, C, WR17, SR17, KR1);
237     RIP4(C, D, E, A, B, WR18, SR18, KR1);
238     RIP4(B, C, D, E, A, WR19, SR19, KR1);
239     RIP4(A, B, C, D, E, WR20, SR20, KR1);
240     RIP4(E, A, B, C, D, WR21, SR21, KR1);
241     RIP4(D, E, A, B, C, WR22, SR22, KR1);
242     RIP4(C, D, E, A, B, WR23, SR23, KR1);
243     RIP4(B, C, D, E, A, WR24, SR24, KR1);
244     RIP4(A, B, C, D, E, WR25, SR25, KR1);
245     RIP4(E, A, B, C, D, WR26, SR26, KR1);
246     RIP4(D, E, A, B, C, WR27, SR27, KR1);
247     RIP4(C, D, E, A, B, WR28, SR28, KR1);
248     RIP4(B, C, D, E, A, WR29, SR29, KR1);
249     RIP4(A, B, C, D, E, WR30, SR30, KR1);
250     RIP4(E, A, B, C, D, WR31, SR31, KR1);
251 
252     RIP3(D, E, A, B, C, WR32, SR32, KR2);
253     RIP3(C, D, E, A, B, WR33, SR33, KR2);
254     RIP3(B, C, D, E, A, WR34, SR34, KR2);
255     RIP3(A, B, C, D, E, WR35, SR35, KR2);
256     RIP3(E, A, B, C, D, WR36, SR36, KR2);
257     RIP3(D, E, A, B, C, WR37, SR37, KR2);
258     RIP3(C, D, E, A, B, WR38, SR38, KR2);
259     RIP3(B, C, D, E, A, WR39, SR39, KR2);
260     RIP3(A, B, C, D, E, WR40, SR40, KR2);
261     RIP3(E, A, B, C, D, WR41, SR41, KR2);
262     RIP3(D, E, A, B, C, WR42, SR42, KR2);
263     RIP3(C, D, E, A, B, WR43, SR43, KR2);
264     RIP3(B, C, D, E, A, WR44, SR44, KR2);
265     RIP3(A, B, C, D, E, WR45, SR45, KR2);
266     RIP3(E, A, B, C, D, WR46, SR46, KR2);
267     RIP3(D, E, A, B, C, WR47, SR47, KR2);
268 
269     RIP2(C, D, E, A, B, WR48, SR48, KR3);
270     RIP2(B, C, D, E, A, WR49, SR49, KR3);
271     RIP2(A, B, C, D, E, WR50, SR50, KR3);
272     RIP2(E, A, B, C, D, WR51, SR51, KR3);
273     RIP2(D, E, A, B, C, WR52, SR52, KR3);
274     RIP2(C, D, E, A, B, WR53, SR53, KR3);
275     RIP2(B, C, D, E, A, WR54, SR54, KR3);
276     RIP2(A, B, C, D, E, WR55, SR55, KR3);
277     RIP2(E, A, B, C, D, WR56, SR56, KR3);
278     RIP2(D, E, A, B, C, WR57, SR57, KR3);
279     RIP2(C, D, E, A, B, WR58, SR58, KR3);
280     RIP2(B, C, D, E, A, WR59, SR59, KR3);
281     RIP2(A, B, C, D, E, WR60, SR60, KR3);
282     RIP2(E, A, B, C, D, WR61, SR61, KR3);
283     RIP2(D, E, A, B, C, WR62, SR62, KR3);
284     RIP2(C, D, E, A, B, WR63, SR63, KR3);
285 
286     RIP1(B, C, D, E, A, WR64, SR64);
287     RIP1(A, B, C, D, E, WR65, SR65);
288     RIP1(E, A, B, C, D, WR66, SR66);
289     RIP1(D, E, A, B, C, WR67, SR67);
290     RIP1(C, D, E, A, B, WR68, SR68);
291     RIP1(B, C, D, E, A, WR69, SR69);
292     RIP1(A, B, C, D, E, WR70, SR70);
293     RIP1(E, A, B, C, D, WR71, SR71);
294     RIP1(D, E, A, B, C, WR72, SR72);
295     RIP1(C, D, E, A, B, WR73, SR73);
296     RIP1(B, C, D, E, A, WR74, SR74);
297     RIP1(A, B, C, D, E, WR75, SR75);
298     RIP1(E, A, B, C, D, WR76, SR76);
299     RIP1(D, E, A, B, C, WR77, SR77);
300     RIP1(C, D, E, A, B, WR78, SR78);
301     RIP1(B, C, D, E, A, WR79, SR79);
302 
303     D = h[1] + c + D;
304     h[1] = h[2] + d + E;
305     h[2] = h[3] + e + A;
306     h[3] = h[4] + a + B;
307     h[4] = h[0] + b + C;
308     h[0] = D;
309   }
310 
311 #undef X
312 }
313 
RIPEMD160(const uint8_t * data,size_t len,unsigned char * out)314 uint8_t *RIPEMD160(const uint8_t *data, size_t len, unsigned char *out) {
315   RIPEMD160_CTX ctx;
316 
317   if (!RIPEMD160_Init(&ctx)) {
318     return NULL;
319   }
320 
321   RIPEMD160_Update(&ctx, data, len);
322   RIPEMD160_Final(out, &ctx);
323   return out;
324 }
325