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 | * |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 6 | * Copyright IBM Corp. 2003,2007 |
| 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 | |
| 17 | #include <crypto/algapi.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/init.h> |
| 19 | #include <linux/module.h> |
Jan Glauber | c135783 | 2006-01-14 13:20:53 -0800 | [diff] [blame] | 20 | |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 21 | #include "crypt_s390.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include "crypto_des.h" |
| 23 | |
| 24 | #define DES_BLOCK_SIZE 8 |
| 25 | #define DES_KEY_SIZE 8 |
| 26 | |
| 27 | #define DES3_128_KEY_SIZE (2 * DES_KEY_SIZE) |
| 28 | #define DES3_128_BLOCK_SIZE DES_BLOCK_SIZE |
| 29 | |
| 30 | #define DES3_192_KEY_SIZE (3 * DES_KEY_SIZE) |
| 31 | #define DES3_192_BLOCK_SIZE DES_BLOCK_SIZE |
| 32 | |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 33 | struct crypt_s390_des_ctx { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | u8 iv[DES_BLOCK_SIZE]; |
| 35 | u8 key[DES_KEY_SIZE]; |
| 36 | }; |
| 37 | |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 38 | struct crypt_s390_des3_128_ctx { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | u8 iv[DES_BLOCK_SIZE]; |
| 40 | u8 key[DES3_128_KEY_SIZE]; |
| 41 | }; |
| 42 | |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 43 | struct crypt_s390_des3_192_ctx { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | u8 iv[DES_BLOCK_SIZE]; |
| 45 | u8 key[DES3_192_KEY_SIZE]; |
| 46 | }; |
| 47 | |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 48 | static int des_setkey(struct crypto_tfm *tfm, const u8 *key, |
Herbert Xu | 560c06a | 2006-08-13 14:16:39 +1000 | [diff] [blame] | 49 | unsigned int keylen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | { |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 51 | struct crypt_s390_des_ctx *dctx = crypto_tfm_ctx(tfm); |
Herbert Xu | 560c06a | 2006-08-13 14:16:39 +1000 | [diff] [blame] | 52 | u32 *flags = &tfm->crt_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | int ret; |
| 54 | |
Jan Glauber | c135783 | 2006-01-14 13:20:53 -0800 | [diff] [blame] | 55 | /* test if key is valid (not a weak key) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | ret = crypto_des_check_key(key, keylen, flags); |
Jan Glauber | c135783 | 2006-01-14 13:20:53 -0800 | [diff] [blame] | 57 | if (ret == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | memcpy(dctx->key, key, keylen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | return ret; |
| 60 | } |
| 61 | |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 62 | 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] | 63 | { |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 64 | struct crypt_s390_des_ctx *dctx = crypto_tfm_ctx(tfm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
Jan Glauber | b8dc603 | 2006-01-14 13:20:53 -0800 | [diff] [blame] | 66 | crypt_s390_km(KM_DEA_ENCRYPT, dctx->key, out, in, DES_BLOCK_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 69 | 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] | 70 | { |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 71 | struct crypt_s390_des_ctx *dctx = crypto_tfm_ctx(tfm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | |
Jan Glauber | b8dc603 | 2006-01-14 13:20:53 -0800 | [diff] [blame] | 73 | crypt_s390_km(KM_DEA_DECRYPT, dctx->key, out, in, DES_BLOCK_SIZE); |
| 74 | } |
| 75 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | static struct crypto_alg des_alg = { |
| 77 | .cra_name = "des", |
Herbert Xu | 65b75c3 | 2006-08-21 21:18:50 +1000 | [diff] [blame] | 78 | .cra_driver_name = "des-s390", |
| 79 | .cra_priority = CRYPT_S390_PRIORITY, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | .cra_flags = CRYPTO_ALG_TYPE_CIPHER, |
| 81 | .cra_blocksize = DES_BLOCK_SIZE, |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 82 | .cra_ctxsize = sizeof(struct crypt_s390_des_ctx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | .cra_module = THIS_MODULE, |
| 84 | .cra_list = LIST_HEAD_INIT(des_alg.cra_list), |
Jan Glauber | c135783 | 2006-01-14 13:20:53 -0800 | [diff] [blame] | 85 | .cra_u = { |
| 86 | .cipher = { |
| 87 | .cia_min_keysize = DES_KEY_SIZE, |
| 88 | .cia_max_keysize = DES_KEY_SIZE, |
| 89 | .cia_setkey = des_setkey, |
| 90 | .cia_encrypt = des_encrypt, |
Jan Glauber | b8dc603 | 2006-01-14 13:20:53 -0800 | [diff] [blame] | 91 | .cia_decrypt = des_decrypt, |
Jan Glauber | c135783 | 2006-01-14 13:20:53 -0800 | [diff] [blame] | 92 | } |
| 93 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | }; |
| 95 | |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 96 | static int ecb_desall_crypt(struct blkcipher_desc *desc, long func, |
| 97 | void *param, struct blkcipher_walk *walk) |
| 98 | { |
| 99 | int ret = blkcipher_walk_virt(desc, walk); |
| 100 | unsigned int nbytes; |
| 101 | |
| 102 | while ((nbytes = walk->nbytes)) { |
| 103 | /* only use complete blocks */ |
| 104 | unsigned int n = nbytes & ~(DES_BLOCK_SIZE - 1); |
| 105 | u8 *out = walk->dst.virt.addr; |
| 106 | u8 *in = walk->src.virt.addr; |
| 107 | |
| 108 | ret = crypt_s390_km(func, param, out, in, n); |
| 109 | BUG_ON((ret < 0) || (ret != n)); |
| 110 | |
| 111 | nbytes &= DES_BLOCK_SIZE - 1; |
| 112 | ret = blkcipher_walk_done(desc, walk, nbytes); |
| 113 | } |
| 114 | |
| 115 | return ret; |
| 116 | } |
| 117 | |
| 118 | static int cbc_desall_crypt(struct blkcipher_desc *desc, long func, |
| 119 | void *param, struct blkcipher_walk *walk) |
| 120 | { |
| 121 | int ret = blkcipher_walk_virt(desc, walk); |
| 122 | unsigned int nbytes = walk->nbytes; |
| 123 | |
| 124 | if (!nbytes) |
| 125 | goto out; |
| 126 | |
| 127 | memcpy(param, walk->iv, DES_BLOCK_SIZE); |
| 128 | do { |
| 129 | /* only use complete blocks */ |
| 130 | unsigned int n = nbytes & ~(DES_BLOCK_SIZE - 1); |
| 131 | u8 *out = walk->dst.virt.addr; |
| 132 | u8 *in = walk->src.virt.addr; |
| 133 | |
| 134 | ret = crypt_s390_kmc(func, param, out, in, n); |
| 135 | BUG_ON((ret < 0) || (ret != n)); |
| 136 | |
| 137 | nbytes &= DES_BLOCK_SIZE - 1; |
| 138 | ret = blkcipher_walk_done(desc, walk, nbytes); |
| 139 | } while ((nbytes = walk->nbytes)); |
| 140 | memcpy(walk->iv, param, DES_BLOCK_SIZE); |
| 141 | |
| 142 | out: |
| 143 | return ret; |
| 144 | } |
| 145 | |
| 146 | static int ecb_des_encrypt(struct blkcipher_desc *desc, |
| 147 | struct scatterlist *dst, struct scatterlist *src, |
| 148 | unsigned int nbytes) |
| 149 | { |
| 150 | struct crypt_s390_des_ctx *sctx = crypto_blkcipher_ctx(desc->tfm); |
| 151 | struct blkcipher_walk walk; |
| 152 | |
| 153 | blkcipher_walk_init(&walk, dst, src, nbytes); |
| 154 | return ecb_desall_crypt(desc, KM_DEA_ENCRYPT, sctx->key, &walk); |
| 155 | } |
| 156 | |
| 157 | static int ecb_des_decrypt(struct blkcipher_desc *desc, |
| 158 | struct scatterlist *dst, struct scatterlist *src, |
| 159 | unsigned int nbytes) |
| 160 | { |
| 161 | struct crypt_s390_des_ctx *sctx = crypto_blkcipher_ctx(desc->tfm); |
| 162 | struct blkcipher_walk walk; |
| 163 | |
| 164 | blkcipher_walk_init(&walk, dst, src, nbytes); |
| 165 | return ecb_desall_crypt(desc, KM_DEA_DECRYPT, sctx->key, &walk); |
| 166 | } |
| 167 | |
| 168 | static struct crypto_alg ecb_des_alg = { |
| 169 | .cra_name = "ecb(des)", |
| 170 | .cra_driver_name = "ecb-des-s390", |
| 171 | .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY, |
| 172 | .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER, |
| 173 | .cra_blocksize = DES_BLOCK_SIZE, |
| 174 | .cra_ctxsize = sizeof(struct crypt_s390_des_ctx), |
| 175 | .cra_type = &crypto_blkcipher_type, |
| 176 | .cra_module = THIS_MODULE, |
| 177 | .cra_list = LIST_HEAD_INIT(ecb_des_alg.cra_list), |
| 178 | .cra_u = { |
| 179 | .blkcipher = { |
| 180 | .min_keysize = DES_KEY_SIZE, |
| 181 | .max_keysize = DES_KEY_SIZE, |
| 182 | .setkey = des_setkey, |
| 183 | .encrypt = ecb_des_encrypt, |
| 184 | .decrypt = ecb_des_decrypt, |
| 185 | } |
| 186 | } |
| 187 | }; |
| 188 | |
| 189 | static int cbc_des_encrypt(struct blkcipher_desc *desc, |
| 190 | struct scatterlist *dst, struct scatterlist *src, |
| 191 | unsigned int nbytes) |
| 192 | { |
| 193 | struct crypt_s390_des_ctx *sctx = crypto_blkcipher_ctx(desc->tfm); |
| 194 | struct blkcipher_walk walk; |
| 195 | |
| 196 | blkcipher_walk_init(&walk, dst, src, nbytes); |
| 197 | return cbc_desall_crypt(desc, KMC_DEA_ENCRYPT, sctx->iv, &walk); |
| 198 | } |
| 199 | |
| 200 | static int cbc_des_decrypt(struct blkcipher_desc *desc, |
| 201 | struct scatterlist *dst, struct scatterlist *src, |
| 202 | unsigned int nbytes) |
| 203 | { |
| 204 | struct crypt_s390_des_ctx *sctx = crypto_blkcipher_ctx(desc->tfm); |
| 205 | struct blkcipher_walk walk; |
| 206 | |
| 207 | blkcipher_walk_init(&walk, dst, src, nbytes); |
| 208 | return cbc_desall_crypt(desc, KMC_DEA_DECRYPT, sctx->iv, &walk); |
| 209 | } |
| 210 | |
| 211 | static struct crypto_alg cbc_des_alg = { |
| 212 | .cra_name = "cbc(des)", |
| 213 | .cra_driver_name = "cbc-des-s390", |
| 214 | .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY, |
| 215 | .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER, |
| 216 | .cra_blocksize = DES_BLOCK_SIZE, |
| 217 | .cra_ctxsize = sizeof(struct crypt_s390_des_ctx), |
| 218 | .cra_type = &crypto_blkcipher_type, |
| 219 | .cra_module = THIS_MODULE, |
| 220 | .cra_list = LIST_HEAD_INIT(cbc_des_alg.cra_list), |
| 221 | .cra_u = { |
| 222 | .blkcipher = { |
| 223 | .min_keysize = DES_KEY_SIZE, |
| 224 | .max_keysize = DES_KEY_SIZE, |
| 225 | .ivsize = DES_BLOCK_SIZE, |
| 226 | .setkey = des_setkey, |
| 227 | .encrypt = cbc_des_encrypt, |
| 228 | .decrypt = cbc_des_decrypt, |
| 229 | } |
| 230 | } |
| 231 | }; |
| 232 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | /* |
| 234 | * RFC2451: |
| 235 | * |
| 236 | * For DES-EDE3, there is no known need to reject weak or |
| 237 | * complementation keys. Any weakness is obviated by the use of |
| 238 | * multiple keys. |
| 239 | * |
| 240 | * However, if the two independent 64-bit keys are equal, |
| 241 | * then the DES3 operation is simply the same as DES. |
| 242 | * Implementers MUST reject keys that exhibit this property. |
| 243 | * |
| 244 | */ |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 245 | static int des3_128_setkey(struct crypto_tfm *tfm, const u8 *key, |
Herbert Xu | 560c06a | 2006-08-13 14:16:39 +1000 | [diff] [blame] | 246 | unsigned int keylen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | { |
| 248 | int i, ret; |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 249 | struct crypt_s390_des3_128_ctx *dctx = crypto_tfm_ctx(tfm); |
Herbert Xu | 560c06a | 2006-08-13 14:16:39 +1000 | [diff] [blame] | 250 | const u8 *temp_key = key; |
| 251 | u32 *flags = &tfm->crt_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | |
Jarod Wilson | 03b56ce | 2009-06-18 19:52:59 +0800 | [diff] [blame] | 253 | if (!(memcmp(key, &key[DES_KEY_SIZE], DES_KEY_SIZE)) && |
| 254 | (*flags & CRYPTO_TFM_REQ_WEAK_KEY)) { |
| 255 | *flags |= CRYPTO_TFM_RES_WEAK_KEY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | return -EINVAL; |
| 257 | } |
Jan Glauber | c135783 | 2006-01-14 13:20:53 -0800 | [diff] [blame] | 258 | for (i = 0; i < 2; i++, temp_key += DES_KEY_SIZE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | ret = crypto_des_check_key(temp_key, DES_KEY_SIZE, flags); |
| 260 | if (ret < 0) |
| 261 | return ret; |
| 262 | } |
| 263 | memcpy(dctx->key, key, keylen); |
| 264 | return 0; |
| 265 | } |
| 266 | |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 267 | static void des3_128_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | { |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 269 | struct crypt_s390_des3_128_ctx *dctx = crypto_tfm_ctx(tfm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 271 | crypt_s390_km(KM_TDEA_128_ENCRYPT, dctx->key, dst, (void*)src, |
Jan Glauber | c135783 | 2006-01-14 13:20:53 -0800 | [diff] [blame] | 272 | DES3_128_BLOCK_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | } |
| 274 | |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 275 | static void des3_128_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | { |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 277 | struct crypt_s390_des3_128_ctx *dctx = crypto_tfm_ctx(tfm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 279 | crypt_s390_km(KM_TDEA_128_DECRYPT, dctx->key, dst, (void*)src, |
Jan Glauber | c135783 | 2006-01-14 13:20:53 -0800 | [diff] [blame] | 280 | DES3_128_BLOCK_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | static struct crypto_alg des3_128_alg = { |
| 284 | .cra_name = "des3_ede128", |
Herbert Xu | 65b75c3 | 2006-08-21 21:18:50 +1000 | [diff] [blame] | 285 | .cra_driver_name = "des3_ede128-s390", |
| 286 | .cra_priority = CRYPT_S390_PRIORITY, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | .cra_flags = CRYPTO_ALG_TYPE_CIPHER, |
| 288 | .cra_blocksize = DES3_128_BLOCK_SIZE, |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 289 | .cra_ctxsize = sizeof(struct crypt_s390_des3_128_ctx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | .cra_module = THIS_MODULE, |
| 291 | .cra_list = LIST_HEAD_INIT(des3_128_alg.cra_list), |
Jan Glauber | c135783 | 2006-01-14 13:20:53 -0800 | [diff] [blame] | 292 | .cra_u = { |
| 293 | .cipher = { |
| 294 | .cia_min_keysize = DES3_128_KEY_SIZE, |
| 295 | .cia_max_keysize = DES3_128_KEY_SIZE, |
| 296 | .cia_setkey = des3_128_setkey, |
| 297 | .cia_encrypt = des3_128_encrypt, |
Jan Glauber | b8dc603 | 2006-01-14 13:20:53 -0800 | [diff] [blame] | 298 | .cia_decrypt = des3_128_decrypt, |
Jan Glauber | c135783 | 2006-01-14 13:20:53 -0800 | [diff] [blame] | 299 | } |
| 300 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | }; |
| 302 | |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 303 | static int ecb_des3_128_encrypt(struct blkcipher_desc *desc, |
| 304 | struct scatterlist *dst, |
| 305 | struct scatterlist *src, unsigned int nbytes) |
| 306 | { |
| 307 | struct crypt_s390_des3_128_ctx *sctx = crypto_blkcipher_ctx(desc->tfm); |
| 308 | struct blkcipher_walk walk; |
| 309 | |
| 310 | blkcipher_walk_init(&walk, dst, src, nbytes); |
| 311 | return ecb_desall_crypt(desc, KM_TDEA_128_ENCRYPT, sctx->key, &walk); |
| 312 | } |
| 313 | |
| 314 | static int ecb_des3_128_decrypt(struct blkcipher_desc *desc, |
| 315 | struct scatterlist *dst, |
| 316 | struct scatterlist *src, unsigned int nbytes) |
| 317 | { |
| 318 | struct crypt_s390_des3_128_ctx *sctx = crypto_blkcipher_ctx(desc->tfm); |
| 319 | struct blkcipher_walk walk; |
| 320 | |
| 321 | blkcipher_walk_init(&walk, dst, src, nbytes); |
| 322 | return ecb_desall_crypt(desc, KM_TDEA_128_DECRYPT, sctx->key, &walk); |
| 323 | } |
| 324 | |
| 325 | static struct crypto_alg ecb_des3_128_alg = { |
| 326 | .cra_name = "ecb(des3_ede128)", |
| 327 | .cra_driver_name = "ecb-des3_ede128-s390", |
| 328 | .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY, |
| 329 | .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER, |
| 330 | .cra_blocksize = DES3_128_BLOCK_SIZE, |
| 331 | .cra_ctxsize = sizeof(struct crypt_s390_des3_128_ctx), |
| 332 | .cra_type = &crypto_blkcipher_type, |
| 333 | .cra_module = THIS_MODULE, |
| 334 | .cra_list = LIST_HEAD_INIT( |
| 335 | ecb_des3_128_alg.cra_list), |
| 336 | .cra_u = { |
| 337 | .blkcipher = { |
| 338 | .min_keysize = DES3_128_KEY_SIZE, |
| 339 | .max_keysize = DES3_128_KEY_SIZE, |
| 340 | .setkey = des3_128_setkey, |
| 341 | .encrypt = ecb_des3_128_encrypt, |
| 342 | .decrypt = ecb_des3_128_decrypt, |
| 343 | } |
| 344 | } |
| 345 | }; |
| 346 | |
| 347 | static int cbc_des3_128_encrypt(struct blkcipher_desc *desc, |
| 348 | struct scatterlist *dst, |
| 349 | struct scatterlist *src, unsigned int nbytes) |
| 350 | { |
| 351 | struct crypt_s390_des3_128_ctx *sctx = crypto_blkcipher_ctx(desc->tfm); |
| 352 | struct blkcipher_walk walk; |
| 353 | |
| 354 | blkcipher_walk_init(&walk, dst, src, nbytes); |
| 355 | return cbc_desall_crypt(desc, KMC_TDEA_128_ENCRYPT, sctx->iv, &walk); |
| 356 | } |
| 357 | |
| 358 | static int cbc_des3_128_decrypt(struct blkcipher_desc *desc, |
| 359 | struct scatterlist *dst, |
| 360 | struct scatterlist *src, unsigned int nbytes) |
| 361 | { |
| 362 | struct crypt_s390_des3_128_ctx *sctx = crypto_blkcipher_ctx(desc->tfm); |
| 363 | struct blkcipher_walk walk; |
| 364 | |
| 365 | blkcipher_walk_init(&walk, dst, src, nbytes); |
| 366 | return cbc_desall_crypt(desc, KMC_TDEA_128_DECRYPT, sctx->iv, &walk); |
| 367 | } |
| 368 | |
| 369 | static struct crypto_alg cbc_des3_128_alg = { |
| 370 | .cra_name = "cbc(des3_ede128)", |
| 371 | .cra_driver_name = "cbc-des3_ede128-s390", |
| 372 | .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY, |
| 373 | .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER, |
| 374 | .cra_blocksize = DES3_128_BLOCK_SIZE, |
| 375 | .cra_ctxsize = sizeof(struct crypt_s390_des3_128_ctx), |
| 376 | .cra_type = &crypto_blkcipher_type, |
| 377 | .cra_module = THIS_MODULE, |
| 378 | .cra_list = LIST_HEAD_INIT( |
| 379 | cbc_des3_128_alg.cra_list), |
| 380 | .cra_u = { |
| 381 | .blkcipher = { |
| 382 | .min_keysize = DES3_128_KEY_SIZE, |
| 383 | .max_keysize = DES3_128_KEY_SIZE, |
| 384 | .ivsize = DES3_128_BLOCK_SIZE, |
| 385 | .setkey = des3_128_setkey, |
| 386 | .encrypt = cbc_des3_128_encrypt, |
| 387 | .decrypt = cbc_des3_128_decrypt, |
| 388 | } |
| 389 | } |
| 390 | }; |
| 391 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | /* |
| 393 | * RFC2451: |
| 394 | * |
| 395 | * For DES-EDE3, there is no known need to reject weak or |
| 396 | * complementation keys. Any weakness is obviated by the use of |
| 397 | * multiple keys. |
| 398 | * |
| 399 | * However, if the first two or last two independent 64-bit keys are |
| 400 | * equal (k1 == k2 or k2 == k3), then the DES3 operation is simply the |
| 401 | * same as DES. Implementers MUST reject keys that exhibit this |
| 402 | * property. |
| 403 | * |
| 404 | */ |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 405 | static int des3_192_setkey(struct crypto_tfm *tfm, const u8 *key, |
Herbert Xu | 560c06a | 2006-08-13 14:16:39 +1000 | [diff] [blame] | 406 | unsigned int keylen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | { |
| 408 | int i, ret; |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 409 | struct crypt_s390_des3_192_ctx *dctx = crypto_tfm_ctx(tfm); |
Herbert Xu | 560c06a | 2006-08-13 14:16:39 +1000 | [diff] [blame] | 410 | const u8 *temp_key = key; |
| 411 | u32 *flags = &tfm->crt_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | if (!(memcmp(key, &key[DES_KEY_SIZE], DES_KEY_SIZE) && |
| 414 | memcmp(&key[DES_KEY_SIZE], &key[DES_KEY_SIZE * 2], |
Jarod Wilson | 03b56ce | 2009-06-18 19:52:59 +0800 | [diff] [blame] | 415 | DES_KEY_SIZE)) && |
| 416 | (*flags & CRYPTO_TFM_REQ_WEAK_KEY)) { |
| 417 | *flags |= CRYPTO_TFM_RES_WEAK_KEY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | return -EINVAL; |
| 419 | } |
| 420 | for (i = 0; i < 3; i++, temp_key += DES_KEY_SIZE) { |
| 421 | ret = crypto_des_check_key(temp_key, DES_KEY_SIZE, flags); |
Jan Glauber | c135783 | 2006-01-14 13:20:53 -0800 | [diff] [blame] | 422 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | } |
| 425 | memcpy(dctx->key, key, keylen); |
| 426 | return 0; |
| 427 | } |
| 428 | |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 429 | static void des3_192_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | { |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 431 | struct crypt_s390_des3_192_ctx *dctx = crypto_tfm_ctx(tfm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 433 | crypt_s390_km(KM_TDEA_192_ENCRYPT, dctx->key, dst, (void*)src, |
Jan Glauber | c135783 | 2006-01-14 13:20:53 -0800 | [diff] [blame] | 434 | DES3_192_BLOCK_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | } |
| 436 | |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 437 | static void des3_192_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | { |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 439 | struct crypt_s390_des3_192_ctx *dctx = crypto_tfm_ctx(tfm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 441 | crypt_s390_km(KM_TDEA_192_DECRYPT, dctx->key, dst, (void*)src, |
Jan Glauber | c135783 | 2006-01-14 13:20:53 -0800 | [diff] [blame] | 442 | DES3_192_BLOCK_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | static struct crypto_alg des3_192_alg = { |
| 446 | .cra_name = "des3_ede", |
Herbert Xu | 65b75c3 | 2006-08-21 21:18:50 +1000 | [diff] [blame] | 447 | .cra_driver_name = "des3_ede-s390", |
| 448 | .cra_priority = CRYPT_S390_PRIORITY, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | .cra_flags = CRYPTO_ALG_TYPE_CIPHER, |
| 450 | .cra_blocksize = DES3_192_BLOCK_SIZE, |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 451 | .cra_ctxsize = sizeof(struct crypt_s390_des3_192_ctx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | .cra_module = THIS_MODULE, |
| 453 | .cra_list = LIST_HEAD_INIT(des3_192_alg.cra_list), |
Jan Glauber | c135783 | 2006-01-14 13:20:53 -0800 | [diff] [blame] | 454 | .cra_u = { |
| 455 | .cipher = { |
| 456 | .cia_min_keysize = DES3_192_KEY_SIZE, |
| 457 | .cia_max_keysize = DES3_192_KEY_SIZE, |
| 458 | .cia_setkey = des3_192_setkey, |
| 459 | .cia_encrypt = des3_192_encrypt, |
Jan Glauber | b8dc603 | 2006-01-14 13:20:53 -0800 | [diff] [blame] | 460 | .cia_decrypt = des3_192_decrypt, |
Jan Glauber | c135783 | 2006-01-14 13:20:53 -0800 | [diff] [blame] | 461 | } |
| 462 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | }; |
| 464 | |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 465 | static int ecb_des3_192_encrypt(struct blkcipher_desc *desc, |
| 466 | struct scatterlist *dst, |
| 467 | struct scatterlist *src, unsigned int nbytes) |
| 468 | { |
| 469 | struct crypt_s390_des3_192_ctx *sctx = crypto_blkcipher_ctx(desc->tfm); |
| 470 | struct blkcipher_walk walk; |
| 471 | |
| 472 | blkcipher_walk_init(&walk, dst, src, nbytes); |
| 473 | return ecb_desall_crypt(desc, KM_TDEA_192_ENCRYPT, sctx->key, &walk); |
| 474 | } |
| 475 | |
| 476 | static int ecb_des3_192_decrypt(struct blkcipher_desc *desc, |
| 477 | struct scatterlist *dst, |
| 478 | struct scatterlist *src, unsigned int nbytes) |
| 479 | { |
| 480 | struct crypt_s390_des3_192_ctx *sctx = crypto_blkcipher_ctx(desc->tfm); |
| 481 | struct blkcipher_walk walk; |
| 482 | |
| 483 | blkcipher_walk_init(&walk, dst, src, nbytes); |
| 484 | return ecb_desall_crypt(desc, KM_TDEA_192_DECRYPT, sctx->key, &walk); |
| 485 | } |
| 486 | |
| 487 | static struct crypto_alg ecb_des3_192_alg = { |
| 488 | .cra_name = "ecb(des3_ede)", |
| 489 | .cra_driver_name = "ecb-des3_ede-s390", |
| 490 | .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY, |
| 491 | .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER, |
| 492 | .cra_blocksize = DES3_192_BLOCK_SIZE, |
| 493 | .cra_ctxsize = sizeof(struct crypt_s390_des3_192_ctx), |
| 494 | .cra_type = &crypto_blkcipher_type, |
| 495 | .cra_module = THIS_MODULE, |
| 496 | .cra_list = LIST_HEAD_INIT( |
| 497 | ecb_des3_192_alg.cra_list), |
| 498 | .cra_u = { |
| 499 | .blkcipher = { |
| 500 | .min_keysize = DES3_192_KEY_SIZE, |
| 501 | .max_keysize = DES3_192_KEY_SIZE, |
| 502 | .setkey = des3_192_setkey, |
| 503 | .encrypt = ecb_des3_192_encrypt, |
| 504 | .decrypt = ecb_des3_192_decrypt, |
| 505 | } |
| 506 | } |
| 507 | }; |
| 508 | |
| 509 | static int cbc_des3_192_encrypt(struct blkcipher_desc *desc, |
| 510 | struct scatterlist *dst, |
| 511 | struct scatterlist *src, unsigned int nbytes) |
| 512 | { |
| 513 | struct crypt_s390_des3_192_ctx *sctx = crypto_blkcipher_ctx(desc->tfm); |
| 514 | struct blkcipher_walk walk; |
| 515 | |
| 516 | blkcipher_walk_init(&walk, dst, src, nbytes); |
| 517 | return cbc_desall_crypt(desc, KMC_TDEA_192_ENCRYPT, sctx->iv, &walk); |
| 518 | } |
| 519 | |
| 520 | static int cbc_des3_192_decrypt(struct blkcipher_desc *desc, |
| 521 | struct scatterlist *dst, |
| 522 | struct scatterlist *src, unsigned int nbytes) |
| 523 | { |
| 524 | struct crypt_s390_des3_192_ctx *sctx = crypto_blkcipher_ctx(desc->tfm); |
| 525 | struct blkcipher_walk walk; |
| 526 | |
| 527 | blkcipher_walk_init(&walk, dst, src, nbytes); |
| 528 | return cbc_desall_crypt(desc, KMC_TDEA_192_DECRYPT, sctx->iv, &walk); |
| 529 | } |
| 530 | |
| 531 | static struct crypto_alg cbc_des3_192_alg = { |
| 532 | .cra_name = "cbc(des3_ede)", |
| 533 | .cra_driver_name = "cbc-des3_ede-s390", |
| 534 | .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY, |
| 535 | .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER, |
| 536 | .cra_blocksize = DES3_192_BLOCK_SIZE, |
| 537 | .cra_ctxsize = sizeof(struct crypt_s390_des3_192_ctx), |
| 538 | .cra_type = &crypto_blkcipher_type, |
| 539 | .cra_module = THIS_MODULE, |
| 540 | .cra_list = LIST_HEAD_INIT( |
| 541 | cbc_des3_192_alg.cra_list), |
| 542 | .cra_u = { |
| 543 | .blkcipher = { |
| 544 | .min_keysize = DES3_192_KEY_SIZE, |
| 545 | .max_keysize = DES3_192_KEY_SIZE, |
| 546 | .ivsize = DES3_192_BLOCK_SIZE, |
| 547 | .setkey = des3_192_setkey, |
| 548 | .encrypt = cbc_des3_192_encrypt, |
| 549 | .decrypt = cbc_des3_192_decrypt, |
| 550 | } |
| 551 | } |
| 552 | }; |
| 553 | |
Heiko Carstens | 9f7819c | 2008-04-17 07:46:17 +0200 | [diff] [blame] | 554 | static int des_s390_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | { |
Jan Glauber | c135783 | 2006-01-14 13:20:53 -0800 | [diff] [blame] | 556 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 558 | if (!crypt_s390_func_available(KM_DEA_ENCRYPT) || |
| 559 | !crypt_s390_func_available(KM_TDEA_128_ENCRYPT) || |
Jan Glauber | c135783 | 2006-01-14 13:20:53 -0800 | [diff] [blame] | 560 | !crypt_s390_func_available(KM_TDEA_192_ENCRYPT)) |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 561 | return -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 563 | ret = crypto_register_alg(&des_alg); |
| 564 | if (ret) |
| 565 | goto des_err; |
| 566 | ret = crypto_register_alg(&ecb_des_alg); |
| 567 | if (ret) |
| 568 | goto ecb_des_err; |
| 569 | ret = crypto_register_alg(&cbc_des_alg); |
| 570 | if (ret) |
| 571 | goto cbc_des_err; |
| 572 | |
| 573 | ret = crypto_register_alg(&des3_128_alg); |
| 574 | if (ret) |
| 575 | goto des3_128_err; |
| 576 | ret = crypto_register_alg(&ecb_des3_128_alg); |
| 577 | if (ret) |
| 578 | goto ecb_des3_128_err; |
| 579 | ret = crypto_register_alg(&cbc_des3_128_alg); |
| 580 | if (ret) |
| 581 | goto cbc_des3_128_err; |
| 582 | |
| 583 | ret = crypto_register_alg(&des3_192_alg); |
| 584 | if (ret) |
| 585 | goto des3_192_err; |
| 586 | ret = crypto_register_alg(&ecb_des3_192_alg); |
| 587 | if (ret) |
| 588 | goto ecb_des3_192_err; |
| 589 | ret = crypto_register_alg(&cbc_des3_192_alg); |
| 590 | if (ret) |
| 591 | goto cbc_des3_192_err; |
| 592 | |
| 593 | out: |
| 594 | return ret; |
| 595 | |
| 596 | cbc_des3_192_err: |
| 597 | crypto_unregister_alg(&ecb_des3_192_alg); |
| 598 | ecb_des3_192_err: |
| 599 | crypto_unregister_alg(&des3_192_alg); |
| 600 | des3_192_err: |
| 601 | crypto_unregister_alg(&cbc_des3_128_alg); |
| 602 | cbc_des3_128_err: |
| 603 | crypto_unregister_alg(&ecb_des3_128_alg); |
| 604 | ecb_des3_128_err: |
| 605 | crypto_unregister_alg(&des3_128_alg); |
| 606 | des3_128_err: |
| 607 | crypto_unregister_alg(&cbc_des_alg); |
| 608 | cbc_des_err: |
| 609 | crypto_unregister_alg(&ecb_des_alg); |
| 610 | ecb_des_err: |
| 611 | crypto_unregister_alg(&des_alg); |
| 612 | des_err: |
| 613 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | } |
| 615 | |
Heiko Carstens | 9f7819c | 2008-04-17 07:46:17 +0200 | [diff] [blame] | 616 | static void __exit des_s390_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | { |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 618 | crypto_unregister_alg(&cbc_des3_192_alg); |
| 619 | crypto_unregister_alg(&ecb_des3_192_alg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | crypto_unregister_alg(&des3_192_alg); |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 621 | crypto_unregister_alg(&cbc_des3_128_alg); |
| 622 | crypto_unregister_alg(&ecb_des3_128_alg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | crypto_unregister_alg(&des3_128_alg); |
Herbert Xu | a9e62fa | 2006-08-21 21:39:24 +1000 | [diff] [blame] | 624 | crypto_unregister_alg(&cbc_des_alg); |
| 625 | crypto_unregister_alg(&ecb_des_alg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | crypto_unregister_alg(&des_alg); |
| 627 | } |
| 628 | |
Heiko Carstens | 9f7819c | 2008-04-17 07:46:17 +0200 | [diff] [blame] | 629 | module_init(des_s390_init); |
| 630 | module_exit(des_s390_fini); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | |
| 632 | MODULE_ALIAS("des"); |
| 633 | MODULE_ALIAS("des3_ede"); |
| 634 | |
| 635 | MODULE_LICENSE("GPL"); |
| 636 | MODULE_DESCRIPTION("DES & Triple DES EDE Cipher Algorithms"); |