1 // SPDX-License-Identifier: GPL-2.0-only
3 * POLYVAL: hash function for HCTR2.
5 * Copyright (c) 2007 Nokia Siemens Networks - Mikko Herranen <mh1@iki.fi>
6 * Copyright (c) 2009 Intel Corp.
7 * Author: Huang Ying <ying.huang@intel.com>
8 * Copyright 2021 Google LLC
12 * Code based on crypto/ghash-generic.c
14 * POLYVAL is a keyed hash function similar to GHASH. POLYVAL uses a different
15 * modulus for finite field multiplication which makes hardware accelerated
16 * implementations on little-endian machines faster. POLYVAL is used in the
17 * kernel to implement HCTR2, but was originally specified for AES-GCM-SIV
20 * For more information see:
21 * Length-preserving encryption with HCTR2:
22 * https://eprint.iacr.org/2021/1441.pdf
23 * AES-GCM-SIV: Nonce Misuse-Resistant Authenticated Encryption:
24 * https://datatracker.ietf.org/doc/html/rfc8452
26 * Like GHASH, POLYVAL is not a cryptographic hash function and should
27 * not be used outside of crypto modes explicitly designed to use POLYVAL.
29 * This implementation uses a convenient trick involving the GHASH and POLYVAL
30 * fields. This trick allows multiplication in the POLYVAL field to be
31 * implemented by using multiplication in the GHASH field as a subroutine. An
32 * element of the POLYVAL field can be converted to an element of the GHASH
33 * field by computing x*REVERSE(a), where REVERSE reverses the byte-ordering of
34 * a. Similarly, an element of the GHASH field can be converted back to the
35 * POLYVAL field by computing REVERSE(x^{-1}*a). For more information, see:
36 * https://datatracker.ietf.org/doc/html/rfc8452#appendix-A
38 * By using this trick, we do not need to implement the POLYVAL field for the
39 * generic implementation.
41 * Warning: this generic implementation is not intended to be used in practice
42 * and is not constant time. For practical use, a hardware accelerated
43 * implementation of POLYVAL should be used instead.
47 #include <asm/unaligned.h>
48 #include <crypto/algapi.h>
49 #include <crypto/gf128mul.h>
50 #include <crypto/polyval.h>
51 #include <crypto/internal/hash.h>
52 #include <linux/crypto.h>
53 #include <linux/init.h>
54 #include <linux/kernel.h>
55 #include <linux/module.h>
57 struct polyval_tfm_ctx {
58 struct gf128mul_4k *gf128;
61 struct polyval_desc_ctx {
63 u8 buffer[POLYVAL_BLOCK_SIZE];
69 static void copy_and_reverse(u8 dst[POLYVAL_BLOCK_SIZE],
70 const u8 src[POLYVAL_BLOCK_SIZE])
72 u64 a = get_unaligned((const u64 *)&src[0]);
73 u64 b = get_unaligned((const u64 *)&src[8]);
75 put_unaligned(swab64(a), (u64 *)&dst[8]);
76 put_unaligned(swab64(b), (u64 *)&dst[0]);
80 * Performs multiplication in the POLYVAL field using the GHASH field as a
81 * subroutine. This function is used as a fallback for hardware accelerated
82 * implementations when simd registers are unavailable.
84 * Note: This function is not used for polyval-generic, instead we use the 4k
85 * lookup table implementation for finite field multiplication.
87 void polyval_mul_non4k(u8 *op1, const u8 *op2)
91 // Assume one argument is in Montgomery form and one is not.
92 copy_and_reverse((u8 *)&a, op1);
93 copy_and_reverse((u8 *)&b, op2);
94 gf128mul_x_lle(&a, &a);
96 copy_and_reverse(op1, (u8 *)&a);
98 EXPORT_SYMBOL_GPL(polyval_mul_non4k);
101 * Perform a POLYVAL update using non4k multiplication. This function is used
102 * as a fallback for hardware accelerated implementations when simd registers
105 * Note: This function is not used for polyval-generic, instead we use the 4k
106 * lookup table implementation of finite field multiplication.
108 void polyval_update_non4k(const u8 *key, const u8 *in,
109 size_t nblocks, u8 *accumulator)
112 crypto_xor(accumulator, in, POLYVAL_BLOCK_SIZE);
113 polyval_mul_non4k(accumulator, key);
114 in += POLYVAL_BLOCK_SIZE;
117 EXPORT_SYMBOL_GPL(polyval_update_non4k);
119 static int polyval_setkey(struct crypto_shash *tfm,
120 const u8 *key, unsigned int keylen)
122 struct polyval_tfm_ctx *ctx = crypto_shash_ctx(tfm);
125 if (keylen != POLYVAL_BLOCK_SIZE)
128 gf128mul_free_4k(ctx->gf128);
130 BUILD_BUG_ON(sizeof(k) != POLYVAL_BLOCK_SIZE);
131 copy_and_reverse((u8 *)&k, key);
132 gf128mul_x_lle(&k, &k);
134 ctx->gf128 = gf128mul_init_4k_lle(&k);
135 memzero_explicit(&k, POLYVAL_BLOCK_SIZE);
143 static int polyval_init(struct shash_desc *desc)
145 struct polyval_desc_ctx *dctx = shash_desc_ctx(desc);
147 memset(dctx, 0, sizeof(*dctx));
152 static int polyval_update(struct shash_desc *desc,
153 const u8 *src, unsigned int srclen)
155 struct polyval_desc_ctx *dctx = shash_desc_ctx(desc);
156 const struct polyval_tfm_ctx *ctx = crypto_shash_ctx(desc->tfm);
158 u8 tmp[POLYVAL_BLOCK_SIZE];
162 n = min(srclen, dctx->bytes);
163 pos = dctx->buffer + dctx->bytes - 1;
172 gf128mul_4k_lle(&dctx->buffer128, ctx->gf128);
175 while (srclen >= POLYVAL_BLOCK_SIZE) {
176 copy_and_reverse(tmp, src);
177 crypto_xor(dctx->buffer, tmp, POLYVAL_BLOCK_SIZE);
178 gf128mul_4k_lle(&dctx->buffer128, ctx->gf128);
179 src += POLYVAL_BLOCK_SIZE;
180 srclen -= POLYVAL_BLOCK_SIZE;
184 dctx->bytes = POLYVAL_BLOCK_SIZE - srclen;
185 pos = dctx->buffer + POLYVAL_BLOCK_SIZE - 1;
193 static int polyval_final(struct shash_desc *desc, u8 *dst)
195 struct polyval_desc_ctx *dctx = shash_desc_ctx(desc);
196 const struct polyval_tfm_ctx *ctx = crypto_shash_ctx(desc->tfm);
199 gf128mul_4k_lle(&dctx->buffer128, ctx->gf128);
200 copy_and_reverse(dst, dctx->buffer);
204 static void polyval_exit_tfm(struct crypto_tfm *tfm)
206 struct polyval_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
208 gf128mul_free_4k(ctx->gf128);
211 static struct shash_alg polyval_alg = {
212 .digestsize = POLYVAL_DIGEST_SIZE,
213 .init = polyval_init,
214 .update = polyval_update,
215 .final = polyval_final,
216 .setkey = polyval_setkey,
217 .descsize = sizeof(struct polyval_desc_ctx),
219 .cra_name = "polyval",
220 .cra_driver_name = "polyval-generic",
222 .cra_blocksize = POLYVAL_BLOCK_SIZE,
223 .cra_ctxsize = sizeof(struct polyval_tfm_ctx),
224 .cra_module = THIS_MODULE,
225 .cra_exit = polyval_exit_tfm,
229 static int __init polyval_mod_init(void)
231 return crypto_register_shash(&polyval_alg);
234 static void __exit polyval_mod_exit(void)
236 crypto_unregister_shash(&polyval_alg);
239 subsys_initcall(polyval_mod_init);
240 module_exit(polyval_mod_exit);
242 MODULE_LICENSE("GPL");
243 MODULE_DESCRIPTION("POLYVAL hash function");
244 MODULE_ALIAS_CRYPTO("polyval");
245 MODULE_ALIAS_CRYPTO("polyval-generic");