rmd256.c 10 KB

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