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(struct crypto_tfm
*tfm
, const u8
*key
,
50 struct crypt_s390_des_ctx
*dctx
= crypto_tfm_ctx(tfm
);
51 u32
*flags
= &tfm
->crt_flags
;
54 /* test if key is valid (not a weak key) */
55 ret
= crypto_des_check_key(key
, keylen
, flags
);
57 memcpy(dctx
->key
, key
, keylen
);
61 static void des_encrypt(struct crypto_tfm
*tfm
, u8
*out
, const u8
*in
)
63 struct crypt_s390_des_ctx
*dctx
= crypto_tfm_ctx(tfm
);
65 crypt_s390_km(KM_DEA_ENCRYPT
, dctx
->key
, out
, in
, DES_BLOCK_SIZE
);
68 static void des_decrypt(struct crypto_tfm
*tfm
, u8
*out
, const u8
*in
)
70 struct crypt_s390_des_ctx
*dctx
= crypto_tfm_ctx(tfm
);
72 crypt_s390_km(KM_DEA_DECRYPT
, dctx
->key
, out
, in
, DES_BLOCK_SIZE
);
75 static unsigned int des_encrypt_ecb(const struct cipher_desc
*desc
, u8
*out
,
76 const u8
*in
, unsigned int nbytes
)
78 struct crypt_s390_des_ctx
*sctx
= crypto_tfm_ctx(desc
->tfm
);
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
));
89 static unsigned int des_decrypt_ecb(const struct cipher_desc
*desc
, u8
*out
,
90 const u8
*in
, unsigned int nbytes
)
92 struct crypt_s390_des_ctx
*sctx
= crypto_tfm_ctx(desc
->tfm
);
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
));
103 static unsigned int des_encrypt_cbc(const struct cipher_desc
*desc
, u8
*out
,
104 const u8
*in
, unsigned int nbytes
)
106 struct crypt_s390_des_ctx
*sctx
= crypto_tfm_ctx(desc
->tfm
);
109 /* only use complete blocks */
110 nbytes
&= ~(DES_BLOCK_SIZE
- 1);
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
));
116 memcpy(desc
->info
, sctx
->iv
, DES_BLOCK_SIZE
);
120 static unsigned int des_decrypt_cbc(const struct cipher_desc
*desc
, u8
*out
,
121 const u8
*in
, unsigned int nbytes
)
123 struct crypt_s390_des_ctx
*sctx
= crypto_tfm_ctx(desc
->tfm
);
126 /* only use complete blocks */
127 nbytes
&= ~(DES_BLOCK_SIZE
- 1);
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
));
136 static struct crypto_alg des_alg
= {
138 .cra_driver_name
= "des-s390",
139 .cra_priority
= CRYPT_S390_PRIORITY
,
140 .cra_flags
= CRYPTO_ALG_TYPE_CIPHER
,
141 .cra_blocksize
= DES_BLOCK_SIZE
,
142 .cra_ctxsize
= sizeof(struct crypt_s390_des_ctx
),
143 .cra_module
= THIS_MODULE
,
144 .cra_list
= LIST_HEAD_INIT(des_alg
.cra_list
),
147 .cia_min_keysize
= DES_KEY_SIZE
,
148 .cia_max_keysize
= DES_KEY_SIZE
,
149 .cia_setkey
= des_setkey
,
150 .cia_encrypt
= des_encrypt
,
151 .cia_decrypt
= des_decrypt
,
152 .cia_encrypt_ecb
= des_encrypt_ecb
,
153 .cia_decrypt_ecb
= des_decrypt_ecb
,
154 .cia_encrypt_cbc
= des_encrypt_cbc
,
155 .cia_decrypt_cbc
= des_decrypt_cbc
,
163 * For DES-EDE3, there is no known need to reject weak or
164 * complementation keys. Any weakness is obviated by the use of
167 * However, if the two independent 64-bit keys are equal,
168 * then the DES3 operation is simply the same as DES.
169 * Implementers MUST reject keys that exhibit this property.
172 static int des3_128_setkey(struct crypto_tfm
*tfm
, const u8
*key
,
176 struct crypt_s390_des3_128_ctx
*dctx
= crypto_tfm_ctx(tfm
);
177 const u8
*temp_key
= key
;
178 u32
*flags
= &tfm
->crt_flags
;
180 if (!(memcmp(key
, &key
[DES_KEY_SIZE
], DES_KEY_SIZE
))) {
181 *flags
|= CRYPTO_TFM_RES_BAD_KEY_SCHED
;
184 for (i
= 0; i
< 2; i
++, temp_key
+= DES_KEY_SIZE
) {
185 ret
= crypto_des_check_key(temp_key
, DES_KEY_SIZE
, flags
);
189 memcpy(dctx
->key
, key
, keylen
);
193 static void des3_128_encrypt(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
)
195 struct crypt_s390_des3_128_ctx
*dctx
= crypto_tfm_ctx(tfm
);
197 crypt_s390_km(KM_TDEA_128_ENCRYPT
, dctx
->key
, dst
, (void*)src
,
198 DES3_128_BLOCK_SIZE
);
201 static void des3_128_decrypt(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
)
203 struct crypt_s390_des3_128_ctx
*dctx
= crypto_tfm_ctx(tfm
);
205 crypt_s390_km(KM_TDEA_128_DECRYPT
, dctx
->key
, dst
, (void*)src
,
206 DES3_128_BLOCK_SIZE
);
209 static unsigned int des3_128_encrypt_ecb(const struct cipher_desc
*desc
,
210 u8
*out
, const u8
*in
,
213 struct crypt_s390_des3_128_ctx
*sctx
= crypto_tfm_ctx(desc
->tfm
);
216 /* only use complete blocks */
217 nbytes
&= ~(DES3_128_BLOCK_SIZE
- 1);
218 ret
= crypt_s390_km(KM_TDEA_128_ENCRYPT
, sctx
->key
, out
, in
, nbytes
);
219 BUG_ON((ret
< 0) || (ret
!= nbytes
));
224 static unsigned int des3_128_decrypt_ecb(const struct cipher_desc
*desc
,
225 u8
*out
, const u8
*in
,
228 struct crypt_s390_des3_128_ctx
*sctx
= crypto_tfm_ctx(desc
->tfm
);
231 /* only use complete blocks */
232 nbytes
&= ~(DES3_128_BLOCK_SIZE
- 1);
233 ret
= crypt_s390_km(KM_TDEA_128_DECRYPT
, sctx
->key
, out
, in
, nbytes
);
234 BUG_ON((ret
< 0) || (ret
!= nbytes
));
239 static unsigned int des3_128_encrypt_cbc(const struct cipher_desc
*desc
,
240 u8
*out
, const u8
*in
,
243 struct crypt_s390_des3_128_ctx
*sctx
= crypto_tfm_ctx(desc
->tfm
);
246 /* only use complete blocks */
247 nbytes
&= ~(DES3_128_BLOCK_SIZE
- 1);
249 memcpy(sctx
->iv
, desc
->info
, DES3_128_BLOCK_SIZE
);
250 ret
= crypt_s390_kmc(KMC_TDEA_128_ENCRYPT
, &sctx
->iv
, out
, in
, nbytes
);
251 BUG_ON((ret
< 0) || (ret
!= nbytes
));
253 memcpy(desc
->info
, sctx
->iv
, DES3_128_BLOCK_SIZE
);
257 static unsigned int des3_128_decrypt_cbc(const struct cipher_desc
*desc
,
258 u8
*out
, const u8
*in
,
261 struct crypt_s390_des3_128_ctx
*sctx
= crypto_tfm_ctx(desc
->tfm
);
264 /* only use complete blocks */
265 nbytes
&= ~(DES3_128_BLOCK_SIZE
- 1);
267 memcpy(&sctx
->iv
, desc
->info
, DES3_128_BLOCK_SIZE
);
268 ret
= crypt_s390_kmc(KMC_TDEA_128_DECRYPT
, &sctx
->iv
, out
, in
, nbytes
);
269 BUG_ON((ret
< 0) || (ret
!= nbytes
));
274 static struct crypto_alg des3_128_alg
= {
275 .cra_name
= "des3_ede128",
276 .cra_driver_name
= "des3_ede128-s390",
277 .cra_priority
= CRYPT_S390_PRIORITY
,
278 .cra_flags
= CRYPTO_ALG_TYPE_CIPHER
,
279 .cra_blocksize
= DES3_128_BLOCK_SIZE
,
280 .cra_ctxsize
= sizeof(struct crypt_s390_des3_128_ctx
),
281 .cra_module
= THIS_MODULE
,
282 .cra_list
= LIST_HEAD_INIT(des3_128_alg
.cra_list
),
285 .cia_min_keysize
= DES3_128_KEY_SIZE
,
286 .cia_max_keysize
= DES3_128_KEY_SIZE
,
287 .cia_setkey
= des3_128_setkey
,
288 .cia_encrypt
= des3_128_encrypt
,
289 .cia_decrypt
= des3_128_decrypt
,
290 .cia_encrypt_ecb
= des3_128_encrypt_ecb
,
291 .cia_decrypt_ecb
= des3_128_decrypt_ecb
,
292 .cia_encrypt_cbc
= des3_128_encrypt_cbc
,
293 .cia_decrypt_cbc
= des3_128_decrypt_cbc
,
301 * For DES-EDE3, there is no known need to reject weak or
302 * complementation keys. Any weakness is obviated by the use of
305 * However, if the first two or last two independent 64-bit keys are
306 * equal (k1 == k2 or k2 == k3), then the DES3 operation is simply the
307 * same as DES. Implementers MUST reject keys that exhibit this
311 static int des3_192_setkey(struct crypto_tfm
*tfm
, const u8
*key
,
315 struct crypt_s390_des3_192_ctx
*dctx
= crypto_tfm_ctx(tfm
);
316 const u8
*temp_key
= key
;
317 u32
*flags
= &tfm
->crt_flags
;
319 if (!(memcmp(key
, &key
[DES_KEY_SIZE
], DES_KEY_SIZE
) &&
320 memcmp(&key
[DES_KEY_SIZE
], &key
[DES_KEY_SIZE
* 2],
323 *flags
|= CRYPTO_TFM_RES_BAD_KEY_SCHED
;
326 for (i
= 0; i
< 3; i
++, temp_key
+= DES_KEY_SIZE
) {
327 ret
= crypto_des_check_key(temp_key
, DES_KEY_SIZE
, flags
);
331 memcpy(dctx
->key
, key
, keylen
);
335 static void des3_192_encrypt(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
)
337 struct crypt_s390_des3_192_ctx
*dctx
= crypto_tfm_ctx(tfm
);
339 crypt_s390_km(KM_TDEA_192_ENCRYPT
, dctx
->key
, dst
, (void*)src
,
340 DES3_192_BLOCK_SIZE
);
343 static void des3_192_decrypt(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
)
345 struct crypt_s390_des3_192_ctx
*dctx
= crypto_tfm_ctx(tfm
);
347 crypt_s390_km(KM_TDEA_192_DECRYPT
, dctx
->key
, dst
, (void*)src
,
348 DES3_192_BLOCK_SIZE
);
351 static unsigned int des3_192_encrypt_ecb(const struct cipher_desc
*desc
,
352 u8
*out
, const u8
*in
,
355 struct crypt_s390_des3_192_ctx
*sctx
= crypto_tfm_ctx(desc
->tfm
);
358 /* only use complete blocks */
359 nbytes
&= ~(DES3_192_BLOCK_SIZE
- 1);
360 ret
= crypt_s390_km(KM_TDEA_192_ENCRYPT
, sctx
->key
, out
, in
, nbytes
);
361 BUG_ON((ret
< 0) || (ret
!= nbytes
));
366 static unsigned int des3_192_decrypt_ecb(const struct cipher_desc
*desc
,
367 u8
*out
, const u8
*in
,
370 struct crypt_s390_des3_192_ctx
*sctx
= crypto_tfm_ctx(desc
->tfm
);
373 /* only use complete blocks */
374 nbytes
&= ~(DES3_192_BLOCK_SIZE
- 1);
375 ret
= crypt_s390_km(KM_TDEA_192_DECRYPT
, sctx
->key
, out
, in
, nbytes
);
376 BUG_ON((ret
< 0) || (ret
!= nbytes
));
381 static unsigned int des3_192_encrypt_cbc(const struct cipher_desc
*desc
,
382 u8
*out
, const u8
*in
,
385 struct crypt_s390_des3_192_ctx
*sctx
= crypto_tfm_ctx(desc
->tfm
);
388 /* only use complete blocks */
389 nbytes
&= ~(DES3_192_BLOCK_SIZE
- 1);
391 memcpy(sctx
->iv
, desc
->info
, DES3_192_BLOCK_SIZE
);
392 ret
= crypt_s390_kmc(KMC_TDEA_192_ENCRYPT
, &sctx
->iv
, out
, in
, nbytes
);
393 BUG_ON((ret
< 0) || (ret
!= nbytes
));
395 memcpy(desc
->info
, sctx
->iv
, DES3_192_BLOCK_SIZE
);
399 static unsigned int des3_192_decrypt_cbc(const struct cipher_desc
*desc
,
400 u8
*out
, const u8
*in
,
403 struct crypt_s390_des3_192_ctx
*sctx
= crypto_tfm_ctx(desc
->tfm
);
406 /* only use complete blocks */
407 nbytes
&= ~(DES3_192_BLOCK_SIZE
- 1);
409 memcpy(&sctx
->iv
, desc
->info
, DES3_192_BLOCK_SIZE
);
410 ret
= crypt_s390_kmc(KMC_TDEA_192_DECRYPT
, &sctx
->iv
, out
, in
, nbytes
);
411 BUG_ON((ret
< 0) || (ret
!= nbytes
));
416 static struct crypto_alg des3_192_alg
= {
417 .cra_name
= "des3_ede",
418 .cra_driver_name
= "des3_ede-s390",
419 .cra_priority
= CRYPT_S390_PRIORITY
,
420 .cra_flags
= CRYPTO_ALG_TYPE_CIPHER
,
421 .cra_blocksize
= DES3_192_BLOCK_SIZE
,
422 .cra_ctxsize
= sizeof(struct crypt_s390_des3_192_ctx
),
423 .cra_module
= THIS_MODULE
,
424 .cra_list
= LIST_HEAD_INIT(des3_192_alg
.cra_list
),
427 .cia_min_keysize
= DES3_192_KEY_SIZE
,
428 .cia_max_keysize
= DES3_192_KEY_SIZE
,
429 .cia_setkey
= des3_192_setkey
,
430 .cia_encrypt
= des3_192_encrypt
,
431 .cia_decrypt
= des3_192_decrypt
,
432 .cia_encrypt_ecb
= des3_192_encrypt_ecb
,
433 .cia_decrypt_ecb
= des3_192_decrypt_ecb
,
434 .cia_encrypt_cbc
= des3_192_encrypt_cbc
,
435 .cia_decrypt_cbc
= des3_192_decrypt_cbc
,
440 static int init(void)
444 if (!crypt_s390_func_available(KM_DEA_ENCRYPT
) ||
445 !crypt_s390_func_available(KM_TDEA_128_ENCRYPT
) ||
446 !crypt_s390_func_available(KM_TDEA_192_ENCRYPT
))
449 ret
|= (crypto_register_alg(&des_alg
) == 0) ? 0:1;
450 ret
|= (crypto_register_alg(&des3_128_alg
) == 0) ? 0:2;
451 ret
|= (crypto_register_alg(&des3_192_alg
) == 0) ? 0:4;
453 crypto_unregister_alg(&des3_192_alg
);
454 crypto_unregister_alg(&des3_128_alg
);
455 crypto_unregister_alg(&des_alg
);
461 static void __exit
fini(void)
463 crypto_unregister_alg(&des3_192_alg
);
464 crypto_unregister_alg(&des3_128_alg
);
465 crypto_unregister_alg(&des_alg
);
472 MODULE_ALIAS("des3_ede");
474 MODULE_LICENSE("GPL");
475 MODULE_DESCRIPTION("DES & Triple DES EDE Cipher Algorithms");