Merge branch 'release-3.4-fixes' into master
[gd/nettle] / 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 #include "ripemd160-internal.h"
44
45 #include "macros.h"
46
47
48 /****************
49  * Transform the message X which consists of 16 32-bit-words
50  */
51 void
52 _nettle_ripemd160_compress(uint32_t *state, const uint8_t *data)
53 {
54   register uint32_t a,b,c,d,e;
55   uint32_t aa,bb,cc,dd,ee,t;
56   uint32_t x[16];
57
58 #ifdef WORDS_BIGENDIAN
59   {
60     int i;
61     for (i=0; i < 16; i++, data += 4 )
62       x[i] = LE_READ_UINT32(data);
63   }
64 #else
65   /* memcpy seems a bit faster. Benchmarked on Intel SU4100, it makes
66      the entire update function roughly 6% faster. */
67   memcpy(x, data, sizeof(x));
68 #endif
69
70
71 #define K0  0x00000000
72 #define K1  0x5A827999
73 #define K2  0x6ED9EBA1
74 #define K3  0x8F1BBCDC
75 #define K4  0xA953FD4E
76 #define KK0 0x50A28BE6
77 #define KK1 0x5C4DD124
78 #define KK2 0x6D703EF3
79 #define KK3 0x7A6D76E9
80 #define KK4 0x00000000
81 #define F0(x,y,z)   ( (x) ^ (y) ^ (z) )
82 #define F1(x,y,z)   ( ((x) & (y)) | (~(x) & (z)) )
83 #define F2(x,y,z)   ( ((x) | ~(y)) ^ (z) )
84 #define F3(x,y,z)   ( ((x) & (z)) | ((y) & ~(z)) )
85 #define F4(x,y,z)   ( (x) ^ ((y) | ~(z)) )
86 #define R(a,b,c,d,e,f,k,r,s) do { t = a + f(b,c,d) + k + x[r]; \
87           a = ROTL32(s,t) + e;        \
88           c = ROTL32(10,c);         \
89         } while(0)
90
91   /* left lane */
92   a = state[0];
93   b = state[1];
94   c = state[2];
95   d = state[3];
96   e = state[4];
97   R( a, b, c, d, e, F0, K0,  0, 11 );
98   R( e, a, b, c, d, F0, K0,  1, 14 );
99   R( d, e, a, b, c, F0, K0,  2, 15 );
100   R( c, d, e, a, b, F0, K0,  3, 12 );
101   R( b, c, d, e, a, F0, K0,  4,  5 );
102   R( a, b, c, d, e, F0, K0,  5,  8 );
103   R( e, a, b, c, d, F0, K0,  6,  7 );
104   R( d, e, a, b, c, F0, K0,  7,  9 );
105   R( c, d, e, a, b, F0, K0,  8, 11 );
106   R( b, c, d, e, a, F0, K0,  9, 13 );
107   R( a, b, c, d, e, F0, K0, 10, 14 );
108   R( e, a, b, c, d, F0, K0, 11, 15 );
109   R( d, e, a, b, c, F0, K0, 12,  6 );
110   R( c, d, e, a, b, F0, K0, 13,  7 );
111   R( b, c, d, e, a, F0, K0, 14,  9 );
112   R( a, b, c, d, e, F0, K0, 15,  8 );
113   R( e, a, b, c, d, F1, K1,  7,  7 );
114   R( d, e, a, b, c, F1, K1,  4,  6 );
115   R( c, d, e, a, b, F1, K1, 13,  8 );
116   R( b, c, d, e, a, F1, K1,  1, 13 );
117   R( a, b, c, d, e, F1, K1, 10, 11 );
118   R( e, a, b, c, d, F1, K1,  6,  9 );
119   R( d, e, a, b, c, F1, K1, 15,  7 );
120   R( c, d, e, a, b, F1, K1,  3, 15 );
121   R( b, c, d, e, a, F1, K1, 12,  7 );
122   R( a, b, c, d, e, F1, K1,  0, 12 );
123   R( e, a, b, c, d, F1, K1,  9, 15 );
124   R( d, e, a, b, c, F1, K1,  5,  9 );
125   R( c, d, e, a, b, F1, K1,  2, 11 );
126   R( b, c, d, e, a, F1, K1, 14,  7 );
127   R( a, b, c, d, e, F1, K1, 11, 13 );
128   R( e, a, b, c, d, F1, K1,  8, 12 );
129   R( d, e, a, b, c, F2, K2,  3, 11 );
130   R( c, d, e, a, b, F2, K2, 10, 13 );
131   R( b, c, d, e, a, F2, K2, 14,  6 );
132   R( a, b, c, d, e, F2, K2,  4,  7 );
133   R( e, a, b, c, d, F2, K2,  9, 14 );
134   R( d, e, a, b, c, F2, K2, 15,  9 );
135   R( c, d, e, a, b, F2, K2,  8, 13 );
136   R( b, c, d, e, a, F2, K2,  1, 15 );
137   R( a, b, c, d, e, F2, K2,  2, 14 );
138   R( e, a, b, c, d, F2, K2,  7,  8 );
139   R( d, e, a, b, c, F2, K2,  0, 13 );
140   R( c, d, e, a, b, F2, K2,  6,  6 );
141   R( b, c, d, e, a, F2, K2, 13,  5 );
142   R( a, b, c, d, e, F2, K2, 11, 12 );
143   R( e, a, b, c, d, F2, K2,  5,  7 );
144   R( d, e, a, b, c, F2, K2, 12,  5 );
145   R( c, d, e, a, b, F3, K3,  1, 11 );
146   R( b, c, d, e, a, F3, K3,  9, 12 );
147   R( a, b, c, d, e, F3, K3, 11, 14 );
148   R( e, a, b, c, d, F3, K3, 10, 15 );
149   R( d, e, a, b, c, F3, K3,  0, 14 );
150   R( c, d, e, a, b, F3, K3,  8, 15 );
151   R( b, c, d, e, a, F3, K3, 12,  9 );
152   R( a, b, c, d, e, F3, K3,  4,  8 );
153   R( e, a, b, c, d, F3, K3, 13,  9 );
154   R( d, e, a, b, c, F3, K3,  3, 14 );
155   R( c, d, e, a, b, F3, K3,  7,  5 );
156   R( b, c, d, e, a, F3, K3, 15,  6 );
157   R( a, b, c, d, e, F3, K3, 14,  8 );
158   R( e, a, b, c, d, F3, K3,  5,  6 );
159   R( d, e, a, b, c, F3, K3,  6,  5 );
160   R( c, d, e, a, b, F3, K3,  2, 12 );
161   R( b, c, d, e, a, F4, K4,  4,  9 );
162   R( a, b, c, d, e, F4, K4,  0, 15 );
163   R( e, a, b, c, d, F4, K4,  5,  5 );
164   R( d, e, a, b, c, F4, K4,  9, 11 );
165   R( c, d, e, a, b, F4, K4,  7,  6 );
166   R( b, c, d, e, a, F4, K4, 12,  8 );
167   R( a, b, c, d, e, F4, K4,  2, 13 );
168   R( e, a, b, c, d, F4, K4, 10, 12 );
169   R( d, e, a, b, c, F4, K4, 14,  5 );
170   R( c, d, e, a, b, F4, K4,  1, 12 );
171   R( b, c, d, e, a, F4, K4,  3, 13 );
172   R( a, b, c, d, e, F4, K4,  8, 14 );
173   R( e, a, b, c, d, F4, K4, 11, 11 );
174   R( d, e, a, b, c, F4, K4,  6,  8 );
175   R( c, d, e, a, b, F4, K4, 15,  5 );
176   R( b, c, d, e, a, F4, K4, 13,  6 );
177
178   aa = a; bb = b; cc = c; dd = d; ee = e;
179
180   /* right lane */
181   a = state[0];
182   b = state[1];
183   c = state[2];
184   d = state[3];
185   e = state[4];
186   R( a, b, c, d, e, F4, KK0,  5,  8);
187   R( e, a, b, c, d, F4, KK0, 14,  9);
188   R( d, e, a, b, c, F4, KK0,  7,  9);
189   R( c, d, e, a, b, F4, KK0,  0, 11);
190   R( b, c, d, e, a, F4, KK0,  9, 13);
191   R( a, b, c, d, e, F4, KK0,  2, 15);
192   R( e, a, b, c, d, F4, KK0, 11, 15);
193   R( d, e, a, b, c, F4, KK0,  4,  5);
194   R( c, d, e, a, b, F4, KK0, 13,  7);
195   R( b, c, d, e, a, F4, KK0,  6,  7);
196   R( a, b, c, d, e, F4, KK0, 15,  8);
197   R( e, a, b, c, d, F4, KK0,  8, 11);
198   R( d, e, a, b, c, F4, KK0,  1, 14);
199   R( c, d, e, a, b, F4, KK0, 10, 14);
200   R( b, c, d, e, a, F4, KK0,  3, 12);
201   R( a, b, c, d, e, F4, KK0, 12,  6);
202   R( e, a, b, c, d, F3, KK1,  6,  9);
203   R( d, e, a, b, c, F3, KK1, 11, 13);
204   R( c, d, e, a, b, F3, KK1,  3, 15);
205   R( b, c, d, e, a, F3, KK1,  7,  7);
206   R( a, b, c, d, e, F3, KK1,  0, 12);
207   R( e, a, b, c, d, F3, KK1, 13,  8);
208   R( d, e, a, b, c, F3, KK1,  5,  9);
209   R( c, d, e, a, b, F3, KK1, 10, 11);
210   R( b, c, d, e, a, F3, KK1, 14,  7);
211   R( a, b, c, d, e, F3, KK1, 15,  7);
212   R( e, a, b, c, d, F3, KK1,  8, 12);
213   R( d, e, a, b, c, F3, KK1, 12,  7);
214   R( c, d, e, a, b, F3, KK1,  4,  6);
215   R( b, c, d, e, a, F3, KK1,  9, 15);
216   R( a, b, c, d, e, F3, KK1,  1, 13);
217   R( e, a, b, c, d, F3, KK1,  2, 11);
218   R( d, e, a, b, c, F2, KK2, 15,  9);
219   R( c, d, e, a, b, F2, KK2,  5,  7);
220   R( b, c, d, e, a, F2, KK2,  1, 15);
221   R( a, b, c, d, e, F2, KK2,  3, 11);
222   R( e, a, b, c, d, F2, KK2,  7,  8);
223   R( d, e, a, b, c, F2, KK2, 14,  6);
224   R( c, d, e, a, b, F2, KK2,  6,  6);
225   R( b, c, d, e, a, F2, KK2,  9, 14);
226   R( a, b, c, d, e, F2, KK2, 11, 12);
227   R( e, a, b, c, d, F2, KK2,  8, 13);
228   R( d, e, a, b, c, F2, KK2, 12,  5);
229   R( c, d, e, a, b, F2, KK2,  2, 14);
230   R( b, c, d, e, a, F2, KK2, 10, 13);
231   R( a, b, c, d, e, F2, KK2,  0, 13);
232   R( e, a, b, c, d, F2, KK2,  4,  7);
233   R( d, e, a, b, c, F2, KK2, 13,  5);
234   R( c, d, e, a, b, F1, KK3,  8, 15);
235   R( b, c, d, e, a, F1, KK3,  6,  5);
236   R( a, b, c, d, e, F1, KK3,  4,  8);
237   R( e, a, b, c, d, F1, KK3,  1, 11);
238   R( d, e, a, b, c, F1, KK3,  3, 14);
239   R( c, d, e, a, b, F1, KK3, 11, 14);
240   R( b, c, d, e, a, F1, KK3, 15,  6);
241   R( a, b, c, d, e, F1, KK3,  0, 14);
242   R( e, a, b, c, d, F1, KK3,  5,  6);
243   R( d, e, a, b, c, F1, KK3, 12,  9);
244   R( c, d, e, a, b, F1, KK3,  2, 12);
245   R( b, c, d, e, a, F1, KK3, 13,  9);
246   R( a, b, c, d, e, F1, KK3,  9, 12);
247   R( e, a, b, c, d, F1, KK3,  7,  5);
248   R( d, e, a, b, c, F1, KK3, 10, 15);
249   R( c, d, e, a, b, F1, KK3, 14,  8);
250   R( b, c, d, e, a, F0, KK4, 12,  8);
251   R( a, b, c, d, e, F0, KK4, 15,  5);
252   R( e, a, b, c, d, F0, KK4, 10, 12);
253   R( d, e, a, b, c, F0, KK4,  4,  9);
254   R( c, d, e, a, b, F0, KK4,  1, 12);
255   R( b, c, d, e, a, F0, KK4,  5,  5);
256   R( a, b, c, d, e, F0, KK4,  8, 14);
257   R( e, a, b, c, d, F0, KK4,  7,  6);
258   R( d, e, a, b, c, F0, KK4,  6,  8);
259   R( c, d, e, a, b, F0, KK4,  2, 13);
260   R( b, c, d, e, a, F0, KK4, 13,  6);
261   R( a, b, c, d, e, F0, KK4, 14,  5);
262   R( e, a, b, c, d, F0, KK4,  0, 15);
263   R( d, e, a, b, c, F0, KK4,  3, 13);
264   R( c, d, e, a, b, F0, KK4,  9, 11);
265   R( b, c, d, e, a, F0, KK4, 11, 11);
266
267
268   t    = state[1] + d + cc;
269   state[1] = state[2] + e + dd;
270   state[2] = state[3] + a + ee;
271   state[3] = state[4] + b + aa;
272   state[4] = state[0] + c + bb;
273   state[0] = t;
274 }