blob: a52bfd124d86e009184c8326a9e1b03bfeedf76e [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 *
Gerald Schaefer0200f3e2011-05-04 15:09:44 +10006 * 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,
73 .cra_list = LIST_HEAD_INIT(des_alg.cra_list),
Jan Glauberc1357832006-01-14 13:20:53 -080074 .cra_u = {
75 .cipher = {
76 .cia_min_keysize = DES_KEY_SIZE,
77 .cia_max_keysize = DES_KEY_SIZE,
78 .cia_setkey = des_setkey,
79 .cia_encrypt = des_encrypt,
Jan Glauberb8dc6032006-01-14 13:20:53 -080080 .cia_decrypt = des_decrypt,
Jan Glauberc1357832006-01-14 13:20:53 -080081 }
82 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070083};
84
Herbert Xua9e62fa2006-08-21 21:39:24 +100085static int ecb_desall_crypt(struct blkcipher_desc *desc, long func,
Jan Glauber98971f82011-04-19 21:29:15 +020086 u8 *key, struct blkcipher_walk *walk)
Herbert Xua9e62fa2006-08-21 21:39:24 +100087{
88 int ret = blkcipher_walk_virt(desc, walk);
89 unsigned int nbytes;
90
91 while ((nbytes = walk->nbytes)) {
92 /* only use complete blocks */
93 unsigned int n = nbytes & ~(DES_BLOCK_SIZE - 1);
94 u8 *out = walk->dst.virt.addr;
95 u8 *in = walk->src.virt.addr;
96
Jan Glauber98971f82011-04-19 21:29:15 +020097 ret = crypt_s390_km(func, key, out, in, n);
Herbert Xua9e62fa2006-08-21 21:39:24 +100098 BUG_ON((ret < 0) || (ret != n));
99
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);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000124 BUG_ON((ret < 0) || (ret != n));
125
126 nbytes &= DES_BLOCK_SIZE - 1;
127 ret = blkcipher_walk_done(desc, walk, nbytes);
128 } while ((nbytes = walk->nbytes));
Jan Glauber98971f82011-04-19 21:29:15 +0200129 memcpy(walk->iv, iv, DES_BLOCK_SIZE);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000130
131out:
132 return ret;
133}
134
135static int ecb_des_encrypt(struct blkcipher_desc *desc,
136 struct scatterlist *dst, struct scatterlist *src,
137 unsigned int nbytes)
138{
Jan Glauber98971f82011-04-19 21:29:15 +0200139 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000140 struct blkcipher_walk walk;
141
142 blkcipher_walk_init(&walk, dst, src, nbytes);
Jan Glauber98971f82011-04-19 21:29:15 +0200143 return ecb_desall_crypt(desc, KM_DEA_ENCRYPT, ctx->key, &walk);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000144}
145
146static int ecb_des_decrypt(struct blkcipher_desc *desc,
147 struct scatterlist *dst, struct scatterlist *src,
148 unsigned int nbytes)
149{
Jan Glauber98971f82011-04-19 21:29:15 +0200150 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000151 struct blkcipher_walk walk;
152
153 blkcipher_walk_init(&walk, dst, src, nbytes);
Jan Glauber98971f82011-04-19 21:29:15 +0200154 return ecb_desall_crypt(desc, KM_DEA_DECRYPT, ctx->key, &walk);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000155}
156
157static struct crypto_alg ecb_des_alg = {
158 .cra_name = "ecb(des)",
159 .cra_driver_name = "ecb-des-s390",
160 .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
161 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
162 .cra_blocksize = DES_BLOCK_SIZE,
Jan Glauber98971f82011-04-19 21:29:15 +0200163 .cra_ctxsize = sizeof(struct s390_des_ctx),
Herbert Xua9e62fa2006-08-21 21:39:24 +1000164 .cra_type = &crypto_blkcipher_type,
165 .cra_module = THIS_MODULE,
166 .cra_list = LIST_HEAD_INIT(ecb_des_alg.cra_list),
167 .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,
209 .cra_list = LIST_HEAD_INIT(cbc_des_alg.cra_list),
210 .cra_u = {
211 .blkcipher = {
212 .min_keysize = DES_KEY_SIZE,
213 .max_keysize = DES_KEY_SIZE,
214 .ivsize = DES_BLOCK_SIZE,
215 .setkey = des_setkey,
216 .encrypt = cbc_des_encrypt,
217 .decrypt = cbc_des_decrypt,
218 }
219 }
220};
221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222/*
223 * RFC2451:
224 *
225 * For DES-EDE3, there is no known need to reject weak or
226 * complementation keys. Any weakness is obviated by the use of
227 * multiple keys.
228 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 * However, if the first two or last two independent 64-bit keys are
230 * equal (k1 == k2 or k2 == k3), then the DES3 operation is simply the
231 * same as DES. Implementers MUST reject keys that exhibit this
232 * property.
233 *
234 */
Jan Glauber98971f82011-04-19 21:29:15 +0200235static int des3_setkey(struct crypto_tfm *tfm, const u8 *key,
236 unsigned int key_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
Jan Glauber98971f82011-04-19 21:29:15 +0200238 struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
Herbert Xu560c06a2006-08-13 14:16:39 +1000239 u32 *flags = &tfm->crt_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 if (!(memcmp(key, &key[DES_KEY_SIZE], DES_KEY_SIZE) &&
242 memcmp(&key[DES_KEY_SIZE], &key[DES_KEY_SIZE * 2],
Jarod Wilson03b56ce2009-06-18 19:52:59 +0800243 DES_KEY_SIZE)) &&
244 (*flags & CRYPTO_TFM_REQ_WEAK_KEY)) {
245 *flags |= CRYPTO_TFM_RES_WEAK_KEY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 return -EINVAL;
247 }
Jan Glauber98971f82011-04-19 21:29:15 +0200248 memcpy(ctx->key, key, key_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return 0;
250}
251
Jan Glauber98971f82011-04-19 21:29:15 +0200252static void des3_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
Jan Glauber98971f82011-04-19 21:29:15 +0200254 struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Jan Glauber98971f82011-04-19 21:29:15 +0200256 crypt_s390_km(KM_TDEA_192_ENCRYPT, ctx->key, dst, src, DES_BLOCK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
258
Jan Glauber98971f82011-04-19 21:29:15 +0200259static void des3_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
Jan Glauber98971f82011-04-19 21:29:15 +0200261 struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Jan Glauber98971f82011-04-19 21:29:15 +0200263 crypt_s390_km(KM_TDEA_192_DECRYPT, ctx->key, dst, src, DES_BLOCK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265
Jan Glauber98971f82011-04-19 21:29:15 +0200266static struct crypto_alg des3_alg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 .cra_name = "des3_ede",
Herbert Xu65b75c32006-08-21 21:18:50 +1000268 .cra_driver_name = "des3_ede-s390",
269 .cra_priority = CRYPT_S390_PRIORITY,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
Jan Glauber1efbd152010-05-21 22:04:46 +1000271 .cra_blocksize = DES_BLOCK_SIZE,
Jan Glauber98971f82011-04-19 21:29:15 +0200272 .cra_ctxsize = sizeof(struct s390_des_ctx),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 .cra_module = THIS_MODULE,
Jan Glauber98971f82011-04-19 21:29:15 +0200274 .cra_list = LIST_HEAD_INIT(des3_alg.cra_list),
Jan Glauberc1357832006-01-14 13:20:53 -0800275 .cra_u = {
276 .cipher = {
Jan Glauber98971f82011-04-19 21:29:15 +0200277 .cia_min_keysize = DES3_KEY_SIZE,
278 .cia_max_keysize = DES3_KEY_SIZE,
279 .cia_setkey = des3_setkey,
280 .cia_encrypt = des3_encrypt,
281 .cia_decrypt = des3_decrypt,
Jan Glauberc1357832006-01-14 13:20:53 -0800282 }
283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284};
285
Jan Glauber98971f82011-04-19 21:29:15 +0200286static int ecb_des3_encrypt(struct blkcipher_desc *desc,
287 struct scatterlist *dst, struct scatterlist *src,
288 unsigned int nbytes)
Herbert Xua9e62fa2006-08-21 21:39:24 +1000289{
Jan Glauber98971f82011-04-19 21:29:15 +0200290 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000291 struct blkcipher_walk walk;
292
293 blkcipher_walk_init(&walk, dst, src, nbytes);
Jan Glauber98971f82011-04-19 21:29:15 +0200294 return ecb_desall_crypt(desc, KM_TDEA_192_ENCRYPT, ctx->key, &walk);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000295}
296
Jan Glauber98971f82011-04-19 21:29:15 +0200297static int ecb_des3_decrypt(struct blkcipher_desc *desc,
298 struct scatterlist *dst, struct scatterlist *src,
299 unsigned int nbytes)
Herbert Xua9e62fa2006-08-21 21:39:24 +1000300{
Jan Glauber98971f82011-04-19 21:29:15 +0200301 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000302 struct blkcipher_walk walk;
303
304 blkcipher_walk_init(&walk, dst, src, nbytes);
Jan Glauber98971f82011-04-19 21:29:15 +0200305 return ecb_desall_crypt(desc, KM_TDEA_192_DECRYPT, ctx->key, &walk);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000306}
307
Jan Glauber98971f82011-04-19 21:29:15 +0200308static struct crypto_alg ecb_des3_alg = {
Herbert Xua9e62fa2006-08-21 21:39:24 +1000309 .cra_name = "ecb(des3_ede)",
310 .cra_driver_name = "ecb-des3_ede-s390",
311 .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
312 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
Jan Glauber1efbd152010-05-21 22:04:46 +1000313 .cra_blocksize = DES_BLOCK_SIZE,
Jan Glauber98971f82011-04-19 21:29:15 +0200314 .cra_ctxsize = sizeof(struct s390_des_ctx),
Herbert Xua9e62fa2006-08-21 21:39:24 +1000315 .cra_type = &crypto_blkcipher_type,
316 .cra_module = THIS_MODULE,
317 .cra_list = LIST_HEAD_INIT(
Jan Glauber98971f82011-04-19 21:29:15 +0200318 ecb_des3_alg.cra_list),
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{
Jan Glauber98971f82011-04-19 21:29:15 +0200334 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000335 struct blkcipher_walk walk;
336
337 blkcipher_walk_init(&walk, dst, src, nbytes);
Jan Glauber98971f82011-04-19 21:29:15 +0200338 return cbc_desall_crypt(desc, KMC_TDEA_192_ENCRYPT, ctx->iv, &walk);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000339}
340
Jan Glauber98971f82011-04-19 21:29:15 +0200341static int cbc_des3_decrypt(struct blkcipher_desc *desc,
342 struct scatterlist *dst, struct scatterlist *src,
343 unsigned int nbytes)
Herbert Xua9e62fa2006-08-21 21:39:24 +1000344{
Jan Glauber98971f82011-04-19 21:29:15 +0200345 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000346 struct blkcipher_walk walk;
347
348 blkcipher_walk_init(&walk, dst, src, nbytes);
Jan Glauber98971f82011-04-19 21:29:15 +0200349 return cbc_desall_crypt(desc, KMC_TDEA_192_DECRYPT, ctx->iv, &walk);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000350}
351
Jan Glauber98971f82011-04-19 21:29:15 +0200352static struct crypto_alg cbc_des3_alg = {
Herbert Xua9e62fa2006-08-21 21:39:24 +1000353 .cra_name = "cbc(des3_ede)",
354 .cra_driver_name = "cbc-des3_ede-s390",
355 .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
356 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
Jan Glauber1efbd152010-05-21 22:04:46 +1000357 .cra_blocksize = DES_BLOCK_SIZE,
Jan Glauber98971f82011-04-19 21:29:15 +0200358 .cra_ctxsize = sizeof(struct s390_des_ctx),
Herbert Xua9e62fa2006-08-21 21:39:24 +1000359 .cra_type = &crypto_blkcipher_type,
360 .cra_module = THIS_MODULE,
361 .cra_list = LIST_HEAD_INIT(
Jan Glauber98971f82011-04-19 21:29:15 +0200362 cbc_des3_alg.cra_list),
Herbert Xua9e62fa2006-08-21 21:39:24 +1000363 .cra_u = {
364 .blkcipher = {
Jan Glauber98971f82011-04-19 21:29:15 +0200365 .min_keysize = DES3_KEY_SIZE,
366 .max_keysize = DES3_KEY_SIZE,
Jan Glauber1efbd152010-05-21 22:04:46 +1000367 .ivsize = DES_BLOCK_SIZE,
Jan Glauber98971f82011-04-19 21:29:15 +0200368 .setkey = des3_setkey,
369 .encrypt = cbc_des3_encrypt,
370 .decrypt = cbc_des3_decrypt,
Herbert Xua9e62fa2006-08-21 21:39:24 +1000371 }
372 }
373};
374
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000375static int ctr_desall_crypt(struct blkcipher_desc *desc, long func,
376 struct s390_des_ctx *ctx, struct blkcipher_walk *walk)
377{
378 int ret = blkcipher_walk_virt_block(desc, walk, DES_BLOCK_SIZE);
379 unsigned int i, n, nbytes;
380 u8 buf[DES_BLOCK_SIZE];
381 u8 *out, *in;
382
383 memcpy(ctrblk, walk->iv, DES_BLOCK_SIZE);
384 while ((nbytes = walk->nbytes) >= DES_BLOCK_SIZE) {
385 out = walk->dst.virt.addr;
386 in = walk->src.virt.addr;
387 while (nbytes >= DES_BLOCK_SIZE) {
388 /* align to block size, max. PAGE_SIZE */
389 n = (nbytes > PAGE_SIZE) ? PAGE_SIZE :
390 nbytes & ~(DES_BLOCK_SIZE - 1);
391 for (i = DES_BLOCK_SIZE; i < n; i += DES_BLOCK_SIZE) {
392 memcpy(ctrblk + i, ctrblk + i - DES_BLOCK_SIZE,
393 DES_BLOCK_SIZE);
394 crypto_inc(ctrblk + i, DES_BLOCK_SIZE);
395 }
396 ret = crypt_s390_kmctr(func, ctx->key, out, in, n, ctrblk);
397 BUG_ON((ret < 0) || (ret != n));
398 if (n > DES_BLOCK_SIZE)
399 memcpy(ctrblk, ctrblk + n - DES_BLOCK_SIZE,
400 DES_BLOCK_SIZE);
401 crypto_inc(ctrblk, DES_BLOCK_SIZE);
402 out += n;
403 in += n;
404 nbytes -= n;
405 }
406 ret = blkcipher_walk_done(desc, walk, nbytes);
407 }
408
409 /* final block may be < DES_BLOCK_SIZE, copy only nbytes */
410 if (nbytes) {
411 out = walk->dst.virt.addr;
412 in = walk->src.virt.addr;
413 ret = crypt_s390_kmctr(func, ctx->key, buf, in,
414 DES_BLOCK_SIZE, ctrblk);
415 BUG_ON(ret < 0 || ret != DES_BLOCK_SIZE);
416 memcpy(out, buf, nbytes);
417 crypto_inc(ctrblk, DES_BLOCK_SIZE);
418 ret = blkcipher_walk_done(desc, walk, 0);
419 }
420 memcpy(walk->iv, ctrblk, DES_BLOCK_SIZE);
421 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{
428 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
429 struct blkcipher_walk walk;
430
431 blkcipher_walk_init(&walk, dst, src, nbytes);
432 return ctr_desall_crypt(desc, KMCTR_DEA_ENCRYPT, ctx, &walk);
433}
434
435static int ctr_des_decrypt(struct blkcipher_desc *desc,
436 struct scatterlist *dst, struct scatterlist *src,
437 unsigned int nbytes)
438{
439 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
440 struct blkcipher_walk walk;
441
442 blkcipher_walk_init(&walk, dst, src, nbytes);
443 return ctr_desall_crypt(desc, KMCTR_DEA_DECRYPT, ctx, &walk);
444}
445
446static struct crypto_alg ctr_des_alg = {
447 .cra_name = "ctr(des)",
448 .cra_driver_name = "ctr-des-s390",
449 .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
450 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
451 .cra_blocksize = 1,
452 .cra_ctxsize = sizeof(struct s390_des_ctx),
453 .cra_type = &crypto_blkcipher_type,
454 .cra_module = THIS_MODULE,
455 .cra_list = LIST_HEAD_INIT(ctr_des_alg.cra_list),
456 .cra_u = {
457 .blkcipher = {
458 .min_keysize = DES_KEY_SIZE,
459 .max_keysize = DES_KEY_SIZE,
460 .ivsize = DES_BLOCK_SIZE,
461 .setkey = des_setkey,
462 .encrypt = ctr_des_encrypt,
463 .decrypt = ctr_des_decrypt,
464 }
465 }
466};
467
468static int ctr_des3_encrypt(struct blkcipher_desc *desc,
469 struct scatterlist *dst, struct scatterlist *src,
470 unsigned int nbytes)
471{
472 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
473 struct blkcipher_walk walk;
474
475 blkcipher_walk_init(&walk, dst, src, nbytes);
476 return ctr_desall_crypt(desc, KMCTR_TDEA_192_ENCRYPT, ctx, &walk);
477}
478
479static int ctr_des3_decrypt(struct blkcipher_desc *desc,
480 struct scatterlist *dst, struct scatterlist *src,
481 unsigned int nbytes)
482{
483 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
484 struct blkcipher_walk walk;
485
486 blkcipher_walk_init(&walk, dst, src, nbytes);
487 return ctr_desall_crypt(desc, KMCTR_TDEA_192_DECRYPT, ctx, &walk);
488}
489
490static struct crypto_alg ctr_des3_alg = {
491 .cra_name = "ctr(des3_ede)",
492 .cra_driver_name = "ctr-des3_ede-s390",
493 .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
494 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
495 .cra_blocksize = 1,
496 .cra_ctxsize = sizeof(struct s390_des_ctx),
497 .cra_type = &crypto_blkcipher_type,
498 .cra_module = THIS_MODULE,
499 .cra_list = LIST_HEAD_INIT(ctr_des3_alg.cra_list),
500 .cra_u = {
501 .blkcipher = {
502 .min_keysize = DES3_KEY_SIZE,
503 .max_keysize = DES3_KEY_SIZE,
504 .ivsize = DES_BLOCK_SIZE,
505 .setkey = des3_setkey,
506 .encrypt = ctr_des3_encrypt,
507 .decrypt = ctr_des3_decrypt,
508 }
509 }
510};
511
Jan Glauber98971f82011-04-19 21:29:15 +0200512static int __init des_s390_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
Jan Glauber80d663a2010-05-21 22:04:08 +1000514 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Jan Glauber1822bc92011-04-19 21:29:14 +0200516 if (!crypt_s390_func_available(KM_DEA_ENCRYPT, CRYPT_S390_MSA) ||
517 !crypt_s390_func_available(KM_TDEA_192_ENCRYPT, CRYPT_S390_MSA))
Jan Glauber86aa9fc2007-02-05 21:18:14 +0100518 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Herbert Xua9e62fa2006-08-21 21:39:24 +1000520 ret = crypto_register_alg(&des_alg);
521 if (ret)
522 goto des_err;
523 ret = crypto_register_alg(&ecb_des_alg);
524 if (ret)
525 goto ecb_des_err;
526 ret = crypto_register_alg(&cbc_des_alg);
527 if (ret)
528 goto cbc_des_err;
Jan Glauber98971f82011-04-19 21:29:15 +0200529 ret = crypto_register_alg(&des3_alg);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000530 if (ret)
Jan Glauber98971f82011-04-19 21:29:15 +0200531 goto des3_err;
532 ret = crypto_register_alg(&ecb_des3_alg);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000533 if (ret)
Jan Glauber98971f82011-04-19 21:29:15 +0200534 goto ecb_des3_err;
535 ret = crypto_register_alg(&cbc_des3_alg);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000536 if (ret)
Jan Glauber98971f82011-04-19 21:29:15 +0200537 goto cbc_des3_err;
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000538
539 if (crypt_s390_func_available(KMCTR_DEA_ENCRYPT,
540 CRYPT_S390_MSA | CRYPT_S390_MSA4) &&
541 crypt_s390_func_available(KMCTR_TDEA_192_ENCRYPT,
542 CRYPT_S390_MSA | CRYPT_S390_MSA4)) {
543 ret = crypto_register_alg(&ctr_des_alg);
544 if (ret)
545 goto ctr_des_err;
546 ret = crypto_register_alg(&ctr_des3_alg);
547 if (ret)
548 goto ctr_des3_err;
549 ctrblk = (u8 *) __get_free_page(GFP_KERNEL);
550 if (!ctrblk) {
551 ret = -ENOMEM;
552 goto ctr_mem_err;
553 }
554 }
Herbert Xua9e62fa2006-08-21 21:39:24 +1000555out:
556 return ret;
557
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000558ctr_mem_err:
559 crypto_unregister_alg(&ctr_des3_alg);
560ctr_des3_err:
561 crypto_unregister_alg(&ctr_des_alg);
562ctr_des_err:
563 crypto_unregister_alg(&cbc_des3_alg);
Jan Glauber98971f82011-04-19 21:29:15 +0200564cbc_des3_err:
565 crypto_unregister_alg(&ecb_des3_alg);
566ecb_des3_err:
567 crypto_unregister_alg(&des3_alg);
568des3_err:
Herbert Xua9e62fa2006-08-21 21:39:24 +1000569 crypto_unregister_alg(&cbc_des_alg);
570cbc_des_err:
571 crypto_unregister_alg(&ecb_des_alg);
572ecb_des_err:
573 crypto_unregister_alg(&des_alg);
574des_err:
575 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576}
577
Jan Glauber1efbd152010-05-21 22:04:46 +1000578static void __exit des_s390_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000580 if (ctrblk) {
581 crypto_unregister_alg(&ctr_des_alg);
582 crypto_unregister_alg(&ctr_des3_alg);
583 free_page((unsigned long) ctrblk);
584 }
Jan Glauber98971f82011-04-19 21:29:15 +0200585 crypto_unregister_alg(&cbc_des3_alg);
586 crypto_unregister_alg(&ecb_des3_alg);
587 crypto_unregister_alg(&des3_alg);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000588 crypto_unregister_alg(&cbc_des_alg);
589 crypto_unregister_alg(&ecb_des_alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 crypto_unregister_alg(&des_alg);
591}
592
Heiko Carstens9f7819c2008-04-17 07:46:17 +0200593module_init(des_s390_init);
Jan Glauber1efbd152010-05-21 22:04:46 +1000594module_exit(des_s390_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596MODULE_ALIAS("des");
597MODULE_ALIAS("des3_ede");
598
599MODULE_LICENSE("GPL");
600MODULE_DESCRIPTION("DES & Triple DES EDE Cipher Algorithms");