blob: 0b9b95f3c7037e6fcad08a5dd6453852c60d4180 [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 Siewior89e12652007-10-17 23:18:57 +080023#include <crypto/aes.h>
Herbert Xua9e62fa2006-08-21 21:39:24 +100024#include <crypto/algapi.h>
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +110025#include <linux/err.h>
Jan Glauberbf754ae2006-01-06 00:19:18 -080026#include <linux/module.h>
Hendrik Bruecknerd05377c2015-02-19 17:34:07 +010027#include <linux/cpufeature.h>
Jan Glauberbf754ae2006-01-06 00:19:18 -080028#include <linux/init.h>
Harald Freudenberger0519e9a2014-01-16 16:01:11 +010029#include <linux/spinlock.h>
Jan Glauberbf754ae2006-01-06 00:19:18 -080030#include "crypt_s390.h"
31
Jan Glauber86aa9fc2007-02-05 21:18:14 +010032#define AES_KEYLEN_128 1
33#define AES_KEYLEN_192 2
34#define AES_KEYLEN_256 4
35
Gerald Schaefer0200f3e2011-05-04 15:09:44 +100036static u8 *ctrblk;
Harald Freudenberger0519e9a2014-01-16 16:01:11 +010037static DEFINE_SPINLOCK(ctrblk_lock);
Gerald Schaefer0200f3e2011-05-04 15:09:44 +100038static char keylen_flag;
Jan Glauberbf754ae2006-01-06 00:19:18 -080039
40struct s390_aes_ctx {
Jan Glauberbf754ae2006-01-06 00:19:18 -080041 u8 key[AES_MAX_KEY_SIZE];
Herbert Xua9e62fa2006-08-21 21:39:24 +100042 long enc;
43 long dec;
Jan Glauberbf754ae2006-01-06 00:19:18 -080044 int key_len;
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +110045 union {
46 struct crypto_blkcipher *blk;
47 struct crypto_cipher *cip;
48 } fallback;
Jan Glauberbf754ae2006-01-06 00:19:18 -080049};
50
Gerald Schaefer99d97222011-04-26 16:12:42 +100051struct pcc_param {
52 u8 key[32];
53 u8 tweak[16];
54 u8 block[16];
55 u8 bit[16];
56 u8 xts[16];
57};
58
59struct s390_xts_ctx {
60 u8 key[32];
Gerald Schaefer9dda2762013-11-19 17:12:47 +010061 u8 pcc_key[32];
Gerald Schaefer99d97222011-04-26 16:12:42 +100062 long enc;
63 long dec;
64 int key_len;
65 struct crypto_blkcipher *fallback;
66};
67
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +110068/*
69 * Check if the key_len is supported by the HW.
70 * Returns 0 if it is, a positive number if it is not and software fallback is
71 * required or a negative number in case the key size is not valid
72 */
73static int need_fallback(unsigned int key_len)
74{
75 switch (key_len) {
76 case 16:
77 if (!(keylen_flag & AES_KEYLEN_128))
78 return 1;
79 break;
80 case 24:
81 if (!(keylen_flag & AES_KEYLEN_192))
82 return 1;
83 break;
84 case 32:
85 if (!(keylen_flag & AES_KEYLEN_256))
86 return 1;
87 break;
88 default:
89 return -1;
90 break;
91 }
92 return 0;
93}
94
95static int setkey_fallback_cip(struct crypto_tfm *tfm, const u8 *in_key,
96 unsigned int key_len)
97{
98 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
99 int ret;
100
Roel Kluind7ac7692010-01-08 14:18:34 +1100101 sctx->fallback.cip->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK;
102 sctx->fallback.cip->base.crt_flags |= (tfm->crt_flags &
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100103 CRYPTO_TFM_REQ_MASK);
104
105 ret = crypto_cipher_setkey(sctx->fallback.cip, in_key, key_len);
106 if (ret) {
107 tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
Roel Kluind7ac7692010-01-08 14:18:34 +1100108 tfm->crt_flags |= (sctx->fallback.cip->base.crt_flags &
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100109 CRYPTO_TFM_RES_MASK);
110 }
111 return ret;
112}
113
Herbert Xu6c2bb982006-05-16 22:09:29 +1000114static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000115 unsigned int key_len)
Jan Glauberbf754ae2006-01-06 00:19:18 -0800116{
Herbert Xu6c2bb982006-05-16 22:09:29 +1000117 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
Herbert Xu560c06a2006-08-13 14:16:39 +1000118 u32 *flags = &tfm->crt_flags;
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100119 int ret;
Jan Glauberbf754ae2006-01-06 00:19:18 -0800120
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100121 ret = need_fallback(key_len);
122 if (ret < 0) {
123 *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
124 return -EINVAL;
Jan Glauberbf754ae2006-01-06 00:19:18 -0800125 }
126
127 sctx->key_len = key_len;
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100128 if (!ret) {
129 memcpy(sctx->key, in_key, key_len);
130 return 0;
131 }
132
133 return setkey_fallback_cip(tfm, in_key, key_len);
Jan Glauberbf754ae2006-01-06 00:19:18 -0800134}
135
Herbert Xu6c2bb982006-05-16 22:09:29 +1000136static void aes_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
Jan Glauberbf754ae2006-01-06 00:19:18 -0800137{
Chen Gange6a67ad2015-01-01 22:56:02 +0800138 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
Jan Glauberbf754ae2006-01-06 00:19:18 -0800139
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100140 if (unlikely(need_fallback(sctx->key_len))) {
141 crypto_cipher_encrypt_one(sctx->fallback.cip, out, in);
142 return;
143 }
144
Jan Glauberbf754ae2006-01-06 00:19:18 -0800145 switch (sctx->key_len) {
146 case 16:
147 crypt_s390_km(KM_AES_128_ENCRYPT, &sctx->key, out, in,
148 AES_BLOCK_SIZE);
149 break;
150 case 24:
151 crypt_s390_km(KM_AES_192_ENCRYPT, &sctx->key, out, in,
152 AES_BLOCK_SIZE);
153 break;
154 case 32:
155 crypt_s390_km(KM_AES_256_ENCRYPT, &sctx->key, out, in,
156 AES_BLOCK_SIZE);
157 break;
158 }
159}
160
Herbert Xu6c2bb982006-05-16 22:09:29 +1000161static void aes_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
Jan Glauberbf754ae2006-01-06 00:19:18 -0800162{
Chen Gange6a67ad2015-01-01 22:56:02 +0800163 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
Jan Glauberbf754ae2006-01-06 00:19:18 -0800164
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100165 if (unlikely(need_fallback(sctx->key_len))) {
166 crypto_cipher_decrypt_one(sctx->fallback.cip, out, in);
167 return;
168 }
169
Jan Glauberbf754ae2006-01-06 00:19:18 -0800170 switch (sctx->key_len) {
171 case 16:
172 crypt_s390_km(KM_AES_128_DECRYPT, &sctx->key, out, in,
173 AES_BLOCK_SIZE);
174 break;
175 case 24:
176 crypt_s390_km(KM_AES_192_DECRYPT, &sctx->key, out, in,
177 AES_BLOCK_SIZE);
178 break;
179 case 32:
180 crypt_s390_km(KM_AES_256_DECRYPT, &sctx->key, out, in,
181 AES_BLOCK_SIZE);
182 break;
183 }
184}
185
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100186static int fallback_init_cip(struct crypto_tfm *tfm)
187{
188 const char *name = tfm->__crt_alg->cra_name;
189 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
190
191 sctx->fallback.cip = crypto_alloc_cipher(name, 0,
192 CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK);
193
194 if (IS_ERR(sctx->fallback.cip)) {
Jan Glauber39f09392008-12-25 13:39:37 +0100195 pr_err("Allocating AES fallback algorithm %s failed\n",
196 name);
Roel Kluinb59cdcb32009-12-18 17:43:18 +0100197 return PTR_ERR(sctx->fallback.cip);
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100198 }
199
200 return 0;
201}
202
203static void fallback_exit_cip(struct crypto_tfm *tfm)
204{
205 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
206
207 crypto_free_cipher(sctx->fallback.cip);
208 sctx->fallback.cip = NULL;
209}
Jan Glauberbf754ae2006-01-06 00:19:18 -0800210
211static struct crypto_alg aes_alg = {
212 .cra_name = "aes",
Herbert Xu65b75c32006-08-21 21:18:50 +1000213 .cra_driver_name = "aes-s390",
214 .cra_priority = CRYPT_S390_PRIORITY,
Jan Glauberf67d1362007-05-04 18:47:47 +0200215 .cra_flags = CRYPTO_ALG_TYPE_CIPHER |
216 CRYPTO_ALG_NEED_FALLBACK,
Jan Glauberbf754ae2006-01-06 00:19:18 -0800217 .cra_blocksize = AES_BLOCK_SIZE,
218 .cra_ctxsize = sizeof(struct s390_aes_ctx),
219 .cra_module = THIS_MODULE,
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100220 .cra_init = fallback_init_cip,
221 .cra_exit = fallback_exit_cip,
Jan Glauberbf754ae2006-01-06 00:19:18 -0800222 .cra_u = {
223 .cipher = {
224 .cia_min_keysize = AES_MIN_KEY_SIZE,
225 .cia_max_keysize = AES_MAX_KEY_SIZE,
226 .cia_setkey = aes_set_key,
227 .cia_encrypt = aes_encrypt,
228 .cia_decrypt = aes_decrypt,
Jan Glauberbf754ae2006-01-06 00:19:18 -0800229 }
230 }
231};
232
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100233static int setkey_fallback_blk(struct crypto_tfm *tfm, const u8 *key,
234 unsigned int len)
235{
236 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
237 unsigned int ret;
238
239 sctx->fallback.blk->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK;
240 sctx->fallback.blk->base.crt_flags |= (tfm->crt_flags &
241 CRYPTO_TFM_REQ_MASK);
242
243 ret = crypto_blkcipher_setkey(sctx->fallback.blk, key, len);
244 if (ret) {
245 tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
246 tfm->crt_flags |= (sctx->fallback.blk->base.crt_flags &
247 CRYPTO_TFM_RES_MASK);
248 }
249 return ret;
250}
251
252static int fallback_blk_dec(struct blkcipher_desc *desc,
253 struct scatterlist *dst, struct scatterlist *src,
254 unsigned int nbytes)
255{
256 unsigned int ret;
257 struct crypto_blkcipher *tfm;
258 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
259
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100260 tfm = desc->tfm;
261 desc->tfm = sctx->fallback.blk;
262
Sebastian Siewior2d74d402007-12-10 15:49:41 +0800263 ret = crypto_blkcipher_decrypt_iv(desc, dst, src, nbytes);
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100264
265 desc->tfm = tfm;
266 return ret;
267}
268
269static int fallback_blk_enc(struct blkcipher_desc *desc,
270 struct scatterlist *dst, struct scatterlist *src,
271 unsigned int nbytes)
272{
273 unsigned int ret;
274 struct crypto_blkcipher *tfm;
275 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
276
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100277 tfm = desc->tfm;
278 desc->tfm = sctx->fallback.blk;
279
Sebastian Siewior2d74d402007-12-10 15:49:41 +0800280 ret = crypto_blkcipher_encrypt_iv(desc, dst, src, nbytes);
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100281
282 desc->tfm = tfm;
283 return ret;
284}
285
Herbert Xua9e62fa2006-08-21 21:39:24 +1000286static int ecb_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
287 unsigned int key_len)
288{
289 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100290 int ret;
291
292 ret = need_fallback(key_len);
293 if (ret > 0) {
294 sctx->key_len = key_len;
295 return setkey_fallback_blk(tfm, in_key, key_len);
296 }
Herbert Xua9e62fa2006-08-21 21:39:24 +1000297
298 switch (key_len) {
299 case 16:
300 sctx->enc = KM_AES_128_ENCRYPT;
301 sctx->dec = KM_AES_128_DECRYPT;
302 break;
303 case 24:
304 sctx->enc = KM_AES_192_ENCRYPT;
305 sctx->dec = KM_AES_192_DECRYPT;
306 break;
307 case 32:
308 sctx->enc = KM_AES_256_ENCRYPT;
309 sctx->dec = KM_AES_256_DECRYPT;
310 break;
311 }
312
313 return aes_set_key(tfm, in_key, key_len);
314}
315
316static int ecb_aes_crypt(struct blkcipher_desc *desc, long func, void *param,
317 struct blkcipher_walk *walk)
318{
319 int ret = blkcipher_walk_virt(desc, walk);
320 unsigned int nbytes;
321
322 while ((nbytes = walk->nbytes)) {
323 /* only use complete blocks */
324 unsigned int n = nbytes & ~(AES_BLOCK_SIZE - 1);
325 u8 *out = walk->dst.virt.addr;
326 u8 *in = walk->src.virt.addr;
327
328 ret = crypt_s390_km(func, param, out, in, n);
Jan Glauber36eb2ca2012-10-26 15:06:12 +0200329 if (ret < 0 || ret != n)
330 return -EIO;
Herbert Xua9e62fa2006-08-21 21:39:24 +1000331
332 nbytes &= AES_BLOCK_SIZE - 1;
333 ret = blkcipher_walk_done(desc, walk, nbytes);
334 }
335
336 return ret;
337}
338
339static int ecb_aes_encrypt(struct blkcipher_desc *desc,
340 struct scatterlist *dst, struct scatterlist *src,
341 unsigned int nbytes)
342{
343 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
344 struct blkcipher_walk walk;
345
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100346 if (unlikely(need_fallback(sctx->key_len)))
347 return fallback_blk_enc(desc, dst, src, nbytes);
348
Herbert Xua9e62fa2006-08-21 21:39:24 +1000349 blkcipher_walk_init(&walk, dst, src, nbytes);
350 return ecb_aes_crypt(desc, sctx->enc, sctx->key, &walk);
351}
352
353static int ecb_aes_decrypt(struct blkcipher_desc *desc,
354 struct scatterlist *dst, struct scatterlist *src,
355 unsigned int nbytes)
356{
357 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
358 struct blkcipher_walk walk;
359
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100360 if (unlikely(need_fallback(sctx->key_len)))
361 return fallback_blk_dec(desc, dst, src, nbytes);
362
Herbert Xua9e62fa2006-08-21 21:39:24 +1000363 blkcipher_walk_init(&walk, dst, src, nbytes);
364 return ecb_aes_crypt(desc, sctx->dec, sctx->key, &walk);
365}
366
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100367static int fallback_init_blk(struct crypto_tfm *tfm)
368{
369 const char *name = tfm->__crt_alg->cra_name;
370 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
371
372 sctx->fallback.blk = crypto_alloc_blkcipher(name, 0,
373 CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK);
374
375 if (IS_ERR(sctx->fallback.blk)) {
Jan Glauber39f09392008-12-25 13:39:37 +0100376 pr_err("Allocating AES fallback algorithm %s failed\n",
377 name);
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100378 return PTR_ERR(sctx->fallback.blk);
379 }
380
381 return 0;
382}
383
384static void fallback_exit_blk(struct crypto_tfm *tfm)
385{
386 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
387
388 crypto_free_blkcipher(sctx->fallback.blk);
389 sctx->fallback.blk = NULL;
390}
391
Herbert Xua9e62fa2006-08-21 21:39:24 +1000392static struct crypto_alg ecb_aes_alg = {
393 .cra_name = "ecb(aes)",
394 .cra_driver_name = "ecb-aes-s390",
395 .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
Jan Glauberf67d1362007-05-04 18:47:47 +0200396 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
397 CRYPTO_ALG_NEED_FALLBACK,
Herbert Xua9e62fa2006-08-21 21:39:24 +1000398 .cra_blocksize = AES_BLOCK_SIZE,
399 .cra_ctxsize = sizeof(struct s390_aes_ctx),
400 .cra_type = &crypto_blkcipher_type,
401 .cra_module = THIS_MODULE,
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100402 .cra_init = fallback_init_blk,
403 .cra_exit = fallback_exit_blk,
Herbert Xua9e62fa2006-08-21 21:39:24 +1000404 .cra_u = {
405 .blkcipher = {
406 .min_keysize = AES_MIN_KEY_SIZE,
407 .max_keysize = AES_MAX_KEY_SIZE,
408 .setkey = ecb_aes_set_key,
409 .encrypt = ecb_aes_encrypt,
410 .decrypt = ecb_aes_decrypt,
411 }
412 }
413};
414
415static int cbc_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
416 unsigned int key_len)
417{
418 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100419 int ret;
420
421 ret = need_fallback(key_len);
422 if (ret > 0) {
423 sctx->key_len = key_len;
424 return setkey_fallback_blk(tfm, in_key, key_len);
425 }
Herbert Xua9e62fa2006-08-21 21:39:24 +1000426
427 switch (key_len) {
428 case 16:
429 sctx->enc = KMC_AES_128_ENCRYPT;
430 sctx->dec = KMC_AES_128_DECRYPT;
431 break;
432 case 24:
433 sctx->enc = KMC_AES_192_ENCRYPT;
434 sctx->dec = KMC_AES_192_DECRYPT;
435 break;
436 case 32:
437 sctx->enc = KMC_AES_256_ENCRYPT;
438 sctx->dec = KMC_AES_256_DECRYPT;
439 break;
440 }
441
442 return aes_set_key(tfm, in_key, key_len);
443}
444
Herbert Xuf262f0f2013-11-05 19:36:27 +0800445static int cbc_aes_crypt(struct blkcipher_desc *desc, long func,
Herbert Xua9e62fa2006-08-21 21:39:24 +1000446 struct blkcipher_walk *walk)
447{
Herbert Xuf262f0f2013-11-05 19:36:27 +0800448 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000449 int ret = blkcipher_walk_virt(desc, walk);
450 unsigned int nbytes = walk->nbytes;
Herbert Xuf262f0f2013-11-05 19:36:27 +0800451 struct {
452 u8 iv[AES_BLOCK_SIZE];
453 u8 key[AES_MAX_KEY_SIZE];
454 } param;
Herbert Xua9e62fa2006-08-21 21:39:24 +1000455
456 if (!nbytes)
457 goto out;
458
Herbert Xuf262f0f2013-11-05 19:36:27 +0800459 memcpy(param.iv, walk->iv, AES_BLOCK_SIZE);
460 memcpy(param.key, sctx->key, sctx->key_len);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000461 do {
462 /* only use complete blocks */
463 unsigned int n = nbytes & ~(AES_BLOCK_SIZE - 1);
464 u8 *out = walk->dst.virt.addr;
465 u8 *in = walk->src.virt.addr;
466
Herbert Xuf262f0f2013-11-05 19:36:27 +0800467 ret = crypt_s390_kmc(func, &param, out, in, n);
Jan Glauber36eb2ca2012-10-26 15:06:12 +0200468 if (ret < 0 || ret != n)
469 return -EIO;
Herbert Xua9e62fa2006-08-21 21:39:24 +1000470
471 nbytes &= AES_BLOCK_SIZE - 1;
472 ret = blkcipher_walk_done(desc, walk, nbytes);
473 } while ((nbytes = walk->nbytes));
Herbert Xuf262f0f2013-11-05 19:36:27 +0800474 memcpy(walk->iv, param.iv, AES_BLOCK_SIZE);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000475
476out:
477 return ret;
478}
479
480static int cbc_aes_encrypt(struct blkcipher_desc *desc,
481 struct scatterlist *dst, struct scatterlist *src,
482 unsigned int nbytes)
483{
484 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
485 struct blkcipher_walk walk;
486
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100487 if (unlikely(need_fallback(sctx->key_len)))
488 return fallback_blk_enc(desc, dst, src, nbytes);
489
Herbert Xua9e62fa2006-08-21 21:39:24 +1000490 blkcipher_walk_init(&walk, dst, src, nbytes);
Herbert Xuf262f0f2013-11-05 19:36:27 +0800491 return cbc_aes_crypt(desc, sctx->enc, &walk);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000492}
493
494static int cbc_aes_decrypt(struct blkcipher_desc *desc,
495 struct scatterlist *dst, struct scatterlist *src,
496 unsigned int nbytes)
497{
498 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
499 struct blkcipher_walk walk;
500
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100501 if (unlikely(need_fallback(sctx->key_len)))
502 return fallback_blk_dec(desc, dst, src, nbytes);
503
Herbert Xua9e62fa2006-08-21 21:39:24 +1000504 blkcipher_walk_init(&walk, dst, src, nbytes);
Herbert Xuf262f0f2013-11-05 19:36:27 +0800505 return cbc_aes_crypt(desc, sctx->dec, &walk);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000506}
507
508static struct crypto_alg cbc_aes_alg = {
509 .cra_name = "cbc(aes)",
510 .cra_driver_name = "cbc-aes-s390",
511 .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
Jan Glauberf67d1362007-05-04 18:47:47 +0200512 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
513 CRYPTO_ALG_NEED_FALLBACK,
Herbert Xua9e62fa2006-08-21 21:39:24 +1000514 .cra_blocksize = AES_BLOCK_SIZE,
515 .cra_ctxsize = sizeof(struct s390_aes_ctx),
516 .cra_type = &crypto_blkcipher_type,
517 .cra_module = THIS_MODULE,
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100518 .cra_init = fallback_init_blk,
519 .cra_exit = fallback_exit_blk,
Herbert Xua9e62fa2006-08-21 21:39:24 +1000520 .cra_u = {
521 .blkcipher = {
522 .min_keysize = AES_MIN_KEY_SIZE,
523 .max_keysize = AES_MAX_KEY_SIZE,
524 .ivsize = AES_BLOCK_SIZE,
525 .setkey = cbc_aes_set_key,
526 .encrypt = cbc_aes_encrypt,
527 .decrypt = cbc_aes_decrypt,
528 }
529 }
530};
531
Gerald Schaefer99d97222011-04-26 16:12:42 +1000532static int xts_fallback_setkey(struct crypto_tfm *tfm, const u8 *key,
533 unsigned int len)
534{
535 struct s390_xts_ctx *xts_ctx = crypto_tfm_ctx(tfm);
536 unsigned int ret;
537
538 xts_ctx->fallback->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK;
539 xts_ctx->fallback->base.crt_flags |= (tfm->crt_flags &
540 CRYPTO_TFM_REQ_MASK);
541
542 ret = crypto_blkcipher_setkey(xts_ctx->fallback, key, len);
543 if (ret) {
544 tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
545 tfm->crt_flags |= (xts_ctx->fallback->base.crt_flags &
546 CRYPTO_TFM_RES_MASK);
547 }
548 return ret;
549}
550
551static int xts_fallback_decrypt(struct blkcipher_desc *desc,
552 struct scatterlist *dst, struct scatterlist *src,
553 unsigned int nbytes)
554{
555 struct s390_xts_ctx *xts_ctx = crypto_blkcipher_ctx(desc->tfm);
556 struct crypto_blkcipher *tfm;
557 unsigned int ret;
558
559 tfm = desc->tfm;
560 desc->tfm = xts_ctx->fallback;
561
562 ret = crypto_blkcipher_decrypt_iv(desc, dst, src, nbytes);
563
564 desc->tfm = tfm;
565 return ret;
566}
567
568static int xts_fallback_encrypt(struct blkcipher_desc *desc,
569 struct scatterlist *dst, struct scatterlist *src,
570 unsigned int nbytes)
571{
572 struct s390_xts_ctx *xts_ctx = crypto_blkcipher_ctx(desc->tfm);
573 struct crypto_blkcipher *tfm;
574 unsigned int ret;
575
576 tfm = desc->tfm;
577 desc->tfm = xts_ctx->fallback;
578
579 ret = crypto_blkcipher_encrypt_iv(desc, dst, src, nbytes);
580
581 desc->tfm = tfm;
582 return ret;
583}
584
585static int xts_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
586 unsigned int key_len)
587{
588 struct s390_xts_ctx *xts_ctx = crypto_tfm_ctx(tfm);
589 u32 *flags = &tfm->crt_flags;
590
591 switch (key_len) {
592 case 32:
593 xts_ctx->enc = KM_XTS_128_ENCRYPT;
594 xts_ctx->dec = KM_XTS_128_DECRYPT;
595 memcpy(xts_ctx->key + 16, in_key, 16);
Gerald Schaefer9dda2762013-11-19 17:12:47 +0100596 memcpy(xts_ctx->pcc_key + 16, in_key + 16, 16);
Gerald Schaefer99d97222011-04-26 16:12:42 +1000597 break;
598 case 48:
599 xts_ctx->enc = 0;
600 xts_ctx->dec = 0;
601 xts_fallback_setkey(tfm, in_key, key_len);
602 break;
603 case 64:
604 xts_ctx->enc = KM_XTS_256_ENCRYPT;
605 xts_ctx->dec = KM_XTS_256_DECRYPT;
606 memcpy(xts_ctx->key, in_key, 32);
Gerald Schaefer9dda2762013-11-19 17:12:47 +0100607 memcpy(xts_ctx->pcc_key, in_key + 32, 32);
Gerald Schaefer99d97222011-04-26 16:12:42 +1000608 break;
609 default:
610 *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
611 return -EINVAL;
612 }
613 xts_ctx->key_len = key_len;
614 return 0;
615}
616
617static int xts_aes_crypt(struct blkcipher_desc *desc, long func,
618 struct s390_xts_ctx *xts_ctx,
619 struct blkcipher_walk *walk)
620{
621 unsigned int offset = (xts_ctx->key_len >> 1) & 0x10;
622 int ret = blkcipher_walk_virt(desc, walk);
623 unsigned int nbytes = walk->nbytes;
624 unsigned int n;
625 u8 *in, *out;
Gerald Schaefer9dda2762013-11-19 17:12:47 +0100626 struct pcc_param pcc_param;
627 struct {
628 u8 key[32];
629 u8 init[16];
630 } xts_param;
Gerald Schaefer99d97222011-04-26 16:12:42 +1000631
632 if (!nbytes)
633 goto out;
634
Gerald Schaefer9dda2762013-11-19 17:12:47 +0100635 memset(pcc_param.block, 0, sizeof(pcc_param.block));
636 memset(pcc_param.bit, 0, sizeof(pcc_param.bit));
637 memset(pcc_param.xts, 0, sizeof(pcc_param.xts));
638 memcpy(pcc_param.tweak, walk->iv, sizeof(pcc_param.tweak));
639 memcpy(pcc_param.key, xts_ctx->pcc_key, 32);
640 ret = crypt_s390_pcc(func, &pcc_param.key[offset]);
Jan Glauber36eb2ca2012-10-26 15:06:12 +0200641 if (ret < 0)
642 return -EIO;
Gerald Schaefer99d97222011-04-26 16:12:42 +1000643
Gerald Schaefer9dda2762013-11-19 17:12:47 +0100644 memcpy(xts_param.key, xts_ctx->key, 32);
645 memcpy(xts_param.init, pcc_param.xts, 16);
Gerald Schaefer99d97222011-04-26 16:12:42 +1000646 do {
647 /* only use complete blocks */
648 n = nbytes & ~(AES_BLOCK_SIZE - 1);
649 out = walk->dst.virt.addr;
650 in = walk->src.virt.addr;
651
Gerald Schaefer9dda2762013-11-19 17:12:47 +0100652 ret = crypt_s390_km(func, &xts_param.key[offset], out, in, n);
Jan Glauber36eb2ca2012-10-26 15:06:12 +0200653 if (ret < 0 || ret != n)
654 return -EIO;
Gerald Schaefer99d97222011-04-26 16:12:42 +1000655
656 nbytes &= AES_BLOCK_SIZE - 1;
657 ret = blkcipher_walk_done(desc, walk, nbytes);
658 } while ((nbytes = walk->nbytes));
659out:
660 return ret;
661}
662
663static int xts_aes_encrypt(struct blkcipher_desc *desc,
664 struct scatterlist *dst, struct scatterlist *src,
665 unsigned int nbytes)
666{
667 struct s390_xts_ctx *xts_ctx = crypto_blkcipher_ctx(desc->tfm);
668 struct blkcipher_walk walk;
669
670 if (unlikely(xts_ctx->key_len == 48))
671 return xts_fallback_encrypt(desc, dst, src, nbytes);
672
673 blkcipher_walk_init(&walk, dst, src, nbytes);
674 return xts_aes_crypt(desc, xts_ctx->enc, xts_ctx, &walk);
675}
676
677static int xts_aes_decrypt(struct blkcipher_desc *desc,
678 struct scatterlist *dst, struct scatterlist *src,
679 unsigned int nbytes)
680{
681 struct s390_xts_ctx *xts_ctx = crypto_blkcipher_ctx(desc->tfm);
682 struct blkcipher_walk walk;
683
684 if (unlikely(xts_ctx->key_len == 48))
685 return xts_fallback_decrypt(desc, dst, src, nbytes);
686
687 blkcipher_walk_init(&walk, dst, src, nbytes);
688 return xts_aes_crypt(desc, xts_ctx->dec, xts_ctx, &walk);
689}
690
691static int xts_fallback_init(struct crypto_tfm *tfm)
692{
693 const char *name = tfm->__crt_alg->cra_name;
694 struct s390_xts_ctx *xts_ctx = crypto_tfm_ctx(tfm);
695
696 xts_ctx->fallback = crypto_alloc_blkcipher(name, 0,
697 CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK);
698
699 if (IS_ERR(xts_ctx->fallback)) {
700 pr_err("Allocating XTS fallback algorithm %s failed\n",
701 name);
702 return PTR_ERR(xts_ctx->fallback);
703 }
704 return 0;
705}
706
707static void xts_fallback_exit(struct crypto_tfm *tfm)
708{
709 struct s390_xts_ctx *xts_ctx = crypto_tfm_ctx(tfm);
710
711 crypto_free_blkcipher(xts_ctx->fallback);
712 xts_ctx->fallback = NULL;
713}
714
715static struct crypto_alg xts_aes_alg = {
716 .cra_name = "xts(aes)",
717 .cra_driver_name = "xts-aes-s390",
718 .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
719 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
720 CRYPTO_ALG_NEED_FALLBACK,
721 .cra_blocksize = AES_BLOCK_SIZE,
722 .cra_ctxsize = sizeof(struct s390_xts_ctx),
723 .cra_type = &crypto_blkcipher_type,
724 .cra_module = THIS_MODULE,
Gerald Schaefer99d97222011-04-26 16:12:42 +1000725 .cra_init = xts_fallback_init,
726 .cra_exit = xts_fallback_exit,
727 .cra_u = {
728 .blkcipher = {
729 .min_keysize = 2 * AES_MIN_KEY_SIZE,
730 .max_keysize = 2 * AES_MAX_KEY_SIZE,
731 .ivsize = AES_BLOCK_SIZE,
732 .setkey = xts_aes_set_key,
733 .encrypt = xts_aes_encrypt,
734 .decrypt = xts_aes_decrypt,
735 }
736 }
737};
738
Ingo Tuchscherer4f57ba72013-10-15 11:24:07 +0200739static int xts_aes_alg_reg;
740
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000741static int ctr_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
742 unsigned int key_len)
743{
744 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
745
746 switch (key_len) {
747 case 16:
748 sctx->enc = KMCTR_AES_128_ENCRYPT;
749 sctx->dec = KMCTR_AES_128_DECRYPT;
750 break;
751 case 24:
752 sctx->enc = KMCTR_AES_192_ENCRYPT;
753 sctx->dec = KMCTR_AES_192_DECRYPT;
754 break;
755 case 32:
756 sctx->enc = KMCTR_AES_256_ENCRYPT;
757 sctx->dec = KMCTR_AES_256_DECRYPT;
758 break;
759 }
760
761 return aes_set_key(tfm, in_key, key_len);
762}
763
Harald Freudenberger0519e9a2014-01-16 16:01:11 +0100764static unsigned int __ctrblk_init(u8 *ctrptr, unsigned int nbytes)
765{
766 unsigned int i, n;
767
768 /* only use complete blocks, max. PAGE_SIZE */
769 n = (nbytes > PAGE_SIZE) ? PAGE_SIZE : nbytes & ~(AES_BLOCK_SIZE - 1);
770 for (i = AES_BLOCK_SIZE; i < n; i += AES_BLOCK_SIZE) {
771 memcpy(ctrptr + i, ctrptr + i - AES_BLOCK_SIZE,
772 AES_BLOCK_SIZE);
773 crypto_inc(ctrptr + i, AES_BLOCK_SIZE);
774 }
775 return n;
776}
777
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000778static int ctr_aes_crypt(struct blkcipher_desc *desc, long func,
779 struct s390_aes_ctx *sctx, struct blkcipher_walk *walk)
780{
781 int ret = blkcipher_walk_virt_block(desc, walk, AES_BLOCK_SIZE);
Harald Freudenberger0519e9a2014-01-16 16:01:11 +0100782 unsigned int n, nbytes;
783 u8 buf[AES_BLOCK_SIZE], ctrbuf[AES_BLOCK_SIZE];
784 u8 *out, *in, *ctrptr = ctrbuf;
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000785
786 if (!walk->nbytes)
787 return ret;
788
Harald Freudenberger0519e9a2014-01-16 16:01:11 +0100789 if (spin_trylock(&ctrblk_lock))
790 ctrptr = ctrblk;
791
792 memcpy(ctrptr, walk->iv, AES_BLOCK_SIZE);
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000793 while ((nbytes = walk->nbytes) >= AES_BLOCK_SIZE) {
794 out = walk->dst.virt.addr;
795 in = walk->src.virt.addr;
796 while (nbytes >= AES_BLOCK_SIZE) {
Harald Freudenberger0519e9a2014-01-16 16:01:11 +0100797 if (ctrptr == ctrblk)
798 n = __ctrblk_init(ctrptr, nbytes);
799 else
800 n = AES_BLOCK_SIZE;
801 ret = crypt_s390_kmctr(func, sctx->key, out, in,
802 n, ctrptr);
803 if (ret < 0 || ret != n) {
804 if (ctrptr == ctrblk)
805 spin_unlock(&ctrblk_lock);
Jan Glauber36eb2ca2012-10-26 15:06:12 +0200806 return -EIO;
Harald Freudenberger0519e9a2014-01-16 16:01:11 +0100807 }
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000808 if (n > AES_BLOCK_SIZE)
Harald Freudenberger0519e9a2014-01-16 16:01:11 +0100809 memcpy(ctrptr, ctrptr + n - AES_BLOCK_SIZE,
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000810 AES_BLOCK_SIZE);
Harald Freudenberger0519e9a2014-01-16 16:01:11 +0100811 crypto_inc(ctrptr, AES_BLOCK_SIZE);
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000812 out += n;
813 in += n;
814 nbytes -= n;
815 }
816 ret = blkcipher_walk_done(desc, walk, nbytes);
817 }
Harald Freudenberger0519e9a2014-01-16 16:01:11 +0100818 if (ctrptr == ctrblk) {
819 if (nbytes)
820 memcpy(ctrbuf, ctrptr, AES_BLOCK_SIZE);
821 else
822 memcpy(walk->iv, ctrptr, AES_BLOCK_SIZE);
823 spin_unlock(&ctrblk_lock);
Harald Freudenberger3901c112014-05-07 16:51:29 +0200824 } else {
825 if (!nbytes)
826 memcpy(walk->iv, ctrptr, AES_BLOCK_SIZE);
Harald Freudenberger0519e9a2014-01-16 16:01:11 +0100827 }
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000828 /*
829 * final block may be < AES_BLOCK_SIZE, copy only nbytes
830 */
831 if (nbytes) {
832 out = walk->dst.virt.addr;
833 in = walk->src.virt.addr;
834 ret = crypt_s390_kmctr(func, sctx->key, buf, in,
Harald Freudenberger0519e9a2014-01-16 16:01:11 +0100835 AES_BLOCK_SIZE, ctrbuf);
Jan Glauber36eb2ca2012-10-26 15:06:12 +0200836 if (ret < 0 || ret != AES_BLOCK_SIZE)
837 return -EIO;
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000838 memcpy(out, buf, nbytes);
Harald Freudenberger0519e9a2014-01-16 16:01:11 +0100839 crypto_inc(ctrbuf, AES_BLOCK_SIZE);
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000840 ret = blkcipher_walk_done(desc, walk, 0);
Harald Freudenberger0519e9a2014-01-16 16:01:11 +0100841 memcpy(walk->iv, ctrbuf, AES_BLOCK_SIZE);
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000842 }
Harald Freudenberger0519e9a2014-01-16 16:01:11 +0100843
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000844 return ret;
845}
846
847static int ctr_aes_encrypt(struct blkcipher_desc *desc,
848 struct scatterlist *dst, struct scatterlist *src,
849 unsigned int nbytes)
850{
851 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
852 struct blkcipher_walk walk;
853
854 blkcipher_walk_init(&walk, dst, src, nbytes);
855 return ctr_aes_crypt(desc, sctx->enc, sctx, &walk);
856}
857
858static int ctr_aes_decrypt(struct blkcipher_desc *desc,
859 struct scatterlist *dst, struct scatterlist *src,
860 unsigned int nbytes)
861{
862 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
863 struct blkcipher_walk walk;
864
865 blkcipher_walk_init(&walk, dst, src, nbytes);
866 return ctr_aes_crypt(desc, sctx->dec, sctx, &walk);
867}
868
869static struct crypto_alg ctr_aes_alg = {
870 .cra_name = "ctr(aes)",
871 .cra_driver_name = "ctr-aes-s390",
872 .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
873 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
874 .cra_blocksize = 1,
875 .cra_ctxsize = sizeof(struct s390_aes_ctx),
876 .cra_type = &crypto_blkcipher_type,
877 .cra_module = THIS_MODULE,
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000878 .cra_u = {
879 .blkcipher = {
880 .min_keysize = AES_MIN_KEY_SIZE,
881 .max_keysize = AES_MAX_KEY_SIZE,
882 .ivsize = AES_BLOCK_SIZE,
883 .setkey = ctr_aes_set_key,
884 .encrypt = ctr_aes_encrypt,
885 .decrypt = ctr_aes_decrypt,
886 }
887 }
888};
889
Ingo Tuchscherer4f57ba72013-10-15 11:24:07 +0200890static int ctr_aes_alg_reg;
891
Heiko Carstens9f7819c2008-04-17 07:46:17 +0200892static int __init aes_s390_init(void)
Jan Glauberbf754ae2006-01-06 00:19:18 -0800893{
894 int ret;
895
Jan Glauber1822bc92011-04-19 21:29:14 +0200896 if (crypt_s390_func_available(KM_AES_128_ENCRYPT, CRYPT_S390_MSA))
Jan Glauber86aa9fc2007-02-05 21:18:14 +0100897 keylen_flag |= AES_KEYLEN_128;
Jan Glauber1822bc92011-04-19 21:29:14 +0200898 if (crypt_s390_func_available(KM_AES_192_ENCRYPT, CRYPT_S390_MSA))
Jan Glauber86aa9fc2007-02-05 21:18:14 +0100899 keylen_flag |= AES_KEYLEN_192;
Jan Glauber1822bc92011-04-19 21:29:14 +0200900 if (crypt_s390_func_available(KM_AES_256_ENCRYPT, CRYPT_S390_MSA))
Jan Glauber86aa9fc2007-02-05 21:18:14 +0100901 keylen_flag |= AES_KEYLEN_256;
Jan Glauberbf754ae2006-01-06 00:19:18 -0800902
Jan Glauber86aa9fc2007-02-05 21:18:14 +0100903 if (!keylen_flag)
904 return -EOPNOTSUPP;
905
906 /* z9 109 and z9 BC/EC only support 128 bit key length */
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100907 if (keylen_flag == AES_KEYLEN_128)
Jan Glauber39f09392008-12-25 13:39:37 +0100908 pr_info("AES hardware acceleration is only available for"
909 " 128-bit keys\n");
Jan Glauberbf754ae2006-01-06 00:19:18 -0800910
911 ret = crypto_register_alg(&aes_alg);
Jan Glauber86aa9fc2007-02-05 21:18:14 +0100912 if (ret)
Herbert Xua9e62fa2006-08-21 21:39:24 +1000913 goto aes_err;
Herbert Xua9e62fa2006-08-21 21:39:24 +1000914
915 ret = crypto_register_alg(&ecb_aes_alg);
Jan Glauber86aa9fc2007-02-05 21:18:14 +0100916 if (ret)
Herbert Xua9e62fa2006-08-21 21:39:24 +1000917 goto ecb_aes_err;
Herbert Xua9e62fa2006-08-21 21:39:24 +1000918
919 ret = crypto_register_alg(&cbc_aes_alg);
Jan Glauber86aa9fc2007-02-05 21:18:14 +0100920 if (ret)
Herbert Xua9e62fa2006-08-21 21:39:24 +1000921 goto cbc_aes_err;
Herbert Xua9e62fa2006-08-21 21:39:24 +1000922
Gerald Schaefer99d97222011-04-26 16:12:42 +1000923 if (crypt_s390_func_available(KM_XTS_128_ENCRYPT,
924 CRYPT_S390_MSA | CRYPT_S390_MSA4) &&
925 crypt_s390_func_available(KM_XTS_256_ENCRYPT,
926 CRYPT_S390_MSA | CRYPT_S390_MSA4)) {
927 ret = crypto_register_alg(&xts_aes_alg);
928 if (ret)
929 goto xts_aes_err;
Ingo Tuchscherer4f57ba72013-10-15 11:24:07 +0200930 xts_aes_alg_reg = 1;
Gerald Schaefer99d97222011-04-26 16:12:42 +1000931 }
932
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000933 if (crypt_s390_func_available(KMCTR_AES_128_ENCRYPT,
934 CRYPT_S390_MSA | CRYPT_S390_MSA4) &&
935 crypt_s390_func_available(KMCTR_AES_192_ENCRYPT,
936 CRYPT_S390_MSA | CRYPT_S390_MSA4) &&
937 crypt_s390_func_available(KMCTR_AES_256_ENCRYPT,
938 CRYPT_S390_MSA | CRYPT_S390_MSA4)) {
939 ctrblk = (u8 *) __get_free_page(GFP_KERNEL);
940 if (!ctrblk) {
941 ret = -ENOMEM;
942 goto ctr_aes_err;
943 }
944 ret = crypto_register_alg(&ctr_aes_alg);
945 if (ret) {
946 free_page((unsigned long) ctrblk);
947 goto ctr_aes_err;
948 }
Ingo Tuchscherer4f57ba72013-10-15 11:24:07 +0200949 ctr_aes_alg_reg = 1;
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000950 }
951
Herbert Xua9e62fa2006-08-21 21:39:24 +1000952out:
Jan Glauberbf754ae2006-01-06 00:19:18 -0800953 return ret;
Herbert Xua9e62fa2006-08-21 21:39:24 +1000954
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000955ctr_aes_err:
956 crypto_unregister_alg(&xts_aes_alg);
Gerald Schaefer99d97222011-04-26 16:12:42 +1000957xts_aes_err:
958 crypto_unregister_alg(&cbc_aes_alg);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000959cbc_aes_err:
960 crypto_unregister_alg(&ecb_aes_alg);
961ecb_aes_err:
962 crypto_unregister_alg(&aes_alg);
963aes_err:
964 goto out;
Jan Glauberbf754ae2006-01-06 00:19:18 -0800965}
966
Heiko Carstens9f7819c2008-04-17 07:46:17 +0200967static void __exit aes_s390_fini(void)
Jan Glauberbf754ae2006-01-06 00:19:18 -0800968{
Ingo Tuchscherer4f57ba72013-10-15 11:24:07 +0200969 if (ctr_aes_alg_reg) {
970 crypto_unregister_alg(&ctr_aes_alg);
971 free_page((unsigned long) ctrblk);
972 }
973 if (xts_aes_alg_reg)
974 crypto_unregister_alg(&xts_aes_alg);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000975 crypto_unregister_alg(&cbc_aes_alg);
976 crypto_unregister_alg(&ecb_aes_alg);
Jan Glauberbf754ae2006-01-06 00:19:18 -0800977 crypto_unregister_alg(&aes_alg);
978}
979
Hendrik Bruecknerd05377c2015-02-19 17:34:07 +0100980module_cpu_feature_match(MSA, aes_s390_init);
Heiko Carstens9f7819c2008-04-17 07:46:17 +0200981module_exit(aes_s390_fini);
Jan Glauberbf754ae2006-01-06 00:19:18 -0800982
Kees Cook5d26a102014-11-20 17:05:53 -0800983MODULE_ALIAS_CRYPTO("aes-all");
Jan Glauberbf754ae2006-01-06 00:19:18 -0800984
985MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm");
986MODULE_LICENSE("GPL");