rmd160.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. /* rmd160.c - RIPE-MD160
  2. * Copyright (C) 1998, 2001, 2002, 2003 Free Software Foundation, Inc.
  3. *
  4. * This file is part of Libgcrypt.
  5. *
  6. * Libgcrypt is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as
  8. * published by the Free Software Foundation; either version 2.1 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * Libgcrypt is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  19. */
  20. #include <config.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include "g10lib.h"
  25. #include "rmd.h"
  26. #include "cipher.h" /* Only used for the rmd160_hash_buffer() prototype. */
  27. #include "bithelp.h"
  28. /*********************************
  29. * RIPEMD-160 is not patented, see (as of 25.10.97)
  30. * http://www.esat.kuleuven.ac.be/~bosselae/ripemd160.html
  31. * Note that the code uses Little Endian byteorder, which is good for
  32. * 386 etc, but we must add some conversion when used on a big endian box.
  33. *
  34. *
  35. * Pseudo-code for RIPEMD-160
  36. *
  37. * RIPEMD-160 is an iterative hash function that operates on 32-bit words.
  38. * The round function takes as input a 5-word chaining variable and a 16-word
  39. * message block and maps this to a new chaining variable. All operations are
  40. * defined on 32-bit words. Padding is identical to that of MD4.
  41. *
  42. *
  43. * RIPEMD-160: definitions
  44. *
  45. *
  46. * nonlinear functions at bit level: exor, mux, -, mux, -
  47. *
  48. * f(j, x, y, z) = x XOR y XOR z (0 <= j <= 15)
  49. * f(j, x, y, z) = (x AND y) OR (NOT(x) AND z) (16 <= j <= 31)
  50. * f(j, x, y, z) = (x OR NOT(y)) XOR z (32 <= j <= 47)
  51. * f(j, x, y, z) = (x AND z) OR (y AND NOT(z)) (48 <= j <= 63)
  52. * f(j, x, y, z) = x XOR (y OR NOT(z)) (64 <= j <= 79)
  53. *
  54. *
  55. * added constants (hexadecimal)
  56. *
  57. * K(j) = 0x00000000 (0 <= j <= 15)
  58. * K(j) = 0x5A827999 (16 <= j <= 31) int(2**30 x sqrt(2))
  59. * K(j) = 0x6ED9EBA1 (32 <= j <= 47) int(2**30 x sqrt(3))
  60. * K(j) = 0x8F1BBCDC (48 <= j <= 63) int(2**30 x sqrt(5))
  61. * K(j) = 0xA953FD4E (64 <= j <= 79) int(2**30 x sqrt(7))
  62. * K'(j) = 0x50A28BE6 (0 <= j <= 15) int(2**30 x cbrt(2))
  63. * K'(j) = 0x5C4DD124 (16 <= j <= 31) int(2**30 x cbrt(3))
  64. * K'(j) = 0x6D703EF3 (32 <= j <= 47) int(2**30 x cbrt(5))
  65. * K'(j) = 0x7A6D76E9 (48 <= j <= 63) int(2**30 x cbrt(7))
  66. * K'(j) = 0x00000000 (64 <= j <= 79)
  67. *
  68. *
  69. * selection of message word
  70. *
  71. * r(j) = j (0 <= j <= 15)
  72. * r(16..31) = 7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8
  73. * r(32..47) = 3, 10, 14, 4, 9, 15, 8, 1, 2, 7, 0, 6, 13, 11, 5, 12
  74. * r(48..63) = 1, 9, 11, 10, 0, 8, 12, 4, 13, 3, 7, 15, 14, 5, 6, 2
  75. * r(64..79) = 4, 0, 5, 9, 7, 12, 2, 10, 14, 1, 3, 8, 11, 6, 15, 13
  76. * r0(0..15) = 5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3, 12
  77. * r0(16..31)= 6, 11, 3, 7, 0, 13, 5, 10, 14, 15, 8, 12, 4, 9, 1, 2
  78. * r0(32..47)= 15, 5, 1, 3, 7, 14, 6, 9, 11, 8, 12, 2, 10, 0, 4, 13
  79. * r0(48..63)= 8, 6, 4, 1, 3, 11, 15, 0, 5, 12, 2, 13, 9, 7, 10, 14
  80. * r0(64..79)= 12, 15, 10, 4, 1, 5, 8, 7, 6, 2, 13, 14, 0, 3, 9, 11
  81. *
  82. *
  83. * amount for rotate left (rol)
  84. *
  85. * s(0..15) = 11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8
  86. * s(16..31) = 7, 6, 8, 13, 11, 9, 7, 15, 7, 12, 15, 9, 11, 7, 13, 12
  87. * s(32..47) = 11, 13, 6, 7, 14, 9, 13, 15, 14, 8, 13, 6, 5, 12, 7, 5
  88. * s(48..63) = 11, 12, 14, 15, 14, 15, 9, 8, 9, 14, 5, 6, 8, 6, 5, 12
  89. * s(64..79) = 9, 15, 5, 11, 6, 8, 13, 12, 5, 12, 13, 14, 11, 8, 5, 6
  90. * s'(0..15) = 8, 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11, 14, 14, 12, 6
  91. * s'(16..31)= 9, 13, 15, 7, 12, 8, 9, 11, 7, 7, 12, 7, 6, 15, 13, 11
  92. * s'(32..47)= 9, 7, 15, 11, 8, 6, 6, 14, 12, 13, 5, 14, 13, 13, 7, 5
  93. * s'(48..63)= 15, 5, 8, 11, 14, 14, 6, 14, 6, 9, 12, 9, 12, 5, 15, 8
  94. * s'(64..79)= 8, 5, 12, 9, 12, 5, 14, 6, 8, 13, 6, 5, 15, 13, 11, 11
  95. *
  96. *
  97. * initial value (hexadecimal)
  98. *
  99. * h0 = 0x67452301; h1 = 0xEFCDAB89; h2 = 0x98BADCFE; h3 = 0x10325476;
  100. * h4 = 0xC3D2E1F0;
  101. *
  102. *
  103. * RIPEMD-160: pseudo-code
  104. *
  105. * It is assumed that the message after padding consists of t 16-word blocks
  106. * that will be denoted with X[i][j], with 0 <= i <= t-1 and 0 <= j <= 15.
  107. * The symbol [+] denotes addition modulo 2**32 and rol_s denotes cyclic left
  108. * shift (rotate) over s positions.
  109. *
  110. *
  111. * for i := 0 to t-1 {
  112. * A := h0; B := h1; C := h2; D = h3; E = h4;
  113. * A' := h0; B' := h1; C' := h2; D' = h3; E' = h4;
  114. * for j := 0 to 79 {
  115. * T := rol_s(j)(A [+] f(j, B, C, D) [+] X[i][r(j)] [+] K(j)) [+] E;
  116. * A := E; E := D; D := rol_10(C); C := B; B := T;
  117. * T := rol_s'(j)(A' [+] f(79-j, B', C', D') [+] X[i][r'(j)]
  118. [+] K'(j)) [+] E';
  119. * A' := E'; E' := D'; D' := rol_10(C'); C' := B'; B' := T;
  120. * }
  121. * T := h1 [+] C [+] D'; h1 := h2 [+] D [+] E'; h2 := h3 [+] E [+] A';
  122. * h3 := h4 [+] A [+] B'; h4 := h0 [+] B [+] C'; h0 := T;
  123. * }
  124. */
  125. /* Some examples:
  126. * "" 9c1185a5c5e9fc54612808977ee8f548b2258d31
  127. * "a" 0bdc9d2d256b3ee9daae347be6f4dc835a467ffe
  128. * "abc" 8eb208f7e05d987a9b044a8e98c6b087f15a0bfc
  129. * "message digest" 5d0689ef49d2fae572b881b123a85ffa21595f36
  130. * "a...z" f71c27109c692c1b56bbdceb5b9d2865b3708dbc
  131. * "abcdbcde...nopq" 12a053384a9c0c88e405a06c27dcf49ada62eb2b
  132. * "A...Za...z0...9" b0e20b6e3116640286ed3a87a5713079b21f5189
  133. * 8 times "1234567890" 9b752e45573d4b39f4dbd3323cab82bf63326bfb
  134. * 1 million times "a" 52783243c1697bdbe16d37f97f68f08325dc1528
  135. */
  136. void
  137. _gcry_rmd160_init (void *context)
  138. {
  139. RMD160_CONTEXT *hd = context;
  140. hd->h0 = 0x67452301;
  141. hd->h1 = 0xEFCDAB89;
  142. hd->h2 = 0x98BADCFE;
  143. hd->h3 = 0x10325476;
  144. hd->h4 = 0xC3D2E1F0;
  145. hd->nblocks = 0;
  146. hd->count = 0;
  147. }
  148. /****************
  149. * Transform the message X which consists of 16 32-bit-words
  150. */
  151. static void
  152. transform ( RMD160_CONTEXT *hd, const unsigned char *data )
  153. {
  154. register u32 a,b,c,d,e;
  155. u32 aa,bb,cc,dd,ee,t;
  156. #ifdef WORDS_BIGENDIAN
  157. u32 x[16];
  158. {
  159. int i;
  160. byte *p2;
  161. const byte *p1;
  162. for (i=0, p1=data, p2=(byte*)x; i < 16; i++, p2 += 4 )
  163. {
  164. p2[3] = *p1++;
  165. p2[2] = *p1++;
  166. p2[1] = *p1++;
  167. p2[0] = *p1++;
  168. }
  169. }
  170. #else
  171. /* This version is better because it is always aligned;
  172. * The performance penalty on a 586-100 is about 6% which
  173. * is acceptable - because the data is more local it might
  174. * also be possible that this is faster on some machines.
  175. * This function (when compiled with -02 on gcc 2.7.2)
  176. * executes on a 586-100 (39.73 bogomips) at about 1900kb/sec;
  177. * [measured with a 4MB data and "gpgm --print-md rmd160"] */
  178. u32 x[16];
  179. memcpy( x, data, 64 );
  180. #endif
  181. #define K0 0x00000000
  182. #define K1 0x5A827999
  183. #define K2 0x6ED9EBA1
  184. #define K3 0x8F1BBCDC
  185. #define K4 0xA953FD4E
  186. #define KK0 0x50A28BE6
  187. #define KK1 0x5C4DD124
  188. #define KK2 0x6D703EF3
  189. #define KK3 0x7A6D76E9
  190. #define KK4 0x00000000
  191. #define F0(x,y,z) ( (x) ^ (y) ^ (z) )
  192. #define F1(x,y,z) ( ((x) & (y)) | (~(x) & (z)) )
  193. #define F2(x,y,z) ( ((x) | ~(y)) ^ (z) )
  194. #define F3(x,y,z) ( ((x) & (z)) | ((y) & ~(z)) )
  195. #define F4(x,y,z) ( (x) ^ ((y) | ~(z)) )
  196. #define R(a,b,c,d,e,f,k,r,s) do { t = a + f(b,c,d) + k + x[r]; \
  197. a = rol(t,s) + e; \
  198. c = rol(c,10); \
  199. } while(0)
  200. /* left lane */
  201. a = hd->h0;
  202. b = hd->h1;
  203. c = hd->h2;
  204. d = hd->h3;
  205. e = hd->h4;
  206. R( a, b, c, d, e, F0, K0, 0, 11 );
  207. R( e, a, b, c, d, F0, K0, 1, 14 );
  208. R( d, e, a, b, c, F0, K0, 2, 15 );
  209. R( c, d, e, a, b, F0, K0, 3, 12 );
  210. R( b, c, d, e, a, F0, K0, 4, 5 );
  211. R( a, b, c, d, e, F0, K0, 5, 8 );
  212. R( e, a, b, c, d, F0, K0, 6, 7 );
  213. R( d, e, a, b, c, F0, K0, 7, 9 );
  214. R( c, d, e, a, b, F0, K0, 8, 11 );
  215. R( b, c, d, e, a, F0, K0, 9, 13 );
  216. R( a, b, c, d, e, F0, K0, 10, 14 );
  217. R( e, a, b, c, d, F0, K0, 11, 15 );
  218. R( d, e, a, b, c, F0, K0, 12, 6 );
  219. R( c, d, e, a, b, F0, K0, 13, 7 );
  220. R( b, c, d, e, a, F0, K0, 14, 9 );
  221. R( a, b, c, d, e, F0, K0, 15, 8 );
  222. R( e, a, b, c, d, F1, K1, 7, 7 );
  223. R( d, e, a, b, c, F1, K1, 4, 6 );
  224. R( c, d, e, a, b, F1, K1, 13, 8 );
  225. R( b, c, d, e, a, F1, K1, 1, 13 );
  226. R( a, b, c, d, e, F1, K1, 10, 11 );
  227. R( e, a, b, c, d, F1, K1, 6, 9 );
  228. R( d, e, a, b, c, F1, K1, 15, 7 );
  229. R( c, d, e, a, b, F1, K1, 3, 15 );
  230. R( b, c, d, e, a, F1, K1, 12, 7 );
  231. R( a, b, c, d, e, F1, K1, 0, 12 );
  232. R( e, a, b, c, d, F1, K1, 9, 15 );
  233. R( d, e, a, b, c, F1, K1, 5, 9 );
  234. R( c, d, e, a, b, F1, K1, 2, 11 );
  235. R( b, c, d, e, a, F1, K1, 14, 7 );
  236. R( a, b, c, d, e, F1, K1, 11, 13 );
  237. R( e, a, b, c, d, F1, K1, 8, 12 );
  238. R( d, e, a, b, c, F2, K2, 3, 11 );
  239. R( c, d, e, a, b, F2, K2, 10, 13 );
  240. R( b, c, d, e, a, F2, K2, 14, 6 );
  241. R( a, b, c, d, e, F2, K2, 4, 7 );
  242. R( e, a, b, c, d, F2, K2, 9, 14 );
  243. R( d, e, a, b, c, F2, K2, 15, 9 );
  244. R( c, d, e, a, b, F2, K2, 8, 13 );
  245. R( b, c, d, e, a, F2, K2, 1, 15 );
  246. R( a, b, c, d, e, F2, K2, 2, 14 );
  247. R( e, a, b, c, d, F2, K2, 7, 8 );
  248. R( d, e, a, b, c, F2, K2, 0, 13 );
  249. R( c, d, e, a, b, F2, K2, 6, 6 );
  250. R( b, c, d, e, a, F2, K2, 13, 5 );
  251. R( a, b, c, d, e, F2, K2, 11, 12 );
  252. R( e, a, b, c, d, F2, K2, 5, 7 );
  253. R( d, e, a, b, c, F2, K2, 12, 5 );
  254. R( c, d, e, a, b, F3, K3, 1, 11 );
  255. R( b, c, d, e, a, F3, K3, 9, 12 );
  256. R( a, b, c, d, e, F3, K3, 11, 14 );
  257. R( e, a, b, c, d, F3, K3, 10, 15 );
  258. R( d, e, a, b, c, F3, K3, 0, 14 );
  259. R( c, d, e, a, b, F3, K3, 8, 15 );
  260. R( b, c, d, e, a, F3, K3, 12, 9 );
  261. R( a, b, c, d, e, F3, K3, 4, 8 );
  262. R( e, a, b, c, d, F3, K3, 13, 9 );
  263. R( d, e, a, b, c, F3, K3, 3, 14 );
  264. R( c, d, e, a, b, F3, K3, 7, 5 );
  265. R( b, c, d, e, a, F3, K3, 15, 6 );
  266. R( a, b, c, d, e, F3, K3, 14, 8 );
  267. R( e, a, b, c, d, F3, K3, 5, 6 );
  268. R( d, e, a, b, c, F3, K3, 6, 5 );
  269. R( c, d, e, a, b, F3, K3, 2, 12 );
  270. R( b, c, d, e, a, F4, K4, 4, 9 );
  271. R( a, b, c, d, e, F4, K4, 0, 15 );
  272. R( e, a, b, c, d, F4, K4, 5, 5 );
  273. R( d, e, a, b, c, F4, K4, 9, 11 );
  274. R( c, d, e, a, b, F4, K4, 7, 6 );
  275. R( b, c, d, e, a, F4, K4, 12, 8 );
  276. R( a, b, c, d, e, F4, K4, 2, 13 );
  277. R( e, a, b, c, d, F4, K4, 10, 12 );
  278. R( d, e, a, b, c, F4, K4, 14, 5 );
  279. R( c, d, e, a, b, F4, K4, 1, 12 );
  280. R( b, c, d, e, a, F4, K4, 3, 13 );
  281. R( a, b, c, d, e, F4, K4, 8, 14 );
  282. R( e, a, b, c, d, F4, K4, 11, 11 );
  283. R( d, e, a, b, c, F4, K4, 6, 8 );
  284. R( c, d, e, a, b, F4, K4, 15, 5 );
  285. R( b, c, d, e, a, F4, K4, 13, 6 );
  286. aa = a; bb = b; cc = c; dd = d; ee = e;
  287. /* right lane */
  288. a = hd->h0;
  289. b = hd->h1;
  290. c = hd->h2;
  291. d = hd->h3;
  292. e = hd->h4;
  293. R( a, b, c, d, e, F4, KK0, 5, 8);
  294. R( e, a, b, c, d, F4, KK0, 14, 9);
  295. R( d, e, a, b, c, F4, KK0, 7, 9);
  296. R( c, d, e, a, b, F4, KK0, 0, 11);
  297. R( b, c, d, e, a, F4, KK0, 9, 13);
  298. R( a, b, c, d, e, F4, KK0, 2, 15);
  299. R( e, a, b, c, d, F4, KK0, 11, 15);
  300. R( d, e, a, b, c, F4, KK0, 4, 5);
  301. R( c, d, e, a, b, F4, KK0, 13, 7);
  302. R( b, c, d, e, a, F4, KK0, 6, 7);
  303. R( a, b, c, d, e, F4, KK0, 15, 8);
  304. R( e, a, b, c, d, F4, KK0, 8, 11);
  305. R( d, e, a, b, c, F4, KK0, 1, 14);
  306. R( c, d, e, a, b, F4, KK0, 10, 14);
  307. R( b, c, d, e, a, F4, KK0, 3, 12);
  308. R( a, b, c, d, e, F4, KK0, 12, 6);
  309. R( e, a, b, c, d, F3, KK1, 6, 9);
  310. R( d, e, a, b, c, F3, KK1, 11, 13);
  311. R( c, d, e, a, b, F3, KK1, 3, 15);
  312. R( b, c, d, e, a, F3, KK1, 7, 7);
  313. R( a, b, c, d, e, F3, KK1, 0, 12);
  314. R( e, a, b, c, d, F3, KK1, 13, 8);
  315. R( d, e, a, b, c, F3, KK1, 5, 9);
  316. R( c, d, e, a, b, F3, KK1, 10, 11);
  317. R( b, c, d, e, a, F3, KK1, 14, 7);
  318. R( a, b, c, d, e, F3, KK1, 15, 7);
  319. R( e, a, b, c, d, F3, KK1, 8, 12);
  320. R( d, e, a, b, c, F3, KK1, 12, 7);
  321. R( c, d, e, a, b, F3, KK1, 4, 6);
  322. R( b, c, d, e, a, F3, KK1, 9, 15);
  323. R( a, b, c, d, e, F3, KK1, 1, 13);
  324. R( e, a, b, c, d, F3, KK1, 2, 11);
  325. R( d, e, a, b, c, F2, KK2, 15, 9);
  326. R( c, d, e, a, b, F2, KK2, 5, 7);
  327. R( b, c, d, e, a, F2, KK2, 1, 15);
  328. R( a, b, c, d, e, F2, KK2, 3, 11);
  329. R( e, a, b, c, d, F2, KK2, 7, 8);
  330. R( d, e, a, b, c, F2, KK2, 14, 6);
  331. R( c, d, e, a, b, F2, KK2, 6, 6);
  332. R( b, c, d, e, a, F2, KK2, 9, 14);
  333. R( a, b, c, d, e, F2, KK2, 11, 12);
  334. R( e, a, b, c, d, F2, KK2, 8, 13);
  335. R( d, e, a, b, c, F2, KK2, 12, 5);
  336. R( c, d, e, a, b, F2, KK2, 2, 14);
  337. R( b, c, d, e, a, F2, KK2, 10, 13);
  338. R( a, b, c, d, e, F2, KK2, 0, 13);
  339. R( e, a, b, c, d, F2, KK2, 4, 7);
  340. R( d, e, a, b, c, F2, KK2, 13, 5);
  341. R( c, d, e, a, b, F1, KK3, 8, 15);
  342. R( b, c, d, e, a, F1, KK3, 6, 5);
  343. R( a, b, c, d, e, F1, KK3, 4, 8);
  344. R( e, a, b, c, d, F1, KK3, 1, 11);
  345. R( d, e, a, b, c, F1, KK3, 3, 14);
  346. R( c, d, e, a, b, F1, KK3, 11, 14);
  347. R( b, c, d, e, a, F1, KK3, 15, 6);
  348. R( a, b, c, d, e, F1, KK3, 0, 14);
  349. R( e, a, b, c, d, F1, KK3, 5, 6);
  350. R( d, e, a, b, c, F1, KK3, 12, 9);
  351. R( c, d, e, a, b, F1, KK3, 2, 12);
  352. R( b, c, d, e, a, F1, KK3, 13, 9);
  353. R( a, b, c, d, e, F1, KK3, 9, 12);
  354. R( e, a, b, c, d, F1, KK3, 7, 5);
  355. R( d, e, a, b, c, F1, KK3, 10, 15);
  356. R( c, d, e, a, b, F1, KK3, 14, 8);
  357. R( b, c, d, e, a, F0, KK4, 12, 8);
  358. R( a, b, c, d, e, F0, KK4, 15, 5);
  359. R( e, a, b, c, d, F0, KK4, 10, 12);
  360. R( d, e, a, b, c, F0, KK4, 4, 9);
  361. R( c, d, e, a, b, F0, KK4, 1, 12);
  362. R( b, c, d, e, a, F0, KK4, 5, 5);
  363. R( a, b, c, d, e, F0, KK4, 8, 14);
  364. R( e, a, b, c, d, F0, KK4, 7, 6);
  365. R( d, e, a, b, c, F0, KK4, 6, 8);
  366. R( c, d, e, a, b, F0, KK4, 2, 13);
  367. R( b, c, d, e, a, F0, KK4, 13, 6);
  368. R( a, b, c, d, e, F0, KK4, 14, 5);
  369. R( e, a, b, c, d, F0, KK4, 0, 15);
  370. R( d, e, a, b, c, F0, KK4, 3, 13);
  371. R( c, d, e, a, b, F0, KK4, 9, 11);
  372. R( b, c, d, e, a, F0, KK4, 11, 11);
  373. t = hd->h1 + d + cc;
  374. hd->h1 = hd->h2 + e + dd;
  375. hd->h2 = hd->h3 + a + ee;
  376. hd->h3 = hd->h4 + b + aa;
  377. hd->h4 = hd->h0 + c + bb;
  378. hd->h0 = t;
  379. }
  380. /* Update the message digest with the contents
  381. * of INBUF with length INLEN.
  382. */
  383. static void
  384. rmd160_write ( void *context, const void *inbuf_arg, size_t inlen)
  385. {
  386. const unsigned char *inbuf = inbuf_arg;
  387. RMD160_CONTEXT *hd = context;
  388. if( hd->count == 64 ) /* flush the buffer */
  389. {
  390. transform( hd, hd->buf );
  391. _gcry_burn_stack (108+5*sizeof(void*));
  392. hd->count = 0;
  393. hd->nblocks++;
  394. }
  395. if( !inbuf )
  396. return;
  397. if( hd->count )
  398. {
  399. for( ; inlen && hd->count < 64; inlen-- )
  400. hd->buf[hd->count++] = *inbuf++;
  401. rmd160_write( hd, NULL, 0 );
  402. if( !inlen )
  403. return;
  404. }
  405. while( inlen >= 64 )
  406. {
  407. transform( hd, inbuf );
  408. hd->count = 0;
  409. hd->nblocks++;
  410. inlen -= 64;
  411. inbuf += 64;
  412. }
  413. _gcry_burn_stack (108+5*sizeof(void*));
  414. for( ; inlen && hd->count < 64; inlen-- )
  415. hd->buf[hd->count++] = *inbuf++;
  416. }
  417. /****************
  418. * Apply the rmd160 transform function on the buffer which must have
  419. * a length 64 bytes. Do not use this function together with the
  420. * other functions, use rmd160_init to initialize internal variables.
  421. * Returns: 16 bytes in buffer with the mixed contentes of buffer.
  422. */
  423. void
  424. _gcry_rmd160_mixblock ( RMD160_CONTEXT *hd, void *blockof64byte )
  425. {
  426. char *p = blockof64byte;
  427. transform ( hd, blockof64byte );
  428. #define X(a) do { *(u32*)p = hd->h##a ; p += 4; } while(0)
  429. X(0);
  430. X(1);
  431. X(2);
  432. X(3);
  433. X(4);
  434. #undef X
  435. }
  436. /* The routine terminates the computation
  437. */
  438. static void
  439. rmd160_final( void *context )
  440. {
  441. RMD160_CONTEXT *hd = context;
  442. u32 t, msb, lsb;
  443. byte *p;
  444. rmd160_write(hd, NULL, 0); /* flush */;
  445. t = hd->nblocks;
  446. /* multiply by 64 to make a byte count */
  447. lsb = t << 6;
  448. msb = t >> 26;
  449. /* add the count */
  450. t = lsb;
  451. if( (lsb += hd->count) < t )
  452. msb++;
  453. /* multiply by 8 to make a bit count */
  454. t = lsb;
  455. lsb <<= 3;
  456. msb <<= 3;
  457. msb |= t >> 29;
  458. if( hd->count < 56 ) /* enough room */
  459. {
  460. hd->buf[hd->count++] = 0x80; /* pad */
  461. while( hd->count < 56 )
  462. hd->buf[hd->count++] = 0; /* pad */
  463. }
  464. else /* need one extra block */
  465. {
  466. hd->buf[hd->count++] = 0x80; /* pad character */
  467. while( hd->count < 64 )
  468. hd->buf[hd->count++] = 0;
  469. rmd160_write(hd, NULL, 0); /* flush */;
  470. memset(hd->buf, 0, 56 ); /* fill next block with zeroes */
  471. }
  472. /* append the 64 bit count */
  473. hd->buf[56] = lsb ;
  474. hd->buf[57] = lsb >> 8;
  475. hd->buf[58] = lsb >> 16;
  476. hd->buf[59] = lsb >> 24;
  477. hd->buf[60] = msb ;
  478. hd->buf[61] = msb >> 8;
  479. hd->buf[62] = msb >> 16;
  480. hd->buf[63] = msb >> 24;
  481. transform( hd, hd->buf );
  482. _gcry_burn_stack (108+5*sizeof(void*));
  483. p = hd->buf;
  484. #ifdef WORDS_BIGENDIAN
  485. #define X(a) do { *p++ = hd->h##a ; *p++ = hd->h##a >> 8; \
  486. *p++ = hd->h##a >> 16; *p++ = hd->h##a >> 24; } while(0)
  487. #else /* little endian */
  488. #define X(a) do { *(u32*)p = hd->h##a ; p += 4; } while(0)
  489. #endif
  490. X(0);
  491. X(1);
  492. X(2);
  493. X(3);
  494. X(4);
  495. #undef X
  496. }
  497. static byte *
  498. rmd160_read( void *context )
  499. {
  500. RMD160_CONTEXT *hd = context;
  501. return hd->buf;
  502. }
  503. /****************
  504. * Shortcut functions which puts the hash value of the supplied buffer
  505. * into outbuf which must have a size of 20 bytes.
  506. */
  507. void
  508. _gcry_rmd160_hash_buffer (void *outbuf, const void *buffer, size_t length )
  509. {
  510. RMD160_CONTEXT hd;
  511. _gcry_rmd160_init ( &hd );
  512. rmd160_write ( &hd, buffer, length );
  513. rmd160_final ( &hd );
  514. memcpy ( outbuf, hd.buf, 20 );
  515. }
  516. static byte asn[15] = /* Object ID is 1.3.36.3.2.1 */
  517. { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x24, 0x03,
  518. 0x02, 0x01, 0x05, 0x00, 0x04, 0x14 };
  519. static gcry_md_oid_spec_t oid_spec_rmd160[] =
  520. {
  521. /* rsaSignatureWithripemd160 */
  522. { "1.3.36.3.3.1.2" },
  523. /* TeleTrust hash algorithm. */
  524. { "1.3.36.3.2.1" },
  525. { NULL }
  526. };
  527. gcry_md_spec_t _gcry_digest_spec_rmd160 =
  528. {
  529. "RIPEMD160", asn, DIM (asn), oid_spec_rmd160, 20,
  530. _gcry_rmd160_init, rmd160_write, rmd160_final, rmd160_read,
  531. sizeof (RMD160_CONTEXT)
  532. };