rmd320.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /*
  2. * Cryptographic API.
  3. *
  4. * RIPEMD-320 - 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 rmd320_ctx {
  24. u64 byte_count;
  25. u32 state[10];
  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 rmd320_transform(u32 *state, const __le32 *in)
  49. {
  50. u32 aa, bb, cc, dd, ee, aaa, bbb, ccc, ddd, eee, tmp;
  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[5];
  59. bbb = state[6];
  60. ccc = state[7];
  61. ddd = state[8];
  62. eee = state[9];
  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 1: right lane */
  81. ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[5], 8);
  82. ROUND(eee, aaa, bbb, ccc, ddd, F5, KK1, in[14], 9);
  83. ROUND(ddd, eee, aaa, bbb, ccc, F5, KK1, in[7], 9);
  84. ROUND(ccc, ddd, eee, aaa, bbb, F5, KK1, in[0], 11);
  85. ROUND(bbb, ccc, ddd, eee, aaa, F5, KK1, in[9], 13);
  86. ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[2], 15);
  87. ROUND(eee, aaa, bbb, ccc, ddd, F5, KK1, in[11], 15);
  88. ROUND(ddd, eee, aaa, bbb, ccc, F5, KK1, in[4], 5);
  89. ROUND(ccc, ddd, eee, aaa, bbb, F5, KK1, in[13], 7);
  90. ROUND(bbb, ccc, ddd, eee, aaa, F5, KK1, in[6], 7);
  91. ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[15], 8);
  92. ROUND(eee, aaa, bbb, ccc, ddd, F5, KK1, in[8], 11);
  93. ROUND(ddd, eee, aaa, bbb, ccc, F5, KK1, in[1], 14);
  94. ROUND(ccc, ddd, eee, aaa, bbb, F5, KK1, in[10], 14);
  95. ROUND(bbb, ccc, ddd, eee, aaa, F5, KK1, in[3], 12);
  96. ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[12], 6);
  97. /* Swap contents of "a" registers */
  98. tmp = aa; aa = aaa; aaa = tmp;
  99. /* round 2: left lane" */
  100. ROUND(ee, aa, bb, cc, dd, F2, K2, in[7], 7);
  101. ROUND(dd, ee, aa, bb, cc, F2, K2, in[4], 6);
  102. ROUND(cc, dd, ee, aa, bb, F2, K2, in[13], 8);
  103. ROUND(bb, cc, dd, ee, aa, F2, K2, in[1], 13);
  104. ROUND(aa, bb, cc, dd, ee, F2, K2, in[10], 11);
  105. ROUND(ee, aa, bb, cc, dd, F2, K2, in[6], 9);
  106. ROUND(dd, ee, aa, bb, cc, F2, K2, in[15], 7);
  107. ROUND(cc, dd, ee, aa, bb, F2, K2, in[3], 15);
  108. ROUND(bb, cc, dd, ee, aa, F2, K2, in[12], 7);
  109. ROUND(aa, bb, cc, dd, ee, F2, K2, in[0], 12);
  110. ROUND(ee, aa, bb, cc, dd, F2, K2, in[9], 15);
  111. ROUND(dd, ee, aa, bb, cc, F2, K2, in[5], 9);
  112. ROUND(cc, dd, ee, aa, bb, F2, K2, in[2], 11);
  113. ROUND(bb, cc, dd, ee, aa, F2, K2, in[14], 7);
  114. ROUND(aa, bb, cc, dd, ee, F2, K2, in[11], 13);
  115. ROUND(ee, aa, bb, cc, dd, F2, K2, in[8], 12);
  116. /* round 2: right lane */
  117. ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[6], 9);
  118. ROUND(ddd, eee, aaa, bbb, ccc, F4, KK2, in[11], 13);
  119. ROUND(ccc, ddd, eee, aaa, bbb, F4, KK2, in[3], 15);
  120. ROUND(bbb, ccc, ddd, eee, aaa, F4, KK2, in[7], 7);
  121. ROUND(aaa, bbb, ccc, ddd, eee, F4, KK2, in[0], 12);
  122. ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[13], 8);
  123. ROUND(ddd, eee, aaa, bbb, ccc, F4, KK2, in[5], 9);
  124. ROUND(ccc, ddd, eee, aaa, bbb, F4, KK2, in[10], 11);
  125. ROUND(bbb, ccc, ddd, eee, aaa, F4, KK2, in[14], 7);
  126. ROUND(aaa, bbb, ccc, ddd, eee, F4, KK2, in[15], 7);
  127. ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[8], 12);
  128. ROUND(ddd, eee, aaa, bbb, ccc, F4, KK2, in[12], 7);
  129. ROUND(ccc, ddd, eee, aaa, bbb, F4, KK2, in[4], 6);
  130. ROUND(bbb, ccc, ddd, eee, aaa, F4, KK2, in[9], 15);
  131. ROUND(aaa, bbb, ccc, ddd, eee, F4, KK2, in[1], 13);
  132. ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[2], 11);
  133. /* Swap contents of "b" registers */
  134. tmp = bb; bb = bbb; bbb = tmp;
  135. /* round 3: left lane" */
  136. ROUND(dd, ee, aa, bb, cc, F3, K3, in[3], 11);
  137. ROUND(cc, dd, ee, aa, bb, F3, K3, in[10], 13);
  138. ROUND(bb, cc, dd, ee, aa, F3, K3, in[14], 6);
  139. ROUND(aa, bb, cc, dd, ee, F3, K3, in[4], 7);
  140. ROUND(ee, aa, bb, cc, dd, F3, K3, in[9], 14);
  141. ROUND(dd, ee, aa, bb, cc, F3, K3, in[15], 9);
  142. ROUND(cc, dd, ee, aa, bb, F3, K3, in[8], 13);
  143. ROUND(bb, cc, dd, ee, aa, F3, K3, in[1], 15);
  144. ROUND(aa, bb, cc, dd, ee, F3, K3, in[2], 14);
  145. ROUND(ee, aa, bb, cc, dd, F3, K3, in[7], 8);
  146. ROUND(dd, ee, aa, bb, cc, F3, K3, in[0], 13);
  147. ROUND(cc, dd, ee, aa, bb, F3, K3, in[6], 6);
  148. ROUND(bb, cc, dd, ee, aa, F3, K3, in[13], 5);
  149. ROUND(aa, bb, cc, dd, ee, F3, K3, in[11], 12);
  150. ROUND(ee, aa, bb, cc, dd, F3, K3, in[5], 7);
  151. ROUND(dd, ee, aa, bb, cc, F3, K3, in[12], 5);
  152. /* round 3: right lane */
  153. ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[15], 9);
  154. ROUND(ccc, ddd, eee, aaa, bbb, F3, KK3, in[5], 7);
  155. ROUND(bbb, ccc, ddd, eee, aaa, F3, KK3, in[1], 15);
  156. ROUND(aaa, bbb, ccc, ddd, eee, F3, KK3, in[3], 11);
  157. ROUND(eee, aaa, bbb, ccc, ddd, F3, KK3, in[7], 8);
  158. ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[14], 6);
  159. ROUND(ccc, ddd, eee, aaa, bbb, F3, KK3, in[6], 6);
  160. ROUND(bbb, ccc, ddd, eee, aaa, F3, KK3, in[9], 14);
  161. ROUND(aaa, bbb, ccc, ddd, eee, F3, KK3, in[11], 12);
  162. ROUND(eee, aaa, bbb, ccc, ddd, F3, KK3, in[8], 13);
  163. ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[12], 5);
  164. ROUND(ccc, ddd, eee, aaa, bbb, F3, KK3, in[2], 14);
  165. ROUND(bbb, ccc, ddd, eee, aaa, F3, KK3, in[10], 13);
  166. ROUND(aaa, bbb, ccc, ddd, eee, F3, KK3, in[0], 13);
  167. ROUND(eee, aaa, bbb, ccc, ddd, F3, KK3, in[4], 7);
  168. ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[13], 5);
  169. /* Swap contents of "c" registers */
  170. tmp = cc; cc = ccc; ccc = tmp;
  171. /* round 4: left lane" */
  172. ROUND(cc, dd, ee, aa, bb, F4, K4, in[1], 11);
  173. ROUND(bb, cc, dd, ee, aa, F4, K4, in[9], 12);
  174. ROUND(aa, bb, cc, dd, ee, F4, K4, in[11], 14);
  175. ROUND(ee, aa, bb, cc, dd, F4, K4, in[10], 15);
  176. ROUND(dd, ee, aa, bb, cc, F4, K4, in[0], 14);
  177. ROUND(cc, dd, ee, aa, bb, F4, K4, in[8], 15);
  178. ROUND(bb, cc, dd, ee, aa, F4, K4, in[12], 9);
  179. ROUND(aa, bb, cc, dd, ee, F4, K4, in[4], 8);
  180. ROUND(ee, aa, bb, cc, dd, F4, K4, in[13], 9);
  181. ROUND(dd, ee, aa, bb, cc, F4, K4, in[3], 14);
  182. ROUND(cc, dd, ee, aa, bb, F4, K4, in[7], 5);
  183. ROUND(bb, cc, dd, ee, aa, F4, K4, in[15], 6);
  184. ROUND(aa, bb, cc, dd, ee, F4, K4, in[14], 8);
  185. ROUND(ee, aa, bb, cc, dd, F4, K4, in[5], 6);
  186. ROUND(dd, ee, aa, bb, cc, F4, K4, in[6], 5);
  187. ROUND(cc, dd, ee, aa, bb, F4, K4, in[2], 12);
  188. /* round 4: right lane */
  189. ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[8], 15);
  190. ROUND(bbb, ccc, ddd, eee, aaa, F2, KK4, in[6], 5);
  191. ROUND(aaa, bbb, ccc, ddd, eee, F2, KK4, in[4], 8);
  192. ROUND(eee, aaa, bbb, ccc, ddd, F2, KK4, in[1], 11);
  193. ROUND(ddd, eee, aaa, bbb, ccc, F2, KK4, in[3], 14);
  194. ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[11], 14);
  195. ROUND(bbb, ccc, ddd, eee, aaa, F2, KK4, in[15], 6);
  196. ROUND(aaa, bbb, ccc, ddd, eee, F2, KK4, in[0], 14);
  197. ROUND(eee, aaa, bbb, ccc, ddd, F2, KK4, in[5], 6);
  198. ROUND(ddd, eee, aaa, bbb, ccc, F2, KK4, in[12], 9);
  199. ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[2], 12);
  200. ROUND(bbb, ccc, ddd, eee, aaa, F2, KK4, in[13], 9);
  201. ROUND(aaa, bbb, ccc, ddd, eee, F2, KK4, in[9], 12);
  202. ROUND(eee, aaa, bbb, ccc, ddd, F2, KK4, in[7], 5);
  203. ROUND(ddd, eee, aaa, bbb, ccc, F2, KK4, in[10], 15);
  204. ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[14], 8);
  205. /* Swap contents of "d" registers */
  206. tmp = dd; dd = ddd; ddd = tmp;
  207. /* round 5: left lane" */
  208. ROUND(bb, cc, dd, ee, aa, F5, K5, in[4], 9);
  209. ROUND(aa, bb, cc, dd, ee, F5, K5, in[0], 15);
  210. ROUND(ee, aa, bb, cc, dd, F5, K5, in[5], 5);
  211. ROUND(dd, ee, aa, bb, cc, F5, K5, in[9], 11);
  212. ROUND(cc, dd, ee, aa, bb, F5, K5, in[7], 6);
  213. ROUND(bb, cc, dd, ee, aa, F5, K5, in[12], 8);
  214. ROUND(aa, bb, cc, dd, ee, F5, K5, in[2], 13);
  215. ROUND(ee, aa, bb, cc, dd, F5, K5, in[10], 12);
  216. ROUND(dd, ee, aa, bb, cc, F5, K5, in[14], 5);
  217. ROUND(cc, dd, ee, aa, bb, F5, K5, in[1], 12);
  218. ROUND(bb, cc, dd, ee, aa, F5, K5, in[3], 13);
  219. ROUND(aa, bb, cc, dd, ee, F5, K5, in[8], 14);
  220. ROUND(ee, aa, bb, cc, dd, F5, K5, in[11], 11);
  221. ROUND(dd, ee, aa, bb, cc, F5, K5, in[6], 8);
  222. ROUND(cc, dd, ee, aa, bb, F5, K5, in[15], 5);
  223. ROUND(bb, cc, dd, ee, aa, F5, K5, in[13], 6);
  224. /* round 5: right lane */
  225. ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[12], 8);
  226. ROUND(aaa, bbb, ccc, ddd, eee, F1, KK5, in[15], 5);
  227. ROUND(eee, aaa, bbb, ccc, ddd, F1, KK5, in[10], 12);
  228. ROUND(ddd, eee, aaa, bbb, ccc, F1, KK5, in[4], 9);
  229. ROUND(ccc, ddd, eee, aaa, bbb, F1, KK5, in[1], 12);
  230. ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[5], 5);
  231. ROUND(aaa, bbb, ccc, ddd, eee, F1, KK5, in[8], 14);
  232. ROUND(eee, aaa, bbb, ccc, ddd, F1, KK5, in[7], 6);
  233. ROUND(ddd, eee, aaa, bbb, ccc, F1, KK5, in[6], 8);
  234. ROUND(ccc, ddd, eee, aaa, bbb, F1, KK5, in[2], 13);
  235. ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[13], 6);
  236. ROUND(aaa, bbb, ccc, ddd, eee, F1, KK5, in[14], 5);
  237. ROUND(eee, aaa, bbb, ccc, ddd, F1, KK5, in[0], 15);
  238. ROUND(ddd, eee, aaa, bbb, ccc, F1, KK5, in[3], 13);
  239. ROUND(ccc, ddd, eee, aaa, bbb, F1, KK5, in[9], 11);
  240. ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[11], 11);
  241. /* Swap contents of "e" registers */
  242. tmp = ee; ee = eee; eee = tmp;
  243. /* combine results */
  244. state[0] += aa;
  245. state[1] += bb;
  246. state[2] += cc;
  247. state[3] += dd;
  248. state[4] += ee;
  249. state[5] += aaa;
  250. state[6] += bbb;
  251. state[7] += ccc;
  252. state[8] += ddd;
  253. state[9] += eee;
  254. return;
  255. }
  256. static int rmd320_init(struct shash_desc *desc)
  257. {
  258. struct rmd320_ctx *rctx = shash_desc_ctx(desc);
  259. rctx->byte_count = 0;
  260. rctx->state[0] = RMD_H0;
  261. rctx->state[1] = RMD_H1;
  262. rctx->state[2] = RMD_H2;
  263. rctx->state[3] = RMD_H3;
  264. rctx->state[4] = RMD_H4;
  265. rctx->state[5] = RMD_H5;
  266. rctx->state[6] = RMD_H6;
  267. rctx->state[7] = RMD_H7;
  268. rctx->state[8] = RMD_H8;
  269. rctx->state[9] = RMD_H9;
  270. memset(rctx->buffer, 0, sizeof(rctx->buffer));
  271. return 0;
  272. }
  273. static int rmd320_update(struct shash_desc *desc, const u8 *data,
  274. unsigned int len)
  275. {
  276. struct rmd320_ctx *rctx = shash_desc_ctx(desc);
  277. const u32 avail = sizeof(rctx->buffer) - (rctx->byte_count & 0x3f);
  278. rctx->byte_count += len;
  279. /* Enough space in buffer? If so copy and we're done */
  280. if (avail > len) {
  281. memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
  282. data, len);
  283. goto out;
  284. }
  285. memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
  286. data, avail);
  287. rmd320_transform(rctx->state, rctx->buffer);
  288. data += avail;
  289. len -= avail;
  290. while (len >= sizeof(rctx->buffer)) {
  291. memcpy(rctx->buffer, data, sizeof(rctx->buffer));
  292. rmd320_transform(rctx->state, rctx->buffer);
  293. data += sizeof(rctx->buffer);
  294. len -= sizeof(rctx->buffer);
  295. }
  296. memcpy(rctx->buffer, data, len);
  297. out:
  298. return 0;
  299. }
  300. /* Add padding and return the message digest. */
  301. static int rmd320_final(struct shash_desc *desc, u8 *out)
  302. {
  303. struct rmd320_ctx *rctx = shash_desc_ctx(desc);
  304. u32 i, index, padlen;
  305. __le64 bits;
  306. __le32 *dst = (__le32 *)out;
  307. static const u8 padding[64] = { 0x80, };
  308. bits = cpu_to_le64(rctx->byte_count << 3);
  309. /* Pad out to 56 mod 64 */
  310. index = rctx->byte_count & 0x3f;
  311. padlen = (index < 56) ? (56 - index) : ((64+56) - index);
  312. rmd320_update(desc, padding, padlen);
  313. /* Append length */
  314. rmd320_update(desc, (const u8 *)&bits, sizeof(bits));
  315. /* Store state in digest */
  316. for (i = 0; i < 10; i++)
  317. dst[i] = cpu_to_le32p(&rctx->state[i]);
  318. /* Wipe context */
  319. memset(rctx, 0, sizeof(*rctx));
  320. return 0;
  321. }
  322. static struct shash_alg alg = {
  323. .digestsize = RMD320_DIGEST_SIZE,
  324. .init = rmd320_init,
  325. .update = rmd320_update,
  326. .final = rmd320_final,
  327. .descsize = sizeof(struct rmd320_ctx),
  328. .base = {
  329. .cra_name = "rmd320",
  330. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  331. .cra_blocksize = RMD320_BLOCK_SIZE,
  332. .cra_module = THIS_MODULE,
  333. }
  334. };
  335. static int __init rmd320_mod_init(void)
  336. {
  337. return crypto_register_shash(&alg);
  338. }
  339. static void __exit rmd320_mod_fini(void)
  340. {
  341. crypto_unregister_shash(&alg);
  342. }
  343. module_init(rmd320_mod_init);
  344. module_exit(rmd320_mod_fini);
  345. MODULE_LICENSE("GPL");
  346. MODULE_AUTHOR("Adrian-Ken Rueegsegger <ken@codelabs.ch>");
  347. MODULE_DESCRIPTION("RIPEMD-320 Message Digest");
  348. MODULE_ALIAS_CRYPTO("rmd320");