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