1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
22cdc6899SHuang Ying /*
38dfa20fcSEric Biggers * GHASH: hash function for GCM (Galois/Counter Mode).
42cdc6899SHuang Ying *
52cdc6899SHuang Ying * Copyright (c) 2007 Nokia Siemens Networks - Mikko Herranen <mh1@iki.fi>
62cdc6899SHuang Ying * Copyright (c) 2009 Intel Corp.
72cdc6899SHuang Ying * Author: Huang Ying <ying.huang@intel.com>
88dfa20fcSEric Biggers */
98dfa20fcSEric Biggers
108dfa20fcSEric Biggers /*
118dfa20fcSEric Biggers * GHASH is a keyed hash function used in GCM authentication tag generation.
122cdc6899SHuang Ying *
138dfa20fcSEric Biggers * The original GCM paper [1] presents GHASH as a function GHASH(H, A, C) which
148dfa20fcSEric Biggers * takes a 16-byte hash key H, additional authenticated data A, and a ciphertext
158dfa20fcSEric Biggers * C. It formats A and C into a single byte string X, interprets X as a
168dfa20fcSEric Biggers * polynomial over GF(2^128), and evaluates this polynomial at the point H.
178dfa20fcSEric Biggers *
188dfa20fcSEric Biggers * However, the NIST standard for GCM [2] presents GHASH as GHASH(H, X) where X
198dfa20fcSEric Biggers * is the already-formatted byte string containing both A and C.
208dfa20fcSEric Biggers *
218dfa20fcSEric Biggers * "ghash" in the Linux crypto API uses the 'X' (pre-formatted) convention,
228dfa20fcSEric Biggers * since the API supports only a single data stream per hash. Thus, the
238dfa20fcSEric Biggers * formatting of 'A' and 'C' is done in the "gcm" template, not in "ghash".
248dfa20fcSEric Biggers *
258dfa20fcSEric Biggers * The reason "ghash" is separate from "gcm" is to allow "gcm" to use an
268dfa20fcSEric Biggers * accelerated "ghash" when a standalone accelerated "gcm(aes)" is unavailable.
278dfa20fcSEric Biggers * It is generally inappropriate to use "ghash" for other purposes, since it is
288dfa20fcSEric Biggers * an "ε-almost-XOR-universal hash function", not a cryptographic hash function.
298dfa20fcSEric Biggers * It can only be used securely in crypto modes specially designed to use it.
308dfa20fcSEric Biggers *
318dfa20fcSEric Biggers * [1] The Galois/Counter Mode of Operation (GCM)
328dfa20fcSEric Biggers * (http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.694.695&rep=rep1&type=pdf)
338dfa20fcSEric Biggers * [2] Recommendation for Block Cipher Modes of Operation: Galois/Counter Mode (GCM) and GMAC
348dfa20fcSEric Biggers * (https://csrc.nist.gov/publications/detail/sp/800-38d/final)
352cdc6899SHuang Ying */
362cdc6899SHuang Ying
372cdc6899SHuang Ying #include <crypto/algapi.h>
382cdc6899SHuang Ying #include <crypto/gf128mul.h>
39a397ba82SMarcelo Cerri #include <crypto/ghash.h>
402cdc6899SHuang Ying #include <crypto/internal/hash.h>
412cdc6899SHuang Ying #include <linux/crypto.h>
422cdc6899SHuang Ying #include <linux/init.h>
432cdc6899SHuang Ying #include <linux/kernel.h>
442cdc6899SHuang Ying #include <linux/module.h>
452cdc6899SHuang Ying
ghash_init(struct shash_desc * desc)462cdc6899SHuang Ying static int ghash_init(struct shash_desc *desc)
472cdc6899SHuang Ying {
482cdc6899SHuang Ying struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
492cdc6899SHuang Ying
502cdc6899SHuang Ying memset(dctx, 0, sizeof(*dctx));
512cdc6899SHuang Ying
522cdc6899SHuang Ying return 0;
532cdc6899SHuang Ying }
542cdc6899SHuang Ying
ghash_setkey(struct crypto_shash * tfm,const u8 * key,unsigned int keylen)552cdc6899SHuang Ying static int ghash_setkey(struct crypto_shash *tfm,
562cdc6899SHuang Ying const u8 *key, unsigned int keylen)
572cdc6899SHuang Ying {
582cdc6899SHuang Ying struct ghash_ctx *ctx = crypto_shash_ctx(tfm);
595c6bc4dfSEric Biggers be128 k;
602cdc6899SHuang Ying
61*674f368aSEric Biggers if (keylen != GHASH_BLOCK_SIZE)
622cdc6899SHuang Ying return -EINVAL;
632cdc6899SHuang Ying
642cdc6899SHuang Ying if (ctx->gf128)
652cdc6899SHuang Ying gf128mul_free_4k(ctx->gf128);
665c6bc4dfSEric Biggers
675c6bc4dfSEric Biggers BUILD_BUG_ON(sizeof(k) != GHASH_BLOCK_SIZE);
685c6bc4dfSEric Biggers memcpy(&k, key, GHASH_BLOCK_SIZE); /* avoid violating alignment rules */
695c6bc4dfSEric Biggers ctx->gf128 = gf128mul_init_4k_lle(&k);
705c6bc4dfSEric Biggers memzero_explicit(&k, GHASH_BLOCK_SIZE);
715c6bc4dfSEric Biggers
722cdc6899SHuang Ying if (!ctx->gf128)
732cdc6899SHuang Ying return -ENOMEM;
742cdc6899SHuang Ying
752cdc6899SHuang Ying return 0;
762cdc6899SHuang Ying }
772cdc6899SHuang Ying
ghash_update(struct shash_desc * desc,const u8 * src,unsigned int srclen)782cdc6899SHuang Ying static int ghash_update(struct shash_desc *desc,
792cdc6899SHuang Ying const u8 *src, unsigned int srclen)
802cdc6899SHuang Ying {
812cdc6899SHuang Ying struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
822cdc6899SHuang Ying struct ghash_ctx *ctx = crypto_shash_ctx(desc->tfm);
832cdc6899SHuang Ying u8 *dst = dctx->buffer;
842cdc6899SHuang Ying
852cdc6899SHuang Ying if (dctx->bytes) {
862cdc6899SHuang Ying int n = min(srclen, dctx->bytes);
872cdc6899SHuang Ying u8 *pos = dst + (GHASH_BLOCK_SIZE - dctx->bytes);
882cdc6899SHuang Ying
892cdc6899SHuang Ying dctx->bytes -= n;
902cdc6899SHuang Ying srclen -= n;
912cdc6899SHuang Ying
922cdc6899SHuang Ying while (n--)
932cdc6899SHuang Ying *pos++ ^= *src++;
942cdc6899SHuang Ying
952cdc6899SHuang Ying if (!dctx->bytes)
962cdc6899SHuang Ying gf128mul_4k_lle((be128 *)dst, ctx->gf128);
972cdc6899SHuang Ying }
982cdc6899SHuang Ying
992cdc6899SHuang Ying while (srclen >= GHASH_BLOCK_SIZE) {
1002cdc6899SHuang Ying crypto_xor(dst, src, GHASH_BLOCK_SIZE);
1012cdc6899SHuang Ying gf128mul_4k_lle((be128 *)dst, ctx->gf128);
1022cdc6899SHuang Ying src += GHASH_BLOCK_SIZE;
1032cdc6899SHuang Ying srclen -= GHASH_BLOCK_SIZE;
1042cdc6899SHuang Ying }
1052cdc6899SHuang Ying
1062cdc6899SHuang Ying if (srclen) {
1072cdc6899SHuang Ying dctx->bytes = GHASH_BLOCK_SIZE - srclen;
1082cdc6899SHuang Ying while (srclen--)
1092cdc6899SHuang Ying *dst++ ^= *src++;
1102cdc6899SHuang Ying }
1112cdc6899SHuang Ying
1122cdc6899SHuang Ying return 0;
1132cdc6899SHuang Ying }
1142cdc6899SHuang Ying
ghash_flush(struct ghash_ctx * ctx,struct ghash_desc_ctx * dctx)1152cdc6899SHuang Ying static void ghash_flush(struct ghash_ctx *ctx, struct ghash_desc_ctx *dctx)
1162cdc6899SHuang Ying {
1172cdc6899SHuang Ying u8 *dst = dctx->buffer;
1182cdc6899SHuang Ying
1192cdc6899SHuang Ying if (dctx->bytes) {
1202cdc6899SHuang Ying u8 *tmp = dst + (GHASH_BLOCK_SIZE - dctx->bytes);
1212cdc6899SHuang Ying
1222cdc6899SHuang Ying while (dctx->bytes--)
1232cdc6899SHuang Ying *tmp++ ^= 0;
1242cdc6899SHuang Ying
1252cdc6899SHuang Ying gf128mul_4k_lle((be128 *)dst, ctx->gf128);
1262cdc6899SHuang Ying }
1272cdc6899SHuang Ying
1282cdc6899SHuang Ying dctx->bytes = 0;
1292cdc6899SHuang Ying }
1302cdc6899SHuang Ying
ghash_final(struct shash_desc * desc,u8 * dst)1312cdc6899SHuang Ying static int ghash_final(struct shash_desc *desc, u8 *dst)
1322cdc6899SHuang Ying {
1332cdc6899SHuang Ying struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
1342cdc6899SHuang Ying struct ghash_ctx *ctx = crypto_shash_ctx(desc->tfm);
1352cdc6899SHuang Ying u8 *buf = dctx->buffer;
1362cdc6899SHuang Ying
1372cdc6899SHuang Ying ghash_flush(ctx, dctx);
1382cdc6899SHuang Ying memcpy(dst, buf, GHASH_BLOCK_SIZE);
1392cdc6899SHuang Ying
1402cdc6899SHuang Ying return 0;
1412cdc6899SHuang Ying }
1422cdc6899SHuang Ying
ghash_exit_tfm(struct crypto_tfm * tfm)1432cdc6899SHuang Ying static void ghash_exit_tfm(struct crypto_tfm *tfm)
1442cdc6899SHuang Ying {
1452cdc6899SHuang Ying struct ghash_ctx *ctx = crypto_tfm_ctx(tfm);
1462cdc6899SHuang Ying if (ctx->gf128)
1472cdc6899SHuang Ying gf128mul_free_4k(ctx->gf128);
1482cdc6899SHuang Ying }
1492cdc6899SHuang Ying
1502cdc6899SHuang Ying static struct shash_alg ghash_alg = {
1512cdc6899SHuang Ying .digestsize = GHASH_DIGEST_SIZE,
1522cdc6899SHuang Ying .init = ghash_init,
1532cdc6899SHuang Ying .update = ghash_update,
1542cdc6899SHuang Ying .final = ghash_final,
1552cdc6899SHuang Ying .setkey = ghash_setkey,
1562cdc6899SHuang Ying .descsize = sizeof(struct ghash_desc_ctx),
1572cdc6899SHuang Ying .base = {
1582cdc6899SHuang Ying .cra_name = "ghash",
1592cdc6899SHuang Ying .cra_driver_name = "ghash-generic",
1602cdc6899SHuang Ying .cra_priority = 100,
1612cdc6899SHuang Ying .cra_blocksize = GHASH_BLOCK_SIZE,
1622cdc6899SHuang Ying .cra_ctxsize = sizeof(struct ghash_ctx),
1632cdc6899SHuang Ying .cra_module = THIS_MODULE,
1642cdc6899SHuang Ying .cra_exit = ghash_exit_tfm,
1652cdc6899SHuang Ying },
1662cdc6899SHuang Ying };
1672cdc6899SHuang Ying
ghash_mod_init(void)1682cdc6899SHuang Ying static int __init ghash_mod_init(void)
1692cdc6899SHuang Ying {
1702cdc6899SHuang Ying return crypto_register_shash(&ghash_alg);
1712cdc6899SHuang Ying }
1722cdc6899SHuang Ying
ghash_mod_exit(void)1732cdc6899SHuang Ying static void __exit ghash_mod_exit(void)
1742cdc6899SHuang Ying {
1752cdc6899SHuang Ying crypto_unregister_shash(&ghash_alg);
1762cdc6899SHuang Ying }
1772cdc6899SHuang Ying
178c4741b23SEric Biggers subsys_initcall(ghash_mod_init);
1792cdc6899SHuang Ying module_exit(ghash_mod_exit);
1802cdc6899SHuang Ying
1812cdc6899SHuang Ying MODULE_LICENSE("GPL");
1828dfa20fcSEric Biggers MODULE_DESCRIPTION("GHASH hash function");
1835d26a105SKees Cook MODULE_ALIAS_CRYPTO("ghash");
1843e14dcf7SMathias Krause MODULE_ALIAS_CRYPTO("ghash-generic");
185