source: UsbWattMeter/trunk/wolfssl-3.7.0/wolfcrypt/src/ripemd.c@ 164

Last change on this file since 164 was 164, checked in by coas-nagasima, 6 years ago

TOPPERS/ECNLサンプルアプリ「USB充電器電力計」を追加

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-csrc
File size: 15.3 KB
Line 
1/* ripemd.c
2 *
3 * Copyright (C) 2006-2015 wolfSSL Inc.
4 *
5 * This file is part of wolfSSL. (formerly known as CyaSSL)
6 *
7 * wolfSSL is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * wolfSSL is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
20 */
21
22
23#ifdef HAVE_CONFIG_H
24 #include <config.h>
25#endif
26
27#include <wolfssl/wolfcrypt/settings.h>
28
29#ifdef WOLFSSL_RIPEMD
30
31#include <wolfssl/wolfcrypt/ripemd.h>
32#ifdef NO_INLINE
33 #include <wolfssl/wolfcrypt/misc.h>
34#else
35 #include <wolfcrypt/src/misc.c>
36#endif
37
38
39#ifdef min
40#define WOLFSSL_HAVE_MIN
41#endif
42#ifndef WOLFSSL_HAVE_MIN
43#define WOLFSSL_HAVE_MIN
44
45 static INLINE word32 min(word32 a, word32 b)
46 {
47 return a > b ? b : a;
48 }
49
50#endif /* WOLFSSL_HAVE_MIN */
51
52void wc_InitRipeMd(RipeMd* ripemd)
53{
54 ripemd->digest[0] = 0x67452301L;
55 ripemd->digest[1] = 0xEFCDAB89L;
56 ripemd->digest[2] = 0x98BADCFEL;
57 ripemd->digest[3] = 0x10325476L;
58 ripemd->digest[4] = 0xC3D2E1F0L;
59
60 ripemd->buffLen = 0;
61 ripemd->loLen = 0;
62 ripemd->hiLen = 0;
63}
64
65
66/* for all */
67#define F(x, y, z) (x ^ y ^ z)
68#define G(x, y, z) (z ^ (x & (y^z)))
69#define H(x, y, z) (z ^ (x | ~y))
70#define I(x, y, z) (y ^ (z & (x^y)))
71#define J(x, y, z) (x ^ (y | ~z))
72
73#define k0 0
74#define k1 0x5a827999
75#define k2 0x6ed9eba1
76#define k3 0x8f1bbcdc
77#define k4 0xa953fd4e
78#define k5 0x50a28be6
79#define k6 0x5c4dd124
80#define k7 0x6d703ef3
81#define k8 0x7a6d76e9
82#define k9 0
83
84/* for 160 and 320 */
85#define Subround(f, a, b, c, d, e, x, s, k) \
86 a += f(b, c, d) + x + k;\
87 a = rotlFixed((word32)a, s) + e;\
88 c = rotlFixed((word32)c, 10U)
89
90static void Transform(RipeMd* ripemd)
91{
92 word32 a1, b1, c1, d1, e1, a2, b2, c2, d2, e2;
93 a1 = a2 = ripemd->digest[0];
94 b1 = b2 = ripemd->digest[1];
95 c1 = c2 = ripemd->digest[2];
96 d1 = d2 = ripemd->digest[3];
97 e1 = e2 = ripemd->digest[4];
98
99 Subround(F, a1, b1, c1, d1, e1, ripemd->buffer[ 0], 11, k0);
100 Subround(F, e1, a1, b1, c1, d1, ripemd->buffer[ 1], 14, k0);
101 Subround(F, d1, e1, a1, b1, c1, ripemd->buffer[ 2], 15, k0);
102 Subround(F, c1, d1, e1, a1, b1, ripemd->buffer[ 3], 12, k0);
103 Subround(F, b1, c1, d1, e1, a1, ripemd->buffer[ 4], 5, k0);
104 Subround(F, a1, b1, c1, d1, e1, ripemd->buffer[ 5], 8, k0);
105 Subround(F, e1, a1, b1, c1, d1, ripemd->buffer[ 6], 7, k0);
106 Subround(F, d1, e1, a1, b1, c1, ripemd->buffer[ 7], 9, k0);
107 Subround(F, c1, d1, e1, a1, b1, ripemd->buffer[ 8], 11, k0);
108 Subround(F, b1, c1, d1, e1, a1, ripemd->buffer[ 9], 13, k0);
109 Subround(F, a1, b1, c1, d1, e1, ripemd->buffer[10], 14, k0);
110 Subround(F, e1, a1, b1, c1, d1, ripemd->buffer[11], 15, k0);
111 Subround(F, d1, e1, a1, b1, c1, ripemd->buffer[12], 6, k0);
112 Subround(F, c1, d1, e1, a1, b1, ripemd->buffer[13], 7, k0);
113 Subround(F, b1, c1, d1, e1, a1, ripemd->buffer[14], 9, k0);
114 Subround(F, a1, b1, c1, d1, e1, ripemd->buffer[15], 8, k0);
115
116 Subround(G, e1, a1, b1, c1, d1, ripemd->buffer[ 7], 7, k1);
117 Subround(G, d1, e1, a1, b1, c1, ripemd->buffer[ 4], 6, k1);
118 Subround(G, c1, d1, e1, a1, b1, ripemd->buffer[13], 8, k1);
119 Subround(G, b1, c1, d1, e1, a1, ripemd->buffer[ 1], 13, k1);
120 Subround(G, a1, b1, c1, d1, e1, ripemd->buffer[10], 11, k1);
121 Subround(G, e1, a1, b1, c1, d1, ripemd->buffer[ 6], 9, k1);
122 Subround(G, d1, e1, a1, b1, c1, ripemd->buffer[15], 7, k1);
123 Subround(G, c1, d1, e1, a1, b1, ripemd->buffer[ 3], 15, k1);
124 Subround(G, b1, c1, d1, e1, a1, ripemd->buffer[12], 7, k1);
125 Subround(G, a1, b1, c1, d1, e1, ripemd->buffer[ 0], 12, k1);
126 Subround(G, e1, a1, b1, c1, d1, ripemd->buffer[ 9], 15, k1);
127 Subround(G, d1, e1, a1, b1, c1, ripemd->buffer[ 5], 9, k1);
128 Subround(G, c1, d1, e1, a1, b1, ripemd->buffer[ 2], 11, k1);
129 Subround(G, b1, c1, d1, e1, a1, ripemd->buffer[14], 7, k1);
130 Subround(G, a1, b1, c1, d1, e1, ripemd->buffer[11], 13, k1);
131 Subround(G, e1, a1, b1, c1, d1, ripemd->buffer[ 8], 12, k1);
132
133 Subround(H, d1, e1, a1, b1, c1, ripemd->buffer[ 3], 11, k2);
134 Subround(H, c1, d1, e1, a1, b1, ripemd->buffer[10], 13, k2);
135 Subround(H, b1, c1, d1, e1, a1, ripemd->buffer[14], 6, k2);
136 Subround(H, a1, b1, c1, d1, e1, ripemd->buffer[ 4], 7, k2);
137 Subround(H, e1, a1, b1, c1, d1, ripemd->buffer[ 9], 14, k2);
138 Subround(H, d1, e1, a1, b1, c1, ripemd->buffer[15], 9, k2);
139 Subround(H, c1, d1, e1, a1, b1, ripemd->buffer[ 8], 13, k2);
140 Subround(H, b1, c1, d1, e1, a1, ripemd->buffer[ 1], 15, k2);
141 Subround(H, a1, b1, c1, d1, e1, ripemd->buffer[ 2], 14, k2);
142 Subround(H, e1, a1, b1, c1, d1, ripemd->buffer[ 7], 8, k2);
143 Subround(H, d1, e1, a1, b1, c1, ripemd->buffer[ 0], 13, k2);
144 Subround(H, c1, d1, e1, a1, b1, ripemd->buffer[ 6], 6, k2);
145 Subround(H, b1, c1, d1, e1, a1, ripemd->buffer[13], 5, k2);
146 Subround(H, a1, b1, c1, d1, e1, ripemd->buffer[11], 12, k2);
147 Subround(H, e1, a1, b1, c1, d1, ripemd->buffer[ 5], 7, k2);
148 Subround(H, d1, e1, a1, b1, c1, ripemd->buffer[12], 5, k2);
149
150 Subround(I, c1, d1, e1, a1, b1, ripemd->buffer[ 1], 11, k3);
151 Subround(I, b1, c1, d1, e1, a1, ripemd->buffer[ 9], 12, k3);
152 Subround(I, a1, b1, c1, d1, e1, ripemd->buffer[11], 14, k3);
153 Subround(I, e1, a1, b1, c1, d1, ripemd->buffer[10], 15, k3);
154 Subround(I, d1, e1, a1, b1, c1, ripemd->buffer[ 0], 14, k3);
155 Subround(I, c1, d1, e1, a1, b1, ripemd->buffer[ 8], 15, k3);
156 Subround(I, b1, c1, d1, e1, a1, ripemd->buffer[12], 9, k3);
157 Subround(I, a1, b1, c1, d1, e1, ripemd->buffer[ 4], 8, k3);
158 Subround(I, e1, a1, b1, c1, d1, ripemd->buffer[13], 9, k3);
159 Subround(I, d1, e1, a1, b1, c1, ripemd->buffer[ 3], 14, k3);
160 Subround(I, c1, d1, e1, a1, b1, ripemd->buffer[ 7], 5, k3);
161 Subround(I, b1, c1, d1, e1, a1, ripemd->buffer[15], 6, k3);
162 Subround(I, a1, b1, c1, d1, e1, ripemd->buffer[14], 8, k3);
163 Subround(I, e1, a1, b1, c1, d1, ripemd->buffer[ 5], 6, k3);
164 Subround(I, d1, e1, a1, b1, c1, ripemd->buffer[ 6], 5, k3);
165 Subround(I, c1, d1, e1, a1, b1, ripemd->buffer[ 2], 12, k3);
166
167 Subround(J, b1, c1, d1, e1, a1, ripemd->buffer[ 4], 9, k4);
168 Subround(J, a1, b1, c1, d1, e1, ripemd->buffer[ 0], 15, k4);
169 Subround(J, e1, a1, b1, c1, d1, ripemd->buffer[ 5], 5, k4);
170 Subround(J, d1, e1, a1, b1, c1, ripemd->buffer[ 9], 11, k4);
171 Subround(J, c1, d1, e1, a1, b1, ripemd->buffer[ 7], 6, k4);
172 Subround(J, b1, c1, d1, e1, a1, ripemd->buffer[12], 8, k4);
173 Subround(J, a1, b1, c1, d1, e1, ripemd->buffer[ 2], 13, k4);
174 Subround(J, e1, a1, b1, c1, d1, ripemd->buffer[10], 12, k4);
175 Subround(J, d1, e1, a1, b1, c1, ripemd->buffer[14], 5, k4);
176 Subround(J, c1, d1, e1, a1, b1, ripemd->buffer[ 1], 12, k4);
177 Subround(J, b1, c1, d1, e1, a1, ripemd->buffer[ 3], 13, k4);
178 Subround(J, a1, b1, c1, d1, e1, ripemd->buffer[ 8], 14, k4);
179 Subround(J, e1, a1, b1, c1, d1, ripemd->buffer[11], 11, k4);
180 Subround(J, d1, e1, a1, b1, c1, ripemd->buffer[ 6], 8, k4);
181 Subround(J, c1, d1, e1, a1, b1, ripemd->buffer[15], 5, k4);
182 Subround(J, b1, c1, d1, e1, a1, ripemd->buffer[13], 6, k4);
183
184 Subround(J, a2, b2, c2, d2, e2, ripemd->buffer[ 5], 8, k5);
185 Subround(J, e2, a2, b2, c2, d2, ripemd->buffer[14], 9, k5);
186 Subround(J, d2, e2, a2, b2, c2, ripemd->buffer[ 7], 9, k5);
187 Subround(J, c2, d2, e2, a2, b2, ripemd->buffer[ 0], 11, k5);
188 Subround(J, b2, c2, d2, e2, a2, ripemd->buffer[ 9], 13, k5);
189 Subround(J, a2, b2, c2, d2, e2, ripemd->buffer[ 2], 15, k5);
190 Subround(J, e2, a2, b2, c2, d2, ripemd->buffer[11], 15, k5);
191 Subround(J, d2, e2, a2, b2, c2, ripemd->buffer[ 4], 5, k5);
192 Subround(J, c2, d2, e2, a2, b2, ripemd->buffer[13], 7, k5);
193 Subround(J, b2, c2, d2, e2, a2, ripemd->buffer[ 6], 7, k5);
194 Subround(J, a2, b2, c2, d2, e2, ripemd->buffer[15], 8, k5);
195 Subround(J, e2, a2, b2, c2, d2, ripemd->buffer[ 8], 11, k5);
196 Subround(J, d2, e2, a2, b2, c2, ripemd->buffer[ 1], 14, k5);
197 Subround(J, c2, d2, e2, a2, b2, ripemd->buffer[10], 14, k5);
198 Subround(J, b2, c2, d2, e2, a2, ripemd->buffer[ 3], 12, k5);
199 Subround(J, a2, b2, c2, d2, e2, ripemd->buffer[12], 6, k5);
200
201 Subround(I, e2, a2, b2, c2, d2, ripemd->buffer[ 6], 9, k6);
202 Subround(I, d2, e2, a2, b2, c2, ripemd->buffer[11], 13, k6);
203 Subround(I, c2, d2, e2, a2, b2, ripemd->buffer[ 3], 15, k6);
204 Subround(I, b2, c2, d2, e2, a2, ripemd->buffer[ 7], 7, k6);
205 Subround(I, a2, b2, c2, d2, e2, ripemd->buffer[ 0], 12, k6);
206 Subround(I, e2, a2, b2, c2, d2, ripemd->buffer[13], 8, k6);
207 Subround(I, d2, e2, a2, b2, c2, ripemd->buffer[ 5], 9, k6);
208 Subround(I, c2, d2, e2, a2, b2, ripemd->buffer[10], 11, k6);
209 Subround(I, b2, c2, d2, e2, a2, ripemd->buffer[14], 7, k6);
210 Subround(I, a2, b2, c2, d2, e2, ripemd->buffer[15], 7, k6);
211 Subround(I, e2, a2, b2, c2, d2, ripemd->buffer[ 8], 12, k6);
212 Subround(I, d2, e2, a2, b2, c2, ripemd->buffer[12], 7, k6);
213 Subround(I, c2, d2, e2, a2, b2, ripemd->buffer[ 4], 6, k6);
214 Subround(I, b2, c2, d2, e2, a2, ripemd->buffer[ 9], 15, k6);
215 Subround(I, a2, b2, c2, d2, e2, ripemd->buffer[ 1], 13, k6);
216 Subround(I, e2, a2, b2, c2, d2, ripemd->buffer[ 2], 11, k6);
217
218 Subround(H, d2, e2, a2, b2, c2, ripemd->buffer[15], 9, k7);
219 Subround(H, c2, d2, e2, a2, b2, ripemd->buffer[ 5], 7, k7);
220 Subround(H, b2, c2, d2, e2, a2, ripemd->buffer[ 1], 15, k7);
221 Subround(H, a2, b2, c2, d2, e2, ripemd->buffer[ 3], 11, k7);
222 Subround(H, e2, a2, b2, c2, d2, ripemd->buffer[ 7], 8, k7);
223 Subround(H, d2, e2, a2, b2, c2, ripemd->buffer[14], 6, k7);
224 Subround(H, c2, d2, e2, a2, b2, ripemd->buffer[ 6], 6, k7);
225 Subround(H, b2, c2, d2, e2, a2, ripemd->buffer[ 9], 14, k7);
226 Subround(H, a2, b2, c2, d2, e2, ripemd->buffer[11], 12, k7);
227 Subround(H, e2, a2, b2, c2, d2, ripemd->buffer[ 8], 13, k7);
228 Subround(H, d2, e2, a2, b2, c2, ripemd->buffer[12], 5, k7);
229 Subround(H, c2, d2, e2, a2, b2, ripemd->buffer[ 2], 14, k7);
230 Subround(H, b2, c2, d2, e2, a2, ripemd->buffer[10], 13, k7);
231 Subround(H, a2, b2, c2, d2, e2, ripemd->buffer[ 0], 13, k7);
232 Subround(H, e2, a2, b2, c2, d2, ripemd->buffer[ 4], 7, k7);
233 Subround(H, d2, e2, a2, b2, c2, ripemd->buffer[13], 5, k7);
234
235 Subround(G, c2, d2, e2, a2, b2, ripemd->buffer[ 8], 15, k8);
236 Subround(G, b2, c2, d2, e2, a2, ripemd->buffer[ 6], 5, k8);
237 Subround(G, a2, b2, c2, d2, e2, ripemd->buffer[ 4], 8, k8);
238 Subround(G, e2, a2, b2, c2, d2, ripemd->buffer[ 1], 11, k8);
239 Subround(G, d2, e2, a2, b2, c2, ripemd->buffer[ 3], 14, k8);
240 Subround(G, c2, d2, e2, a2, b2, ripemd->buffer[11], 14, k8);
241 Subround(G, b2, c2, d2, e2, a2, ripemd->buffer[15], 6, k8);
242 Subround(G, a2, b2, c2, d2, e2, ripemd->buffer[ 0], 14, k8);
243 Subround(G, e2, a2, b2, c2, d2, ripemd->buffer[ 5], 6, k8);
244 Subround(G, d2, e2, a2, b2, c2, ripemd->buffer[12], 9, k8);
245 Subround(G, c2, d2, e2, a2, b2, ripemd->buffer[ 2], 12, k8);
246 Subround(G, b2, c2, d2, e2, a2, ripemd->buffer[13], 9, k8);
247 Subround(G, a2, b2, c2, d2, e2, ripemd->buffer[ 9], 12, k8);
248 Subround(G, e2, a2, b2, c2, d2, ripemd->buffer[ 7], 5, k8);
249 Subround(G, d2, e2, a2, b2, c2, ripemd->buffer[10], 15, k8);
250 Subround(G, c2, d2, e2, a2, b2, ripemd->buffer[14], 8, k8);
251
252 Subround(F, b2, c2, d2, e2, a2, ripemd->buffer[12], 8, k9);
253 Subround(F, a2, b2, c2, d2, e2, ripemd->buffer[15], 5, k9);
254 Subround(F, e2, a2, b2, c2, d2, ripemd->buffer[10], 12, k9);
255 Subround(F, d2, e2, a2, b2, c2, ripemd->buffer[ 4], 9, k9);
256 Subround(F, c2, d2, e2, a2, b2, ripemd->buffer[ 1], 12, k9);
257 Subround(F, b2, c2, d2, e2, a2, ripemd->buffer[ 5], 5, k9);
258 Subround(F, a2, b2, c2, d2, e2, ripemd->buffer[ 8], 14, k9);
259 Subround(F, e2, a2, b2, c2, d2, ripemd->buffer[ 7], 6, k9);
260 Subround(F, d2, e2, a2, b2, c2, ripemd->buffer[ 6], 8, k9);
261 Subround(F, c2, d2, e2, a2, b2, ripemd->buffer[ 2], 13, k9);
262 Subround(F, b2, c2, d2, e2, a2, ripemd->buffer[13], 6, k9);
263 Subround(F, a2, b2, c2, d2, e2, ripemd->buffer[14], 5, k9);
264 Subround(F, e2, a2, b2, c2, d2, ripemd->buffer[ 0], 15, k9);
265 Subround(F, d2, e2, a2, b2, c2, ripemd->buffer[ 3], 13, k9);
266 Subround(F, c2, d2, e2, a2, b2, ripemd->buffer[ 9], 11, k9);
267 Subround(F, b2, c2, d2, e2, a2, ripemd->buffer[11], 11, k9);
268
269 c1 = ripemd->digest[1] + c1 + d2;
270 ripemd->digest[1] = ripemd->digest[2] + d1 + e2;
271 ripemd->digest[2] = ripemd->digest[3] + e1 + a2;
272 ripemd->digest[3] = ripemd->digest[4] + a1 + b2;
273 ripemd->digest[4] = ripemd->digest[0] + b1 + c2;
274 ripemd->digest[0] = c1;
275}
276
277
278static INLINE void AddLength(RipeMd* ripemd, word32 len)
279{
280 word32 tmp = ripemd->loLen;
281 if ( (ripemd->loLen += len) < tmp)
282 ripemd->hiLen++; /* carry low to high */
283}
284
285
286void wc_RipeMdUpdate(RipeMd* ripemd, const byte* data, word32 len)
287{
288 /* do block size increments */
289 byte* local = (byte*)ripemd->buffer;
290
291 while (len) {
292 word32 add = min(len, RIPEMD_BLOCK_SIZE - ripemd->buffLen);
293 XMEMCPY(&local[ripemd->buffLen], data, add);
294
295 ripemd->buffLen += add;
296 data += add;
297 len -= add;
298
299 if (ripemd->buffLen == RIPEMD_BLOCK_SIZE) {
300 #ifdef BIG_ENDIAN_ORDER
301 ByteReverseWords(ripemd->buffer, ripemd->buffer,
302 RIPEMD_BLOCK_SIZE);
303 #endif
304 Transform(ripemd);
305 AddLength(ripemd, RIPEMD_BLOCK_SIZE);
306 ripemd->buffLen = 0;
307 }
308 }
309}
310
311
312void wc_RipeMdFinal(RipeMd* ripemd, byte* hash)
313{
314 byte* local = (byte*)ripemd->buffer;
315
316 AddLength(ripemd, ripemd->buffLen); /* before adding pads */
317
318 local[ripemd->buffLen++] = 0x80; /* add 1 */
319
320 /* pad with zeros */
321 if (ripemd->buffLen > RIPEMD_PAD_SIZE) {
322 XMEMSET(&local[ripemd->buffLen], 0, RIPEMD_BLOCK_SIZE - ripemd->buffLen);
323 ripemd->buffLen += RIPEMD_BLOCK_SIZE - ripemd->buffLen;
324
325 #ifdef BIG_ENDIAN_ORDER
326 ByteReverseWords(ripemd->buffer, ripemd->buffer, RIPEMD_BLOCK_SIZE);
327 #endif
328 Transform(ripemd);
329 ripemd->buffLen = 0;
330 }
331 XMEMSET(&local[ripemd->buffLen], 0, RIPEMD_PAD_SIZE - ripemd->buffLen);
332
333 /* put lengths in bits */
334 ripemd->loLen = ripemd->loLen << 3;
335 ripemd->hiLen = (ripemd->loLen >> (8*sizeof(ripemd->loLen) - 3)) +
336 (ripemd->hiLen << 3);
337
338 /* store lengths */
339 #ifdef BIG_ENDIAN_ORDER
340 ByteReverseWords(ripemd->buffer, ripemd->buffer, RIPEMD_BLOCK_SIZE);
341 #endif
342 /* ! length ordering dependent on digest endian type ! */
343 XMEMCPY(&local[RIPEMD_PAD_SIZE], &ripemd->loLen, sizeof(word32));
344 XMEMCPY(&local[RIPEMD_PAD_SIZE + sizeof(word32)], &ripemd->hiLen,
345 sizeof(word32));
346
347 Transform(ripemd);
348 #ifdef BIG_ENDIAN_ORDER
349 ByteReverseWords(ripemd->digest, ripemd->digest, RIPEMD_DIGEST_SIZE);
350 #endif
351 XMEMCPY(hash, ripemd->digest, RIPEMD_DIGEST_SIZE);
352
353 wc_InitRipeMd(ripemd); /* reset state */
354}
355
356
357#endif /* WOLFSSL_RIPEMD */
Note: See TracBrowser for help on using the repository browser.