4 * s390 implementation of the DES Cipher Algorithm.
6 * Copyright (c) 2003 IBM Deutschland Entwicklung GmbH, IBM Corporation
7 * Author(s): Thomas Spatzier (tspat@de.ibm.com)
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.
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/crypto.h>
20 #include "crypt_s390.h"
21 #include "crypto_des.h"
23 #define DES_BLOCK_SIZE 8
24 #define DES_KEY_SIZE 8
26 #define DES3_128_KEY_SIZE (2 * DES_KEY_SIZE)
27 #define DES3_128_BLOCK_SIZE DES_BLOCK_SIZE
29 #define DES3_192_KEY_SIZE (3 * DES_KEY_SIZE)
30 #define DES3_192_BLOCK_SIZE DES_BLOCK_SIZE
32 struct crypt_s390_des_ctx
{
33 u8 iv
[DES_BLOCK_SIZE
];
37 struct crypt_s390_des3_128_ctx
{
38 u8 iv
[DES_BLOCK_SIZE
];
39 u8 key
[DES3_128_KEY_SIZE
];
42 struct crypt_s390_des3_192_ctx
{
43 u8 iv
[DES_BLOCK_SIZE
];
44 u8 key
[DES3_192_KEY_SIZE
];
47 static int des_setkey(void *ctx
, const u8
*key
, unsigned int keylen
,
50 struct crypt_s390_des_ctx
*dctx
= ctx
;
53 /* test if key is valid (not a weak key) */
54 ret
= crypto_des_check_key(key
, keylen
, flags
);
56 memcpy(dctx
->key
, key
, keylen
);
60 static void des_encrypt(void *ctx
, u8
*out
, const u8
*in
)
62 struct crypt_s390_des_ctx
*dctx
= ctx
;
64 crypt_s390_km(KM_DEA_ENCRYPT
, dctx
->key
, out
, in
, DES_BLOCK_SIZE
);
67 static void des_decrypt(void *ctx
, u8
*out
, const u8
*in
)
69 struct crypt_s390_des_ctx
*dctx
= ctx
;
71 crypt_s390_km(KM_DEA_DECRYPT
, dctx
->key
, out
, in
, DES_BLOCK_SIZE
);
74 static unsigned int des_encrypt_ecb(const struct cipher_desc
*desc
, u8
*out
,
75 const u8
*in
, unsigned int nbytes
)
77 struct crypt_s390_des_ctx
*sctx
= crypto_tfm_ctx(desc
->tfm
);
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
));
88 static unsigned int des_decrypt_ecb(const struct cipher_desc
*desc
, u8
*out
,
89 const u8
*in
, unsigned int nbytes
)
91 struct crypt_s390_des_ctx
*sctx
= crypto_tfm_ctx(desc
->tfm
);
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
));
102 static unsigned int des_encrypt_cbc(const struct cipher_desc
*desc
, u8
*out
,
103 const u8
*in
, unsigned int nbytes
)
105 struct crypt_s390_des_ctx
*sctx
= crypto_tfm_ctx(desc
->tfm
);
108 /* only use complete blocks */
109 nbytes
&= ~(DES_BLOCK_SIZE
- 1);
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
));
115 memcpy(desc
->info
, sctx
->iv
, DES_BLOCK_SIZE
);
119 static unsigned int des_decrypt_cbc(const struct cipher_desc
*desc
, u8
*out
,
120 const u8
*in
, unsigned int nbytes
)
122 struct crypt_s390_des_ctx
*sctx
= crypto_tfm_ctx(desc
->tfm
);
125 /* only use complete blocks */
126 nbytes
&= ~(DES_BLOCK_SIZE
- 1);
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
));
135 static struct crypto_alg des_alg
= {
137 .cra_flags
= CRYPTO_ALG_TYPE_CIPHER
,
138 .cra_blocksize
= DES_BLOCK_SIZE
,
139 .cra_ctxsize
= sizeof(struct crypt_s390_des_ctx
),
140 .cra_module
= THIS_MODULE
,
141 .cra_list
= LIST_HEAD_INIT(des_alg
.cra_list
),
144 .cia_min_keysize
= DES_KEY_SIZE
,
145 .cia_max_keysize
= DES_KEY_SIZE
,
146 .cia_setkey
= des_setkey
,
147 .cia_encrypt
= des_encrypt
,
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
,
160 * For DES-EDE3, there is no known need to reject weak or
161 * complementation keys. Any weakness is obviated by the use of
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.
169 static int des3_128_setkey(void *ctx
, const u8
*key
, unsigned int keylen
,
173 struct crypt_s390_des3_128_ctx
*dctx
= ctx
;
174 const u8
* temp_key
= key
;
176 if (!(memcmp(key
, &key
[DES_KEY_SIZE
], DES_KEY_SIZE
))) {
177 *flags
|= CRYPTO_TFM_RES_BAD_KEY_SCHED
;
180 for (i
= 0; i
< 2; i
++, temp_key
+= DES_KEY_SIZE
) {
181 ret
= crypto_des_check_key(temp_key
, DES_KEY_SIZE
, flags
);
185 memcpy(dctx
->key
, key
, keylen
);
189 static void des3_128_encrypt(void *ctx
, u8
*dst
, const u8
*src
)
191 struct crypt_s390_des3_128_ctx
*dctx
= ctx
;
193 crypt_s390_km(KM_TDEA_128_ENCRYPT
, dctx
->key
, dst
, (void*)src
,
194 DES3_128_BLOCK_SIZE
);
197 static void des3_128_decrypt(void *ctx
, u8
*dst
, const u8
*src
)
199 struct crypt_s390_des3_128_ctx
*dctx
= ctx
;
201 crypt_s390_km(KM_TDEA_128_DECRYPT
, dctx
->key
, dst
, (void*)src
,
202 DES3_128_BLOCK_SIZE
);
205 static unsigned int des3_128_encrypt_ecb(const struct cipher_desc
*desc
,
206 u8
*out
, const u8
*in
,
209 struct crypt_s390_des3_128_ctx
*sctx
= crypto_tfm_ctx(desc
->tfm
);
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
));
220 static unsigned int des3_128_decrypt_ecb(const struct cipher_desc
*desc
,
221 u8
*out
, const u8
*in
,
224 struct crypt_s390_des3_128_ctx
*sctx
= crypto_tfm_ctx(desc
->tfm
);
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
));
235 static unsigned int des3_128_encrypt_cbc(const struct cipher_desc
*desc
,
236 u8
*out
, const u8
*in
,
239 struct crypt_s390_des3_128_ctx
*sctx
= crypto_tfm_ctx(desc
->tfm
);
242 /* only use complete blocks */
243 nbytes
&= ~(DES3_128_BLOCK_SIZE
- 1);
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
));
249 memcpy(desc
->info
, sctx
->iv
, DES3_128_BLOCK_SIZE
);
253 static unsigned int des3_128_decrypt_cbc(const struct cipher_desc
*desc
,
254 u8
*out
, const u8
*in
,
257 struct crypt_s390_des3_128_ctx
*sctx
= crypto_tfm_ctx(desc
->tfm
);
260 /* only use complete blocks */
261 nbytes
&= ~(DES3_128_BLOCK_SIZE
- 1);
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
));
270 static 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
,
274 .cra_ctxsize
= sizeof(struct crypt_s390_des3_128_ctx
),
275 .cra_module
= THIS_MODULE
,
276 .cra_list
= LIST_HEAD_INIT(des3_128_alg
.cra_list
),
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
,
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
,
295 * For DES-EDE3, there is no known need to reject weak or
296 * complementation keys. Any weakness is obviated by the use of
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
305 static int des3_192_setkey(void *ctx
, const u8
*key
, unsigned int keylen
,
309 struct crypt_s390_des3_192_ctx
*dctx
= ctx
;
310 const u8
* temp_key
= key
;
312 if (!(memcmp(key
, &key
[DES_KEY_SIZE
], DES_KEY_SIZE
) &&
313 memcmp(&key
[DES_KEY_SIZE
], &key
[DES_KEY_SIZE
* 2],
316 *flags
|= CRYPTO_TFM_RES_BAD_KEY_SCHED
;
319 for (i
= 0; i
< 3; i
++, temp_key
+= DES_KEY_SIZE
) {
320 ret
= crypto_des_check_key(temp_key
, DES_KEY_SIZE
, flags
);
324 memcpy(dctx
->key
, key
, keylen
);
328 static void des3_192_encrypt(void *ctx
, u8
*dst
, const u8
*src
)
330 struct crypt_s390_des3_192_ctx
*dctx
= ctx
;
332 crypt_s390_km(KM_TDEA_192_ENCRYPT
, dctx
->key
, dst
, (void*)src
,
333 DES3_192_BLOCK_SIZE
);
336 static void des3_192_decrypt(void *ctx
, u8
*dst
, const u8
*src
)
338 struct crypt_s390_des3_192_ctx
*dctx
= ctx
;
340 crypt_s390_km(KM_TDEA_192_DECRYPT
, dctx
->key
, dst
, (void*)src
,
341 DES3_192_BLOCK_SIZE
);
344 static unsigned int des3_192_encrypt_ecb(const struct cipher_desc
*desc
,
345 u8
*out
, const u8
*in
,
348 struct crypt_s390_des3_192_ctx
*sctx
= crypto_tfm_ctx(desc
->tfm
);
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
));
359 static unsigned int des3_192_decrypt_ecb(const struct cipher_desc
*desc
,
360 u8
*out
, const u8
*in
,
363 struct crypt_s390_des3_192_ctx
*sctx
= crypto_tfm_ctx(desc
->tfm
);
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
));
374 static unsigned int des3_192_encrypt_cbc(const struct cipher_desc
*desc
,
375 u8
*out
, const u8
*in
,
378 struct crypt_s390_des3_192_ctx
*sctx
= crypto_tfm_ctx(desc
->tfm
);
381 /* only use complete blocks */
382 nbytes
&= ~(DES3_192_BLOCK_SIZE
- 1);
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
));
388 memcpy(desc
->info
, sctx
->iv
, DES3_192_BLOCK_SIZE
);
392 static unsigned int des3_192_decrypt_cbc(const struct cipher_desc
*desc
,
393 u8
*out
, const u8
*in
,
396 struct crypt_s390_des3_192_ctx
*sctx
= crypto_tfm_ctx(desc
->tfm
);
399 /* only use complete blocks */
400 nbytes
&= ~(DES3_192_BLOCK_SIZE
- 1);
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
));
409 static 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
,
413 .cra_ctxsize
= sizeof(struct crypt_s390_des3_192_ctx
),
414 .cra_module
= THIS_MODULE
,
415 .cra_list
= LIST_HEAD_INIT(des3_192_alg
.cra_list
),
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
,
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
,
431 static int init(void)
435 if (!crypt_s390_func_available(KM_DEA_ENCRYPT
) ||
436 !crypt_s390_func_available(KM_TDEA_128_ENCRYPT
) ||
437 !crypt_s390_func_available(KM_TDEA_192_ENCRYPT
))
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;
444 crypto_unregister_alg(&des3_192_alg
);
445 crypto_unregister_alg(&des3_128_alg
);
446 crypto_unregister_alg(&des_alg
);
452 static void __exit
fini(void)
454 crypto_unregister_alg(&des3_192_alg
);
455 crypto_unregister_alg(&des3_128_alg
);
456 crypto_unregister_alg(&des_alg
);
463 MODULE_ALIAS("des3_ede");
465 MODULE_LICENSE("GPL");
466 MODULE_DESCRIPTION("DES & Triple DES EDE Cipher Algorithms");