blob: 0d296662bbf0aba03dbaa79de182114e315b6184 [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>
Matthew Rosatof3d35842016-12-15 14:54:30 +010021#include <linux/fips.h>
Jan Glauber1efbd152010-05-21 22:04:46 +100022#include <crypto/algapi.h>
23#include <crypto/des.h>
Martin Schwidefskyc7d4d252016-03-17 15:22:12 +010024#include <asm/cpacf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Jan Glauber98971f82011-04-19 21:29:15 +020026#define DES3_KEY_SIZE (3 * DES_KEY_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Gerald Schaefer0200f3e2011-05-04 15:09:44 +100028static u8 *ctrblk;
Harald Freudenbergeree97dc72014-01-22 13:01:33 +010029static DEFINE_SPINLOCK(ctrblk_lock);
Gerald Schaefer0200f3e2011-05-04 15:09:44 +100030
Martin Schwidefsky69c0e362016-08-18 12:59:46 +020031static cpacf_mask_t km_functions, kmc_functions, kmctr_functions;
32
Jan Glauber98971f82011-04-19 21:29:15 +020033struct s390_des_ctx {
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 u8 iv[DES_BLOCK_SIZE];
Jan Glauber98971f82011-04-19 21:29:15 +020035 u8 key[DES3_KEY_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -070036};
37
Herbert Xu6c2bb982006-05-16 22:09:29 +100038static int des_setkey(struct crypto_tfm *tfm, const u8 *key,
Jan Glauber98971f82011-04-19 21:29:15 +020039 unsigned int key_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
Jan Glauber98971f82011-04-19 21:29:15 +020041 struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
Jan Glauber1efbd152010-05-21 22:04:46 +100042 u32 tmp[DES_EXPKEY_WORDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Jan Glauber1efbd152010-05-21 22:04:46 +100044 /* check for weak keys */
Martin Schwidefsky69c0e362016-08-18 12:59:46 +020045 if (!des_ekey(tmp, key) &&
46 (tfm->crt_flags & CRYPTO_TFM_REQ_WEAK_KEY)) {
47 tfm->crt_flags |= CRYPTO_TFM_RES_WEAK_KEY;
Jan Glauber1efbd152010-05-21 22:04:46 +100048 return -EINVAL;
49 }
50
Jan Glauber98971f82011-04-19 21:29:15 +020051 memcpy(ctx->key, key, key_len);
Jan Glauber1efbd152010-05-21 22:04:46 +100052 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053}
54
Herbert Xu6c2bb982006-05-16 22:09:29 +100055static void des_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
Jan Glauber98971f82011-04-19 21:29:15 +020057 struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Martin Schwidefskyedc63a32016-08-15 09:19:16 +020059 cpacf_km(CPACF_KM_DEA, ctx->key, out, in, DES_BLOCK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060}
61
Herbert Xu6c2bb982006-05-16 22:09:29 +100062static void des_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
Jan Glauber98971f82011-04-19 21:29:15 +020064 struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Martin Schwidefskyedc63a32016-08-15 09:19:16 +020066 cpacf_km(CPACF_KM_DEA | CPACF_DECRYPT,
67 ctx->key, out, in, DES_BLOCK_SIZE);
Jan Glauberb8dc6032006-01-14 13:20:53 -080068}
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070static struct crypto_alg des_alg = {
71 .cra_name = "des",
Herbert Xu65b75c32006-08-21 21:18:50 +100072 .cra_driver_name = "des-s390",
Martin Schwidefskyc7d4d252016-03-17 15:22:12 +010073 .cra_priority = 300,
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
75 .cra_blocksize = DES_BLOCK_SIZE,
Jan Glauber98971f82011-04-19 21:29:15 +020076 .cra_ctxsize = sizeof(struct s390_des_ctx),
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 .cra_module = THIS_MODULE,
Jan Glauberc1357832006-01-14 13:20:53 -080078 .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 Glauberb8dc6032006-01-14 13:20:53 -080084 .cia_decrypt = des_decrypt,
Jan Glauberc1357832006-01-14 13:20:53 -080085 }
86 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070087};
88
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +020089static int ecb_desall_crypt(struct blkcipher_desc *desc, unsigned long fc,
Harald Freudenbergeradc3fcf2014-01-22 13:00:04 +010090 struct blkcipher_walk *walk)
Herbert Xua9e62fa2006-08-21 21:39:24 +100091{
Harald Freudenbergeradc3fcf2014-01-22 13:00:04 +010092 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +020093 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
107static 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 Freudenbergeradc3fcf2014-01-22 13:00:04 +0100113 struct {
114 u8 iv[DES_BLOCK_SIZE];
115 u8 key[DES3_KEY_SIZE];
116 } param;
Herbert Xua9e62fa2006-08-21 21:39:24 +1000117
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200118 ret = blkcipher_walk_virt(desc, walk);
Harald Freudenbergeradc3fcf2014-01-22 13:00:04 +0100119 memcpy(param.iv, walk->iv, DES_BLOCK_SIZE);
120 memcpy(param.key, ctx->key, DES3_KEY_SIZE);
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200121 while ((nbytes = walk->nbytes) >= DES_BLOCK_SIZE) {
Herbert Xua9e62fa2006-08-21 21:39:24 +1000122 /* only use complete blocks */
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200123 n = nbytes & ~(DES_BLOCK_SIZE - 1);
124 cpacf_kmc(fc, &param, walk->dst.virt.addr,
125 walk->src.virt.addr, n);
126 ret = blkcipher_walk_done(desc, walk, nbytes - n);
127 }
Harald Freudenbergeradc3fcf2014-01-22 13:00:04 +0100128 memcpy(walk->iv, param.iv, DES_BLOCK_SIZE);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000129 return ret;
130}
131
132static int ecb_des_encrypt(struct blkcipher_desc *desc,
133 struct scatterlist *dst, struct scatterlist *src,
134 unsigned int nbytes)
135{
Herbert Xua9e62fa2006-08-21 21:39:24 +1000136 struct blkcipher_walk walk;
137
138 blkcipher_walk_init(&walk, dst, src, nbytes);
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200139 return ecb_desall_crypt(desc, CPACF_KM_DEA, &walk);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000140}
141
142static int ecb_des_decrypt(struct blkcipher_desc *desc,
143 struct scatterlist *dst, struct scatterlist *src,
144 unsigned int nbytes)
145{
Herbert Xua9e62fa2006-08-21 21:39:24 +1000146 struct blkcipher_walk walk;
147
148 blkcipher_walk_init(&walk, dst, src, nbytes);
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200149 return ecb_desall_crypt(desc, CPACF_KM_DEA | CPACF_DECRYPT, &walk);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000150}
151
152static struct crypto_alg ecb_des_alg = {
153 .cra_name = "ecb(des)",
154 .cra_driver_name = "ecb-des-s390",
Martin Schwidefskyc7d4d252016-03-17 15:22:12 +0100155 .cra_priority = 400, /* combo: des + ecb */
Herbert Xua9e62fa2006-08-21 21:39:24 +1000156 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
157 .cra_blocksize = DES_BLOCK_SIZE,
Jan Glauber98971f82011-04-19 21:29:15 +0200158 .cra_ctxsize = sizeof(struct s390_des_ctx),
Herbert Xua9e62fa2006-08-21 21:39:24 +1000159 .cra_type = &crypto_blkcipher_type,
160 .cra_module = THIS_MODULE,
Herbert Xua9e62fa2006-08-21 21:39:24 +1000161 .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
172static int cbc_des_encrypt(struct blkcipher_desc *desc,
173 struct scatterlist *dst, struct scatterlist *src,
174 unsigned int nbytes)
175{
Herbert Xua9e62fa2006-08-21 21:39:24 +1000176 struct blkcipher_walk walk;
177
178 blkcipher_walk_init(&walk, dst, src, nbytes);
Martin Schwidefskyedc63a32016-08-15 09:19:16 +0200179 return cbc_desall_crypt(desc, CPACF_KMC_DEA, &walk);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000180}
181
182static int cbc_des_decrypt(struct blkcipher_desc *desc,
183 struct scatterlist *dst, struct scatterlist *src,
184 unsigned int nbytes)
185{
Herbert Xua9e62fa2006-08-21 21:39:24 +1000186 struct blkcipher_walk walk;
187
188 blkcipher_walk_init(&walk, dst, src, nbytes);
Martin Schwidefskyedc63a32016-08-15 09:19:16 +0200189 return cbc_desall_crypt(desc, CPACF_KMC_DEA | CPACF_DECRYPT, &walk);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000190}
191
192static struct crypto_alg cbc_des_alg = {
193 .cra_name = "cbc(des)",
194 .cra_driver_name = "cbc-des-s390",
Martin Schwidefskyc7d4d252016-03-17 15:22:12 +0100195 .cra_priority = 400, /* combo: des + cbc */
Herbert Xua9e62fa2006-08-21 21:39:24 +1000196 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
197 .cra_blocksize = DES_BLOCK_SIZE,
Jan Glauber98971f82011-04-19 21:29:15 +0200198 .cra_ctxsize = sizeof(struct s390_des_ctx),
Herbert Xua9e62fa2006-08-21 21:39:24 +1000199 .cra_type = &crypto_blkcipher_type,
200 .cra_module = THIS_MODULE,
Herbert Xua9e62fa2006-08-21 21:39:24 +1000201 .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 Torvalds1da177e2005-04-16 15:20:36 -0700213/*
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 Torvalds1da177e2005-04-16 15:20:36 -0700220 * 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 Rosatof3d35842016-12-15 14:54:30 +0100225 * In fips mode additinally check for all 3 keys are unique.
226 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 */
Jan Glauber98971f82011-04-19 21:29:15 +0200228static int des3_setkey(struct crypto_tfm *tfm, const u8 *key,
229 unsigned int key_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
Jan Glauber98971f82011-04-19 21:29:15 +0200231 struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Daniel Borkmannfed28612013-12-11 11:28:59 +0100233 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 Schwidefsky69c0e362016-08-18 12:59:46 +0200236 (tfm->crt_flags & CRYPTO_TFM_REQ_WEAK_KEY)) {
237 tfm->crt_flags |= CRYPTO_TFM_RES_WEAK_KEY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 return -EINVAL;
239 }
Matthew Rosatof3d35842016-12-15 14:54:30 +0100240
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 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{
Herbert Xua9e62fa2006-08-21 21:39:24 +1000293 struct blkcipher_walk walk;
294
295 blkcipher_walk_init(&walk, dst, src, nbytes);
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200296 return ecb_desall_crypt(desc, CPACF_KM_TDEA_192, &walk);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000297}
298
Jan Glauber98971f82011-04-19 21:29:15 +0200299static int ecb_des3_decrypt(struct blkcipher_desc *desc,
300 struct scatterlist *dst, struct scatterlist *src,
301 unsigned int nbytes)
Herbert Xua9e62fa2006-08-21 21:39:24 +1000302{
Herbert Xua9e62fa2006-08-21 21:39:24 +1000303 struct blkcipher_walk walk;
304
305 blkcipher_walk_init(&walk, dst, src, nbytes);
Martin Schwidefskyedc63a32016-08-15 09:19:16 +0200306 return ecb_desall_crypt(desc, CPACF_KM_TDEA_192 | CPACF_DECRYPT,
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200307 &walk);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000308}
309
Jan Glauber98971f82011-04-19 21:29:15 +0200310static struct crypto_alg ecb_des3_alg = {
Herbert Xua9e62fa2006-08-21 21:39:24 +1000311 .cra_name = "ecb(des3_ede)",
312 .cra_driver_name = "ecb-des3_ede-s390",
Martin Schwidefskyc7d4d252016-03-17 15:22:12 +0100313 .cra_priority = 400, /* combo: des3 + ecb */
Herbert Xua9e62fa2006-08-21 21:39:24 +1000314 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
Jan Glauber1efbd152010-05-21 22:04:46 +1000315 .cra_blocksize = DES_BLOCK_SIZE,
Jan Glauber98971f82011-04-19 21:29:15 +0200316 .cra_ctxsize = sizeof(struct s390_des_ctx),
Herbert Xua9e62fa2006-08-21 21:39:24 +1000317 .cra_type = &crypto_blkcipher_type,
318 .cra_module = THIS_MODULE,
Herbert Xua9e62fa2006-08-21 21:39:24 +1000319 .cra_u = {
320 .blkcipher = {
Jan Glauber98971f82011-04-19 21:29:15 +0200321 .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 Xua9e62fa2006-08-21 21:39:24 +1000326 }
327 }
328};
329
Jan Glauber98971f82011-04-19 21:29:15 +0200330static int cbc_des3_encrypt(struct blkcipher_desc *desc,
331 struct scatterlist *dst, struct scatterlist *src,
332 unsigned int nbytes)
Herbert Xua9e62fa2006-08-21 21:39:24 +1000333{
Herbert Xua9e62fa2006-08-21 21:39:24 +1000334 struct blkcipher_walk walk;
335
336 blkcipher_walk_init(&walk, dst, src, nbytes);
Martin Schwidefskyedc63a32016-08-15 09:19:16 +0200337 return cbc_desall_crypt(desc, CPACF_KMC_TDEA_192, &walk);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000338}
339
Jan Glauber98971f82011-04-19 21:29:15 +0200340static int cbc_des3_decrypt(struct blkcipher_desc *desc,
341 struct scatterlist *dst, struct scatterlist *src,
342 unsigned int nbytes)
Herbert Xua9e62fa2006-08-21 21:39:24 +1000343{
Herbert Xua9e62fa2006-08-21 21:39:24 +1000344 struct blkcipher_walk walk;
345
346 blkcipher_walk_init(&walk, dst, src, nbytes);
Martin Schwidefskyedc63a32016-08-15 09:19:16 +0200347 return cbc_desall_crypt(desc, CPACF_KMC_TDEA_192 | CPACF_DECRYPT,
348 &walk);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000349}
350
Jan Glauber98971f82011-04-19 21:29:15 +0200351static struct crypto_alg cbc_des3_alg = {
Herbert Xua9e62fa2006-08-21 21:39:24 +1000352 .cra_name = "cbc(des3_ede)",
353 .cra_driver_name = "cbc-des3_ede-s390",
Martin Schwidefskyc7d4d252016-03-17 15:22:12 +0100354 .cra_priority = 400, /* combo: des3 + cbc */
Herbert Xua9e62fa2006-08-21 21:39:24 +1000355 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
Jan Glauber1efbd152010-05-21 22:04:46 +1000356 .cra_blocksize = DES_BLOCK_SIZE,
Jan Glauber98971f82011-04-19 21:29:15 +0200357 .cra_ctxsize = sizeof(struct s390_des_ctx),
Herbert Xua9e62fa2006-08-21 21:39:24 +1000358 .cra_type = &crypto_blkcipher_type,
359 .cra_module = THIS_MODULE,
Herbert Xua9e62fa2006-08-21 21:39:24 +1000360 .cra_u = {
361 .blkcipher = {
Jan Glauber98971f82011-04-19 21:29:15 +0200362 .min_keysize = DES3_KEY_SIZE,
363 .max_keysize = DES3_KEY_SIZE,
Jan Glauber1efbd152010-05-21 22:04:46 +1000364 .ivsize = DES_BLOCK_SIZE,
Jan Glauber98971f82011-04-19 21:29:15 +0200365 .setkey = des3_setkey,
366 .encrypt = cbc_des3_encrypt,
367 .decrypt = cbc_des3_decrypt,
Herbert Xua9e62fa2006-08-21 21:39:24 +1000368 }
369 }
370};
371
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200372static unsigned int __ctrblk_init(u8 *ctrptr, u8 *iv, unsigned int nbytes)
Harald Freudenbergeree97dc72014-01-22 13:01:33 +0100373{
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 Schwidefsky7bac4f52016-08-15 15:17:52 +0200378 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 Freudenbergeree97dc72014-01-22 13:01:33 +0100383 }
384 return n;
385}
386
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200387static int ctr_desall_crypt(struct blkcipher_desc *desc, unsigned long fc,
Harald Freudenbergeree97dc72014-01-22 13:01:33 +0100388 struct blkcipher_walk *walk)
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000389{
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200390 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
391 u8 buf[DES_BLOCK_SIZE], *ctrptr;
Harald Freudenbergeree97dc72014-01-22 13:01:33 +0100392 unsigned int n, nbytes;
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200393 int ret, locked;
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000394
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200395 locked = spin_trylock(&ctrblk_lock);
Harald Freudenbergeree97dc72014-01-22 13:01:33 +0100396
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200397 ret = blkcipher_walk_virt_block(desc, walk, DES_BLOCK_SIZE);
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000398 while ((nbytes = walk->nbytes) >= DES_BLOCK_SIZE) {
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200399 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 Schaefer0200f3e2011-05-04 15:09:44 +1000410 }
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200411 if (locked)
Harald Freudenbergeree97dc72014-01-22 13:01:33 +0100412 spin_unlock(&ctrblk_lock);
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000413 /* final block may be < DES_BLOCK_SIZE, copy only nbytes */
414 if (nbytes) {
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200415 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 Schaefer0200f3e2011-05-04 15:09:44 +1000419 ret = blkcipher_walk_done(desc, walk, 0);
420 }
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000421 return ret;
422}
423
424static int ctr_des_encrypt(struct blkcipher_desc *desc,
425 struct scatterlist *dst, struct scatterlist *src,
426 unsigned int nbytes)
427{
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000428 struct blkcipher_walk walk;
429
430 blkcipher_walk_init(&walk, dst, src, nbytes);
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200431 return ctr_desall_crypt(desc, CPACF_KMCTR_DEA, &walk);
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000432}
433
434static int ctr_des_decrypt(struct blkcipher_desc *desc,
435 struct scatterlist *dst, struct scatterlist *src,
436 unsigned int nbytes)
437{
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000438 struct blkcipher_walk walk;
439
440 blkcipher_walk_init(&walk, dst, src, nbytes);
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200441 return ctr_desall_crypt(desc, CPACF_KMCTR_DEA | CPACF_DECRYPT, &walk);
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000442}
443
444static struct crypto_alg ctr_des_alg = {
445 .cra_name = "ctr(des)",
446 .cra_driver_name = "ctr-des-s390",
Martin Schwidefskyc7d4d252016-03-17 15:22:12 +0100447 .cra_priority = 400, /* combo: des + ctr */
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000448 .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 Schaefer0200f3e2011-05-04 15:09:44 +1000453 .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
465static int ctr_des3_encrypt(struct blkcipher_desc *desc,
466 struct scatterlist *dst, struct scatterlist *src,
467 unsigned int nbytes)
468{
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000469 struct blkcipher_walk walk;
470
471 blkcipher_walk_init(&walk, dst, src, nbytes);
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200472 return ctr_desall_crypt(desc, CPACF_KMCTR_TDEA_192, &walk);
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000473}
474
475static int ctr_des3_decrypt(struct blkcipher_desc *desc,
476 struct scatterlist *dst, struct scatterlist *src,
477 unsigned int nbytes)
478{
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000479 struct blkcipher_walk walk;
480
481 blkcipher_walk_init(&walk, dst, src, nbytes);
Martin Schwidefskyedc63a32016-08-15 09:19:16 +0200482 return ctr_desall_crypt(desc, CPACF_KMCTR_TDEA_192 | CPACF_DECRYPT,
Martin Schwidefsky7bac4f52016-08-15 15:17:52 +0200483 &walk);
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000484}
485
486static struct crypto_alg ctr_des3_alg = {
487 .cra_name = "ctr(des3_ede)",
488 .cra_driver_name = "ctr-des3_ede-s390",
Martin Schwidefskyc7d4d252016-03-17 15:22:12 +0100489 .cra_priority = 400, /* combo: des3 + ede */
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000490 .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 Schaefer0200f3e2011-05-04 15:09:44 +1000495 .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 Schwidefskyd863d592016-08-18 12:34:34 +0200507static struct crypto_alg *des_s390_algs_ptr[8];
508static int des_s390_algs_num;
509
510static 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
520static 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 Glauber98971f82011-04-19 21:29:15 +0200528static int __init des_s390_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
Jan Glauber80d663a2010-05-21 22:04:08 +1000530 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200532 /* 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 Torvalds1da177e2005-04-16 15:20:36 -0700536
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200537 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 Schaefer0200f3e2011-05-04 15:09:44 +1000563
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200564 if (cpacf_test_func(&kmctr_functions, CPACF_KMCTR_DEA) ||
565 cpacf_test_func(&kmctr_functions, CPACF_KMCTR_TDEA_192)) {
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000566 ctrblk = (u8 *) __get_free_page(GFP_KERNEL);
567 if (!ctrblk) {
568 ret = -ENOMEM;
Martin Schwidefskyd863d592016-08-18 12:34:34 +0200569 goto out_err;
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000570 }
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200571 }
572
573 if (cpacf_test_func(&kmctr_functions, CPACF_KMCTR_DEA)) {
Martin Schwidefskyd863d592016-08-18 12:34:34 +0200574 ret = des_s390_register_alg(&ctr_des_alg);
575 if (ret)
576 goto out_err;
Martin Schwidefsky69c0e362016-08-18 12:59:46 +0200577 }
578 if (cpacf_test_func(&kmctr_functions, CPACF_KMCTR_TDEA_192)) {
Martin Schwidefskyd863d592016-08-18 12:34:34 +0200579 ret = des_s390_register_alg(&ctr_des3_alg);
580 if (ret)
581 goto out_err;
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000582 }
Martin Schwidefskyd863d592016-08-18 12:34:34 +0200583
584 return 0;
585out_err:
586 des_s390_exit();
Herbert Xua9e62fa2006-08-21 21:39:24 +1000587 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588}
589
Hendrik Bruecknerd05377c2015-02-19 17:34:07 +0100590module_cpu_feature_match(MSA, des_s390_init);
Jan Glauber1efbd152010-05-21 22:04:46 +1000591module_exit(des_s390_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Kees Cook5d26a102014-11-20 17:05:53 -0800593MODULE_ALIAS_CRYPTO("des");
594MODULE_ALIAS_CRYPTO("des3_ede");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596MODULE_LICENSE("GPL");
597MODULE_DESCRIPTION("DES & Triple DES EDE Cipher Algorithms");