blob: ad47abd086308d3040c12807c54174f84e2e35d6 [file] [log] [blame]
Greg Kroah-Hartman20a884f2017-11-24 15:00:34 +01001// SPDX-License-Identifier: GPL-2.0+
Jan Glauberbf754ae2006-01-06 00:19:18 -08002/*
3 * Cryptographic API.
4 *
5 * s390 implementation of the AES Cipher Algorithm.
6 *
7 * s390 Version:
Harald Freudenbergerbf7fa032017-09-18 12:48:09 +02008 * Copyright IBM Corp. 2005, 2017
Jan Glauberbf754ae2006-01-06 00:19:18 -08009 * Author(s): Jan Glauber (jang@de.ibm.com)
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +110010 * Sebastian Siewior (sebastian@breakpoint.cc> SW-Fallback
Harald Freudenbergerbf7fa032017-09-18 12:48:09 +020011 * Patrick Steuer <patrick.steuer@de.ibm.com>
12 * Harald Freudenberger <freude@de.ibm.com>
Jan Glauberbf754ae2006-01-06 00:19:18 -080013 *
Sebastian Siewiorf8246af2007-10-05 16:52:01 +080014 * Derived from "crypto/aes_generic.c"
Jan Glauberbf754ae2006-01-06 00:19:18 -080015 */
16
Jan Glauber39f09392008-12-25 13:39:37 +010017#define KMSG_COMPONENT "aes_s390"
18#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
19
Sebastian Siewior89e126542007-10-17 23:18:57 +080020#include <crypto/aes.h>
Herbert Xua9e62fa2006-08-21 21:39:24 +100021#include <crypto/algapi.h>
Harald Freudenbergerbf7fa032017-09-18 12:48:09 +020022#include <crypto/ghash.h>
23#include <crypto/internal/aead.h>
Herbert Xu64e26802016-06-29 18:04:07 +080024#include <crypto/internal/skcipher.h>
Harald Freudenbergerbf7fa032017-09-18 12:48:09 +020025#include <crypto/scatterwalk.h>
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +110026#include <linux/err.h>
Jan Glauberbf754ae2006-01-06 00:19:18 -080027#include <linux/module.h>
Hendrik Bruecknerd05377c2015-02-19 17:34:07 +010028#include <linux/cpufeature.h>
Jan Glauberbf754ae2006-01-06 00:19:18 -080029#include <linux/init.h>
Harald Freudenberger0519e9a2014-01-16 16:01:11 +010030#include <linux/spinlock.h>
Harald Freudenbergera4f27792016-12-15 14:58:08 +010031#include <linux/fips.h>
Harald Freudenbergerbf7fa032017-09-18 12:48:09 +020032#include <linux/string.h>
Stephan Mueller49abc0d2016-02-17 07:00:01 +010033#include <crypto/xts.h>
Martin Schwidefskyc7d4d252016-03-17 15:22:12 +010034#include <asm/cpacf.h>
Jan Glauberbf754ae2006-01-06 00:19:18 -080035
Gerald Schaefer0200f3e2011-05-04 15:09:44 +100036static u8 *ctrblk;
Harald Freudenberger0519e9a2014-01-16 16:01:11 +010037static DEFINE_SPINLOCK(ctrblk_lock);
Martin Schwidefsky69c0e362016-08-18 12:59:46 +020038
Harald Freudenbergerbf7fa032017-09-18 12:48:09 +020039static cpacf_mask_t km_functions, kmc_functions, kmctr_functions,
40 kma_functions;
Jan Glauberbf754ae2006-01-06 00:19:18 -080041
42struct s390_aes_ctx {
Jan Glauberbf754ae2006-01-06 00:19:18 -080043 u8 key[AES_MAX_KEY_SIZE];
44 int key_len;
Martin Schwidefskyedc63a32016-08-15 09:19:16 +020045 unsigned long fc;
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +110046 union {
Herbert Xu64e26802016-06-29 18:04:07 +080047 struct crypto_skcipher *blk;
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +110048 struct crypto_cipher *cip;
49 } fallback;
Jan Glauberbf754ae2006-01-06 00:19:18 -080050};
51
Gerald Schaefer99d97222011-04-26 16:12:42 +100052struct s390_xts_ctx {
53 u8 key[32];
Gerald Schaefer9dda2762013-11-19 17:12:47 +010054 u8 pcc_key[32];
Gerald Schaefer99d97222011-04-26 16:12:42 +100055 int key_len;
Martin Schwidefskyedc63a32016-08-15 09:19:16 +020056 unsigned long fc;
Herbert Xu64e26802016-06-29 18:04:07 +080057 struct crypto_skcipher *fallback;
Gerald Schaefer99d97222011-04-26 16:12:42 +100058};
59
Harald Freudenbergerbf7fa032017-09-18 12:48:09 +020060struct gcm_sg_walk {
61 struct scatter_walk walk;
62 unsigned int walk_bytes;
63 u8 *walk_ptr;
64 unsigned int walk_bytes_remain;
65 u8 buf[AES_BLOCK_SIZE];
66 unsigned int buf_bytes;
67 u8 *ptr;
68 unsigned int nbytes;
69};
70
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +110071static int setkey_fallback_cip(struct crypto_tfm *tfm, const u8 *in_key,
72 unsigned int key_len)
73{
74 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
75 int ret;
76
Roel Kluind7ac7692010-01-08 14:18:34 +110077 sctx->fallback.cip->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK;
78 sctx->fallback.cip->base.crt_flags |= (tfm->crt_flags &
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +110079 CRYPTO_TFM_REQ_MASK);
80
81 ret = crypto_cipher_setkey(sctx->fallback.cip, in_key, key_len);
82 if (ret) {
83 tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
Roel Kluind7ac7692010-01-08 14:18:34 +110084 tfm->crt_flags |= (sctx->fallback.cip->base.crt_flags &
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +110085 CRYPTO_TFM_RES_MASK);
86 }
87 return ret;
88}
89
Herbert Xu6c2bb982006-05-16 22:09:29 +100090static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
Herbert Xu560c06a2006-08-13 14:16:39 +100091 unsigned int key_len)
Jan Glauberbf754ae2006-01-06 00:19:18 -080092{
Herbert Xu6c2bb982006-05-16 22:09:29 +100093 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
Martin Schwidefsky69c0e362016-08-18 12:59:46 +020094 unsigned long fc;
Jan Glauberbf754ae2006-01-06 00:19:18 -080095
Martin Schwidefsky69c0e362016-08-18 12:59:46 +020096 /* Pick the correct function code based on the key length */
97 fc = (key_len == 16) ? CPACF_KM_AES_128 :
98 (key_len == 24) ? CPACF_KM_AES_192 :
99 (key_len == 32) ? CPACF_KM_AES_256 : 0;
100
101 /* Check if the function code is available */
102 sctx->fc = (fc && cpacf_test_func(&km_functions, fc)) ? fc : 0;
103 if (!sctx->fc)
104 return setkey_fallback_cip(tfm, in_key, key_len);
Jan Glauberbf754ae2006-01-06 00:19:18 -0800105
106 sctx->key_len = key_len;
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200107 memcpy(sctx->key, in_key, key_len);
108 return 0;
Jan Glauberbf754ae2006-01-06 00:19:18 -0800109}
110
Herbert Xu6c2bb982006-05-16 22:09:29 +1000111static void aes_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
Jan Glauberbf754ae2006-01-06 00:19:18 -0800112{
Chen Gange6a67ad2015-01-01 22:56:02 +0800113 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
Jan Glauberbf754ae2006-01-06 00:19:18 -0800114
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200115 if (unlikely(!sctx->fc)) {
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100116 crypto_cipher_encrypt_one(sctx->fallback.cip, out, in);
117 return;
118 }
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200119 cpacf_km(sctx->fc, &sctx->key, out, in, AES_BLOCK_SIZE);
Jan Glauberbf754ae2006-01-06 00:19:18 -0800120}
121
Herbert Xu6c2bb982006-05-16 22:09:29 +1000122static void aes_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
Jan Glauberbf754ae2006-01-06 00:19:18 -0800123{
Chen Gange6a67ad2015-01-01 22:56:02 +0800124 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
Jan Glauberbf754ae2006-01-06 00:19:18 -0800125
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200126 if (unlikely(!sctx->fc)) {
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100127 crypto_cipher_decrypt_one(sctx->fallback.cip, out, in);
128 return;
129 }
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200130 cpacf_km(sctx->fc | CPACF_DECRYPT,
131 &sctx->key, out, in, AES_BLOCK_SIZE);
Jan Glauberbf754ae2006-01-06 00:19:18 -0800132}
133
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100134static int fallback_init_cip(struct crypto_tfm *tfm)
135{
136 const char *name = tfm->__crt_alg->cra_name;
137 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
138
139 sctx->fallback.cip = crypto_alloc_cipher(name, 0,
140 CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK);
141
142 if (IS_ERR(sctx->fallback.cip)) {
Jan Glauber39f09392008-12-25 13:39:37 +0100143 pr_err("Allocating AES fallback algorithm %s failed\n",
144 name);
Roel Kluinb59cdcb32009-12-18 17:43:18 +0100145 return PTR_ERR(sctx->fallback.cip);
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100146 }
147
148 return 0;
149}
150
151static void fallback_exit_cip(struct crypto_tfm *tfm)
152{
153 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
154
155 crypto_free_cipher(sctx->fallback.cip);
156 sctx->fallback.cip = NULL;
157}
Jan Glauberbf754ae2006-01-06 00:19:18 -0800158
159static struct crypto_alg aes_alg = {
160 .cra_name = "aes",
Herbert Xu65b75c32006-08-21 21:18:50 +1000161 .cra_driver_name = "aes-s390",
Martin Schwidefskyc7d4d252016-03-17 15:22:12 +0100162 .cra_priority = 300,
Jan Glauberf67d1362007-05-04 18:47:47 +0200163 .cra_flags = CRYPTO_ALG_TYPE_CIPHER |
164 CRYPTO_ALG_NEED_FALLBACK,
Jan Glauberbf754ae2006-01-06 00:19:18 -0800165 .cra_blocksize = AES_BLOCK_SIZE,
166 .cra_ctxsize = sizeof(struct s390_aes_ctx),
167 .cra_module = THIS_MODULE,
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100168 .cra_init = fallback_init_cip,
169 .cra_exit = fallback_exit_cip,
Jan Glauberbf754ae2006-01-06 00:19:18 -0800170 .cra_u = {
171 .cipher = {
172 .cia_min_keysize = AES_MIN_KEY_SIZE,
173 .cia_max_keysize = AES_MAX_KEY_SIZE,
174 .cia_setkey = aes_set_key,
175 .cia_encrypt = aes_encrypt,
176 .cia_decrypt = aes_decrypt,
Jan Glauberbf754ae2006-01-06 00:19:18 -0800177 }
178 }
179};
180
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100181static int setkey_fallback_blk(struct crypto_tfm *tfm, const u8 *key,
182 unsigned int len)
183{
184 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
185 unsigned int ret;
186
Herbert Xu64e26802016-06-29 18:04:07 +0800187 crypto_skcipher_clear_flags(sctx->fallback.blk, CRYPTO_TFM_REQ_MASK);
188 crypto_skcipher_set_flags(sctx->fallback.blk, tfm->crt_flags &
189 CRYPTO_TFM_REQ_MASK);
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100190
Herbert Xu64e26802016-06-29 18:04:07 +0800191 ret = crypto_skcipher_setkey(sctx->fallback.blk, key, len);
192
193 tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
194 tfm->crt_flags |= crypto_skcipher_get_flags(sctx->fallback.blk) &
195 CRYPTO_TFM_RES_MASK;
196
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100197 return ret;
198}
199
200static int fallback_blk_dec(struct blkcipher_desc *desc,
201 struct scatterlist *dst, struct scatterlist *src,
202 unsigned int nbytes)
203{
204 unsigned int ret;
Herbert Xu64e26802016-06-29 18:04:07 +0800205 struct crypto_blkcipher *tfm = desc->tfm;
206 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(tfm);
207 SKCIPHER_REQUEST_ON_STACK(req, sctx->fallback.blk);
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100208
Herbert Xu64e26802016-06-29 18:04:07 +0800209 skcipher_request_set_tfm(req, sctx->fallback.blk);
210 skcipher_request_set_callback(req, desc->flags, NULL, NULL);
211 skcipher_request_set_crypt(req, src, dst, nbytes, desc->info);
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100212
Herbert Xu64e26802016-06-29 18:04:07 +0800213 ret = crypto_skcipher_decrypt(req);
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100214
Herbert Xu64e26802016-06-29 18:04:07 +0800215 skcipher_request_zero(req);
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100216 return ret;
217}
218
219static int fallback_blk_enc(struct blkcipher_desc *desc,
220 struct scatterlist *dst, struct scatterlist *src,
221 unsigned int nbytes)
222{
223 unsigned int ret;
Herbert Xu64e26802016-06-29 18:04:07 +0800224 struct crypto_blkcipher *tfm = desc->tfm;
225 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(tfm);
226 SKCIPHER_REQUEST_ON_STACK(req, sctx->fallback.blk);
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100227
Herbert Xu64e26802016-06-29 18:04:07 +0800228 skcipher_request_set_tfm(req, sctx->fallback.blk);
229 skcipher_request_set_callback(req, desc->flags, NULL, NULL);
230 skcipher_request_set_crypt(req, src, dst, nbytes, desc->info);
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100231
Herbert Xu64e26802016-06-29 18:04:07 +0800232 ret = crypto_skcipher_encrypt(req);
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100233 return ret;
234}
235
Herbert Xua9e62fa2006-08-21 21:39:24 +1000236static int ecb_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
237 unsigned int key_len)
238{
239 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200240 unsigned long fc;
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100241
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200242 /* Pick the correct function code based on the key length */
243 fc = (key_len == 16) ? CPACF_KM_AES_128 :
244 (key_len == 24) ? CPACF_KM_AES_192 :
245 (key_len == 32) ? CPACF_KM_AES_256 : 0;
246
247 /* Check if the function code is available */
248 sctx->fc = (fc && cpacf_test_func(&km_functions, fc)) ? fc : 0;
249 if (!sctx->fc)
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100250 return setkey_fallback_blk(tfm, in_key, key_len);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000251
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200252 sctx->key_len = key_len;
253 memcpy(sctx->key, in_key, key_len);
254 return 0;
Herbert Xua9e62fa2006-08-21 21:39:24 +1000255}
256
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200257static int ecb_aes_crypt(struct blkcipher_desc *desc, unsigned long modifier,
Herbert Xua9e62fa2006-08-21 21:39:24 +1000258 struct blkcipher_walk *walk)
259{
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200260 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
261 unsigned int nbytes, n;
262 int ret;
Herbert Xua9e62fa2006-08-21 21:39:24 +1000263
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200264 ret = blkcipher_walk_virt(desc, walk);
265 while ((nbytes = walk->nbytes) >= AES_BLOCK_SIZE) {
Herbert Xua9e62fa2006-08-21 21:39:24 +1000266 /* only use complete blocks */
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200267 n = nbytes & ~(AES_BLOCK_SIZE - 1);
268 cpacf_km(sctx->fc | modifier, sctx->key,
269 walk->dst.virt.addr, walk->src.virt.addr, n);
270 ret = blkcipher_walk_done(desc, walk, nbytes - n);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000271 }
272
273 return ret;
274}
275
276static int ecb_aes_encrypt(struct blkcipher_desc *desc,
277 struct scatterlist *dst, struct scatterlist *src,
278 unsigned int nbytes)
279{
280 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
281 struct blkcipher_walk walk;
282
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200283 if (unlikely(!sctx->fc))
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100284 return fallback_blk_enc(desc, dst, src, nbytes);
285
Herbert Xua9e62fa2006-08-21 21:39:24 +1000286 blkcipher_walk_init(&walk, dst, src, nbytes);
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200287 return ecb_aes_crypt(desc, 0, &walk);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000288}
289
290static int ecb_aes_decrypt(struct blkcipher_desc *desc,
291 struct scatterlist *dst, struct scatterlist *src,
292 unsigned int nbytes)
293{
294 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
295 struct blkcipher_walk walk;
296
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200297 if (unlikely(!sctx->fc))
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100298 return fallback_blk_dec(desc, dst, src, nbytes);
299
Herbert Xua9e62fa2006-08-21 21:39:24 +1000300 blkcipher_walk_init(&walk, dst, src, nbytes);
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200301 return ecb_aes_crypt(desc, CPACF_DECRYPT, &walk);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000302}
303
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100304static int fallback_init_blk(struct crypto_tfm *tfm)
305{
306 const char *name = tfm->__crt_alg->cra_name;
307 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
308
Herbert Xu64e26802016-06-29 18:04:07 +0800309 sctx->fallback.blk = crypto_alloc_skcipher(name, 0,
310 CRYPTO_ALG_ASYNC |
311 CRYPTO_ALG_NEED_FALLBACK);
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100312
313 if (IS_ERR(sctx->fallback.blk)) {
Jan Glauber39f09392008-12-25 13:39:37 +0100314 pr_err("Allocating AES fallback algorithm %s failed\n",
315 name);
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100316 return PTR_ERR(sctx->fallback.blk);
317 }
318
319 return 0;
320}
321
322static void fallback_exit_blk(struct crypto_tfm *tfm)
323{
324 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
325
Herbert Xu64e26802016-06-29 18:04:07 +0800326 crypto_free_skcipher(sctx->fallback.blk);
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100327}
328
Herbert Xua9e62fa2006-08-21 21:39:24 +1000329static struct crypto_alg ecb_aes_alg = {
330 .cra_name = "ecb(aes)",
331 .cra_driver_name = "ecb-aes-s390",
Harald Freudenbergeraff304e2018-04-05 20:27:47 +0200332 .cra_priority = 401, /* combo: aes + ecb + 1 */
Jan Glauberf67d1362007-05-04 18:47:47 +0200333 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
334 CRYPTO_ALG_NEED_FALLBACK,
Herbert Xua9e62fa2006-08-21 21:39:24 +1000335 .cra_blocksize = AES_BLOCK_SIZE,
336 .cra_ctxsize = sizeof(struct s390_aes_ctx),
337 .cra_type = &crypto_blkcipher_type,
338 .cra_module = THIS_MODULE,
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100339 .cra_init = fallback_init_blk,
340 .cra_exit = fallback_exit_blk,
Herbert Xua9e62fa2006-08-21 21:39:24 +1000341 .cra_u = {
342 .blkcipher = {
343 .min_keysize = AES_MIN_KEY_SIZE,
344 .max_keysize = AES_MAX_KEY_SIZE,
345 .setkey = ecb_aes_set_key,
346 .encrypt = ecb_aes_encrypt,
347 .decrypt = ecb_aes_decrypt,
348 }
349 }
350};
351
352static int cbc_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
353 unsigned int key_len)
354{
355 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200356 unsigned long fc;
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100357
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200358 /* Pick the correct function code based on the key length */
359 fc = (key_len == 16) ? CPACF_KMC_AES_128 :
360 (key_len == 24) ? CPACF_KMC_AES_192 :
361 (key_len == 32) ? CPACF_KMC_AES_256 : 0;
362
363 /* Check if the function code is available */
364 sctx->fc = (fc && cpacf_test_func(&kmc_functions, fc)) ? fc : 0;
365 if (!sctx->fc)
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100366 return setkey_fallback_blk(tfm, in_key, key_len);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000367
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200368 sctx->key_len = key_len;
369 memcpy(sctx->key, in_key, key_len);
370 return 0;
Herbert Xua9e62fa2006-08-21 21:39:24 +1000371}
372
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200373static int cbc_aes_crypt(struct blkcipher_desc *desc, unsigned long modifier,
Herbert Xua9e62fa2006-08-21 21:39:24 +1000374 struct blkcipher_walk *walk)
375{
Herbert Xuf262f0f2013-11-05 19:36:27 +0800376 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200377 unsigned int nbytes, n;
378 int ret;
Herbert Xuf262f0f2013-11-05 19:36:27 +0800379 struct {
380 u8 iv[AES_BLOCK_SIZE];
381 u8 key[AES_MAX_KEY_SIZE];
382 } param;
Herbert Xua9e62fa2006-08-21 21:39:24 +1000383
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200384 ret = blkcipher_walk_virt(desc, walk);
Herbert Xuf262f0f2013-11-05 19:36:27 +0800385 memcpy(param.iv, walk->iv, AES_BLOCK_SIZE);
386 memcpy(param.key, sctx->key, sctx->key_len);
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200387 while ((nbytes = walk->nbytes) >= AES_BLOCK_SIZE) {
Herbert Xua9e62fa2006-08-21 21:39:24 +1000388 /* only use complete blocks */
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200389 n = nbytes & ~(AES_BLOCK_SIZE - 1);
390 cpacf_kmc(sctx->fc | modifier, &param,
391 walk->dst.virt.addr, walk->src.virt.addr, n);
392 ret = blkcipher_walk_done(desc, walk, nbytes - n);
393 }
Herbert Xuf262f0f2013-11-05 19:36:27 +0800394 memcpy(walk->iv, param.iv, AES_BLOCK_SIZE);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000395 return ret;
396}
397
398static int cbc_aes_encrypt(struct blkcipher_desc *desc,
399 struct scatterlist *dst, struct scatterlist *src,
400 unsigned int nbytes)
401{
402 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
403 struct blkcipher_walk walk;
404
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200405 if (unlikely(!sctx->fc))
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100406 return fallback_blk_enc(desc, dst, src, nbytes);
407
Herbert Xua9e62fa2006-08-21 21:39:24 +1000408 blkcipher_walk_init(&walk, dst, src, nbytes);
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200409 return cbc_aes_crypt(desc, 0, &walk);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000410}
411
412static int cbc_aes_decrypt(struct blkcipher_desc *desc,
413 struct scatterlist *dst, struct scatterlist *src,
414 unsigned int nbytes)
415{
416 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
417 struct blkcipher_walk walk;
418
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200419 if (unlikely(!sctx->fc))
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100420 return fallback_blk_dec(desc, dst, src, nbytes);
421
Herbert Xua9e62fa2006-08-21 21:39:24 +1000422 blkcipher_walk_init(&walk, dst, src, nbytes);
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200423 return cbc_aes_crypt(desc, CPACF_DECRYPT, &walk);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000424}
425
426static struct crypto_alg cbc_aes_alg = {
427 .cra_name = "cbc(aes)",
428 .cra_driver_name = "cbc-aes-s390",
Harald Freudenbergeraff304e2018-04-05 20:27:47 +0200429 .cra_priority = 402, /* ecb-aes-s390 + 1 */
Jan Glauberf67d1362007-05-04 18:47:47 +0200430 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
431 CRYPTO_ALG_NEED_FALLBACK,
Herbert Xua9e62fa2006-08-21 21:39:24 +1000432 .cra_blocksize = AES_BLOCK_SIZE,
433 .cra_ctxsize = sizeof(struct s390_aes_ctx),
434 .cra_type = &crypto_blkcipher_type,
435 .cra_module = THIS_MODULE,
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100436 .cra_init = fallback_init_blk,
437 .cra_exit = fallback_exit_blk,
Herbert Xua9e62fa2006-08-21 21:39:24 +1000438 .cra_u = {
439 .blkcipher = {
440 .min_keysize = AES_MIN_KEY_SIZE,
441 .max_keysize = AES_MAX_KEY_SIZE,
442 .ivsize = AES_BLOCK_SIZE,
443 .setkey = cbc_aes_set_key,
444 .encrypt = cbc_aes_encrypt,
445 .decrypt = cbc_aes_decrypt,
446 }
447 }
448};
449
Gerald Schaefer99d97222011-04-26 16:12:42 +1000450static int xts_fallback_setkey(struct crypto_tfm *tfm, const u8 *key,
451 unsigned int len)
452{
453 struct s390_xts_ctx *xts_ctx = crypto_tfm_ctx(tfm);
454 unsigned int ret;
455
Herbert Xu64e26802016-06-29 18:04:07 +0800456 crypto_skcipher_clear_flags(xts_ctx->fallback, CRYPTO_TFM_REQ_MASK);
457 crypto_skcipher_set_flags(xts_ctx->fallback, tfm->crt_flags &
458 CRYPTO_TFM_REQ_MASK);
Gerald Schaefer99d97222011-04-26 16:12:42 +1000459
Herbert Xu64e26802016-06-29 18:04:07 +0800460 ret = crypto_skcipher_setkey(xts_ctx->fallback, key, len);
461
462 tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
463 tfm->crt_flags |= crypto_skcipher_get_flags(xts_ctx->fallback) &
464 CRYPTO_TFM_RES_MASK;
465
Gerald Schaefer99d97222011-04-26 16:12:42 +1000466 return ret;
467}
468
469static int xts_fallback_decrypt(struct blkcipher_desc *desc,
470 struct scatterlist *dst, struct scatterlist *src,
471 unsigned int nbytes)
472{
Herbert Xu64e26802016-06-29 18:04:07 +0800473 struct crypto_blkcipher *tfm = desc->tfm;
474 struct s390_xts_ctx *xts_ctx = crypto_blkcipher_ctx(tfm);
475 SKCIPHER_REQUEST_ON_STACK(req, xts_ctx->fallback);
Gerald Schaefer99d97222011-04-26 16:12:42 +1000476 unsigned int ret;
477
Herbert Xu64e26802016-06-29 18:04:07 +0800478 skcipher_request_set_tfm(req, xts_ctx->fallback);
479 skcipher_request_set_callback(req, desc->flags, NULL, NULL);
480 skcipher_request_set_crypt(req, src, dst, nbytes, desc->info);
Gerald Schaefer99d97222011-04-26 16:12:42 +1000481
Herbert Xu64e26802016-06-29 18:04:07 +0800482 ret = crypto_skcipher_decrypt(req);
Gerald Schaefer99d97222011-04-26 16:12:42 +1000483
Herbert Xu64e26802016-06-29 18:04:07 +0800484 skcipher_request_zero(req);
Gerald Schaefer99d97222011-04-26 16:12:42 +1000485 return ret;
486}
487
488static int xts_fallback_encrypt(struct blkcipher_desc *desc,
489 struct scatterlist *dst, struct scatterlist *src,
490 unsigned int nbytes)
491{
Herbert Xu64e26802016-06-29 18:04:07 +0800492 struct crypto_blkcipher *tfm = desc->tfm;
493 struct s390_xts_ctx *xts_ctx = crypto_blkcipher_ctx(tfm);
494 SKCIPHER_REQUEST_ON_STACK(req, xts_ctx->fallback);
Gerald Schaefer99d97222011-04-26 16:12:42 +1000495 unsigned int ret;
496
Herbert Xu64e26802016-06-29 18:04:07 +0800497 skcipher_request_set_tfm(req, xts_ctx->fallback);
498 skcipher_request_set_callback(req, desc->flags, NULL, NULL);
499 skcipher_request_set_crypt(req, src, dst, nbytes, desc->info);
Gerald Schaefer99d97222011-04-26 16:12:42 +1000500
Herbert Xu64e26802016-06-29 18:04:07 +0800501 ret = crypto_skcipher_encrypt(req);
Gerald Schaefer99d97222011-04-26 16:12:42 +1000502
Herbert Xu64e26802016-06-29 18:04:07 +0800503 skcipher_request_zero(req);
Gerald Schaefer99d97222011-04-26 16:12:42 +1000504 return ret;
505}
506
507static int xts_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
508 unsigned int key_len)
509{
510 struct s390_xts_ctx *xts_ctx = crypto_tfm_ctx(tfm);
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200511 unsigned long fc;
Stephan Mueller28856a92016-02-09 15:37:47 +0100512 int err;
513
514 err = xts_check_key(tfm, in_key, key_len);
515 if (err)
516 return err;
Gerald Schaefer99d97222011-04-26 16:12:42 +1000517
Harald Freudenbergera4f27792016-12-15 14:58:08 +0100518 /* In fips mode only 128 bit or 256 bit keys are valid */
519 if (fips_enabled && key_len != 32 && key_len != 64) {
520 tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
521 return -EINVAL;
522 }
523
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200524 /* Pick the correct function code based on the key length */
525 fc = (key_len == 32) ? CPACF_KM_XTS_128 :
526 (key_len == 64) ? CPACF_KM_XTS_256 : 0;
527
528 /* Check if the function code is available */
529 xts_ctx->fc = (fc && cpacf_test_func(&km_functions, fc)) ? fc : 0;
530 if (!xts_ctx->fc)
531 return xts_fallback_setkey(tfm, in_key, key_len);
532
533 /* Split the XTS key into the two subkeys */
534 key_len = key_len / 2;
Gerald Schaefer99d97222011-04-26 16:12:42 +1000535 xts_ctx->key_len = key_len;
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200536 memcpy(xts_ctx->key, in_key, key_len);
537 memcpy(xts_ctx->pcc_key, in_key + key_len, key_len);
Gerald Schaefer99d97222011-04-26 16:12:42 +1000538 return 0;
539}
540
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200541static int xts_aes_crypt(struct blkcipher_desc *desc, unsigned long modifier,
Gerald Schaefer99d97222011-04-26 16:12:42 +1000542 struct blkcipher_walk *walk)
543{
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200544 struct s390_xts_ctx *xts_ctx = crypto_blkcipher_ctx(desc->tfm);
545 unsigned int offset, nbytes, n;
546 int ret;
547 struct {
548 u8 key[32];
549 u8 tweak[16];
550 u8 block[16];
551 u8 bit[16];
552 u8 xts[16];
553 } pcc_param;
Gerald Schaefer9dda2762013-11-19 17:12:47 +0100554 struct {
555 u8 key[32];
556 u8 init[16];
557 } xts_param;
Gerald Schaefer99d97222011-04-26 16:12:42 +1000558
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200559 ret = blkcipher_walk_virt(desc, walk);
560 offset = xts_ctx->key_len & 0x10;
Gerald Schaefer9dda2762013-11-19 17:12:47 +0100561 memset(pcc_param.block, 0, sizeof(pcc_param.block));
562 memset(pcc_param.bit, 0, sizeof(pcc_param.bit));
563 memset(pcc_param.xts, 0, sizeof(pcc_param.xts));
564 memcpy(pcc_param.tweak, walk->iv, sizeof(pcc_param.tweak));
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200565 memcpy(pcc_param.key + offset, xts_ctx->pcc_key, xts_ctx->key_len);
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200566 cpacf_pcc(xts_ctx->fc, pcc_param.key + offset);
Gerald Schaefer99d97222011-04-26 16:12:42 +1000567
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200568 memcpy(xts_param.key + offset, xts_ctx->key, xts_ctx->key_len);
Gerald Schaefer9dda2762013-11-19 17:12:47 +0100569 memcpy(xts_param.init, pcc_param.xts, 16);
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200570
571 while ((nbytes = walk->nbytes) >= AES_BLOCK_SIZE) {
Gerald Schaefer99d97222011-04-26 16:12:42 +1000572 /* only use complete blocks */
573 n = nbytes & ~(AES_BLOCK_SIZE - 1);
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200574 cpacf_km(xts_ctx->fc | modifier, xts_param.key + offset,
575 walk->dst.virt.addr, walk->src.virt.addr, n);
576 ret = blkcipher_walk_done(desc, walk, nbytes - n);
577 }
Gerald Schaefer99d97222011-04-26 16:12:42 +1000578 return ret;
579}
580
581static int xts_aes_encrypt(struct blkcipher_desc *desc,
582 struct scatterlist *dst, struct scatterlist *src,
583 unsigned int nbytes)
584{
585 struct s390_xts_ctx *xts_ctx = crypto_blkcipher_ctx(desc->tfm);
586 struct blkcipher_walk walk;
587
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200588 if (unlikely(!xts_ctx->fc))
Gerald Schaefer99d97222011-04-26 16:12:42 +1000589 return xts_fallback_encrypt(desc, dst, src, nbytes);
590
591 blkcipher_walk_init(&walk, dst, src, nbytes);
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200592 return xts_aes_crypt(desc, 0, &walk);
Gerald Schaefer99d97222011-04-26 16:12:42 +1000593}
594
595static int xts_aes_decrypt(struct blkcipher_desc *desc,
596 struct scatterlist *dst, struct scatterlist *src,
597 unsigned int nbytes)
598{
599 struct s390_xts_ctx *xts_ctx = crypto_blkcipher_ctx(desc->tfm);
600 struct blkcipher_walk walk;
601
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200602 if (unlikely(!xts_ctx->fc))
Gerald Schaefer99d97222011-04-26 16:12:42 +1000603 return xts_fallback_decrypt(desc, dst, src, nbytes);
604
605 blkcipher_walk_init(&walk, dst, src, nbytes);
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200606 return xts_aes_crypt(desc, CPACF_DECRYPT, &walk);
Gerald Schaefer99d97222011-04-26 16:12:42 +1000607}
608
609static int xts_fallback_init(struct crypto_tfm *tfm)
610{
611 const char *name = tfm->__crt_alg->cra_name;
612 struct s390_xts_ctx *xts_ctx = crypto_tfm_ctx(tfm);
613
Herbert Xu64e26802016-06-29 18:04:07 +0800614 xts_ctx->fallback = crypto_alloc_skcipher(name, 0,
615 CRYPTO_ALG_ASYNC |
616 CRYPTO_ALG_NEED_FALLBACK);
Gerald Schaefer99d97222011-04-26 16:12:42 +1000617
618 if (IS_ERR(xts_ctx->fallback)) {
619 pr_err("Allocating XTS fallback algorithm %s failed\n",
620 name);
621 return PTR_ERR(xts_ctx->fallback);
622 }
623 return 0;
624}
625
626static void xts_fallback_exit(struct crypto_tfm *tfm)
627{
628 struct s390_xts_ctx *xts_ctx = crypto_tfm_ctx(tfm);
629
Herbert Xu64e26802016-06-29 18:04:07 +0800630 crypto_free_skcipher(xts_ctx->fallback);
Gerald Schaefer99d97222011-04-26 16:12:42 +1000631}
632
633static struct crypto_alg xts_aes_alg = {
634 .cra_name = "xts(aes)",
635 .cra_driver_name = "xts-aes-s390",
Harald Freudenbergeraff304e2018-04-05 20:27:47 +0200636 .cra_priority = 402, /* ecb-aes-s390 + 1 */
Gerald Schaefer99d97222011-04-26 16:12:42 +1000637 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
638 CRYPTO_ALG_NEED_FALLBACK,
639 .cra_blocksize = AES_BLOCK_SIZE,
640 .cra_ctxsize = sizeof(struct s390_xts_ctx),
641 .cra_type = &crypto_blkcipher_type,
642 .cra_module = THIS_MODULE,
Gerald Schaefer99d97222011-04-26 16:12:42 +1000643 .cra_init = xts_fallback_init,
644 .cra_exit = xts_fallback_exit,
645 .cra_u = {
646 .blkcipher = {
647 .min_keysize = 2 * AES_MIN_KEY_SIZE,
648 .max_keysize = 2 * AES_MAX_KEY_SIZE,
649 .ivsize = AES_BLOCK_SIZE,
650 .setkey = xts_aes_set_key,
651 .encrypt = xts_aes_encrypt,
652 .decrypt = xts_aes_decrypt,
653 }
654 }
655};
656
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000657static int ctr_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
658 unsigned int key_len)
659{
660 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200661 unsigned long fc;
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000662
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200663 /* Pick the correct function code based on the key length */
664 fc = (key_len == 16) ? CPACF_KMCTR_AES_128 :
665 (key_len == 24) ? CPACF_KMCTR_AES_192 :
666 (key_len == 32) ? CPACF_KMCTR_AES_256 : 0;
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000667
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200668 /* Check if the function code is available */
669 sctx->fc = (fc && cpacf_test_func(&kmctr_functions, fc)) ? fc : 0;
670 if (!sctx->fc)
671 return setkey_fallback_blk(tfm, in_key, key_len);
672
673 sctx->key_len = key_len;
674 memcpy(sctx->key, in_key, key_len);
675 return 0;
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000676}
677
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200678static unsigned int __ctrblk_init(u8 *ctrptr, u8 *iv, unsigned int nbytes)
Harald Freudenberger0519e9a2014-01-16 16:01:11 +0100679{
680 unsigned int i, n;
681
682 /* only use complete blocks, max. PAGE_SIZE */
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200683 memcpy(ctrptr, iv, AES_BLOCK_SIZE);
Harald Freudenberger0519e9a2014-01-16 16:01:11 +0100684 n = (nbytes > PAGE_SIZE) ? PAGE_SIZE : nbytes & ~(AES_BLOCK_SIZE - 1);
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200685 for (i = (n / AES_BLOCK_SIZE) - 1; i > 0; i--) {
686 memcpy(ctrptr + AES_BLOCK_SIZE, ctrptr, AES_BLOCK_SIZE);
687 crypto_inc(ctrptr + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
688 ctrptr += AES_BLOCK_SIZE;
Harald Freudenberger0519e9a2014-01-16 16:01:11 +0100689 }
690 return n;
691}
692
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200693static int ctr_aes_crypt(struct blkcipher_desc *desc, unsigned long modifier,
694 struct blkcipher_walk *walk)
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000695{
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200696 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
697 u8 buf[AES_BLOCK_SIZE], *ctrptr;
Harald Freudenberger0519e9a2014-01-16 16:01:11 +0100698 unsigned int n, nbytes;
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200699 int ret, locked;
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000700
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200701 locked = spin_trylock(&ctrblk_lock);
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000702
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200703 ret = blkcipher_walk_virt_block(desc, walk, AES_BLOCK_SIZE);
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000704 while ((nbytes = walk->nbytes) >= AES_BLOCK_SIZE) {
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200705 n = AES_BLOCK_SIZE;
706 if (nbytes >= 2*AES_BLOCK_SIZE && locked)
707 n = __ctrblk_init(ctrblk, walk->iv, nbytes);
708 ctrptr = (n > AES_BLOCK_SIZE) ? ctrblk : walk->iv;
709 cpacf_kmctr(sctx->fc | modifier, sctx->key,
710 walk->dst.virt.addr, walk->src.virt.addr,
711 n, ctrptr);
712 if (ctrptr == ctrblk)
713 memcpy(walk->iv, ctrptr + n - AES_BLOCK_SIZE,
714 AES_BLOCK_SIZE);
715 crypto_inc(walk->iv, AES_BLOCK_SIZE);
716 ret = blkcipher_walk_done(desc, walk, nbytes - n);
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000717 }
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200718 if (locked)
Harald Freudenberger0519e9a2014-01-16 16:01:11 +0100719 spin_unlock(&ctrblk_lock);
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000720 /*
721 * final block may be < AES_BLOCK_SIZE, copy only nbytes
722 */
723 if (nbytes) {
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200724 cpacf_kmctr(sctx->fc | modifier, sctx->key,
725 buf, walk->src.virt.addr,
726 AES_BLOCK_SIZE, walk->iv);
727 memcpy(walk->dst.virt.addr, buf, nbytes);
728 crypto_inc(walk->iv, AES_BLOCK_SIZE);
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000729 ret = blkcipher_walk_done(desc, walk, 0);
730 }
Harald Freudenberger0519e9a2014-01-16 16:01:11 +0100731
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000732 return ret;
733}
734
735static int ctr_aes_encrypt(struct blkcipher_desc *desc,
736 struct scatterlist *dst, struct scatterlist *src,
737 unsigned int nbytes)
738{
739 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
740 struct blkcipher_walk walk;
741
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200742 if (unlikely(!sctx->fc))
743 return fallback_blk_enc(desc, dst, src, nbytes);
744
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000745 blkcipher_walk_init(&walk, dst, src, nbytes);
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200746 return ctr_aes_crypt(desc, 0, &walk);
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000747}
748
749static int ctr_aes_decrypt(struct blkcipher_desc *desc,
750 struct scatterlist *dst, struct scatterlist *src,
751 unsigned int nbytes)
752{
753 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
754 struct blkcipher_walk walk;
755
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200756 if (unlikely(!sctx->fc))
757 return fallback_blk_dec(desc, dst, src, nbytes);
758
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000759 blkcipher_walk_init(&walk, dst, src, nbytes);
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200760 return ctr_aes_crypt(desc, CPACF_DECRYPT, &walk);
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000761}
762
763static struct crypto_alg ctr_aes_alg = {
764 .cra_name = "ctr(aes)",
765 .cra_driver_name = "ctr-aes-s390",
Harald Freudenbergeraff304e2018-04-05 20:27:47 +0200766 .cra_priority = 402, /* ecb-aes-s390 + 1 */
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200767 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
768 CRYPTO_ALG_NEED_FALLBACK,
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000769 .cra_blocksize = 1,
770 .cra_ctxsize = sizeof(struct s390_aes_ctx),
771 .cra_type = &crypto_blkcipher_type,
772 .cra_module = THIS_MODULE,
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200773 .cra_init = fallback_init_blk,
774 .cra_exit = fallback_exit_blk,
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000775 .cra_u = {
776 .blkcipher = {
777 .min_keysize = AES_MIN_KEY_SIZE,
778 .max_keysize = AES_MAX_KEY_SIZE,
779 .ivsize = AES_BLOCK_SIZE,
780 .setkey = ctr_aes_set_key,
781 .encrypt = ctr_aes_encrypt,
782 .decrypt = ctr_aes_decrypt,
783 }
784 }
785};
786
Harald Freudenbergerbf7fa032017-09-18 12:48:09 +0200787static int gcm_aes_setkey(struct crypto_aead *tfm, const u8 *key,
788 unsigned int keylen)
789{
790 struct s390_aes_ctx *ctx = crypto_aead_ctx(tfm);
791
792 switch (keylen) {
793 case AES_KEYSIZE_128:
794 ctx->fc = CPACF_KMA_GCM_AES_128;
795 break;
796 case AES_KEYSIZE_192:
797 ctx->fc = CPACF_KMA_GCM_AES_192;
798 break;
799 case AES_KEYSIZE_256:
800 ctx->fc = CPACF_KMA_GCM_AES_256;
801 break;
802 default:
803 return -EINVAL;
804 }
805
806 memcpy(ctx->key, key, keylen);
807 ctx->key_len = keylen;
808 return 0;
809}
810
811static int gcm_aes_setauthsize(struct crypto_aead *tfm, unsigned int authsize)
812{
813 switch (authsize) {
814 case 4:
815 case 8:
816 case 12:
817 case 13:
818 case 14:
819 case 15:
820 case 16:
821 break;
822 default:
823 return -EINVAL;
824 }
825
826 return 0;
827}
828
829static void gcm_sg_walk_start(struct gcm_sg_walk *gw, struct scatterlist *sg,
830 unsigned int len)
831{
832 memset(gw, 0, sizeof(*gw));
833 gw->walk_bytes_remain = len;
834 scatterwalk_start(&gw->walk, sg);
835}
836
837static int gcm_sg_walk_go(struct gcm_sg_walk *gw, unsigned int minbytesneeded)
838{
839 int n;
840
841 /* minbytesneeded <= AES_BLOCK_SIZE */
842 if (gw->buf_bytes && gw->buf_bytes >= minbytesneeded) {
843 gw->ptr = gw->buf;
844 gw->nbytes = gw->buf_bytes;
845 goto out;
846 }
847
848 if (gw->walk_bytes_remain == 0) {
849 gw->ptr = NULL;
850 gw->nbytes = 0;
851 goto out;
852 }
853
854 gw->walk_bytes = scatterwalk_clamp(&gw->walk, gw->walk_bytes_remain);
855 if (!gw->walk_bytes) {
856 scatterwalk_start(&gw->walk, sg_next(gw->walk.sg));
857 gw->walk_bytes = scatterwalk_clamp(&gw->walk,
858 gw->walk_bytes_remain);
859 }
860 gw->walk_ptr = scatterwalk_map(&gw->walk);
861
862 if (!gw->buf_bytes && gw->walk_bytes >= minbytesneeded) {
863 gw->ptr = gw->walk_ptr;
864 gw->nbytes = gw->walk_bytes;
865 goto out;
866 }
867
868 while (1) {
869 n = min(gw->walk_bytes, AES_BLOCK_SIZE - gw->buf_bytes);
870 memcpy(gw->buf + gw->buf_bytes, gw->walk_ptr, n);
871 gw->buf_bytes += n;
872 gw->walk_bytes_remain -= n;
873 scatterwalk_unmap(&gw->walk);
874 scatterwalk_advance(&gw->walk, n);
875 scatterwalk_done(&gw->walk, 0, gw->walk_bytes_remain);
876
877 if (gw->buf_bytes >= minbytesneeded) {
878 gw->ptr = gw->buf;
879 gw->nbytes = gw->buf_bytes;
880 goto out;
881 }
882
883 gw->walk_bytes = scatterwalk_clamp(&gw->walk,
884 gw->walk_bytes_remain);
885 if (!gw->walk_bytes) {
886 scatterwalk_start(&gw->walk, sg_next(gw->walk.sg));
887 gw->walk_bytes = scatterwalk_clamp(&gw->walk,
888 gw->walk_bytes_remain);
889 }
890 gw->walk_ptr = scatterwalk_map(&gw->walk);
891 }
892
893out:
894 return gw->nbytes;
895}
896
897static void gcm_sg_walk_done(struct gcm_sg_walk *gw, unsigned int bytesdone)
898{
899 int n;
900
901 if (gw->ptr == NULL)
902 return;
903
904 if (gw->ptr == gw->buf) {
905 n = gw->buf_bytes - bytesdone;
906 if (n > 0) {
907 memmove(gw->buf, gw->buf + bytesdone, n);
908 gw->buf_bytes -= n;
909 } else
910 gw->buf_bytes = 0;
911 } else {
912 gw->walk_bytes_remain -= bytesdone;
913 scatterwalk_unmap(&gw->walk);
914 scatterwalk_advance(&gw->walk, bytesdone);
915 scatterwalk_done(&gw->walk, 0, gw->walk_bytes_remain);
916 }
917}
918
919static int gcm_aes_crypt(struct aead_request *req, unsigned int flags)
920{
921 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
922 struct s390_aes_ctx *ctx = crypto_aead_ctx(tfm);
923 unsigned int ivsize = crypto_aead_ivsize(tfm);
924 unsigned int taglen = crypto_aead_authsize(tfm);
925 unsigned int aadlen = req->assoclen;
926 unsigned int pclen = req->cryptlen;
927 int ret = 0;
928
929 unsigned int len, in_bytes, out_bytes,
930 min_bytes, bytes, aad_bytes, pc_bytes;
931 struct gcm_sg_walk gw_in, gw_out;
932 u8 tag[GHASH_DIGEST_SIZE];
933
934 struct {
935 u32 _[3]; /* reserved */
936 u32 cv; /* Counter Value */
937 u8 t[GHASH_DIGEST_SIZE];/* Tag */
938 u8 h[AES_BLOCK_SIZE]; /* Hash-subkey */
939 u64 taadl; /* Total AAD Length */
940 u64 tpcl; /* Total Plain-/Cipher-text Length */
941 u8 j0[GHASH_BLOCK_SIZE];/* initial counter value */
942 u8 k[AES_MAX_KEY_SIZE]; /* Key */
943 } param;
944
945 /*
946 * encrypt
947 * req->src: aad||plaintext
948 * req->dst: aad||ciphertext||tag
949 * decrypt
950 * req->src: aad||ciphertext||tag
951 * req->dst: aad||plaintext, return 0 or -EBADMSG
952 * aad, plaintext and ciphertext may be empty.
953 */
954 if (flags & CPACF_DECRYPT)
955 pclen -= taglen;
956 len = aadlen + pclen;
957
958 memset(&param, 0, sizeof(param));
959 param.cv = 1;
960 param.taadl = aadlen * 8;
961 param.tpcl = pclen * 8;
962 memcpy(param.j0, req->iv, ivsize);
963 *(u32 *)(param.j0 + ivsize) = 1;
964 memcpy(param.k, ctx->key, ctx->key_len);
965
966 gcm_sg_walk_start(&gw_in, req->src, len);
967 gcm_sg_walk_start(&gw_out, req->dst, len);
968
969 do {
970 min_bytes = min_t(unsigned int,
971 aadlen > 0 ? aadlen : pclen, AES_BLOCK_SIZE);
972 in_bytes = gcm_sg_walk_go(&gw_in, min_bytes);
973 out_bytes = gcm_sg_walk_go(&gw_out, min_bytes);
974 bytes = min(in_bytes, out_bytes);
975
976 if (aadlen + pclen <= bytes) {
977 aad_bytes = aadlen;
978 pc_bytes = pclen;
979 flags |= CPACF_KMA_LAAD | CPACF_KMA_LPC;
980 } else {
981 if (aadlen <= bytes) {
982 aad_bytes = aadlen;
983 pc_bytes = (bytes - aadlen) &
984 ~(AES_BLOCK_SIZE - 1);
985 flags |= CPACF_KMA_LAAD;
986 } else {
987 aad_bytes = bytes & ~(AES_BLOCK_SIZE - 1);
988 pc_bytes = 0;
989 }
990 }
991
992 if (aad_bytes > 0)
993 memcpy(gw_out.ptr, gw_in.ptr, aad_bytes);
994
995 cpacf_kma(ctx->fc | flags, &param,
996 gw_out.ptr + aad_bytes,
997 gw_in.ptr + aad_bytes, pc_bytes,
998 gw_in.ptr, aad_bytes);
999
1000 gcm_sg_walk_done(&gw_in, aad_bytes + pc_bytes);
1001 gcm_sg_walk_done(&gw_out, aad_bytes + pc_bytes);
1002 aadlen -= aad_bytes;
1003 pclen -= pc_bytes;
1004 } while (aadlen + pclen > 0);
1005
1006 if (flags & CPACF_DECRYPT) {
1007 scatterwalk_map_and_copy(tag, req->src, len, taglen, 0);
1008 if (crypto_memneq(tag, param.t, taglen))
1009 ret = -EBADMSG;
1010 } else
1011 scatterwalk_map_and_copy(param.t, req->dst, len, taglen, 1);
1012
1013 memzero_explicit(&param, sizeof(param));
1014 return ret;
1015}
1016
1017static int gcm_aes_encrypt(struct aead_request *req)
1018{
1019 return gcm_aes_crypt(req, CPACF_ENCRYPT);
1020}
1021
1022static int gcm_aes_decrypt(struct aead_request *req)
1023{
1024 return gcm_aes_crypt(req, CPACF_DECRYPT);
1025}
1026
1027static struct aead_alg gcm_aes_aead = {
1028 .setkey = gcm_aes_setkey,
1029 .setauthsize = gcm_aes_setauthsize,
1030 .encrypt = gcm_aes_encrypt,
1031 .decrypt = gcm_aes_decrypt,
1032
1033 .ivsize = GHASH_BLOCK_SIZE - sizeof(u32),
1034 .maxauthsize = GHASH_DIGEST_SIZE,
1035 .chunksize = AES_BLOCK_SIZE,
1036
1037 .base = {
1038 .cra_flags = CRYPTO_ALG_TYPE_AEAD,
1039 .cra_blocksize = 1,
1040 .cra_ctxsize = sizeof(struct s390_aes_ctx),
1041 .cra_priority = 900,
1042 .cra_name = "gcm(aes)",
1043 .cra_driver_name = "gcm-aes-s390",
1044 .cra_module = THIS_MODULE,
1045 },
1046};
1047
Martin Schwidefskyd863d592016-08-18 12:34:34 +02001048static struct crypto_alg *aes_s390_algs_ptr[5];
1049static int aes_s390_algs_num;
Harald Freudenbergerc7260ca2018-03-01 16:40:00 +01001050static struct aead_alg *aes_s390_aead_alg;
Martin Schwidefskyd863d592016-08-18 12:34:34 +02001051
1052static int aes_s390_register_alg(struct crypto_alg *alg)
1053{
1054 int ret;
1055
1056 ret = crypto_register_alg(alg);
1057 if (!ret)
1058 aes_s390_algs_ptr[aes_s390_algs_num++] = alg;
1059 return ret;
1060}
1061
1062static void aes_s390_fini(void)
1063{
1064 while (aes_s390_algs_num--)
1065 crypto_unregister_alg(aes_s390_algs_ptr[aes_s390_algs_num]);
1066 if (ctrblk)
1067 free_page((unsigned long) ctrblk);
Harald Freudenbergerbf7fa032017-09-18 12:48:09 +02001068
Harald Freudenbergerc7260ca2018-03-01 16:40:00 +01001069 if (aes_s390_aead_alg)
1070 crypto_unregister_aead(aes_s390_aead_alg);
Martin Schwidefskyd863d592016-08-18 12:34:34 +02001071}
Ingo Tuchscherer4f57ba72013-10-15 11:24:07 +02001072
Heiko Carstens9f7819c2008-04-17 07:46:17 +02001073static int __init aes_s390_init(void)
Jan Glauberbf754ae2006-01-06 00:19:18 -08001074{
1075 int ret;
1076
Harald Freudenbergerbf7fa032017-09-18 12:48:09 +02001077 /* Query available functions for KM, KMC, KMCTR and KMA */
Martin Schwidefsky69c0e362016-08-18 12:59:46 +02001078 cpacf_query(CPACF_KM, &km_functions);
1079 cpacf_query(CPACF_KMC, &kmc_functions);
1080 cpacf_query(CPACF_KMCTR, &kmctr_functions);
Harald Freudenbergerbf7fa032017-09-18 12:48:09 +02001081 cpacf_query(CPACF_KMA, &kma_functions);
Jan Glauberbf754ae2006-01-06 00:19:18 -08001082
Martin Schwidefsky69c0e362016-08-18 12:59:46 +02001083 if (cpacf_test_func(&km_functions, CPACF_KM_AES_128) ||
1084 cpacf_test_func(&km_functions, CPACF_KM_AES_192) ||
1085 cpacf_test_func(&km_functions, CPACF_KM_AES_256)) {
1086 ret = aes_s390_register_alg(&aes_alg);
1087 if (ret)
1088 goto out_err;
1089 ret = aes_s390_register_alg(&ecb_aes_alg);
1090 if (ret)
1091 goto out_err;
1092 }
Jan Glauber86aa9fc2007-02-05 21:18:14 +01001093
Martin Schwidefsky69c0e362016-08-18 12:59:46 +02001094 if (cpacf_test_func(&kmc_functions, CPACF_KMC_AES_128) ||
1095 cpacf_test_func(&kmc_functions, CPACF_KMC_AES_192) ||
1096 cpacf_test_func(&kmc_functions, CPACF_KMC_AES_256)) {
1097 ret = aes_s390_register_alg(&cbc_aes_alg);
1098 if (ret)
1099 goto out_err;
1100 }
Jan Glauberbf754ae2006-01-06 00:19:18 -08001101
Martin Schwidefsky69c0e362016-08-18 12:59:46 +02001102 if (cpacf_test_func(&km_functions, CPACF_KM_XTS_128) ||
1103 cpacf_test_func(&km_functions, CPACF_KM_XTS_256)) {
Martin Schwidefskyd863d592016-08-18 12:34:34 +02001104 ret = aes_s390_register_alg(&xts_aes_alg);
Gerald Schaefer99d97222011-04-26 16:12:42 +10001105 if (ret)
Martin Schwidefskyd863d592016-08-18 12:34:34 +02001106 goto out_err;
Gerald Schaefer99d97222011-04-26 16:12:42 +10001107 }
1108
Martin Schwidefsky69c0e362016-08-18 12:59:46 +02001109 if (cpacf_test_func(&kmctr_functions, CPACF_KMCTR_AES_128) ||
1110 cpacf_test_func(&kmctr_functions, CPACF_KMCTR_AES_192) ||
1111 cpacf_test_func(&kmctr_functions, CPACF_KMCTR_AES_256)) {
Gerald Schaefer0200f3e2011-05-04 15:09:44 +10001112 ctrblk = (u8 *) __get_free_page(GFP_KERNEL);
1113 if (!ctrblk) {
1114 ret = -ENOMEM;
Martin Schwidefskyd863d592016-08-18 12:34:34 +02001115 goto out_err;
Gerald Schaefer0200f3e2011-05-04 15:09:44 +10001116 }
Martin Schwidefskyd863d592016-08-18 12:34:34 +02001117 ret = aes_s390_register_alg(&ctr_aes_alg);
1118 if (ret)
1119 goto out_err;
Gerald Schaefer0200f3e2011-05-04 15:09:44 +10001120 }
1121
Harald Freudenbergerbf7fa032017-09-18 12:48:09 +02001122 if (cpacf_test_func(&kma_functions, CPACF_KMA_GCM_AES_128) ||
1123 cpacf_test_func(&kma_functions, CPACF_KMA_GCM_AES_192) ||
1124 cpacf_test_func(&kma_functions, CPACF_KMA_GCM_AES_256)) {
1125 ret = crypto_register_aead(&gcm_aes_aead);
1126 if (ret)
1127 goto out_err;
Harald Freudenbergerc7260ca2018-03-01 16:40:00 +01001128 aes_s390_aead_alg = &gcm_aes_aead;
Harald Freudenbergerbf7fa032017-09-18 12:48:09 +02001129 }
1130
Martin Schwidefskyd863d592016-08-18 12:34:34 +02001131 return 0;
1132out_err:
1133 aes_s390_fini();
Jan Glauberbf754ae2006-01-06 00:19:18 -08001134 return ret;
Jan Glauberbf754ae2006-01-06 00:19:18 -08001135}
1136
Hendrik Bruecknerd05377c2015-02-19 17:34:07 +01001137module_cpu_feature_match(MSA, aes_s390_init);
Heiko Carstens9f7819c2008-04-17 07:46:17 +02001138module_exit(aes_s390_fini);
Jan Glauberbf754ae2006-01-06 00:19:18 -08001139
Kees Cook5d26a102014-11-20 17:05:53 -08001140MODULE_ALIAS_CRYPTO("aes-all");
Jan Glauberbf754ae2006-01-06 00:19:18 -08001141
1142MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm");
1143MODULE_LICENSE("GPL");