1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * GHASH: hash function for GCM (Galois/Counter Mode).
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (c) 2007 Nokia Siemens Networks - Mikko Herranen <mh1@iki.fi>
6*4882a593Smuzhiyun * Copyright (c) 2009 Intel Corp.
7*4882a593Smuzhiyun * Author: Huang Ying <ying.huang@intel.com>
8*4882a593Smuzhiyun */
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun /*
11*4882a593Smuzhiyun * GHASH is a keyed hash function used in GCM authentication tag generation.
12*4882a593Smuzhiyun *
13*4882a593Smuzhiyun * The original GCM paper [1] presents GHASH as a function GHASH(H, A, C) which
14*4882a593Smuzhiyun * takes a 16-byte hash key H, additional authenticated data A, and a ciphertext
15*4882a593Smuzhiyun * C. It formats A and C into a single byte string X, interprets X as a
16*4882a593Smuzhiyun * polynomial over GF(2^128), and evaluates this polynomial at the point H.
17*4882a593Smuzhiyun *
18*4882a593Smuzhiyun * However, the NIST standard for GCM [2] presents GHASH as GHASH(H, X) where X
19*4882a593Smuzhiyun * is the already-formatted byte string containing both A and C.
20*4882a593Smuzhiyun *
21*4882a593Smuzhiyun * "ghash" in the Linux crypto API uses the 'X' (pre-formatted) convention,
22*4882a593Smuzhiyun * since the API supports only a single data stream per hash. Thus, the
23*4882a593Smuzhiyun * formatting of 'A' and 'C' is done in the "gcm" template, not in "ghash".
24*4882a593Smuzhiyun *
25*4882a593Smuzhiyun * The reason "ghash" is separate from "gcm" is to allow "gcm" to use an
26*4882a593Smuzhiyun * accelerated "ghash" when a standalone accelerated "gcm(aes)" is unavailable.
27*4882a593Smuzhiyun * It is generally inappropriate to use "ghash" for other purposes, since it is
28*4882a593Smuzhiyun * an "ε-almost-XOR-universal hash function", not a cryptographic hash function.
29*4882a593Smuzhiyun * It can only be used securely in crypto modes specially designed to use it.
30*4882a593Smuzhiyun *
31*4882a593Smuzhiyun * [1] The Galois/Counter Mode of Operation (GCM)
32*4882a593Smuzhiyun * (http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.694.695&rep=rep1&type=pdf)
33*4882a593Smuzhiyun * [2] Recommendation for Block Cipher Modes of Operation: Galois/Counter Mode (GCM) and GMAC
34*4882a593Smuzhiyun * (https://csrc.nist.gov/publications/detail/sp/800-38d/final)
35*4882a593Smuzhiyun */
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun #include <crypto/algapi.h>
38*4882a593Smuzhiyun #include <crypto/gf128mul.h>
39*4882a593Smuzhiyun #include <crypto/ghash.h>
40*4882a593Smuzhiyun #include <crypto/internal/hash.h>
41*4882a593Smuzhiyun #include <linux/crypto.h>
42*4882a593Smuzhiyun #include <linux/init.h>
43*4882a593Smuzhiyun #include <linux/kernel.h>
44*4882a593Smuzhiyun #include <linux/module.h>
45*4882a593Smuzhiyun
ghash_init(struct shash_desc * desc)46*4882a593Smuzhiyun static int ghash_init(struct shash_desc *desc)
47*4882a593Smuzhiyun {
48*4882a593Smuzhiyun struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun memset(dctx, 0, sizeof(*dctx));
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun return 0;
53*4882a593Smuzhiyun }
54*4882a593Smuzhiyun
ghash_setkey(struct crypto_shash * tfm,const u8 * key,unsigned int keylen)55*4882a593Smuzhiyun static int ghash_setkey(struct crypto_shash *tfm,
56*4882a593Smuzhiyun const u8 *key, unsigned int keylen)
57*4882a593Smuzhiyun {
58*4882a593Smuzhiyun struct ghash_ctx *ctx = crypto_shash_ctx(tfm);
59*4882a593Smuzhiyun be128 k;
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun if (keylen != GHASH_BLOCK_SIZE)
62*4882a593Smuzhiyun return -EINVAL;
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun if (ctx->gf128)
65*4882a593Smuzhiyun gf128mul_free_4k(ctx->gf128);
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun BUILD_BUG_ON(sizeof(k) != GHASH_BLOCK_SIZE);
68*4882a593Smuzhiyun memcpy(&k, key, GHASH_BLOCK_SIZE); /* avoid violating alignment rules */
69*4882a593Smuzhiyun ctx->gf128 = gf128mul_init_4k_lle(&k);
70*4882a593Smuzhiyun memzero_explicit(&k, GHASH_BLOCK_SIZE);
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun if (!ctx->gf128)
73*4882a593Smuzhiyun return -ENOMEM;
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun return 0;
76*4882a593Smuzhiyun }
77*4882a593Smuzhiyun
ghash_update(struct shash_desc * desc,const u8 * src,unsigned int srclen)78*4882a593Smuzhiyun static int ghash_update(struct shash_desc *desc,
79*4882a593Smuzhiyun const u8 *src, unsigned int srclen)
80*4882a593Smuzhiyun {
81*4882a593Smuzhiyun struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
82*4882a593Smuzhiyun struct ghash_ctx *ctx = crypto_shash_ctx(desc->tfm);
83*4882a593Smuzhiyun u8 *dst = dctx->buffer;
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun if (dctx->bytes) {
86*4882a593Smuzhiyun int n = min(srclen, dctx->bytes);
87*4882a593Smuzhiyun u8 *pos = dst + (GHASH_BLOCK_SIZE - dctx->bytes);
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun dctx->bytes -= n;
90*4882a593Smuzhiyun srclen -= n;
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun while (n--)
93*4882a593Smuzhiyun *pos++ ^= *src++;
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun if (!dctx->bytes)
96*4882a593Smuzhiyun gf128mul_4k_lle((be128 *)dst, ctx->gf128);
97*4882a593Smuzhiyun }
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun while (srclen >= GHASH_BLOCK_SIZE) {
100*4882a593Smuzhiyun crypto_xor(dst, src, GHASH_BLOCK_SIZE);
101*4882a593Smuzhiyun gf128mul_4k_lle((be128 *)dst, ctx->gf128);
102*4882a593Smuzhiyun src += GHASH_BLOCK_SIZE;
103*4882a593Smuzhiyun srclen -= GHASH_BLOCK_SIZE;
104*4882a593Smuzhiyun }
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun if (srclen) {
107*4882a593Smuzhiyun dctx->bytes = GHASH_BLOCK_SIZE - srclen;
108*4882a593Smuzhiyun while (srclen--)
109*4882a593Smuzhiyun *dst++ ^= *src++;
110*4882a593Smuzhiyun }
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun return 0;
113*4882a593Smuzhiyun }
114*4882a593Smuzhiyun
ghash_flush(struct ghash_ctx * ctx,struct ghash_desc_ctx * dctx)115*4882a593Smuzhiyun static void ghash_flush(struct ghash_ctx *ctx, struct ghash_desc_ctx *dctx)
116*4882a593Smuzhiyun {
117*4882a593Smuzhiyun u8 *dst = dctx->buffer;
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun if (dctx->bytes) {
120*4882a593Smuzhiyun u8 *tmp = dst + (GHASH_BLOCK_SIZE - dctx->bytes);
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun while (dctx->bytes--)
123*4882a593Smuzhiyun *tmp++ ^= 0;
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun gf128mul_4k_lle((be128 *)dst, ctx->gf128);
126*4882a593Smuzhiyun }
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun dctx->bytes = 0;
129*4882a593Smuzhiyun }
130*4882a593Smuzhiyun
ghash_final(struct shash_desc * desc,u8 * dst)131*4882a593Smuzhiyun static int ghash_final(struct shash_desc *desc, u8 *dst)
132*4882a593Smuzhiyun {
133*4882a593Smuzhiyun struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
134*4882a593Smuzhiyun struct ghash_ctx *ctx = crypto_shash_ctx(desc->tfm);
135*4882a593Smuzhiyun u8 *buf = dctx->buffer;
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun ghash_flush(ctx, dctx);
138*4882a593Smuzhiyun memcpy(dst, buf, GHASH_BLOCK_SIZE);
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun return 0;
141*4882a593Smuzhiyun }
142*4882a593Smuzhiyun
ghash_exit_tfm(struct crypto_tfm * tfm)143*4882a593Smuzhiyun static void ghash_exit_tfm(struct crypto_tfm *tfm)
144*4882a593Smuzhiyun {
145*4882a593Smuzhiyun struct ghash_ctx *ctx = crypto_tfm_ctx(tfm);
146*4882a593Smuzhiyun if (ctx->gf128)
147*4882a593Smuzhiyun gf128mul_free_4k(ctx->gf128);
148*4882a593Smuzhiyun }
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun static struct shash_alg ghash_alg = {
151*4882a593Smuzhiyun .digestsize = GHASH_DIGEST_SIZE,
152*4882a593Smuzhiyun .init = ghash_init,
153*4882a593Smuzhiyun .update = ghash_update,
154*4882a593Smuzhiyun .final = ghash_final,
155*4882a593Smuzhiyun .setkey = ghash_setkey,
156*4882a593Smuzhiyun .descsize = sizeof(struct ghash_desc_ctx),
157*4882a593Smuzhiyun .base = {
158*4882a593Smuzhiyun .cra_name = "ghash",
159*4882a593Smuzhiyun .cra_driver_name = "ghash-generic",
160*4882a593Smuzhiyun .cra_priority = 100,
161*4882a593Smuzhiyun .cra_blocksize = GHASH_BLOCK_SIZE,
162*4882a593Smuzhiyun .cra_ctxsize = sizeof(struct ghash_ctx),
163*4882a593Smuzhiyun .cra_module = THIS_MODULE,
164*4882a593Smuzhiyun .cra_exit = ghash_exit_tfm,
165*4882a593Smuzhiyun },
166*4882a593Smuzhiyun };
167*4882a593Smuzhiyun
ghash_mod_init(void)168*4882a593Smuzhiyun static int __init ghash_mod_init(void)
169*4882a593Smuzhiyun {
170*4882a593Smuzhiyun return crypto_register_shash(&ghash_alg);
171*4882a593Smuzhiyun }
172*4882a593Smuzhiyun
ghash_mod_exit(void)173*4882a593Smuzhiyun static void __exit ghash_mod_exit(void)
174*4882a593Smuzhiyun {
175*4882a593Smuzhiyun crypto_unregister_shash(&ghash_alg);
176*4882a593Smuzhiyun }
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun subsys_initcall(ghash_mod_init);
179*4882a593Smuzhiyun module_exit(ghash_mod_exit);
180*4882a593Smuzhiyun
181*4882a593Smuzhiyun MODULE_LICENSE("GPL");
182*4882a593Smuzhiyun MODULE_DESCRIPTION("GHASH hash function");
183*4882a593Smuzhiyun MODULE_ALIAS_CRYPTO("ghash");
184*4882a593Smuzhiyun MODULE_ALIAS_CRYPTO("ghash-generic");
185