Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * GHASH: hash function for GCM (Galois/Counter Mode).
0004  *
0005  * Copyright (c) 2007 Nokia Siemens Networks - Mikko Herranen <mh1@iki.fi>
0006  * Copyright (c) 2009 Intel Corp.
0007  *   Author: Huang Ying <ying.huang@intel.com>
0008  */
0009 
0010 /*
0011  * GHASH is a keyed hash function used in GCM authentication tag generation.
0012  *
0013  * The original GCM paper [1] presents GHASH as a function GHASH(H, A, C) which
0014  * takes a 16-byte hash key H, additional authenticated data A, and a ciphertext
0015  * C.  It formats A and C into a single byte string X, interprets X as a
0016  * polynomial over GF(2^128), and evaluates this polynomial at the point H.
0017  *
0018  * However, the NIST standard for GCM [2] presents GHASH as GHASH(H, X) where X
0019  * is the already-formatted byte string containing both A and C.
0020  *
0021  * "ghash" in the Linux crypto API uses the 'X' (pre-formatted) convention,
0022  * since the API supports only a single data stream per hash.  Thus, the
0023  * formatting of 'A' and 'C' is done in the "gcm" template, not in "ghash".
0024  *
0025  * The reason "ghash" is separate from "gcm" is to allow "gcm" to use an
0026  * accelerated "ghash" when a standalone accelerated "gcm(aes)" is unavailable.
0027  * It is generally inappropriate to use "ghash" for other purposes, since it is
0028  * an "ε-almost-XOR-universal hash function", not a cryptographic hash function.
0029  * It can only be used securely in crypto modes specially designed to use it.
0030  *
0031  * [1] The Galois/Counter Mode of Operation (GCM)
0032  *     (http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.694.695&rep=rep1&type=pdf)
0033  * [2] Recommendation for Block Cipher Modes of Operation: Galois/Counter Mode (GCM) and GMAC
0034  *     (https://csrc.nist.gov/publications/detail/sp/800-38d/final)
0035  */
0036 
0037 #include <crypto/algapi.h>
0038 #include <crypto/gf128mul.h>
0039 #include <crypto/ghash.h>
0040 #include <crypto/internal/hash.h>
0041 #include <linux/crypto.h>
0042 #include <linux/init.h>
0043 #include <linux/kernel.h>
0044 #include <linux/module.h>
0045 
0046 static int ghash_init(struct shash_desc *desc)
0047 {
0048     struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
0049 
0050     memset(dctx, 0, sizeof(*dctx));
0051 
0052     return 0;
0053 }
0054 
0055 static int ghash_setkey(struct crypto_shash *tfm,
0056             const u8 *key, unsigned int keylen)
0057 {
0058     struct ghash_ctx *ctx = crypto_shash_ctx(tfm);
0059     be128 k;
0060 
0061     if (keylen != GHASH_BLOCK_SIZE)
0062         return -EINVAL;
0063 
0064     if (ctx->gf128)
0065         gf128mul_free_4k(ctx->gf128);
0066 
0067     BUILD_BUG_ON(sizeof(k) != GHASH_BLOCK_SIZE);
0068     memcpy(&k, key, GHASH_BLOCK_SIZE); /* avoid violating alignment rules */
0069     ctx->gf128 = gf128mul_init_4k_lle(&k);
0070     memzero_explicit(&k, GHASH_BLOCK_SIZE);
0071 
0072     if (!ctx->gf128)
0073         return -ENOMEM;
0074 
0075     return 0;
0076 }
0077 
0078 static int ghash_update(struct shash_desc *desc,
0079              const u8 *src, unsigned int srclen)
0080 {
0081     struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
0082     struct ghash_ctx *ctx = crypto_shash_ctx(desc->tfm);
0083     u8 *dst = dctx->buffer;
0084 
0085     if (dctx->bytes) {
0086         int n = min(srclen, dctx->bytes);
0087         u8 *pos = dst + (GHASH_BLOCK_SIZE - dctx->bytes);
0088 
0089         dctx->bytes -= n;
0090         srclen -= n;
0091 
0092         while (n--)
0093             *pos++ ^= *src++;
0094 
0095         if (!dctx->bytes)
0096             gf128mul_4k_lle((be128 *)dst, ctx->gf128);
0097     }
0098 
0099     while (srclen >= GHASH_BLOCK_SIZE) {
0100         crypto_xor(dst, src, GHASH_BLOCK_SIZE);
0101         gf128mul_4k_lle((be128 *)dst, ctx->gf128);
0102         src += GHASH_BLOCK_SIZE;
0103         srclen -= GHASH_BLOCK_SIZE;
0104     }
0105 
0106     if (srclen) {
0107         dctx->bytes = GHASH_BLOCK_SIZE - srclen;
0108         while (srclen--)
0109             *dst++ ^= *src++;
0110     }
0111 
0112     return 0;
0113 }
0114 
0115 static void ghash_flush(struct ghash_ctx *ctx, struct ghash_desc_ctx *dctx)
0116 {
0117     u8 *dst = dctx->buffer;
0118 
0119     if (dctx->bytes) {
0120         u8 *tmp = dst + (GHASH_BLOCK_SIZE - dctx->bytes);
0121 
0122         while (dctx->bytes--)
0123             *tmp++ ^= 0;
0124 
0125         gf128mul_4k_lle((be128 *)dst, ctx->gf128);
0126     }
0127 
0128     dctx->bytes = 0;
0129 }
0130 
0131 static int ghash_final(struct shash_desc *desc, u8 *dst)
0132 {
0133     struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
0134     struct ghash_ctx *ctx = crypto_shash_ctx(desc->tfm);
0135     u8 *buf = dctx->buffer;
0136 
0137     ghash_flush(ctx, dctx);
0138     memcpy(dst, buf, GHASH_BLOCK_SIZE);
0139 
0140     return 0;
0141 }
0142 
0143 static void ghash_exit_tfm(struct crypto_tfm *tfm)
0144 {
0145     struct ghash_ctx *ctx = crypto_tfm_ctx(tfm);
0146     if (ctx->gf128)
0147         gf128mul_free_4k(ctx->gf128);
0148 }
0149 
0150 static struct shash_alg ghash_alg = {
0151     .digestsize = GHASH_DIGEST_SIZE,
0152     .init       = ghash_init,
0153     .update     = ghash_update,
0154     .final      = ghash_final,
0155     .setkey     = ghash_setkey,
0156     .descsize   = sizeof(struct ghash_desc_ctx),
0157     .base       = {
0158         .cra_name       = "ghash",
0159         .cra_driver_name    = "ghash-generic",
0160         .cra_priority       = 100,
0161         .cra_blocksize      = GHASH_BLOCK_SIZE,
0162         .cra_ctxsize        = sizeof(struct ghash_ctx),
0163         .cra_module     = THIS_MODULE,
0164         .cra_exit       = ghash_exit_tfm,
0165     },
0166 };
0167 
0168 static int __init ghash_mod_init(void)
0169 {
0170     return crypto_register_shash(&ghash_alg);
0171 }
0172 
0173 static void __exit ghash_mod_exit(void)
0174 {
0175     crypto_unregister_shash(&ghash_alg);
0176 }
0177 
0178 subsys_initcall(ghash_mod_init);
0179 module_exit(ghash_mod_exit);
0180 
0181 MODULE_LICENSE("GPL");
0182 MODULE_DESCRIPTION("GHASH hash function");
0183 MODULE_ALIAS_CRYPTO("ghash");
0184 MODULE_ALIAS_CRYPTO("ghash-generic");