Skip to content

Commit 0200f3e

Browse files
gerald-schaeferherbertx
authored andcommitted
crypto: s390 - add System z hardware support for CTR mode
This patch adds System z hardware acceleration support for AES, DES and 3DES in CTR mode. The hardware support is available starting with System z196. Signed-off-by: Gerald Schaefer <gerald.schaefer@de.ibm.com> Signed-off-by: Jan Glauber <jang@linux.vnet.ibm.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 9996e34 commit 0200f3e

4 files changed

Lines changed: 383 additions & 6 deletions

File tree

‎arch/s390/crypto/aes_s390.c‎

Lines changed: 144 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
#define AES_KEYLEN_192 2
3232
#define AES_KEYLEN_256 4
3333

34-
static char keylen_flag = 0;
34+
static u8 *ctrblk;
35+
static char keylen_flag;
3536

3637
struct s390_aes_ctx {
3738
u8 iv[AES_BLOCK_SIZE];
@@ -724,9 +725,128 @@ static struct crypto_alg xts_aes_alg = {
724725
}
725726
};
726727

728+
static int ctr_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
729+
unsigned int key_len)
730+
{
731+
struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
732+
733+
switch (key_len) {
734+
case 16:
735+
sctx->enc = KMCTR_AES_128_ENCRYPT;
736+
sctx->dec = KMCTR_AES_128_DECRYPT;
737+
break;
738+
case 24:
739+
sctx->enc = KMCTR_AES_192_ENCRYPT;
740+
sctx->dec = KMCTR_AES_192_DECRYPT;
741+
break;
742+
case 32:
743+
sctx->enc = KMCTR_AES_256_ENCRYPT;
744+
sctx->dec = KMCTR_AES_256_DECRYPT;
745+
break;
746+
}
747+
748+
return aes_set_key(tfm, in_key, key_len);
749+
}
750+
751+
static int ctr_aes_crypt(struct blkcipher_desc *desc, long func,
752+
struct s390_aes_ctx *sctx, struct blkcipher_walk *walk)
753+
{
754+
int ret = blkcipher_walk_virt_block(desc, walk, AES_BLOCK_SIZE);
755+
unsigned int i, n, nbytes;
756+
u8 buf[AES_BLOCK_SIZE];
757+
u8 *out, *in;
758+
759+
if (!walk->nbytes)
760+
return ret;
761+
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,
772+
AES_BLOCK_SIZE);
773+
crypto_inc(ctrblk + i, AES_BLOCK_SIZE);
774+
}
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,
779+
AES_BLOCK_SIZE);
780+
crypto_inc(ctrblk, AES_BLOCK_SIZE);
781+
out += n;
782+
in += n;
783+
nbytes -= n;
784+
}
785+
ret = blkcipher_walk_done(desc, walk, nbytes);
786+
}
787+
/*
788+
* final block may be < AES_BLOCK_SIZE, copy only nbytes
789+
*/
790+
if (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);
799+
}
800+
memcpy(walk->iv, ctrblk, AES_BLOCK_SIZE);
801+
return ret;
802+
}
803+
804+
static int ctr_aes_encrypt(struct blkcipher_desc *desc,
805+
struct scatterlist *dst, struct scatterlist *src,
806+
unsigned int nbytes)
807+
{
808+
struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
809+
struct blkcipher_walk walk;
810+
811+
blkcipher_walk_init(&walk, dst, src, nbytes);
812+
return ctr_aes_crypt(desc, sctx->enc, sctx, &walk);
813+
}
814+
815+
static int ctr_aes_decrypt(struct blkcipher_desc *desc,
816+
struct scatterlist *dst, struct scatterlist *src,
817+
unsigned int nbytes)
818+
{
819+
struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
820+
struct blkcipher_walk walk;
821+
822+
blkcipher_walk_init(&walk, dst, src, nbytes);
823+
return ctr_aes_crypt(desc, sctx->dec, sctx, &walk);
824+
}
825+
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,
831+
.cra_blocksize = 1,
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),
836+
.cra_u = {
837+
.blkcipher = {
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,
844+
}
845+
}
846+
};
847+
727848
static int __init aes_s390_init(void)
728849
{
729-
unsigned long long facility_bits[2];
730850
int ret;
731851

732852
if (crypt_s390_func_available(KM_AES_128_ENCRYPT, CRYPT_S390_MSA))
@@ -765,9 +885,29 @@ static int __init aes_s390_init(void)
765885
goto xts_aes_err;
766886
}
767887

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);
895+
if (!ctrblk) {
896+
ret = -ENOMEM;
897+
goto ctr_aes_err;
898+
}
899+
ret = crypto_register_alg(&ctr_aes_alg);
900+
if (ret) {
901+
free_page((unsigned long) ctrblk);
902+
goto ctr_aes_err;
903+
}
904+
}
905+
768906
out:
769907
return ret;
770908

909+
ctr_aes_err:
910+
crypto_unregister_alg(&xts_aes_alg);
771911
xts_aes_err:
772912
crypto_unregister_alg(&cbc_aes_alg);
773913
cbc_aes_err:
@@ -780,6 +920,8 @@ static int __init aes_s390_init(void)
780920

781921
static void __exit aes_s390_fini(void)
782922
{
923+
crypto_unregister_alg(&ctr_aes_alg);
924+
free_page((unsigned long) ctrblk);
783925
crypto_unregister_alg(&xts_aes_alg);
784926
crypto_unregister_alg(&cbc_aes_alg);
785927
crypto_unregister_alg(&ecb_aes_alg);

‎arch/s390/crypto/crypt_s390.h‎

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ enum crypt_s390_operations {
3434
CRYPT_S390_KMC = 0x0200,
3535
CRYPT_S390_KIMD = 0x0300,
3636
CRYPT_S390_KLMD = 0x0400,
37-
CRYPT_S390_KMAC = 0x0500
37+
CRYPT_S390_KMAC = 0x0500,
38+
CRYPT_S390_KMCTR = 0x0600
3839
};
3940

4041
/*
@@ -82,6 +83,26 @@ enum crypt_s390_kmc_func {
8283
KMC_PRNG = CRYPT_S390_KMC | 0x43,
8384
};
8485

86+
/*
87+
* function codes for KMCTR (CIPHER MESSAGE WITH COUNTER)
88+
* instruction
89+
*/
90+
enum crypt_s390_kmctr_func {
91+
KMCTR_QUERY = CRYPT_S390_KMCTR | 0x0,
92+
KMCTR_DEA_ENCRYPT = CRYPT_S390_KMCTR | 0x1,
93+
KMCTR_DEA_DECRYPT = CRYPT_S390_KMCTR | 0x1 | 0x80,
94+
KMCTR_TDEA_128_ENCRYPT = CRYPT_S390_KMCTR | 0x2,
95+
KMCTR_TDEA_128_DECRYPT = CRYPT_S390_KMCTR | 0x2 | 0x80,
96+
KMCTR_TDEA_192_ENCRYPT = CRYPT_S390_KMCTR | 0x3,
97+
KMCTR_TDEA_192_DECRYPT = CRYPT_S390_KMCTR | 0x3 | 0x80,
98+
KMCTR_AES_128_ENCRYPT = CRYPT_S390_KMCTR | 0x12,
99+
KMCTR_AES_128_DECRYPT = CRYPT_S390_KMCTR | 0x12 | 0x80,
100+
KMCTR_AES_192_ENCRYPT = CRYPT_S390_KMCTR | 0x13,
101+
KMCTR_AES_192_DECRYPT = CRYPT_S390_KMCTR | 0x13 | 0x80,
102+
KMCTR_AES_256_ENCRYPT = CRYPT_S390_KMCTR | 0x14,
103+
KMCTR_AES_256_DECRYPT = CRYPT_S390_KMCTR | 0x14 | 0x80,
104+
};
105+
85106
/*
86107
* function codes for KIMD (COMPUTE INTERMEDIATE MESSAGE DIGEST)
87108
* instruction
@@ -292,6 +313,45 @@ static inline int crypt_s390_kmac(long func, void *param,
292313
return (func & CRYPT_S390_FUNC_MASK) ? src_len - __src_len : __src_len;
293314
}
294315

316+
/**
317+
* crypt_s390_kmctr:
318+
* @func: the function code passed to KMCTR; see crypt_s390_kmctr_func
319+
* @param: address of parameter block; see POP for details on each func
320+
* @dest: address of destination memory area
321+
* @src: address of source memory area
322+
* @src_len: length of src operand in bytes
323+
* @counter: address of counter value
324+
*
325+
* Executes the KMCTR (CIPHER MESSAGE WITH COUNTER) operation of the CPU.
326+
*
327+
* Returns -1 for failure, 0 for the query func, number of processed
328+
* bytes for encryption/decryption funcs
329+
*/
330+
static inline int crypt_s390_kmctr(long func, void *param, u8 *dest,
331+
const u8 *src, long src_len, u8 *counter)
332+
{
333+
register long __func asm("0") = func & CRYPT_S390_FUNC_MASK;
334+
register void *__param asm("1") = param;
335+
register const u8 *__src asm("2") = src;
336+
register long __src_len asm("3") = src_len;
337+
register u8 *__dest asm("4") = dest;
338+
register u8 *__ctr asm("6") = counter;
339+
int ret = -1;
340+
341+
asm volatile(
342+
"0: .insn rrf,0xb92d0000,%3,%1,%4,0 \n" /* KMCTR opcode */
343+
"1: brc 1,0b \n" /* handle partial completion */
344+
" la %0,0\n"
345+
"2:\n"
346+
EX_TABLE(0b,2b) EX_TABLE(1b,2b)
347+
: "+d" (ret), "+a" (__src), "+d" (__src_len), "+a" (__dest),
348+
"+a" (__ctr)
349+
: "d" (__func), "a" (__param) : "cc", "memory");
350+
if (ret < 0)
351+
return ret;
352+
return (func & CRYPT_S390_FUNC_MASK) ? src_len - __src_len : __src_len;
353+
}
354+
295355
/**
296356
* crypt_s390_func_available:
297357
* @func: the function code of the specific function; 0 if op in general
@@ -329,6 +389,10 @@ static inline int crypt_s390_func_available(int func,
329389
case CRYPT_S390_KMAC:
330390
ret = crypt_s390_kmac(KMAC_QUERY, &status, NULL, 0);
331391
break;
392+
case CRYPT_S390_KMCTR:
393+
ret = crypt_s390_kmctr(KMCTR_QUERY, &status, NULL, NULL, 0,
394+
NULL);
395+
break;
332396
default:
333397
return 0;
334398
}

0 commit comments

Comments
 (0)