rmd160.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /* $OpenBSD: rmd160.c,v 1.3 2001/09/26 21:40:13 markus Exp $ */
  2. /*-
  3. * Copyright (c) 2001 Markus Friedl. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. /*
  26. * Preneel, Bosselaers, Dobbertin, "The Cryptographic Hash Function RIPEMD-160",
  27. * RSA Laboratories, CryptoBytes, Volume 3, Number 2, Autumn 1997,
  28. * ftp://ftp.rsasecurity.com/pub/cryptobytes/crypto3n2.pdf
  29. */
  30. #include <sys/cdefs.h>
  31. __FBSDID("$FreeBSD$");
  32. #include <sys/param.h>
  33. #include <sys/systm.h>
  34. #include <sys/endian.h>
  35. #include <opencrypto/rmd160.h>
  36. #define PUT_64BIT_LE(cp, value) do { \
  37. (cp)[7] = (value) >> 56; \
  38. (cp)[6] = (value) >> 48; \
  39. (cp)[5] = (value) >> 40; \
  40. (cp)[4] = (value) >> 32; \
  41. (cp)[3] = (value) >> 24; \
  42. (cp)[2] = (value) >> 16; \
  43. (cp)[1] = (value) >> 8; \
  44. (cp)[0] = (value); } while (0)
  45. #define PUT_32BIT_LE(cp, value) do { \
  46. (cp)[3] = (value) >> 24; \
  47. (cp)[2] = (value) >> 16; \
  48. (cp)[1] = (value) >> 8; \
  49. (cp)[0] = (value); } while (0)
  50. #define H0 0x67452301U
  51. #define H1 0xEFCDAB89U
  52. #define H2 0x98BADCFEU
  53. #define H3 0x10325476U
  54. #define H4 0xC3D2E1F0U
  55. #define K0 0x00000000U
  56. #define K1 0x5A827999U
  57. #define K2 0x6ED9EBA1U
  58. #define K3 0x8F1BBCDCU
  59. #define K4 0xA953FD4EU
  60. #define KK0 0x50A28BE6U
  61. #define KK1 0x5C4DD124U
  62. #define KK2 0x6D703EF3U
  63. #define KK3 0x7A6D76E9U
  64. #define KK4 0x00000000U
  65. /* rotate x left n bits. */
  66. #define ROL(n, x) (((x) << (n)) | ((x) >> (32-(n))))
  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, Fj, Kj, sj, rj) \
  73. do { \
  74. a = ROL(sj, a + Fj(b,c,d) + X(rj) + Kj) + e; \
  75. c = ROL(10, c); \
  76. } while(0)
  77. #define X(i) x[i]
  78. static u_char PADDING[64] = {
  79. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  80. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  81. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  82. };
  83. void
  84. RMD160Init(RMD160_CTX *ctx)
  85. {
  86. ctx->count = 0;
  87. ctx->state[0] = H0;
  88. ctx->state[1] = H1;
  89. ctx->state[2] = H2;
  90. ctx->state[3] = H3;
  91. ctx->state[4] = H4;
  92. }
  93. void
  94. RMD160Update(RMD160_CTX *ctx, const u_char *input, uint32_t len)
  95. {
  96. uint32_t have, off, need;
  97. have = (ctx->count/8) % 64;
  98. need = 64 - have;
  99. ctx->count += 8 * len;
  100. off = 0;
  101. if (len >= need) {
  102. if (have) {
  103. memcpy(ctx->buffer + have, input, need);
  104. RMD160Transform(ctx->state, ctx->buffer);
  105. off = need;
  106. have = 0;
  107. }
  108. /* now the buffer is empty */
  109. while (off + 64 <= len) {
  110. RMD160Transform(ctx->state, input+off);
  111. off += 64;
  112. }
  113. }
  114. if (off < len)
  115. memcpy(ctx->buffer + have, input+off, len-off);
  116. }
  117. void
  118. RMD160Final(u_char digest[20], RMD160_CTX *ctx)
  119. {
  120. int i;
  121. u_char size[8];
  122. uint32_t padlen;
  123. PUT_64BIT_LE(size, ctx->count);
  124. /*
  125. * pad to 64 byte blocks, at least one byte from PADDING plus 8 bytes
  126. * for the size
  127. */
  128. padlen = 64 - ((ctx->count/8) % 64);
  129. if (padlen < 1 + 8)
  130. padlen += 64;
  131. RMD160Update(ctx, PADDING, padlen - 8); /* padlen - 8 <= 64 */
  132. RMD160Update(ctx, size, 8);
  133. if (digest != NULL)
  134. for (i = 0; i < 5; i++)
  135. PUT_32BIT_LE(digest + i*4, ctx->state[i]);
  136. memset(ctx, 0, sizeof (*ctx));
  137. }
  138. void
  139. RMD160Transform(uint32_t state[5], const u_char block[64])
  140. {
  141. uint32_t a, b, c, d, e, aa, bb, cc, dd, ee, t, x[16];
  142. #if BYTE_ORDER == LITTLE_ENDIAN
  143. memcpy(x, block, 64);
  144. #else
  145. int i;
  146. for (i = 0; i < 16; i++)
  147. x[i] = bswap32(*(const uint32_t*)(block+i*4));
  148. #endif
  149. a = state[0];
  150. b = state[1];
  151. c = state[2];
  152. d = state[3];
  153. e = state[4];
  154. /* Round 1 */
  155. R(a, b, c, d, e, F0, K0, 11, 0);
  156. R(e, a, b, c, d, F0, K0, 14, 1);
  157. R(d, e, a, b, c, F0, K0, 15, 2);
  158. R(c, d, e, a, b, F0, K0, 12, 3);
  159. R(b, c, d, e, a, F0, K0, 5, 4);
  160. R(a, b, c, d, e, F0, K0, 8, 5);
  161. R(e, a, b, c, d, F0, K0, 7, 6);
  162. R(d, e, a, b, c, F0, K0, 9, 7);
  163. R(c, d, e, a, b, F0, K0, 11, 8);
  164. R(b, c, d, e, a, F0, K0, 13, 9);
  165. R(a, b, c, d, e, F0, K0, 14, 10);
  166. R(e, a, b, c, d, F0, K0, 15, 11);
  167. R(d, e, a, b, c, F0, K0, 6, 12);
  168. R(c, d, e, a, b, F0, K0, 7, 13);
  169. R(b, c, d, e, a, F0, K0, 9, 14);
  170. R(a, b, c, d, e, F0, K0, 8, 15); /* #15 */
  171. /* Round 2 */
  172. R(e, a, b, c, d, F1, K1, 7, 7);
  173. R(d, e, a, b, c, F1, K1, 6, 4);
  174. R(c, d, e, a, b, F1, K1, 8, 13);
  175. R(b, c, d, e, a, F1, K1, 13, 1);
  176. R(a, b, c, d, e, F1, K1, 11, 10);
  177. R(e, a, b, c, d, F1, K1, 9, 6);
  178. R(d, e, a, b, c, F1, K1, 7, 15);
  179. R(c, d, e, a, b, F1, K1, 15, 3);
  180. R(b, c, d, e, a, F1, K1, 7, 12);
  181. R(a, b, c, d, e, F1, K1, 12, 0);
  182. R(e, a, b, c, d, F1, K1, 15, 9);
  183. R(d, e, a, b, c, F1, K1, 9, 5);
  184. R(c, d, e, a, b, F1, K1, 11, 2);
  185. R(b, c, d, e, a, F1, K1, 7, 14);
  186. R(a, b, c, d, e, F1, K1, 13, 11);
  187. R(e, a, b, c, d, F1, K1, 12, 8); /* #31 */
  188. /* Round 3 */
  189. R(d, e, a, b, c, F2, K2, 11, 3);
  190. R(c, d, e, a, b, F2, K2, 13, 10);
  191. R(b, c, d, e, a, F2, K2, 6, 14);
  192. R(a, b, c, d, e, F2, K2, 7, 4);
  193. R(e, a, b, c, d, F2, K2, 14, 9);
  194. R(d, e, a, b, c, F2, K2, 9, 15);
  195. R(c, d, e, a, b, F2, K2, 13, 8);
  196. R(b, c, d, e, a, F2, K2, 15, 1);
  197. R(a, b, c, d, e, F2, K2, 14, 2);
  198. R(e, a, b, c, d, F2, K2, 8, 7);
  199. R(d, e, a, b, c, F2, K2, 13, 0);
  200. R(c, d, e, a, b, F2, K2, 6, 6);
  201. R(b, c, d, e, a, F2, K2, 5, 13);
  202. R(a, b, c, d, e, F2, K2, 12, 11);
  203. R(e, a, b, c, d, F2, K2, 7, 5);
  204. R(d, e, a, b, c, F2, K2, 5, 12); /* #47 */
  205. /* Round 4 */
  206. R(c, d, e, a, b, F3, K3, 11, 1);
  207. R(b, c, d, e, a, F3, K3, 12, 9);
  208. R(a, b, c, d, e, F3, K3, 14, 11);
  209. R(e, a, b, c, d, F3, K3, 15, 10);
  210. R(d, e, a, b, c, F3, K3, 14, 0);
  211. R(c, d, e, a, b, F3, K3, 15, 8);
  212. R(b, c, d, e, a, F3, K3, 9, 12);
  213. R(a, b, c, d, e, F3, K3, 8, 4);
  214. R(e, a, b, c, d, F3, K3, 9, 13);
  215. R(d, e, a, b, c, F3, K3, 14, 3);
  216. R(c, d, e, a, b, F3, K3, 5, 7);
  217. R(b, c, d, e, a, F3, K3, 6, 15);
  218. R(a, b, c, d, e, F3, K3, 8, 14);
  219. R(e, a, b, c, d, F3, K3, 6, 5);
  220. R(d, e, a, b, c, F3, K3, 5, 6);
  221. R(c, d, e, a, b, F3, K3, 12, 2); /* #63 */
  222. /* Round 5 */
  223. R(b, c, d, e, a, F4, K4, 9, 4);
  224. R(a, b, c, d, e, F4, K4, 15, 0);
  225. R(e, a, b, c, d, F4, K4, 5, 5);
  226. R(d, e, a, b, c, F4, K4, 11, 9);
  227. R(c, d, e, a, b, F4, K4, 6, 7);
  228. R(b, c, d, e, a, F4, K4, 8, 12);
  229. R(a, b, c, d, e, F4, K4, 13, 2);
  230. R(e, a, b, c, d, F4, K4, 12, 10);
  231. R(d, e, a, b, c, F4, K4, 5, 14);
  232. R(c, d, e, a, b, F4, K4, 12, 1);
  233. R(b, c, d, e, a, F4, K4, 13, 3);
  234. R(a, b, c, d, e, F4, K4, 14, 8);
  235. R(e, a, b, c, d, F4, K4, 11, 11);
  236. R(d, e, a, b, c, F4, K4, 8, 6);
  237. R(c, d, e, a, b, F4, K4, 5, 15);
  238. R(b, c, d, e, a, F4, K4, 6, 13); /* #79 */
  239. aa = a ; bb = b; cc = c; dd = d; ee = e;
  240. a = state[0];
  241. b = state[1];
  242. c = state[2];
  243. d = state[3];
  244. e = state[4];
  245. /* Parallel round 1 */
  246. R(a, b, c, d, e, F4, KK0, 8, 5);
  247. R(e, a, b, c, d, F4, KK0, 9, 14);
  248. R(d, e, a, b, c, F4, KK0, 9, 7);
  249. R(c, d, e, a, b, F4, KK0, 11, 0);
  250. R(b, c, d, e, a, F4, KK0, 13, 9);
  251. R(a, b, c, d, e, F4, KK0, 15, 2);
  252. R(e, a, b, c, d, F4, KK0, 15, 11);
  253. R(d, e, a, b, c, F4, KK0, 5, 4);
  254. R(c, d, e, a, b, F4, KK0, 7, 13);
  255. R(b, c, d, e, a, F4, KK0, 7, 6);
  256. R(a, b, c, d, e, F4, KK0, 8, 15);
  257. R(e, a, b, c, d, F4, KK0, 11, 8);
  258. R(d, e, a, b, c, F4, KK0, 14, 1);
  259. R(c, d, e, a, b, F4, KK0, 14, 10);
  260. R(b, c, d, e, a, F4, KK0, 12, 3);
  261. R(a, b, c, d, e, F4, KK0, 6, 12); /* #15 */
  262. /* Parallel round 2 */
  263. R(e, a, b, c, d, F3, KK1, 9, 6);
  264. R(d, e, a, b, c, F3, KK1, 13, 11);
  265. R(c, d, e, a, b, F3, KK1, 15, 3);
  266. R(b, c, d, e, a, F3, KK1, 7, 7);
  267. R(a, b, c, d, e, F3, KK1, 12, 0);
  268. R(e, a, b, c, d, F3, KK1, 8, 13);
  269. R(d, e, a, b, c, F3, KK1, 9, 5);
  270. R(c, d, e, a, b, F3, KK1, 11, 10);
  271. R(b, c, d, e, a, F3, KK1, 7, 14);
  272. R(a, b, c, d, e, F3, KK1, 7, 15);
  273. R(e, a, b, c, d, F3, KK1, 12, 8);
  274. R(d, e, a, b, c, F3, KK1, 7, 12);
  275. R(c, d, e, a, b, F3, KK1, 6, 4);
  276. R(b, c, d, e, a, F3, KK1, 15, 9);
  277. R(a, b, c, d, e, F3, KK1, 13, 1);
  278. R(e, a, b, c, d, F3, KK1, 11, 2); /* #31 */
  279. /* Parallel round 3 */
  280. R(d, e, a, b, c, F2, KK2, 9, 15);
  281. R(c, d, e, a, b, F2, KK2, 7, 5);
  282. R(b, c, d, e, a, F2, KK2, 15, 1);
  283. R(a, b, c, d, e, F2, KK2, 11, 3);
  284. R(e, a, b, c, d, F2, KK2, 8, 7);
  285. R(d, e, a, b, c, F2, KK2, 6, 14);
  286. R(c, d, e, a, b, F2, KK2, 6, 6);
  287. R(b, c, d, e, a, F2, KK2, 14, 9);
  288. R(a, b, c, d, e, F2, KK2, 12, 11);
  289. R(e, a, b, c, d, F2, KK2, 13, 8);
  290. R(d, e, a, b, c, F2, KK2, 5, 12);
  291. R(c, d, e, a, b, F2, KK2, 14, 2);
  292. R(b, c, d, e, a, F2, KK2, 13, 10);
  293. R(a, b, c, d, e, F2, KK2, 13, 0);
  294. R(e, a, b, c, d, F2, KK2, 7, 4);
  295. R(d, e, a, b, c, F2, KK2, 5, 13); /* #47 */
  296. /* Parallel round 4 */
  297. R(c, d, e, a, b, F1, KK3, 15, 8);
  298. R(b, c, d, e, a, F1, KK3, 5, 6);
  299. R(a, b, c, d, e, F1, KK3, 8, 4);
  300. R(e, a, b, c, d, F1, KK3, 11, 1);
  301. R(d, e, a, b, c, F1, KK3, 14, 3);
  302. R(c, d, e, a, b, F1, KK3, 14, 11);
  303. R(b, c, d, e, a, F1, KK3, 6, 15);
  304. R(a, b, c, d, e, F1, KK3, 14, 0);
  305. R(e, a, b, c, d, F1, KK3, 6, 5);
  306. R(d, e, a, b, c, F1, KK3, 9, 12);
  307. R(c, d, e, a, b, F1, KK3, 12, 2);
  308. R(b, c, d, e, a, F1, KK3, 9, 13);
  309. R(a, b, c, d, e, F1, KK3, 12, 9);
  310. R(e, a, b, c, d, F1, KK3, 5, 7);
  311. R(d, e, a, b, c, F1, KK3, 15, 10);
  312. R(c, d, e, a, b, F1, KK3, 8, 14); /* #63 */
  313. /* Parallel round 5 */
  314. R(b, c, d, e, a, F0, KK4, 8, 12);
  315. R(a, b, c, d, e, F0, KK4, 5, 15);
  316. R(e, a, b, c, d, F0, KK4, 12, 10);
  317. R(d, e, a, b, c, F0, KK4, 9, 4);
  318. R(c, d, e, a, b, F0, KK4, 12, 1);
  319. R(b, c, d, e, a, F0, KK4, 5, 5);
  320. R(a, b, c, d, e, F0, KK4, 14, 8);
  321. R(e, a, b, c, d, F0, KK4, 6, 7);
  322. R(d, e, a, b, c, F0, KK4, 8, 6);
  323. R(c, d, e, a, b, F0, KK4, 13, 2);
  324. R(b, c, d, e, a, F0, KK4, 6, 13);
  325. R(a, b, c, d, e, F0, KK4, 5, 14);
  326. R(e, a, b, c, d, F0, KK4, 15, 0);
  327. R(d, e, a, b, c, F0, KK4, 13, 3);
  328. R(c, d, e, a, b, F0, KK4, 11, 9);
  329. R(b, c, d, e, a, F0, KK4, 11, 11); /* #79 */
  330. t = state[1] + cc + d;
  331. state[1] = state[2] + dd + e;
  332. state[2] = state[3] + ee + a;
  333. state[3] = state[4] + aa + b;
  334. state[4] = state[0] + bb + c;
  335. state[0] = t;
  336. }