blob: a69670b5b849c51f913094f1a9ef379697f93233 [file] [log] [blame]
Martin Willic70f4ab2015-07-16 19:14:06 +02001/*
2 * Poly1305 authenticator algorithm, RFC7539, SIMD glue code
3 *
4 * Copyright (C) 2015 Martin Willi
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 */
11
12#include <crypto/algapi.h>
13#include <crypto/internal/hash.h>
14#include <crypto/poly1305.h>
15#include <linux/crypto.h>
16#include <linux/kernel.h>
17#include <linux/module.h>
18#include <asm/fpu/api.h>
19#include <asm/simd.h>
20
Martin Willida35b222015-07-16 19:14:07 +020021struct poly1305_simd_desc_ctx {
22 struct poly1305_desc_ctx base;
23 /* derived key u set? */
24 bool uset;
Martin Willib1ccc8f2015-07-16 19:14:08 +020025#ifdef CONFIG_AS_AVX2
26 /* derived keys r^3, r^4 set? */
27 bool wset;
28#endif
Martin Willida35b222015-07-16 19:14:07 +020029 /* derived Poly1305 key r^2 */
30 u32 u[5];
Martin Willib1ccc8f2015-07-16 19:14:08 +020031 /* ... silently appended r^3 and r^4 when using AVX2 */
Martin Willida35b222015-07-16 19:14:07 +020032};
33
Martin Willic70f4ab2015-07-16 19:14:06 +020034asmlinkage void poly1305_block_sse2(u32 *h, const u8 *src,
35 const u32 *r, unsigned int blocks);
Martin Willida35b222015-07-16 19:14:07 +020036asmlinkage void poly1305_2block_sse2(u32 *h, const u8 *src, const u32 *r,
37 unsigned int blocks, const u32 *u);
Martin Willib1ccc8f2015-07-16 19:14:08 +020038#ifdef CONFIG_AS_AVX2
39asmlinkage void poly1305_4block_avx2(u32 *h, const u8 *src, const u32 *r,
40 unsigned int blocks, const u32 *u);
41static bool poly1305_use_avx2;
42#endif
Martin Willida35b222015-07-16 19:14:07 +020043
44static int poly1305_simd_init(struct shash_desc *desc)
45{
46 struct poly1305_simd_desc_ctx *sctx = shash_desc_ctx(desc);
47
48 sctx->uset = false;
Martin Willib1ccc8f2015-07-16 19:14:08 +020049#ifdef CONFIG_AS_AVX2
50 sctx->wset = false;
51#endif
Martin Willida35b222015-07-16 19:14:07 +020052
53 return crypto_poly1305_init(desc);
54}
55
56static void poly1305_simd_mult(u32 *a, const u32 *b)
57{
58 u8 m[POLY1305_BLOCK_SIZE];
59
60 memset(m, 0, sizeof(m));
61 /* The poly1305 block function adds a hi-bit to the accumulator which
62 * we don't need for key multiplication; compensate for it. */
63 a[4] -= 1 << 24;
64 poly1305_block_sse2(a, m, b, 1);
65}
Martin Willic70f4ab2015-07-16 19:14:06 +020066
67static unsigned int poly1305_simd_blocks(struct poly1305_desc_ctx *dctx,
68 const u8 *src, unsigned int srclen)
69{
Martin Willida35b222015-07-16 19:14:07 +020070 struct poly1305_simd_desc_ctx *sctx;
Martin Willic70f4ab2015-07-16 19:14:06 +020071 unsigned int blocks, datalen;
72
Martin Willida35b222015-07-16 19:14:07 +020073 BUILD_BUG_ON(offsetof(struct poly1305_simd_desc_ctx, base));
74 sctx = container_of(dctx, struct poly1305_simd_desc_ctx, base);
75
Martin Willic70f4ab2015-07-16 19:14:06 +020076 if (unlikely(!dctx->sset)) {
77 datalen = crypto_poly1305_setdesckey(dctx, src, srclen);
78 src += srclen - datalen;
79 srclen = datalen;
80 }
81
Martin Willib1ccc8f2015-07-16 19:14:08 +020082#ifdef CONFIG_AS_AVX2
83 if (poly1305_use_avx2 && srclen >= POLY1305_BLOCK_SIZE * 4) {
84 if (unlikely(!sctx->wset)) {
85 if (!sctx->uset) {
Eric Biggers888679d2018-11-16 17:26:27 -080086 memcpy(sctx->u, dctx->r.r, sizeof(sctx->u));
87 poly1305_simd_mult(sctx->u, dctx->r.r);
Martin Willib1ccc8f2015-07-16 19:14:08 +020088 sctx->uset = true;
89 }
90 memcpy(sctx->u + 5, sctx->u, sizeof(sctx->u));
Eric Biggers888679d2018-11-16 17:26:27 -080091 poly1305_simd_mult(sctx->u + 5, dctx->r.r);
Martin Willib1ccc8f2015-07-16 19:14:08 +020092 memcpy(sctx->u + 10, sctx->u + 5, sizeof(sctx->u));
Eric Biggers888679d2018-11-16 17:26:27 -080093 poly1305_simd_mult(sctx->u + 10, dctx->r.r);
Martin Willib1ccc8f2015-07-16 19:14:08 +020094 sctx->wset = true;
95 }
96 blocks = srclen / (POLY1305_BLOCK_SIZE * 4);
Eric Biggers888679d2018-11-16 17:26:27 -080097 poly1305_4block_avx2(dctx->h.h, src, dctx->r.r, blocks,
98 sctx->u);
Martin Willib1ccc8f2015-07-16 19:14:08 +020099 src += POLY1305_BLOCK_SIZE * 4 * blocks;
100 srclen -= POLY1305_BLOCK_SIZE * 4 * blocks;
101 }
102#endif
Martin Willida35b222015-07-16 19:14:07 +0200103 if (likely(srclen >= POLY1305_BLOCK_SIZE * 2)) {
104 if (unlikely(!sctx->uset)) {
Eric Biggers888679d2018-11-16 17:26:27 -0800105 memcpy(sctx->u, dctx->r.r, sizeof(sctx->u));
106 poly1305_simd_mult(sctx->u, dctx->r.r);
Martin Willida35b222015-07-16 19:14:07 +0200107 sctx->uset = true;
108 }
109 blocks = srclen / (POLY1305_BLOCK_SIZE * 2);
Eric Biggers888679d2018-11-16 17:26:27 -0800110 poly1305_2block_sse2(dctx->h.h, src, dctx->r.r, blocks,
111 sctx->u);
Martin Willida35b222015-07-16 19:14:07 +0200112 src += POLY1305_BLOCK_SIZE * 2 * blocks;
113 srclen -= POLY1305_BLOCK_SIZE * 2 * blocks;
114 }
Martin Willic70f4ab2015-07-16 19:14:06 +0200115 if (srclen >= POLY1305_BLOCK_SIZE) {
Eric Biggers888679d2018-11-16 17:26:27 -0800116 poly1305_block_sse2(dctx->h.h, src, dctx->r.r, 1);
Martin Willida35b222015-07-16 19:14:07 +0200117 srclen -= POLY1305_BLOCK_SIZE;
Martin Willic70f4ab2015-07-16 19:14:06 +0200118 }
119 return srclen;
120}
121
122static int poly1305_simd_update(struct shash_desc *desc,
123 const u8 *src, unsigned int srclen)
124{
125 struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc);
126 unsigned int bytes;
127
128 /* kernel_fpu_begin/end is costly, use fallback for small updates */
129 if (srclen <= 288 || !may_use_simd())
130 return crypto_poly1305_update(desc, src, srclen);
131
132 kernel_fpu_begin();
133
134 if (unlikely(dctx->buflen)) {
135 bytes = min(srclen, POLY1305_BLOCK_SIZE - dctx->buflen);
136 memcpy(dctx->buf + dctx->buflen, src, bytes);
137 src += bytes;
138 srclen -= bytes;
139 dctx->buflen += bytes;
140
141 if (dctx->buflen == POLY1305_BLOCK_SIZE) {
142 poly1305_simd_blocks(dctx, dctx->buf,
143 POLY1305_BLOCK_SIZE);
144 dctx->buflen = 0;
145 }
146 }
147
148 if (likely(srclen >= POLY1305_BLOCK_SIZE)) {
149 bytes = poly1305_simd_blocks(dctx, src, srclen);
150 src += srclen - bytes;
151 srclen = bytes;
152 }
153
154 kernel_fpu_end();
155
156 if (unlikely(srclen)) {
157 dctx->buflen = srclen;
158 memcpy(dctx->buf, src, srclen);
159 }
160
161 return 0;
162}
163
164static struct shash_alg alg = {
165 .digestsize = POLY1305_DIGEST_SIZE,
Martin Willida35b222015-07-16 19:14:07 +0200166 .init = poly1305_simd_init,
Martin Willic70f4ab2015-07-16 19:14:06 +0200167 .update = poly1305_simd_update,
168 .final = crypto_poly1305_final,
Martin Willida35b222015-07-16 19:14:07 +0200169 .descsize = sizeof(struct poly1305_simd_desc_ctx),
Martin Willic70f4ab2015-07-16 19:14:06 +0200170 .base = {
171 .cra_name = "poly1305",
172 .cra_driver_name = "poly1305-simd",
173 .cra_priority = 300,
174 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
175 .cra_alignmask = sizeof(u32) - 1,
176 .cra_blocksize = POLY1305_BLOCK_SIZE,
177 .cra_module = THIS_MODULE,
178 },
179};
180
181static int __init poly1305_simd_mod_init(void)
182{
Borislav Petkov054efb62016-03-29 17:42:00 +0200183 if (!boot_cpu_has(X86_FEATURE_XMM2))
Martin Willic70f4ab2015-07-16 19:14:06 +0200184 return -ENODEV;
185
Martin Willib1ccc8f2015-07-16 19:14:08 +0200186#ifdef CONFIG_AS_AVX2
Borislav Petkovda154e82016-04-04 22:24:56 +0200187 poly1305_use_avx2 = boot_cpu_has(X86_FEATURE_AVX) &&
188 boot_cpu_has(X86_FEATURE_AVX2) &&
Dave Hansend91cab72015-09-02 16:31:26 -0700189 cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM, NULL);
Martin Willib1ccc8f2015-07-16 19:14:08 +0200190 alg.descsize = sizeof(struct poly1305_simd_desc_ctx);
191 if (poly1305_use_avx2)
192 alg.descsize += 10 * sizeof(u32);
193#endif
Martin Willic70f4ab2015-07-16 19:14:06 +0200194 return crypto_register_shash(&alg);
195}
196
197static void __exit poly1305_simd_mod_exit(void)
198{
199 crypto_unregister_shash(&alg);
200}
201
202module_init(poly1305_simd_mod_init);
203module_exit(poly1305_simd_mod_exit);
204
205MODULE_LICENSE("GPL");
206MODULE_AUTHOR("Martin Willi <martin@strongswan.org>");
207MODULE_DESCRIPTION("Poly1305 authenticator");
208MODULE_ALIAS_CRYPTO("poly1305");
209MODULE_ALIAS_CRYPTO("poly1305-simd");