blob: 965587eefc3996d8d1b66f30dfb30700ced03d36 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Cryptographic API.
3 *
Jan Glauberc1e26e12006-01-06 00:19:17 -08004 * s390 implementation of the DES Cipher Algorithm.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02006 * Copyright IBM Corp. 2003, 2011
Jan Glauber86aa9fc2007-02-05 21:18:14 +01007 * Author(s): Thomas Spatzier
8 * Jan Glauber (jan.glauber@de.ibm.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
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 Xua9e62fa2006-08-21 21:39:24 +100016
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/init.h>
18#include <linux/module.h>
Hendrik Bruecknerd05377c2015-02-19 17:34:07 +010019#include <linux/cpufeature.h>
Jan Glauber1efbd152010-05-21 22:04:46 +100020#include <linux/crypto.h>
21#include <crypto/algapi.h>
22#include <crypto/des.h>
Martin Schwidefskyc7d4d252016-03-17 15:22:12 +010023#include <asm/cpacf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Jan Glauber98971f82011-04-19 21:29:15 +020025#define DES3_KEY_SIZE (3 * DES_KEY_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Gerald Schaefer0200f3e2011-05-04 15:09:44 +100027static u8 *ctrblk;
Harald Freudenbergeree97dc72014-01-22 13:01:33 +010028static DEFINE_SPINLOCK(ctrblk_lock);
Gerald Schaefer0200f3e2011-05-04 15:09:44 +100029
Martin Schwidefsky69c0e362016-08-18 12:59:46 +020030static cpacf_mask_t km_functions, kmc_functions, kmctr_functions;
31
Jan Glauber98971f82011-04-19 21:29:15 +020032struct s390_des_ctx {
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 u8 iv[DES_BLOCK_SIZE];
Jan Glauber98971f82011-04-19 21:29:15 +020034 u8 key[DES3_KEY_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -070035};
36
Herbert Xu6c2bb982006-05-16 22:09:29 +100037static int des_setkey(struct crypto_tfm *tfm, const u8 *key,
Jan Glauber98971f82011-04-19 21:29:15 +020038 unsigned int key_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039{
Jan Glauber98971f82011-04-19 21:29:15 +020040 struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
Jan Glauber1efbd152010-05-21 22:04:46 +100041 u32 tmp[DES_EXPKEY_WORDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Jan Glauber1efbd152010-05-21 22:04:46 +100043 /* check for weak keys */
Martin Schwidefsky69c0e362016-08-18 12:59:46 +020044 if (!des_ekey(tmp, key) &&
45 (tfm->crt_flags & CRYPTO_TFM_REQ_WEAK_KEY)) {
46 tfm->crt_flags |= CRYPTO_TFM_RES_WEAK_KEY;
Jan Glauber1efbd152010-05-21 22:04:46 +100047 return -EINVAL;
48 }
49
Jan Glauber98971f82011-04-19 21:29:15 +020050 memcpy(ctx->key, key, key_len);
Jan Glauber1efbd152010-05-21 22:04:46 +100051 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052}
53
Herbert Xu6c2bb982006-05-16 22:09:29 +100054static void des_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
Jan Glauber98971f82011-04-19 21:29:15 +020056 struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Martin Schwidefskyedc63a32016-08-15 09:19:16 +020058 cpacf_km(CPACF_KM_DEA, ctx->key, out, in, DES_BLOCK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059}
60
Herbert Xu6c2bb982006-05-16 22:09:29 +100061static void des_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
Jan Glauber98971f82011-04-19 21:29:15 +020063 struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Martin Schwidefskyedc63a32016-08-15 09:19:16 +020065 cpacf_km(CPACF_KM_DEA | CPACF_DECRYPT,
66 ctx->key, out, in, DES_BLOCK_SIZE);
Jan Glauberb8dc6032006-01-14 13:20:53 -080067}
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static struct crypto_alg des_alg = {
70 .cra_name = "des",
Herbert Xu65b75c32006-08-21 21:18:50 +100071 .cra_driver_name = "des-s390",
Martin Schwidefskyc7d4d252016-03-17 15:22:12 +010072 .cra_priority = 300,
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
74 .cra_blocksize = DES_BLOCK_SIZE,
Jan Glauber98971f82011-04-19 21:29:15 +020075 .cra_ctxsize = sizeof(struct s390_des_ctx),
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 .cra_module = THIS_MODULE,
Jan Glauberc1357832006-01-14 13:20:53 -080077 .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 Glauberb8dc6032006-01-14 13:20:53 -080083 .cia_decrypt = des_decrypt,
Jan Glauberc1357832006-01-14 13:20:53 -080084 }
85 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070086};
87
Herbert Xua9e62fa2006-08-21 21:39:24 +100088static int ecb_desall_crypt(struct blkcipher_desc *desc, long func,
Jan Glauber98971f82011-04-19 21:29:15 +020089 u8 *key, struct blkcipher_walk *walk)
Herbert Xua9e62fa2006-08-21 21:39:24 +100090{
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 Schwidefsky0177db02016-08-15 10:41:52 +0200100 cpacf_km(func, key, out, in, n);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000101
102 nbytes &= DES_BLOCK_SIZE - 1;
103 ret = blkcipher_walk_done(desc, walk, nbytes);
104 }
105
106 return ret;
107}
108
109static int cbc_desall_crypt(struct blkcipher_desc *desc, long func,
Harald Freudenbergeradc3fcf2014-01-22 13:00:04 +0100110 struct blkcipher_walk *walk)
Herbert Xua9e62fa2006-08-21 21:39:24 +1000111{
Harald Freudenbergeradc3fcf2014-01-22 13:00:04 +0100112 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000113 int ret = blkcipher_walk_virt(desc, walk);
114 unsigned int nbytes = walk->nbytes;
Harald Freudenbergeradc3fcf2014-01-22 13:00:04 +0100115 struct {
116 u8 iv[DES_BLOCK_SIZE];
117 u8 key[DES3_KEY_SIZE];
118 } param;
Herbert Xua9e62fa2006-08-21 21:39:24 +1000119
120 if (!nbytes)
121 goto out;
122
Harald Freudenbergeradc3fcf2014-01-22 13:00:04 +0100123 memcpy(param.iv, walk->iv, DES_BLOCK_SIZE);
124 memcpy(param.key, ctx->key, DES3_KEY_SIZE);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000125 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 Schwidefsky0177db02016-08-15 10:41:52 +0200131 cpacf_kmc(func, &param, out, in, n);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000132
133 nbytes &= DES_BLOCK_SIZE - 1;
134 ret = blkcipher_walk_done(desc, walk, nbytes);
135 } while ((nbytes = walk->nbytes));
Harald Freudenbergeradc3fcf2014-01-22 13:00:04 +0100136 memcpy(walk->iv, param.iv, DES_BLOCK_SIZE);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000137
138out:
139 return ret;
140}
141
142static int ecb_des_encrypt(struct blkcipher_desc *desc,
143 struct scatterlist *dst, struct scatterlist *src,
144 unsigned int nbytes)
145{
Jan Glauber98971f82011-04-19 21:29:15 +0200146 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000147 struct blkcipher_walk walk;
148
149 blkcipher_walk_init(&walk, dst, src, nbytes);
Martin Schwidefskyedc63a32016-08-15 09:19:16 +0200150 return ecb_desall_crypt(desc, CPACF_KM_DEA, ctx->key, &walk);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000151}
152
153static int ecb_des_decrypt(struct blkcipher_desc *desc,
154 struct scatterlist *dst, struct scatterlist *src,
155 unsigned int nbytes)
156{
Jan Glauber98971f82011-04-19 21:29:15 +0200157 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000158 struct blkcipher_walk walk;
159
160 blkcipher_walk_init(&walk, dst, src, nbytes);
Martin Schwidefskyedc63a32016-08-15 09:19:16 +0200161 return ecb_desall_crypt(desc, CPACF_KM_DEA | CPACF_DECRYPT,
162 ctx->key, &walk);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000163}
164
165static struct crypto_alg ecb_des_alg = {
166 .cra_name = "ecb(des)",
167 .cra_driver_name = "ecb-des-s390",
Martin Schwidefskyc7d4d252016-03-17 15:22:12 +0100168 .cra_priority = 400, /* combo: des + ecb */
Herbert Xua9e62fa2006-08-21 21:39:24 +1000169 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
170 .cra_blocksize = DES_BLOCK_SIZE,
Jan Glauber98971f82011-04-19 21:29:15 +0200171 .cra_ctxsize = sizeof(struct s390_des_ctx),
Herbert Xua9e62fa2006-08-21 21:39:24 +1000172 .cra_type = &crypto_blkcipher_type,
173 .cra_module = THIS_MODULE,
Herbert Xua9e62fa2006-08-21 21:39:24 +1000174 .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
185static int cbc_des_encrypt(struct blkcipher_desc *desc,
186 struct scatterlist *dst, struct scatterlist *src,
187 unsigned int nbytes)
188{
Herbert Xua9e62fa2006-08-21 21:39:24 +1000189 struct blkcipher_walk walk;
190
191 blkcipher_walk_init(&walk, dst, src, nbytes);
Martin Schwidefskyedc63a32016-08-15 09:19:16 +0200192 return cbc_desall_crypt(desc, CPACF_KMC_DEA, &walk);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000193}
194
195static int cbc_des_decrypt(struct blkcipher_desc *desc,
196 struct scatterlist *dst, struct scatterlist *src,
197 unsigned int nbytes)
198{
Herbert Xua9e62fa2006-08-21 21:39:24 +1000199 struct blkcipher_walk walk;
200
201 blkcipher_walk_init(&walk, dst, src, nbytes);
Martin Schwidefskyedc63a32016-08-15 09:19:16 +0200202 return cbc_desall_crypt(desc, CPACF_KMC_DEA | CPACF_DECRYPT, &walk);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000203}
204
205static struct crypto_alg cbc_des_alg = {
206 .cra_name = "cbc(des)",
207 .cra_driver_name = "cbc-des-s390",
Martin Schwidefskyc7d4d252016-03-17 15:22:12 +0100208 .cra_priority = 400, /* combo: des + cbc */
Herbert Xua9e62fa2006-08-21 21:39:24 +1000209 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
210 .cra_blocksize = DES_BLOCK_SIZE,
Jan Glauber98971f82011-04-19 21:29:15 +0200211 .cra_ctxsize = sizeof(struct s390_des_ctx),
Herbert Xua9e62fa2006-08-21 21:39:24 +1000212 .cra_type = &crypto_blkcipher_type,
213 .cra_module = THIS_MODULE,
Herbert Xua9e62fa2006-08-21 21:39:24 +1000214 .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 Torvalds1da177e2005-04-16 15:20:36 -0700226/*
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 Torvalds1da177e2005-04-16 15:20:36 -0700233 * 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 Glauber98971f82011-04-19 21:29:15 +0200239static int des3_setkey(struct crypto_tfm *tfm, const u8 *key,
240 unsigned int key_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
Jan Glauber98971f82011-04-19 21:29:15 +0200242 struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Daniel Borkmannfed28612013-12-11 11:28:59 +0100244 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 Schwidefsky69c0e362016-08-18 12:59:46 +0200247 (tfm->crt_flags & CRYPTO_TFM_REQ_WEAK_KEY)) {
248 tfm->crt_flags |= CRYPTO_TFM_RES_WEAK_KEY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return -EINVAL;
250 }
Jan Glauber98971f82011-04-19 21:29:15 +0200251 memcpy(ctx->key, key, key_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 return 0;
253}
254
Jan Glauber98971f82011-04-19 21:29:15 +0200255static void des3_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
Jan Glauber98971f82011-04-19 21:29:15 +0200257 struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Martin Schwidefskyedc63a32016-08-15 09:19:16 +0200259 cpacf_km(CPACF_KM_TDEA_192, ctx->key, dst, src, DES_BLOCK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
261
Jan Glauber98971f82011-04-19 21:29:15 +0200262static void des3_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
Jan Glauber98971f82011-04-19 21:29:15 +0200264 struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Martin Schwidefskyedc63a32016-08-15 09:19:16 +0200266 cpacf_km(CPACF_KM_TDEA_192 | CPACF_DECRYPT,
267 ctx->key, dst, src, DES_BLOCK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}
269
Jan Glauber98971f82011-04-19 21:29:15 +0200270static struct crypto_alg des3_alg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 .cra_name = "des3_ede",
Herbert Xu65b75c32006-08-21 21:18:50 +1000272 .cra_driver_name = "des3_ede-s390",
Martin Schwidefskyc7d4d252016-03-17 15:22:12 +0100273 .cra_priority = 300,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
Jan Glauber1efbd152010-05-21 22:04:46 +1000275 .cra_blocksize = DES_BLOCK_SIZE,
Jan Glauber98971f82011-04-19 21:29:15 +0200276 .cra_ctxsize = sizeof(struct s390_des_ctx),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 .cra_module = THIS_MODULE,
Jan Glauberc1357832006-01-14 13:20:53 -0800278 .cra_u = {
279 .cipher = {
Jan Glauber98971f82011-04-19 21:29:15 +0200280 .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 Glauberc1357832006-01-14 13:20:53 -0800285 }
286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287};
288
Jan Glauber98971f82011-04-19 21:29:15 +0200289static int ecb_des3_encrypt(struct blkcipher_desc *desc,
290 struct scatterlist *dst, struct scatterlist *src,
291 unsigned int nbytes)
Herbert Xua9e62fa2006-08-21 21:39:24 +1000292{
Jan Glauber98971f82011-04-19 21:29:15 +0200293 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000294 struct blkcipher_walk walk;
295
296 blkcipher_walk_init(&walk, dst, src, nbytes);
Martin Schwidefskyedc63a32016-08-15 09:19:16 +0200297 return ecb_desall_crypt(desc, CPACF_KM_TDEA_192, ctx->key, &walk);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000298}
299
Jan Glauber98971f82011-04-19 21:29:15 +0200300static int ecb_des3_decrypt(struct blkcipher_desc *desc,
301 struct scatterlist *dst, struct scatterlist *src,
302 unsigned int nbytes)
Herbert Xua9e62fa2006-08-21 21:39:24 +1000303{
Jan Glauber98971f82011-04-19 21:29:15 +0200304 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000305 struct blkcipher_walk walk;
306
307 blkcipher_walk_init(&walk, dst, src, nbytes);
Martin Schwidefskyedc63a32016-08-15 09:19:16 +0200308 return ecb_desall_crypt(desc, CPACF_KM_TDEA_192 | CPACF_DECRYPT,
309 ctx->key, &walk);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000310}
311
Jan Glauber98971f82011-04-19 21:29:15 +0200312static struct crypto_alg ecb_des3_alg = {
Herbert Xua9e62fa2006-08-21 21:39:24 +1000313 .cra_name = "ecb(des3_ede)",
314 .cra_driver_name = "ecb-des3_ede-s390",
Martin Schwidefskyc7d4d252016-03-17 15:22:12 +0100315 .cra_priority = 400, /* combo: des3 + ecb */
Herbert Xua9e62fa2006-08-21 21:39:24 +1000316 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
Jan Glauber1efbd152010-05-21 22:04:46 +1000317 .cra_blocksize = DES_BLOCK_SIZE,
Jan Glauber98971f82011-04-19 21:29:15 +0200318 .cra_ctxsize = sizeof(struct s390_des_ctx),
Herbert Xua9e62fa2006-08-21 21:39:24 +1000319 .cra_type = &crypto_blkcipher_type,
320 .cra_module = THIS_MODULE,
Herbert Xua9e62fa2006-08-21 21:39:24 +1000321 .cra_u = {
322 .blkcipher = {
Jan Glauber98971f82011-04-19 21:29:15 +0200323 .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 Xua9e62fa2006-08-21 21:39:24 +1000328 }
329 }
330};
331
Jan Glauber98971f82011-04-19 21:29:15 +0200332static int cbc_des3_encrypt(struct blkcipher_desc *desc,
333 struct scatterlist *dst, struct scatterlist *src,
334 unsigned int nbytes)
Herbert Xua9e62fa2006-08-21 21:39:24 +1000335{
Herbert Xua9e62fa2006-08-21 21:39:24 +1000336 struct blkcipher_walk walk;
337
338 blkcipher_walk_init(&walk, dst, src, nbytes);
Martin Schwidefskyedc63a32016-08-15 09:19:16 +0200339 return cbc_desall_crypt(desc, CPACF_KMC_TDEA_192, &walk);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000340}
341
Jan Glauber98971f82011-04-19 21:29:15 +0200342static int cbc_des3_decrypt(struct blkcipher_desc *desc,
343 struct scatterlist *dst, struct scatterlist *src,
344 unsigned int nbytes)
Herbert Xua9e62fa2006-08-21 21:39:24 +1000345{
Herbert Xua9e62fa2006-08-21 21:39:24 +1000346 struct blkcipher_walk walk;
347
348 blkcipher_walk_init(&walk, dst, src, nbytes);
Martin Schwidefskyedc63a32016-08-15 09:19:16 +0200349 return cbc_desall_crypt(desc, CPACF_KMC_TDEA_192 | CPACF_DECRYPT,
350 &walk);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000351}
352
Jan Glauber98971f82011-04-19 21:29:15 +0200353static struct crypto_alg cbc_des3_alg = {
Herbert Xua9e62fa2006-08-21 21:39:24 +1000354 .cra_name = "cbc(des3_ede)",
355 .cra_driver_name = "cbc-des3_ede-s390",
Martin Schwidefskyc7d4d252016-03-17 15:22:12 +0100356 .cra_priority = 400, /* combo: des3 + cbc */
Herbert Xua9e62fa2006-08-21 21:39:24 +1000357 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
Jan Glauber1efbd152010-05-21 22:04:46 +1000358 .cra_blocksize = DES_BLOCK_SIZE,
Jan Glauber98971f82011-04-19 21:29:15 +0200359 .cra_ctxsize = sizeof(struct s390_des_ctx),
Herbert Xua9e62fa2006-08-21 21:39:24 +1000360 .cra_type = &crypto_blkcipher_type,
361 .cra_module = THIS_MODULE,
Herbert Xua9e62fa2006-08-21 21:39:24 +1000362 .cra_u = {
363 .blkcipher = {
Jan Glauber98971f82011-04-19 21:29:15 +0200364 .min_keysize = DES3_KEY_SIZE,
365 .max_keysize = DES3_KEY_SIZE,
Jan Glauber1efbd152010-05-21 22:04:46 +1000366 .ivsize = DES_BLOCK_SIZE,
Jan Glauber98971f82011-04-19 21:29:15 +0200367 .setkey = des3_setkey,
368 .encrypt = cbc_des3_encrypt,
369 .decrypt = cbc_des3_decrypt,
Herbert Xua9e62fa2006-08-21 21:39:24 +1000370 }
371 }
372};
373
Harald Freudenbergeree97dc72014-01-22 13:01:33 +0100374static 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 Schaefer0200f3e2011-05-04 15:09:44 +1000387static int ctr_desall_crypt(struct blkcipher_desc *desc, long func,
Harald Freudenbergeree97dc72014-01-22 13:01:33 +0100388 struct s390_des_ctx *ctx,
389 struct blkcipher_walk *walk)
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000390{
391 int ret = blkcipher_walk_virt_block(desc, walk, DES_BLOCK_SIZE);
Harald Freudenbergeree97dc72014-01-22 13:01:33 +0100392 unsigned int n, nbytes;
393 u8 buf[DES_BLOCK_SIZE], ctrbuf[DES_BLOCK_SIZE];
394 u8 *out, *in, *ctrptr = ctrbuf;
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000395
Harald Freudenbergeree97dc72014-01-22 13:01:33 +0100396 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 Schaefer0200f3e2011-05-04 15:09:44 +1000403 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 Freudenbergeree97dc72014-01-22 13:01:33 +0100407 if (ctrptr == ctrblk)
408 n = __ctrblk_init(ctrptr, nbytes);
409 else
410 n = DES_BLOCK_SIZE;
Martin Schwidefsky0177db02016-08-15 10:41:52 +0200411 cpacf_kmctr(func, ctx->key, out, in, n, ctrptr);
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000412 if (n > DES_BLOCK_SIZE)
Harald Freudenbergeree97dc72014-01-22 13:01:33 +0100413 memcpy(ctrptr, ctrptr + n - DES_BLOCK_SIZE,
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000414 DES_BLOCK_SIZE);
Harald Freudenbergeree97dc72014-01-22 13:01:33 +0100415 crypto_inc(ctrptr, DES_BLOCK_SIZE);
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000416 out += n;
417 in += n;
418 nbytes -= n;
419 }
420 ret = blkcipher_walk_done(desc, walk, nbytes);
421 }
Harald Freudenbergeree97dc72014-01-22 13:01:33 +0100422 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 Freudenberger3901c112014-05-07 16:51:29 +0200428 } else {
429 if (!nbytes)
430 memcpy(walk->iv, ctrptr, DES_BLOCK_SIZE);
Harald Freudenbergeree97dc72014-01-22 13:01:33 +0100431 }
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000432 /* 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 Schwidefsky0177db02016-08-15 10:41:52 +0200436 cpacf_kmctr(func, ctx->key, buf, in, DES_BLOCK_SIZE, ctrbuf);
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000437 memcpy(out, buf, nbytes);
Harald Freudenbergeree97dc72014-01-22 13:01:33 +0100438 crypto_inc(ctrbuf, DES_BLOCK_SIZE);
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000439 ret = blkcipher_walk_done(desc, walk, 0);
Harald Freudenbergeree97dc72014-01-22 13:01:33 +0100440 memcpy(walk->iv, ctrbuf, DES_BLOCK_SIZE);
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000441 }
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000442 return ret;
443}
444
445static 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 Schwidefskyedc63a32016-08-15 09:19:16 +0200453 return ctr_desall_crypt(desc, CPACF_KMCTR_DEA, ctx, &walk);
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000454}
455
456static 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 Schwidefskyedc63a32016-08-15 09:19:16 +0200464 return ctr_desall_crypt(desc, CPACF_KMCTR_DEA | CPACF_DECRYPT,
465 ctx, &walk);
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000466}
467
468static struct crypto_alg ctr_des_alg = {
469 .cra_name = "ctr(des)",
470 .cra_driver_name = "ctr-des-s390",
Martin Schwidefskyc7d4d252016-03-17 15:22:12 +0100471 .cra_priority = 400, /* combo: des + ctr */
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000472 .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 Schaefer0200f3e2011-05-04 15:09:44 +1000477 .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
489static 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 Schwidefskyedc63a32016-08-15 09:19:16 +0200497 return ctr_desall_crypt(desc, CPACF_KMCTR_TDEA_192, ctx, &walk);
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000498}
499
500static 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 Schwidefskyedc63a32016-08-15 09:19:16 +0200508 return ctr_desall_crypt(desc, CPACF_KMCTR_TDEA_192 | CPACF_DECRYPT,
509 ctx, &walk);
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000510}
511
512static struct crypto_alg ctr_des3_alg = {
513 .cra_name = "ctr(des3_ede)",
514 .cra_driver_name = "ctr-des3_ede-s390",
Martin Schwidefskyc7d4d252016-03-17 15:22:12 +0100515 .cra_priority = 400, /* combo: des3 + ede */
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000516 .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 Schaefer0200f3e2011-05-04 15:09:44 +1000521 .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 Schwidefskyd863d592016-08-18 12:34:34 +0200533static struct crypto_alg *des_s390_algs_ptr[8];
534static int des_s390_algs_num;
535
536static 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
546static 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 Glauber98971f82011-04-19 21:29:15 +0200554static int __init des_s390_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
Jan Glauber80d663a2010-05-21 22:04:08 +1000556 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200558 /* 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 Torvalds1da177e2005-04-16 15:20:36 -0700562
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200563 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 Schaefer0200f3e2011-05-04 15:09:44 +1000589
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200590 if (cpacf_test_func(&kmctr_functions, CPACF_KMCTR_DEA) ||
591 cpacf_test_func(&kmctr_functions, CPACF_KMCTR_TDEA_192)) {
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000592 ctrblk = (u8 *) __get_free_page(GFP_KERNEL);
593 if (!ctrblk) {
594 ret = -ENOMEM;
Martin Schwidefskyd863d592016-08-18 12:34:34 +0200595 goto out_err;
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000596 }
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200597 }
598
599 if (cpacf_test_func(&kmctr_functions, CPACF_KMCTR_DEA)) {
Martin Schwidefskyd863d592016-08-18 12:34:34 +0200600 ret = des_s390_register_alg(&ctr_des_alg);
601 if (ret)
602 goto out_err;
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200603 }
604 if (cpacf_test_func(&kmctr_functions, CPACF_KMCTR_TDEA_192)) {
Martin Schwidefskyd863d592016-08-18 12:34:34 +0200605 ret = des_s390_register_alg(&ctr_des3_alg);
606 if (ret)
607 goto out_err;
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000608 }
Martin Schwidefskyd863d592016-08-18 12:34:34 +0200609
610 return 0;
611out_err:
612 des_s390_exit();
Herbert Xua9e62fa2006-08-21 21:39:24 +1000613 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
615
Hendrik Bruecknerd05377c2015-02-19 17:34:07 +0100616module_cpu_feature_match(MSA, des_s390_init);
Jan Glauber1efbd152010-05-21 22:04:46 +1000617module_exit(des_s390_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Kees Cook5d26a102014-11-20 17:05:53 -0800619MODULE_ALIAS_CRYPTO("des");
620MODULE_ALIAS_CRYPTO("des3_ede");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622MODULE_LICENSE("GPL");
623MODULE_DESCRIPTION("DES & Triple DES EDE Cipher Algorithms");