blob: 4aba83b31596e78ddaa7ccb441025b59194fe5e7 [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 *
Jan Glauber86aa9fc2007-02-05 21:18:14 +01006 * Copyright IBM Corp. 2003,2007
7 * 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
17#include <crypto/algapi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/init.h>
19#include <linux/module.h>
Jan Glauberc1357832006-01-14 13:20:53 -080020
Jan Glauberc1e26e12006-01-06 00:19:17 -080021#include "crypt_s390.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#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 Glauberc1e26e12006-01-06 00:19:17 -080033struct crypt_s390_des_ctx {
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 u8 iv[DES_BLOCK_SIZE];
35 u8 key[DES_KEY_SIZE];
36};
37
Jan Glauberc1e26e12006-01-06 00:19:17 -080038struct crypt_s390_des3_128_ctx {
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 u8 iv[DES_BLOCK_SIZE];
40 u8 key[DES3_128_KEY_SIZE];
41};
42
Jan Glauberc1e26e12006-01-06 00:19:17 -080043struct crypt_s390_des3_192_ctx {
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 u8 iv[DES_BLOCK_SIZE];
45 u8 key[DES3_192_KEY_SIZE];
46};
47
Herbert Xu6c2bb982006-05-16 22:09:29 +100048static int des_setkey(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +100049 unsigned int keylen)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
Herbert Xu6c2bb982006-05-16 22:09:29 +100051 struct crypt_s390_des_ctx *dctx = crypto_tfm_ctx(tfm);
Herbert Xu560c06a2006-08-13 14:16:39 +100052 u32 *flags = &tfm->crt_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 int ret;
54
Jan Glauberc1357832006-01-14 13:20:53 -080055 /* test if key is valid (not a weak key) */
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 ret = crypto_des_check_key(key, keylen, flags);
Jan Glauberc1357832006-01-14 13:20:53 -080057 if (ret == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 memcpy(dctx->key, key, keylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 return ret;
60}
61
Herbert Xu6c2bb982006-05-16 22:09:29 +100062static void des_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
Herbert Xu6c2bb982006-05-16 22:09:29 +100064 struct crypt_s390_des_ctx *dctx = crypto_tfm_ctx(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Jan Glauberb8dc6032006-01-14 13:20:53 -080066 crypt_s390_km(KM_DEA_ENCRYPT, dctx->key, out, in, DES_BLOCK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067}
68
Herbert Xu6c2bb982006-05-16 22:09:29 +100069static void des_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
Herbert Xu6c2bb982006-05-16 22:09:29 +100071 struct crypt_s390_des_ctx *dctx = crypto_tfm_ctx(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Jan Glauberb8dc6032006-01-14 13:20:53 -080073 crypt_s390_km(KM_DEA_DECRYPT, dctx->key, out, in, DES_BLOCK_SIZE);
74}
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076static struct crypto_alg des_alg = {
77 .cra_name = "des",
Herbert Xu65b75c32006-08-21 21:18:50 +100078 .cra_driver_name = "des-s390",
79 .cra_priority = CRYPT_S390_PRIORITY,
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
81 .cra_blocksize = DES_BLOCK_SIZE,
Jan Glauberc1e26e12006-01-06 00:19:17 -080082 .cra_ctxsize = sizeof(struct crypt_s390_des_ctx),
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 .cra_module = THIS_MODULE,
84 .cra_list = LIST_HEAD_INIT(des_alg.cra_list),
Jan Glauberc1357832006-01-14 13:20:53 -080085 .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 Glauberb8dc6032006-01-14 13:20:53 -080091 .cia_decrypt = des_decrypt,
Jan Glauberc1357832006-01-14 13:20:53 -080092 }
93 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070094};
95
Herbert Xua9e62fa2006-08-21 21:39:24 +100096static 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
118static 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
142out:
143 return ret;
144}
145
146static 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
157static 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
168static 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
189static 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
200static 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
211static 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 Torvalds1da177e2005-04-16 15:20:36 -0700233/*
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 Xu6c2bb982006-05-16 22:09:29 +1000245static int des3_128_setkey(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000246 unsigned int keylen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
248 int i, ret;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000249 struct crypt_s390_des3_128_ctx *dctx = crypto_tfm_ctx(tfm);
Herbert Xu560c06a2006-08-13 14:16:39 +1000250 const u8 *temp_key = key;
251 u32 *flags = &tfm->crt_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 if (!(memcmp(key, &key[DES_KEY_SIZE], DES_KEY_SIZE))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 *flags |= CRYPTO_TFM_RES_BAD_KEY_SCHED;
255 return -EINVAL;
256 }
Jan Glauberc1357832006-01-14 13:20:53 -0800257 for (i = 0; i < 2; i++, temp_key += DES_KEY_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 ret = crypto_des_check_key(temp_key, DES_KEY_SIZE, flags);
259 if (ret < 0)
260 return ret;
261 }
262 memcpy(dctx->key, key, keylen);
263 return 0;
264}
265
Herbert Xu6c2bb982006-05-16 22:09:29 +1000266static void des3_128_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Herbert Xu6c2bb982006-05-16 22:09:29 +1000268 struct crypt_s390_des3_128_ctx *dctx = crypto_tfm_ctx(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Jan Glauberc1e26e12006-01-06 00:19:17 -0800270 crypt_s390_km(KM_TDEA_128_ENCRYPT, dctx->key, dst, (void*)src,
Jan Glauberc1357832006-01-14 13:20:53 -0800271 DES3_128_BLOCK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272}
273
Herbert Xu6c2bb982006-05-16 22:09:29 +1000274static void des3_128_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
Herbert Xu6c2bb982006-05-16 22:09:29 +1000276 struct crypt_s390_des3_128_ctx *dctx = crypto_tfm_ctx(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Jan Glauberc1e26e12006-01-06 00:19:17 -0800278 crypt_s390_km(KM_TDEA_128_DECRYPT, dctx->key, dst, (void*)src,
Jan Glauberc1357832006-01-14 13:20:53 -0800279 DES3_128_BLOCK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
282static struct crypto_alg des3_128_alg = {
283 .cra_name = "des3_ede128",
Herbert Xu65b75c32006-08-21 21:18:50 +1000284 .cra_driver_name = "des3_ede128-s390",
285 .cra_priority = CRYPT_S390_PRIORITY,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
287 .cra_blocksize = DES3_128_BLOCK_SIZE,
Jan Glauberc1e26e12006-01-06 00:19:17 -0800288 .cra_ctxsize = sizeof(struct crypt_s390_des3_128_ctx),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 .cra_module = THIS_MODULE,
290 .cra_list = LIST_HEAD_INIT(des3_128_alg.cra_list),
Jan Glauberc1357832006-01-14 13:20:53 -0800291 .cra_u = {
292 .cipher = {
293 .cia_min_keysize = DES3_128_KEY_SIZE,
294 .cia_max_keysize = DES3_128_KEY_SIZE,
295 .cia_setkey = des3_128_setkey,
296 .cia_encrypt = des3_128_encrypt,
Jan Glauberb8dc6032006-01-14 13:20:53 -0800297 .cia_decrypt = des3_128_decrypt,
Jan Glauberc1357832006-01-14 13:20:53 -0800298 }
299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300};
301
Herbert Xua9e62fa2006-08-21 21:39:24 +1000302static int ecb_des3_128_encrypt(struct blkcipher_desc *desc,
303 struct scatterlist *dst,
304 struct scatterlist *src, unsigned int nbytes)
305{
306 struct crypt_s390_des3_128_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
307 struct blkcipher_walk walk;
308
309 blkcipher_walk_init(&walk, dst, src, nbytes);
310 return ecb_desall_crypt(desc, KM_TDEA_128_ENCRYPT, sctx->key, &walk);
311}
312
313static int ecb_des3_128_decrypt(struct blkcipher_desc *desc,
314 struct scatterlist *dst,
315 struct scatterlist *src, unsigned int nbytes)
316{
317 struct crypt_s390_des3_128_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
318 struct blkcipher_walk walk;
319
320 blkcipher_walk_init(&walk, dst, src, nbytes);
321 return ecb_desall_crypt(desc, KM_TDEA_128_DECRYPT, sctx->key, &walk);
322}
323
324static struct crypto_alg ecb_des3_128_alg = {
325 .cra_name = "ecb(des3_ede128)",
326 .cra_driver_name = "ecb-des3_ede128-s390",
327 .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
328 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
329 .cra_blocksize = DES3_128_BLOCK_SIZE,
330 .cra_ctxsize = sizeof(struct crypt_s390_des3_128_ctx),
331 .cra_type = &crypto_blkcipher_type,
332 .cra_module = THIS_MODULE,
333 .cra_list = LIST_HEAD_INIT(
334 ecb_des3_128_alg.cra_list),
335 .cra_u = {
336 .blkcipher = {
337 .min_keysize = DES3_128_KEY_SIZE,
338 .max_keysize = DES3_128_KEY_SIZE,
339 .setkey = des3_128_setkey,
340 .encrypt = ecb_des3_128_encrypt,
341 .decrypt = ecb_des3_128_decrypt,
342 }
343 }
344};
345
346static int cbc_des3_128_encrypt(struct blkcipher_desc *desc,
347 struct scatterlist *dst,
348 struct scatterlist *src, unsigned int nbytes)
349{
350 struct crypt_s390_des3_128_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
351 struct blkcipher_walk walk;
352
353 blkcipher_walk_init(&walk, dst, src, nbytes);
354 return cbc_desall_crypt(desc, KMC_TDEA_128_ENCRYPT, sctx->iv, &walk);
355}
356
357static int cbc_des3_128_decrypt(struct blkcipher_desc *desc,
358 struct scatterlist *dst,
359 struct scatterlist *src, unsigned int nbytes)
360{
361 struct crypt_s390_des3_128_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
362 struct blkcipher_walk walk;
363
364 blkcipher_walk_init(&walk, dst, src, nbytes);
365 return cbc_desall_crypt(desc, KMC_TDEA_128_DECRYPT, sctx->iv, &walk);
366}
367
368static struct crypto_alg cbc_des3_128_alg = {
369 .cra_name = "cbc(des3_ede128)",
370 .cra_driver_name = "cbc-des3_ede128-s390",
371 .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
372 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
373 .cra_blocksize = DES3_128_BLOCK_SIZE,
374 .cra_ctxsize = sizeof(struct crypt_s390_des3_128_ctx),
375 .cra_type = &crypto_blkcipher_type,
376 .cra_module = THIS_MODULE,
377 .cra_list = LIST_HEAD_INIT(
378 cbc_des3_128_alg.cra_list),
379 .cra_u = {
380 .blkcipher = {
381 .min_keysize = DES3_128_KEY_SIZE,
382 .max_keysize = DES3_128_KEY_SIZE,
383 .ivsize = DES3_128_BLOCK_SIZE,
384 .setkey = des3_128_setkey,
385 .encrypt = cbc_des3_128_encrypt,
386 .decrypt = cbc_des3_128_decrypt,
387 }
388 }
389};
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391/*
392 * RFC2451:
393 *
394 * For DES-EDE3, there is no known need to reject weak or
395 * complementation keys. Any weakness is obviated by the use of
396 * multiple keys.
397 *
398 * However, if the first two or last two independent 64-bit keys are
399 * equal (k1 == k2 or k2 == k3), then the DES3 operation is simply the
400 * same as DES. Implementers MUST reject keys that exhibit this
401 * property.
402 *
403 */
Herbert Xu6c2bb982006-05-16 22:09:29 +1000404static int des3_192_setkey(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000405 unsigned int keylen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
407 int i, ret;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000408 struct crypt_s390_des3_192_ctx *dctx = crypto_tfm_ctx(tfm);
Herbert Xu560c06a2006-08-13 14:16:39 +1000409 const u8 *temp_key = key;
410 u32 *flags = &tfm->crt_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 if (!(memcmp(key, &key[DES_KEY_SIZE], DES_KEY_SIZE) &&
413 memcmp(&key[DES_KEY_SIZE], &key[DES_KEY_SIZE * 2],
Jan Glauberc1357832006-01-14 13:20:53 -0800414 DES_KEY_SIZE))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 *flags |= CRYPTO_TFM_RES_BAD_KEY_SCHED;
417 return -EINVAL;
418 }
419 for (i = 0; i < 3; i++, temp_key += DES_KEY_SIZE) {
420 ret = crypto_des_check_key(temp_key, DES_KEY_SIZE, flags);
Jan Glauberc1357832006-01-14 13:20:53 -0800421 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 }
424 memcpy(dctx->key, key, keylen);
425 return 0;
426}
427
Herbert Xu6c2bb982006-05-16 22:09:29 +1000428static void des3_192_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
Herbert Xu6c2bb982006-05-16 22:09:29 +1000430 struct crypt_s390_des3_192_ctx *dctx = crypto_tfm_ctx(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Jan Glauberc1e26e12006-01-06 00:19:17 -0800432 crypt_s390_km(KM_TDEA_192_ENCRYPT, dctx->key, dst, (void*)src,
Jan Glauberc1357832006-01-14 13:20:53 -0800433 DES3_192_BLOCK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
Herbert Xu6c2bb982006-05-16 22:09:29 +1000436static void des3_192_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
Herbert Xu6c2bb982006-05-16 22:09:29 +1000438 struct crypt_s390_des3_192_ctx *dctx = crypto_tfm_ctx(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Jan Glauberc1e26e12006-01-06 00:19:17 -0800440 crypt_s390_km(KM_TDEA_192_DECRYPT, dctx->key, dst, (void*)src,
Jan Glauberc1357832006-01-14 13:20:53 -0800441 DES3_192_BLOCK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
443
444static struct crypto_alg des3_192_alg = {
445 .cra_name = "des3_ede",
Herbert Xu65b75c32006-08-21 21:18:50 +1000446 .cra_driver_name = "des3_ede-s390",
447 .cra_priority = CRYPT_S390_PRIORITY,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
449 .cra_blocksize = DES3_192_BLOCK_SIZE,
Jan Glauberc1e26e12006-01-06 00:19:17 -0800450 .cra_ctxsize = sizeof(struct crypt_s390_des3_192_ctx),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 .cra_module = THIS_MODULE,
452 .cra_list = LIST_HEAD_INIT(des3_192_alg.cra_list),
Jan Glauberc1357832006-01-14 13:20:53 -0800453 .cra_u = {
454 .cipher = {
455 .cia_min_keysize = DES3_192_KEY_SIZE,
456 .cia_max_keysize = DES3_192_KEY_SIZE,
457 .cia_setkey = des3_192_setkey,
458 .cia_encrypt = des3_192_encrypt,
Jan Glauberb8dc6032006-01-14 13:20:53 -0800459 .cia_decrypt = des3_192_decrypt,
Jan Glauberc1357832006-01-14 13:20:53 -0800460 }
461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462};
463
Herbert Xua9e62fa2006-08-21 21:39:24 +1000464static int ecb_des3_192_encrypt(struct blkcipher_desc *desc,
465 struct scatterlist *dst,
466 struct scatterlist *src, unsigned int nbytes)
467{
468 struct crypt_s390_des3_192_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
469 struct blkcipher_walk walk;
470
471 blkcipher_walk_init(&walk, dst, src, nbytes);
472 return ecb_desall_crypt(desc, KM_TDEA_192_ENCRYPT, sctx->key, &walk);
473}
474
475static int ecb_des3_192_decrypt(struct blkcipher_desc *desc,
476 struct scatterlist *dst,
477 struct scatterlist *src, unsigned int nbytes)
478{
479 struct crypt_s390_des3_192_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
480 struct blkcipher_walk walk;
481
482 blkcipher_walk_init(&walk, dst, src, nbytes);
483 return ecb_desall_crypt(desc, KM_TDEA_192_DECRYPT, sctx->key, &walk);
484}
485
486static struct crypto_alg ecb_des3_192_alg = {
487 .cra_name = "ecb(des3_ede)",
488 .cra_driver_name = "ecb-des3_ede-s390",
489 .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
490 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
491 .cra_blocksize = DES3_192_BLOCK_SIZE,
492 .cra_ctxsize = sizeof(struct crypt_s390_des3_192_ctx),
493 .cra_type = &crypto_blkcipher_type,
494 .cra_module = THIS_MODULE,
495 .cra_list = LIST_HEAD_INIT(
496 ecb_des3_192_alg.cra_list),
497 .cra_u = {
498 .blkcipher = {
499 .min_keysize = DES3_192_KEY_SIZE,
500 .max_keysize = DES3_192_KEY_SIZE,
501 .setkey = des3_192_setkey,
502 .encrypt = ecb_des3_192_encrypt,
503 .decrypt = ecb_des3_192_decrypt,
504 }
505 }
506};
507
508static int cbc_des3_192_encrypt(struct blkcipher_desc *desc,
509 struct scatterlist *dst,
510 struct scatterlist *src, unsigned int nbytes)
511{
512 struct crypt_s390_des3_192_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
513 struct blkcipher_walk walk;
514
515 blkcipher_walk_init(&walk, dst, src, nbytes);
516 return cbc_desall_crypt(desc, KMC_TDEA_192_ENCRYPT, sctx->iv, &walk);
517}
518
519static int cbc_des3_192_decrypt(struct blkcipher_desc *desc,
520 struct scatterlist *dst,
521 struct scatterlist *src, unsigned int nbytes)
522{
523 struct crypt_s390_des3_192_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
524 struct blkcipher_walk walk;
525
526 blkcipher_walk_init(&walk, dst, src, nbytes);
527 return cbc_desall_crypt(desc, KMC_TDEA_192_DECRYPT, sctx->iv, &walk);
528}
529
530static struct crypto_alg cbc_des3_192_alg = {
531 .cra_name = "cbc(des3_ede)",
532 .cra_driver_name = "cbc-des3_ede-s390",
533 .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
534 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
535 .cra_blocksize = DES3_192_BLOCK_SIZE,
536 .cra_ctxsize = sizeof(struct crypt_s390_des3_192_ctx),
537 .cra_type = &crypto_blkcipher_type,
538 .cra_module = THIS_MODULE,
539 .cra_list = LIST_HEAD_INIT(
540 cbc_des3_192_alg.cra_list),
541 .cra_u = {
542 .blkcipher = {
543 .min_keysize = DES3_192_KEY_SIZE,
544 .max_keysize = DES3_192_KEY_SIZE,
545 .ivsize = DES3_192_BLOCK_SIZE,
546 .setkey = des3_192_setkey,
547 .encrypt = cbc_des3_192_encrypt,
548 .decrypt = cbc_des3_192_decrypt,
549 }
550 }
551};
552
Heiko Carstens9f7819c2008-04-17 07:46:17 +0200553static int des_s390_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
Jan Glauberc1357832006-01-14 13:20:53 -0800555 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Jan Glauberc1e26e12006-01-06 00:19:17 -0800557 if (!crypt_s390_func_available(KM_DEA_ENCRYPT) ||
558 !crypt_s390_func_available(KM_TDEA_128_ENCRYPT) ||
Jan Glauberc1357832006-01-14 13:20:53 -0800559 !crypt_s390_func_available(KM_TDEA_192_ENCRYPT))
Jan Glauber86aa9fc2007-02-05 21:18:14 +0100560 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Herbert Xua9e62fa2006-08-21 21:39:24 +1000562 ret = crypto_register_alg(&des_alg);
563 if (ret)
564 goto des_err;
565 ret = crypto_register_alg(&ecb_des_alg);
566 if (ret)
567 goto ecb_des_err;
568 ret = crypto_register_alg(&cbc_des_alg);
569 if (ret)
570 goto cbc_des_err;
571
572 ret = crypto_register_alg(&des3_128_alg);
573 if (ret)
574 goto des3_128_err;
575 ret = crypto_register_alg(&ecb_des3_128_alg);
576 if (ret)
577 goto ecb_des3_128_err;
578 ret = crypto_register_alg(&cbc_des3_128_alg);
579 if (ret)
580 goto cbc_des3_128_err;
581
582 ret = crypto_register_alg(&des3_192_alg);
583 if (ret)
584 goto des3_192_err;
585 ret = crypto_register_alg(&ecb_des3_192_alg);
586 if (ret)
587 goto ecb_des3_192_err;
588 ret = crypto_register_alg(&cbc_des3_192_alg);
589 if (ret)
590 goto cbc_des3_192_err;
591
592out:
593 return ret;
594
595cbc_des3_192_err:
596 crypto_unregister_alg(&ecb_des3_192_alg);
597ecb_des3_192_err:
598 crypto_unregister_alg(&des3_192_alg);
599des3_192_err:
600 crypto_unregister_alg(&cbc_des3_128_alg);
601cbc_des3_128_err:
602 crypto_unregister_alg(&ecb_des3_128_alg);
603ecb_des3_128_err:
604 crypto_unregister_alg(&des3_128_alg);
605des3_128_err:
606 crypto_unregister_alg(&cbc_des_alg);
607cbc_des_err:
608 crypto_unregister_alg(&ecb_des_alg);
609ecb_des_err:
610 crypto_unregister_alg(&des_alg);
611des_err:
612 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613}
614
Heiko Carstens9f7819c2008-04-17 07:46:17 +0200615static void __exit des_s390_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
Herbert Xua9e62fa2006-08-21 21:39:24 +1000617 crypto_unregister_alg(&cbc_des3_192_alg);
618 crypto_unregister_alg(&ecb_des3_192_alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 crypto_unregister_alg(&des3_192_alg);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000620 crypto_unregister_alg(&cbc_des3_128_alg);
621 crypto_unregister_alg(&ecb_des3_128_alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 crypto_unregister_alg(&des3_128_alg);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000623 crypto_unregister_alg(&cbc_des_alg);
624 crypto_unregister_alg(&ecb_des_alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 crypto_unregister_alg(&des_alg);
626}
627
Heiko Carstens9f7819c2008-04-17 07:46:17 +0200628module_init(des_s390_init);
629module_exit(des_s390_fini);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
631MODULE_ALIAS("des");
632MODULE_ALIAS("des3_ede");
633
634MODULE_LICENSE("GPL");
635MODULE_DESCRIPTION("DES & Triple DES EDE Cipher Algorithms");