mirror of
https://mirrors.bfsu.edu.cn/git/linux.git
synced 2024-11-16 08:44:21 +08:00
b1ccc8f4b6
Extends the x86_64 Poly1305 authenticator by a function processing four consecutive Poly1305 blocks in parallel using AVX2 instructions. For large messages, throughput increases by ~15-45% compared to two block SSE2: testing speed of poly1305 (poly1305-simd) test 0 ( 96 byte blocks, 16 bytes per update, 6 updates): 3809514 opers/sec, 365713411 bytes/sec test 1 ( 96 byte blocks, 32 bytes per update, 3 updates): 5973423 opers/sec, 573448627 bytes/sec test 2 ( 96 byte blocks, 96 bytes per update, 1 updates): 9446779 opers/sec, 906890803 bytes/sec test 3 ( 288 byte blocks, 16 bytes per update, 18 updates): 1364814 opers/sec, 393066691 bytes/sec test 4 ( 288 byte blocks, 32 bytes per update, 9 updates): 2045780 opers/sec, 589184697 bytes/sec test 5 ( 288 byte blocks, 288 bytes per update, 1 updates): 3711946 opers/sec, 1069040592 bytes/sec test 6 ( 1056 byte blocks, 32 bytes per update, 33 updates): 573686 opers/sec, 605812732 bytes/sec test 7 ( 1056 byte blocks, 1056 bytes per update, 1 updates): 1647802 opers/sec, 1740079440 bytes/sec test 8 ( 2080 byte blocks, 32 bytes per update, 65 updates): 292970 opers/sec, 609378224 bytes/sec test 9 ( 2080 byte blocks, 2080 bytes per update, 1 updates): 943229 opers/sec, 1961916528 bytes/sec test 10 ( 4128 byte blocks, 4128 bytes per update, 1 updates): 494623 opers/sec, 2041804569 bytes/sec test 11 ( 8224 byte blocks, 8224 bytes per update, 1 updates): 254045 opers/sec, 2089271014 bytes/sec testing speed of poly1305 (poly1305-simd) test 0 ( 96 byte blocks, 16 bytes per update, 6 updates): 3826224 opers/sec, 367317552 bytes/sec test 1 ( 96 byte blocks, 32 bytes per update, 3 updates): 5948638 opers/sec, 571069267 bytes/sec test 2 ( 96 byte blocks, 96 bytes per update, 1 updates): 9439110 opers/sec, 906154627 bytes/sec test 3 ( 288 byte blocks, 16 bytes per update, 18 updates): 1367756 opers/sec, 393913872 bytes/sec test 4 ( 288 byte blocks, 32 bytes per update, 9 updates): 2056881 opers/sec, 592381958 bytes/sec test 5 ( 288 byte blocks, 288 bytes per update, 1 updates): 3711153 opers/sec, 1068812179 bytes/sec test 6 ( 1056 byte blocks, 32 bytes per update, 33 updates): 574940 opers/sec, 607136745 bytes/sec test 7 ( 1056 byte blocks, 1056 bytes per update, 1 updates): 1948830 opers/sec, 2057964585 bytes/sec test 8 ( 2080 byte blocks, 32 bytes per update, 65 updates): 293308 opers/sec, 610082096 bytes/sec test 9 ( 2080 byte blocks, 2080 bytes per update, 1 updates): 1235224 opers/sec, 2569267792 bytes/sec test 10 ( 4128 byte blocks, 4128 bytes per update, 1 updates): 684405 opers/sec, 2825226316 bytes/sec test 11 ( 8224 byte blocks, 8224 bytes per update, 1 updates): 367101 opers/sec, 3019039446 bytes/sec Benchmark results from a Core i5-4670T. Signed-off-by: Martin Willi <martin@strongswan.org> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
208 lines
5.6 KiB
C
208 lines
5.6 KiB
C
/*
|
|
* Poly1305 authenticator algorithm, RFC7539, SIMD glue code
|
|
*
|
|
* Copyright (C) 2015 Martin Willi
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
* (at your option) any later version.
|
|
*/
|
|
|
|
#include <crypto/algapi.h>
|
|
#include <crypto/internal/hash.h>
|
|
#include <crypto/poly1305.h>
|
|
#include <linux/crypto.h>
|
|
#include <linux/kernel.h>
|
|
#include <linux/module.h>
|
|
#include <asm/fpu/api.h>
|
|
#include <asm/simd.h>
|
|
|
|
struct poly1305_simd_desc_ctx {
|
|
struct poly1305_desc_ctx base;
|
|
/* derived key u set? */
|
|
bool uset;
|
|
#ifdef CONFIG_AS_AVX2
|
|
/* derived keys r^3, r^4 set? */
|
|
bool wset;
|
|
#endif
|
|
/* derived Poly1305 key r^2 */
|
|
u32 u[5];
|
|
/* ... silently appended r^3 and r^4 when using AVX2 */
|
|
};
|
|
|
|
asmlinkage void poly1305_block_sse2(u32 *h, const u8 *src,
|
|
const u32 *r, unsigned int blocks);
|
|
asmlinkage void poly1305_2block_sse2(u32 *h, const u8 *src, const u32 *r,
|
|
unsigned int blocks, const u32 *u);
|
|
#ifdef CONFIG_AS_AVX2
|
|
asmlinkage void poly1305_4block_avx2(u32 *h, const u8 *src, const u32 *r,
|
|
unsigned int blocks, const u32 *u);
|
|
static bool poly1305_use_avx2;
|
|
#endif
|
|
|
|
static int poly1305_simd_init(struct shash_desc *desc)
|
|
{
|
|
struct poly1305_simd_desc_ctx *sctx = shash_desc_ctx(desc);
|
|
|
|
sctx->uset = false;
|
|
#ifdef CONFIG_AS_AVX2
|
|
sctx->wset = false;
|
|
#endif
|
|
|
|
return crypto_poly1305_init(desc);
|
|
}
|
|
|
|
static void poly1305_simd_mult(u32 *a, const u32 *b)
|
|
{
|
|
u8 m[POLY1305_BLOCK_SIZE];
|
|
|
|
memset(m, 0, sizeof(m));
|
|
/* The poly1305 block function adds a hi-bit to the accumulator which
|
|
* we don't need for key multiplication; compensate for it. */
|
|
a[4] -= 1 << 24;
|
|
poly1305_block_sse2(a, m, b, 1);
|
|
}
|
|
|
|
static unsigned int poly1305_simd_blocks(struct poly1305_desc_ctx *dctx,
|
|
const u8 *src, unsigned int srclen)
|
|
{
|
|
struct poly1305_simd_desc_ctx *sctx;
|
|
unsigned int blocks, datalen;
|
|
|
|
BUILD_BUG_ON(offsetof(struct poly1305_simd_desc_ctx, base));
|
|
sctx = container_of(dctx, struct poly1305_simd_desc_ctx, base);
|
|
|
|
if (unlikely(!dctx->sset)) {
|
|
datalen = crypto_poly1305_setdesckey(dctx, src, srclen);
|
|
src += srclen - datalen;
|
|
srclen = datalen;
|
|
}
|
|
|
|
#ifdef CONFIG_AS_AVX2
|
|
if (poly1305_use_avx2 && srclen >= POLY1305_BLOCK_SIZE * 4) {
|
|
if (unlikely(!sctx->wset)) {
|
|
if (!sctx->uset) {
|
|
memcpy(sctx->u, dctx->r, sizeof(sctx->u));
|
|
poly1305_simd_mult(sctx->u, dctx->r);
|
|
sctx->uset = true;
|
|
}
|
|
memcpy(sctx->u + 5, sctx->u, sizeof(sctx->u));
|
|
poly1305_simd_mult(sctx->u + 5, dctx->r);
|
|
memcpy(sctx->u + 10, sctx->u + 5, sizeof(sctx->u));
|
|
poly1305_simd_mult(sctx->u + 10, dctx->r);
|
|
sctx->wset = true;
|
|
}
|
|
blocks = srclen / (POLY1305_BLOCK_SIZE * 4);
|
|
poly1305_4block_avx2(dctx->h, src, dctx->r, blocks, sctx->u);
|
|
src += POLY1305_BLOCK_SIZE * 4 * blocks;
|
|
srclen -= POLY1305_BLOCK_SIZE * 4 * blocks;
|
|
}
|
|
#endif
|
|
if (likely(srclen >= POLY1305_BLOCK_SIZE * 2)) {
|
|
if (unlikely(!sctx->uset)) {
|
|
memcpy(sctx->u, dctx->r, sizeof(sctx->u));
|
|
poly1305_simd_mult(sctx->u, dctx->r);
|
|
sctx->uset = true;
|
|
}
|
|
blocks = srclen / (POLY1305_BLOCK_SIZE * 2);
|
|
poly1305_2block_sse2(dctx->h, src, dctx->r, blocks, sctx->u);
|
|
src += POLY1305_BLOCK_SIZE * 2 * blocks;
|
|
srclen -= POLY1305_BLOCK_SIZE * 2 * blocks;
|
|
}
|
|
if (srclen >= POLY1305_BLOCK_SIZE) {
|
|
poly1305_block_sse2(dctx->h, src, dctx->r, 1);
|
|
srclen -= POLY1305_BLOCK_SIZE;
|
|
}
|
|
return srclen;
|
|
}
|
|
|
|
static int poly1305_simd_update(struct shash_desc *desc,
|
|
const u8 *src, unsigned int srclen)
|
|
{
|
|
struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc);
|
|
unsigned int bytes;
|
|
|
|
/* kernel_fpu_begin/end is costly, use fallback for small updates */
|
|
if (srclen <= 288 || !may_use_simd())
|
|
return crypto_poly1305_update(desc, src, srclen);
|
|
|
|
kernel_fpu_begin();
|
|
|
|
if (unlikely(dctx->buflen)) {
|
|
bytes = min(srclen, POLY1305_BLOCK_SIZE - dctx->buflen);
|
|
memcpy(dctx->buf + dctx->buflen, src, bytes);
|
|
src += bytes;
|
|
srclen -= bytes;
|
|
dctx->buflen += bytes;
|
|
|
|
if (dctx->buflen == POLY1305_BLOCK_SIZE) {
|
|
poly1305_simd_blocks(dctx, dctx->buf,
|
|
POLY1305_BLOCK_SIZE);
|
|
dctx->buflen = 0;
|
|
}
|
|
}
|
|
|
|
if (likely(srclen >= POLY1305_BLOCK_SIZE)) {
|
|
bytes = poly1305_simd_blocks(dctx, src, srclen);
|
|
src += srclen - bytes;
|
|
srclen = bytes;
|
|
}
|
|
|
|
kernel_fpu_end();
|
|
|
|
if (unlikely(srclen)) {
|
|
dctx->buflen = srclen;
|
|
memcpy(dctx->buf, src, srclen);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static struct shash_alg alg = {
|
|
.digestsize = POLY1305_DIGEST_SIZE,
|
|
.init = poly1305_simd_init,
|
|
.update = poly1305_simd_update,
|
|
.final = crypto_poly1305_final,
|
|
.setkey = crypto_poly1305_setkey,
|
|
.descsize = sizeof(struct poly1305_simd_desc_ctx),
|
|
.base = {
|
|
.cra_name = "poly1305",
|
|
.cra_driver_name = "poly1305-simd",
|
|
.cra_priority = 300,
|
|
.cra_flags = CRYPTO_ALG_TYPE_SHASH,
|
|
.cra_alignmask = sizeof(u32) - 1,
|
|
.cra_blocksize = POLY1305_BLOCK_SIZE,
|
|
.cra_module = THIS_MODULE,
|
|
},
|
|
};
|
|
|
|
static int __init poly1305_simd_mod_init(void)
|
|
{
|
|
if (!cpu_has_xmm2)
|
|
return -ENODEV;
|
|
|
|
#ifdef CONFIG_AS_AVX2
|
|
poly1305_use_avx2 = cpu_has_avx && cpu_has_avx2 &&
|
|
cpu_has_xfeatures(XSTATE_SSE | XSTATE_YMM, NULL);
|
|
alg.descsize = sizeof(struct poly1305_simd_desc_ctx);
|
|
if (poly1305_use_avx2)
|
|
alg.descsize += 10 * sizeof(u32);
|
|
#endif
|
|
return crypto_register_shash(&alg);
|
|
}
|
|
|
|
static void __exit poly1305_simd_mod_exit(void)
|
|
{
|
|
crypto_unregister_shash(&alg);
|
|
}
|
|
|
|
module_init(poly1305_simd_mod_init);
|
|
module_exit(poly1305_simd_mod_exit);
|
|
|
|
MODULE_LICENSE("GPL");
|
|
MODULE_AUTHOR("Martin Willi <martin@strongswan.org>");
|
|
MODULE_DESCRIPTION("Poly1305 authenticator");
|
|
MODULE_ALIAS_CRYPTO("poly1305");
|
|
MODULE_ALIAS_CRYPTO("poly1305-simd");
|