1 // SPDX-License-Identifier: GPL-2.0
5 * s390 implementation of the GHASH algorithm for GCM (Galois/Counter Mode).
7 * Copyright IBM Corp. 2011
8 * Author(s): Gerald Schaefer <gerald.schaefer@de.ibm.com>
11 #include <crypto/internal/hash.h>
12 #include <linux/module.h>
13 #include <linux/cpufeature.h>
14 #include <asm/cpacf.h>
16 #define GHASH_BLOCK_SIZE 16
17 #define GHASH_DIGEST_SIZE 16
20 u8 key[GHASH_BLOCK_SIZE];
23 struct ghash_desc_ctx {
24 u8 icv[GHASH_BLOCK_SIZE];
25 u8 key[GHASH_BLOCK_SIZE];
26 u8 buffer[GHASH_BLOCK_SIZE];
30 static int ghash_init(struct shash_desc *desc)
32 struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
33 struct ghash_ctx *ctx = crypto_shash_ctx(desc->tfm);
35 memset(dctx, 0, sizeof(*dctx));
36 memcpy(dctx->key, ctx->key, GHASH_BLOCK_SIZE);
41 static int ghash_setkey(struct crypto_shash *tfm,
42 const u8 *key, unsigned int keylen)
44 struct ghash_ctx *ctx = crypto_shash_ctx(tfm);
46 if (keylen != GHASH_BLOCK_SIZE)
49 memcpy(ctx->key, key, GHASH_BLOCK_SIZE);
54 static int ghash_update(struct shash_desc *desc,
55 const u8 *src, unsigned int srclen)
57 struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
59 u8 *buf = dctx->buffer;
62 u8 *pos = buf + (GHASH_BLOCK_SIZE - dctx->bytes);
64 n = min(srclen, dctx->bytes);
72 cpacf_kimd(CPACF_KIMD_GHASH, dctx, buf,
77 n = srclen & ~(GHASH_BLOCK_SIZE - 1);
79 cpacf_kimd(CPACF_KIMD_GHASH, dctx, src, n);
85 dctx->bytes = GHASH_BLOCK_SIZE - srclen;
86 memcpy(buf, src, srclen);
92 static int ghash_flush(struct ghash_desc_ctx *dctx)
94 u8 *buf = dctx->buffer;
97 u8 *pos = buf + (GHASH_BLOCK_SIZE - dctx->bytes);
99 memset(pos, 0, dctx->bytes);
100 cpacf_kimd(CPACF_KIMD_GHASH, dctx, buf, GHASH_BLOCK_SIZE);
107 static int ghash_final(struct shash_desc *desc, u8 *dst)
109 struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
112 ret = ghash_flush(dctx);
114 memcpy(dst, dctx->icv, GHASH_BLOCK_SIZE);
118 static struct shash_alg ghash_alg = {
119 .digestsize = GHASH_DIGEST_SIZE,
121 .update = ghash_update,
122 .final = ghash_final,
123 .setkey = ghash_setkey,
124 .descsize = sizeof(struct ghash_desc_ctx),
127 .cra_driver_name = "ghash-s390",
129 .cra_blocksize = GHASH_BLOCK_SIZE,
130 .cra_ctxsize = sizeof(struct ghash_ctx),
131 .cra_module = THIS_MODULE,
135 static int __init ghash_mod_init(void)
137 if (!cpacf_query_func(CPACF_KIMD, CPACF_KIMD_GHASH))
140 return crypto_register_shash(&ghash_alg);
143 static void __exit ghash_mod_exit(void)
145 crypto_unregister_shash(&ghash_alg);
148 module_cpu_feature_match(S390_CPU_FEATURE_MSA, ghash_mod_init);
149 module_exit(ghash_mod_exit);
151 MODULE_ALIAS_CRYPTO("ghash");
153 MODULE_LICENSE("GPL");
154 MODULE_DESCRIPTION("GHASH hash function, s390 implementation");