poly1305_generic.c 8.0 KB

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