443b255ca26e7021d91781f990ae929e5fd69e7a
[platform/upstream/nettle.git] / ripemd160-compress.c
1 /* ripemd160-compress.c
2
3    RIPE-MD160 (Transform function)
4
5    Copyright (C) 1998, 2001, 2002, 2003 Free Software Foundation, Inc.
6
7    This file is part of GNU Nettle.
8
9    GNU Nettle is free software: you can redistribute it and/or
10    modify it under the terms of either:
11
12      * the GNU Lesser General Public License as published by the Free
13        Software Foundation; either version 3 of the License, or (at your
14        option) any later version.
15
16    or
17
18      * the GNU General Public License as published by the Free
19        Software Foundation; either version 2 of the License, or (at your
20        option) any later version.
21
22    or both in parallel, as here.
23
24    GNU Nettle is distributed in the hope that it will be useful,
25    but WITHOUT ANY WARRANTY; without even the implied warranty of
26    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
27    General Public License for more details.
28
29    You should have received copies of the GNU General Public License and
30    the GNU Lesser General Public License along with this program.  If
31    not, see http://www.gnu.org/licenses/.
32 */
33
34 /* Ported from libgcrypt by Andres Mejia <mcitadel@gmail.com> */
35
36 #if HAVE_CONFIG_H
37 # include "config.h"
38 #endif
39
40 #include <string.h>
41
42 #include "ripemd160.h"
43
44 #include "macros.h"
45
46
47 /****************
48  * Transform the message X which consists of 16 32-bit-words
49  */
50 void
51 _nettle_ripemd160_compress(uint32_t *state, const uint8_t *data)
52 {
53   register uint32_t a,b,c,d,e;
54   uint32_t aa,bb,cc,dd,ee,t;
55   uint32_t x[16];
56
57 #ifdef WORDS_BIGENDIAN
58   {
59     int i;
60     for (i=0; i < 16; i++, data += 4 )
61       x[i] = LE_READ_UINT32(data);
62   }
63 #else
64   /* memcpy seems a bit faster. Benchmarked on Intel SU4100, it makes
65      the entire update function roughly 6% faster. */
66   memcpy(x, data, sizeof(x));
67 #endif
68
69
70 #define K0  0x00000000
71 #define K1  0x5A827999
72 #define K2  0x6ED9EBA1
73 #define K3  0x8F1BBCDC
74 #define K4  0xA953FD4E
75 #define KK0 0x50A28BE6
76 #define KK1 0x5C4DD124
77 #define KK2 0x6D703EF3
78 #define KK3 0x7A6D76E9
79 #define KK4 0x00000000
80 #define F0(x,y,z)   ( (x) ^ (y) ^ (z) )
81 #define F1(x,y,z)   ( ((x) & (y)) | (~(x) & (z)) )
82 #define F2(x,y,z)   ( ((x) | ~(y)) ^ (z) )
83 #define F3(x,y,z)   ( ((x) & (z)) | ((y) & ~(z)) )
84 #define F4(x,y,z)   ( (x) ^ ((y) | ~(z)) )
85 #define R(a,b,c,d,e,f,k,r,s) do { t = a + f(b,c,d) + k + x[r]; \
86           a = ROTL32(s,t) + e;        \
87           c = ROTL32(10,c);         \
88         } while(0)
89
90   /* left lane */
91   a = state[0];
92   b = state[1];
93   c = state[2];
94   d = state[3];
95   e = state[4];
96   R( a, b, c, d, e, F0, K0,  0, 11 );
97   R( e, a, b, c, d, F0, K0,  1, 14 );
98   R( d, e, a, b, c, F0, K0,  2, 15 );
99   R( c, d, e, a, b, F0, K0,  3, 12 );
100   R( b, c, d, e, a, F0, K0,  4,  5 );
101   R( a, b, c, d, e, F0, K0,  5,  8 );
102   R( e, a, b, c, d, F0, K0,  6,  7 );
103   R( d, e, a, b, c, F0, K0,  7,  9 );
104   R( c, d, e, a, b, F0, K0,  8, 11 );
105   R( b, c, d, e, a, F0, K0,  9, 13 );
106   R( a, b, c, d, e, F0, K0, 10, 14 );
107   R( e, a, b, c, d, F0, K0, 11, 15 );
108   R( d, e, a, b, c, F0, K0, 12,  6 );
109   R( c, d, e, a, b, F0, K0, 13,  7 );
110   R( b, c, d, e, a, F0, K0, 14,  9 );
111   R( a, b, c, d, e, F0, K0, 15,  8 );
112   R( e, a, b, c, d, F1, K1,  7,  7 );
113   R( d, e, a, b, c, F1, K1,  4,  6 );
114   R( c, d, e, a, b, F1, K1, 13,  8 );
115   R( b, c, d, e, a, F1, K1,  1, 13 );
116   R( a, b, c, d, e, F1, K1, 10, 11 );
117   R( e, a, b, c, d, F1, K1,  6,  9 );
118   R( d, e, a, b, c, F1, K1, 15,  7 );
119   R( c, d, e, a, b, F1, K1,  3, 15 );
120   R( b, c, d, e, a, F1, K1, 12,  7 );
121   R( a, b, c, d, e, F1, K1,  0, 12 );
122   R( e, a, b, c, d, F1, K1,  9, 15 );
123   R( d, e, a, b, c, F1, K1,  5,  9 );
124   R( c, d, e, a, b, F1, K1,  2, 11 );
125   R( b, c, d, e, a, F1, K1, 14,  7 );
126   R( a, b, c, d, e, F1, K1, 11, 13 );
127   R( e, a, b, c, d, F1, K1,  8, 12 );
128   R( d, e, a, b, c, F2, K2,  3, 11 );
129   R( c, d, e, a, b, F2, K2, 10, 13 );
130   R( b, c, d, e, a, F2, K2, 14,  6 );
131   R( a, b, c, d, e, F2, K2,  4,  7 );
132   R( e, a, b, c, d, F2, K2,  9, 14 );
133   R( d, e, a, b, c, F2, K2, 15,  9 );
134   R( c, d, e, a, b, F2, K2,  8, 13 );
135   R( b, c, d, e, a, F2, K2,  1, 15 );
136   R( a, b, c, d, e, F2, K2,  2, 14 );
137   R( e, a, b, c, d, F2, K2,  7,  8 );
138   R( d, e, a, b, c, F2, K2,  0, 13 );
139   R( c, d, e, a, b, F2, K2,  6,  6 );
140   R( b, c, d, e, a, F2, K2, 13,  5 );
141   R( a, b, c, d, e, F2, K2, 11, 12 );
142   R( e, a, b, c, d, F2, K2,  5,  7 );
143   R( d, e, a, b, c, F2, K2, 12,  5 );
144   R( c, d, e, a, b, F3, K3,  1, 11 );
145   R( b, c, d, e, a, F3, K3,  9, 12 );
146   R( a, b, c, d, e, F3, K3, 11, 14 );
147   R( e, a, b, c, d, F3, K3, 10, 15 );
148   R( d, e, a, b, c, F3, K3,  0, 14 );
149   R( c, d, e, a, b, F3, K3,  8, 15 );
150   R( b, c, d, e, a, F3, K3, 12,  9 );
151   R( a, b, c, d, e, F3, K3,  4,  8 );
152   R( e, a, b, c, d, F3, K3, 13,  9 );
153   R( d, e, a, b, c, F3, K3,  3, 14 );
154   R( c, d, e, a, b, F3, K3,  7,  5 );
155   R( b, c, d, e, a, F3, K3, 15,  6 );
156   R( a, b, c, d, e, F3, K3, 14,  8 );
157   R( e, a, b, c, d, F3, K3,  5,  6 );
158   R( d, e, a, b, c, F3, K3,  6,  5 );
159   R( c, d, e, a, b, F3, K3,  2, 12 );
160   R( b, c, d, e, a, F4, K4,  4,  9 );
161   R( a, b, c, d, e, F4, K4,  0, 15 );
162   R( e, a, b, c, d, F4, K4,  5,  5 );
163   R( d, e, a, b, c, F4, K4,  9, 11 );
164   R( c, d, e, a, b, F4, K4,  7,  6 );
165   R( b, c, d, e, a, F4, K4, 12,  8 );
166   R( a, b, c, d, e, F4, K4,  2, 13 );
167   R( e, a, b, c, d, F4, K4, 10, 12 );
168   R( d, e, a, b, c, F4, K4, 14,  5 );
169   R( c, d, e, a, b, F4, K4,  1, 12 );
170   R( b, c, d, e, a, F4, K4,  3, 13 );
171   R( a, b, c, d, e, F4, K4,  8, 14 );
172   R( e, a, b, c, d, F4, K4, 11, 11 );
173   R( d, e, a, b, c, F4, K4,  6,  8 );
174   R( c, d, e, a, b, F4, K4, 15,  5 );
175   R( b, c, d, e, a, F4, K4, 13,  6 );
176
177   aa = a; bb = b; cc = c; dd = d; ee = e;
178
179   /* right lane */
180   a = state[0];
181   b = state[1];
182   c = state[2];
183   d = state[3];
184   e = state[4];
185   R( a, b, c, d, e, F4, KK0,  5,  8);
186   R( e, a, b, c, d, F4, KK0, 14,  9);
187   R( d, e, a, b, c, F4, KK0,  7,  9);
188   R( c, d, e, a, b, F4, KK0,  0, 11);
189   R( b, c, d, e, a, F4, KK0,  9, 13);
190   R( a, b, c, d, e, F4, KK0,  2, 15);
191   R( e, a, b, c, d, F4, KK0, 11, 15);
192   R( d, e, a, b, c, F4, KK0,  4,  5);
193   R( c, d, e, a, b, F4, KK0, 13,  7);
194   R( b, c, d, e, a, F4, KK0,  6,  7);
195   R( a, b, c, d, e, F4, KK0, 15,  8);
196   R( e, a, b, c, d, F4, KK0,  8, 11);
197   R( d, e, a, b, c, F4, KK0,  1, 14);
198   R( c, d, e, a, b, F4, KK0, 10, 14);
199   R( b, c, d, e, a, F4, KK0,  3, 12);
200   R( a, b, c, d, e, F4, KK0, 12,  6);
201   R( e, a, b, c, d, F3, KK1,  6,  9);
202   R( d, e, a, b, c, F3, KK1, 11, 13);
203   R( c, d, e, a, b, F3, KK1,  3, 15);
204   R( b, c, d, e, a, F3, KK1,  7,  7);
205   R( a, b, c, d, e, F3, KK1,  0, 12);
206   R( e, a, b, c, d, F3, KK1, 13,  8);
207   R( d, e, a, b, c, F3, KK1,  5,  9);
208   R( c, d, e, a, b, F3, KK1, 10, 11);
209   R( b, c, d, e, a, F3, KK1, 14,  7);
210   R( a, b, c, d, e, F3, KK1, 15,  7);
211   R( e, a, b, c, d, F3, KK1,  8, 12);
212   R( d, e, a, b, c, F3, KK1, 12,  7);
213   R( c, d, e, a, b, F3, KK1,  4,  6);
214   R( b, c, d, e, a, F3, KK1,  9, 15);
215   R( a, b, c, d, e, F3, KK1,  1, 13);
216   R( e, a, b, c, d, F3, KK1,  2, 11);
217   R( d, e, a, b, c, F2, KK2, 15,  9);
218   R( c, d, e, a, b, F2, KK2,  5,  7);
219   R( b, c, d, e, a, F2, KK2,  1, 15);
220   R( a, b, c, d, e, F2, KK2,  3, 11);
221   R( e, a, b, c, d, F2, KK2,  7,  8);
222   R( d, e, a, b, c, F2, KK2, 14,  6);
223   R( c, d, e, a, b, F2, KK2,  6,  6);
224   R( b, c, d, e, a, F2, KK2,  9, 14);
225   R( a, b, c, d, e, F2, KK2, 11, 12);
226   R( e, a, b, c, d, F2, KK2,  8, 13);
227   R( d, e, a, b, c, F2, KK2, 12,  5);
228   R( c, d, e, a, b, F2, KK2,  2, 14);
229   R( b, c, d, e, a, F2, KK2, 10, 13);
230   R( a, b, c, d, e, F2, KK2,  0, 13);
231   R( e, a, b, c, d, F2, KK2,  4,  7);
232   R( d, e, a, b, c, F2, KK2, 13,  5);
233   R( c, d, e, a, b, F1, KK3,  8, 15);
234   R( b, c, d, e, a, F1, KK3,  6,  5);
235   R( a, b, c, d, e, F1, KK3,  4,  8);
236   R( e, a, b, c, d, F1, KK3,  1, 11);
237   R( d, e, a, b, c, F1, KK3,  3, 14);
238   R( c, d, e, a, b, F1, KK3, 11, 14);
239   R( b, c, d, e, a, F1, KK3, 15,  6);
240   R( a, b, c, d, e, F1, KK3,  0, 14);
241   R( e, a, b, c, d, F1, KK3,  5,  6);
242   R( d, e, a, b, c, F1, KK3, 12,  9);
243   R( c, d, e, a, b, F1, KK3,  2, 12);
244   R( b, c, d, e, a, F1, KK3, 13,  9);
245   R( a, b, c, d, e, F1, KK3,  9, 12);
246   R( e, a, b, c, d, F1, KK3,  7,  5);
247   R( d, e, a, b, c, F1, KK3, 10, 15);
248   R( c, d, e, a, b, F1, KK3, 14,  8);
249   R( b, c, d, e, a, F0, KK4, 12,  8);
250   R( a, b, c, d, e, F0, KK4, 15,  5);
251   R( e, a, b, c, d, F0, KK4, 10, 12);
252   R( d, e, a, b, c, F0, KK4,  4,  9);
253   R( c, d, e, a, b, F0, KK4,  1, 12);
254   R( b, c, d, e, a, F0, KK4,  5,  5);
255   R( a, b, c, d, e, F0, KK4,  8, 14);
256   R( e, a, b, c, d, F0, KK4,  7,  6);
257   R( d, e, a, b, c, F0, KK4,  6,  8);
258   R( c, d, e, a, b, F0, KK4,  2, 13);
259   R( b, c, d, e, a, F0, KK4, 13,  6);
260   R( a, b, c, d, e, F0, KK4, 14,  5);
261   R( e, a, b, c, d, F0, KK4,  0, 15);
262   R( d, e, a, b, c, F0, KK4,  3, 13);
263   R( c, d, e, a, b, F0, KK4,  9, 11);
264   R( b, c, d, e, a, F0, KK4, 11, 11);
265
266
267   t    = state[1] + d + cc;
268   state[1] = state[2] + e + dd;
269   state[2] = state[3] + a + ee;
270   state[3] = state[4] + b + aa;
271   state[4] = state[0] + c + bb;
272   state[0] = t;
273 }