Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Cryptographic API. |
| 3 | * |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 4 | * s390 implementation of the DES Cipher Algorithm. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * |
Heiko Carstens | a53c8fa | 2012-07-20 11:15:04 +0200 | [diff] [blame] | 6 | * Copyright IBM Corp. 2003, 2011 |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 7 | * Author(s): Thomas Spatzier |
| 8 | * Jan Glauber (jan.glauber@de.ibm.com) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | */ |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 16 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/init.h> |
| 18 | #include <linux/module.h> |
Hendrik Brueckner | d05377c | 2015-02-19 17:34:07 +0100 | [diff] [blame] | 19 | #include <linux/cpufeature.h> |
Jan Glauber | 1efbd15 | 2010-05-21 22:04:46 +1000 | [diff] [blame] | 20 | #include <linux/crypto.h> |
| 21 | #include <crypto/algapi.h> |
| 22 | #include <crypto/des.h> |
Martin Schwidefsky | c7d4d25 | 2016-03-17 15:22:12 +0100 | [diff] [blame] | 23 | #include <asm/cpacf.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
Jan Glauber | 98971f8 | 2011-04-19 21:29:15 +0200 | [diff] [blame] | 25 | #define DES3_KEY_SIZE (3 * DES_KEY_SIZE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
Gerald Schaefer | 0200f3e | 2011-05-04 15:09:44 +1000 | [diff] [blame] | 27 | static u8 *ctrblk; |
Harald Freudenberger | ee97dc7 | 2014-01-22 13:01:33 +0100 | [diff] [blame] | 28 | static DEFINE_SPINLOCK(ctrblk_lock); |
Gerald Schaefer | 0200f3e | 2011-05-04 15:09:44 +1000 | [diff] [blame] | 29 | |
Martin Schwidefsky | 69c0e36 | 2016-08-18 12:59:46 +0200 | [diff] [blame^] | 30 | static cpacf_mask_t km_functions, kmc_functions, kmctr_functions; |
| 31 | |
Jan Glauber | 98971f8 | 2011-04-19 21:29:15 +0200 | [diff] [blame] | 32 | struct s390_des_ctx { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | u8 iv[DES_BLOCK_SIZE]; |
Jan Glauber | 98971f8 | 2011-04-19 21:29:15 +0200 | [diff] [blame] | 34 | u8 key[DES3_KEY_SIZE]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | }; |
| 36 | |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 37 | static int des_setkey(struct crypto_tfm *tfm, const u8 *key, |
Jan Glauber | 98971f8 | 2011-04-19 21:29:15 +0200 | [diff] [blame] | 38 | unsigned int key_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | { |
Jan Glauber | 98971f8 | 2011-04-19 21:29:15 +0200 | [diff] [blame] | 40 | struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm); |
Jan Glauber | 1efbd15 | 2010-05-21 22:04:46 +1000 | [diff] [blame] | 41 | u32 tmp[DES_EXPKEY_WORDS]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
Jan Glauber | 1efbd15 | 2010-05-21 22:04:46 +1000 | [diff] [blame] | 43 | /* check for weak keys */ |
Martin Schwidefsky | 69c0e36 | 2016-08-18 12:59:46 +0200 | [diff] [blame^] | 44 | if (!des_ekey(tmp, key) && |
| 45 | (tfm->crt_flags & CRYPTO_TFM_REQ_WEAK_KEY)) { |
| 46 | tfm->crt_flags |= CRYPTO_TFM_RES_WEAK_KEY; |
Jan Glauber | 1efbd15 | 2010-05-21 22:04:46 +1000 | [diff] [blame] | 47 | return -EINVAL; |
| 48 | } |
| 49 | |
Jan Glauber | 98971f8 | 2011-04-19 21:29:15 +0200 | [diff] [blame] | 50 | memcpy(ctx->key, key, key_len); |
Jan Glauber | 1efbd15 | 2010-05-21 22:04:46 +1000 | [diff] [blame] | 51 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 54 | static void des_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | { |
Jan Glauber | 98971f8 | 2011-04-19 21:29:15 +0200 | [diff] [blame] | 56 | struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
Martin Schwidefsky | edc63a3 | 2016-08-15 09:19:16 +0200 | [diff] [blame] | 58 | cpacf_km(CPACF_KM_DEA, ctx->key, out, in, DES_BLOCK_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 61 | static void des_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | { |
Jan Glauber | 98971f8 | 2011-04-19 21:29:15 +0200 | [diff] [blame] | 63 | struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
Martin Schwidefsky | edc63a3 | 2016-08-15 09:19:16 +0200 | [diff] [blame] | 65 | cpacf_km(CPACF_KM_DEA | CPACF_DECRYPT, |
| 66 | ctx->key, out, in, DES_BLOCK_SIZE); |
Jan Glauber | b8dc603 | 2006-01-14 13:20:53 -0800 | [diff] [blame] | 67 | } |
| 68 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | static struct crypto_alg des_alg = { |
| 70 | .cra_name = "des", |
Herbert Xu | 65b75c3 | 2006-08-21 21:18:50 +1000 | [diff] [blame] | 71 | .cra_driver_name = "des-s390", |
Martin Schwidefsky | c7d4d25 | 2016-03-17 15:22:12 +0100 | [diff] [blame] | 72 | .cra_priority = 300, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | .cra_flags = CRYPTO_ALG_TYPE_CIPHER, |
| 74 | .cra_blocksize = DES_BLOCK_SIZE, |
Jan Glauber | 98971f8 | 2011-04-19 21:29:15 +0200 | [diff] [blame] | 75 | .cra_ctxsize = sizeof(struct s390_des_ctx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | .cra_module = THIS_MODULE, |
Jan Glauber | c135783 | 2006-01-14 13:20:53 -0800 | [diff] [blame] | 77 | .cra_u = { |
| 78 | .cipher = { |
| 79 | .cia_min_keysize = DES_KEY_SIZE, |
| 80 | .cia_max_keysize = DES_KEY_SIZE, |
| 81 | .cia_setkey = des_setkey, |
| 82 | .cia_encrypt = des_encrypt, |
Jan Glauber | b8dc603 | 2006-01-14 13:20:53 -0800 | [diff] [blame] | 83 | .cia_decrypt = des_decrypt, |
Jan Glauber | c135783 | 2006-01-14 13:20:53 -0800 | [diff] [blame] | 84 | } |
| 85 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | }; |
| 87 | |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 88 | static int ecb_desall_crypt(struct blkcipher_desc *desc, long func, |
Jan Glauber | 98971f8 | 2011-04-19 21:29:15 +0200 | [diff] [blame] | 89 | u8 *key, struct blkcipher_walk *walk) |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 90 | { |
| 91 | int ret = blkcipher_walk_virt(desc, walk); |
| 92 | unsigned int nbytes; |
| 93 | |
| 94 | while ((nbytes = walk->nbytes)) { |
| 95 | /* only use complete blocks */ |
| 96 | unsigned int n = nbytes & ~(DES_BLOCK_SIZE - 1); |
| 97 | u8 *out = walk->dst.virt.addr; |
| 98 | u8 *in = walk->src.virt.addr; |
| 99 | |
Martin Schwidefsky | 0177db0 | 2016-08-15 10:41:52 +0200 | [diff] [blame] | 100 | cpacf_km(func, key, out, in, n); |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 101 | |
| 102 | nbytes &= DES_BLOCK_SIZE - 1; |
| 103 | ret = blkcipher_walk_done(desc, walk, nbytes); |
| 104 | } |
| 105 | |
| 106 | return ret; |
| 107 | } |
| 108 | |
| 109 | static int cbc_desall_crypt(struct blkcipher_desc *desc, long func, |
Harald Freudenberger | adc3fcf | 2014-01-22 13:00:04 +0100 | [diff] [blame] | 110 | struct blkcipher_walk *walk) |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 111 | { |
Harald Freudenberger | adc3fcf | 2014-01-22 13:00:04 +0100 | [diff] [blame] | 112 | struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm); |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 113 | int ret = blkcipher_walk_virt(desc, walk); |
| 114 | unsigned int nbytes = walk->nbytes; |
Harald Freudenberger | adc3fcf | 2014-01-22 13:00:04 +0100 | [diff] [blame] | 115 | struct { |
| 116 | u8 iv[DES_BLOCK_SIZE]; |
| 117 | u8 key[DES3_KEY_SIZE]; |
| 118 | } param; |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 119 | |
| 120 | if (!nbytes) |
| 121 | goto out; |
| 122 | |
Harald Freudenberger | adc3fcf | 2014-01-22 13:00:04 +0100 | [diff] [blame] | 123 | memcpy(param.iv, walk->iv, DES_BLOCK_SIZE); |
| 124 | memcpy(param.key, ctx->key, DES3_KEY_SIZE); |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 125 | do { |
| 126 | /* only use complete blocks */ |
| 127 | unsigned int n = nbytes & ~(DES_BLOCK_SIZE - 1); |
| 128 | u8 *out = walk->dst.virt.addr; |
| 129 | u8 *in = walk->src.virt.addr; |
| 130 | |
Martin Schwidefsky | 0177db0 | 2016-08-15 10:41:52 +0200 | [diff] [blame] | 131 | cpacf_kmc(func, ¶m, out, in, n); |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 132 | |
| 133 | nbytes &= DES_BLOCK_SIZE - 1; |
| 134 | ret = blkcipher_walk_done(desc, walk, nbytes); |
| 135 | } while ((nbytes = walk->nbytes)); |
Harald Freudenberger | adc3fcf | 2014-01-22 13:00:04 +0100 | [diff] [blame] | 136 | memcpy(walk->iv, param.iv, DES_BLOCK_SIZE); |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 137 | |
| 138 | out: |
| 139 | return ret; |
| 140 | } |
| 141 | |
| 142 | static int ecb_des_encrypt(struct blkcipher_desc *desc, |
| 143 | struct scatterlist *dst, struct scatterlist *src, |
| 144 | unsigned int nbytes) |
| 145 | { |
Jan Glauber | 98971f8 | 2011-04-19 21:29:15 +0200 | [diff] [blame] | 146 | struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm); |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 147 | struct blkcipher_walk walk; |
| 148 | |
| 149 | blkcipher_walk_init(&walk, dst, src, nbytes); |
Martin Schwidefsky | edc63a3 | 2016-08-15 09:19:16 +0200 | [diff] [blame] | 150 | return ecb_desall_crypt(desc, CPACF_KM_DEA, ctx->key, &walk); |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | static int ecb_des_decrypt(struct blkcipher_desc *desc, |
| 154 | struct scatterlist *dst, struct scatterlist *src, |
| 155 | unsigned int nbytes) |
| 156 | { |
Jan Glauber | 98971f8 | 2011-04-19 21:29:15 +0200 | [diff] [blame] | 157 | struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm); |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 158 | struct blkcipher_walk walk; |
| 159 | |
| 160 | blkcipher_walk_init(&walk, dst, src, nbytes); |
Martin Schwidefsky | edc63a3 | 2016-08-15 09:19:16 +0200 | [diff] [blame] | 161 | return ecb_desall_crypt(desc, CPACF_KM_DEA | CPACF_DECRYPT, |
| 162 | ctx->key, &walk); |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | static struct crypto_alg ecb_des_alg = { |
| 166 | .cra_name = "ecb(des)", |
| 167 | .cra_driver_name = "ecb-des-s390", |
Martin Schwidefsky | c7d4d25 | 2016-03-17 15:22:12 +0100 | [diff] [blame] | 168 | .cra_priority = 400, /* combo: des + ecb */ |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 169 | .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER, |
| 170 | .cra_blocksize = DES_BLOCK_SIZE, |
Jan Glauber | 98971f8 | 2011-04-19 21:29:15 +0200 | [diff] [blame] | 171 | .cra_ctxsize = sizeof(struct s390_des_ctx), |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 172 | .cra_type = &crypto_blkcipher_type, |
| 173 | .cra_module = THIS_MODULE, |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 174 | .cra_u = { |
| 175 | .blkcipher = { |
| 176 | .min_keysize = DES_KEY_SIZE, |
| 177 | .max_keysize = DES_KEY_SIZE, |
| 178 | .setkey = des_setkey, |
| 179 | .encrypt = ecb_des_encrypt, |
| 180 | .decrypt = ecb_des_decrypt, |
| 181 | } |
| 182 | } |
| 183 | }; |
| 184 | |
| 185 | static int cbc_des_encrypt(struct blkcipher_desc *desc, |
| 186 | struct scatterlist *dst, struct scatterlist *src, |
| 187 | unsigned int nbytes) |
| 188 | { |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 189 | struct blkcipher_walk walk; |
| 190 | |
| 191 | blkcipher_walk_init(&walk, dst, src, nbytes); |
Martin Schwidefsky | edc63a3 | 2016-08-15 09:19:16 +0200 | [diff] [blame] | 192 | return cbc_desall_crypt(desc, CPACF_KMC_DEA, &walk); |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | static int cbc_des_decrypt(struct blkcipher_desc *desc, |
| 196 | struct scatterlist *dst, struct scatterlist *src, |
| 197 | unsigned int nbytes) |
| 198 | { |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 199 | struct blkcipher_walk walk; |
| 200 | |
| 201 | blkcipher_walk_init(&walk, dst, src, nbytes); |
Martin Schwidefsky | edc63a3 | 2016-08-15 09:19:16 +0200 | [diff] [blame] | 202 | return cbc_desall_crypt(desc, CPACF_KMC_DEA | CPACF_DECRYPT, &walk); |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | static struct crypto_alg cbc_des_alg = { |
| 206 | .cra_name = "cbc(des)", |
| 207 | .cra_driver_name = "cbc-des-s390", |
Martin Schwidefsky | c7d4d25 | 2016-03-17 15:22:12 +0100 | [diff] [blame] | 208 | .cra_priority = 400, /* combo: des + cbc */ |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 209 | .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER, |
| 210 | .cra_blocksize = DES_BLOCK_SIZE, |
Jan Glauber | 98971f8 | 2011-04-19 21:29:15 +0200 | [diff] [blame] | 211 | .cra_ctxsize = sizeof(struct s390_des_ctx), |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 212 | .cra_type = &crypto_blkcipher_type, |
| 213 | .cra_module = THIS_MODULE, |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 214 | .cra_u = { |
| 215 | .blkcipher = { |
| 216 | .min_keysize = DES_KEY_SIZE, |
| 217 | .max_keysize = DES_KEY_SIZE, |
| 218 | .ivsize = DES_BLOCK_SIZE, |
| 219 | .setkey = des_setkey, |
| 220 | .encrypt = cbc_des_encrypt, |
| 221 | .decrypt = cbc_des_decrypt, |
| 222 | } |
| 223 | } |
| 224 | }; |
| 225 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | /* |
| 227 | * RFC2451: |
| 228 | * |
| 229 | * For DES-EDE3, there is no known need to reject weak or |
| 230 | * complementation keys. Any weakness is obviated by the use of |
| 231 | * multiple keys. |
| 232 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | * However, if the first two or last two independent 64-bit keys are |
| 234 | * equal (k1 == k2 or k2 == k3), then the DES3 operation is simply the |
| 235 | * same as DES. Implementers MUST reject keys that exhibit this |
| 236 | * property. |
| 237 | * |
| 238 | */ |
Jan Glauber | 98971f8 | 2011-04-19 21:29:15 +0200 | [diff] [blame] | 239 | static int des3_setkey(struct crypto_tfm *tfm, const u8 *key, |
| 240 | unsigned int key_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | { |
Jan Glauber | 98971f8 | 2011-04-19 21:29:15 +0200 | [diff] [blame] | 242 | struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | |
Daniel Borkmann | fed2861 | 2013-12-11 11:28:59 +0100 | [diff] [blame] | 244 | if (!(crypto_memneq(key, &key[DES_KEY_SIZE], DES_KEY_SIZE) && |
| 245 | crypto_memneq(&key[DES_KEY_SIZE], &key[DES_KEY_SIZE * 2], |
| 246 | DES_KEY_SIZE)) && |
Martin Schwidefsky | 69c0e36 | 2016-08-18 12:59:46 +0200 | [diff] [blame^] | 247 | (tfm->crt_flags & CRYPTO_TFM_REQ_WEAK_KEY)) { |
| 248 | tfm->crt_flags |= CRYPTO_TFM_RES_WEAK_KEY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | return -EINVAL; |
| 250 | } |
Jan Glauber | 98971f8 | 2011-04-19 21:29:15 +0200 | [diff] [blame] | 251 | memcpy(ctx->key, key, key_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | return 0; |
| 253 | } |
| 254 | |
Jan Glauber | 98971f8 | 2011-04-19 21:29:15 +0200 | [diff] [blame] | 255 | static void des3_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | { |
Jan Glauber | 98971f8 | 2011-04-19 21:29:15 +0200 | [diff] [blame] | 257 | struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | |
Martin Schwidefsky | edc63a3 | 2016-08-15 09:19:16 +0200 | [diff] [blame] | 259 | cpacf_km(CPACF_KM_TDEA_192, ctx->key, dst, src, DES_BLOCK_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Jan Glauber | 98971f8 | 2011-04-19 21:29:15 +0200 | [diff] [blame] | 262 | static void des3_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | { |
Jan Glauber | 98971f8 | 2011-04-19 21:29:15 +0200 | [diff] [blame] | 264 | struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | |
Martin Schwidefsky | edc63a3 | 2016-08-15 09:19:16 +0200 | [diff] [blame] | 266 | cpacf_km(CPACF_KM_TDEA_192 | CPACF_DECRYPT, |
| 267 | ctx->key, dst, src, DES_BLOCK_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | } |
| 269 | |
Jan Glauber | 98971f8 | 2011-04-19 21:29:15 +0200 | [diff] [blame] | 270 | static struct crypto_alg des3_alg = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | .cra_name = "des3_ede", |
Herbert Xu | 65b75c3 | 2006-08-21 21:18:50 +1000 | [diff] [blame] | 272 | .cra_driver_name = "des3_ede-s390", |
Martin Schwidefsky | c7d4d25 | 2016-03-17 15:22:12 +0100 | [diff] [blame] | 273 | .cra_priority = 300, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | .cra_flags = CRYPTO_ALG_TYPE_CIPHER, |
Jan Glauber | 1efbd15 | 2010-05-21 22:04:46 +1000 | [diff] [blame] | 275 | .cra_blocksize = DES_BLOCK_SIZE, |
Jan Glauber | 98971f8 | 2011-04-19 21:29:15 +0200 | [diff] [blame] | 276 | .cra_ctxsize = sizeof(struct s390_des_ctx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | .cra_module = THIS_MODULE, |
Jan Glauber | c135783 | 2006-01-14 13:20:53 -0800 | [diff] [blame] | 278 | .cra_u = { |
| 279 | .cipher = { |
Jan Glauber | 98971f8 | 2011-04-19 21:29:15 +0200 | [diff] [blame] | 280 | .cia_min_keysize = DES3_KEY_SIZE, |
| 281 | .cia_max_keysize = DES3_KEY_SIZE, |
| 282 | .cia_setkey = des3_setkey, |
| 283 | .cia_encrypt = des3_encrypt, |
| 284 | .cia_decrypt = des3_decrypt, |
Jan Glauber | c135783 | 2006-01-14 13:20:53 -0800 | [diff] [blame] | 285 | } |
| 286 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | }; |
| 288 | |
Jan Glauber | 98971f8 | 2011-04-19 21:29:15 +0200 | [diff] [blame] | 289 | static int ecb_des3_encrypt(struct blkcipher_desc *desc, |
| 290 | struct scatterlist *dst, struct scatterlist *src, |
| 291 | unsigned int nbytes) |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 292 | { |
Jan Glauber | 98971f8 | 2011-04-19 21:29:15 +0200 | [diff] [blame] | 293 | struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm); |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 294 | struct blkcipher_walk walk; |
| 295 | |
| 296 | blkcipher_walk_init(&walk, dst, src, nbytes); |
Martin Schwidefsky | edc63a3 | 2016-08-15 09:19:16 +0200 | [diff] [blame] | 297 | return ecb_desall_crypt(desc, CPACF_KM_TDEA_192, ctx->key, &walk); |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 298 | } |
| 299 | |
Jan Glauber | 98971f8 | 2011-04-19 21:29:15 +0200 | [diff] [blame] | 300 | static int ecb_des3_decrypt(struct blkcipher_desc *desc, |
| 301 | struct scatterlist *dst, struct scatterlist *src, |
| 302 | unsigned int nbytes) |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 303 | { |
Jan Glauber | 98971f8 | 2011-04-19 21:29:15 +0200 | [diff] [blame] | 304 | struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm); |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 305 | struct blkcipher_walk walk; |
| 306 | |
| 307 | blkcipher_walk_init(&walk, dst, src, nbytes); |
Martin Schwidefsky | edc63a3 | 2016-08-15 09:19:16 +0200 | [diff] [blame] | 308 | return ecb_desall_crypt(desc, CPACF_KM_TDEA_192 | CPACF_DECRYPT, |
| 309 | ctx->key, &walk); |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 310 | } |
| 311 | |
Jan Glauber | 98971f8 | 2011-04-19 21:29:15 +0200 | [diff] [blame] | 312 | static struct crypto_alg ecb_des3_alg = { |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 313 | .cra_name = "ecb(des3_ede)", |
| 314 | .cra_driver_name = "ecb-des3_ede-s390", |
Martin Schwidefsky | c7d4d25 | 2016-03-17 15:22:12 +0100 | [diff] [blame] | 315 | .cra_priority = 400, /* combo: des3 + ecb */ |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 316 | .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER, |
Jan Glauber | 1efbd15 | 2010-05-21 22:04:46 +1000 | [diff] [blame] | 317 | .cra_blocksize = DES_BLOCK_SIZE, |
Jan Glauber | 98971f8 | 2011-04-19 21:29:15 +0200 | [diff] [blame] | 318 | .cra_ctxsize = sizeof(struct s390_des_ctx), |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 319 | .cra_type = &crypto_blkcipher_type, |
| 320 | .cra_module = THIS_MODULE, |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 321 | .cra_u = { |
| 322 | .blkcipher = { |
Jan Glauber | 98971f8 | 2011-04-19 21:29:15 +0200 | [diff] [blame] | 323 | .min_keysize = DES3_KEY_SIZE, |
| 324 | .max_keysize = DES3_KEY_SIZE, |
| 325 | .setkey = des3_setkey, |
| 326 | .encrypt = ecb_des3_encrypt, |
| 327 | .decrypt = ecb_des3_decrypt, |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | }; |
| 331 | |
Jan Glauber | 98971f8 | 2011-04-19 21:29:15 +0200 | [diff] [blame] | 332 | static int cbc_des3_encrypt(struct blkcipher_desc *desc, |
| 333 | struct scatterlist *dst, struct scatterlist *src, |
| 334 | unsigned int nbytes) |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 335 | { |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 336 | struct blkcipher_walk walk; |
| 337 | |
| 338 | blkcipher_walk_init(&walk, dst, src, nbytes); |
Martin Schwidefsky | edc63a3 | 2016-08-15 09:19:16 +0200 | [diff] [blame] | 339 | return cbc_desall_crypt(desc, CPACF_KMC_TDEA_192, &walk); |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 340 | } |
| 341 | |
Jan Glauber | 98971f8 | 2011-04-19 21:29:15 +0200 | [diff] [blame] | 342 | static int cbc_des3_decrypt(struct blkcipher_desc *desc, |
| 343 | struct scatterlist *dst, struct scatterlist *src, |
| 344 | unsigned int nbytes) |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 345 | { |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 346 | struct blkcipher_walk walk; |
| 347 | |
| 348 | blkcipher_walk_init(&walk, dst, src, nbytes); |
Martin Schwidefsky | edc63a3 | 2016-08-15 09:19:16 +0200 | [diff] [blame] | 349 | return cbc_desall_crypt(desc, CPACF_KMC_TDEA_192 | CPACF_DECRYPT, |
| 350 | &walk); |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 351 | } |
| 352 | |
Jan Glauber | 98971f8 | 2011-04-19 21:29:15 +0200 | [diff] [blame] | 353 | static struct crypto_alg cbc_des3_alg = { |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 354 | .cra_name = "cbc(des3_ede)", |
| 355 | .cra_driver_name = "cbc-des3_ede-s390", |
Martin Schwidefsky | c7d4d25 | 2016-03-17 15:22:12 +0100 | [diff] [blame] | 356 | .cra_priority = 400, /* combo: des3 + cbc */ |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 357 | .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER, |
Jan Glauber | 1efbd15 | 2010-05-21 22:04:46 +1000 | [diff] [blame] | 358 | .cra_blocksize = DES_BLOCK_SIZE, |
Jan Glauber | 98971f8 | 2011-04-19 21:29:15 +0200 | [diff] [blame] | 359 | .cra_ctxsize = sizeof(struct s390_des_ctx), |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 360 | .cra_type = &crypto_blkcipher_type, |
| 361 | .cra_module = THIS_MODULE, |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 362 | .cra_u = { |
| 363 | .blkcipher = { |
Jan Glauber | 98971f8 | 2011-04-19 21:29:15 +0200 | [diff] [blame] | 364 | .min_keysize = DES3_KEY_SIZE, |
| 365 | .max_keysize = DES3_KEY_SIZE, |
Jan Glauber | 1efbd15 | 2010-05-21 22:04:46 +1000 | [diff] [blame] | 366 | .ivsize = DES_BLOCK_SIZE, |
Jan Glauber | 98971f8 | 2011-04-19 21:29:15 +0200 | [diff] [blame] | 367 | .setkey = des3_setkey, |
| 368 | .encrypt = cbc_des3_encrypt, |
| 369 | .decrypt = cbc_des3_decrypt, |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 370 | } |
| 371 | } |
| 372 | }; |
| 373 | |
Harald Freudenberger | ee97dc7 | 2014-01-22 13:01:33 +0100 | [diff] [blame] | 374 | static unsigned int __ctrblk_init(u8 *ctrptr, unsigned int nbytes) |
| 375 | { |
| 376 | unsigned int i, n; |
| 377 | |
| 378 | /* align to block size, max. PAGE_SIZE */ |
| 379 | n = (nbytes > PAGE_SIZE) ? PAGE_SIZE : nbytes & ~(DES_BLOCK_SIZE - 1); |
| 380 | for (i = DES_BLOCK_SIZE; i < n; i += DES_BLOCK_SIZE) { |
| 381 | memcpy(ctrptr + i, ctrptr + i - DES_BLOCK_SIZE, DES_BLOCK_SIZE); |
| 382 | crypto_inc(ctrptr + i, DES_BLOCK_SIZE); |
| 383 | } |
| 384 | return n; |
| 385 | } |
| 386 | |
Gerald Schaefer | 0200f3e | 2011-05-04 15:09:44 +1000 | [diff] [blame] | 387 | static int ctr_desall_crypt(struct blkcipher_desc *desc, long func, |
Harald Freudenberger | ee97dc7 | 2014-01-22 13:01:33 +0100 | [diff] [blame] | 388 | struct s390_des_ctx *ctx, |
| 389 | struct blkcipher_walk *walk) |
Gerald Schaefer | 0200f3e | 2011-05-04 15:09:44 +1000 | [diff] [blame] | 390 | { |
| 391 | int ret = blkcipher_walk_virt_block(desc, walk, DES_BLOCK_SIZE); |
Harald Freudenberger | ee97dc7 | 2014-01-22 13:01:33 +0100 | [diff] [blame] | 392 | unsigned int n, nbytes; |
| 393 | u8 buf[DES_BLOCK_SIZE], ctrbuf[DES_BLOCK_SIZE]; |
| 394 | u8 *out, *in, *ctrptr = ctrbuf; |
Gerald Schaefer | 0200f3e | 2011-05-04 15:09:44 +1000 | [diff] [blame] | 395 | |
Harald Freudenberger | ee97dc7 | 2014-01-22 13:01:33 +0100 | [diff] [blame] | 396 | if (!walk->nbytes) |
| 397 | return ret; |
| 398 | |
| 399 | if (spin_trylock(&ctrblk_lock)) |
| 400 | ctrptr = ctrblk; |
| 401 | |
| 402 | memcpy(ctrptr, walk->iv, DES_BLOCK_SIZE); |
Gerald Schaefer | 0200f3e | 2011-05-04 15:09:44 +1000 | [diff] [blame] | 403 | while ((nbytes = walk->nbytes) >= DES_BLOCK_SIZE) { |
| 404 | out = walk->dst.virt.addr; |
| 405 | in = walk->src.virt.addr; |
| 406 | while (nbytes >= DES_BLOCK_SIZE) { |
Harald Freudenberger | ee97dc7 | 2014-01-22 13:01:33 +0100 | [diff] [blame] | 407 | if (ctrptr == ctrblk) |
| 408 | n = __ctrblk_init(ctrptr, nbytes); |
| 409 | else |
| 410 | n = DES_BLOCK_SIZE; |
Martin Schwidefsky | 0177db0 | 2016-08-15 10:41:52 +0200 | [diff] [blame] | 411 | cpacf_kmctr(func, ctx->key, out, in, n, ctrptr); |
Gerald Schaefer | 0200f3e | 2011-05-04 15:09:44 +1000 | [diff] [blame] | 412 | if (n > DES_BLOCK_SIZE) |
Harald Freudenberger | ee97dc7 | 2014-01-22 13:01:33 +0100 | [diff] [blame] | 413 | memcpy(ctrptr, ctrptr + n - DES_BLOCK_SIZE, |
Gerald Schaefer | 0200f3e | 2011-05-04 15:09:44 +1000 | [diff] [blame] | 414 | DES_BLOCK_SIZE); |
Harald Freudenberger | ee97dc7 | 2014-01-22 13:01:33 +0100 | [diff] [blame] | 415 | crypto_inc(ctrptr, DES_BLOCK_SIZE); |
Gerald Schaefer | 0200f3e | 2011-05-04 15:09:44 +1000 | [diff] [blame] | 416 | out += n; |
| 417 | in += n; |
| 418 | nbytes -= n; |
| 419 | } |
| 420 | ret = blkcipher_walk_done(desc, walk, nbytes); |
| 421 | } |
Harald Freudenberger | ee97dc7 | 2014-01-22 13:01:33 +0100 | [diff] [blame] | 422 | if (ctrptr == ctrblk) { |
| 423 | if (nbytes) |
| 424 | memcpy(ctrbuf, ctrptr, DES_BLOCK_SIZE); |
| 425 | else |
| 426 | memcpy(walk->iv, ctrptr, DES_BLOCK_SIZE); |
| 427 | spin_unlock(&ctrblk_lock); |
Harald Freudenberger | 3901c11 | 2014-05-07 16:51:29 +0200 | [diff] [blame] | 428 | } else { |
| 429 | if (!nbytes) |
| 430 | memcpy(walk->iv, ctrptr, DES_BLOCK_SIZE); |
Harald Freudenberger | ee97dc7 | 2014-01-22 13:01:33 +0100 | [diff] [blame] | 431 | } |
Gerald Schaefer | 0200f3e | 2011-05-04 15:09:44 +1000 | [diff] [blame] | 432 | /* final block may be < DES_BLOCK_SIZE, copy only nbytes */ |
| 433 | if (nbytes) { |
| 434 | out = walk->dst.virt.addr; |
| 435 | in = walk->src.virt.addr; |
Martin Schwidefsky | 0177db0 | 2016-08-15 10:41:52 +0200 | [diff] [blame] | 436 | cpacf_kmctr(func, ctx->key, buf, in, DES_BLOCK_SIZE, ctrbuf); |
Gerald Schaefer | 0200f3e | 2011-05-04 15:09:44 +1000 | [diff] [blame] | 437 | memcpy(out, buf, nbytes); |
Harald Freudenberger | ee97dc7 | 2014-01-22 13:01:33 +0100 | [diff] [blame] | 438 | crypto_inc(ctrbuf, DES_BLOCK_SIZE); |
Gerald Schaefer | 0200f3e | 2011-05-04 15:09:44 +1000 | [diff] [blame] | 439 | ret = blkcipher_walk_done(desc, walk, 0); |
Harald Freudenberger | ee97dc7 | 2014-01-22 13:01:33 +0100 | [diff] [blame] | 440 | memcpy(walk->iv, ctrbuf, DES_BLOCK_SIZE); |
Gerald Schaefer | 0200f3e | 2011-05-04 15:09:44 +1000 | [diff] [blame] | 441 | } |
Gerald Schaefer | 0200f3e | 2011-05-04 15:09:44 +1000 | [diff] [blame] | 442 | return ret; |
| 443 | } |
| 444 | |
| 445 | static int ctr_des_encrypt(struct blkcipher_desc *desc, |
| 446 | struct scatterlist *dst, struct scatterlist *src, |
| 447 | unsigned int nbytes) |
| 448 | { |
| 449 | struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm); |
| 450 | struct blkcipher_walk walk; |
| 451 | |
| 452 | blkcipher_walk_init(&walk, dst, src, nbytes); |
Martin Schwidefsky | edc63a3 | 2016-08-15 09:19:16 +0200 | [diff] [blame] | 453 | return ctr_desall_crypt(desc, CPACF_KMCTR_DEA, ctx, &walk); |
Gerald Schaefer | 0200f3e | 2011-05-04 15:09:44 +1000 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | static int ctr_des_decrypt(struct blkcipher_desc *desc, |
| 457 | struct scatterlist *dst, struct scatterlist *src, |
| 458 | unsigned int nbytes) |
| 459 | { |
| 460 | struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm); |
| 461 | struct blkcipher_walk walk; |
| 462 | |
| 463 | blkcipher_walk_init(&walk, dst, src, nbytes); |
Martin Schwidefsky | edc63a3 | 2016-08-15 09:19:16 +0200 | [diff] [blame] | 464 | return ctr_desall_crypt(desc, CPACF_KMCTR_DEA | CPACF_DECRYPT, |
| 465 | ctx, &walk); |
Gerald Schaefer | 0200f3e | 2011-05-04 15:09:44 +1000 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | static struct crypto_alg ctr_des_alg = { |
| 469 | .cra_name = "ctr(des)", |
| 470 | .cra_driver_name = "ctr-des-s390", |
Martin Schwidefsky | c7d4d25 | 2016-03-17 15:22:12 +0100 | [diff] [blame] | 471 | .cra_priority = 400, /* combo: des + ctr */ |
Gerald Schaefer | 0200f3e | 2011-05-04 15:09:44 +1000 | [diff] [blame] | 472 | .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER, |
| 473 | .cra_blocksize = 1, |
| 474 | .cra_ctxsize = sizeof(struct s390_des_ctx), |
| 475 | .cra_type = &crypto_blkcipher_type, |
| 476 | .cra_module = THIS_MODULE, |
Gerald Schaefer | 0200f3e | 2011-05-04 15:09:44 +1000 | [diff] [blame] | 477 | .cra_u = { |
| 478 | .blkcipher = { |
| 479 | .min_keysize = DES_KEY_SIZE, |
| 480 | .max_keysize = DES_KEY_SIZE, |
| 481 | .ivsize = DES_BLOCK_SIZE, |
| 482 | .setkey = des_setkey, |
| 483 | .encrypt = ctr_des_encrypt, |
| 484 | .decrypt = ctr_des_decrypt, |
| 485 | } |
| 486 | } |
| 487 | }; |
| 488 | |
| 489 | static int ctr_des3_encrypt(struct blkcipher_desc *desc, |
| 490 | struct scatterlist *dst, struct scatterlist *src, |
| 491 | unsigned int nbytes) |
| 492 | { |
| 493 | struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm); |
| 494 | struct blkcipher_walk walk; |
| 495 | |
| 496 | blkcipher_walk_init(&walk, dst, src, nbytes); |
Martin Schwidefsky | edc63a3 | 2016-08-15 09:19:16 +0200 | [diff] [blame] | 497 | return ctr_desall_crypt(desc, CPACF_KMCTR_TDEA_192, ctx, &walk); |
Gerald Schaefer | 0200f3e | 2011-05-04 15:09:44 +1000 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | static int ctr_des3_decrypt(struct blkcipher_desc *desc, |
| 501 | struct scatterlist *dst, struct scatterlist *src, |
| 502 | unsigned int nbytes) |
| 503 | { |
| 504 | struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm); |
| 505 | struct blkcipher_walk walk; |
| 506 | |
| 507 | blkcipher_walk_init(&walk, dst, src, nbytes); |
Martin Schwidefsky | edc63a3 | 2016-08-15 09:19:16 +0200 | [diff] [blame] | 508 | return ctr_desall_crypt(desc, CPACF_KMCTR_TDEA_192 | CPACF_DECRYPT, |
| 509 | ctx, &walk); |
Gerald Schaefer | 0200f3e | 2011-05-04 15:09:44 +1000 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | static struct crypto_alg ctr_des3_alg = { |
| 513 | .cra_name = "ctr(des3_ede)", |
| 514 | .cra_driver_name = "ctr-des3_ede-s390", |
Martin Schwidefsky | c7d4d25 | 2016-03-17 15:22:12 +0100 | [diff] [blame] | 515 | .cra_priority = 400, /* combo: des3 + ede */ |
Gerald Schaefer | 0200f3e | 2011-05-04 15:09:44 +1000 | [diff] [blame] | 516 | .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER, |
| 517 | .cra_blocksize = 1, |
| 518 | .cra_ctxsize = sizeof(struct s390_des_ctx), |
| 519 | .cra_type = &crypto_blkcipher_type, |
| 520 | .cra_module = THIS_MODULE, |
Gerald Schaefer | 0200f3e | 2011-05-04 15:09:44 +1000 | [diff] [blame] | 521 | .cra_u = { |
| 522 | .blkcipher = { |
| 523 | .min_keysize = DES3_KEY_SIZE, |
| 524 | .max_keysize = DES3_KEY_SIZE, |
| 525 | .ivsize = DES_BLOCK_SIZE, |
| 526 | .setkey = des3_setkey, |
| 527 | .encrypt = ctr_des3_encrypt, |
| 528 | .decrypt = ctr_des3_decrypt, |
| 529 | } |
| 530 | } |
| 531 | }; |
| 532 | |
Martin Schwidefsky | d863d59 | 2016-08-18 12:34:34 +0200 | [diff] [blame] | 533 | static struct crypto_alg *des_s390_algs_ptr[8]; |
| 534 | static int des_s390_algs_num; |
| 535 | |
| 536 | static int des_s390_register_alg(struct crypto_alg *alg) |
| 537 | { |
| 538 | int ret; |
| 539 | |
| 540 | ret = crypto_register_alg(alg); |
| 541 | if (!ret) |
| 542 | des_s390_algs_ptr[des_s390_algs_num++] = alg; |
| 543 | return ret; |
| 544 | } |
| 545 | |
| 546 | static void des_s390_exit(void) |
| 547 | { |
| 548 | while (des_s390_algs_num--) |
| 549 | crypto_unregister_alg(des_s390_algs_ptr[des_s390_algs_num]); |
| 550 | if (ctrblk) |
| 551 | free_page((unsigned long) ctrblk); |
| 552 | } |
| 553 | |
Jan Glauber | 98971f8 | 2011-04-19 21:29:15 +0200 | [diff] [blame] | 554 | static int __init des_s390_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | { |
Jan Glauber | 80d663a | 2010-05-21 22:04:08 +1000 | [diff] [blame] | 556 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | |
Martin Schwidefsky | 69c0e36 | 2016-08-18 12:59:46 +0200 | [diff] [blame^] | 558 | /* Query available functions for KM, KMC and KMCTR */ |
| 559 | cpacf_query(CPACF_KM, &km_functions); |
| 560 | cpacf_query(CPACF_KMC, &kmc_functions); |
| 561 | cpacf_query(CPACF_KMCTR, &kmctr_functions); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | |
Martin Schwidefsky | 69c0e36 | 2016-08-18 12:59:46 +0200 | [diff] [blame^] | 563 | if (cpacf_test_func(&km_functions, CPACF_KM_DEA)) { |
| 564 | ret = des_s390_register_alg(&des_alg); |
| 565 | if (ret) |
| 566 | goto out_err; |
| 567 | ret = des_s390_register_alg(&ecb_des_alg); |
| 568 | if (ret) |
| 569 | goto out_err; |
| 570 | } |
| 571 | if (cpacf_test_func(&kmc_functions, CPACF_KMC_DEA)) { |
| 572 | ret = des_s390_register_alg(&cbc_des_alg); |
| 573 | if (ret) |
| 574 | goto out_err; |
| 575 | } |
| 576 | if (cpacf_test_func(&km_functions, CPACF_KM_TDEA_192)) { |
| 577 | ret = des_s390_register_alg(&des3_alg); |
| 578 | if (ret) |
| 579 | goto out_err; |
| 580 | ret = des_s390_register_alg(&ecb_des3_alg); |
| 581 | if (ret) |
| 582 | goto out_err; |
| 583 | } |
| 584 | if (cpacf_test_func(&kmc_functions, CPACF_KMC_TDEA_192)) { |
| 585 | ret = des_s390_register_alg(&cbc_des3_alg); |
| 586 | if (ret) |
| 587 | goto out_err; |
| 588 | } |
Gerald Schaefer | 0200f3e | 2011-05-04 15:09:44 +1000 | [diff] [blame] | 589 | |
Martin Schwidefsky | 69c0e36 | 2016-08-18 12:59:46 +0200 | [diff] [blame^] | 590 | if (cpacf_test_func(&kmctr_functions, CPACF_KMCTR_DEA) || |
| 591 | cpacf_test_func(&kmctr_functions, CPACF_KMCTR_TDEA_192)) { |
Gerald Schaefer | 0200f3e | 2011-05-04 15:09:44 +1000 | [diff] [blame] | 592 | ctrblk = (u8 *) __get_free_page(GFP_KERNEL); |
| 593 | if (!ctrblk) { |
| 594 | ret = -ENOMEM; |
Martin Schwidefsky | d863d59 | 2016-08-18 12:34:34 +0200 | [diff] [blame] | 595 | goto out_err; |
Gerald Schaefer | 0200f3e | 2011-05-04 15:09:44 +1000 | [diff] [blame] | 596 | } |
Martin Schwidefsky | 69c0e36 | 2016-08-18 12:59:46 +0200 | [diff] [blame^] | 597 | } |
| 598 | |
| 599 | if (cpacf_test_func(&kmctr_functions, CPACF_KMCTR_DEA)) { |
Martin Schwidefsky | d863d59 | 2016-08-18 12:34:34 +0200 | [diff] [blame] | 600 | ret = des_s390_register_alg(&ctr_des_alg); |
| 601 | if (ret) |
| 602 | goto out_err; |
Martin Schwidefsky | 69c0e36 | 2016-08-18 12:59:46 +0200 | [diff] [blame^] | 603 | } |
| 604 | if (cpacf_test_func(&kmctr_functions, CPACF_KMCTR_TDEA_192)) { |
Martin Schwidefsky | d863d59 | 2016-08-18 12:34:34 +0200 | [diff] [blame] | 605 | ret = des_s390_register_alg(&ctr_des3_alg); |
| 606 | if (ret) |
| 607 | goto out_err; |
Gerald Schaefer | 0200f3e | 2011-05-04 15:09:44 +1000 | [diff] [blame] | 608 | } |
Martin Schwidefsky | d863d59 | 2016-08-18 12:34:34 +0200 | [diff] [blame] | 609 | |
| 610 | return 0; |
| 611 | out_err: |
| 612 | des_s390_exit(); |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 613 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | } |
| 615 | |
Hendrik Brueckner | d05377c | 2015-02-19 17:34:07 +0100 | [diff] [blame] | 616 | module_cpu_feature_match(MSA, des_s390_init); |
Jan Glauber | 1efbd15 | 2010-05-21 22:04:46 +1000 | [diff] [blame] | 617 | module_exit(des_s390_exit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | |
Kees Cook | 5d26a10 | 2014-11-20 17:05:53 -0800 | [diff] [blame] | 619 | MODULE_ALIAS_CRYPTO("des"); |
| 620 | MODULE_ALIAS_CRYPTO("des3_ede"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | |
| 622 | MODULE_LICENSE("GPL"); |
| 623 | MODULE_DESCRIPTION("DES & Triple DES EDE Cipher Algorithms"); |