blob: 86aed30fad3a4e98352b52ae2a1a7eee4da58c77 [file] [log] [blame]
Greg Kroah-Hartman20a884f2017-11-24 15:00:34 +01001// SPDX-License-Identifier: GPL-2.0
Gerald Schaeferdf1309c2011-04-19 21:29:18 +02002/*
3 * Cryptographic API.
4 *
5 * s390 implementation of the GHASH algorithm for GCM (Galois/Counter Mode).
6 *
7 * Copyright IBM Corp. 2011
8 * Author(s): Gerald Schaefer <gerald.schaefer@de.ibm.com>
9 */
10
11#include <crypto/internal/hash.h>
12#include <linux/module.h>
Hendrik Bruecknerd05377c2015-02-19 17:34:07 +010013#include <linux/cpufeature.h>
Martin Schwidefskyc7d4d252016-03-17 15:22:12 +010014#include <asm/cpacf.h>
Gerald Schaeferdf1309c2011-04-19 21:29:18 +020015
16#define GHASH_BLOCK_SIZE 16
17#define GHASH_DIGEST_SIZE 16
18
19struct ghash_ctx {
Harald Freudenbergera1cae342015-05-21 10:01:11 +020020 u8 key[GHASH_BLOCK_SIZE];
Gerald Schaeferdf1309c2011-04-19 21:29:18 +020021};
22
23struct ghash_desc_ctx {
Harald Freudenbergera1cae342015-05-21 10:01:11 +020024 u8 icv[GHASH_BLOCK_SIZE];
25 u8 key[GHASH_BLOCK_SIZE];
Gerald Schaeferdf1309c2011-04-19 21:29:18 +020026 u8 buffer[GHASH_BLOCK_SIZE];
27 u32 bytes;
28};
29
30static int ghash_init(struct shash_desc *desc)
31{
32 struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
Harald Freudenbergera1cae342015-05-21 10:01:11 +020033 struct ghash_ctx *ctx = crypto_shash_ctx(desc->tfm);
Gerald Schaeferdf1309c2011-04-19 21:29:18 +020034
35 memset(dctx, 0, sizeof(*dctx));
Harald Freudenbergera1cae342015-05-21 10:01:11 +020036 memcpy(dctx->key, ctx->key, GHASH_BLOCK_SIZE);
Gerald Schaeferdf1309c2011-04-19 21:29:18 +020037
38 return 0;
39}
40
41static int ghash_setkey(struct crypto_shash *tfm,
42 const u8 *key, unsigned int keylen)
43{
44 struct ghash_ctx *ctx = crypto_shash_ctx(tfm);
45
46 if (keylen != GHASH_BLOCK_SIZE) {
47 crypto_shash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
48 return -EINVAL;
49 }
50
51 memcpy(ctx->key, key, GHASH_BLOCK_SIZE);
Gerald Schaeferdf1309c2011-04-19 21:29:18 +020052
53 return 0;
54}
55
56static int ghash_update(struct shash_desc *desc,
57 const u8 *src, unsigned int srclen)
58{
59 struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
Gerald Schaeferdf1309c2011-04-19 21:29:18 +020060 unsigned int n;
61 u8 *buf = dctx->buffer;
Gerald Schaeferdf1309c2011-04-19 21:29:18 +020062
63 if (dctx->bytes) {
64 u8 *pos = buf + (GHASH_BLOCK_SIZE - dctx->bytes);
65
66 n = min(srclen, dctx->bytes);
67 dctx->bytes -= n;
68 srclen -= n;
69
70 memcpy(pos, src, n);
71 src += n;
72
73 if (!dctx->bytes) {
Martin Schwidefsky0177db02016-08-15 10:41:52 +020074 cpacf_kimd(CPACF_KIMD_GHASH, dctx, buf,
75 GHASH_BLOCK_SIZE);
Gerald Schaeferdf1309c2011-04-19 21:29:18 +020076 }
77 }
78
79 n = srclen & ~(GHASH_BLOCK_SIZE - 1);
80 if (n) {
Martin Schwidefsky0177db02016-08-15 10:41:52 +020081 cpacf_kimd(CPACF_KIMD_GHASH, dctx, src, n);
Gerald Schaeferdf1309c2011-04-19 21:29:18 +020082 src += n;
83 srclen -= n;
84 }
85
86 if (srclen) {
87 dctx->bytes = GHASH_BLOCK_SIZE - srclen;
88 memcpy(buf, src, srclen);
89 }
90
91 return 0;
92}
93
Harald Freudenbergera1cae342015-05-21 10:01:11 +020094static int ghash_flush(struct ghash_desc_ctx *dctx)
Gerald Schaeferdf1309c2011-04-19 21:29:18 +020095{
96 u8 *buf = dctx->buffer;
Gerald Schaeferdf1309c2011-04-19 21:29:18 +020097
98 if (dctx->bytes) {
99 u8 *pos = buf + (GHASH_BLOCK_SIZE - dctx->bytes);
100
101 memset(pos, 0, dctx->bytes);
Martin Schwidefsky0177db02016-08-15 10:41:52 +0200102 cpacf_kimd(CPACF_KIMD_GHASH, dctx, buf, GHASH_BLOCK_SIZE);
Harald Freudenbergera1cae342015-05-21 10:01:11 +0200103 dctx->bytes = 0;
Gerald Schaeferdf1309c2011-04-19 21:29:18 +0200104 }
105
Jan Glauber36eb2ca2012-10-26 15:06:12 +0200106 return 0;
Gerald Schaeferdf1309c2011-04-19 21:29:18 +0200107}
108
109static int ghash_final(struct shash_desc *desc, u8 *dst)
110{
111 struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
Jan Glauber36eb2ca2012-10-26 15:06:12 +0200112 int ret;
Gerald Schaeferdf1309c2011-04-19 21:29:18 +0200113
Harald Freudenbergera1cae342015-05-21 10:01:11 +0200114 ret = ghash_flush(dctx);
Jan Glauber36eb2ca2012-10-26 15:06:12 +0200115 if (!ret)
Harald Freudenbergera1cae342015-05-21 10:01:11 +0200116 memcpy(dst, dctx->icv, GHASH_BLOCK_SIZE);
Jan Glauber36eb2ca2012-10-26 15:06:12 +0200117 return ret;
Gerald Schaeferdf1309c2011-04-19 21:29:18 +0200118}
119
120static struct shash_alg ghash_alg = {
121 .digestsize = GHASH_DIGEST_SIZE,
122 .init = ghash_init,
123 .update = ghash_update,
124 .final = ghash_final,
125 .setkey = ghash_setkey,
126 .descsize = sizeof(struct ghash_desc_ctx),
127 .base = {
128 .cra_name = "ghash",
129 .cra_driver_name = "ghash-s390",
Martin Schwidefskyc7d4d252016-03-17 15:22:12 +0100130 .cra_priority = 300,
Gerald Schaeferdf1309c2011-04-19 21:29:18 +0200131 .cra_blocksize = GHASH_BLOCK_SIZE,
132 .cra_ctxsize = sizeof(struct ghash_ctx),
133 .cra_module = THIS_MODULE,
Gerald Schaeferdf1309c2011-04-19 21:29:18 +0200134 },
135};
136
137static int __init ghash_mod_init(void)
138{
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200139 if (!cpacf_query_func(CPACF_KIMD, CPACF_KIMD_GHASH))
Gerald Schaeferdf1309c2011-04-19 21:29:18 +0200140 return -EOPNOTSUPP;
141
142 return crypto_register_shash(&ghash_alg);
143}
144
145static void __exit ghash_mod_exit(void)
146{
147 crypto_unregister_shash(&ghash_alg);
148}
149
Hendrik Bruecknerd05377c2015-02-19 17:34:07 +0100150module_cpu_feature_match(MSA, ghash_mod_init);
Gerald Schaeferdf1309c2011-04-19 21:29:18 +0200151module_exit(ghash_mod_exit);
152
Kees Cook5d26a102014-11-20 17:05:53 -0800153MODULE_ALIAS_CRYPTO("ghash");
Gerald Schaeferdf1309c2011-04-19 21:29:18 +0200154
155MODULE_LICENSE("GPL");
156MODULE_DESCRIPTION("GHASH Message Digest Algorithm, s390 implementation");