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_init
= fallback_init_cip
,
220 .cra_exit
= fallback_exit_cip
,
223 .cia_min_keysize
= AES_MIN_KEY_SIZE
,
224 .cia_max_keysize
= AES_MAX_KEY_SIZE
,
225 .cia_setkey
= aes_set_key
,
226 .cia_encrypt
= aes_encrypt
,
227 .cia_decrypt
= aes_decrypt
,
232 static int setkey_fallback_blk(struct crypto_tfm
*tfm
, const u8
*key
,
235 struct s390_aes_ctx
*sctx
= crypto_tfm_ctx(tfm
);
238 sctx
->fallback
.blk
->base
.crt_flags
&= ~CRYPTO_TFM_REQ_MASK
;
239 sctx
->fallback
.blk
->base
.crt_flags
|= (tfm
->crt_flags
&
240 CRYPTO_TFM_REQ_MASK
);
242 ret
= crypto_blkcipher_setkey(sctx
->fallback
.blk
, key
, len
);
244 tfm
->crt_flags
&= ~CRYPTO_TFM_RES_MASK
;
245 tfm
->crt_flags
|= (sctx
->fallback
.blk
->base
.crt_flags
&
246 CRYPTO_TFM_RES_MASK
);
251 static int fallback_blk_dec(struct blkcipher_desc
*desc
,
252 struct scatterlist
*dst
, struct scatterlist
*src
,
256 struct crypto_blkcipher
*tfm
;
257 struct s390_aes_ctx
*sctx
= crypto_blkcipher_ctx(desc
->tfm
);
260 desc
->tfm
= sctx
->fallback
.blk
;
262 ret
= crypto_blkcipher_decrypt_iv(desc
, dst
, src
, nbytes
);
268 static int fallback_blk_enc(struct blkcipher_desc
*desc
,
269 struct scatterlist
*dst
, struct scatterlist
*src
,
273 struct crypto_blkcipher
*tfm
;
274 struct s390_aes_ctx
*sctx
= crypto_blkcipher_ctx(desc
->tfm
);
277 desc
->tfm
= sctx
->fallback
.blk
;
279 ret
= crypto_blkcipher_encrypt_iv(desc
, dst
, src
, nbytes
);
285 static int ecb_aes_set_key(struct crypto_tfm
*tfm
, const u8
*in_key
,
286 unsigned int key_len
)
288 struct s390_aes_ctx
*sctx
= crypto_tfm_ctx(tfm
);
291 ret
= need_fallback(key_len
);
293 sctx
->key_len
= key_len
;
294 return setkey_fallback_blk(tfm
, in_key
, key_len
);
299 sctx
->enc
= KM_AES_128_ENCRYPT
;
300 sctx
->dec
= KM_AES_128_DECRYPT
;
303 sctx
->enc
= KM_AES_192_ENCRYPT
;
304 sctx
->dec
= KM_AES_192_DECRYPT
;
307 sctx
->enc
= KM_AES_256_ENCRYPT
;
308 sctx
->dec
= KM_AES_256_DECRYPT
;
312 return aes_set_key(tfm
, in_key
, key_len
);
315 static int ecb_aes_crypt(struct blkcipher_desc
*desc
, long func
, void *param
,
316 struct blkcipher_walk
*walk
)
318 int ret
= blkcipher_walk_virt(desc
, walk
);
321 while ((nbytes
= walk
->nbytes
)) {
322 /* only use complete blocks */
323 unsigned int n
= nbytes
& ~(AES_BLOCK_SIZE
- 1);
324 u8
*out
= walk
->dst
.virt
.addr
;
325 u8
*in
= walk
->src
.virt
.addr
;
327 ret
= crypt_s390_km(func
, param
, out
, in
, n
);
328 BUG_ON((ret
< 0) || (ret
!= n
));
330 nbytes
&= AES_BLOCK_SIZE
- 1;
331 ret
= blkcipher_walk_done(desc
, walk
, nbytes
);
337 static int ecb_aes_encrypt(struct blkcipher_desc
*desc
,
338 struct scatterlist
*dst
, struct scatterlist
*src
,
341 struct s390_aes_ctx
*sctx
= crypto_blkcipher_ctx(desc
->tfm
);
342 struct blkcipher_walk walk
;
344 if (unlikely(need_fallback(sctx
->key_len
)))
345 return fallback_blk_enc(desc
, dst
, src
, nbytes
);
347 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
348 return ecb_aes_crypt(desc
, sctx
->enc
, sctx
->key
, &walk
);
351 static int ecb_aes_decrypt(struct blkcipher_desc
*desc
,
352 struct scatterlist
*dst
, struct scatterlist
*src
,
355 struct s390_aes_ctx
*sctx
= crypto_blkcipher_ctx(desc
->tfm
);
356 struct blkcipher_walk walk
;
358 if (unlikely(need_fallback(sctx
->key_len
)))
359 return fallback_blk_dec(desc
, dst
, src
, nbytes
);
361 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
362 return ecb_aes_crypt(desc
, sctx
->dec
, sctx
->key
, &walk
);
365 static int fallback_init_blk(struct crypto_tfm
*tfm
)
367 const char *name
= tfm
->__crt_alg
->cra_name
;
368 struct s390_aes_ctx
*sctx
= crypto_tfm_ctx(tfm
);
370 sctx
->fallback
.blk
= crypto_alloc_blkcipher(name
, 0,
371 CRYPTO_ALG_ASYNC
| CRYPTO_ALG_NEED_FALLBACK
);
373 if (IS_ERR(sctx
->fallback
.blk
)) {
374 pr_err("Allocating AES fallback algorithm %s failed\n",
376 return PTR_ERR(sctx
->fallback
.blk
);
382 static void fallback_exit_blk(struct crypto_tfm
*tfm
)
384 struct s390_aes_ctx
*sctx
= crypto_tfm_ctx(tfm
);
386 crypto_free_blkcipher(sctx
->fallback
.blk
);
387 sctx
->fallback
.blk
= NULL
;
390 static struct crypto_alg ecb_aes_alg
= {
391 .cra_name
= "ecb(aes)",
392 .cra_driver_name
= "ecb-aes-s390",
393 .cra_priority
= CRYPT_S390_COMPOSITE_PRIORITY
,
394 .cra_flags
= CRYPTO_ALG_TYPE_BLKCIPHER
|
395 CRYPTO_ALG_NEED_FALLBACK
,
396 .cra_blocksize
= AES_BLOCK_SIZE
,
397 .cra_ctxsize
= sizeof(struct s390_aes_ctx
),
398 .cra_type
= &crypto_blkcipher_type
,
399 .cra_module
= THIS_MODULE
,
400 .cra_init
= fallback_init_blk
,
401 .cra_exit
= fallback_exit_blk
,
404 .min_keysize
= AES_MIN_KEY_SIZE
,
405 .max_keysize
= AES_MAX_KEY_SIZE
,
406 .setkey
= ecb_aes_set_key
,
407 .encrypt
= ecb_aes_encrypt
,
408 .decrypt
= ecb_aes_decrypt
,
413 static int cbc_aes_set_key(struct crypto_tfm
*tfm
, const u8
*in_key
,
414 unsigned int key_len
)
416 struct s390_aes_ctx
*sctx
= crypto_tfm_ctx(tfm
);
419 ret
= need_fallback(key_len
);
421 sctx
->key_len
= key_len
;
422 return setkey_fallback_blk(tfm
, in_key
, key_len
);
427 sctx
->enc
= KMC_AES_128_ENCRYPT
;
428 sctx
->dec
= KMC_AES_128_DECRYPT
;
431 sctx
->enc
= KMC_AES_192_ENCRYPT
;
432 sctx
->dec
= KMC_AES_192_DECRYPT
;
435 sctx
->enc
= KMC_AES_256_ENCRYPT
;
436 sctx
->dec
= KMC_AES_256_DECRYPT
;
440 return aes_set_key(tfm
, in_key
, key_len
);
443 static int cbc_aes_crypt(struct blkcipher_desc
*desc
, long func
, void *param
,
444 struct blkcipher_walk
*walk
)
446 int ret
= blkcipher_walk_virt(desc
, walk
);
447 unsigned int nbytes
= walk
->nbytes
;
452 memcpy(param
, walk
->iv
, AES_BLOCK_SIZE
);
454 /* only use complete blocks */
455 unsigned int n
= nbytes
& ~(AES_BLOCK_SIZE
- 1);
456 u8
*out
= walk
->dst
.virt
.addr
;
457 u8
*in
= walk
->src
.virt
.addr
;
459 ret
= crypt_s390_kmc(func
, param
, out
, in
, n
);
460 BUG_ON((ret
< 0) || (ret
!= n
));
462 nbytes
&= AES_BLOCK_SIZE
- 1;
463 ret
= blkcipher_walk_done(desc
, walk
, nbytes
);
464 } while ((nbytes
= walk
->nbytes
));
465 memcpy(walk
->iv
, param
, AES_BLOCK_SIZE
);
471 static int cbc_aes_encrypt(struct blkcipher_desc
*desc
,
472 struct scatterlist
*dst
, struct scatterlist
*src
,
475 struct s390_aes_ctx
*sctx
= crypto_blkcipher_ctx(desc
->tfm
);
476 struct blkcipher_walk walk
;
478 if (unlikely(need_fallback(sctx
->key_len
)))
479 return fallback_blk_enc(desc
, dst
, src
, nbytes
);
481 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
482 return cbc_aes_crypt(desc
, sctx
->enc
, sctx
->iv
, &walk
);
485 static int cbc_aes_decrypt(struct blkcipher_desc
*desc
,
486 struct scatterlist
*dst
, struct scatterlist
*src
,
489 struct s390_aes_ctx
*sctx
= crypto_blkcipher_ctx(desc
->tfm
);
490 struct blkcipher_walk walk
;
492 if (unlikely(need_fallback(sctx
->key_len
)))
493 return fallback_blk_dec(desc
, dst
, src
, nbytes
);
495 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
496 return cbc_aes_crypt(desc
, sctx
->dec
, sctx
->iv
, &walk
);
499 static struct crypto_alg cbc_aes_alg
= {
500 .cra_name
= "cbc(aes)",
501 .cra_driver_name
= "cbc-aes-s390",
502 .cra_priority
= CRYPT_S390_COMPOSITE_PRIORITY
,
503 .cra_flags
= CRYPTO_ALG_TYPE_BLKCIPHER
|
504 CRYPTO_ALG_NEED_FALLBACK
,
505 .cra_blocksize
= AES_BLOCK_SIZE
,
506 .cra_ctxsize
= sizeof(struct s390_aes_ctx
),
507 .cra_type
= &crypto_blkcipher_type
,
508 .cra_module
= THIS_MODULE
,
509 .cra_init
= fallback_init_blk
,
510 .cra_exit
= fallback_exit_blk
,
513 .min_keysize
= AES_MIN_KEY_SIZE
,
514 .max_keysize
= AES_MAX_KEY_SIZE
,
515 .ivsize
= AES_BLOCK_SIZE
,
516 .setkey
= cbc_aes_set_key
,
517 .encrypt
= cbc_aes_encrypt
,
518 .decrypt
= cbc_aes_decrypt
,
523 static int xts_fallback_setkey(struct crypto_tfm
*tfm
, const u8
*key
,
526 struct s390_xts_ctx
*xts_ctx
= crypto_tfm_ctx(tfm
);
529 xts_ctx
->fallback
->base
.crt_flags
&= ~CRYPTO_TFM_REQ_MASK
;
530 xts_ctx
->fallback
->base
.crt_flags
|= (tfm
->crt_flags
&
531 CRYPTO_TFM_REQ_MASK
);
533 ret
= crypto_blkcipher_setkey(xts_ctx
->fallback
, key
, len
);
535 tfm
->crt_flags
&= ~CRYPTO_TFM_RES_MASK
;
536 tfm
->crt_flags
|= (xts_ctx
->fallback
->base
.crt_flags
&
537 CRYPTO_TFM_RES_MASK
);
542 static int xts_fallback_decrypt(struct blkcipher_desc
*desc
,
543 struct scatterlist
*dst
, struct scatterlist
*src
,
546 struct s390_xts_ctx
*xts_ctx
= crypto_blkcipher_ctx(desc
->tfm
);
547 struct crypto_blkcipher
*tfm
;
551 desc
->tfm
= xts_ctx
->fallback
;
553 ret
= crypto_blkcipher_decrypt_iv(desc
, dst
, src
, nbytes
);
559 static int xts_fallback_encrypt(struct blkcipher_desc
*desc
,
560 struct scatterlist
*dst
, struct scatterlist
*src
,
563 struct s390_xts_ctx
*xts_ctx
= crypto_blkcipher_ctx(desc
->tfm
);
564 struct crypto_blkcipher
*tfm
;
568 desc
->tfm
= xts_ctx
->fallback
;
570 ret
= crypto_blkcipher_encrypt_iv(desc
, dst
, src
, nbytes
);
576 static int xts_aes_set_key(struct crypto_tfm
*tfm
, const u8
*in_key
,
577 unsigned int key_len
)
579 struct s390_xts_ctx
*xts_ctx
= crypto_tfm_ctx(tfm
);
580 u32
*flags
= &tfm
->crt_flags
;
584 xts_ctx
->enc
= KM_XTS_128_ENCRYPT
;
585 xts_ctx
->dec
= KM_XTS_128_DECRYPT
;
586 memcpy(xts_ctx
->key
+ 16, in_key
, 16);
587 memcpy(xts_ctx
->pcc
.key
+ 16, in_key
+ 16, 16);
592 xts_fallback_setkey(tfm
, in_key
, key_len
);
595 xts_ctx
->enc
= KM_XTS_256_ENCRYPT
;
596 xts_ctx
->dec
= KM_XTS_256_DECRYPT
;
597 memcpy(xts_ctx
->key
, in_key
, 32);
598 memcpy(xts_ctx
->pcc
.key
, in_key
+ 32, 32);
601 *flags
|= CRYPTO_TFM_RES_BAD_KEY_LEN
;
604 xts_ctx
->key_len
= key_len
;
608 static int xts_aes_crypt(struct blkcipher_desc
*desc
, long func
,
609 struct s390_xts_ctx
*xts_ctx
,
610 struct blkcipher_walk
*walk
)
612 unsigned int offset
= (xts_ctx
->key_len
>> 1) & 0x10;
613 int ret
= blkcipher_walk_virt(desc
, walk
);
614 unsigned int nbytes
= walk
->nbytes
;
622 memset(xts_ctx
->pcc
.block
, 0, sizeof(xts_ctx
->pcc
.block
));
623 memset(xts_ctx
->pcc
.bit
, 0, sizeof(xts_ctx
->pcc
.bit
));
624 memset(xts_ctx
->pcc
.xts
, 0, sizeof(xts_ctx
->pcc
.xts
));
625 memcpy(xts_ctx
->pcc
.tweak
, walk
->iv
, sizeof(xts_ctx
->pcc
.tweak
));
626 param
= xts_ctx
->pcc
.key
+ offset
;
627 ret
= crypt_s390_pcc(func
, param
);
630 memcpy(xts_ctx
->xts_param
, xts_ctx
->pcc
.xts
, 16);
631 param
= xts_ctx
->key
+ offset
;
633 /* only use complete blocks */
634 n
= nbytes
& ~(AES_BLOCK_SIZE
- 1);
635 out
= walk
->dst
.virt
.addr
;
636 in
= walk
->src
.virt
.addr
;
638 ret
= crypt_s390_km(func
, param
, out
, in
, n
);
639 BUG_ON(ret
< 0 || ret
!= n
);
641 nbytes
&= AES_BLOCK_SIZE
- 1;
642 ret
= blkcipher_walk_done(desc
, walk
, nbytes
);
643 } while ((nbytes
= walk
->nbytes
));
648 static int xts_aes_encrypt(struct blkcipher_desc
*desc
,
649 struct scatterlist
*dst
, struct scatterlist
*src
,
652 struct s390_xts_ctx
*xts_ctx
= crypto_blkcipher_ctx(desc
->tfm
);
653 struct blkcipher_walk walk
;
655 if (unlikely(xts_ctx
->key_len
== 48))
656 return xts_fallback_encrypt(desc
, dst
, src
, nbytes
);
658 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
659 return xts_aes_crypt(desc
, xts_ctx
->enc
, xts_ctx
, &walk
);
662 static int xts_aes_decrypt(struct blkcipher_desc
*desc
,
663 struct scatterlist
*dst
, struct scatterlist
*src
,
666 struct s390_xts_ctx
*xts_ctx
= crypto_blkcipher_ctx(desc
->tfm
);
667 struct blkcipher_walk walk
;
669 if (unlikely(xts_ctx
->key_len
== 48))
670 return xts_fallback_decrypt(desc
, dst
, src
, nbytes
);
672 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
673 return xts_aes_crypt(desc
, xts_ctx
->dec
, xts_ctx
, &walk
);
676 static int xts_fallback_init(struct crypto_tfm
*tfm
)
678 const char *name
= tfm
->__crt_alg
->cra_name
;
679 struct s390_xts_ctx
*xts_ctx
= crypto_tfm_ctx(tfm
);
681 xts_ctx
->fallback
= crypto_alloc_blkcipher(name
, 0,
682 CRYPTO_ALG_ASYNC
| CRYPTO_ALG_NEED_FALLBACK
);
684 if (IS_ERR(xts_ctx
->fallback
)) {
685 pr_err("Allocating XTS fallback algorithm %s failed\n",
687 return PTR_ERR(xts_ctx
->fallback
);
692 static void xts_fallback_exit(struct crypto_tfm
*tfm
)
694 struct s390_xts_ctx
*xts_ctx
= crypto_tfm_ctx(tfm
);
696 crypto_free_blkcipher(xts_ctx
->fallback
);
697 xts_ctx
->fallback
= NULL
;
700 static struct crypto_alg xts_aes_alg
= {
701 .cra_name
= "xts(aes)",
702 .cra_driver_name
= "xts-aes-s390",
703 .cra_priority
= CRYPT_S390_COMPOSITE_PRIORITY
,
704 .cra_flags
= CRYPTO_ALG_TYPE_BLKCIPHER
|
705 CRYPTO_ALG_NEED_FALLBACK
,
706 .cra_blocksize
= AES_BLOCK_SIZE
,
707 .cra_ctxsize
= sizeof(struct s390_xts_ctx
),
708 .cra_type
= &crypto_blkcipher_type
,
709 .cra_module
= THIS_MODULE
,
710 .cra_init
= xts_fallback_init
,
711 .cra_exit
= xts_fallback_exit
,
714 .min_keysize
= 2 * AES_MIN_KEY_SIZE
,
715 .max_keysize
= 2 * AES_MAX_KEY_SIZE
,
716 .ivsize
= AES_BLOCK_SIZE
,
717 .setkey
= xts_aes_set_key
,
718 .encrypt
= xts_aes_encrypt
,
719 .decrypt
= xts_aes_decrypt
,
724 static int ctr_aes_set_key(struct crypto_tfm
*tfm
, const u8
*in_key
,
725 unsigned int key_len
)
727 struct s390_aes_ctx
*sctx
= crypto_tfm_ctx(tfm
);
731 sctx
->enc
= KMCTR_AES_128_ENCRYPT
;
732 sctx
->dec
= KMCTR_AES_128_DECRYPT
;
735 sctx
->enc
= KMCTR_AES_192_ENCRYPT
;
736 sctx
->dec
= KMCTR_AES_192_DECRYPT
;
739 sctx
->enc
= KMCTR_AES_256_ENCRYPT
;
740 sctx
->dec
= KMCTR_AES_256_DECRYPT
;
744 return aes_set_key(tfm
, in_key
, key_len
);
747 static int ctr_aes_crypt(struct blkcipher_desc
*desc
, long func
,
748 struct s390_aes_ctx
*sctx
, struct blkcipher_walk
*walk
)
750 int ret
= blkcipher_walk_virt_block(desc
, walk
, AES_BLOCK_SIZE
);
751 unsigned int i
, n
, nbytes
;
752 u8 buf
[AES_BLOCK_SIZE
];
758 memcpy(ctrblk
, walk
->iv
, AES_BLOCK_SIZE
);
759 while ((nbytes
= walk
->nbytes
) >= AES_BLOCK_SIZE
) {
760 out
= walk
->dst
.virt
.addr
;
761 in
= walk
->src
.virt
.addr
;
762 while (nbytes
>= AES_BLOCK_SIZE
) {
763 /* only use complete blocks, max. PAGE_SIZE */
764 n
= (nbytes
> PAGE_SIZE
) ? PAGE_SIZE
:
765 nbytes
& ~(AES_BLOCK_SIZE
- 1);
766 for (i
= AES_BLOCK_SIZE
; i
< n
; i
+= AES_BLOCK_SIZE
) {
767 memcpy(ctrblk
+ i
, ctrblk
+ i
- AES_BLOCK_SIZE
,
769 crypto_inc(ctrblk
+ i
, AES_BLOCK_SIZE
);
771 ret
= crypt_s390_kmctr(func
, sctx
->key
, out
, in
, n
, ctrblk
);
772 BUG_ON(ret
< 0 || ret
!= n
);
773 if (n
> AES_BLOCK_SIZE
)
774 memcpy(ctrblk
, ctrblk
+ n
- AES_BLOCK_SIZE
,
776 crypto_inc(ctrblk
, AES_BLOCK_SIZE
);
781 ret
= blkcipher_walk_done(desc
, walk
, nbytes
);
784 * final block may be < AES_BLOCK_SIZE, copy only nbytes
787 out
= walk
->dst
.virt
.addr
;
788 in
= walk
->src
.virt
.addr
;
789 ret
= crypt_s390_kmctr(func
, sctx
->key
, buf
, in
,
790 AES_BLOCK_SIZE
, ctrblk
);
791 BUG_ON(ret
< 0 || ret
!= AES_BLOCK_SIZE
);
792 memcpy(out
, buf
, nbytes
);
793 crypto_inc(ctrblk
, AES_BLOCK_SIZE
);
794 ret
= blkcipher_walk_done(desc
, walk
, 0);
796 memcpy(walk
->iv
, ctrblk
, AES_BLOCK_SIZE
);
800 static int ctr_aes_encrypt(struct blkcipher_desc
*desc
,
801 struct scatterlist
*dst
, struct scatterlist
*src
,
804 struct s390_aes_ctx
*sctx
= crypto_blkcipher_ctx(desc
->tfm
);
805 struct blkcipher_walk walk
;
807 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
808 return ctr_aes_crypt(desc
, sctx
->enc
, sctx
, &walk
);
811 static int ctr_aes_decrypt(struct blkcipher_desc
*desc
,
812 struct scatterlist
*dst
, struct scatterlist
*src
,
815 struct s390_aes_ctx
*sctx
= crypto_blkcipher_ctx(desc
->tfm
);
816 struct blkcipher_walk walk
;
818 blkcipher_walk_init(&walk
, dst
, src
, nbytes
);
819 return ctr_aes_crypt(desc
, sctx
->dec
, sctx
, &walk
);
822 static struct crypto_alg ctr_aes_alg
= {
823 .cra_name
= "ctr(aes)",
824 .cra_driver_name
= "ctr-aes-s390",
825 .cra_priority
= CRYPT_S390_COMPOSITE_PRIORITY
,
826 .cra_flags
= CRYPTO_ALG_TYPE_BLKCIPHER
,
828 .cra_ctxsize
= sizeof(struct s390_aes_ctx
),
829 .cra_type
= &crypto_blkcipher_type
,
830 .cra_module
= THIS_MODULE
,
833 .min_keysize
= AES_MIN_KEY_SIZE
,
834 .max_keysize
= AES_MAX_KEY_SIZE
,
835 .ivsize
= AES_BLOCK_SIZE
,
836 .setkey
= ctr_aes_set_key
,
837 .encrypt
= ctr_aes_encrypt
,
838 .decrypt
= ctr_aes_decrypt
,
843 static int __init
aes_s390_init(void)
847 if (crypt_s390_func_available(KM_AES_128_ENCRYPT
, CRYPT_S390_MSA
))
848 keylen_flag
|= AES_KEYLEN_128
;
849 if (crypt_s390_func_available(KM_AES_192_ENCRYPT
, CRYPT_S390_MSA
))
850 keylen_flag
|= AES_KEYLEN_192
;
851 if (crypt_s390_func_available(KM_AES_256_ENCRYPT
, CRYPT_S390_MSA
))
852 keylen_flag
|= AES_KEYLEN_256
;
857 /* z9 109 and z9 BC/EC only support 128 bit key length */
858 if (keylen_flag
== AES_KEYLEN_128
)
859 pr_info("AES hardware acceleration is only available for"
862 ret
= crypto_register_alg(&aes_alg
);
866 ret
= crypto_register_alg(&ecb_aes_alg
);
870 ret
= crypto_register_alg(&cbc_aes_alg
);
874 if (crypt_s390_func_available(KM_XTS_128_ENCRYPT
,
875 CRYPT_S390_MSA
| CRYPT_S390_MSA4
) &&
876 crypt_s390_func_available(KM_XTS_256_ENCRYPT
,
877 CRYPT_S390_MSA
| CRYPT_S390_MSA4
)) {
878 ret
= crypto_register_alg(&xts_aes_alg
);
883 if (crypt_s390_func_available(KMCTR_AES_128_ENCRYPT
,
884 CRYPT_S390_MSA
| CRYPT_S390_MSA4
) &&
885 crypt_s390_func_available(KMCTR_AES_192_ENCRYPT
,
886 CRYPT_S390_MSA
| CRYPT_S390_MSA4
) &&
887 crypt_s390_func_available(KMCTR_AES_256_ENCRYPT
,
888 CRYPT_S390_MSA
| CRYPT_S390_MSA4
)) {
889 ctrblk
= (u8
*) __get_free_page(GFP_KERNEL
);
894 ret
= crypto_register_alg(&ctr_aes_alg
);
896 free_page((unsigned long) ctrblk
);
905 crypto_unregister_alg(&xts_aes_alg
);
907 crypto_unregister_alg(&cbc_aes_alg
);
909 crypto_unregister_alg(&ecb_aes_alg
);
911 crypto_unregister_alg(&aes_alg
);
916 static void __exit
aes_s390_fini(void)
918 crypto_unregister_alg(&ctr_aes_alg
);
919 free_page((unsigned long) ctrblk
);
920 crypto_unregister_alg(&xts_aes_alg
);
921 crypto_unregister_alg(&cbc_aes_alg
);
922 crypto_unregister_alg(&ecb_aes_alg
);
923 crypto_unregister_alg(&aes_alg
);
926 module_init(aes_s390_init
);
927 module_exit(aes_s390_fini
);
929 MODULE_ALIAS("aes-all");
931 MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm");
932 MODULE_LICENSE("GPL");