poly1305_generic.c 7.8 KB

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