rmd160.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*
  2. * Cryptographic API.
  3. *
  4. * RIPEMD-160 - RACE Integrity Primitives Evaluation Message Digest.
  5. *
  6. * Based on the reference implementation by Antoon Bosselaers, ESAT-COSIC
  7. *
  8. * Copyright (c) 2008 Adrian-Ken Rueegsegger <ken@codelabs.ch>
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the Free
  12. * Software Foundation; either version 2 of the License, or (at your option)
  13. * any later version.
  14. *
  15. */
  16. #include <crypto/internal/hash.h>
  17. #include <linux/init.h>
  18. #include <linux/module.h>
  19. #include <linux/mm.h>
  20. #include <linux/types.h>
  21. #include <asm/byteorder.h>
  22. #include "ripemd.h"
  23. struct rmd160_ctx {
  24. u64 byte_count;
  25. u32 state[5];
  26. __le32 buffer[16];
  27. };
  28. #define K1 RMD_K1
  29. #define K2 RMD_K2
  30. #define K3 RMD_K3
  31. #define K4 RMD_K4
  32. #define K5 RMD_K5
  33. #define KK1 RMD_K6
  34. #define KK2 RMD_K7
  35. #define KK3 RMD_K8
  36. #define KK4 RMD_K9
  37. #define KK5 RMD_K1
  38. #define F1(x, y, z) (x ^ y ^ z) /* XOR */
  39. #define F2(x, y, z) (z ^ (x & (y ^ z))) /* x ? y : z */
  40. #define F3(x, y, z) ((x | ~y) ^ z)
  41. #define F4(x, y, z) (y ^ (z & (x ^ y))) /* z ? x : y */
  42. #define F5(x, y, z) (x ^ (y | ~z))
  43. #define ROUND(a, b, c, d, e, f, k, x, s) { \
  44. (a) += f((b), (c), (d)) + le32_to_cpup(&(x)) + (k); \
  45. (a) = rol32((a), (s)) + (e); \
  46. (c) = rol32((c), 10); \
  47. }
  48. static void rmd160_transform(u32 *state, const __le32 *in)
  49. {
  50. u32 aa, bb, cc, dd, ee, aaa, bbb, ccc, ddd, eee;
  51. /* Initialize left lane */
  52. aa = state[0];
  53. bb = state[1];
  54. cc = state[2];
  55. dd = state[3];
  56. ee = state[4];
  57. /* Initialize right lane */
  58. aaa = state[0];
  59. bbb = state[1];
  60. ccc = state[2];
  61. ddd = state[3];
  62. eee = state[4];
  63. /* round 1: left lane */
  64. ROUND(aa, bb, cc, dd, ee, F1, K1, in[0], 11);
  65. ROUND(ee, aa, bb, cc, dd, F1, K1, in[1], 14);
  66. ROUND(dd, ee, aa, bb, cc, F1, K1, in[2], 15);
  67. ROUND(cc, dd, ee, aa, bb, F1, K1, in[3], 12);
  68. ROUND(bb, cc, dd, ee, aa, F1, K1, in[4], 5);
  69. ROUND(aa, bb, cc, dd, ee, F1, K1, in[5], 8);
  70. ROUND(ee, aa, bb, cc, dd, F1, K1, in[6], 7);
  71. ROUND(dd, ee, aa, bb, cc, F1, K1, in[7], 9);
  72. ROUND(cc, dd, ee, aa, bb, F1, K1, in[8], 11);
  73. ROUND(bb, cc, dd, ee, aa, F1, K1, in[9], 13);
  74. ROUND(aa, bb, cc, dd, ee, F1, K1, in[10], 14);
  75. ROUND(ee, aa, bb, cc, dd, F1, K1, in[11], 15);
  76. ROUND(dd, ee, aa, bb, cc, F1, K1, in[12], 6);
  77. ROUND(cc, dd, ee, aa, bb, F1, K1, in[13], 7);
  78. ROUND(bb, cc, dd, ee, aa, F1, K1, in[14], 9);
  79. ROUND(aa, bb, cc, dd, ee, F1, K1, in[15], 8);
  80. /* round 2: left lane" */
  81. ROUND(ee, aa, bb, cc, dd, F2, K2, in[7], 7);
  82. ROUND(dd, ee, aa, bb, cc, F2, K2, in[4], 6);
  83. ROUND(cc, dd, ee, aa, bb, F2, K2, in[13], 8);
  84. ROUND(bb, cc, dd, ee, aa, F2, K2, in[1], 13);
  85. ROUND(aa, bb, cc, dd, ee, F2, K2, in[10], 11);
  86. ROUND(ee, aa, bb, cc, dd, F2, K2, in[6], 9);
  87. ROUND(dd, ee, aa, bb, cc, F2, K2, in[15], 7);
  88. ROUND(cc, dd, ee, aa, bb, F2, K2, in[3], 15);
  89. ROUND(bb, cc, dd, ee, aa, F2, K2, in[12], 7);
  90. ROUND(aa, bb, cc, dd, ee, F2, K2, in[0], 12);
  91. ROUND(ee, aa, bb, cc, dd, F2, K2, in[9], 15);
  92. ROUND(dd, ee, aa, bb, cc, F2, K2, in[5], 9);
  93. ROUND(cc, dd, ee, aa, bb, F2, K2, in[2], 11);
  94. ROUND(bb, cc, dd, ee, aa, F2, K2, in[14], 7);
  95. ROUND(aa, bb, cc, dd, ee, F2, K2, in[11], 13);
  96. ROUND(ee, aa, bb, cc, dd, F2, K2, in[8], 12);
  97. /* round 3: left lane" */
  98. ROUND(dd, ee, aa, bb, cc, F3, K3, in[3], 11);
  99. ROUND(cc, dd, ee, aa, bb, F3, K3, in[10], 13);
  100. ROUND(bb, cc, dd, ee, aa, F3, K3, in[14], 6);
  101. ROUND(aa, bb, cc, dd, ee, F3, K3, in[4], 7);
  102. ROUND(ee, aa, bb, cc, dd, F3, K3, in[9], 14);
  103. ROUND(dd, ee, aa, bb, cc, F3, K3, in[15], 9);
  104. ROUND(cc, dd, ee, aa, bb, F3, K3, in[8], 13);
  105. ROUND(bb, cc, dd, ee, aa, F3, K3, in[1], 15);
  106. ROUND(aa, bb, cc, dd, ee, F3, K3, in[2], 14);
  107. ROUND(ee, aa, bb, cc, dd, F3, K3, in[7], 8);
  108. ROUND(dd, ee, aa, bb, cc, F3, K3, in[0], 13);
  109. ROUND(cc, dd, ee, aa, bb, F3, K3, in[6], 6);
  110. ROUND(bb, cc, dd, ee, aa, F3, K3, in[13], 5);
  111. ROUND(aa, bb, cc, dd, ee, F3, K3, in[11], 12);
  112. ROUND(ee, aa, bb, cc, dd, F3, K3, in[5], 7);
  113. ROUND(dd, ee, aa, bb, cc, F3, K3, in[12], 5);
  114. /* round 4: left lane" */
  115. ROUND(cc, dd, ee, aa, bb, F4, K4, in[1], 11);
  116. ROUND(bb, cc, dd, ee, aa, F4, K4, in[9], 12);
  117. ROUND(aa, bb, cc, dd, ee, F4, K4, in[11], 14);
  118. ROUND(ee, aa, bb, cc, dd, F4, K4, in[10], 15);
  119. ROUND(dd, ee, aa, bb, cc, F4, K4, in[0], 14);
  120. ROUND(cc, dd, ee, aa, bb, F4, K4, in[8], 15);
  121. ROUND(bb, cc, dd, ee, aa, F4, K4, in[12], 9);
  122. ROUND(aa, bb, cc, dd, ee, F4, K4, in[4], 8);
  123. ROUND(ee, aa, bb, cc, dd, F4, K4, in[13], 9);
  124. ROUND(dd, ee, aa, bb, cc, F4, K4, in[3], 14);
  125. ROUND(cc, dd, ee, aa, bb, F4, K4, in[7], 5);
  126. ROUND(bb, cc, dd, ee, aa, F4, K4, in[15], 6);
  127. ROUND(aa, bb, cc, dd, ee, F4, K4, in[14], 8);
  128. ROUND(ee, aa, bb, cc, dd, F4, K4, in[5], 6);
  129. ROUND(dd, ee, aa, bb, cc, F4, K4, in[6], 5);
  130. ROUND(cc, dd, ee, aa, bb, F4, K4, in[2], 12);
  131. /* round 5: left lane" */
  132. ROUND(bb, cc, dd, ee, aa, F5, K5, in[4], 9);
  133. ROUND(aa, bb, cc, dd, ee, F5, K5, in[0], 15);
  134. ROUND(ee, aa, bb, cc, dd, F5, K5, in[5], 5);
  135. ROUND(dd, ee, aa, bb, cc, F5, K5, in[9], 11);
  136. ROUND(cc, dd, ee, aa, bb, F5, K5, in[7], 6);
  137. ROUND(bb, cc, dd, ee, aa, F5, K5, in[12], 8);
  138. ROUND(aa, bb, cc, dd, ee, F5, K5, in[2], 13);
  139. ROUND(ee, aa, bb, cc, dd, F5, K5, in[10], 12);
  140. ROUND(dd, ee, aa, bb, cc, F5, K5, in[14], 5);
  141. ROUND(cc, dd, ee, aa, bb, F5, K5, in[1], 12);
  142. ROUND(bb, cc, dd, ee, aa, F5, K5, in[3], 13);
  143. ROUND(aa, bb, cc, dd, ee, F5, K5, in[8], 14);
  144. ROUND(ee, aa, bb, cc, dd, F5, K5, in[11], 11);
  145. ROUND(dd, ee, aa, bb, cc, F5, K5, in[6], 8);
  146. ROUND(cc, dd, ee, aa, bb, F5, K5, in[15], 5);
  147. ROUND(bb, cc, dd, ee, aa, F5, K5, in[13], 6);
  148. /* round 1: right lane */
  149. ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[5], 8);
  150. ROUND(eee, aaa, bbb, ccc, ddd, F5, KK1, in[14], 9);
  151. ROUND(ddd, eee, aaa, bbb, ccc, F5, KK1, in[7], 9);
  152. ROUND(ccc, ddd, eee, aaa, bbb, F5, KK1, in[0], 11);
  153. ROUND(bbb, ccc, ddd, eee, aaa, F5, KK1, in[9], 13);
  154. ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[2], 15);
  155. ROUND(eee, aaa, bbb, ccc, ddd, F5, KK1, in[11], 15);
  156. ROUND(ddd, eee, aaa, bbb, ccc, F5, KK1, in[4], 5);
  157. ROUND(ccc, ddd, eee, aaa, bbb, F5, KK1, in[13], 7);
  158. ROUND(bbb, ccc, ddd, eee, aaa, F5, KK1, in[6], 7);
  159. ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[15], 8);
  160. ROUND(eee, aaa, bbb, ccc, ddd, F5, KK1, in[8], 11);
  161. ROUND(ddd, eee, aaa, bbb, ccc, F5, KK1, in[1], 14);
  162. ROUND(ccc, ddd, eee, aaa, bbb, F5, KK1, in[10], 14);
  163. ROUND(bbb, ccc, ddd, eee, aaa, F5, KK1, in[3], 12);
  164. ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[12], 6);
  165. /* round 2: right lane */
  166. ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[6], 9);
  167. ROUND(ddd, eee, aaa, bbb, ccc, F4, KK2, in[11], 13);
  168. ROUND(ccc, ddd, eee, aaa, bbb, F4, KK2, in[3], 15);
  169. ROUND(bbb, ccc, ddd, eee, aaa, F4, KK2, in[7], 7);
  170. ROUND(aaa, bbb, ccc, ddd, eee, F4, KK2, in[0], 12);
  171. ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[13], 8);
  172. ROUND(ddd, eee, aaa, bbb, ccc, F4, KK2, in[5], 9);
  173. ROUND(ccc, ddd, eee, aaa, bbb, F4, KK2, in[10], 11);
  174. ROUND(bbb, ccc, ddd, eee, aaa, F4, KK2, in[14], 7);
  175. ROUND(aaa, bbb, ccc, ddd, eee, F4, KK2, in[15], 7);
  176. ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[8], 12);
  177. ROUND(ddd, eee, aaa, bbb, ccc, F4, KK2, in[12], 7);
  178. ROUND(ccc, ddd, eee, aaa, bbb, F4, KK2, in[4], 6);
  179. ROUND(bbb, ccc, ddd, eee, aaa, F4, KK2, in[9], 15);
  180. ROUND(aaa, bbb, ccc, ddd, eee, F4, KK2, in[1], 13);
  181. ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[2], 11);
  182. /* round 3: right lane */
  183. ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[15], 9);
  184. ROUND(ccc, ddd, eee, aaa, bbb, F3, KK3, in[5], 7);
  185. ROUND(bbb, ccc, ddd, eee, aaa, F3, KK3, in[1], 15);
  186. ROUND(aaa, bbb, ccc, ddd, eee, F3, KK3, in[3], 11);
  187. ROUND(eee, aaa, bbb, ccc, ddd, F3, KK3, in[7], 8);
  188. ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[14], 6);
  189. ROUND(ccc, ddd, eee, aaa, bbb, F3, KK3, in[6], 6);
  190. ROUND(bbb, ccc, ddd, eee, aaa, F3, KK3, in[9], 14);
  191. ROUND(aaa, bbb, ccc, ddd, eee, F3, KK3, in[11], 12);
  192. ROUND(eee, aaa, bbb, ccc, ddd, F3, KK3, in[8], 13);
  193. ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[12], 5);
  194. ROUND(ccc, ddd, eee, aaa, bbb, F3, KK3, in[2], 14);
  195. ROUND(bbb, ccc, ddd, eee, aaa, F3, KK3, in[10], 13);
  196. ROUND(aaa, bbb, ccc, ddd, eee, F3, KK3, in[0], 13);
  197. ROUND(eee, aaa, bbb, ccc, ddd, F3, KK3, in[4], 7);
  198. ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[13], 5);
  199. /* round 4: right lane */
  200. ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[8], 15);
  201. ROUND(bbb, ccc, ddd, eee, aaa, F2, KK4, in[6], 5);
  202. ROUND(aaa, bbb, ccc, ddd, eee, F2, KK4, in[4], 8);
  203. ROUND(eee, aaa, bbb, ccc, ddd, F2, KK4, in[1], 11);
  204. ROUND(ddd, eee, aaa, bbb, ccc, F2, KK4, in[3], 14);
  205. ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[11], 14);
  206. ROUND(bbb, ccc, ddd, eee, aaa, F2, KK4, in[15], 6);
  207. ROUND(aaa, bbb, ccc, ddd, eee, F2, KK4, in[0], 14);
  208. ROUND(eee, aaa, bbb, ccc, ddd, F2, KK4, in[5], 6);
  209. ROUND(ddd, eee, aaa, bbb, ccc, F2, KK4, in[12], 9);
  210. ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[2], 12);
  211. ROUND(bbb, ccc, ddd, eee, aaa, F2, KK4, in[13], 9);
  212. ROUND(aaa, bbb, ccc, ddd, eee, F2, KK4, in[9], 12);
  213. ROUND(eee, aaa, bbb, ccc, ddd, F2, KK4, in[7], 5);
  214. ROUND(ddd, eee, aaa, bbb, ccc, F2, KK4, in[10], 15);
  215. ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[14], 8);
  216. /* round 5: right lane */
  217. ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[12], 8);
  218. ROUND(aaa, bbb, ccc, ddd, eee, F1, KK5, in[15], 5);
  219. ROUND(eee, aaa, bbb, ccc, ddd, F1, KK5, in[10], 12);
  220. ROUND(ddd, eee, aaa, bbb, ccc, F1, KK5, in[4], 9);
  221. ROUND(ccc, ddd, eee, aaa, bbb, F1, KK5, in[1], 12);
  222. ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[5], 5);
  223. ROUND(aaa, bbb, ccc, ddd, eee, F1, KK5, in[8], 14);
  224. ROUND(eee, aaa, bbb, ccc, ddd, F1, KK5, in[7], 6);
  225. ROUND(ddd, eee, aaa, bbb, ccc, F1, KK5, in[6], 8);
  226. ROUND(ccc, ddd, eee, aaa, bbb, F1, KK5, in[2], 13);
  227. ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[13], 6);
  228. ROUND(aaa, bbb, ccc, ddd, eee, F1, KK5, in[14], 5);
  229. ROUND(eee, aaa, bbb, ccc, ddd, F1, KK5, in[0], 15);
  230. ROUND(ddd, eee, aaa, bbb, ccc, F1, KK5, in[3], 13);
  231. ROUND(ccc, ddd, eee, aaa, bbb, F1, KK5, in[9], 11);
  232. ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[11], 11);
  233. /* combine results */
  234. ddd += cc + state[1]; /* final result for state[0] */
  235. state[1] = state[2] + dd + eee;
  236. state[2] = state[3] + ee + aaa;
  237. state[3] = state[4] + aa + bbb;
  238. state[4] = state[0] + bb + ccc;
  239. state[0] = ddd;
  240. return;
  241. }
  242. static int rmd160_init(struct shash_desc *desc)
  243. {
  244. struct rmd160_ctx *rctx = shash_desc_ctx(desc);
  245. rctx->byte_count = 0;
  246. rctx->state[0] = RMD_H0;
  247. rctx->state[1] = RMD_H1;
  248. rctx->state[2] = RMD_H2;
  249. rctx->state[3] = RMD_H3;
  250. rctx->state[4] = RMD_H4;
  251. memset(rctx->buffer, 0, sizeof(rctx->buffer));
  252. return 0;
  253. }
  254. static int rmd160_update(struct shash_desc *desc, const u8 *data,
  255. unsigned int len)
  256. {
  257. struct rmd160_ctx *rctx = shash_desc_ctx(desc);
  258. const u32 avail = sizeof(rctx->buffer) - (rctx->byte_count & 0x3f);
  259. rctx->byte_count += len;
  260. /* Enough space in buffer? If so copy and we're done */
  261. if (avail > len) {
  262. memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
  263. data, len);
  264. goto out;
  265. }
  266. memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
  267. data, avail);
  268. rmd160_transform(rctx->state, rctx->buffer);
  269. data += avail;
  270. len -= avail;
  271. while (len >= sizeof(rctx->buffer)) {
  272. memcpy(rctx->buffer, data, sizeof(rctx->buffer));
  273. rmd160_transform(rctx->state, rctx->buffer);
  274. data += sizeof(rctx->buffer);
  275. len -= sizeof(rctx->buffer);
  276. }
  277. memcpy(rctx->buffer, data, len);
  278. out:
  279. return 0;
  280. }
  281. /* Add padding and return the message digest. */
  282. static int rmd160_final(struct shash_desc *desc, u8 *out)
  283. {
  284. struct rmd160_ctx *rctx = shash_desc_ctx(desc);
  285. u32 i, index, padlen;
  286. __le64 bits;
  287. __le32 *dst = (__le32 *)out;
  288. static const u8 padding[64] = { 0x80, };
  289. bits = cpu_to_le64(rctx->byte_count << 3);
  290. /* Pad out to 56 mod 64 */
  291. index = rctx->byte_count & 0x3f;
  292. padlen = (index < 56) ? (56 - index) : ((64+56) - index);
  293. rmd160_update(desc, padding, padlen);
  294. /* Append length */
  295. rmd160_update(desc, (const u8 *)&bits, sizeof(bits));
  296. /* Store state in digest */
  297. for (i = 0; i < 5; i++)
  298. dst[i] = cpu_to_le32p(&rctx->state[i]);
  299. /* Wipe context */
  300. memset(rctx, 0, sizeof(*rctx));
  301. return 0;
  302. }
  303. static struct shash_alg alg = {
  304. .digestsize = RMD160_DIGEST_SIZE,
  305. .init = rmd160_init,
  306. .update = rmd160_update,
  307. .final = rmd160_final,
  308. .descsize = sizeof(struct rmd160_ctx),
  309. .base = {
  310. .cra_name = "rmd160",
  311. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  312. .cra_blocksize = RMD160_BLOCK_SIZE,
  313. .cra_module = THIS_MODULE,
  314. }
  315. };
  316. static int __init rmd160_mod_init(void)
  317. {
  318. return crypto_register_shash(&alg);
  319. }
  320. static void __exit rmd160_mod_fini(void)
  321. {
  322. crypto_unregister_shash(&alg);
  323. }
  324. module_init(rmd160_mod_init);
  325. module_exit(rmd160_mod_fini);
  326. MODULE_LICENSE("GPL");
  327. MODULE_AUTHOR("Adrian-Ken Rueegsegger <ken@codelabs.ch>");
  328. MODULE_DESCRIPTION("RIPEMD-160 Message Digest");