aboutsummaryrefslogtreecommitdiffstats
path: root/arch/s390/crypto/des_s390.c
diff options
context:
space:
mode:
Diffstat (limited to 'arch/s390/crypto/des_s390.c')
-rw-r--r--arch/s390/crypto/des_s390.c370
1 files changed, 265 insertions, 105 deletions
diff --git a/arch/s390/crypto/des_s390.c b/arch/s390/crypto/des_s390.c
index cc5420118393..a52bfd124d86 100644
--- a/arch/s390/crypto/des_s390.c
+++ b/arch/s390/crypto/des_s390.c
@@ -3,7 +3,7 @@
3 * 3 *
4 * s390 implementation of the DES Cipher Algorithm. 4 * s390 implementation of the DES Cipher Algorithm.
5 * 5 *
6 * Copyright IBM Corp. 2003,2007 6 * Copyright IBM Corp. 2003,2011
7 * Author(s): Thomas Spatzier 7 * Author(s): Thomas Spatzier
8 * Jan Glauber (jan.glauber@de.ibm.com) 8 * Jan Glauber (jan.glauber@de.ibm.com)
9 * 9 *
@@ -22,22 +22,19 @@
22 22
23#include "crypt_s390.h" 23#include "crypt_s390.h"
24 24
25#define DES3_192_KEY_SIZE (3 * DES_KEY_SIZE) 25#define DES3_KEY_SIZE (3 * DES_KEY_SIZE)
26 26
27struct crypt_s390_des_ctx { 27static u8 *ctrblk;
28 u8 iv[DES_BLOCK_SIZE];
29 u8 key[DES_KEY_SIZE];
30};
31 28
32struct crypt_s390_des3_192_ctx { 29struct s390_des_ctx {
33 u8 iv[DES_BLOCK_SIZE]; 30 u8 iv[DES_BLOCK_SIZE];
34 u8 key[DES3_192_KEY_SIZE]; 31 u8 key[DES3_KEY_SIZE];
35}; 32};
36 33
37static int des_setkey(struct crypto_tfm *tfm, const u8 *key, 34static int des_setkey(struct crypto_tfm *tfm, const u8 *key,
38 unsigned int keylen) 35 unsigned int key_len)
39{ 36{
40 struct crypt_s390_des_ctx *dctx = crypto_tfm_ctx(tfm); 37 struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
41 u32 *flags = &tfm->crt_flags; 38 u32 *flags = &tfm->crt_flags;
42 u32 tmp[DES_EXPKEY_WORDS]; 39 u32 tmp[DES_EXPKEY_WORDS];
43 40
@@ -47,22 +44,22 @@ static int des_setkey(struct crypto_tfm *tfm, const u8 *key,
47 return -EINVAL; 44 return -EINVAL;
48 } 45 }
49 46
50 memcpy(dctx->key, key, keylen); 47 memcpy(ctx->key, key, key_len);
51 return 0; 48 return 0;
52} 49}
53 50
54static void des_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in) 51static void des_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
55{ 52{
56 struct crypt_s390_des_ctx *dctx = crypto_tfm_ctx(tfm); 53 struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
57 54
58 crypt_s390_km(KM_DEA_ENCRYPT, dctx->key, out, in, DES_BLOCK_SIZE); 55 crypt_s390_km(KM_DEA_ENCRYPT, ctx->key, out, in, DES_BLOCK_SIZE);
59} 56}
60 57
61static void des_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in) 58static void des_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
62{ 59{
63 struct crypt_s390_des_ctx *dctx = crypto_tfm_ctx(tfm); 60 struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
64 61
65 crypt_s390_km(KM_DEA_DECRYPT, dctx->key, out, in, DES_BLOCK_SIZE); 62 crypt_s390_km(KM_DEA_DECRYPT, ctx->key, out, in, DES_BLOCK_SIZE);
66} 63}
67 64
68static struct crypto_alg des_alg = { 65static struct crypto_alg des_alg = {
@@ -71,7 +68,7 @@ static struct crypto_alg des_alg = {
71 .cra_priority = CRYPT_S390_PRIORITY, 68 .cra_priority = CRYPT_S390_PRIORITY,
72 .cra_flags = CRYPTO_ALG_TYPE_CIPHER, 69 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
73 .cra_blocksize = DES_BLOCK_SIZE, 70 .cra_blocksize = DES_BLOCK_SIZE,
74 .cra_ctxsize = sizeof(struct crypt_s390_des_ctx), 71 .cra_ctxsize = sizeof(struct s390_des_ctx),
75 .cra_module = THIS_MODULE, 72 .cra_module = THIS_MODULE,
76 .cra_list = LIST_HEAD_INIT(des_alg.cra_list), 73 .cra_list = LIST_HEAD_INIT(des_alg.cra_list),
77 .cra_u = { 74 .cra_u = {
@@ -86,7 +83,7 @@ static struct crypto_alg des_alg = {
86}; 83};
87 84
88static int ecb_desall_crypt(struct blkcipher_desc *desc, long func, 85static int ecb_desall_crypt(struct blkcipher_desc *desc, long func,
89 void *param, struct blkcipher_walk *walk) 86 u8 *key, struct blkcipher_walk *walk)
90{ 87{
91 int ret = blkcipher_walk_virt(desc, walk); 88 int ret = blkcipher_walk_virt(desc, walk);
92 unsigned int nbytes; 89 unsigned int nbytes;
@@ -97,7 +94,7 @@ static int ecb_desall_crypt(struct blkcipher_desc *desc, long func,
97 u8 *out = walk->dst.virt.addr; 94 u8 *out = walk->dst.virt.addr;
98 u8 *in = walk->src.virt.addr; 95 u8 *in = walk->src.virt.addr;
99 96
100 ret = crypt_s390_km(func, param, out, in, n); 97 ret = crypt_s390_km(func, key, out, in, n);
101 BUG_ON((ret < 0) || (ret != n)); 98 BUG_ON((ret < 0) || (ret != n));
102 99
103 nbytes &= DES_BLOCK_SIZE - 1; 100 nbytes &= DES_BLOCK_SIZE - 1;
@@ -108,7 +105,7 @@ static int ecb_desall_crypt(struct blkcipher_desc *desc, long func,
108} 105}
109 106
110static int cbc_desall_crypt(struct blkcipher_desc *desc, long func, 107static int cbc_desall_crypt(struct blkcipher_desc *desc, long func,
111 void *param, struct blkcipher_walk *walk) 108 u8 *iv, struct blkcipher_walk *walk)
112{ 109{
113 int ret = blkcipher_walk_virt(desc, walk); 110 int ret = blkcipher_walk_virt(desc, walk);
114 unsigned int nbytes = walk->nbytes; 111 unsigned int nbytes = walk->nbytes;
@@ -116,20 +113,20 @@ static int cbc_desall_crypt(struct blkcipher_desc *desc, long func,
116 if (!nbytes) 113 if (!nbytes)
117 goto out; 114 goto out;
118 115
119 memcpy(param, walk->iv, DES_BLOCK_SIZE); 116 memcpy(iv, walk->iv, DES_BLOCK_SIZE);
120 do { 117 do {
121 /* only use complete blocks */ 118 /* only use complete blocks */
122 unsigned int n = nbytes & ~(DES_BLOCK_SIZE - 1); 119 unsigned int n = nbytes & ~(DES_BLOCK_SIZE - 1);
123 u8 *out = walk->dst.virt.addr; 120 u8 *out = walk->dst.virt.addr;
124 u8 *in = walk->src.virt.addr; 121 u8 *in = walk->src.virt.addr;
125 122
126 ret = crypt_s390_kmc(func, param, out, in, n); 123 ret = crypt_s390_kmc(func, iv, out, in, n);
127 BUG_ON((ret < 0) || (ret != n)); 124 BUG_ON((ret < 0) || (ret != n));
128 125
129 nbytes &= DES_BLOCK_SIZE - 1; 126 nbytes &= DES_BLOCK_SIZE - 1;
130 ret = blkcipher_walk_done(desc, walk, nbytes); 127 ret = blkcipher_walk_done(desc, walk, nbytes);
131 } while ((nbytes = walk->nbytes)); 128 } while ((nbytes = walk->nbytes));
132 memcpy(walk->iv, param, DES_BLOCK_SIZE); 129 memcpy(walk->iv, iv, DES_BLOCK_SIZE);
133 130
134out: 131out:
135 return ret; 132 return ret;
@@ -139,22 +136,22 @@ static int ecb_des_encrypt(struct blkcipher_desc *desc,
139 struct scatterlist *dst, struct scatterlist *src, 136 struct scatterlist *dst, struct scatterlist *src,
140 unsigned int nbytes) 137 unsigned int nbytes)
141{ 138{
142 struct crypt_s390_des_ctx *sctx = crypto_blkcipher_ctx(desc->tfm); 139 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
143 struct blkcipher_walk walk; 140 struct blkcipher_walk walk;
144 141
145 blkcipher_walk_init(&walk, dst, src, nbytes); 142 blkcipher_walk_init(&walk, dst, src, nbytes);
146 return ecb_desall_crypt(desc, KM_DEA_ENCRYPT, sctx->key, &walk); 143 return ecb_desall_crypt(desc, KM_DEA_ENCRYPT, ctx->key, &walk);
147} 144}
148 145
149static int ecb_des_decrypt(struct blkcipher_desc *desc, 146static int ecb_des_decrypt(struct blkcipher_desc *desc,
150 struct scatterlist *dst, struct scatterlist *src, 147 struct scatterlist *dst, struct scatterlist *src,
151 unsigned int nbytes) 148 unsigned int nbytes)
152{ 149{
153 struct crypt_s390_des_ctx *sctx = crypto_blkcipher_ctx(desc->tfm); 150 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
154 struct blkcipher_walk walk; 151 struct blkcipher_walk walk;
155 152
156 blkcipher_walk_init(&walk, dst, src, nbytes); 153 blkcipher_walk_init(&walk, dst, src, nbytes);
157 return ecb_desall_crypt(desc, KM_DEA_DECRYPT, sctx->key, &walk); 154 return ecb_desall_crypt(desc, KM_DEA_DECRYPT, ctx->key, &walk);
158} 155}
159 156
160static struct crypto_alg ecb_des_alg = { 157static struct crypto_alg ecb_des_alg = {
@@ -163,7 +160,7 @@ static struct crypto_alg ecb_des_alg = {
163 .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY, 160 .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
164 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER, 161 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
165 .cra_blocksize = DES_BLOCK_SIZE, 162 .cra_blocksize = DES_BLOCK_SIZE,
166 .cra_ctxsize = sizeof(struct crypt_s390_des_ctx), 163 .cra_ctxsize = sizeof(struct s390_des_ctx),
167 .cra_type = &crypto_blkcipher_type, 164 .cra_type = &crypto_blkcipher_type,
168 .cra_module = THIS_MODULE, 165 .cra_module = THIS_MODULE,
169 .cra_list = LIST_HEAD_INIT(ecb_des_alg.cra_list), 166 .cra_list = LIST_HEAD_INIT(ecb_des_alg.cra_list),
@@ -182,22 +179,22 @@ static int cbc_des_encrypt(struct blkcipher_desc *desc,
182 struct scatterlist *dst, struct scatterlist *src, 179 struct scatterlist *dst, struct scatterlist *src,
183 unsigned int nbytes) 180 unsigned int nbytes)
184{ 181{
185 struct crypt_s390_des_ctx *sctx = crypto_blkcipher_ctx(desc->tfm); 182 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
186 struct blkcipher_walk walk; 183 struct blkcipher_walk walk;
187 184
188 blkcipher_walk_init(&walk, dst, src, nbytes); 185 blkcipher_walk_init(&walk, dst, src, nbytes);
189 return cbc_desall_crypt(desc, KMC_DEA_ENCRYPT, sctx->iv, &walk); 186 return cbc_desall_crypt(desc, KMC_DEA_ENCRYPT, ctx->iv, &walk);
190} 187}
191 188
192static int cbc_des_decrypt(struct blkcipher_desc *desc, 189static int cbc_des_decrypt(struct blkcipher_desc *desc,
193 struct scatterlist *dst, struct scatterlist *src, 190 struct scatterlist *dst, struct scatterlist *src,
194 unsigned int nbytes) 191 unsigned int nbytes)
195{ 192{
196 struct crypt_s390_des_ctx *sctx = crypto_blkcipher_ctx(desc->tfm); 193 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
197 struct blkcipher_walk walk; 194 struct blkcipher_walk walk;
198 195
199 blkcipher_walk_init(&walk, dst, src, nbytes); 196 blkcipher_walk_init(&walk, dst, src, nbytes);
200 return cbc_desall_crypt(desc, KMC_DEA_DECRYPT, sctx->iv, &walk); 197 return cbc_desall_crypt(desc, KMC_DEA_DECRYPT, ctx->iv, &walk);
201} 198}
202 199
203static struct crypto_alg cbc_des_alg = { 200static struct crypto_alg cbc_des_alg = {
@@ -206,7 +203,7 @@ static struct crypto_alg cbc_des_alg = {
206 .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY, 203 .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
207 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER, 204 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
208 .cra_blocksize = DES_BLOCK_SIZE, 205 .cra_blocksize = DES_BLOCK_SIZE,
209 .cra_ctxsize = sizeof(struct crypt_s390_des_ctx), 206 .cra_ctxsize = sizeof(struct s390_des_ctx),
210 .cra_type = &crypto_blkcipher_type, 207 .cra_type = &crypto_blkcipher_type,
211 .cra_module = THIS_MODULE, 208 .cra_module = THIS_MODULE,
212 .cra_list = LIST_HEAD_INIT(cbc_des_alg.cra_list), 209 .cra_list = LIST_HEAD_INIT(cbc_des_alg.cra_list),
@@ -235,10 +232,10 @@ static struct crypto_alg cbc_des_alg = {
235 * property. 232 * property.
236 * 233 *
237 */ 234 */
238static int des3_192_setkey(struct crypto_tfm *tfm, const u8 *key, 235static int des3_setkey(struct crypto_tfm *tfm, const u8 *key,
239 unsigned int keylen) 236 unsigned int key_len)
240{ 237{
241 struct crypt_s390_des3_192_ctx *dctx = crypto_tfm_ctx(tfm); 238 struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
242 u32 *flags = &tfm->crt_flags; 239 u32 *flags = &tfm->crt_flags;
243 240
244 if (!(memcmp(key, &key[DES_KEY_SIZE], DES_KEY_SIZE) && 241 if (!(memcmp(key, &key[DES_KEY_SIZE], DES_KEY_SIZE) &&
@@ -248,141 +245,276 @@ static int des3_192_setkey(struct crypto_tfm *tfm, const u8 *key,
248 *flags |= CRYPTO_TFM_RES_WEAK_KEY; 245 *flags |= CRYPTO_TFM_RES_WEAK_KEY;
249 return -EINVAL; 246 return -EINVAL;
250 } 247 }
251 memcpy(dctx->key, key, keylen); 248 memcpy(ctx->key, key, key_len);
252 return 0; 249 return 0;
253} 250}
254 251
255static void des3_192_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src) 252static void des3_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
256{ 253{
257 struct crypt_s390_des3_192_ctx *dctx = crypto_tfm_ctx(tfm); 254 struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
258 255
259 crypt_s390_km(KM_TDEA_192_ENCRYPT, dctx->key, dst, (void*)src, 256 crypt_s390_km(KM_TDEA_192_ENCRYPT, ctx->key, dst, src, DES_BLOCK_SIZE);
260 DES_BLOCK_SIZE);
261} 257}
262 258
263static void des3_192_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src) 259static void des3_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
264{ 260{
265 struct crypt_s390_des3_192_ctx *dctx = crypto_tfm_ctx(tfm); 261 struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
266 262
267 crypt_s390_km(KM_TDEA_192_DECRYPT, dctx->key, dst, (void*)src, 263 crypt_s390_km(KM_TDEA_192_DECRYPT, ctx->key, dst, src, DES_BLOCK_SIZE);
268 DES_BLOCK_SIZE);
269} 264}
270 265
271static struct crypto_alg des3_192_alg = { 266static struct crypto_alg des3_alg = {
272 .cra_name = "des3_ede", 267 .cra_name = "des3_ede",
273 .cra_driver_name = "des3_ede-s390", 268 .cra_driver_name = "des3_ede-s390",
274 .cra_priority = CRYPT_S390_PRIORITY, 269 .cra_priority = CRYPT_S390_PRIORITY,
275 .cra_flags = CRYPTO_ALG_TYPE_CIPHER, 270 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
276 .cra_blocksize = DES_BLOCK_SIZE, 271 .cra_blocksize = DES_BLOCK_SIZE,
277 .cra_ctxsize = sizeof(struct crypt_s390_des3_192_ctx), 272 .cra_ctxsize = sizeof(struct s390_des_ctx),
278 .cra_module = THIS_MODULE, 273 .cra_module = THIS_MODULE,
279 .cra_list = LIST_HEAD_INIT(des3_192_alg.cra_list), 274 .cra_list = LIST_HEAD_INIT(des3_alg.cra_list),
280 .cra_u = { 275 .cra_u = {
281 .cipher = { 276 .cipher = {
282 .cia_min_keysize = DES3_192_KEY_SIZE, 277 .cia_min_keysize = DES3_KEY_SIZE,
283 .cia_max_keysize = DES3_192_KEY_SIZE, 278 .cia_max_keysize = DES3_KEY_SIZE,
284 .cia_setkey = des3_192_setkey, 279 .cia_setkey = des3_setkey,
285 .cia_encrypt = des3_192_encrypt, 280 .cia_encrypt = des3_encrypt,
286 .cia_decrypt = des3_192_decrypt, 281 .cia_decrypt = des3_decrypt,
287 } 282 }
288 } 283 }
289}; 284};
290 285
291static int ecb_des3_192_encrypt(struct blkcipher_desc *desc, 286static int ecb_des3_encrypt(struct blkcipher_desc *desc,
292 struct scatterlist *dst, 287 struct scatterlist *dst, struct scatterlist *src,
293 struct scatterlist *src, unsigned int nbytes) 288 unsigned int nbytes)
294{ 289{
295 struct crypt_s390_des3_192_ctx *sctx = crypto_blkcipher_ctx(desc->tfm); 290 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
296 struct blkcipher_walk walk; 291 struct blkcipher_walk walk;
297 292
298 blkcipher_walk_init(&walk, dst, src, nbytes); 293 blkcipher_walk_init(&walk, dst, src, nbytes);
299 return ecb_desall_crypt(desc, KM_TDEA_192_ENCRYPT, sctx->key, &walk); 294 return ecb_desall_crypt(desc, KM_TDEA_192_ENCRYPT, ctx->key, &walk);
300} 295}
301 296
302static int ecb_des3_192_decrypt(struct blkcipher_desc *desc, 297static int ecb_des3_decrypt(struct blkcipher_desc *desc,
303 struct scatterlist *dst, 298 struct scatterlist *dst, struct scatterlist *src,
304 struct scatterlist *src, unsigned int nbytes) 299 unsigned int nbytes)
305{ 300{
306 struct crypt_s390_des3_192_ctx *sctx = crypto_blkcipher_ctx(desc->tfm); 301 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
307 struct blkcipher_walk walk; 302 struct blkcipher_walk walk;
308 303
309 blkcipher_walk_init(&walk, dst, src, nbytes); 304 blkcipher_walk_init(&walk, dst, src, nbytes);
310 return ecb_desall_crypt(desc, KM_TDEA_192_DECRYPT, sctx->key, &walk); 305 return ecb_desall_crypt(desc, KM_TDEA_192_DECRYPT, ctx->key, &walk);
311} 306}
312 307
313static struct crypto_alg ecb_des3_192_alg = { 308static struct crypto_alg ecb_des3_alg = {
314 .cra_name = "ecb(des3_ede)", 309 .cra_name = "ecb(des3_ede)",
315 .cra_driver_name = "ecb-des3_ede-s390", 310 .cra_driver_name = "ecb-des3_ede-s390",
316 .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY, 311 .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
317 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER, 312 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
318 .cra_blocksize = DES_BLOCK_SIZE, 313 .cra_blocksize = DES_BLOCK_SIZE,
319 .cra_ctxsize = sizeof(struct crypt_s390_des3_192_ctx), 314 .cra_ctxsize = sizeof(struct s390_des_ctx),
320 .cra_type = &crypto_blkcipher_type, 315 .cra_type = &crypto_blkcipher_type,
321 .cra_module = THIS_MODULE, 316 .cra_module = THIS_MODULE,
322 .cra_list = LIST_HEAD_INIT( 317 .cra_list = LIST_HEAD_INIT(
323 ecb_des3_192_alg.cra_list), 318 ecb_des3_alg.cra_list),
324 .cra_u = { 319 .cra_u = {
325 .blkcipher = { 320 .blkcipher = {
326 .min_keysize = DES3_192_KEY_SIZE, 321 .min_keysize = DES3_KEY_SIZE,
327 .max_keysize = DES3_192_KEY_SIZE, 322 .max_keysize = DES3_KEY_SIZE,
328 .setkey = des3_192_setkey, 323 .setkey = des3_setkey,
329 .encrypt = ecb_des3_192_encrypt, 324 .encrypt = ecb_des3_encrypt,
330 .decrypt = ecb_des3_192_decrypt, 325 .decrypt = ecb_des3_decrypt,
331 } 326 }
332 } 327 }
333}; 328};
334 329
335static int cbc_des3_192_encrypt(struct blkcipher_desc *desc, 330static int cbc_des3_encrypt(struct blkcipher_desc *desc,
336 struct scatterlist *dst, 331 struct scatterlist *dst, struct scatterlist *src,
337 struct scatterlist *src, unsigned int nbytes) 332 unsigned int nbytes)
338{ 333{
339 struct crypt_s390_des3_192_ctx *sctx = crypto_blkcipher_ctx(desc->tfm); 334 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
340 struct blkcipher_walk walk; 335 struct blkcipher_walk walk;
341 336
342 blkcipher_walk_init(&walk, dst, src, nbytes); 337 blkcipher_walk_init(&walk, dst, src, nbytes);
343 return cbc_desall_crypt(desc, KMC_TDEA_192_ENCRYPT, sctx->iv, &walk); 338 return cbc_desall_crypt(desc, KMC_TDEA_192_ENCRYPT, ctx->iv, &walk);
344} 339}
345 340
346static int cbc_des3_192_decrypt(struct blkcipher_desc *desc, 341static int cbc_des3_decrypt(struct blkcipher_desc *desc,
347 struct scatterlist *dst, 342 struct scatterlist *dst, struct scatterlist *src,
348 struct scatterlist *src, unsigned int nbytes) 343 unsigned int nbytes)
349{ 344{
350 struct crypt_s390_des3_192_ctx *sctx = crypto_blkcipher_ctx(desc->tfm); 345 struct s390_des_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
351 struct blkcipher_walk walk; 346 struct blkcipher_walk walk;
352 347
353 blkcipher_walk_init(&walk, dst, src, nbytes); 348 blkcipher_walk_init(&walk, dst, src, nbytes);
354 return cbc_desall_crypt(desc, KMC_TDEA_192_DECRYPT, sctx->iv, &walk); 349 return cbc_desall_crypt(desc, KMC_TDEA_192_DECRYPT, ctx->iv, &walk);
355} 350}
356 351
357static struct crypto_alg cbc_des3_192_alg = { 352static struct crypto_alg cbc_des3_alg = {
358 .cra_name = "cbc(des3_ede)", 353 .cra_name = "cbc(des3_ede)",
359 .cra_driver_name = "cbc-des3_ede-s390", 354 .cra_driver_name = "cbc-des3_ede-s390",
360 .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY, 355 .cra_priority = CRYPT_S390_COMPOSITE_PRIORITY,
361 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER, 356 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
362 .cra_blocksize = DES_BLOCK_SIZE, 357 .cra_blocksize = DES_BLOCK_SIZE,
363 .cra_ctxsize = sizeof(struct crypt_s390_des3_192_ctx), 358 .cra_ctxsize = sizeof(struct s390_des_ctx),
364 .cra_type = &crypto_blkcipher_type, 359 .cra_type = &crypto_blkcipher_type,
365 .cra_module = THIS_MODULE, 360 .cra_module = THIS_MODULE,
366 .cra_list = LIST_HEAD_INIT( 361 .cra_list = LIST_HEAD_INIT(
367 cbc_des3_192_alg.cra_list), 362 cbc_des3_alg.cra_list),
368 .cra_u = { 363 .cra_u = {
369 .blkcipher = { 364 .blkcipher = {
370 .min_keysize = DES3_192_KEY_SIZE, 365 .min_keysize = DES3_KEY_SIZE,
371 .max_keysize = DES3_192_KEY_SIZE, 366 .max_keysize = DES3_KEY_SIZE,
372 .ivsize = DES_BLOCK_SIZE, 367 .ivsize = DES_BLOCK_SIZE,
373 .setkey = des3_192_setkey, 368 .setkey = des3_setkey,
374 .encrypt = cbc_des3_192_encrypt, 369 .encrypt = cbc_des3_encrypt,
375 .decrypt = cbc_des3_192_decrypt, 370 .decrypt = cbc_des3_decrypt,
376 } 371 }
377 } 372 }
378}; 373};
379 374
380static int des_s390_init(void) 375static 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
512static int __init des_s390_init(void)
381{ 513{
382 int ret; 514 int ret;
383 515
384 if (!crypt_s390_func_available(KM_DEA_ENCRYPT) || 516 if (!crypt_s390_func_available(KM_DEA_ENCRYPT, CRYPT_S390_MSA) ||
385 !crypt_s390_func_available(KM_TDEA_192_ENCRYPT)) 517 !crypt_s390_func_available(KM_TDEA_192_ENCRYPT, CRYPT_S390_MSA))
386 return -EOPNOTSUPP; 518 return -EOPNOTSUPP;
387 519
388 ret = crypto_register_alg(&des_alg); 520 ret = crypto_register_alg(&des_alg);
@@ -394,23 +526,46 @@ static int des_s390_init(void)
394 ret = crypto_register_alg(&cbc_des_alg); 526 ret = crypto_register_alg(&cbc_des_alg);
395 if (ret) 527 if (ret)
396 goto cbc_des_err; 528 goto cbc_des_err;
397 ret = crypto_register_alg(&des3_192_alg); 529 ret = crypto_register_alg(&des3_alg);
398 if (ret) 530 if (ret)
399 goto des3_192_err; 531 goto des3_err;
400 ret = crypto_register_alg(&ecb_des3_192_alg); 532 ret = crypto_register_alg(&ecb_des3_alg);
401 if (ret) 533 if (ret)
402 goto ecb_des3_192_err; 534 goto ecb_des3_err;
403 ret = crypto_register_alg(&cbc_des3_192_alg); 535 ret = crypto_register_alg(&cbc_des3_alg);
404 if (ret) 536 if (ret)
405 goto cbc_des3_192_err; 537 goto cbc_des3_err;
538
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 }
406out: 555out:
407 return ret; 556 return ret;
408 557
409cbc_des3_192_err: 558ctr_mem_err:
410 crypto_unregister_alg(&ecb_des3_192_alg); 559 crypto_unregister_alg(&ctr_des3_alg);
411ecb_des3_192_err: 560ctr_des3_err:
412 crypto_unregister_alg(&des3_192_alg); 561 crypto_unregister_alg(&ctr_des_alg);
413des3_192_err: 562ctr_des_err:
563 crypto_unregister_alg(&cbc_des3_alg);
564cbc_des3_err:
565 crypto_unregister_alg(&ecb_des3_alg);
566ecb_des3_err:
567 crypto_unregister_alg(&des3_alg);
568des3_err:
414 crypto_unregister_alg(&cbc_des_alg); 569 crypto_unregister_alg(&cbc_des_alg);
415cbc_des_err: 570cbc_des_err:
416 crypto_unregister_alg(&ecb_des_alg); 571 crypto_unregister_alg(&ecb_des_alg);
@@ -422,9 +577,14 @@ des_err:
422 577
423static void __exit des_s390_exit(void) 578static void __exit des_s390_exit(void)
424{ 579{
425 crypto_unregister_alg(&cbc_des3_192_alg); 580 if (ctrblk) {
426 crypto_unregister_alg(&ecb_des3_192_alg); 581 crypto_unregister_alg(&ctr_des_alg);
427 crypto_unregister_alg(&des3_192_alg); 582 crypto_unregister_alg(&ctr_des3_alg);
583 free_page((unsigned long) ctrblk);
584 }
585 crypto_unregister_alg(&cbc_des3_alg);
586 crypto_unregister_alg(&ecb_des3_alg);
587 crypto_unregister_alg(&des3_alg);
428 crypto_unregister_alg(&cbc_des_alg); 588 crypto_unregister_alg(&cbc_des_alg);
429 crypto_unregister_alg(&ecb_des_alg); 589 crypto_unregister_alg(&ecb_des_alg);
430 crypto_unregister_alg(&des_alg); 590 crypto_unregister_alg(&des_alg);