blob: 303d28eb03a2f697b5ceb18c25cf8a72df4caf4d [file] [log] [blame]
Jan Glauberbf754ae2006-01-06 00:19:18 -08001/*
2 * Cryptographic API.
3 *
4 * s390 implementation of the AES Cipher Algorithm.
5 *
6 * s390 Version:
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02007 * Copyright IBM Corp. 2005, 2007
Jan Glauberbf754ae2006-01-06 00:19:18 -08008 * Author(s): Jan Glauber (jang@de.ibm.com)
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +11009 * Sebastian Siewior (sebastian@breakpoint.cc> SW-Fallback
Jan Glauberbf754ae2006-01-06 00:19:18 -080010 *
Sebastian Siewiorf8246af2007-10-05 16:52:01 +080011 * Derived from "crypto/aes_generic.c"
Jan Glauberbf754ae2006-01-06 00:19:18 -080012 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the Free
15 * Software Foundation; either version 2 of the License, or (at your option)
16 * any later version.
17 *
18 */
19
Jan Glauber39f09392008-12-25 13:39:37 +010020#define KMSG_COMPONENT "aes_s390"
21#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
22
Sebastian Siewior89e126542007-10-17 23:18:57 +080023#include <crypto/aes.h>
Herbert Xua9e62fa2006-08-21 21:39:24 +100024#include <crypto/algapi.h>
Herbert Xu64e26802016-06-29 18:04:07 +080025#include <crypto/internal/skcipher.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>
Stephan Mueller49abc0d2016-02-17 07:00:01 +010031#include <crypto/xts.h>
Martin Schwidefskyc7d4d252016-03-17 15:22:12 +010032#include <asm/cpacf.h>
Jan Glauberbf754ae2006-01-06 00:19:18 -080033
Gerald Schaefer0200f3e2011-05-04 15:09:44 +100034static u8 *ctrblk;
Harald Freudenberger0519e9a2014-01-16 16:01:11 +010035static DEFINE_SPINLOCK(ctrblk_lock);
Martin Schwidefsky69c0e362016-08-18 12:59:46 +020036
37static cpacf_mask_t km_functions, kmc_functions, kmctr_functions;
Jan Glauberbf754ae2006-01-06 00:19:18 -080038
39struct s390_aes_ctx {
Jan Glauberbf754ae2006-01-06 00:19:18 -080040 u8 key[AES_MAX_KEY_SIZE];
41 int key_len;
Martin Schwidefskyedc63a32016-08-15 09:19:16 +020042 unsigned long fc;
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +110043 union {
Herbert Xu64e26802016-06-29 18:04:07 +080044 struct crypto_skcipher *blk;
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +110045 struct crypto_cipher *cip;
46 } fallback;
Jan Glauberbf754ae2006-01-06 00:19:18 -080047};
48
Gerald Schaefer99d97222011-04-26 16:12:42 +100049struct s390_xts_ctx {
50 u8 key[32];
Gerald Schaefer9dda2762013-11-19 17:12:47 +010051 u8 pcc_key[32];
Gerald Schaefer99d97222011-04-26 16:12:42 +100052 int key_len;
Martin Schwidefskyedc63a32016-08-15 09:19:16 +020053 unsigned long fc;
Herbert Xu64e26802016-06-29 18:04:07 +080054 struct crypto_skcipher *fallback;
Gerald Schaefer99d97222011-04-26 16:12:42 +100055};
56
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +110057static int setkey_fallback_cip(struct crypto_tfm *tfm, const u8 *in_key,
58 unsigned int key_len)
59{
60 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
61 int ret;
62
Roel Kluind7ac7692010-01-08 14:18:34 +110063 sctx->fallback.cip->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK;
64 sctx->fallback.cip->base.crt_flags |= (tfm->crt_flags &
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +110065 CRYPTO_TFM_REQ_MASK);
66
67 ret = crypto_cipher_setkey(sctx->fallback.cip, in_key, key_len);
68 if (ret) {
69 tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
Roel Kluind7ac7692010-01-08 14:18:34 +110070 tfm->crt_flags |= (sctx->fallback.cip->base.crt_flags &
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +110071 CRYPTO_TFM_RES_MASK);
72 }
73 return ret;
74}
75
Herbert Xu6c2bb982006-05-16 22:09:29 +100076static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
Herbert Xu560c06a2006-08-13 14:16:39 +100077 unsigned int key_len)
Jan Glauberbf754ae2006-01-06 00:19:18 -080078{
Herbert Xu6c2bb982006-05-16 22:09:29 +100079 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
Martin Schwidefsky69c0e362016-08-18 12:59:46 +020080 unsigned long fc;
Jan Glauberbf754ae2006-01-06 00:19:18 -080081
Martin Schwidefsky69c0e362016-08-18 12:59:46 +020082 /* Pick the correct function code based on the key length */
83 fc = (key_len == 16) ? CPACF_KM_AES_128 :
84 (key_len == 24) ? CPACF_KM_AES_192 :
85 (key_len == 32) ? CPACF_KM_AES_256 : 0;
86
87 /* Check if the function code is available */
88 sctx->fc = (fc && cpacf_test_func(&km_functions, fc)) ? fc : 0;
89 if (!sctx->fc)
90 return setkey_fallback_cip(tfm, in_key, key_len);
Jan Glauberbf754ae2006-01-06 00:19:18 -080091
92 sctx->key_len = key_len;
Martin Schwidefsky69c0e362016-08-18 12:59:46 +020093 memcpy(sctx->key, in_key, key_len);
94 return 0;
Jan Glauberbf754ae2006-01-06 00:19:18 -080095}
96
Herbert Xu6c2bb982006-05-16 22:09:29 +100097static void aes_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
Jan Glauberbf754ae2006-01-06 00:19:18 -080098{
Chen Gange6a67ad2015-01-01 22:56:02 +080099 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
Jan Glauberbf754ae2006-01-06 00:19:18 -0800100
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200101 if (unlikely(!sctx->fc)) {
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100102 crypto_cipher_encrypt_one(sctx->fallback.cip, out, in);
103 return;
104 }
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200105 cpacf_km(sctx->fc, &sctx->key, out, in, AES_BLOCK_SIZE);
Jan Glauberbf754ae2006-01-06 00:19:18 -0800106}
107
Herbert Xu6c2bb982006-05-16 22:09:29 +1000108static void aes_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
Jan Glauberbf754ae2006-01-06 00:19:18 -0800109{
Chen Gange6a67ad2015-01-01 22:56:02 +0800110 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
Jan Glauberbf754ae2006-01-06 00:19:18 -0800111
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200112 if (unlikely(!sctx->fc)) {
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100113 crypto_cipher_decrypt_one(sctx->fallback.cip, out, in);
114 return;
115 }
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200116 cpacf_km(sctx->fc | CPACF_DECRYPT,
117 &sctx->key, out, in, AES_BLOCK_SIZE);
Jan Glauberbf754ae2006-01-06 00:19:18 -0800118}
119
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100120static int fallback_init_cip(struct crypto_tfm *tfm)
121{
122 const char *name = tfm->__crt_alg->cra_name;
123 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
124
125 sctx->fallback.cip = crypto_alloc_cipher(name, 0,
126 CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK);
127
128 if (IS_ERR(sctx->fallback.cip)) {
Jan Glauber39f09392008-12-25 13:39:37 +0100129 pr_err("Allocating AES fallback algorithm %s failed\n",
130 name);
Roel Kluinb59cdcb32009-12-18 17:43:18 +0100131 return PTR_ERR(sctx->fallback.cip);
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100132 }
133
134 return 0;
135}
136
137static void fallback_exit_cip(struct crypto_tfm *tfm)
138{
139 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
140
141 crypto_free_cipher(sctx->fallback.cip);
142 sctx->fallback.cip = NULL;
143}
Jan Glauberbf754ae2006-01-06 00:19:18 -0800144
145static struct crypto_alg aes_alg = {
146 .cra_name = "aes",
Herbert Xu65b75c32006-08-21 21:18:50 +1000147 .cra_driver_name = "aes-s390",
Martin Schwidefskyc7d4d252016-03-17 15:22:12 +0100148 .cra_priority = 300,
Jan Glauberf67d1362007-05-04 18:47:47 +0200149 .cra_flags = CRYPTO_ALG_TYPE_CIPHER |
150 CRYPTO_ALG_NEED_FALLBACK,
Jan Glauberbf754ae2006-01-06 00:19:18 -0800151 .cra_blocksize = AES_BLOCK_SIZE,
152 .cra_ctxsize = sizeof(struct s390_aes_ctx),
153 .cra_module = THIS_MODULE,
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100154 .cra_init = fallback_init_cip,
155 .cra_exit = fallback_exit_cip,
Jan Glauberbf754ae2006-01-06 00:19:18 -0800156 .cra_u = {
157 .cipher = {
158 .cia_min_keysize = AES_MIN_KEY_SIZE,
159 .cia_max_keysize = AES_MAX_KEY_SIZE,
160 .cia_setkey = aes_set_key,
161 .cia_encrypt = aes_encrypt,
162 .cia_decrypt = aes_decrypt,
Jan Glauberbf754ae2006-01-06 00:19:18 -0800163 }
164 }
165};
166
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100167static int setkey_fallback_blk(struct crypto_tfm *tfm, const u8 *key,
168 unsigned int len)
169{
170 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
171 unsigned int ret;
172
Herbert Xu64e26802016-06-29 18:04:07 +0800173 crypto_skcipher_clear_flags(sctx->fallback.blk, CRYPTO_TFM_REQ_MASK);
174 crypto_skcipher_set_flags(sctx->fallback.blk, tfm->crt_flags &
175 CRYPTO_TFM_REQ_MASK);
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100176
Herbert Xu64e26802016-06-29 18:04:07 +0800177 ret = crypto_skcipher_setkey(sctx->fallback.blk, key, len);
178
179 tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
180 tfm->crt_flags |= crypto_skcipher_get_flags(sctx->fallback.blk) &
181 CRYPTO_TFM_RES_MASK;
182
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100183 return ret;
184}
185
186static int fallback_blk_dec(struct blkcipher_desc *desc,
187 struct scatterlist *dst, struct scatterlist *src,
188 unsigned int nbytes)
189{
190 unsigned int ret;
Herbert Xu64e26802016-06-29 18:04:07 +0800191 struct crypto_blkcipher *tfm = desc->tfm;
192 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(tfm);
193 SKCIPHER_REQUEST_ON_STACK(req, sctx->fallback.blk);
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100194
Herbert Xu64e26802016-06-29 18:04:07 +0800195 skcipher_request_set_tfm(req, sctx->fallback.blk);
196 skcipher_request_set_callback(req, desc->flags, NULL, NULL);
197 skcipher_request_set_crypt(req, src, dst, nbytes, desc->info);
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100198
Herbert Xu64e26802016-06-29 18:04:07 +0800199 ret = crypto_skcipher_decrypt(req);
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100200
Herbert Xu64e26802016-06-29 18:04:07 +0800201 skcipher_request_zero(req);
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100202 return ret;
203}
204
205static int fallback_blk_enc(struct blkcipher_desc *desc,
206 struct scatterlist *dst, struct scatterlist *src,
207 unsigned int nbytes)
208{
209 unsigned int ret;
Herbert Xu64e26802016-06-29 18:04:07 +0800210 struct crypto_blkcipher *tfm = desc->tfm;
211 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(tfm);
212 SKCIPHER_REQUEST_ON_STACK(req, sctx->fallback.blk);
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100213
Herbert Xu64e26802016-06-29 18:04:07 +0800214 skcipher_request_set_tfm(req, sctx->fallback.blk);
215 skcipher_request_set_callback(req, desc->flags, NULL, NULL);
216 skcipher_request_set_crypt(req, src, dst, nbytes, desc->info);
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100217
Herbert Xu64e26802016-06-29 18:04:07 +0800218 ret = crypto_skcipher_encrypt(req);
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100219 return ret;
220}
221
Herbert Xua9e62fa2006-08-21 21:39:24 +1000222static int ecb_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
223 unsigned int key_len)
224{
225 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200226 unsigned long fc;
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100227
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200228 /* Pick the correct function code based on the key length */
229 fc = (key_len == 16) ? CPACF_KM_AES_128 :
230 (key_len == 24) ? CPACF_KM_AES_192 :
231 (key_len == 32) ? CPACF_KM_AES_256 : 0;
232
233 /* Check if the function code is available */
234 sctx->fc = (fc && cpacf_test_func(&km_functions, fc)) ? fc : 0;
235 if (!sctx->fc)
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100236 return setkey_fallback_blk(tfm, in_key, key_len);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000237
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200238 sctx->key_len = key_len;
239 memcpy(sctx->key, in_key, key_len);
240 return 0;
Herbert Xua9e62fa2006-08-21 21:39:24 +1000241}
242
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200243static int ecb_aes_crypt(struct blkcipher_desc *desc, unsigned long modifier,
Herbert Xua9e62fa2006-08-21 21:39:24 +1000244 struct blkcipher_walk *walk)
245{
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200246 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
247 unsigned int nbytes, n;
248 int ret;
Herbert Xua9e62fa2006-08-21 21:39:24 +1000249
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200250 ret = blkcipher_walk_virt(desc, walk);
251 while ((nbytes = walk->nbytes) >= AES_BLOCK_SIZE) {
Herbert Xua9e62fa2006-08-21 21:39:24 +1000252 /* only use complete blocks */
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200253 n = nbytes & ~(AES_BLOCK_SIZE - 1);
254 cpacf_km(sctx->fc | modifier, sctx->key,
255 walk->dst.virt.addr, walk->src.virt.addr, n);
256 ret = blkcipher_walk_done(desc, walk, nbytes - n);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000257 }
258
259 return ret;
260}
261
262static int ecb_aes_encrypt(struct blkcipher_desc *desc,
263 struct scatterlist *dst, struct scatterlist *src,
264 unsigned int nbytes)
265{
266 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
267 struct blkcipher_walk walk;
268
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200269 if (unlikely(!sctx->fc))
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100270 return fallback_blk_enc(desc, dst, src, nbytes);
271
Herbert Xua9e62fa2006-08-21 21:39:24 +1000272 blkcipher_walk_init(&walk, dst, src, nbytes);
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200273 return ecb_aes_crypt(desc, 0, &walk);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000274}
275
276static int ecb_aes_decrypt(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_dec(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, CPACF_DECRYPT, &walk);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000288}
289
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100290static int fallback_init_blk(struct crypto_tfm *tfm)
291{
292 const char *name = tfm->__crt_alg->cra_name;
293 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
294
Herbert Xu64e26802016-06-29 18:04:07 +0800295 sctx->fallback.blk = crypto_alloc_skcipher(name, 0,
296 CRYPTO_ALG_ASYNC |
297 CRYPTO_ALG_NEED_FALLBACK);
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100298
299 if (IS_ERR(sctx->fallback.blk)) {
Jan Glauber39f09392008-12-25 13:39:37 +0100300 pr_err("Allocating AES fallback algorithm %s failed\n",
301 name);
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100302 return PTR_ERR(sctx->fallback.blk);
303 }
304
305 return 0;
306}
307
308static void fallback_exit_blk(struct crypto_tfm *tfm)
309{
310 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
311
Herbert Xu64e26802016-06-29 18:04:07 +0800312 crypto_free_skcipher(sctx->fallback.blk);
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100313}
314
Herbert Xua9e62fa2006-08-21 21:39:24 +1000315static struct crypto_alg ecb_aes_alg = {
316 .cra_name = "ecb(aes)",
317 .cra_driver_name = "ecb-aes-s390",
Martin Schwidefskyc7d4d252016-03-17 15:22:12 +0100318 .cra_priority = 400, /* combo: aes + ecb */
Jan Glauberf67d1362007-05-04 18:47:47 +0200319 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
320 CRYPTO_ALG_NEED_FALLBACK,
Herbert Xua9e62fa2006-08-21 21:39:24 +1000321 .cra_blocksize = AES_BLOCK_SIZE,
322 .cra_ctxsize = sizeof(struct s390_aes_ctx),
323 .cra_type = &crypto_blkcipher_type,
324 .cra_module = THIS_MODULE,
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100325 .cra_init = fallback_init_blk,
326 .cra_exit = fallback_exit_blk,
Herbert Xua9e62fa2006-08-21 21:39:24 +1000327 .cra_u = {
328 .blkcipher = {
329 .min_keysize = AES_MIN_KEY_SIZE,
330 .max_keysize = AES_MAX_KEY_SIZE,
331 .setkey = ecb_aes_set_key,
332 .encrypt = ecb_aes_encrypt,
333 .decrypt = ecb_aes_decrypt,
334 }
335 }
336};
337
338static int cbc_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
339 unsigned int key_len)
340{
341 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200342 unsigned long fc;
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100343
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200344 /* Pick the correct function code based on the key length */
345 fc = (key_len == 16) ? CPACF_KMC_AES_128 :
346 (key_len == 24) ? CPACF_KMC_AES_192 :
347 (key_len == 32) ? CPACF_KMC_AES_256 : 0;
348
349 /* Check if the function code is available */
350 sctx->fc = (fc && cpacf_test_func(&kmc_functions, fc)) ? fc : 0;
351 if (!sctx->fc)
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100352 return setkey_fallback_blk(tfm, in_key, key_len);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000353
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200354 sctx->key_len = key_len;
355 memcpy(sctx->key, in_key, key_len);
356 return 0;
Herbert Xua9e62fa2006-08-21 21:39:24 +1000357}
358
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200359static int cbc_aes_crypt(struct blkcipher_desc *desc, unsigned long modifier,
Herbert Xua9e62fa2006-08-21 21:39:24 +1000360 struct blkcipher_walk *walk)
361{
Herbert Xuf262f0f2013-11-05 19:36:27 +0800362 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200363 unsigned int nbytes, n;
364 int ret;
Herbert Xuf262f0f2013-11-05 19:36:27 +0800365 struct {
366 u8 iv[AES_BLOCK_SIZE];
367 u8 key[AES_MAX_KEY_SIZE];
368 } param;
Herbert Xua9e62fa2006-08-21 21:39:24 +1000369
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200370 ret = blkcipher_walk_virt(desc, walk);
Herbert Xuf262f0f2013-11-05 19:36:27 +0800371 memcpy(param.iv, walk->iv, AES_BLOCK_SIZE);
372 memcpy(param.key, sctx->key, sctx->key_len);
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200373 while ((nbytes = walk->nbytes) >= AES_BLOCK_SIZE) {
Herbert Xua9e62fa2006-08-21 21:39:24 +1000374 /* only use complete blocks */
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200375 n = nbytes & ~(AES_BLOCK_SIZE - 1);
376 cpacf_kmc(sctx->fc | modifier, &param,
377 walk->dst.virt.addr, walk->src.virt.addr, n);
378 ret = blkcipher_walk_done(desc, walk, nbytes - n);
379 }
Herbert Xuf262f0f2013-11-05 19:36:27 +0800380 memcpy(walk->iv, param.iv, AES_BLOCK_SIZE);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000381 return ret;
382}
383
384static int cbc_aes_encrypt(struct blkcipher_desc *desc,
385 struct scatterlist *dst, struct scatterlist *src,
386 unsigned int nbytes)
387{
388 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
389 struct blkcipher_walk walk;
390
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200391 if (unlikely(!sctx->fc))
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100392 return fallback_blk_enc(desc, dst, src, nbytes);
393
Herbert Xua9e62fa2006-08-21 21:39:24 +1000394 blkcipher_walk_init(&walk, dst, src, nbytes);
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200395 return cbc_aes_crypt(desc, 0, &walk);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000396}
397
398static int cbc_aes_decrypt(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_dec(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, CPACF_DECRYPT, &walk);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000410}
411
412static struct crypto_alg cbc_aes_alg = {
413 .cra_name = "cbc(aes)",
414 .cra_driver_name = "cbc-aes-s390",
Martin Schwidefskyc7d4d252016-03-17 15:22:12 +0100415 .cra_priority = 400, /* combo: aes + cbc */
Jan Glauberf67d1362007-05-04 18:47:47 +0200416 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
417 CRYPTO_ALG_NEED_FALLBACK,
Herbert Xua9e62fa2006-08-21 21:39:24 +1000418 .cra_blocksize = AES_BLOCK_SIZE,
419 .cra_ctxsize = sizeof(struct s390_aes_ctx),
420 .cra_type = &crypto_blkcipher_type,
421 .cra_module = THIS_MODULE,
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100422 .cra_init = fallback_init_blk,
423 .cra_exit = fallback_exit_blk,
Herbert Xua9e62fa2006-08-21 21:39:24 +1000424 .cra_u = {
425 .blkcipher = {
426 .min_keysize = AES_MIN_KEY_SIZE,
427 .max_keysize = AES_MAX_KEY_SIZE,
428 .ivsize = AES_BLOCK_SIZE,
429 .setkey = cbc_aes_set_key,
430 .encrypt = cbc_aes_encrypt,
431 .decrypt = cbc_aes_decrypt,
432 }
433 }
434};
435
Gerald Schaefer99d97222011-04-26 16:12:42 +1000436static int xts_fallback_setkey(struct crypto_tfm *tfm, const u8 *key,
437 unsigned int len)
438{
439 struct s390_xts_ctx *xts_ctx = crypto_tfm_ctx(tfm);
440 unsigned int ret;
441
Herbert Xu64e26802016-06-29 18:04:07 +0800442 crypto_skcipher_clear_flags(xts_ctx->fallback, CRYPTO_TFM_REQ_MASK);
443 crypto_skcipher_set_flags(xts_ctx->fallback, tfm->crt_flags &
444 CRYPTO_TFM_REQ_MASK);
Gerald Schaefer99d97222011-04-26 16:12:42 +1000445
Herbert Xu64e26802016-06-29 18:04:07 +0800446 ret = crypto_skcipher_setkey(xts_ctx->fallback, key, len);
447
448 tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
449 tfm->crt_flags |= crypto_skcipher_get_flags(xts_ctx->fallback) &
450 CRYPTO_TFM_RES_MASK;
451
Gerald Schaefer99d97222011-04-26 16:12:42 +1000452 return ret;
453}
454
455static int xts_fallback_decrypt(struct blkcipher_desc *desc,
456 struct scatterlist *dst, struct scatterlist *src,
457 unsigned int nbytes)
458{
Herbert Xu64e26802016-06-29 18:04:07 +0800459 struct crypto_blkcipher *tfm = desc->tfm;
460 struct s390_xts_ctx *xts_ctx = crypto_blkcipher_ctx(tfm);
461 SKCIPHER_REQUEST_ON_STACK(req, xts_ctx->fallback);
Gerald Schaefer99d97222011-04-26 16:12:42 +1000462 unsigned int ret;
463
Herbert Xu64e26802016-06-29 18:04:07 +0800464 skcipher_request_set_tfm(req, xts_ctx->fallback);
465 skcipher_request_set_callback(req, desc->flags, NULL, NULL);
466 skcipher_request_set_crypt(req, src, dst, nbytes, desc->info);
Gerald Schaefer99d97222011-04-26 16:12:42 +1000467
Herbert Xu64e26802016-06-29 18:04:07 +0800468 ret = crypto_skcipher_decrypt(req);
Gerald Schaefer99d97222011-04-26 16:12:42 +1000469
Herbert Xu64e26802016-06-29 18:04:07 +0800470 skcipher_request_zero(req);
Gerald Schaefer99d97222011-04-26 16:12:42 +1000471 return ret;
472}
473
474static int xts_fallback_encrypt(struct blkcipher_desc *desc,
475 struct scatterlist *dst, struct scatterlist *src,
476 unsigned int nbytes)
477{
Herbert Xu64e26802016-06-29 18:04:07 +0800478 struct crypto_blkcipher *tfm = desc->tfm;
479 struct s390_xts_ctx *xts_ctx = crypto_blkcipher_ctx(tfm);
480 SKCIPHER_REQUEST_ON_STACK(req, xts_ctx->fallback);
Gerald Schaefer99d97222011-04-26 16:12:42 +1000481 unsigned int ret;
482
Herbert Xu64e26802016-06-29 18:04:07 +0800483 skcipher_request_set_tfm(req, xts_ctx->fallback);
484 skcipher_request_set_callback(req, desc->flags, NULL, NULL);
485 skcipher_request_set_crypt(req, src, dst, nbytes, desc->info);
Gerald Schaefer99d97222011-04-26 16:12:42 +1000486
Herbert Xu64e26802016-06-29 18:04:07 +0800487 ret = crypto_skcipher_encrypt(req);
Gerald Schaefer99d97222011-04-26 16:12:42 +1000488
Herbert Xu64e26802016-06-29 18:04:07 +0800489 skcipher_request_zero(req);
Gerald Schaefer99d97222011-04-26 16:12:42 +1000490 return ret;
491}
492
493static int xts_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
494 unsigned int key_len)
495{
496 struct s390_xts_ctx *xts_ctx = crypto_tfm_ctx(tfm);
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200497 unsigned long fc;
Stephan Mueller28856a92016-02-09 15:37:47 +0100498 int err;
499
500 err = xts_check_key(tfm, in_key, key_len);
501 if (err)
502 return err;
Gerald Schaefer99d97222011-04-26 16:12:42 +1000503
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200504 /* Pick the correct function code based on the key length */
505 fc = (key_len == 32) ? CPACF_KM_XTS_128 :
506 (key_len == 64) ? CPACF_KM_XTS_256 : 0;
507
508 /* Check if the function code is available */
509 xts_ctx->fc = (fc && cpacf_test_func(&km_functions, fc)) ? fc : 0;
510 if (!xts_ctx->fc)
511 return xts_fallback_setkey(tfm, in_key, key_len);
512
513 /* Split the XTS key into the two subkeys */
514 key_len = key_len / 2;
Gerald Schaefer99d97222011-04-26 16:12:42 +1000515 xts_ctx->key_len = key_len;
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200516 memcpy(xts_ctx->key, in_key, key_len);
517 memcpy(xts_ctx->pcc_key, in_key + key_len, key_len);
Gerald Schaefer99d97222011-04-26 16:12:42 +1000518 return 0;
519}
520
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200521static int xts_aes_crypt(struct blkcipher_desc *desc, unsigned long modifier,
Gerald Schaefer99d97222011-04-26 16:12:42 +1000522 struct blkcipher_walk *walk)
523{
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200524 struct s390_xts_ctx *xts_ctx = crypto_blkcipher_ctx(desc->tfm);
525 unsigned int offset, nbytes, n;
526 int ret;
527 struct {
528 u8 key[32];
529 u8 tweak[16];
530 u8 block[16];
531 u8 bit[16];
532 u8 xts[16];
533 } pcc_param;
Gerald Schaefer9dda2762013-11-19 17:12:47 +0100534 struct {
535 u8 key[32];
536 u8 init[16];
537 } xts_param;
Gerald Schaefer99d97222011-04-26 16:12:42 +1000538
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200539 ret = blkcipher_walk_virt(desc, walk);
540 offset = xts_ctx->key_len & 0x10;
Gerald Schaefer9dda2762013-11-19 17:12:47 +0100541 memset(pcc_param.block, 0, sizeof(pcc_param.block));
542 memset(pcc_param.bit, 0, sizeof(pcc_param.bit));
543 memset(pcc_param.xts, 0, sizeof(pcc_param.xts));
544 memcpy(pcc_param.tweak, walk->iv, sizeof(pcc_param.tweak));
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200545 memcpy(pcc_param.key + offset, xts_ctx->pcc_key, xts_ctx->key_len);
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200546 cpacf_pcc(xts_ctx->fc, pcc_param.key + offset);
Gerald Schaefer99d97222011-04-26 16:12:42 +1000547
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200548 memcpy(xts_param.key + offset, xts_ctx->key, xts_ctx->key_len);
Gerald Schaefer9dda2762013-11-19 17:12:47 +0100549 memcpy(xts_param.init, pcc_param.xts, 16);
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200550
551 while ((nbytes = walk->nbytes) >= AES_BLOCK_SIZE) {
Gerald Schaefer99d97222011-04-26 16:12:42 +1000552 /* only use complete blocks */
553 n = nbytes & ~(AES_BLOCK_SIZE - 1);
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200554 cpacf_km(xts_ctx->fc | modifier, xts_param.key + offset,
555 walk->dst.virt.addr, walk->src.virt.addr, n);
556 ret = blkcipher_walk_done(desc, walk, nbytes - n);
557 }
Gerald Schaefer99d97222011-04-26 16:12:42 +1000558 return ret;
559}
560
561static int xts_aes_encrypt(struct blkcipher_desc *desc,
562 struct scatterlist *dst, struct scatterlist *src,
563 unsigned int nbytes)
564{
565 struct s390_xts_ctx *xts_ctx = crypto_blkcipher_ctx(desc->tfm);
566 struct blkcipher_walk walk;
567
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200568 if (unlikely(!xts_ctx->fc))
Gerald Schaefer99d97222011-04-26 16:12:42 +1000569 return xts_fallback_encrypt(desc, dst, src, nbytes);
570
571 blkcipher_walk_init(&walk, dst, src, nbytes);
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200572 return xts_aes_crypt(desc, 0, &walk);
Gerald Schaefer99d97222011-04-26 16:12:42 +1000573}
574
575static int xts_aes_decrypt(struct blkcipher_desc *desc,
576 struct scatterlist *dst, struct scatterlist *src,
577 unsigned int nbytes)
578{
579 struct s390_xts_ctx *xts_ctx = crypto_blkcipher_ctx(desc->tfm);
580 struct blkcipher_walk walk;
581
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200582 if (unlikely(!xts_ctx->fc))
Gerald Schaefer99d97222011-04-26 16:12:42 +1000583 return xts_fallback_decrypt(desc, dst, src, nbytes);
584
585 blkcipher_walk_init(&walk, dst, src, nbytes);
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200586 return xts_aes_crypt(desc, CPACF_DECRYPT, &walk);
Gerald Schaefer99d97222011-04-26 16:12:42 +1000587}
588
589static int xts_fallback_init(struct crypto_tfm *tfm)
590{
591 const char *name = tfm->__crt_alg->cra_name;
592 struct s390_xts_ctx *xts_ctx = crypto_tfm_ctx(tfm);
593
Herbert Xu64e26802016-06-29 18:04:07 +0800594 xts_ctx->fallback = crypto_alloc_skcipher(name, 0,
595 CRYPTO_ALG_ASYNC |
596 CRYPTO_ALG_NEED_FALLBACK);
Gerald Schaefer99d97222011-04-26 16:12:42 +1000597
598 if (IS_ERR(xts_ctx->fallback)) {
599 pr_err("Allocating XTS fallback algorithm %s failed\n",
600 name);
601 return PTR_ERR(xts_ctx->fallback);
602 }
603 return 0;
604}
605
606static void xts_fallback_exit(struct crypto_tfm *tfm)
607{
608 struct s390_xts_ctx *xts_ctx = crypto_tfm_ctx(tfm);
609
Herbert Xu64e26802016-06-29 18:04:07 +0800610 crypto_free_skcipher(xts_ctx->fallback);
Gerald Schaefer99d97222011-04-26 16:12:42 +1000611}
612
613static struct crypto_alg xts_aes_alg = {
614 .cra_name = "xts(aes)",
615 .cra_driver_name = "xts-aes-s390",
Martin Schwidefskyc7d4d252016-03-17 15:22:12 +0100616 .cra_priority = 400, /* combo: aes + xts */
Gerald Schaefer99d97222011-04-26 16:12:42 +1000617 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
618 CRYPTO_ALG_NEED_FALLBACK,
619 .cra_blocksize = AES_BLOCK_SIZE,
620 .cra_ctxsize = sizeof(struct s390_xts_ctx),
621 .cra_type = &crypto_blkcipher_type,
622 .cra_module = THIS_MODULE,
Gerald Schaefer99d97222011-04-26 16:12:42 +1000623 .cra_init = xts_fallback_init,
624 .cra_exit = xts_fallback_exit,
625 .cra_u = {
626 .blkcipher = {
627 .min_keysize = 2 * AES_MIN_KEY_SIZE,
628 .max_keysize = 2 * AES_MAX_KEY_SIZE,
629 .ivsize = AES_BLOCK_SIZE,
630 .setkey = xts_aes_set_key,
631 .encrypt = xts_aes_encrypt,
632 .decrypt = xts_aes_decrypt,
633 }
634 }
635};
636
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000637static int ctr_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
638 unsigned int key_len)
639{
640 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200641 unsigned long fc;
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000642
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200643 /* Pick the correct function code based on the key length */
644 fc = (key_len == 16) ? CPACF_KMCTR_AES_128 :
645 (key_len == 24) ? CPACF_KMCTR_AES_192 :
646 (key_len == 32) ? CPACF_KMCTR_AES_256 : 0;
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000647
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200648 /* Check if the function code is available */
649 sctx->fc = (fc && cpacf_test_func(&kmctr_functions, fc)) ? fc : 0;
650 if (!sctx->fc)
651 return setkey_fallback_blk(tfm, in_key, key_len);
652
653 sctx->key_len = key_len;
654 memcpy(sctx->key, in_key, key_len);
655 return 0;
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000656}
657
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200658static unsigned int __ctrblk_init(u8 *ctrptr, u8 *iv, unsigned int nbytes)
Harald Freudenberger0519e9a2014-01-16 16:01:11 +0100659{
660 unsigned int i, n;
661
662 /* only use complete blocks, max. PAGE_SIZE */
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200663 memcpy(ctrptr, iv, AES_BLOCK_SIZE);
Harald Freudenberger0519e9a2014-01-16 16:01:11 +0100664 n = (nbytes > PAGE_SIZE) ? PAGE_SIZE : nbytes & ~(AES_BLOCK_SIZE - 1);
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200665 for (i = (n / AES_BLOCK_SIZE) - 1; i > 0; i--) {
666 memcpy(ctrptr + AES_BLOCK_SIZE, ctrptr, AES_BLOCK_SIZE);
667 crypto_inc(ctrptr + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
668 ctrptr += AES_BLOCK_SIZE;
Harald Freudenberger0519e9a2014-01-16 16:01:11 +0100669 }
670 return n;
671}
672
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200673static int ctr_aes_crypt(struct blkcipher_desc *desc, unsigned long modifier,
674 struct blkcipher_walk *walk)
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000675{
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200676 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
677 u8 buf[AES_BLOCK_SIZE], *ctrptr;
Harald Freudenberger0519e9a2014-01-16 16:01:11 +0100678 unsigned int n, nbytes;
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200679 int ret, locked;
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000680
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200681 locked = spin_trylock(&ctrblk_lock);
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000682
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200683 ret = blkcipher_walk_virt_block(desc, walk, AES_BLOCK_SIZE);
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000684 while ((nbytes = walk->nbytes) >= AES_BLOCK_SIZE) {
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200685 n = AES_BLOCK_SIZE;
686 if (nbytes >= 2*AES_BLOCK_SIZE && locked)
687 n = __ctrblk_init(ctrblk, walk->iv, nbytes);
688 ctrptr = (n > AES_BLOCK_SIZE) ? ctrblk : walk->iv;
689 cpacf_kmctr(sctx->fc | modifier, sctx->key,
690 walk->dst.virt.addr, walk->src.virt.addr,
691 n, ctrptr);
692 if (ctrptr == ctrblk)
693 memcpy(walk->iv, ctrptr + n - AES_BLOCK_SIZE,
694 AES_BLOCK_SIZE);
695 crypto_inc(walk->iv, AES_BLOCK_SIZE);
696 ret = blkcipher_walk_done(desc, walk, nbytes - n);
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000697 }
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200698 if (locked)
Harald Freudenberger0519e9a2014-01-16 16:01:11 +0100699 spin_unlock(&ctrblk_lock);
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000700 /*
701 * final block may be < AES_BLOCK_SIZE, copy only nbytes
702 */
703 if (nbytes) {
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200704 cpacf_kmctr(sctx->fc | modifier, sctx->key,
705 buf, walk->src.virt.addr,
706 AES_BLOCK_SIZE, walk->iv);
707 memcpy(walk->dst.virt.addr, buf, nbytes);
708 crypto_inc(walk->iv, AES_BLOCK_SIZE);
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000709 ret = blkcipher_walk_done(desc, walk, 0);
710 }
Harald Freudenberger0519e9a2014-01-16 16:01:11 +0100711
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000712 return ret;
713}
714
715static int ctr_aes_encrypt(struct blkcipher_desc *desc,
716 struct scatterlist *dst, struct scatterlist *src,
717 unsigned int nbytes)
718{
719 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
720 struct blkcipher_walk walk;
721
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200722 if (unlikely(!sctx->fc))
723 return fallback_blk_enc(desc, dst, src, nbytes);
724
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000725 blkcipher_walk_init(&walk, dst, src, nbytes);
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200726 return ctr_aes_crypt(desc, 0, &walk);
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000727}
728
729static int ctr_aes_decrypt(struct blkcipher_desc *desc,
730 struct scatterlist *dst, struct scatterlist *src,
731 unsigned int nbytes)
732{
733 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
734 struct blkcipher_walk walk;
735
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200736 if (unlikely(!sctx->fc))
737 return fallback_blk_dec(desc, dst, src, nbytes);
738
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000739 blkcipher_walk_init(&walk, dst, src, nbytes);
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200740 return ctr_aes_crypt(desc, CPACF_DECRYPT, &walk);
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000741}
742
743static struct crypto_alg ctr_aes_alg = {
744 .cra_name = "ctr(aes)",
745 .cra_driver_name = "ctr-aes-s390",
Martin Schwidefskyc7d4d252016-03-17 15:22:12 +0100746 .cra_priority = 400, /* combo: aes + ctr */
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200747 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
748 CRYPTO_ALG_NEED_FALLBACK,
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000749 .cra_blocksize = 1,
750 .cra_ctxsize = sizeof(struct s390_aes_ctx),
751 .cra_type = &crypto_blkcipher_type,
752 .cra_module = THIS_MODULE,
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200753 .cra_init = fallback_init_blk,
754 .cra_exit = fallback_exit_blk,
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000755 .cra_u = {
756 .blkcipher = {
757 .min_keysize = AES_MIN_KEY_SIZE,
758 .max_keysize = AES_MAX_KEY_SIZE,
759 .ivsize = AES_BLOCK_SIZE,
760 .setkey = ctr_aes_set_key,
761 .encrypt = ctr_aes_encrypt,
762 .decrypt = ctr_aes_decrypt,
763 }
764 }
765};
766
Martin Schwidefskyd863d592016-08-18 12:34:34 +0200767static struct crypto_alg *aes_s390_algs_ptr[5];
768static int aes_s390_algs_num;
769
770static int aes_s390_register_alg(struct crypto_alg *alg)
771{
772 int ret;
773
774 ret = crypto_register_alg(alg);
775 if (!ret)
776 aes_s390_algs_ptr[aes_s390_algs_num++] = alg;
777 return ret;
778}
779
780static void aes_s390_fini(void)
781{
782 while (aes_s390_algs_num--)
783 crypto_unregister_alg(aes_s390_algs_ptr[aes_s390_algs_num]);
784 if (ctrblk)
785 free_page((unsigned long) ctrblk);
786}
Ingo Tuchscherer4f57ba72013-10-15 11:24:07 +0200787
Heiko Carstens9f7819c2008-04-17 07:46:17 +0200788static int __init aes_s390_init(void)
Jan Glauberbf754ae2006-01-06 00:19:18 -0800789{
790 int ret;
791
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200792 /* Query available functions for KM, KMC and KMCTR */
793 cpacf_query(CPACF_KM, &km_functions);
794 cpacf_query(CPACF_KMC, &kmc_functions);
795 cpacf_query(CPACF_KMCTR, &kmctr_functions);
Jan Glauberbf754ae2006-01-06 00:19:18 -0800796
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200797 if (cpacf_test_func(&km_functions, CPACF_KM_AES_128) ||
798 cpacf_test_func(&km_functions, CPACF_KM_AES_192) ||
799 cpacf_test_func(&km_functions, CPACF_KM_AES_256)) {
800 ret = aes_s390_register_alg(&aes_alg);
801 if (ret)
802 goto out_err;
803 ret = aes_s390_register_alg(&ecb_aes_alg);
804 if (ret)
805 goto out_err;
806 }
Jan Glauber86aa9fc2007-02-05 21:18:14 +0100807
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200808 if (cpacf_test_func(&kmc_functions, CPACF_KMC_AES_128) ||
809 cpacf_test_func(&kmc_functions, CPACF_KMC_AES_192) ||
810 cpacf_test_func(&kmc_functions, CPACF_KMC_AES_256)) {
811 ret = aes_s390_register_alg(&cbc_aes_alg);
812 if (ret)
813 goto out_err;
814 }
Jan Glauberbf754ae2006-01-06 00:19:18 -0800815
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200816 if (cpacf_test_func(&km_functions, CPACF_KM_XTS_128) ||
817 cpacf_test_func(&km_functions, CPACF_KM_XTS_256)) {
Martin Schwidefskyd863d592016-08-18 12:34:34 +0200818 ret = aes_s390_register_alg(&xts_aes_alg);
Gerald Schaefer99d97222011-04-26 16:12:42 +1000819 if (ret)
Martin Schwidefskyd863d592016-08-18 12:34:34 +0200820 goto out_err;
Gerald Schaefer99d97222011-04-26 16:12:42 +1000821 }
822
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200823 if (cpacf_test_func(&kmctr_functions, CPACF_KMCTR_AES_128) ||
824 cpacf_test_func(&kmctr_functions, CPACF_KMCTR_AES_192) ||
825 cpacf_test_func(&kmctr_functions, CPACF_KMCTR_AES_256)) {
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000826 ctrblk = (u8 *) __get_free_page(GFP_KERNEL);
827 if (!ctrblk) {
828 ret = -ENOMEM;
Martin Schwidefskyd863d592016-08-18 12:34:34 +0200829 goto out_err;
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000830 }
Martin Schwidefskyd863d592016-08-18 12:34:34 +0200831 ret = aes_s390_register_alg(&ctr_aes_alg);
832 if (ret)
833 goto out_err;
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000834 }
835
Martin Schwidefskyd863d592016-08-18 12:34:34 +0200836 return 0;
837out_err:
838 aes_s390_fini();
Jan Glauberbf754ae2006-01-06 00:19:18 -0800839 return ret;
Jan Glauberbf754ae2006-01-06 00:19:18 -0800840}
841
Hendrik Bruecknerd05377c2015-02-19 17:34:07 +0100842module_cpu_feature_match(MSA, aes_s390_init);
Heiko Carstens9f7819c2008-04-17 07:46:17 +0200843module_exit(aes_s390_fini);
Jan Glauberbf754ae2006-01-06 00:19:18 -0800844
Kees Cook5d26a102014-11-20 17:05:53 -0800845MODULE_ALIAS_CRYPTO("aes-all");
Jan Glauberbf754ae2006-01-06 00:19:18 -0800846
847MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm");
848MODULE_LICENSE("GPL");