[CRYPTO] all: Pass tfm instead of ctx to algorithms
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / arch / s390 / crypto / des_s390.c
CommitLineData
1da177e4
LT
1/*
2 * Cryptographic API.
3 *
c1e26e1e 4 * s390 implementation of the DES Cipher Algorithm.
1da177e4
LT
5 *
6 * Copyright (c) 2003 IBM Deutschland Entwicklung GmbH, IBM Corporation
7 * Author(s): Thomas Spatzier (tspat@de.ibm.com)
8 *
9 *
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 */
16#include <linux/init.h>
17#include <linux/module.h>
1da177e4 18#include <linux/crypto.h>
c1357833 19
c1e26e1e 20#include "crypt_s390.h"
1da177e4
LT
21#include "crypto_des.h"
22
23#define DES_BLOCK_SIZE 8
24#define DES_KEY_SIZE 8
25
26#define DES3_128_KEY_SIZE (2 * DES_KEY_SIZE)
27#define DES3_128_BLOCK_SIZE DES_BLOCK_SIZE
28
29#define DES3_192_KEY_SIZE (3 * DES_KEY_SIZE)
30#define DES3_192_BLOCK_SIZE DES_BLOCK_SIZE
31
c1e26e1e 32struct crypt_s390_des_ctx {
1da177e4
LT
33 u8 iv[DES_BLOCK_SIZE];
34 u8 key[DES_KEY_SIZE];
35};
36
c1e26e1e 37struct crypt_s390_des3_128_ctx {
1da177e4
LT
38 u8 iv[DES_BLOCK_SIZE];
39 u8 key[DES3_128_KEY_SIZE];
40};
41
c1e26e1e 42struct crypt_s390_des3_192_ctx {
1da177e4
LT
43 u8 iv[DES_BLOCK_SIZE];
44 u8 key[DES3_192_KEY_SIZE];
45};
46
6c2bb98b
HX
47static int des_setkey(struct crypto_tfm *tfm, const u8 *key,
48 unsigned int keylen, u32 *flags)
1da177e4 49{
6c2bb98b 50 struct crypt_s390_des_ctx *dctx = crypto_tfm_ctx(tfm);
1da177e4
LT
51 int ret;
52
c1357833 53 /* test if key is valid (not a weak key) */
1da177e4 54 ret = crypto_des_check_key(key, keylen, flags);
c1357833 55 if (ret == 0)
1da177e4 56 memcpy(dctx->key, key, keylen);
1da177e4
LT
57 return ret;
58}
59
6c2bb98b 60static void des_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
1da177e4 61{
6c2bb98b 62 struct crypt_s390_des_ctx *dctx = crypto_tfm_ctx(tfm);
1da177e4 63
b8dc6038 64 crypt_s390_km(KM_DEA_ENCRYPT, dctx->key, out, in, DES_BLOCK_SIZE);
1da177e4
LT
65}
66
6c2bb98b 67static void des_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
1da177e4 68{
6c2bb98b 69 struct crypt_s390_des_ctx *dctx = crypto_tfm_ctx(tfm);
1da177e4 70
b8dc6038
JG
71 crypt_s390_km(KM_DEA_DECRYPT, dctx->key, out, in, DES_BLOCK_SIZE);
72}
73
74static unsigned int des_encrypt_ecb(const struct cipher_desc *desc, u8 *out,
75 const u8 *in, unsigned int nbytes)
76{
77 struct crypt_s390_des_ctx *sctx = crypto_tfm_ctx(desc->tfm);
78 int ret;
79
80 /* only use complete blocks */
81 nbytes &= ~(DES_BLOCK_SIZE - 1);
82 ret = crypt_s390_km(KM_DEA_ENCRYPT, sctx->key, out, in, nbytes);
83 BUG_ON((ret < 0) || (ret != nbytes));
84
85 return nbytes;
86}
87
88static unsigned int des_decrypt_ecb(const struct cipher_desc *desc, u8 *out,
89 const u8 *in, unsigned int nbytes)
90{
91 struct crypt_s390_des_ctx *sctx = crypto_tfm_ctx(desc->tfm);
92 int ret;
93
94 /* only use complete blocks */
95 nbytes &= ~(DES_BLOCK_SIZE - 1);
96 ret = crypt_s390_km(KM_DEA_DECRYPT, sctx->key, out, in, nbytes);
97 BUG_ON((ret < 0) || (ret != nbytes));
98
99 return nbytes;
100}
101
102static unsigned int des_encrypt_cbc(const struct cipher_desc *desc, u8 *out,
103 const u8 *in, unsigned int nbytes)
104{
105 struct crypt_s390_des_ctx *sctx = crypto_tfm_ctx(desc->tfm);
106 int ret;
107
108 /* only use complete blocks */
109 nbytes &= ~(DES_BLOCK_SIZE - 1);
110
111 memcpy(sctx->iv, desc->info, DES_BLOCK_SIZE);
112 ret = crypt_s390_kmc(KMC_DEA_ENCRYPT, &sctx->iv, out, in, nbytes);
113 BUG_ON((ret < 0) || (ret != nbytes));
114
115 memcpy(desc->info, sctx->iv, DES_BLOCK_SIZE);
116 return nbytes;
117}
118
119static unsigned int des_decrypt_cbc(const struct cipher_desc *desc, u8 *out,
120 const u8 *in, unsigned int nbytes)
121{
122 struct crypt_s390_des_ctx *sctx = crypto_tfm_ctx(desc->tfm);
123 int ret;
124
125 /* only use complete blocks */
126 nbytes &= ~(DES_BLOCK_SIZE - 1);
127
128 memcpy(&sctx->iv, desc->info, DES_BLOCK_SIZE);
129 ret = crypt_s390_kmc(KMC_DEA_DECRYPT, &sctx->iv, out, in, nbytes);
130 BUG_ON((ret < 0) || (ret != nbytes));
131
132 return nbytes;
1da177e4
LT
133}
134
135static struct crypto_alg des_alg = {
136 .cra_name = "des",
137 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
138 .cra_blocksize = DES_BLOCK_SIZE,
c1e26e1e 139 .cra_ctxsize = sizeof(struct crypt_s390_des_ctx),
1da177e4
LT
140 .cra_module = THIS_MODULE,
141 .cra_list = LIST_HEAD_INIT(des_alg.cra_list),
c1357833
JG
142 .cra_u = {
143 .cipher = {
144 .cia_min_keysize = DES_KEY_SIZE,
145 .cia_max_keysize = DES_KEY_SIZE,
146 .cia_setkey = des_setkey,
147 .cia_encrypt = des_encrypt,
b8dc6038
JG
148 .cia_decrypt = des_decrypt,
149 .cia_encrypt_ecb = des_encrypt_ecb,
150 .cia_decrypt_ecb = des_decrypt_ecb,
151 .cia_encrypt_cbc = des_encrypt_cbc,
152 .cia_decrypt_cbc = des_decrypt_cbc,
c1357833
JG
153 }
154 }
1da177e4
LT
155};
156
157/*
158 * RFC2451:
159 *
160 * For DES-EDE3, there is no known need to reject weak or
161 * complementation keys. Any weakness is obviated by the use of
162 * multiple keys.
163 *
164 * However, if the two independent 64-bit keys are equal,
165 * then the DES3 operation is simply the same as DES.
166 * Implementers MUST reject keys that exhibit this property.
167 *
168 */
6c2bb98b
HX
169static int des3_128_setkey(struct crypto_tfm *tfm, const u8 *key,
170 unsigned int keylen, u32 *flags)
1da177e4
LT
171{
172 int i, ret;
6c2bb98b 173 struct crypt_s390_des3_128_ctx *dctx = crypto_tfm_ctx(tfm);
1da177e4
LT
174 const u8* temp_key = key;
175
1da177e4 176 if (!(memcmp(key, &key[DES_KEY_SIZE], DES_KEY_SIZE))) {
1da177e4
LT
177 *flags |= CRYPTO_TFM_RES_BAD_KEY_SCHED;
178 return -EINVAL;
179 }
c1357833 180 for (i = 0; i < 2; i++, temp_key += DES_KEY_SIZE) {
1da177e4
LT
181 ret = crypto_des_check_key(temp_key, DES_KEY_SIZE, flags);
182 if (ret < 0)
183 return ret;
184 }
185 memcpy(dctx->key, key, keylen);
186 return 0;
187}
188
6c2bb98b 189static void des3_128_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
1da177e4 190{
6c2bb98b 191 struct crypt_s390_des3_128_ctx *dctx = crypto_tfm_ctx(tfm);
1da177e4 192
c1e26e1e 193 crypt_s390_km(KM_TDEA_128_ENCRYPT, dctx->key, dst, (void*)src,
c1357833 194 DES3_128_BLOCK_SIZE);
1da177e4
LT
195}
196
6c2bb98b 197static void des3_128_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
1da177e4 198{
6c2bb98b 199 struct crypt_s390_des3_128_ctx *dctx = crypto_tfm_ctx(tfm);
1da177e4 200
c1e26e1e 201 crypt_s390_km(KM_TDEA_128_DECRYPT, dctx->key, dst, (void*)src,
c1357833 202 DES3_128_BLOCK_SIZE);
1da177e4
LT
203}
204
b8dc6038
JG
205static unsigned int des3_128_encrypt_ecb(const struct cipher_desc *desc,
206 u8 *out, const u8 *in,
207 unsigned int nbytes)
208{
209 struct crypt_s390_des3_128_ctx *sctx = crypto_tfm_ctx(desc->tfm);
210 int ret;
211
212 /* only use complete blocks */
213 nbytes &= ~(DES3_128_BLOCK_SIZE - 1);
214 ret = crypt_s390_km(KM_TDEA_128_ENCRYPT, sctx->key, out, in, nbytes);
215 BUG_ON((ret < 0) || (ret != nbytes));
216
217 return nbytes;
218}
219
220static unsigned int des3_128_decrypt_ecb(const struct cipher_desc *desc,
221 u8 *out, const u8 *in,
222 unsigned int nbytes)
223{
224 struct crypt_s390_des3_128_ctx *sctx = crypto_tfm_ctx(desc->tfm);
225 int ret;
226
227 /* only use complete blocks */
228 nbytes &= ~(DES3_128_BLOCK_SIZE - 1);
229 ret = crypt_s390_km(KM_TDEA_128_DECRYPT, sctx->key, out, in, nbytes);
230 BUG_ON((ret < 0) || (ret != nbytes));
231
232 return nbytes;
233}
234
235static unsigned int des3_128_encrypt_cbc(const struct cipher_desc *desc,
236 u8 *out, const u8 *in,
237 unsigned int nbytes)
238{
239 struct crypt_s390_des3_128_ctx *sctx = crypto_tfm_ctx(desc->tfm);
240 int ret;
241
242 /* only use complete blocks */
243 nbytes &= ~(DES3_128_BLOCK_SIZE - 1);
244
245 memcpy(sctx->iv, desc->info, DES3_128_BLOCK_SIZE);
246 ret = crypt_s390_kmc(KMC_TDEA_128_ENCRYPT, &sctx->iv, out, in, nbytes);
247 BUG_ON((ret < 0) || (ret != nbytes));
248
249 memcpy(desc->info, sctx->iv, DES3_128_BLOCK_SIZE);
250 return nbytes;
251}
252
253static unsigned int des3_128_decrypt_cbc(const struct cipher_desc *desc,
254 u8 *out, const u8 *in,
255 unsigned int nbytes)
256{
257 struct crypt_s390_des3_128_ctx *sctx = crypto_tfm_ctx(desc->tfm);
258 int ret;
259
260 /* only use complete blocks */
261 nbytes &= ~(DES3_128_BLOCK_SIZE - 1);
262
263 memcpy(&sctx->iv, desc->info, DES3_128_BLOCK_SIZE);
264 ret = crypt_s390_kmc(KMC_TDEA_128_DECRYPT, &sctx->iv, out, in, nbytes);
265 BUG_ON((ret < 0) || (ret != nbytes));
266
267 return nbytes;
268}
269
1da177e4
LT
270static struct crypto_alg des3_128_alg = {
271 .cra_name = "des3_ede128",
272 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
273 .cra_blocksize = DES3_128_BLOCK_SIZE,
c1e26e1e 274 .cra_ctxsize = sizeof(struct crypt_s390_des3_128_ctx),
1da177e4
LT
275 .cra_module = THIS_MODULE,
276 .cra_list = LIST_HEAD_INIT(des3_128_alg.cra_list),
c1357833
JG
277 .cra_u = {
278 .cipher = {
279 .cia_min_keysize = DES3_128_KEY_SIZE,
280 .cia_max_keysize = DES3_128_KEY_SIZE,
281 .cia_setkey = des3_128_setkey,
282 .cia_encrypt = des3_128_encrypt,
b8dc6038
JG
283 .cia_decrypt = des3_128_decrypt,
284 .cia_encrypt_ecb = des3_128_encrypt_ecb,
285 .cia_decrypt_ecb = des3_128_decrypt_ecb,
286 .cia_encrypt_cbc = des3_128_encrypt_cbc,
287 .cia_decrypt_cbc = des3_128_decrypt_cbc,
c1357833
JG
288 }
289 }
1da177e4
LT
290};
291
292/*
293 * RFC2451:
294 *
295 * For DES-EDE3, there is no known need to reject weak or
296 * complementation keys. Any weakness is obviated by the use of
297 * multiple keys.
298 *
299 * However, if the first two or last two independent 64-bit keys are
300 * equal (k1 == k2 or k2 == k3), then the DES3 operation is simply the
301 * same as DES. Implementers MUST reject keys that exhibit this
302 * property.
303 *
304 */
6c2bb98b
HX
305static int des3_192_setkey(struct crypto_tfm *tfm, const u8 *key,
306 unsigned int keylen, u32 *flags)
1da177e4
LT
307{
308 int i, ret;
6c2bb98b 309 struct crypt_s390_des3_192_ctx *dctx = crypto_tfm_ctx(tfm);
c1357833 310 const u8* temp_key = key;
1da177e4 311
1da177e4
LT
312 if (!(memcmp(key, &key[DES_KEY_SIZE], DES_KEY_SIZE) &&
313 memcmp(&key[DES_KEY_SIZE], &key[DES_KEY_SIZE * 2],
c1357833 314 DES_KEY_SIZE))) {
1da177e4
LT
315
316 *flags |= CRYPTO_TFM_RES_BAD_KEY_SCHED;
317 return -EINVAL;
318 }
319 for (i = 0; i < 3; i++, temp_key += DES_KEY_SIZE) {
320 ret = crypto_des_check_key(temp_key, DES_KEY_SIZE, flags);
c1357833 321 if (ret < 0)
1da177e4 322 return ret;
1da177e4
LT
323 }
324 memcpy(dctx->key, key, keylen);
325 return 0;
326}
327
6c2bb98b 328static void des3_192_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
1da177e4 329{
6c2bb98b 330 struct crypt_s390_des3_192_ctx *dctx = crypto_tfm_ctx(tfm);
1da177e4 331
c1e26e1e 332 crypt_s390_km(KM_TDEA_192_ENCRYPT, dctx->key, dst, (void*)src,
c1357833 333 DES3_192_BLOCK_SIZE);
1da177e4
LT
334}
335
6c2bb98b 336static void des3_192_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
1da177e4 337{
6c2bb98b 338 struct crypt_s390_des3_192_ctx *dctx = crypto_tfm_ctx(tfm);
1da177e4 339
c1e26e1e 340 crypt_s390_km(KM_TDEA_192_DECRYPT, dctx->key, dst, (void*)src,
c1357833 341 DES3_192_BLOCK_SIZE);
1da177e4
LT
342}
343
b8dc6038
JG
344static unsigned int des3_192_encrypt_ecb(const struct cipher_desc *desc,
345 u8 *out, const u8 *in,
346 unsigned int nbytes)
347{
348 struct crypt_s390_des3_192_ctx *sctx = crypto_tfm_ctx(desc->tfm);
349 int ret;
350
351 /* only use complete blocks */
352 nbytes &= ~(DES3_192_BLOCK_SIZE - 1);
353 ret = crypt_s390_km(KM_TDEA_192_ENCRYPT, sctx->key, out, in, nbytes);
354 BUG_ON((ret < 0) || (ret != nbytes));
355
356 return nbytes;
357}
358
359static unsigned int des3_192_decrypt_ecb(const struct cipher_desc *desc,
360 u8 *out, const u8 *in,
361 unsigned int nbytes)
362{
363 struct crypt_s390_des3_192_ctx *sctx = crypto_tfm_ctx(desc->tfm);
364 int ret;
365
366 /* only use complete blocks */
367 nbytes &= ~(DES3_192_BLOCK_SIZE - 1);
368 ret = crypt_s390_km(KM_TDEA_192_DECRYPT, sctx->key, out, in, nbytes);
369 BUG_ON((ret < 0) || (ret != nbytes));
370
371 return nbytes;
372}
373
374static unsigned int des3_192_encrypt_cbc(const struct cipher_desc *desc,
375 u8 *out, const u8 *in,
376 unsigned int nbytes)
377{
378 struct crypt_s390_des3_192_ctx *sctx = crypto_tfm_ctx(desc->tfm);
379 int ret;
380
381 /* only use complete blocks */
382 nbytes &= ~(DES3_192_BLOCK_SIZE - 1);
383
384 memcpy(sctx->iv, desc->info, DES3_192_BLOCK_SIZE);
385 ret = crypt_s390_kmc(KMC_TDEA_192_ENCRYPT, &sctx->iv, out, in, nbytes);
386 BUG_ON((ret < 0) || (ret != nbytes));
387
388 memcpy(desc->info, sctx->iv, DES3_192_BLOCK_SIZE);
389 return nbytes;
390}
391
392static unsigned int des3_192_decrypt_cbc(const struct cipher_desc *desc,
393 u8 *out, const u8 *in,
394 unsigned int nbytes)
395{
396 struct crypt_s390_des3_192_ctx *sctx = crypto_tfm_ctx(desc->tfm);
397 int ret;
398
399 /* only use complete blocks */
400 nbytes &= ~(DES3_192_BLOCK_SIZE - 1);
401
402 memcpy(&sctx->iv, desc->info, DES3_192_BLOCK_SIZE);
403 ret = crypt_s390_kmc(KMC_TDEA_192_DECRYPT, &sctx->iv, out, in, nbytes);
404 BUG_ON((ret < 0) || (ret != nbytes));
405
406 return nbytes;
407}
408
1da177e4
LT
409static struct crypto_alg des3_192_alg = {
410 .cra_name = "des3_ede",
411 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
412 .cra_blocksize = DES3_192_BLOCK_SIZE,
c1e26e1e 413 .cra_ctxsize = sizeof(struct crypt_s390_des3_192_ctx),
1da177e4
LT
414 .cra_module = THIS_MODULE,
415 .cra_list = LIST_HEAD_INIT(des3_192_alg.cra_list),
c1357833
JG
416 .cra_u = {
417 .cipher = {
418 .cia_min_keysize = DES3_192_KEY_SIZE,
419 .cia_max_keysize = DES3_192_KEY_SIZE,
420 .cia_setkey = des3_192_setkey,
421 .cia_encrypt = des3_192_encrypt,
b8dc6038
JG
422 .cia_decrypt = des3_192_decrypt,
423 .cia_encrypt_ecb = des3_192_encrypt_ecb,
424 .cia_decrypt_ecb = des3_192_decrypt_ecb,
425 .cia_encrypt_cbc = des3_192_encrypt_cbc,
426 .cia_decrypt_cbc = des3_192_decrypt_cbc,
c1357833
JG
427 }
428 }
1da177e4
LT
429};
430
c1357833 431static int init(void)
1da177e4 432{
c1357833 433 int ret = 0;
1da177e4 434
c1e26e1e
JG
435 if (!crypt_s390_func_available(KM_DEA_ENCRYPT) ||
436 !crypt_s390_func_available(KM_TDEA_128_ENCRYPT) ||
c1357833 437 !crypt_s390_func_available(KM_TDEA_192_ENCRYPT))
1da177e4 438 return -ENOSYS;
1da177e4 439
c1357833
JG
440 ret |= (crypto_register_alg(&des_alg) == 0) ? 0:1;
441 ret |= (crypto_register_alg(&des3_128_alg) == 0) ? 0:2;
442 ret |= (crypto_register_alg(&des3_192_alg) == 0) ? 0:4;
443 if (ret) {
1da177e4
LT
444 crypto_unregister_alg(&des3_192_alg);
445 crypto_unregister_alg(&des3_128_alg);
446 crypto_unregister_alg(&des_alg);
447 return -EEXIST;
448 }
1da177e4
LT
449 return 0;
450}
451
c1357833 452static void __exit fini(void)
1da177e4
LT
453{
454 crypto_unregister_alg(&des3_192_alg);
455 crypto_unregister_alg(&des3_128_alg);
456 crypto_unregister_alg(&des_alg);
457}
458
459module_init(init);
460module_exit(fini);
461
462MODULE_ALIAS("des");
463MODULE_ALIAS("des3_ede");
464
465MODULE_LICENSE("GPL");
466MODULE_DESCRIPTION("DES & Triple DES EDE Cipher Algorithms");