poly1305_generic.c 8.6 KB

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