poly1305_generic.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * Poly1305 authenticator algorithm, RFC7539
  3. *
  4. * Copyright (C) 2015 Martin Willi
  5. *
  6. * Based on public domain code by Andrew Moon and Daniel J. Bernstein.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. #include <crypto/algapi.h>
  14. #include <crypto/internal/hash.h>
  15. #include <crypto/poly1305.h>
  16. #include <linux/crypto.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. static inline u64 mlt(u64 a, u64 b)
  20. {
  21. return a * b;
  22. }
  23. static inline u32 sr(u64 v, u_char n)
  24. {
  25. return v >> n;
  26. }
  27. static inline u32 and(u32 v, u32 mask)
  28. {
  29. return v & mask;
  30. }
  31. static inline u32 le32_to_cpuvp(const void *p)
  32. {
  33. return le32_to_cpup(p);
  34. }
  35. int crypto_poly1305_init(struct shash_desc *desc)
  36. {
  37. struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc);
  38. memset(dctx->h, 0, sizeof(dctx->h));
  39. dctx->buflen = 0;
  40. dctx->rset = false;
  41. dctx->sset = false;
  42. return 0;
  43. }
  44. EXPORT_SYMBOL_GPL(crypto_poly1305_init);
  45. static void poly1305_setrkey(struct poly1305_desc_ctx *dctx, const u8 *key)
  46. {
  47. /* r &= 0xffffffc0ffffffc0ffffffc0fffffff */
  48. dctx->r[0] = (le32_to_cpuvp(key + 0) >> 0) & 0x3ffffff;
  49. dctx->r[1] = (le32_to_cpuvp(key + 3) >> 2) & 0x3ffff03;
  50. dctx->r[2] = (le32_to_cpuvp(key + 6) >> 4) & 0x3ffc0ff;
  51. dctx->r[3] = (le32_to_cpuvp(key + 9) >> 6) & 0x3f03fff;
  52. dctx->r[4] = (le32_to_cpuvp(key + 12) >> 8) & 0x00fffff;
  53. }
  54. static void poly1305_setskey(struct poly1305_desc_ctx *dctx, const u8 *key)
  55. {
  56. dctx->s[0] = le32_to_cpuvp(key + 0);
  57. dctx->s[1] = le32_to_cpuvp(key + 4);
  58. dctx->s[2] = le32_to_cpuvp(key + 8);
  59. dctx->s[3] = le32_to_cpuvp(key + 12);
  60. }
  61. /*
  62. * Poly1305 requires a unique key for each tag, which implies that we can't set
  63. * it on the tfm that gets accessed by multiple users simultaneously. Instead we
  64. * expect the key as the first 32 bytes in the update() call.
  65. */
  66. unsigned int crypto_poly1305_setdesckey(struct poly1305_desc_ctx *dctx,
  67. const u8 *src, unsigned int srclen)
  68. {
  69. if (!dctx->sset) {
  70. if (!dctx->rset && srclen >= POLY1305_BLOCK_SIZE) {
  71. poly1305_setrkey(dctx, src);
  72. src += POLY1305_BLOCK_SIZE;
  73. srclen -= POLY1305_BLOCK_SIZE;
  74. dctx->rset = true;
  75. }
  76. if (srclen >= POLY1305_BLOCK_SIZE) {
  77. poly1305_setskey(dctx, src);
  78. src += POLY1305_BLOCK_SIZE;
  79. srclen -= POLY1305_BLOCK_SIZE;
  80. dctx->sset = true;
  81. }
  82. }
  83. return srclen;
  84. }
  85. EXPORT_SYMBOL_GPL(crypto_poly1305_setdesckey);
  86. static unsigned int poly1305_blocks(struct poly1305_desc_ctx *dctx,
  87. const u8 *src, unsigned int srclen,
  88. u32 hibit)
  89. {
  90. u32 r0, r1, r2, r3, r4;
  91. u32 s1, s2, s3, s4;
  92. u32 h0, h1, h2, h3, h4;
  93. u64 d0, d1, d2, d3, d4;
  94. unsigned int datalen;
  95. if (unlikely(!dctx->sset)) {
  96. datalen = crypto_poly1305_setdesckey(dctx, src, srclen);
  97. src += srclen - datalen;
  98. srclen = datalen;
  99. }
  100. r0 = dctx->r[0];
  101. r1 = dctx->r[1];
  102. r2 = dctx->r[2];
  103. r3 = dctx->r[3];
  104. r4 = dctx->r[4];
  105. s1 = r1 * 5;
  106. s2 = r2 * 5;
  107. s3 = r3 * 5;
  108. s4 = r4 * 5;
  109. h0 = dctx->h[0];
  110. h1 = dctx->h[1];
  111. h2 = dctx->h[2];
  112. h3 = dctx->h[3];
  113. h4 = dctx->h[4];
  114. while (likely(srclen >= POLY1305_BLOCK_SIZE)) {
  115. /* h += m[i] */
  116. h0 += (le32_to_cpuvp(src + 0) >> 0) & 0x3ffffff;
  117. h1 += (le32_to_cpuvp(src + 3) >> 2) & 0x3ffffff;
  118. h2 += (le32_to_cpuvp(src + 6) >> 4) & 0x3ffffff;
  119. h3 += (le32_to_cpuvp(src + 9) >> 6) & 0x3ffffff;
  120. h4 += (le32_to_cpuvp(src + 12) >> 8) | hibit;
  121. /* h *= r */
  122. d0 = mlt(h0, r0) + mlt(h1, s4) + mlt(h2, s3) +
  123. mlt(h3, s2) + mlt(h4, s1);
  124. d1 = mlt(h0, r1) + mlt(h1, r0) + mlt(h2, s4) +
  125. mlt(h3, s3) + mlt(h4, s2);
  126. d2 = mlt(h0, r2) + mlt(h1, r1) + mlt(h2, r0) +
  127. mlt(h3, s4) + mlt(h4, s3);
  128. d3 = mlt(h0, r3) + mlt(h1, r2) + mlt(h2, r1) +
  129. mlt(h3, r0) + mlt(h4, s4);
  130. d4 = mlt(h0, r4) + mlt(h1, r3) + mlt(h2, r2) +
  131. mlt(h3, r1) + mlt(h4, r0);
  132. /* (partial) h %= p */
  133. d1 += sr(d0, 26); h0 = and(d0, 0x3ffffff);
  134. d2 += sr(d1, 26); h1 = and(d1, 0x3ffffff);
  135. d3 += sr(d2, 26); h2 = and(d2, 0x3ffffff);
  136. d4 += sr(d3, 26); h3 = and(d3, 0x3ffffff);
  137. h0 += sr(d4, 26) * 5; h4 = and(d4, 0x3ffffff);
  138. h1 += h0 >> 26; h0 = h0 & 0x3ffffff;
  139. src += POLY1305_BLOCK_SIZE;
  140. srclen -= POLY1305_BLOCK_SIZE;
  141. }
  142. dctx->h[0] = h0;
  143. dctx->h[1] = h1;
  144. dctx->h[2] = h2;
  145. dctx->h[3] = h3;
  146. dctx->h[4] = h4;
  147. return srclen;
  148. }
  149. int crypto_poly1305_update(struct shash_desc *desc,
  150. const u8 *src, unsigned int srclen)
  151. {
  152. struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc);
  153. unsigned int bytes;
  154. if (unlikely(dctx->buflen)) {
  155. bytes = min(srclen, POLY1305_BLOCK_SIZE - dctx->buflen);
  156. memcpy(dctx->buf + dctx->buflen, src, bytes);
  157. src += bytes;
  158. srclen -= bytes;
  159. dctx->buflen += bytes;
  160. if (dctx->buflen == POLY1305_BLOCK_SIZE) {
  161. poly1305_blocks(dctx, dctx->buf,
  162. POLY1305_BLOCK_SIZE, 1 << 24);
  163. dctx->buflen = 0;
  164. }
  165. }
  166. if (likely(srclen >= POLY1305_BLOCK_SIZE)) {
  167. bytes = poly1305_blocks(dctx, src, srclen, 1 << 24);
  168. src += srclen - bytes;
  169. srclen = bytes;
  170. }
  171. if (unlikely(srclen)) {
  172. dctx->buflen = srclen;
  173. memcpy(dctx->buf, src, srclen);
  174. }
  175. return 0;
  176. }
  177. EXPORT_SYMBOL_GPL(crypto_poly1305_update);
  178. int crypto_poly1305_final(struct shash_desc *desc, u8 *dst)
  179. {
  180. struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc);
  181. __le32 *mac = (__le32 *)dst;
  182. u32 h0, h1, h2, h3, h4;
  183. u32 g0, g1, g2, g3, g4;
  184. u32 mask;
  185. u64 f = 0;
  186. if (unlikely(!dctx->sset))
  187. return -ENOKEY;
  188. if (unlikely(dctx->buflen)) {
  189. dctx->buf[dctx->buflen++] = 1;
  190. memset(dctx->buf + dctx->buflen, 0,
  191. POLY1305_BLOCK_SIZE - dctx->buflen);
  192. poly1305_blocks(dctx, dctx->buf, POLY1305_BLOCK_SIZE, 0);
  193. }
  194. /* fully carry h */
  195. h0 = dctx->h[0];
  196. h1 = dctx->h[1];
  197. h2 = dctx->h[2];
  198. h3 = dctx->h[3];
  199. h4 = dctx->h[4];
  200. h2 += (h1 >> 26); h1 = h1 & 0x3ffffff;
  201. h3 += (h2 >> 26); h2 = h2 & 0x3ffffff;
  202. h4 += (h3 >> 26); h3 = h3 & 0x3ffffff;
  203. h0 += (h4 >> 26) * 5; h4 = h4 & 0x3ffffff;
  204. h1 += (h0 >> 26); h0 = h0 & 0x3ffffff;
  205. /* compute h + -p */
  206. g0 = h0 + 5;
  207. g1 = h1 + (g0 >> 26); g0 &= 0x3ffffff;
  208. g2 = h2 + (g1 >> 26); g1 &= 0x3ffffff;
  209. g3 = h3 + (g2 >> 26); g2 &= 0x3ffffff;
  210. g4 = h4 + (g3 >> 26) - (1 << 26); g3 &= 0x3ffffff;
  211. /* select h if h < p, or h + -p if h >= p */
  212. mask = (g4 >> ((sizeof(u32) * 8) - 1)) - 1;
  213. g0 &= mask;
  214. g1 &= mask;
  215. g2 &= mask;
  216. g3 &= mask;
  217. g4 &= mask;
  218. mask = ~mask;
  219. h0 = (h0 & mask) | g0;
  220. h1 = (h1 & mask) | g1;
  221. h2 = (h2 & mask) | g2;
  222. h3 = (h3 & mask) | g3;
  223. h4 = (h4 & mask) | g4;
  224. /* h = h % (2^128) */
  225. h0 = (h0 >> 0) | (h1 << 26);
  226. h1 = (h1 >> 6) | (h2 << 20);
  227. h2 = (h2 >> 12) | (h3 << 14);
  228. h3 = (h3 >> 18) | (h4 << 8);
  229. /* mac = (h + s) % (2^128) */
  230. f = (f >> 32) + h0 + dctx->s[0]; mac[0] = cpu_to_le32(f);
  231. f = (f >> 32) + h1 + dctx->s[1]; mac[1] = cpu_to_le32(f);
  232. f = (f >> 32) + h2 + dctx->s[2]; mac[2] = cpu_to_le32(f);
  233. f = (f >> 32) + h3 + dctx->s[3]; mac[3] = cpu_to_le32(f);
  234. return 0;
  235. }
  236. EXPORT_SYMBOL_GPL(crypto_poly1305_final);
  237. static struct shash_alg poly1305_alg = {
  238. .digestsize = POLY1305_DIGEST_SIZE,
  239. .init = crypto_poly1305_init,
  240. .update = crypto_poly1305_update,
  241. .final = crypto_poly1305_final,
  242. .descsize = sizeof(struct poly1305_desc_ctx),
  243. .base = {
  244. .cra_name = "poly1305",
  245. .cra_driver_name = "poly1305-generic",
  246. .cra_priority = 100,
  247. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  248. .cra_alignmask = sizeof(u32) - 1,
  249. .cra_blocksize = POLY1305_BLOCK_SIZE,
  250. .cra_module = THIS_MODULE,
  251. },
  252. };
  253. static int __init poly1305_mod_init(void)
  254. {
  255. return crypto_register_shash(&poly1305_alg);
  256. }
  257. static void __exit poly1305_mod_exit(void)
  258. {
  259. crypto_unregister_shash(&poly1305_alg);
  260. }
  261. module_init(poly1305_mod_init);
  262. module_exit(poly1305_mod_exit);
  263. MODULE_LICENSE("GPL");
  264. MODULE_AUTHOR("Martin Willi <martin@strongswan.org>");
  265. MODULE_DESCRIPTION("Poly1305 authenticator");
  266. MODULE_ALIAS_CRYPTO("poly1305");
  267. MODULE_ALIAS_CRYPTO("poly1305-generic");