blob: bcca01c9989daf57b963db9168d64ec09c7ee5af [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>
Jan Glauber1efbd152010-05-21 22:04:46 +100019#include <linux/crypto.h>
20#include <crypto/algapi.h>
21#include <crypto/des.h>
Jan Glauberc1357832006-01-14 13:20:53 -080022
Jan Glauberc1e26e12006-01-06 00:19:17 -080023#include "crypt_s390.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;
28
Jan Glauber98971f82011-04-19 21:29:15 +020029struct s390_des_ctx {
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 u8 iv[DES_BLOCK_SIZE];
Jan Glauber98971f82011-04-19 21:29:15 +020031 u8 key[DES3_KEY_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -070032};
33
Herbert Xu6c2bb982006-05-16 22:09:29 +100034static int des_setkey(struct crypto_tfm *tfm, const u8 *key,
Jan Glauber98971f82011-04-19 21:29:15 +020035 unsigned int key_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
Jan Glauber98971f82011-04-19 21:29:15 +020037 struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
Herbert Xu560c06a2006-08-13 14:16:39 +100038 u32 *flags = &tfm->crt_flags;
Jan Glauber1efbd152010-05-21 22:04:46 +100039 u32 tmp[DES_EXPKEY_WORDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Jan Glauber1efbd152010-05-21 22:04:46 +100041 /* check for weak keys */
42 if (!des_ekey(tmp, key) && (*flags & CRYPTO_TFM_REQ_WEAK_KEY)) {
43 *flags |= CRYPTO_TFM_RES_WEAK_KEY;
44 return -EINVAL;
45 }
46
Jan Glauber98971f82011-04-19 21:29:15 +020047 memcpy(ctx->key, key, key_len);
Jan Glauber1efbd152010-05-21 22:04:46 +100048 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049}
50
Herbert Xu6c2bb982006-05-16 22:09:29 +100051static void des_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Jan Glauber98971f82011-04-19 21:29:15 +020053 struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Jan Glauber98971f82011-04-19 21:29:15 +020055 crypt_s390_km(KM_DEA_ENCRYPT, ctx->key, out, in, DES_BLOCK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056}
57
Herbert Xu6c2bb982006-05-16 22:09:29 +100058static void des_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
Jan Glauber98971f82011-04-19 21:29:15 +020060 struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Jan Glauber98971f82011-04-19 21:29:15 +020062 crypt_s390_km(KM_DEA_DECRYPT, ctx->key, out, in, DES_BLOCK_SIZE);
Jan Glauberb8dc6032006-01-14 13:20:53 -080063}
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065static struct crypto_alg des_alg = {
66 .cra_name = "des",
Herbert Xu65b75c32006-08-21 21:18:50 +100067 .cra_driver_name = "des-s390",
68 .cra_priority = CRYPT_S390_PRIORITY,
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
70 .cra_blocksize = DES_BLOCK_SIZE,
Jan Glauber98971f82011-04-19 21:29:15 +020071 .cra_ctxsize = sizeof(struct s390_des_ctx),
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 .cra_module = THIS_MODULE,
Jan Glauberc1357832006-01-14 13:20:53 -080073 .cra_u = {
74 .cipher = {
75 .cia_min_keysize = DES_KEY_SIZE,
76 .cia_max_keysize = DES_KEY_SIZE,
77 .cia_setkey = des_setkey,
78 .cia_encrypt = des_encrypt,
Jan Glauberb8dc6032006-01-14 13:20:53 -080079 .cia_decrypt = des_decrypt,
Jan Glauberc1357832006-01-14 13:20:53 -080080 }
81 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070082};
83
Herbert Xua9e62fa2006-08-21 21:39:24 +100084static int ecb_desall_crypt(struct blkcipher_desc *desc, long func,
Jan Glauber98971f82011-04-19 21:29:15 +020085 u8 *key, struct blkcipher_walk *walk)
Herbert Xua9e62fa2006-08-21 21:39:24 +100086{
87 int ret = blkcipher_walk_virt(desc, walk);
88 unsigned int nbytes;
89
90 while ((nbytes = walk->nbytes)) {
91 /* only use complete blocks */
92 unsigned int n = nbytes & ~(DES_BLOCK_SIZE - 1);
93 u8 *out = walk->dst.virt.addr;
94 u8 *in = walk->src.virt.addr;
95
Jan Glauber98971f82011-04-19 21:29:15 +020096 ret = crypt_s390_km(func, key, out, in, n);
Jan Glauber36eb2ca2012-10-26 15:06:12 +020097 if (ret < 0 || ret != n)
98 return -EIO;
Herbert Xua9e62fa2006-08-21 21:39:24 +100099
100 nbytes &= DES_BLOCK_SIZE - 1;
101 ret = blkcipher_walk_done(desc, walk, nbytes);
102 }
103
104 return ret;
105}
106
107static int cbc_desall_crypt(struct blkcipher_desc *desc, long func,
Jan Glauber98971f82011-04-19 21:29:15 +0200108 u8 *iv, struct blkcipher_walk *walk)
Herbert Xua9e62fa2006-08-21 21:39:24 +1000109{
110 int ret = blkcipher_walk_virt(desc, walk);
111 unsigned int nbytes = walk->nbytes;
112
113 if (!nbytes)
114 goto out;
115
Jan Glauber98971f82011-04-19 21:29:15 +0200116 memcpy(iv, walk->iv, DES_BLOCK_SIZE);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000117 do {
118 /* only use complete blocks */
119 unsigned int n = nbytes & ~(DES_BLOCK_SIZE - 1);
120 u8 *out = walk->dst.virt.addr;
121 u8 *in = walk->src.virt.addr;
122
Jan Glauber98971f82011-04-19 21:29:15 +0200123 ret = crypt_s390_kmc(func, iv, out, in, n);
Jan Glauber36eb2ca2012-10-26 15:06:12 +0200124 if (ret < 0 || ret != n)
125 return -EIO;
Herbert Xua9e62fa2006-08-21 21:39:24 +1000126
127 nbytes &= DES_BLOCK_SIZE - 1;
128 ret = blkcipher_walk_done(desc, walk, nbytes);
129 } while ((nbytes = walk->nbytes));
Jan Glauber98971f82011-04-19 21:29:15 +0200130 memcpy(walk->iv, iv, DES_BLOCK_SIZE);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000131
132out:
133 return ret;
134}
135
136static int ecb_des_encrypt(struct blkcipher_desc *desc,
137 struct scatterlist *dst, struct scatterlist *src,
138 unsigned int nbytes)
139{
Jan Glauber98971f82011-04-19 21:29:15 +0200140 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000141 struct blkcipher_walk walk;
142
143 blkcipher_walk_init(&walk, dst, src, nbytes);
Jan Glauber98971f82011-04-19 21:29:15 +0200144 return ecb_desall_crypt(desc, KM_DEA_ENCRYPT, ctx->key, &walk);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000145}
146
147static int ecb_des_decrypt(struct blkcipher_desc *desc,
148 struct scatterlist *dst, struct scatterlist *src,
149 unsigned int nbytes)
150{
Jan Glauber98971f82011-04-19 21:29:15 +0200151 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000152 struct blkcipher_walk walk;
153
154 blkcipher_walk_init(&walk, dst, src, nbytes);
Jan Glauber98971f82011-04-19 21:29:15 +0200155 return ecb_desall_crypt(desc, KM_DEA_DECRYPT, ctx->key, &walk);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000156}
157
158static struct crypto_alg ecb_des_alg = {
159 .cra_name = "ecb(des)",
160 .cra_driver_name = "ecb-des-s390",
161 .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
162 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
163 .cra_blocksize = DES_BLOCK_SIZE,
Jan Glauber98971f82011-04-19 21:29:15 +0200164 .cra_ctxsize = sizeof(struct s390_des_ctx),
Herbert Xua9e62fa2006-08-21 21:39:24 +1000165 .cra_type = &crypto_blkcipher_type,
166 .cra_module = THIS_MODULE,
Herbert Xua9e62fa2006-08-21 21:39:24 +1000167 .cra_u = {
168 .blkcipher = {
169 .min_keysize = DES_KEY_SIZE,
170 .max_keysize = DES_KEY_SIZE,
171 .setkey = des_setkey,
172 .encrypt = ecb_des_encrypt,
173 .decrypt = ecb_des_decrypt,
174 }
175 }
176};
177
178static int cbc_des_encrypt(struct blkcipher_desc *desc,
179 struct scatterlist *dst, struct scatterlist *src,
180 unsigned int nbytes)
181{
Jan Glauber98971f82011-04-19 21:29:15 +0200182 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000183 struct blkcipher_walk walk;
184
185 blkcipher_walk_init(&walk, dst, src, nbytes);
Jan Glauber98971f82011-04-19 21:29:15 +0200186 return cbc_desall_crypt(desc, KMC_DEA_ENCRYPT, ctx->iv, &walk);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000187}
188
189static int cbc_des_decrypt(struct blkcipher_desc *desc,
190 struct scatterlist *dst, struct scatterlist *src,
191 unsigned int nbytes)
192{
Jan Glauber98971f82011-04-19 21:29:15 +0200193 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000194 struct blkcipher_walk walk;
195
196 blkcipher_walk_init(&walk, dst, src, nbytes);
Jan Glauber98971f82011-04-19 21:29:15 +0200197 return cbc_desall_crypt(desc, KMC_DEA_DECRYPT, ctx->iv, &walk);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000198}
199
200static struct crypto_alg cbc_des_alg = {
201 .cra_name = "cbc(des)",
202 .cra_driver_name = "cbc-des-s390",
203 .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
204 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
205 .cra_blocksize = DES_BLOCK_SIZE,
Jan Glauber98971f82011-04-19 21:29:15 +0200206 .cra_ctxsize = sizeof(struct s390_des_ctx),
Herbert Xua9e62fa2006-08-21 21:39:24 +1000207 .cra_type = &crypto_blkcipher_type,
208 .cra_module = THIS_MODULE,
Herbert Xua9e62fa2006-08-21 21:39:24 +1000209 .cra_u = {
210 .blkcipher = {
211 .min_keysize = DES_KEY_SIZE,
212 .max_keysize = DES_KEY_SIZE,
213 .ivsize = DES_BLOCK_SIZE,
214 .setkey = des_setkey,
215 .encrypt = cbc_des_encrypt,
216 .decrypt = cbc_des_decrypt,
217 }
218 }
219};
220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221/*
222 * RFC2451:
223 *
224 * For DES-EDE3, there is no known need to reject weak or
225 * complementation keys. Any weakness is obviated by the use of
226 * multiple keys.
227 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 * However, if the first two or last two independent 64-bit keys are
229 * equal (k1 == k2 or k2 == k3), then the DES3 operation is simply the
230 * same as DES. Implementers MUST reject keys that exhibit this
231 * property.
232 *
233 */
Jan Glauber98971f82011-04-19 21:29:15 +0200234static int des3_setkey(struct crypto_tfm *tfm, const u8 *key,
235 unsigned int key_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
Jan Glauber98971f82011-04-19 21:29:15 +0200237 struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
Herbert Xu560c06a2006-08-13 14:16:39 +1000238 u32 *flags = &tfm->crt_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 if (!(memcmp(key, &key[DES_KEY_SIZE], DES_KEY_SIZE) &&
241 memcmp(&key[DES_KEY_SIZE], &key[DES_KEY_SIZE * 2],
Jarod Wilson03b56ce2009-06-18 19:52:59 +0800242 DES_KEY_SIZE)) &&
243 (*flags & CRYPTO_TFM_REQ_WEAK_KEY)) {
244 *flags |= CRYPTO_TFM_RES_WEAK_KEY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 return -EINVAL;
246 }
Jan Glauber98971f82011-04-19 21:29:15 +0200247 memcpy(ctx->key, key, key_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 return 0;
249}
250
Jan Glauber98971f82011-04-19 21:29:15 +0200251static void des3_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
Jan Glauber98971f82011-04-19 21:29:15 +0200253 struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Jan Glauber98971f82011-04-19 21:29:15 +0200255 crypt_s390_km(KM_TDEA_192_ENCRYPT, ctx->key, dst, src, DES_BLOCK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
257
Jan Glauber98971f82011-04-19 21:29:15 +0200258static void des3_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
Jan Glauber98971f82011-04-19 21:29:15 +0200260 struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Jan Glauber98971f82011-04-19 21:29:15 +0200262 crypt_s390_km(KM_TDEA_192_DECRYPT, ctx->key, dst, src, DES_BLOCK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263}
264
Jan Glauber98971f82011-04-19 21:29:15 +0200265static struct crypto_alg des3_alg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 .cra_name = "des3_ede",
Herbert Xu65b75c32006-08-21 21:18:50 +1000267 .cra_driver_name = "des3_ede-s390",
268 .cra_priority = CRYPT_S390_PRIORITY,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
Jan Glauber1efbd152010-05-21 22:04:46 +1000270 .cra_blocksize = DES_BLOCK_SIZE,
Jan Glauber98971f82011-04-19 21:29:15 +0200271 .cra_ctxsize = sizeof(struct s390_des_ctx),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 .cra_module = THIS_MODULE,
Jan Glauberc1357832006-01-14 13:20:53 -0800273 .cra_u = {
274 .cipher = {
Jan Glauber98971f82011-04-19 21:29:15 +0200275 .cia_min_keysize = DES3_KEY_SIZE,
276 .cia_max_keysize = DES3_KEY_SIZE,
277 .cia_setkey = des3_setkey,
278 .cia_encrypt = des3_encrypt,
279 .cia_decrypt = des3_decrypt,
Jan Glauberc1357832006-01-14 13:20:53 -0800280 }
281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282};
283
Jan Glauber98971f82011-04-19 21:29:15 +0200284static int ecb_des3_encrypt(struct blkcipher_desc *desc,
285 struct scatterlist *dst, struct scatterlist *src,
286 unsigned int nbytes)
Herbert Xua9e62fa2006-08-21 21:39:24 +1000287{
Jan Glauber98971f82011-04-19 21:29:15 +0200288 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000289 struct blkcipher_walk walk;
290
291 blkcipher_walk_init(&walk, dst, src, nbytes);
Jan Glauber98971f82011-04-19 21:29:15 +0200292 return ecb_desall_crypt(desc, KM_TDEA_192_ENCRYPT, ctx->key, &walk);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000293}
294
Jan Glauber98971f82011-04-19 21:29:15 +0200295static int ecb_des3_decrypt(struct blkcipher_desc *desc,
296 struct scatterlist *dst, struct scatterlist *src,
297 unsigned int nbytes)
Herbert Xua9e62fa2006-08-21 21:39:24 +1000298{
Jan Glauber98971f82011-04-19 21:29:15 +0200299 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000300 struct blkcipher_walk walk;
301
302 blkcipher_walk_init(&walk, dst, src, nbytes);
Jan Glauber98971f82011-04-19 21:29:15 +0200303 return ecb_desall_crypt(desc, KM_TDEA_192_DECRYPT, ctx->key, &walk);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000304}
305
Jan Glauber98971f82011-04-19 21:29:15 +0200306static struct crypto_alg ecb_des3_alg = {
Herbert Xua9e62fa2006-08-21 21:39:24 +1000307 .cra_name = "ecb(des3_ede)",
308 .cra_driver_name = "ecb-des3_ede-s390",
309 .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
310 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
Jan Glauber1efbd152010-05-21 22:04:46 +1000311 .cra_blocksize = DES_BLOCK_SIZE,
Jan Glauber98971f82011-04-19 21:29:15 +0200312 .cra_ctxsize = sizeof(struct s390_des_ctx),
Herbert Xua9e62fa2006-08-21 21:39:24 +1000313 .cra_type = &crypto_blkcipher_type,
314 .cra_module = THIS_MODULE,
Herbert Xua9e62fa2006-08-21 21:39:24 +1000315 .cra_u = {
316 .blkcipher = {
Jan Glauber98971f82011-04-19 21:29:15 +0200317 .min_keysize = DES3_KEY_SIZE,
318 .max_keysize = DES3_KEY_SIZE,
319 .setkey = des3_setkey,
320 .encrypt = ecb_des3_encrypt,
321 .decrypt = ecb_des3_decrypt,
Herbert Xua9e62fa2006-08-21 21:39:24 +1000322 }
323 }
324};
325
Jan Glauber98971f82011-04-19 21:29:15 +0200326static int cbc_des3_encrypt(struct blkcipher_desc *desc,
327 struct scatterlist *dst, struct scatterlist *src,
328 unsigned int nbytes)
Herbert Xua9e62fa2006-08-21 21:39:24 +1000329{
Jan Glauber98971f82011-04-19 21:29:15 +0200330 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000331 struct blkcipher_walk walk;
332
333 blkcipher_walk_init(&walk, dst, src, nbytes);
Jan Glauber98971f82011-04-19 21:29:15 +0200334 return cbc_desall_crypt(desc, KMC_TDEA_192_ENCRYPT, ctx->iv, &walk);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000335}
336
Jan Glauber98971f82011-04-19 21:29:15 +0200337static int cbc_des3_decrypt(struct blkcipher_desc *desc,
338 struct scatterlist *dst, struct scatterlist *src,
339 unsigned int nbytes)
Herbert Xua9e62fa2006-08-21 21:39:24 +1000340{
Jan Glauber98971f82011-04-19 21:29:15 +0200341 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000342 struct blkcipher_walk walk;
343
344 blkcipher_walk_init(&walk, dst, src, nbytes);
Jan Glauber98971f82011-04-19 21:29:15 +0200345 return cbc_desall_crypt(desc, KMC_TDEA_192_DECRYPT, ctx->iv, &walk);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000346}
347
Jan Glauber98971f82011-04-19 21:29:15 +0200348static struct crypto_alg cbc_des3_alg = {
Herbert Xua9e62fa2006-08-21 21:39:24 +1000349 .cra_name = "cbc(des3_ede)",
350 .cra_driver_name = "cbc-des3_ede-s390",
351 .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
352 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
Jan Glauber1efbd152010-05-21 22:04:46 +1000353 .cra_blocksize = DES_BLOCK_SIZE,
Jan Glauber98971f82011-04-19 21:29:15 +0200354 .cra_ctxsize = sizeof(struct s390_des_ctx),
Herbert Xua9e62fa2006-08-21 21:39:24 +1000355 .cra_type = &crypto_blkcipher_type,
356 .cra_module = THIS_MODULE,
Herbert Xua9e62fa2006-08-21 21:39:24 +1000357 .cra_u = {
358 .blkcipher = {
Jan Glauber98971f82011-04-19 21:29:15 +0200359 .min_keysize = DES3_KEY_SIZE,
360 .max_keysize = DES3_KEY_SIZE,
Jan Glauber1efbd152010-05-21 22:04:46 +1000361 .ivsize = DES_BLOCK_SIZE,
Jan Glauber98971f82011-04-19 21:29:15 +0200362 .setkey = des3_setkey,
363 .encrypt = cbc_des3_encrypt,
364 .decrypt = cbc_des3_decrypt,
Herbert Xua9e62fa2006-08-21 21:39:24 +1000365 }
366 }
367};
368
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000369static int ctr_desall_crypt(struct blkcipher_desc *desc, long func,
370 struct s390_des_ctx *ctx, struct blkcipher_walk *walk)
371{
372 int ret = blkcipher_walk_virt_block(desc, walk, DES_BLOCK_SIZE);
373 unsigned int i, n, nbytes;
374 u8 buf[DES_BLOCK_SIZE];
375 u8 *out, *in;
376
377 memcpy(ctrblk, walk->iv, DES_BLOCK_SIZE);
378 while ((nbytes = walk->nbytes) >= DES_BLOCK_SIZE) {
379 out = walk->dst.virt.addr;
380 in = walk->src.virt.addr;
381 while (nbytes >= DES_BLOCK_SIZE) {
382 /* align to block size, max. PAGE_SIZE */
383 n = (nbytes > PAGE_SIZE) ? PAGE_SIZE :
384 nbytes & ~(DES_BLOCK_SIZE - 1);
385 for (i = DES_BLOCK_SIZE; i < n; i += DES_BLOCK_SIZE) {
386 memcpy(ctrblk + i, ctrblk + i - DES_BLOCK_SIZE,
387 DES_BLOCK_SIZE);
388 crypto_inc(ctrblk + i, DES_BLOCK_SIZE);
389 }
390 ret = crypt_s390_kmctr(func, ctx->key, out, in, n, ctrblk);
Jan Glauber36eb2ca2012-10-26 15:06:12 +0200391 if (ret < 0 || ret != n)
392 return -EIO;
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000393 if (n > DES_BLOCK_SIZE)
394 memcpy(ctrblk, ctrblk + n - DES_BLOCK_SIZE,
395 DES_BLOCK_SIZE);
396 crypto_inc(ctrblk, DES_BLOCK_SIZE);
397 out += n;
398 in += n;
399 nbytes -= n;
400 }
401 ret = blkcipher_walk_done(desc, walk, nbytes);
402 }
403
404 /* final block may be < DES_BLOCK_SIZE, copy only nbytes */
405 if (nbytes) {
406 out = walk->dst.virt.addr;
407 in = walk->src.virt.addr;
408 ret = crypt_s390_kmctr(func, ctx->key, buf, in,
409 DES_BLOCK_SIZE, ctrblk);
Jan Glauber36eb2ca2012-10-26 15:06:12 +0200410 if (ret < 0 || ret != DES_BLOCK_SIZE)
411 return -EIO;
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000412 memcpy(out, buf, nbytes);
413 crypto_inc(ctrblk, DES_BLOCK_SIZE);
414 ret = blkcipher_walk_done(desc, walk, 0);
415 }
416 memcpy(walk->iv, ctrblk, DES_BLOCK_SIZE);
417 return ret;
418}
419
420static int ctr_des_encrypt(struct blkcipher_desc *desc,
421 struct scatterlist *dst, struct scatterlist *src,
422 unsigned int nbytes)
423{
424 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
425 struct blkcipher_walk walk;
426
427 blkcipher_walk_init(&walk, dst, src, nbytes);
428 return ctr_desall_crypt(desc, KMCTR_DEA_ENCRYPT, ctx, &walk);
429}
430
431static int ctr_des_decrypt(struct blkcipher_desc *desc,
432 struct scatterlist *dst, struct scatterlist *src,
433 unsigned int nbytes)
434{
435 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
436 struct blkcipher_walk walk;
437
438 blkcipher_walk_init(&walk, dst, src, nbytes);
439 return ctr_desall_crypt(desc, KMCTR_DEA_DECRYPT, ctx, &walk);
440}
441
442static struct crypto_alg ctr_des_alg = {
443 .cra_name = "ctr(des)",
444 .cra_driver_name = "ctr-des-s390",
445 .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
446 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
447 .cra_blocksize = 1,
448 .cra_ctxsize = sizeof(struct s390_des_ctx),
449 .cra_type = &crypto_blkcipher_type,
450 .cra_module = THIS_MODULE,
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000451 .cra_u = {
452 .blkcipher = {
453 .min_keysize = DES_KEY_SIZE,
454 .max_keysize = DES_KEY_SIZE,
455 .ivsize = DES_BLOCK_SIZE,
456 .setkey = des_setkey,
457 .encrypt = ctr_des_encrypt,
458 .decrypt = ctr_des_decrypt,
459 }
460 }
461};
462
463static int ctr_des3_encrypt(struct blkcipher_desc *desc,
464 struct scatterlist *dst, struct scatterlist *src,
465 unsigned int nbytes)
466{
467 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
468 struct blkcipher_walk walk;
469
470 blkcipher_walk_init(&walk, dst, src, nbytes);
471 return ctr_desall_crypt(desc, KMCTR_TDEA_192_ENCRYPT, ctx, &walk);
472}
473
474static int ctr_des3_decrypt(struct blkcipher_desc *desc,
475 struct scatterlist *dst, struct scatterlist *src,
476 unsigned int nbytes)
477{
478 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
479 struct blkcipher_walk walk;
480
481 blkcipher_walk_init(&walk, dst, src, nbytes);
482 return ctr_desall_crypt(desc, KMCTR_TDEA_192_DECRYPT, ctx, &walk);
483}
484
485static struct crypto_alg ctr_des3_alg = {
486 .cra_name = "ctr(des3_ede)",
487 .cra_driver_name = "ctr-des3_ede-s390",
488 .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
489 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
490 .cra_blocksize = 1,
491 .cra_ctxsize = sizeof(struct s390_des_ctx),
492 .cra_type = &crypto_blkcipher_type,
493 .cra_module = THIS_MODULE,
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000494 .cra_u = {
495 .blkcipher = {
496 .min_keysize = DES3_KEY_SIZE,
497 .max_keysize = DES3_KEY_SIZE,
498 .ivsize = DES_BLOCK_SIZE,
499 .setkey = des3_setkey,
500 .encrypt = ctr_des3_encrypt,
501 .decrypt = ctr_des3_decrypt,
502 }
503 }
504};
505
Jan Glauber98971f82011-04-19 21:29:15 +0200506static int __init des_s390_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
Jan Glauber80d663a2010-05-21 22:04:08 +1000508 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Jan Glauber1822bc92011-04-19 21:29:14 +0200510 if (!crypt_s390_func_available(KM_DEA_ENCRYPT, CRYPT_S390_MSA) ||
511 !crypt_s390_func_available(KM_TDEA_192_ENCRYPT, CRYPT_S390_MSA))
Jan Glauber86aa9fc2007-02-05 21:18:14 +0100512 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Herbert Xua9e62fa2006-08-21 21:39:24 +1000514 ret = crypto_register_alg(&des_alg);
515 if (ret)
516 goto des_err;
517 ret = crypto_register_alg(&ecb_des_alg);
518 if (ret)
519 goto ecb_des_err;
520 ret = crypto_register_alg(&cbc_des_alg);
521 if (ret)
522 goto cbc_des_err;
Jan Glauber98971f82011-04-19 21:29:15 +0200523 ret = crypto_register_alg(&des3_alg);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000524 if (ret)
Jan Glauber98971f82011-04-19 21:29:15 +0200525 goto des3_err;
526 ret = crypto_register_alg(&ecb_des3_alg);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000527 if (ret)
Jan Glauber98971f82011-04-19 21:29:15 +0200528 goto ecb_des3_err;
529 ret = crypto_register_alg(&cbc_des3_alg);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000530 if (ret)
Jan Glauber98971f82011-04-19 21:29:15 +0200531 goto cbc_des3_err;
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000532
533 if (crypt_s390_func_available(KMCTR_DEA_ENCRYPT,
534 CRYPT_S390_MSA | CRYPT_S390_MSA4) &&
535 crypt_s390_func_available(KMCTR_TDEA_192_ENCRYPT,
536 CRYPT_S390_MSA | CRYPT_S390_MSA4)) {
537 ret = crypto_register_alg(&ctr_des_alg);
538 if (ret)
539 goto ctr_des_err;
540 ret = crypto_register_alg(&ctr_des3_alg);
541 if (ret)
542 goto ctr_des3_err;
543 ctrblk = (u8 *) __get_free_page(GFP_KERNEL);
544 if (!ctrblk) {
545 ret = -ENOMEM;
546 goto ctr_mem_err;
547 }
548 }
Herbert Xua9e62fa2006-08-21 21:39:24 +1000549out:
550 return ret;
551
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000552ctr_mem_err:
553 crypto_unregister_alg(&ctr_des3_alg);
554ctr_des3_err:
555 crypto_unregister_alg(&ctr_des_alg);
556ctr_des_err:
557 crypto_unregister_alg(&cbc_des3_alg);
Jan Glauber98971f82011-04-19 21:29:15 +0200558cbc_des3_err:
559 crypto_unregister_alg(&ecb_des3_alg);
560ecb_des3_err:
561 crypto_unregister_alg(&des3_alg);
562des3_err:
Herbert Xua9e62fa2006-08-21 21:39:24 +1000563 crypto_unregister_alg(&cbc_des_alg);
564cbc_des_err:
565 crypto_unregister_alg(&ecb_des_alg);
566ecb_des_err:
567 crypto_unregister_alg(&des_alg);
568des_err:
569 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570}
571
Jan Glauber1efbd152010-05-21 22:04:46 +1000572static void __exit des_s390_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000574 if (ctrblk) {
575 crypto_unregister_alg(&ctr_des_alg);
576 crypto_unregister_alg(&ctr_des3_alg);
577 free_page((unsigned long) ctrblk);
578 }
Jan Glauber98971f82011-04-19 21:29:15 +0200579 crypto_unregister_alg(&cbc_des3_alg);
580 crypto_unregister_alg(&ecb_des3_alg);
581 crypto_unregister_alg(&des3_alg);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000582 crypto_unregister_alg(&cbc_des_alg);
583 crypto_unregister_alg(&ecb_des_alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 crypto_unregister_alg(&des_alg);
585}
586
Heiko Carstens9f7819c2008-04-17 07:46:17 +0200587module_init(des_s390_init);
Jan Glauber1efbd152010-05-21 22:04:46 +1000588module_exit(des_s390_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
590MODULE_ALIAS("des");
591MODULE_ALIAS("des3_ede");
592
593MODULE_LICENSE("GPL");
594MODULE_DESCRIPTION("DES & Triple DES EDE Cipher Algorithms");