1 // SPDX-License-Identifier: GPL-2.0-only
3 * Glue code for POLYVAL using PCMULQDQ-NI
6 * Copyright (c) 2009 Intel Corp.
8 * Copyright 2021 Google LLC
12 * Glue code based on ghash-clmulni-intel_glue.c.
14 * This implementation of POLYVAL uses montgomery multiplication
15 * accelerated by PCLMULQDQ-NI to implement the finite field
19 #include <crypto/algapi.h>
20 #include <crypto/internal/hash.h>
21 #include <crypto/internal/simd.h>
22 #include <crypto/polyval.h>
23 #include <linux/crypto.h>
24 #include <linux/init.h>
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <asm/cpu_device_id.h>
30 #define NUM_KEY_POWERS 8
32 struct polyval_tfm_ctx {
34 * These powers must be in the order h^8, ..., h^1.
36 u8 key_powers[NUM_KEY_POWERS][POLYVAL_BLOCK_SIZE];
39 struct polyval_desc_ctx {
40 u8 buffer[POLYVAL_BLOCK_SIZE];
44 asmlinkage void clmul_polyval_update(const struct polyval_tfm_ctx *keys,
45 const u8 *in, size_t nblocks, u8 *accumulator);
46 asmlinkage void clmul_polyval_mul(u8 *op1, const u8 *op2);
48 static void internal_polyval_update(const struct polyval_tfm_ctx *keys,
49 const u8 *in, size_t nblocks, u8 *accumulator)
51 if (likely(crypto_simd_usable())) {
53 clmul_polyval_update(keys, in, nblocks, accumulator);
56 polyval_update_non4k(keys->key_powers[NUM_KEY_POWERS-1], in,
57 nblocks, accumulator);
61 static void internal_polyval_mul(u8 *op1, const u8 *op2)
63 if (likely(crypto_simd_usable())) {
65 clmul_polyval_mul(op1, op2);
68 polyval_mul_non4k(op1, op2);
72 static int polyval_x86_setkey(struct crypto_shash *tfm,
73 const u8 *key, unsigned int keylen)
75 struct polyval_tfm_ctx *tctx = crypto_shash_ctx(tfm);
78 if (keylen != POLYVAL_BLOCK_SIZE)
81 memcpy(tctx->key_powers[NUM_KEY_POWERS-1], key, POLYVAL_BLOCK_SIZE);
83 for (i = NUM_KEY_POWERS-2; i >= 0; i--) {
84 memcpy(tctx->key_powers[i], key, POLYVAL_BLOCK_SIZE);
85 internal_polyval_mul(tctx->key_powers[i],
86 tctx->key_powers[i+1]);
92 static int polyval_x86_init(struct shash_desc *desc)
94 struct polyval_desc_ctx *dctx = shash_desc_ctx(desc);
96 memset(dctx, 0, sizeof(*dctx));
101 static int polyval_x86_update(struct shash_desc *desc,
102 const u8 *src, unsigned int srclen)
104 struct polyval_desc_ctx *dctx = shash_desc_ctx(desc);
105 const struct polyval_tfm_ctx *tctx = crypto_shash_ctx(desc->tfm);
107 unsigned int nblocks;
111 n = min(srclen, dctx->bytes);
112 pos = dctx->buffer + POLYVAL_BLOCK_SIZE - dctx->bytes;
121 internal_polyval_mul(dctx->buffer,
122 tctx->key_powers[NUM_KEY_POWERS-1]);
125 while (srclen >= POLYVAL_BLOCK_SIZE) {
126 /* Allow rescheduling every 4K bytes. */
127 nblocks = min(srclen, 4096U) / POLYVAL_BLOCK_SIZE;
128 internal_polyval_update(tctx, src, nblocks, dctx->buffer);
129 srclen -= nblocks * POLYVAL_BLOCK_SIZE;
130 src += nblocks * POLYVAL_BLOCK_SIZE;
134 dctx->bytes = POLYVAL_BLOCK_SIZE - srclen;
143 static int polyval_x86_final(struct shash_desc *desc, u8 *dst)
145 struct polyval_desc_ctx *dctx = shash_desc_ctx(desc);
146 const struct polyval_tfm_ctx *tctx = crypto_shash_ctx(desc->tfm);
149 internal_polyval_mul(dctx->buffer,
150 tctx->key_powers[NUM_KEY_POWERS-1]);
153 memcpy(dst, dctx->buffer, POLYVAL_BLOCK_SIZE);
158 static struct shash_alg polyval_alg = {
159 .digestsize = POLYVAL_DIGEST_SIZE,
160 .init = polyval_x86_init,
161 .update = polyval_x86_update,
162 .final = polyval_x86_final,
163 .setkey = polyval_x86_setkey,
164 .descsize = sizeof(struct polyval_desc_ctx),
166 .cra_name = "polyval",
167 .cra_driver_name = "polyval-clmulni",
169 .cra_blocksize = POLYVAL_BLOCK_SIZE,
170 .cra_ctxsize = sizeof(struct polyval_tfm_ctx),
171 .cra_module = THIS_MODULE,
175 __maybe_unused static const struct x86_cpu_id pcmul_cpu_id[] = {
176 X86_MATCH_FEATURE(X86_FEATURE_PCLMULQDQ, NULL),
179 MODULE_DEVICE_TABLE(x86cpu, pcmul_cpu_id);
181 static int __init polyval_clmulni_mod_init(void)
183 if (!x86_match_cpu(pcmul_cpu_id))
186 if (!boot_cpu_has(X86_FEATURE_AVX))
189 return crypto_register_shash(&polyval_alg);
192 static void __exit polyval_clmulni_mod_exit(void)
194 crypto_unregister_shash(&polyval_alg);
197 module_init(polyval_clmulni_mod_init);
198 module_exit(polyval_clmulni_mod_exit);
200 MODULE_LICENSE("GPL");
201 MODULE_DESCRIPTION("POLYVAL hash function accelerated by PCLMULQDQ-NI");
202 MODULE_ALIAS_CRYPTO("polyval");
203 MODULE_ALIAS_CRYPTO("polyval-clmulni");