rmd160.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. }
  241. static int rmd160_init(struct shash_desc *desc)
  242. {
  243. struct rmd160_ctx *rctx = shash_desc_ctx(desc);
  244. rctx->byte_count = 0;
  245. rctx->state[0] = RMD_H0;
  246. rctx->state[1] = RMD_H1;
  247. rctx->state[2] = RMD_H2;
  248. rctx->state[3] = RMD_H3;
  249. rctx->state[4] = RMD_H4;
  250. memset(rctx->buffer, 0, sizeof(rctx->buffer));
  251. return 0;
  252. }
  253. static int rmd160_update(struct shash_desc *desc, const u8 *data,
  254. unsigned int len)
  255. {
  256. struct rmd160_ctx *rctx = shash_desc_ctx(desc);
  257. const u32 avail = sizeof(rctx->buffer) - (rctx->byte_count & 0x3f);
  258. rctx->byte_count += len;
  259. /* Enough space in buffer? If so copy and we're done */
  260. if (avail > len) {
  261. memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
  262. data, len);
  263. goto out;
  264. }
  265. memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
  266. data, avail);
  267. rmd160_transform(rctx->state, rctx->buffer);
  268. data += avail;
  269. len -= avail;
  270. while (len >= sizeof(rctx->buffer)) {
  271. memcpy(rctx->buffer, data, sizeof(rctx->buffer));
  272. rmd160_transform(rctx->state, rctx->buffer);
  273. data += sizeof(rctx->buffer);
  274. len -= sizeof(rctx->buffer);
  275. }
  276. memcpy(rctx->buffer, data, len);
  277. out:
  278. return 0;
  279. }
  280. /* Add padding and return the message digest. */
  281. static int rmd160_final(struct shash_desc *desc, u8 *out)
  282. {
  283. struct rmd160_ctx *rctx = shash_desc_ctx(desc);
  284. u32 i, index, padlen;
  285. __le64 bits;
  286. __le32 *dst = (__le32 *)out;
  287. static const u8 padding[64] = { 0x80, };
  288. bits = cpu_to_le64(rctx->byte_count << 3);
  289. /* Pad out to 56 mod 64 */
  290. index = rctx->byte_count & 0x3f;
  291. padlen = (index < 56) ? (56 - index) : ((64+56) - index);
  292. rmd160_update(desc, padding, padlen);
  293. /* Append length */
  294. rmd160_update(desc, (const u8 *)&bits, sizeof(bits));
  295. /* Store state in digest */
  296. for (i = 0; i < 5; i++)
  297. dst[i] = cpu_to_le32p(&rctx->state[i]);
  298. /* Wipe context */
  299. memset(rctx, 0, sizeof(*rctx));
  300. return 0;
  301. }
  302. static struct shash_alg alg = {
  303. .digestsize = RMD160_DIGEST_SIZE,
  304. .init = rmd160_init,
  305. .update = rmd160_update,
  306. .final = rmd160_final,
  307. .descsize = sizeof(struct rmd160_ctx),
  308. .base = {
  309. .cra_name = "rmd160",
  310. .cra_blocksize = RMD160_BLOCK_SIZE,
  311. .cra_module = THIS_MODULE,
  312. }
  313. };
  314. static int __init rmd160_mod_init(void)
  315. {
  316. return crypto_register_shash(&alg);
  317. }
  318. static void __exit rmd160_mod_fini(void)
  319. {
  320. crypto_unregister_shash(&alg);
  321. }
  322. module_init(rmd160_mod_init);
  323. module_exit(rmd160_mod_fini);
  324. MODULE_LICENSE("GPL");
  325. MODULE_AUTHOR("Adrian-Ken Rueegsegger <ken@codelabs.ch>");
  326. MODULE_DESCRIPTION("RIPEMD-160 Message Digest");
  327. MODULE_ALIAS_CRYPTO("rmd160");