4 * s390 implementation of the AES Cipher Algorithm.
7 * Copyright IBM Corp. 2005,2007
8 * Author(s): Jan Glauber (jang@de.ibm.com)
9 * Sebastian Siewior (sebastian@breakpoint.cc> SW-Fallback
11 * Derived from "crypto/aes_generic.c"
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)
20 #define KMSG_COMPONENT "aes_s390"
21 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
23 #include <crypto/aes.h>
24 #include <crypto/algapi.h>
25 #include <linux/err.h>
26 #include <linux/module.h>
27 #include <linux/init.h>
28 #include "crypt_s390.h"
30 #define AES_KEYLEN_128 1
31 #define AES_KEYLEN_192 2
32 #define AES_KEYLEN_256 4
35 static char keylen_flag
;
38 u8 iv
[AES_BLOCK_SIZE
];
39 u8 key
[AES_MAX_KEY_SIZE
];
44 struct crypto_blkcipher
*blk
;
45 struct crypto_cipher
*cip
;
64 struct crypto_blkcipher
*fallback
;
68 * Check if the key_len is supported by the HW.
69 * Returns 0 if it is, a positive number if it is not and software fallback is
70 * required or a negative number in case the key size is not valid
72 static int need_fallback(unsigned int key_len
)
76 if (!(keylen_flag
& AES_KEYLEN_128
))
80 if (!(keylen_flag
& AES_KEYLEN_192
))
84 if (!(keylen_flag
& AES_KEYLEN_256
))
94 static int setkey_fallback_cip(struct crypto_tfm
*tfm
, const u8
*in_key
,
97 struct s390_aes_ctx
*sctx
= crypto_tfm_ctx(tfm
);
100 sctx
->fallback
.cip
->base
.crt_flags
&= ~CRYPTO_TFM_REQ_MASK
;
101 sctx
->fallback
.cip
->base
.crt_flags
|= (tfm
->crt_flags
&
102 CRYPTO_TFM_REQ_MASK
);
104 ret
= crypto_cipher_setkey(sctx
->fallback
.cip
, in_key
, key_len
);
106 tfm
->crt_flags
&= ~CRYPTO_TFM_RES_MASK
;
107 tfm
->crt_flags
|= (sctx
->fallback
.cip
->base
.crt_flags
&
108 CRYPTO_TFM_RES_MASK
);
113 static int aes_set_key(struct crypto_tfm
*tfm
, const u8
*in_key
,
114 unsigned int key_len
)
116 struct s390_aes_ctx
*sctx
= crypto_tfm_ctx(tfm
);
117 u32
*flags
= &tfm
->crt_flags
;
120 ret
= need_fallback(key_len
);
122 *flags
|= CRYPTO_TFM_RES_BAD_KEY_LEN
;
126 sctx
->key_len
= key_len
;
128 memcpy(sctx
->key
, in_key
, key_len
);
132 return setkey_fallback_cip(tfm
, in_key
, key_len
);
135 static void aes_encrypt(struct crypto_tfm
*tfm
, u8
*out
, const u8
*in
)
137 const struct s390_aes_ctx
*sctx
= crypto_tfm_ctx(tfm
);
139 if (unlikely(need_fallback(sctx
->key_len
))) {
140 crypto_cipher_encrypt_one(sctx
->fallback
.cip
, out
, in
);
144 switch (sctx
->key_len
) {
146 crypt_s390_km(KM_AES_128_ENCRYPT
, &sctx
->key
, out
, in
,
150 crypt_s390_km(KM_AES_192_ENCRYPT
, &sctx
->key
, out
, in
,
154 crypt_s390_km(KM_AES_256_ENCRYPT
, &sctx
->key
, out
, in
,
160 static void aes_decrypt(struct crypto_tfm
*tfm
, u8
*out
, const u8
*in
)
162 const struct s390_aes_ctx
*sctx
= crypto_tfm_ctx(tfm
);
164 if (unlikely(need_fallback(sctx
->key_len
))) {
165 crypto_cipher_decrypt_one(sctx
->fallback
.cip
, out
, in
);
169 switch (sctx
->key_len
) {
171 crypt_s390_km(KM_AES_128_DECRYPT
, &sctx
->key
, out
, in
,
175 crypt_s390_km(KM_AES_192_DECRYPT
, &sctx
->key
, out
, in
,
179 crypt_s390_km(KM_AES_256_DECRYPT
, &sctx
->key
, out
, in
,
185 static int fallback_init_cip(struct crypto_tfm
*tfm
)
187 const char *name
= tfm
->__crt_alg
->cra_name
;
188 struct s390_aes_ctx
*sctx
= crypto_tfm_ctx(tfm
);
190 sctx
->fallback
.cip
= crypto_alloc_cipher(name
, 0,
191 CRYPTO_ALG_ASYNC
| CRYPTO_ALG_NEED_FALLBACK
);
193 if (IS_ERR(sctx
->fallback
.cip
)) {
194 pr_err("Allocating AES fallback algorithm %s failed\n",
196 return PTR_ERR(sctx
->fallback
.cip
);
202 static void fallback_exit_cip(struct crypto_tfm
*tfm
)
204 struct s390_aes_ctx
*sctx
= crypto_tfm_ctx(tfm
);
206 crypto_free_cipher(sctx
->fallback
.cip
);
207 sctx
->fallback
.cip
= NULL
;
210 static struct crypto_alg aes_alg
= {
212 .cra_driver_name
= "aes-s390",
213 .cra_priority
= CRYPT_S390_PRIORITY
,
214 .cra_flags
= CRYPTO_ALG_TYPE_CIPHER
|
215 CRYPTO_ALG_NEED_FALLBACK
,
216 .cra_blocksize
= AES_BLOCK_SIZE
,
217 .cra_ctxsize
= sizeof(struct s390_aes_ctx
),
218 .cra_module
= THIS_MODULE
,
219 .cra_list
= LIST_HEAD_INIT(aes_alg
.cra_list
),
220 .cra_init
= fallback_init_cip
,
221 .cra_exit
= fallback_exit_cip
,
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
,
233 static int setkey_fallback_blk(struct crypto_tfm
*tfm
, const u8
*key
,
236 struct s390_aes_ctx
*sctx
= crypto_tfm_ctx(tfm
);
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
);
243 ret
= crypto_blkcipher_setkey(sctx
->fallback
.blk
, key
, len
);
245 tfm
->crt_flags
&= ~CRYPTO_TFM_RES_MASK
;
246 tfm
->crt_flags
|= (sctx
->fallback
.blk
->base
.crt_flags
&
247 CRYPTO_TFM_RES_MASK
);
252 static int fallback_blk_dec(struct blkcipher_desc
*desc
,
253 struct scatterlist
*dst
, struct scatterlist
*src
,
257 struct crypto_blkcipher
*tfm
;
258 struct s390_aes_ctx
*sctx
= crypto_blkcipher_ctx(desc
->tfm
);
261 desc
->tfm
= sctx
->fallback
.blk
;
263 ret
= crypto_blkcipher_decrypt_iv(desc
, dst
, src
, nbytes
);
269 static int fallback_blk_enc(struct blkcipher_desc
*desc
,
270 struct scatterlist
*dst
, struct scatterlist
*src
,
274 struct crypto_blkcipher
*tfm
;
275 struct s390_aes_ctx
*sctx
= crypto_blkcipher_ctx(desc
->tfm
);
278 desc
->tfm
= sctx
->fallback
.blk
;
280 ret
= crypto_blkcipher_encrypt_iv(desc
, dst
, src
, nbytes
);
286 static int ecb_aes_set_key(struct crypto_tfm
*tfm
, const u8
*in_key
,
287 unsigned int key_len
)
289 struct s390_aes_ctx
*sctx
= crypto_tfm_ctx(tfm
);
292 ret
= need_fallback(key_len
);
294 sctx
->key_len
= key_len
;
295 return setkey_fallback_blk(tfm
, in_key
, key_len
);
300 sctx
->enc
= KM_AES_128_ENCRYPT
;
301 sctx
->dec
= KM_AES_128_DECRYPT
;
304 sctx
->enc
= KM_AES_192_ENCRYPT
;
305 sctx
->dec
= KM_AES_192_DECRYPT
;
308 sctx
->enc
= KM_AES_256_ENCRYPT
;
309 sctx
->dec
= KM_AES_256_DECRYPT
;
313 return aes_set_key(tfm
, in_key
, key_len
);
316 static int ecb_aes_crypt(struct blkcipher_desc
*desc
, long func
, void *param
,
317 struct blkcipher_walk
*walk
)
319 int ret
= blkcipher_walk_virt(desc
, walk
);
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
;
328 ret
= crypt_s390_km(func
, param
, out
, in
, n
);
329 BUG_ON((ret
< 0) || (ret
!= n
));
331 nbytes
&= AES_BLOCK_SIZE
- 1;
332 ret
= blkcipher_walk_done(desc
, walk
, nbytes
);
338 static int ecb_aes_encrypt(struct blkcipher_desc
*desc
,
339 struct scatterlist
*dst
, struct scatterlist
*src
,
342 struct s390_aes_ctx
*sctx
= crypto_blkcipher_ctx(desc
->tfm
);
343 struct blkcipher_walk walk
;
345 if (unlikely(need_fallback(sctx
->key_len
)))
346 return fallback_blk_enc(desc
, dst
, src
, nbytes
);
348 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
349 return ecb_aes_crypt(desc
, sctx
->enc
, sctx
->key
, &walk
);
352 static int ecb_aes_decrypt(struct blkcipher_desc
*desc
,
353 struct scatterlist
*dst
, struct scatterlist
*src
,
356 struct s390_aes_ctx
*sctx
= crypto_blkcipher_ctx(desc
->tfm
);
357 struct blkcipher_walk walk
;
359 if (unlikely(need_fallback(sctx
->key_len
)))
360 return fallback_blk_dec(desc
, dst
, src
, nbytes
);
362 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
363 return ecb_aes_crypt(desc
, sctx
->dec
, sctx
->key
, &walk
);
366 static int fallback_init_blk(struct crypto_tfm
*tfm
)
368 const char *name
= tfm
->__crt_alg
->cra_name
;
369 struct s390_aes_ctx
*sctx
= crypto_tfm_ctx(tfm
);
371 sctx
->fallback
.blk
= crypto_alloc_blkcipher(name
, 0,
372 CRYPTO_ALG_ASYNC
| CRYPTO_ALG_NEED_FALLBACK
);
374 if (IS_ERR(sctx
->fallback
.blk
)) {
375 pr_err("Allocating AES fallback algorithm %s failed\n",
377 return PTR_ERR(sctx
->fallback
.blk
);
383 static void fallback_exit_blk(struct crypto_tfm
*tfm
)
385 struct s390_aes_ctx
*sctx
= crypto_tfm_ctx(tfm
);
387 crypto_free_blkcipher(sctx
->fallback
.blk
);
388 sctx
->fallback
.blk
= NULL
;
391 static struct crypto_alg ecb_aes_alg
= {
392 .cra_name
= "ecb(aes)",
393 .cra_driver_name
= "ecb-aes-s390",
394 .cra_priority
= CRYPT_S390_COMPOSITE_PRIORITY
,
395 .cra_flags
= CRYPTO_ALG_TYPE_BLKCIPHER
|
396 CRYPTO_ALG_NEED_FALLBACK
,
397 .cra_blocksize
= AES_BLOCK_SIZE
,
398 .cra_ctxsize
= sizeof(struct s390_aes_ctx
),
399 .cra_type
= &crypto_blkcipher_type
,
400 .cra_module
= THIS_MODULE
,
401 .cra_list
= LIST_HEAD_INIT(ecb_aes_alg
.cra_list
),
402 .cra_init
= fallback_init_blk
,
403 .cra_exit
= fallback_exit_blk
,
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
,
415 static int cbc_aes_set_key(struct crypto_tfm
*tfm
, const u8
*in_key
,
416 unsigned int key_len
)
418 struct s390_aes_ctx
*sctx
= crypto_tfm_ctx(tfm
);
421 ret
= need_fallback(key_len
);
423 sctx
->key_len
= key_len
;
424 return setkey_fallback_blk(tfm
, in_key
, key_len
);
429 sctx
->enc
= KMC_AES_128_ENCRYPT
;
430 sctx
->dec
= KMC_AES_128_DECRYPT
;
433 sctx
->enc
= KMC_AES_192_ENCRYPT
;
434 sctx
->dec
= KMC_AES_192_DECRYPT
;
437 sctx
->enc
= KMC_AES_256_ENCRYPT
;
438 sctx
->dec
= KMC_AES_256_DECRYPT
;
442 return aes_set_key(tfm
, in_key
, key_len
);
445 static int cbc_aes_crypt(struct blkcipher_desc
*desc
, long func
, void *param
,
446 struct blkcipher_walk
*walk
)
448 int ret
= blkcipher_walk_virt(desc
, walk
);
449 unsigned int nbytes
= walk
->nbytes
;
454 memcpy(param
, walk
->iv
, AES_BLOCK_SIZE
);
456 /* only use complete blocks */
457 unsigned int n
= nbytes
& ~(AES_BLOCK_SIZE
- 1);
458 u8
*out
= walk
->dst
.virt
.addr
;
459 u8
*in
= walk
->src
.virt
.addr
;
461 ret
= crypt_s390_kmc(func
, param
, out
, in
, n
);
462 BUG_ON((ret
< 0) || (ret
!= n
));
464 nbytes
&= AES_BLOCK_SIZE
- 1;
465 ret
= blkcipher_walk_done(desc
, walk
, nbytes
);
466 } while ((nbytes
= walk
->nbytes
));
467 memcpy(walk
->iv
, param
, AES_BLOCK_SIZE
);
473 static int cbc_aes_encrypt(struct blkcipher_desc
*desc
,
474 struct scatterlist
*dst
, struct scatterlist
*src
,
477 struct s390_aes_ctx
*sctx
= crypto_blkcipher_ctx(desc
->tfm
);
478 struct blkcipher_walk walk
;
480 if (unlikely(need_fallback(sctx
->key_len
)))
481 return fallback_blk_enc(desc
, dst
, src
, nbytes
);
483 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
484 return cbc_aes_crypt(desc
, sctx
->enc
, sctx
->iv
, &walk
);
487 static int cbc_aes_decrypt(struct blkcipher_desc
*desc
,
488 struct scatterlist
*dst
, struct scatterlist
*src
,
491 struct s390_aes_ctx
*sctx
= crypto_blkcipher_ctx(desc
->tfm
);
492 struct blkcipher_walk walk
;
494 if (unlikely(need_fallback(sctx
->key_len
)))
495 return fallback_blk_dec(desc
, dst
, src
, nbytes
);
497 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
498 return cbc_aes_crypt(desc
, sctx
->dec
, sctx
->iv
, &walk
);
501 static struct crypto_alg cbc_aes_alg
= {
502 .cra_name
= "cbc(aes)",
503 .cra_driver_name
= "cbc-aes-s390",
504 .cra_priority
= CRYPT_S390_COMPOSITE_PRIORITY
,
505 .cra_flags
= CRYPTO_ALG_TYPE_BLKCIPHER
|
506 CRYPTO_ALG_NEED_FALLBACK
,
507 .cra_blocksize
= AES_BLOCK_SIZE
,
508 .cra_ctxsize
= sizeof(struct s390_aes_ctx
),
509 .cra_type
= &crypto_blkcipher_type
,
510 .cra_module
= THIS_MODULE
,
511 .cra_list
= LIST_HEAD_INIT(cbc_aes_alg
.cra_list
),
512 .cra_init
= fallback_init_blk
,
513 .cra_exit
= fallback_exit_blk
,
516 .min_keysize
= AES_MIN_KEY_SIZE
,
517 .max_keysize
= AES_MAX_KEY_SIZE
,
518 .ivsize
= AES_BLOCK_SIZE
,
519 .setkey
= cbc_aes_set_key
,
520 .encrypt
= cbc_aes_encrypt
,
521 .decrypt
= cbc_aes_decrypt
,
526 static int xts_fallback_setkey(struct crypto_tfm
*tfm
, const u8
*key
,
529 struct s390_xts_ctx
*xts_ctx
= crypto_tfm_ctx(tfm
);
532 xts_ctx
->fallback
->base
.crt_flags
&= ~CRYPTO_TFM_REQ_MASK
;
533 xts_ctx
->fallback
->base
.crt_flags
|= (tfm
->crt_flags
&
534 CRYPTO_TFM_REQ_MASK
);
536 ret
= crypto_blkcipher_setkey(xts_ctx
->fallback
, key
, len
);
538 tfm
->crt_flags
&= ~CRYPTO_TFM_RES_MASK
;
539 tfm
->crt_flags
|= (xts_ctx
->fallback
->base
.crt_flags
&
540 CRYPTO_TFM_RES_MASK
);
545 static int xts_fallback_decrypt(struct blkcipher_desc
*desc
,
546 struct scatterlist
*dst
, struct scatterlist
*src
,
549 struct s390_xts_ctx
*xts_ctx
= crypto_blkcipher_ctx(desc
->tfm
);
550 struct crypto_blkcipher
*tfm
;
554 desc
->tfm
= xts_ctx
->fallback
;
556 ret
= crypto_blkcipher_decrypt_iv(desc
, dst
, src
, nbytes
);
562 static int xts_fallback_encrypt(struct blkcipher_desc
*desc
,
563 struct scatterlist
*dst
, struct scatterlist
*src
,
566 struct s390_xts_ctx
*xts_ctx
= crypto_blkcipher_ctx(desc
->tfm
);
567 struct crypto_blkcipher
*tfm
;
571 desc
->tfm
= xts_ctx
->fallback
;
573 ret
= crypto_blkcipher_encrypt_iv(desc
, dst
, src
, nbytes
);
579 static int xts_aes_set_key(struct crypto_tfm
*tfm
, const u8
*in_key
,
580 unsigned int key_len
)
582 struct s390_xts_ctx
*xts_ctx
= crypto_tfm_ctx(tfm
);
583 u32
*flags
= &tfm
->crt_flags
;
587 xts_ctx
->enc
= KM_XTS_128_ENCRYPT
;
588 xts_ctx
->dec
= KM_XTS_128_DECRYPT
;
589 memcpy(xts_ctx
->key
+ 16, in_key
, 16);
590 memcpy(xts_ctx
->pcc
.key
+ 16, in_key
+ 16, 16);
595 xts_fallback_setkey(tfm
, in_key
, key_len
);
598 xts_ctx
->enc
= KM_XTS_256_ENCRYPT
;
599 xts_ctx
->dec
= KM_XTS_256_DECRYPT
;
600 memcpy(xts_ctx
->key
, in_key
, 32);
601 memcpy(xts_ctx
->pcc
.key
, in_key
+ 32, 32);
604 *flags
|= CRYPTO_TFM_RES_BAD_KEY_LEN
;
607 xts_ctx
->key_len
= key_len
;
611 static int xts_aes_crypt(struct blkcipher_desc
*desc
, long func
,
612 struct s390_xts_ctx
*xts_ctx
,
613 struct blkcipher_walk
*walk
)
615 unsigned int offset
= (xts_ctx
->key_len
>> 1) & 0x10;
616 int ret
= blkcipher_walk_virt(desc
, walk
);
617 unsigned int nbytes
= walk
->nbytes
;
625 memset(xts_ctx
->pcc
.block
, 0, sizeof(xts_ctx
->pcc
.block
));
626 memset(xts_ctx
->pcc
.bit
, 0, sizeof(xts_ctx
->pcc
.bit
));
627 memset(xts_ctx
->pcc
.xts
, 0, sizeof(xts_ctx
->pcc
.xts
));
628 memcpy(xts_ctx
->pcc
.tweak
, walk
->iv
, sizeof(xts_ctx
->pcc
.tweak
));
629 param
= xts_ctx
->pcc
.key
+ offset
;
630 ret
= crypt_s390_pcc(func
, param
);
633 memcpy(xts_ctx
->xts_param
, xts_ctx
->pcc
.xts
, 16);
634 param
= xts_ctx
->key
+ offset
;
636 /* only use complete blocks */
637 n
= nbytes
& ~(AES_BLOCK_SIZE
- 1);
638 out
= walk
->dst
.virt
.addr
;
639 in
= walk
->src
.virt
.addr
;
641 ret
= crypt_s390_km(func
, param
, out
, in
, n
);
642 BUG_ON(ret
< 0 || ret
!= n
);
644 nbytes
&= AES_BLOCK_SIZE
- 1;
645 ret
= blkcipher_walk_done(desc
, walk
, nbytes
);
646 } while ((nbytes
= walk
->nbytes
));
651 static int xts_aes_encrypt(struct blkcipher_desc
*desc
,
652 struct scatterlist
*dst
, struct scatterlist
*src
,
655 struct s390_xts_ctx
*xts_ctx
= crypto_blkcipher_ctx(desc
->tfm
);
656 struct blkcipher_walk walk
;
658 if (unlikely(xts_ctx
->key_len
== 48))
659 return xts_fallback_encrypt(desc
, dst
, src
, nbytes
);
661 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
662 return xts_aes_crypt(desc
, xts_ctx
->enc
, xts_ctx
, &walk
);
665 static int xts_aes_decrypt(struct blkcipher_desc
*desc
,
666 struct scatterlist
*dst
, struct scatterlist
*src
,
669 struct s390_xts_ctx
*xts_ctx
= crypto_blkcipher_ctx(desc
->tfm
);
670 struct blkcipher_walk walk
;
672 if (unlikely(xts_ctx
->key_len
== 48))
673 return xts_fallback_decrypt(desc
, dst
, src
, nbytes
);
675 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
676 return xts_aes_crypt(desc
, xts_ctx
->dec
, xts_ctx
, &walk
);
679 static int xts_fallback_init(struct crypto_tfm
*tfm
)
681 const char *name
= tfm
->__crt_alg
->cra_name
;
682 struct s390_xts_ctx
*xts_ctx
= crypto_tfm_ctx(tfm
);
684 xts_ctx
->fallback
= crypto_alloc_blkcipher(name
, 0,
685 CRYPTO_ALG_ASYNC
| CRYPTO_ALG_NEED_FALLBACK
);
687 if (IS_ERR(xts_ctx
->fallback
)) {
688 pr_err("Allocating XTS fallback algorithm %s failed\n",
690 return PTR_ERR(xts_ctx
->fallback
);
695 static void xts_fallback_exit(struct crypto_tfm
*tfm
)
697 struct s390_xts_ctx
*xts_ctx
= crypto_tfm_ctx(tfm
);
699 crypto_free_blkcipher(xts_ctx
->fallback
);
700 xts_ctx
->fallback
= NULL
;
703 static struct crypto_alg xts_aes_alg
= {
704 .cra_name
= "xts(aes)",
705 .cra_driver_name
= "xts-aes-s390",
706 .cra_priority
= CRYPT_S390_COMPOSITE_PRIORITY
,
707 .cra_flags
= CRYPTO_ALG_TYPE_BLKCIPHER
|
708 CRYPTO_ALG_NEED_FALLBACK
,
709 .cra_blocksize
= AES_BLOCK_SIZE
,
710 .cra_ctxsize
= sizeof(struct s390_xts_ctx
),
711 .cra_type
= &crypto_blkcipher_type
,
712 .cra_module
= THIS_MODULE
,
713 .cra_list
= LIST_HEAD_INIT(xts_aes_alg
.cra_list
),
714 .cra_init
= xts_fallback_init
,
715 .cra_exit
= xts_fallback_exit
,
718 .min_keysize
= 2 * AES_MIN_KEY_SIZE
,
719 .max_keysize
= 2 * AES_MAX_KEY_SIZE
,
720 .ivsize
= AES_BLOCK_SIZE
,
721 .setkey
= xts_aes_set_key
,
722 .encrypt
= xts_aes_encrypt
,
723 .decrypt
= xts_aes_decrypt
,
728 static int ctr_aes_set_key(struct crypto_tfm
*tfm
, const u8
*in_key
,
729 unsigned int key_len
)
731 struct s390_aes_ctx
*sctx
= crypto_tfm_ctx(tfm
);
735 sctx
->enc
= KMCTR_AES_128_ENCRYPT
;
736 sctx
->dec
= KMCTR_AES_128_DECRYPT
;
739 sctx
->enc
= KMCTR_AES_192_ENCRYPT
;
740 sctx
->dec
= KMCTR_AES_192_DECRYPT
;
743 sctx
->enc
= KMCTR_AES_256_ENCRYPT
;
744 sctx
->dec
= KMCTR_AES_256_DECRYPT
;
748 return aes_set_key(tfm
, in_key
, key_len
);
751 static int ctr_aes_crypt(struct blkcipher_desc
*desc
, long func
,
752 struct s390_aes_ctx
*sctx
, struct blkcipher_walk
*walk
)
754 int ret
= blkcipher_walk_virt_block(desc
, walk
, AES_BLOCK_SIZE
);
755 unsigned int i
, n
, nbytes
;
756 u8 buf
[AES_BLOCK_SIZE
];
762 memcpy(ctrblk
, walk
->iv
, AES_BLOCK_SIZE
);
763 while ((nbytes
= walk
->nbytes
) >= AES_BLOCK_SIZE
) {
764 out
= walk
->dst
.virt
.addr
;
765 in
= walk
->src
.virt
.addr
;
766 while (nbytes
>= AES_BLOCK_SIZE
) {
767 /* only use complete blocks, max. PAGE_SIZE */
768 n
= (nbytes
> PAGE_SIZE
) ? PAGE_SIZE
:
769 nbytes
& ~(AES_BLOCK_SIZE
- 1);
770 for (i
= AES_BLOCK_SIZE
; i
< n
; i
+= AES_BLOCK_SIZE
) {
771 memcpy(ctrblk
+ i
, ctrblk
+ i
- AES_BLOCK_SIZE
,
773 crypto_inc(ctrblk
+ i
, AES_BLOCK_SIZE
);
775 ret
= crypt_s390_kmctr(func
, sctx
->key
, out
, in
, n
, ctrblk
);
776 BUG_ON(ret
< 0 || ret
!= n
);
777 if (n
> AES_BLOCK_SIZE
)
778 memcpy(ctrblk
, ctrblk
+ n
- AES_BLOCK_SIZE
,
780 crypto_inc(ctrblk
, AES_BLOCK_SIZE
);
785 ret
= blkcipher_walk_done(desc
, walk
, nbytes
);
788 * final block may be < AES_BLOCK_SIZE, copy only nbytes
791 out
= walk
->dst
.virt
.addr
;
792 in
= walk
->src
.virt
.addr
;
793 ret
= crypt_s390_kmctr(func
, sctx
->key
, buf
, in
,
794 AES_BLOCK_SIZE
, ctrblk
);
795 BUG_ON(ret
< 0 || ret
!= AES_BLOCK_SIZE
);
796 memcpy(out
, buf
, nbytes
);
797 crypto_inc(ctrblk
, AES_BLOCK_SIZE
);
798 ret
= blkcipher_walk_done(desc
, walk
, 0);
800 memcpy(walk
->iv
, ctrblk
, AES_BLOCK_SIZE
);
804 static int ctr_aes_encrypt(struct blkcipher_desc
*desc
,
805 struct scatterlist
*dst
, struct scatterlist
*src
,
808 struct s390_aes_ctx
*sctx
= crypto_blkcipher_ctx(desc
->tfm
);
809 struct blkcipher_walk walk
;
811 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
812 return ctr_aes_crypt(desc
, sctx
->enc
, sctx
, &walk
);
815 static int ctr_aes_decrypt(struct blkcipher_desc
*desc
,
816 struct scatterlist
*dst
, struct scatterlist
*src
,
819 struct s390_aes_ctx
*sctx
= crypto_blkcipher_ctx(desc
->tfm
);
820 struct blkcipher_walk walk
;
822 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
823 return ctr_aes_crypt(desc
, sctx
->dec
, sctx
, &walk
);
826 static struct crypto_alg ctr_aes_alg
= {
827 .cra_name
= "ctr(aes)",
828 .cra_driver_name
= "ctr-aes-s390",
829 .cra_priority
= CRYPT_S390_COMPOSITE_PRIORITY
,
830 .cra_flags
= CRYPTO_ALG_TYPE_BLKCIPHER
,
832 .cra_ctxsize
= sizeof(struct s390_aes_ctx
),
833 .cra_type
= &crypto_blkcipher_type
,
834 .cra_module
= THIS_MODULE
,
835 .cra_list
= LIST_HEAD_INIT(ctr_aes_alg
.cra_list
),
838 .min_keysize
= AES_MIN_KEY_SIZE
,
839 .max_keysize
= AES_MAX_KEY_SIZE
,
840 .ivsize
= AES_BLOCK_SIZE
,
841 .setkey
= ctr_aes_set_key
,
842 .encrypt
= ctr_aes_encrypt
,
843 .decrypt
= ctr_aes_decrypt
,
848 static int __init
aes_s390_init(void)
852 if (crypt_s390_func_available(KM_AES_128_ENCRYPT
, CRYPT_S390_MSA
))
853 keylen_flag
|= AES_KEYLEN_128
;
854 if (crypt_s390_func_available(KM_AES_192_ENCRYPT
, CRYPT_S390_MSA
))
855 keylen_flag
|= AES_KEYLEN_192
;
856 if (crypt_s390_func_available(KM_AES_256_ENCRYPT
, CRYPT_S390_MSA
))
857 keylen_flag
|= AES_KEYLEN_256
;
862 /* z9 109 and z9 BC/EC only support 128 bit key length */
863 if (keylen_flag
== AES_KEYLEN_128
)
864 pr_info("AES hardware acceleration is only available for"
867 ret
= crypto_register_alg(&aes_alg
);
871 ret
= crypto_register_alg(&ecb_aes_alg
);
875 ret
= crypto_register_alg(&cbc_aes_alg
);
879 if (crypt_s390_func_available(KM_XTS_128_ENCRYPT
,
880 CRYPT_S390_MSA
| CRYPT_S390_MSA4
) &&
881 crypt_s390_func_available(KM_XTS_256_ENCRYPT
,
882 CRYPT_S390_MSA
| CRYPT_S390_MSA4
)) {
883 ret
= crypto_register_alg(&xts_aes_alg
);
888 if (crypt_s390_func_available(KMCTR_AES_128_ENCRYPT
,
889 CRYPT_S390_MSA
| CRYPT_S390_MSA4
) &&
890 crypt_s390_func_available(KMCTR_AES_192_ENCRYPT
,
891 CRYPT_S390_MSA
| CRYPT_S390_MSA4
) &&
892 crypt_s390_func_available(KMCTR_AES_256_ENCRYPT
,
893 CRYPT_S390_MSA
| CRYPT_S390_MSA4
)) {
894 ctrblk
= (u8
*) __get_free_page(GFP_KERNEL
);
899 ret
= crypto_register_alg(&ctr_aes_alg
);
901 free_page((unsigned long) ctrblk
);
910 crypto_unregister_alg(&xts_aes_alg
);
912 crypto_unregister_alg(&cbc_aes_alg
);
914 crypto_unregister_alg(&ecb_aes_alg
);
916 crypto_unregister_alg(&aes_alg
);
921 static void __exit
aes_s390_fini(void)
923 crypto_unregister_alg(&ctr_aes_alg
);
924 free_page((unsigned long) ctrblk
);
925 crypto_unregister_alg(&xts_aes_alg
);
926 crypto_unregister_alg(&cbc_aes_alg
);
927 crypto_unregister_alg(&ecb_aes_alg
);
928 crypto_unregister_alg(&aes_alg
);
931 module_init(aes_s390_init
);
932 module_exit(aes_s390_fini
);
934 MODULE_ALIAS("aes-all");
936 MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm");
937 MODULE_LICENSE("GPL");