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