poly1305_generic.c 8.0 KB

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