rmd256.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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, tmp;
  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. tmp = aa; aa = aaa; aaa = tmp;
  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. tmp = bb; bb = bbb; bbb = tmp;
  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. tmp = cc; cc = ccc; ccc = tmp;
  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. tmp = dd; dd = ddd; ddd = tmp;
  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. return;
  211. }
  212. static int rmd256_init(struct shash_desc *desc)
  213. {
  214. struct rmd256_ctx *rctx = shash_desc_ctx(desc);
  215. rctx->byte_count = 0;
  216. rctx->state[0] = RMD_H0;
  217. rctx->state[1] = RMD_H1;
  218. rctx->state[2] = RMD_H2;
  219. rctx->state[3] = RMD_H3;
  220. rctx->state[4] = RMD_H5;
  221. rctx->state[5] = RMD_H6;
  222. rctx->state[6] = RMD_H7;
  223. rctx->state[7] = RMD_H8;
  224. memset(rctx->buffer, 0, sizeof(rctx->buffer));
  225. return 0;
  226. }
  227. static int rmd256_update(struct shash_desc *desc, const u8 *data,
  228. unsigned int len)
  229. {
  230. struct rmd256_ctx *rctx = shash_desc_ctx(desc);
  231. const u32 avail = sizeof(rctx->buffer) - (rctx->byte_count & 0x3f);
  232. rctx->byte_count += len;
  233. /* Enough space in buffer? If so copy and we're done */
  234. if (avail > len) {
  235. memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
  236. data, len);
  237. goto out;
  238. }
  239. memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
  240. data, avail);
  241. rmd256_transform(rctx->state, rctx->buffer);
  242. data += avail;
  243. len -= avail;
  244. while (len >= sizeof(rctx->buffer)) {
  245. memcpy(rctx->buffer, data, sizeof(rctx->buffer));
  246. rmd256_transform(rctx->state, rctx->buffer);
  247. data += sizeof(rctx->buffer);
  248. len -= sizeof(rctx->buffer);
  249. }
  250. memcpy(rctx->buffer, data, len);
  251. out:
  252. return 0;
  253. }
  254. /* Add padding and return the message digest. */
  255. static int rmd256_final(struct shash_desc *desc, u8 *out)
  256. {
  257. struct rmd256_ctx *rctx = shash_desc_ctx(desc);
  258. u32 i, index, padlen;
  259. __le64 bits;
  260. __le32 *dst = (__le32 *)out;
  261. static const u8 padding[64] = { 0x80, };
  262. bits = cpu_to_le64(rctx->byte_count << 3);
  263. /* Pad out to 56 mod 64 */
  264. index = rctx->byte_count & 0x3f;
  265. padlen = (index < 56) ? (56 - index) : ((64+56) - index);
  266. rmd256_update(desc, padding, padlen);
  267. /* Append length */
  268. rmd256_update(desc, (const u8 *)&bits, sizeof(bits));
  269. /* Store state in digest */
  270. for (i = 0; i < 8; i++)
  271. dst[i] = cpu_to_le32p(&rctx->state[i]);
  272. /* Wipe context */
  273. memset(rctx, 0, sizeof(*rctx));
  274. return 0;
  275. }
  276. static struct shash_alg alg = {
  277. .digestsize = RMD256_DIGEST_SIZE,
  278. .init = rmd256_init,
  279. .update = rmd256_update,
  280. .final = rmd256_final,
  281. .descsize = sizeof(struct rmd256_ctx),
  282. .base = {
  283. .cra_name = "rmd256",
  284. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  285. .cra_blocksize = RMD256_BLOCK_SIZE,
  286. .cra_module = THIS_MODULE,
  287. }
  288. };
  289. static int __init rmd256_mod_init(void)
  290. {
  291. return crypto_register_shash(&alg);
  292. }
  293. static void __exit rmd256_mod_fini(void)
  294. {
  295. crypto_unregister_shash(&alg);
  296. }
  297. module_init(rmd256_mod_init);
  298. module_exit(rmd256_mod_fini);
  299. MODULE_LICENSE("GPL");
  300. MODULE_AUTHOR("Adrian-Ken Rueegsegger <ken@codelabs.ch>");
  301. MODULE_DESCRIPTION("RIPEMD-256 Message Digest");
  302. MODULE_ALIAS_CRYPTO("rmd256");