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