Commit | Line | Data |
---|---|---|
20a884f5 | 1 | // SPDX-License-Identifier: GPL-2.0 |
df1309ce GS |
2 | /* |
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> | |
d05377c1 | 13 | #include <linux/cpufeature.h> |
c7d4d259 | 14 | #include <asm/cpacf.h> |
df1309ce GS |
15 | |
16 | #define GHASH_BLOCK_SIZE 16 | |
17 | #define GHASH_DIGEST_SIZE 16 | |
18 | ||
19 | struct ghash_ctx { | |
a1cae34e | 20 | u8 key[GHASH_BLOCK_SIZE]; |
df1309ce GS |
21 | }; |
22 | ||
23 | struct ghash_desc_ctx { | |
a1cae34e HF |
24 | u8 icv[GHASH_BLOCK_SIZE]; |
25 | u8 key[GHASH_BLOCK_SIZE]; | |
df1309ce GS |
26 | u8 buffer[GHASH_BLOCK_SIZE]; |
27 | u32 bytes; | |
28 | }; | |
29 | ||
30 | static int ghash_init(struct shash_desc *desc) | |
31 | { | |
32 | struct ghash_desc_ctx *dctx = shash_desc_ctx(desc); | |
a1cae34e | 33 | struct ghash_ctx *ctx = crypto_shash_ctx(desc->tfm); |
df1309ce GS |
34 | |
35 | memset(dctx, 0, sizeof(*dctx)); | |
a1cae34e | 36 | memcpy(dctx->key, ctx->key, GHASH_BLOCK_SIZE); |
df1309ce GS |
37 | |
38 | return 0; | |
39 | } | |
40 | ||
41 | static 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 | ||
674f368a | 46 | if (keylen != GHASH_BLOCK_SIZE) |
df1309ce | 47 | return -EINVAL; |
df1309ce GS |
48 | |
49 | memcpy(ctx->key, key, GHASH_BLOCK_SIZE); | |
df1309ce GS |
50 | |
51 | return 0; | |
52 | } | |
53 | ||
54 | static int ghash_update(struct shash_desc *desc, | |
55 | const u8 *src, unsigned int srclen) | |
56 | { | |
57 | struct ghash_desc_ctx *dctx = shash_desc_ctx(desc); | |
df1309ce GS |
58 | unsigned int n; |
59 | u8 *buf = dctx->buffer; | |
df1309ce GS |
60 | |
61 | if (dctx->bytes) { | |
62 | u8 *pos = buf + (GHASH_BLOCK_SIZE - dctx->bytes); | |
63 | ||
64 | n = min(srclen, dctx->bytes); | |
65 | dctx->bytes -= n; | |
66 | srclen -= n; | |
67 | ||
68 | memcpy(pos, src, n); | |
69 | src += n; | |
70 | ||
71 | if (!dctx->bytes) { | |
0177db01 MS |
72 | cpacf_kimd(CPACF_KIMD_GHASH, dctx, buf, |
73 | GHASH_BLOCK_SIZE); | |
df1309ce GS |
74 | } |
75 | } | |
76 | ||
77 | n = srclen & ~(GHASH_BLOCK_SIZE - 1); | |
78 | if (n) { | |
0177db01 | 79 | cpacf_kimd(CPACF_KIMD_GHASH, dctx, src, n); |
df1309ce GS |
80 | src += n; |
81 | srclen -= n; | |
82 | } | |
83 | ||
84 | if (srclen) { | |
85 | dctx->bytes = GHASH_BLOCK_SIZE - srclen; | |
86 | memcpy(buf, src, srclen); | |
87 | } | |
88 | ||
89 | return 0; | |
90 | } | |
91 | ||
a1cae34e | 92 | static int ghash_flush(struct ghash_desc_ctx *dctx) |
df1309ce GS |
93 | { |
94 | u8 *buf = dctx->buffer; | |
df1309ce GS |
95 | |
96 | if (dctx->bytes) { | |
97 | u8 *pos = buf + (GHASH_BLOCK_SIZE - dctx->bytes); | |
98 | ||
99 | memset(pos, 0, dctx->bytes); | |
0177db01 | 100 | cpacf_kimd(CPACF_KIMD_GHASH, dctx, buf, GHASH_BLOCK_SIZE); |
a1cae34e | 101 | dctx->bytes = 0; |
df1309ce GS |
102 | } |
103 | ||
36eb2caa | 104 | return 0; |
df1309ce GS |
105 | } |
106 | ||
107 | static int ghash_final(struct shash_desc *desc, u8 *dst) | |
108 | { | |
109 | struct ghash_desc_ctx *dctx = shash_desc_ctx(desc); | |
36eb2caa | 110 | int ret; |
df1309ce | 111 | |
a1cae34e | 112 | ret = ghash_flush(dctx); |
36eb2caa | 113 | if (!ret) |
a1cae34e | 114 | memcpy(dst, dctx->icv, GHASH_BLOCK_SIZE); |
36eb2caa | 115 | return ret; |
df1309ce GS |
116 | } |
117 | ||
118 | static struct shash_alg ghash_alg = { | |
119 | .digestsize = GHASH_DIGEST_SIZE, | |
120 | .init = ghash_init, | |
121 | .update = ghash_update, | |
122 | .final = ghash_final, | |
123 | .setkey = ghash_setkey, | |
124 | .descsize = sizeof(struct ghash_desc_ctx), | |
125 | .base = { | |
126 | .cra_name = "ghash", | |
127 | .cra_driver_name = "ghash-s390", | |
c7d4d259 | 128 | .cra_priority = 300, |
df1309ce GS |
129 | .cra_blocksize = GHASH_BLOCK_SIZE, |
130 | .cra_ctxsize = sizeof(struct ghash_ctx), | |
131 | .cra_module = THIS_MODULE, | |
df1309ce GS |
132 | }, |
133 | }; | |
134 | ||
135 | static int __init ghash_mod_init(void) | |
136 | { | |
69c0e360 | 137 | if (!cpacf_query_func(CPACF_KIMD, CPACF_KIMD_GHASH)) |
1c0908fc | 138 | return -ENODEV; |
df1309ce GS |
139 | |
140 | return crypto_register_shash(&ghash_alg); | |
141 | } | |
142 | ||
143 | static void __exit ghash_mod_exit(void) | |
144 | { | |
145 | crypto_unregister_shash(&ghash_alg); | |
146 | } | |
147 | ||
d05377c1 | 148 | module_cpu_feature_match(MSA, ghash_mod_init); |
df1309ce GS |
149 | module_exit(ghash_mod_exit); |
150 | ||
5d26a105 | 151 | MODULE_ALIAS_CRYPTO("ghash"); |
df1309ce GS |
152 | |
153 | MODULE_LICENSE("GPL"); | |
8dfa20fc | 154 | MODULE_DESCRIPTION("GHASH hash function, s390 implementation"); |