Skip to content

Commit ad8f5b8

Browse files
ardbiesheuvelherbertx
authored andcommitted
crypto: x86/poly1305 - unify Poly1305 state struct with generic code
In preparation of exposing a Poly1305 library interface directly from the accelerated x86 driver, align the state descriptor of the x86 code with the one used by the generic driver. This is needed to make the library interface unified between all implementations. Signed-off-by: Ard Biesheuvel <ardb@kernel.org> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 48ea8c6 commit ad8f5b8

4 files changed

Lines changed: 43 additions & 73 deletions

File tree

‎arch/x86/crypto/poly1305_glue.c‎

Lines changed: 29 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -14,40 +14,14 @@
1414
#include <linux/module.h>
1515
#include <asm/simd.h>
1616

17-
struct poly1305_simd_desc_ctx {
18-
struct poly1305_desc_ctx base;
19-
/* derived key u set? */
20-
bool uset;
21-
#ifdef CONFIG_AS_AVX2
22-
/* derived keys r^3, r^4 set? */
23-
bool wset;
24-
#endif
25-
/* derived Poly1305 key r^2 */
26-
u32 u[5];
27-
/* ... silently appended r^3 and r^4 when using AVX2 */
28-
};
29-
3017
asmlinkage void poly1305_block_sse2(u32 *h, const u8 *src,
3118
const u32 *r, unsigned int blocks);
3219
asmlinkage void poly1305_2block_sse2(u32 *h, const u8 *src, const u32 *r,
3320
unsigned int blocks, const u32 *u);
34-
#ifdef CONFIG_AS_AVX2
3521
asmlinkage void poly1305_4block_avx2(u32 *h, const u8 *src, const u32 *r,
3622
unsigned int blocks, const u32 *u);
37-
static bool poly1305_use_avx2;
38-
#endif
3923

40-
static int poly1305_simd_init(struct shash_desc *desc)
41-
{
42-
struct poly1305_simd_desc_ctx *sctx = shash_desc_ctx(desc);
43-
44-
sctx->uset = false;
45-
#ifdef CONFIG_AS_AVX2
46-
sctx->wset = false;
47-
#endif
48-
49-
return crypto_poly1305_init(desc);
50-
}
24+
static bool poly1305_use_avx2 __ro_after_init;
5125

5226
static void poly1305_simd_mult(u32 *a, const u32 *b)
5327
{
@@ -63,53 +37,49 @@ static void poly1305_simd_mult(u32 *a, const u32 *b)
6337
static unsigned int poly1305_simd_blocks(struct poly1305_desc_ctx *dctx,
6438
const u8 *src, unsigned int srclen)
6539
{
66-
struct poly1305_simd_desc_ctx *sctx;
6740
unsigned int blocks, datalen;
6841

69-
BUILD_BUG_ON(offsetof(struct poly1305_simd_desc_ctx, base));
70-
sctx = container_of(dctx, struct poly1305_simd_desc_ctx, base);
71-
7242
if (unlikely(!dctx->sset)) {
7343
datalen = crypto_poly1305_setdesckey(dctx, src, srclen);
7444
src += srclen - datalen;
7545
srclen = datalen;
7646
}
7747

78-
#ifdef CONFIG_AS_AVX2
79-
if (poly1305_use_avx2 && srclen >= POLY1305_BLOCK_SIZE * 4) {
80-
if (unlikely(!sctx->wset)) {
81-
if (!sctx->uset) {
82-
memcpy(sctx->u, dctx->r.r, sizeof(sctx->u));
83-
poly1305_simd_mult(sctx->u, dctx->r.r);
84-
sctx->uset = true;
48+
if (IS_ENABLED(CONFIG_AS_AVX2) &&
49+
poly1305_use_avx2 &&
50+
srclen >= POLY1305_BLOCK_SIZE * 4) {
51+
if (unlikely(dctx->rset < 4)) {
52+
if (dctx->rset < 2) {
53+
dctx->r[1] = dctx->r[0];
54+
poly1305_simd_mult(dctx->r[1].r, dctx->r[0].r);
8555
}
86-
memcpy(sctx->u + 5, sctx->u, sizeof(sctx->u));
87-
poly1305_simd_mult(sctx->u + 5, dctx->r.r);
88-
memcpy(sctx->u + 10, sctx->u + 5, sizeof(sctx->u));
89-
poly1305_simd_mult(sctx->u + 10, dctx->r.r);
90-
sctx->wset = true;
56+
dctx->r[2] = dctx->r[1];
57+
poly1305_simd_mult(dctx->r[2].r, dctx->r[0].r);
58+
dctx->r[3] = dctx->r[2];
59+
poly1305_simd_mult(dctx->r[3].r, dctx->r[0].r);
60+
dctx->rset = 4;
9161
}
9262
blocks = srclen / (POLY1305_BLOCK_SIZE * 4);
93-
poly1305_4block_avx2(dctx->h.h, src, dctx->r.r, blocks,
94-
sctx->u);
63+
poly1305_4block_avx2(dctx->h.h, src, dctx->r[0].r, blocks,
64+
dctx->r[1].r);
9565
src += POLY1305_BLOCK_SIZE * 4 * blocks;
9666
srclen -= POLY1305_BLOCK_SIZE * 4 * blocks;
9767
}
98-
#endif
68+
9969
if (likely(srclen >= POLY1305_BLOCK_SIZE * 2)) {
100-
if (unlikely(!sctx->uset)) {
101-
memcpy(sctx->u, dctx->r.r, sizeof(sctx->u));
102-
poly1305_simd_mult(sctx->u, dctx->r.r);
103-
sctx->uset = true;
70+
if (unlikely(dctx->rset < 2)) {
71+
dctx->r[1] = dctx->r[0];
72+
poly1305_simd_mult(dctx->r[1].r, dctx->r[0].r);
73+
dctx->rset = 2;
10474
}
10575
blocks = srclen / (POLY1305_BLOCK_SIZE * 2);
106-
poly1305_2block_sse2(dctx->h.h, src, dctx->r.r, blocks,
107-
sctx->u);
76+
poly1305_2block_sse2(dctx->h.h, src, dctx->r[0].r,
77+
blocks, dctx->r[1].r);
10878
src += POLY1305_BLOCK_SIZE * 2 * blocks;
10979
srclen -= POLY1305_BLOCK_SIZE * 2 * blocks;
11080
}
11181
if (srclen >= POLY1305_BLOCK_SIZE) {
112-
poly1305_block_sse2(dctx->h.h, src, dctx->r.r, 1);
82+
poly1305_block_sse2(dctx->h.h, src, dctx->r[0].r, 1);
11383
srclen -= POLY1305_BLOCK_SIZE;
11484
}
11585
return srclen;
@@ -159,10 +129,10 @@ static int poly1305_simd_update(struct shash_desc *desc,
159129

160130
static struct shash_alg alg = {
161131
.digestsize = POLY1305_DIGEST_SIZE,
162-
.init = poly1305_simd_init,
132+
.init = crypto_poly1305_init,
163133
.update = poly1305_simd_update,
164134
.final = crypto_poly1305_final,
165-
.descsize = sizeof(struct poly1305_simd_desc_ctx),
135+
.descsize = sizeof(struct poly1305_desc_ctx),
166136
.base = {
167137
.cra_name = "poly1305",
168138
.cra_driver_name = "poly1305-simd",
@@ -177,14 +147,14 @@ static int __init poly1305_simd_mod_init(void)
177147
if (!boot_cpu_has(X86_FEATURE_XMM2))
178148
return -ENODEV;
179149

180-
#ifdef CONFIG_AS_AVX2
181-
poly1305_use_avx2 = boot_cpu_has(X86_FEATURE_AVX) &&
150+
poly1305_use_avx2 = IS_ENABLED(CONFIG_AS_AVX2) &&
151+
boot_cpu_has(X86_FEATURE_AVX) &&
182152
boot_cpu_has(X86_FEATURE_AVX2) &&
183153
cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM, NULL);
184-
alg.descsize = sizeof(struct poly1305_simd_desc_ctx);
154+
alg.descsize = sizeof(struct poly1305_desc_ctx) + 5 * sizeof(u32);
185155
if (poly1305_use_avx2)
186156
alg.descsize += 10 * sizeof(u32);
187-
#endif
157+
188158
return crypto_register_shash(&alg);
189159
}
190160

‎crypto/poly1305_generic.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ int crypto_poly1305_init(struct shash_desc *desc)
2525

2626
poly1305_core_init(&dctx->h);
2727
dctx->buflen = 0;
28-
dctx->rset = false;
28+
dctx->rset = 0;
2929
dctx->sset = false;
3030

3131
return 0;
@@ -43,7 +43,7 @@ static void poly1305_blocks(struct poly1305_desc_ctx *dctx, const u8 *src,
4343
srclen = datalen;
4444
}
4545

46-
poly1305_core_blocks(&dctx->h, &dctx->r, src,
46+
poly1305_core_blocks(&dctx->h, dctx->r, src,
4747
srclen / POLY1305_BLOCK_SIZE, 1);
4848
}
4949

@@ -95,7 +95,7 @@ int crypto_poly1305_final(struct shash_desc *desc, u8 *dst)
9595
dctx->buf[dctx->buflen++] = 1;
9696
memset(dctx->buf + dctx->buflen, 0,
9797
POLY1305_BLOCK_SIZE - dctx->buflen);
98-
poly1305_core_blocks(&dctx->h, &dctx->r, dctx->buf, 1, 0);
98+
poly1305_core_blocks(&dctx->h, dctx->r, dctx->buf, 1, 0);
9999
}
100100

101101
poly1305_core_emit(&dctx->h, digest);

‎include/crypto/internal/poly1305.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ unsigned int crypto_poly1305_setdesckey(struct poly1305_desc_ctx *dctx,
4646
{
4747
if (!dctx->sset) {
4848
if (!dctx->rset && srclen >= POLY1305_BLOCK_SIZE) {
49-
poly1305_core_setkey(&dctx->r, src);
49+
poly1305_core_setkey(dctx->r, src);
5050
src += POLY1305_BLOCK_SIZE;
5151
srclen -= POLY1305_BLOCK_SIZE;
52-
dctx->rset = true;
52+
dctx->rset = 1;
5353
}
5454
if (srclen >= POLY1305_BLOCK_SIZE) {
5555
dctx->s[0] = get_unaligned_le32(src + 0);

‎include/crypto/poly1305.h‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,20 @@ struct poly1305_state {
2222
};
2323

2424
struct poly1305_desc_ctx {
25-
/* key */
26-
struct poly1305_key r;
27-
/* finalize key */
28-
u32 s[4];
29-
/* accumulator */
30-
struct poly1305_state h;
3125
/* partial buffer */
3226
u8 buf[POLY1305_BLOCK_SIZE];
3327
/* bytes used in partial buffer */
3428
unsigned int buflen;
35-
/* r key has been set */
36-
bool rset;
37-
/* s key has been set */
29+
/* how many keys have been set in r[] */
30+
unsigned short rset;
31+
/* whether s[] has been set */
3832
bool sset;
33+
/* finalize key */
34+
u32 s[4];
35+
/* accumulator */
36+
struct poly1305_state h;
37+
/* key */
38+
struct poly1305_key r[1];
3939
};
4040

4141
#endif

0 commit comments

Comments
 (0)