Linux Audio

Check our new training course

Loading...
v5.9
  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/algapi.h>
 38#include <crypto/gf128mul.h>
 39#include <crypto/ghash.h>
 40#include <crypto/internal/hash.h>
 41#include <linux/crypto.h>
 42#include <linux/init.h>
 43#include <linux/kernel.h>
 44#include <linux/module.h>
 45
 46static int ghash_init(struct shash_desc *desc)
 47{
 48	struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
 49
 50	memset(dctx, 0, 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(ctx->gf128);
 66
 67	BUILD_BUG_ON(sizeof(k) != GHASH_BLOCK_SIZE);
 68	memcpy(&k, key, GHASH_BLOCK_SIZE); /* avoid violating alignment rules */
 69	ctx->gf128 = gf128mul_init_4k_lle(&k);
 70	memzero_explicit(&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(desc->tfm);
 83	u8 *dst = dctx->buffer;
 84
 85	if (dctx->bytes) {
 86		int n = min(srclen, dctx->bytes);
 87		u8 *pos = dst + (GHASH_BLOCK_SIZE - dctx->bytes);
 88
 89		dctx->bytes -= n;
 90		srclen -= n;
 91
 92		while (n--)
 93			*pos++ ^= *src++;
 94
 95		if (!dctx->bytes)
 96			gf128mul_4k_lle((be128 *)dst, ctx->gf128);
 97	}
 98
 99	while (srclen >= GHASH_BLOCK_SIZE) {
100		crypto_xor(dst, src, GHASH_BLOCK_SIZE);
101		gf128mul_4k_lle((be128 *)dst, ctx->gf128);
102		src += GHASH_BLOCK_SIZE;
103		srclen -= GHASH_BLOCK_SIZE;
104	}
105
106	if (srclen) {
107		dctx->bytes = GHASH_BLOCK_SIZE - srclen;
108		while (srclen--)
109			*dst++ ^= *src++;
110	}
111
112	return 0;
113}
114
115static void ghash_flush(struct ghash_ctx *ctx, struct ghash_desc_ctx *dctx)
116{
117	u8 *dst = dctx->buffer;
118
119	if (dctx->bytes) {
120		u8 *tmp = dst + (GHASH_BLOCK_SIZE - dctx->bytes);
121
122		while (dctx->bytes--)
123			*tmp++ ^= 0;
124
125		gf128mul_4k_lle((be128 *)dst, ctx->gf128);
126	}
127
128	dctx->bytes = 0;
129}
130
131static int ghash_final(struct shash_desc *desc, u8 *dst)
132{
133	struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
134	struct ghash_ctx *ctx = crypto_shash_ctx(desc->tfm);
135	u8 *buf = dctx->buffer;
136
137	ghash_flush(ctx, dctx);
138	memcpy(dst, buf, GHASH_BLOCK_SIZE);
139
140	return 0;
141}
142
143static void ghash_exit_tfm(struct crypto_tfm *tfm)
144{
145	struct ghash_ctx *ctx = crypto_tfm_ctx(tfm);
146	if (ctx->gf128)
147		gf128mul_free_4k(ctx->gf128);
148}
149
150static struct shash_alg ghash_alg = {
151	.digestsize	= GHASH_DIGEST_SIZE,
152	.init		= ghash_init,
153	.update		= ghash_update,
154	.final		= ghash_final,
155	.setkey		= ghash_setkey,
156	.descsize	= sizeof(struct ghash_desc_ctx),
157	.base		= {
158		.cra_name		= "ghash",
159		.cra_driver_name	= "ghash-generic",
160		.cra_priority		= 100,
 
161		.cra_blocksize		= GHASH_BLOCK_SIZE,
162		.cra_ctxsize		= sizeof(struct ghash_ctx),
163		.cra_module		= THIS_MODULE,
164		.cra_exit		= ghash_exit_tfm,
165	},
166};
167
168static int __init ghash_mod_init(void)
169{
170	return crypto_register_shash(&ghash_alg);
171}
172
173static void __exit ghash_mod_exit(void)
174{
175	crypto_unregister_shash(&ghash_alg);
176}
177
178subsys_initcall(ghash_mod_init);
179module_exit(ghash_mod_exit);
180
181MODULE_LICENSE("GPL");
182MODULE_DESCRIPTION("GHASH hash function");
183MODULE_ALIAS_CRYPTO("ghash");
184MODULE_ALIAS_CRYPTO("ghash-generic");
v4.17
 
  1/*
  2 * GHASH: digest algorithm for GCM (Galois/Counter Mode).
  3 *
  4 * Copyright (c) 2007 Nokia Siemens Networks - Mikko Herranen <mh1@iki.fi>
  5 * Copyright (c) 2009 Intel Corp.
  6 *   Author: Huang Ying <ying.huang@intel.com>
 
 
 
 
 
 
 
 
 
 
 
 
  7 *
  8 * The algorithm implementation is copied from gcm.c.
 
 
  9 *
 10 * This program is free software; you can redistribute it and/or modify it
 11 * under the terms of the GNU General Public License version 2 as published
 12 * by the Free Software Foundation.
 
 
 
 
 
 
 
 13 */
 14
 15#include <crypto/algapi.h>
 16#include <crypto/gf128mul.h>
 17#include <crypto/ghash.h>
 18#include <crypto/internal/hash.h>
 19#include <linux/crypto.h>
 20#include <linux/init.h>
 21#include <linux/kernel.h>
 22#include <linux/module.h>
 23
 24static int ghash_init(struct shash_desc *desc)
 25{
 26	struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
 27
 28	memset(dctx, 0, sizeof(*dctx));
 29
 30	return 0;
 31}
 32
 33static int ghash_setkey(struct crypto_shash *tfm,
 34			const u8 *key, unsigned int keylen)
 35{
 36	struct ghash_ctx *ctx = crypto_shash_ctx(tfm);
 
 37
 38	if (keylen != GHASH_BLOCK_SIZE) {
 39		crypto_shash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
 40		return -EINVAL;
 41	}
 42
 43	if (ctx->gf128)
 44		gf128mul_free_4k(ctx->gf128);
 45	ctx->gf128 = gf128mul_init_4k_lle((be128 *)key);
 
 
 
 
 
 46	if (!ctx->gf128)
 47		return -ENOMEM;
 48
 49	return 0;
 50}
 51
 52static int ghash_update(struct shash_desc *desc,
 53			 const u8 *src, unsigned int srclen)
 54{
 55	struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
 56	struct ghash_ctx *ctx = crypto_shash_ctx(desc->tfm);
 57	u8 *dst = dctx->buffer;
 58
 59	if (dctx->bytes) {
 60		int n = min(srclen, dctx->bytes);
 61		u8 *pos = dst + (GHASH_BLOCK_SIZE - dctx->bytes);
 62
 63		dctx->bytes -= n;
 64		srclen -= n;
 65
 66		while (n--)
 67			*pos++ ^= *src++;
 68
 69		if (!dctx->bytes)
 70			gf128mul_4k_lle((be128 *)dst, ctx->gf128);
 71	}
 72
 73	while (srclen >= GHASH_BLOCK_SIZE) {
 74		crypto_xor(dst, src, GHASH_BLOCK_SIZE);
 75		gf128mul_4k_lle((be128 *)dst, ctx->gf128);
 76		src += GHASH_BLOCK_SIZE;
 77		srclen -= GHASH_BLOCK_SIZE;
 78	}
 79
 80	if (srclen) {
 81		dctx->bytes = GHASH_BLOCK_SIZE - srclen;
 82		while (srclen--)
 83			*dst++ ^= *src++;
 84	}
 85
 86	return 0;
 87}
 88
 89static void ghash_flush(struct ghash_ctx *ctx, struct ghash_desc_ctx *dctx)
 90{
 91	u8 *dst = dctx->buffer;
 92
 93	if (dctx->bytes) {
 94		u8 *tmp = dst + (GHASH_BLOCK_SIZE - dctx->bytes);
 95
 96		while (dctx->bytes--)
 97			*tmp++ ^= 0;
 98
 99		gf128mul_4k_lle((be128 *)dst, ctx->gf128);
100	}
101
102	dctx->bytes = 0;
103}
104
105static int ghash_final(struct shash_desc *desc, u8 *dst)
106{
107	struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
108	struct ghash_ctx *ctx = crypto_shash_ctx(desc->tfm);
109	u8 *buf = dctx->buffer;
110
111	ghash_flush(ctx, dctx);
112	memcpy(dst, buf, GHASH_BLOCK_SIZE);
113
114	return 0;
115}
116
117static void ghash_exit_tfm(struct crypto_tfm *tfm)
118{
119	struct ghash_ctx *ctx = crypto_tfm_ctx(tfm);
120	if (ctx->gf128)
121		gf128mul_free_4k(ctx->gf128);
122}
123
124static struct shash_alg ghash_alg = {
125	.digestsize	= GHASH_DIGEST_SIZE,
126	.init		= ghash_init,
127	.update		= ghash_update,
128	.final		= ghash_final,
129	.setkey		= ghash_setkey,
130	.descsize	= sizeof(struct ghash_desc_ctx),
131	.base		= {
132		.cra_name		= "ghash",
133		.cra_driver_name	= "ghash-generic",
134		.cra_priority		= 100,
135		.cra_flags		= CRYPTO_ALG_TYPE_SHASH,
136		.cra_blocksize		= GHASH_BLOCK_SIZE,
137		.cra_ctxsize		= sizeof(struct ghash_ctx),
138		.cra_module		= THIS_MODULE,
139		.cra_exit		= ghash_exit_tfm,
140	},
141};
142
143static int __init ghash_mod_init(void)
144{
145	return crypto_register_shash(&ghash_alg);
146}
147
148static void __exit ghash_mod_exit(void)
149{
150	crypto_unregister_shash(&ghash_alg);
151}
152
153module_init(ghash_mod_init);
154module_exit(ghash_mod_exit);
155
156MODULE_LICENSE("GPL");
157MODULE_DESCRIPTION("GHASH Message Digest Algorithm");
158MODULE_ALIAS_CRYPTO("ghash");
159MODULE_ALIAS_CRYPTO("ghash-generic");