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