rmd128.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. * Cryptographic API.
  3. *
  4. * RIPEMD-128 - 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 rmd128_ctx {
  24. u64 byte_count;
  25. u32 state[4];
  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 KK1 RMD_K6
  33. #define KK2 RMD_K7
  34. #define KK3 RMD_K8
  35. #define KK4 RMD_K1
  36. #define F1(x, y, z) (x ^ y ^ z) /* XOR */
  37. #define F2(x, y, z) (z ^ (x & (y ^ z))) /* x ? y : z */
  38. #define F3(x, y, z) ((x | ~y) ^ z)
  39. #define F4(x, y, z) (y ^ (z & (x ^ y))) /* z ? x : y */
  40. #define ROUND(a, b, c, d, f, k, x, s) { \
  41. (a) += f((b), (c), (d)) + le32_to_cpup(&(x)) + (k); \
  42. (a) = rol32((a), (s)); \
  43. }
  44. static void rmd128_transform(u32 *state, const __le32 *in)
  45. {
  46. u32 aa, bb, cc, dd, aaa, bbb, ccc, ddd;
  47. /* Initialize left lane */
  48. aa = state[0];
  49. bb = state[1];
  50. cc = state[2];
  51. dd = state[3];
  52. /* Initialize right lane */
  53. aaa = state[0];
  54. bbb = state[1];
  55. ccc = state[2];
  56. ddd = state[3];
  57. /* round 1: left lane */
  58. ROUND(aa, bb, cc, dd, F1, K1, in[0], 11);
  59. ROUND(dd, aa, bb, cc, F1, K1, in[1], 14);
  60. ROUND(cc, dd, aa, bb, F1, K1, in[2], 15);
  61. ROUND(bb, cc, dd, aa, F1, K1, in[3], 12);
  62. ROUND(aa, bb, cc, dd, F1, K1, in[4], 5);
  63. ROUND(dd, aa, bb, cc, F1, K1, in[5], 8);
  64. ROUND(cc, dd, aa, bb, F1, K1, in[6], 7);
  65. ROUND(bb, cc, dd, aa, F1, K1, in[7], 9);
  66. ROUND(aa, bb, cc, dd, F1, K1, in[8], 11);
  67. ROUND(dd, aa, bb, cc, F1, K1, in[9], 13);
  68. ROUND(cc, dd, aa, bb, F1, K1, in[10], 14);
  69. ROUND(bb, cc, dd, aa, F1, K1, in[11], 15);
  70. ROUND(aa, bb, cc, dd, F1, K1, in[12], 6);
  71. ROUND(dd, aa, bb, cc, F1, K1, in[13], 7);
  72. ROUND(cc, dd, aa, bb, F1, K1, in[14], 9);
  73. ROUND(bb, cc, dd, aa, F1, K1, in[15], 8);
  74. /* round 2: left lane */
  75. ROUND(aa, bb, cc, dd, F2, K2, in[7], 7);
  76. ROUND(dd, aa, bb, cc, F2, K2, in[4], 6);
  77. ROUND(cc, dd, aa, bb, F2, K2, in[13], 8);
  78. ROUND(bb, cc, dd, aa, F2, K2, in[1], 13);
  79. ROUND(aa, bb, cc, dd, F2, K2, in[10], 11);
  80. ROUND(dd, aa, bb, cc, F2, K2, in[6], 9);
  81. ROUND(cc, dd, aa, bb, F2, K2, in[15], 7);
  82. ROUND(bb, cc, dd, aa, F2, K2, in[3], 15);
  83. ROUND(aa, bb, cc, dd, F2, K2, in[12], 7);
  84. ROUND(dd, aa, bb, cc, F2, K2, in[0], 12);
  85. ROUND(cc, dd, aa, bb, F2, K2, in[9], 15);
  86. ROUND(bb, cc, dd, aa, F2, K2, in[5], 9);
  87. ROUND(aa, bb, cc, dd, F2, K2, in[2], 11);
  88. ROUND(dd, aa, bb, cc, F2, K2, in[14], 7);
  89. ROUND(cc, dd, aa, bb, F2, K2, in[11], 13);
  90. ROUND(bb, cc, dd, aa, F2, K2, in[8], 12);
  91. /* round 3: left lane */
  92. ROUND(aa, bb, cc, dd, F3, K3, in[3], 11);
  93. ROUND(dd, aa, bb, cc, F3, K3, in[10], 13);
  94. ROUND(cc, dd, aa, bb, F3, K3, in[14], 6);
  95. ROUND(bb, cc, dd, aa, F3, K3, in[4], 7);
  96. ROUND(aa, bb, cc, dd, F3, K3, in[9], 14);
  97. ROUND(dd, aa, bb, cc, F3, K3, in[15], 9);
  98. ROUND(cc, dd, aa, bb, F3, K3, in[8], 13);
  99. ROUND(bb, cc, dd, aa, F3, K3, in[1], 15);
  100. ROUND(aa, bb, cc, dd, F3, K3, in[2], 14);
  101. ROUND(dd, aa, bb, cc, F3, K3, in[7], 8);
  102. ROUND(cc, dd, aa, bb, F3, K3, in[0], 13);
  103. ROUND(bb, cc, dd, aa, F3, K3, in[6], 6);
  104. ROUND(aa, bb, cc, dd, F3, K3, in[13], 5);
  105. ROUND(dd, aa, bb, cc, F3, K3, in[11], 12);
  106. ROUND(cc, dd, aa, bb, F3, K3, in[5], 7);
  107. ROUND(bb, cc, dd, aa, F3, K3, in[12], 5);
  108. /* round 4: left lane */
  109. ROUND(aa, bb, cc, dd, F4, K4, in[1], 11);
  110. ROUND(dd, aa, bb, cc, F4, K4, in[9], 12);
  111. ROUND(cc, dd, aa, bb, F4, K4, in[11], 14);
  112. ROUND(bb, cc, dd, aa, F4, K4, in[10], 15);
  113. ROUND(aa, bb, cc, dd, F4, K4, in[0], 14);
  114. ROUND(dd, aa, bb, cc, F4, K4, in[8], 15);
  115. ROUND(cc, dd, aa, bb, F4, K4, in[12], 9);
  116. ROUND(bb, cc, dd, aa, F4, K4, in[4], 8);
  117. ROUND(aa, bb, cc, dd, F4, K4, in[13], 9);
  118. ROUND(dd, aa, bb, cc, F4, K4, in[3], 14);
  119. ROUND(cc, dd, aa, bb, F4, K4, in[7], 5);
  120. ROUND(bb, cc, dd, aa, F4, K4, in[15], 6);
  121. ROUND(aa, bb, cc, dd, F4, K4, in[14], 8);
  122. ROUND(dd, aa, bb, cc, F4, K4, in[5], 6);
  123. ROUND(cc, dd, aa, bb, F4, K4, in[6], 5);
  124. ROUND(bb, cc, dd, aa, F4, K4, in[2], 12);
  125. /* round 1: right lane */
  126. ROUND(aaa, bbb, ccc, ddd, F4, KK1, in[5], 8);
  127. ROUND(ddd, aaa, bbb, ccc, F4, KK1, in[14], 9);
  128. ROUND(ccc, ddd, aaa, bbb, F4, KK1, in[7], 9);
  129. ROUND(bbb, ccc, ddd, aaa, F4, KK1, in[0], 11);
  130. ROUND(aaa, bbb, ccc, ddd, F4, KK1, in[9], 13);
  131. ROUND(ddd, aaa, bbb, ccc, F4, KK1, in[2], 15);
  132. ROUND(ccc, ddd, aaa, bbb, F4, KK1, in[11], 15);
  133. ROUND(bbb, ccc, ddd, aaa, F4, KK1, in[4], 5);
  134. ROUND(aaa, bbb, ccc, ddd, F4, KK1, in[13], 7);
  135. ROUND(ddd, aaa, bbb, ccc, F4, KK1, in[6], 7);
  136. ROUND(ccc, ddd, aaa, bbb, F4, KK1, in[15], 8);
  137. ROUND(bbb, ccc, ddd, aaa, F4, KK1, in[8], 11);
  138. ROUND(aaa, bbb, ccc, ddd, F4, KK1, in[1], 14);
  139. ROUND(ddd, aaa, bbb, ccc, F4, KK1, in[10], 14);
  140. ROUND(ccc, ddd, aaa, bbb, F4, KK1, in[3], 12);
  141. ROUND(bbb, ccc, ddd, aaa, F4, KK1, in[12], 6);
  142. /* round 2: right lane */
  143. ROUND(aaa, bbb, ccc, ddd, F3, KK2, in[6], 9);
  144. ROUND(ddd, aaa, bbb, ccc, F3, KK2, in[11], 13);
  145. ROUND(ccc, ddd, aaa, bbb, F3, KK2, in[3], 15);
  146. ROUND(bbb, ccc, ddd, aaa, F3, KK2, in[7], 7);
  147. ROUND(aaa, bbb, ccc, ddd, F3, KK2, in[0], 12);
  148. ROUND(ddd, aaa, bbb, ccc, F3, KK2, in[13], 8);
  149. ROUND(ccc, ddd, aaa, bbb, F3, KK2, in[5], 9);
  150. ROUND(bbb, ccc, ddd, aaa, F3, KK2, in[10], 11);
  151. ROUND(aaa, bbb, ccc, ddd, F3, KK2, in[14], 7);
  152. ROUND(ddd, aaa, bbb, ccc, F3, KK2, in[15], 7);
  153. ROUND(ccc, ddd, aaa, bbb, F3, KK2, in[8], 12);
  154. ROUND(bbb, ccc, ddd, aaa, F3, KK2, in[12], 7);
  155. ROUND(aaa, bbb, ccc, ddd, F3, KK2, in[4], 6);
  156. ROUND(ddd, aaa, bbb, ccc, F3, KK2, in[9], 15);
  157. ROUND(ccc, ddd, aaa, bbb, F3, KK2, in[1], 13);
  158. ROUND(bbb, ccc, ddd, aaa, F3, KK2, in[2], 11);
  159. /* round 3: right lane */
  160. ROUND(aaa, bbb, ccc, ddd, F2, KK3, in[15], 9);
  161. ROUND(ddd, aaa, bbb, ccc, F2, KK3, in[5], 7);
  162. ROUND(ccc, ddd, aaa, bbb, F2, KK3, in[1], 15);
  163. ROUND(bbb, ccc, ddd, aaa, F2, KK3, in[3], 11);
  164. ROUND(aaa, bbb, ccc, ddd, F2, KK3, in[7], 8);
  165. ROUND(ddd, aaa, bbb, ccc, F2, KK3, in[14], 6);
  166. ROUND(ccc, ddd, aaa, bbb, F2, KK3, in[6], 6);
  167. ROUND(bbb, ccc, ddd, aaa, F2, KK3, in[9], 14);
  168. ROUND(aaa, bbb, ccc, ddd, F2, KK3, in[11], 12);
  169. ROUND(ddd, aaa, bbb, ccc, F2, KK3, in[8], 13);
  170. ROUND(ccc, ddd, aaa, bbb, F2, KK3, in[12], 5);
  171. ROUND(bbb, ccc, ddd, aaa, F2, KK3, in[2], 14);
  172. ROUND(aaa, bbb, ccc, ddd, F2, KK3, in[10], 13);
  173. ROUND(ddd, aaa, bbb, ccc, F2, KK3, in[0], 13);
  174. ROUND(ccc, ddd, aaa, bbb, F2, KK3, in[4], 7);
  175. ROUND(bbb, ccc, ddd, aaa, F2, KK3, in[13], 5);
  176. /* round 4: right lane */
  177. ROUND(aaa, bbb, ccc, ddd, F1, KK4, in[8], 15);
  178. ROUND(ddd, aaa, bbb, ccc, F1, KK4, in[6], 5);
  179. ROUND(ccc, ddd, aaa, bbb, F1, KK4, in[4], 8);
  180. ROUND(bbb, ccc, ddd, aaa, F1, KK4, in[1], 11);
  181. ROUND(aaa, bbb, ccc, ddd, F1, KK4, in[3], 14);
  182. ROUND(ddd, aaa, bbb, ccc, F1, KK4, in[11], 14);
  183. ROUND(ccc, ddd, aaa, bbb, F1, KK4, in[15], 6);
  184. ROUND(bbb, ccc, ddd, aaa, F1, KK4, in[0], 14);
  185. ROUND(aaa, bbb, ccc, ddd, F1, KK4, in[5], 6);
  186. ROUND(ddd, aaa, bbb, ccc, F1, KK4, in[12], 9);
  187. ROUND(ccc, ddd, aaa, bbb, F1, KK4, in[2], 12);
  188. ROUND(bbb, ccc, ddd, aaa, F1, KK4, in[13], 9);
  189. ROUND(aaa, bbb, ccc, ddd, F1, KK4, in[9], 12);
  190. ROUND(ddd, aaa, bbb, ccc, F1, KK4, in[7], 5);
  191. ROUND(ccc, ddd, aaa, bbb, F1, KK4, in[10], 15);
  192. ROUND(bbb, ccc, ddd, aaa, F1, KK4, in[14], 8);
  193. /* combine results */
  194. ddd += cc + state[1]; /* final result for state[0] */
  195. state[1] = state[2] + dd + aaa;
  196. state[2] = state[3] + aa + bbb;
  197. state[3] = state[0] + bb + ccc;
  198. state[0] = ddd;
  199. }
  200. static int rmd128_init(struct shash_desc *desc)
  201. {
  202. struct rmd128_ctx *rctx = shash_desc_ctx(desc);
  203. rctx->byte_count = 0;
  204. rctx->state[0] = RMD_H0;
  205. rctx->state[1] = RMD_H1;
  206. rctx->state[2] = RMD_H2;
  207. rctx->state[3] = RMD_H3;
  208. memset(rctx->buffer, 0, sizeof(rctx->buffer));
  209. return 0;
  210. }
  211. static int rmd128_update(struct shash_desc *desc, const u8 *data,
  212. unsigned int len)
  213. {
  214. struct rmd128_ctx *rctx = shash_desc_ctx(desc);
  215. const u32 avail = sizeof(rctx->buffer) - (rctx->byte_count & 0x3f);
  216. rctx->byte_count += len;
  217. /* Enough space in buffer? If so copy and we're done */
  218. if (avail > len) {
  219. memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
  220. data, len);
  221. goto out;
  222. }
  223. memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
  224. data, avail);
  225. rmd128_transform(rctx->state, rctx->buffer);
  226. data += avail;
  227. len -= avail;
  228. while (len >= sizeof(rctx->buffer)) {
  229. memcpy(rctx->buffer, data, sizeof(rctx->buffer));
  230. rmd128_transform(rctx->state, rctx->buffer);
  231. data += sizeof(rctx->buffer);
  232. len -= sizeof(rctx->buffer);
  233. }
  234. memcpy(rctx->buffer, data, len);
  235. out:
  236. return 0;
  237. }
  238. /* Add padding and return the message digest. */
  239. static int rmd128_final(struct shash_desc *desc, u8 *out)
  240. {
  241. struct rmd128_ctx *rctx = shash_desc_ctx(desc);
  242. u32 i, index, padlen;
  243. __le64 bits;
  244. __le32 *dst = (__le32 *)out;
  245. static const u8 padding[64] = { 0x80, };
  246. bits = cpu_to_le64(rctx->byte_count << 3);
  247. /* Pad out to 56 mod 64 */
  248. index = rctx->byte_count & 0x3f;
  249. padlen = (index < 56) ? (56 - index) : ((64+56) - index);
  250. rmd128_update(desc, padding, padlen);
  251. /* Append length */
  252. rmd128_update(desc, (const u8 *)&bits, sizeof(bits));
  253. /* Store state in digest */
  254. for (i = 0; i < 4; i++)
  255. dst[i] = cpu_to_le32p(&rctx->state[i]);
  256. /* Wipe context */
  257. memset(rctx, 0, sizeof(*rctx));
  258. return 0;
  259. }
  260. static struct shash_alg alg = {
  261. .digestsize = RMD128_DIGEST_SIZE,
  262. .init = rmd128_init,
  263. .update = rmd128_update,
  264. .final = rmd128_final,
  265. .descsize = sizeof(struct rmd128_ctx),
  266. .base = {
  267. .cra_name = "rmd128",
  268. .cra_blocksize = RMD128_BLOCK_SIZE,
  269. .cra_module = THIS_MODULE,
  270. }
  271. };
  272. static int __init rmd128_mod_init(void)
  273. {
  274. return crypto_register_shash(&alg);
  275. }
  276. static void __exit rmd128_mod_fini(void)
  277. {
  278. crypto_unregister_shash(&alg);
  279. }
  280. module_init(rmd128_mod_init);
  281. module_exit(rmd128_mod_fini);
  282. MODULE_LICENSE("GPL");
  283. MODULE_AUTHOR("Adrian-Ken Rueegsegger <ken@codelabs.ch>");
  284. MODULE_DESCRIPTION("RIPEMD-128 Message Digest");
  285. MODULE_ALIAS_CRYPTO("rmd128");