rmd320.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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;
  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. swap(aa, aaa);
  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. swap(bb, bbb);
  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. swap(cc, ccc);
  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. swap(dd, ddd);
  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. swap(ee, eee);
  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. }
  255. static int rmd320_init(struct shash_desc *desc)
  256. {
  257. struct rmd320_ctx *rctx = shash_desc_ctx(desc);
  258. rctx->byte_count = 0;
  259. rctx->state[0] = RMD_H0;
  260. rctx->state[1] = RMD_H1;
  261. rctx->state[2] = RMD_H2;
  262. rctx->state[3] = RMD_H3;
  263. rctx->state[4] = RMD_H4;
  264. rctx->state[5] = RMD_H5;
  265. rctx->state[6] = RMD_H6;
  266. rctx->state[7] = RMD_H7;
  267. rctx->state[8] = RMD_H8;
  268. rctx->state[9] = RMD_H9;
  269. memset(rctx->buffer, 0, sizeof(rctx->buffer));
  270. return 0;
  271. }
  272. static int rmd320_update(struct shash_desc *desc, const u8 *data,
  273. unsigned int len)
  274. {
  275. struct rmd320_ctx *rctx = shash_desc_ctx(desc);
  276. const u32 avail = sizeof(rctx->buffer) - (rctx->byte_count & 0x3f);
  277. rctx->byte_count += len;
  278. /* Enough space in buffer? If so copy and we're done */
  279. if (avail > len) {
  280. memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
  281. data, len);
  282. goto out;
  283. }
  284. memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
  285. data, avail);
  286. rmd320_transform(rctx->state, rctx->buffer);
  287. data += avail;
  288. len -= avail;
  289. while (len >= sizeof(rctx->buffer)) {
  290. memcpy(rctx->buffer, data, sizeof(rctx->buffer));
  291. rmd320_transform(rctx->state, rctx->buffer);
  292. data += sizeof(rctx->buffer);
  293. len -= sizeof(rctx->buffer);
  294. }
  295. memcpy(rctx->buffer, data, len);
  296. out:
  297. return 0;
  298. }
  299. /* Add padding and return the message digest. */
  300. static int rmd320_final(struct shash_desc *desc, u8 *out)
  301. {
  302. struct rmd320_ctx *rctx = shash_desc_ctx(desc);
  303. u32 i, index, padlen;
  304. __le64 bits;
  305. __le32 *dst = (__le32 *)out;
  306. static const u8 padding[64] = { 0x80, };
  307. bits = cpu_to_le64(rctx->byte_count << 3);
  308. /* Pad out to 56 mod 64 */
  309. index = rctx->byte_count & 0x3f;
  310. padlen = (index < 56) ? (56 - index) : ((64+56) - index);
  311. rmd320_update(desc, padding, padlen);
  312. /* Append length */
  313. rmd320_update(desc, (const u8 *)&bits, sizeof(bits));
  314. /* Store state in digest */
  315. for (i = 0; i < 10; i++)
  316. dst[i] = cpu_to_le32p(&rctx->state[i]);
  317. /* Wipe context */
  318. memset(rctx, 0, sizeof(*rctx));
  319. return 0;
  320. }
  321. static struct shash_alg alg = {
  322. .digestsize = RMD320_DIGEST_SIZE,
  323. .init = rmd320_init,
  324. .update = rmd320_update,
  325. .final = rmd320_final,
  326. .descsize = sizeof(struct rmd320_ctx),
  327. .base = {
  328. .cra_name = "rmd320",
  329. .cra_blocksize = RMD320_BLOCK_SIZE,
  330. .cra_module = THIS_MODULE,
  331. }
  332. };
  333. static int __init rmd320_mod_init(void)
  334. {
  335. return crypto_register_shash(&alg);
  336. }
  337. static void __exit rmd320_mod_fini(void)
  338. {
  339. crypto_unregister_shash(&alg);
  340. }
  341. module_init(rmd320_mod_init);
  342. module_exit(rmd320_mod_fini);
  343. MODULE_LICENSE("GPL");
  344. MODULE_AUTHOR("Adrian-Ken Rueegsegger <ken@codelabs.ch>");
  345. MODULE_DESCRIPTION("RIPEMD-320 Message Digest");
  346. MODULE_ALIAS_CRYPTO("rmd320");