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
*dst
, const u8
*src
)
62 struct crypt_s390_des_ctx
*dctx
= ctx
;
64 crypt_s390_km(KM_DEA_ENCRYPT
, dctx
->key
, dst
, src
, DES_BLOCK_SIZE
);
67 static void des_decrypt(void *ctx
, u8
*dst
, const u8
*src
)
69 struct crypt_s390_des_ctx
*dctx
= ctx
;
71 crypt_s390_km(KM_DEA_DECRYPT
, dctx
->key
, dst
, src
, DES_BLOCK_SIZE
);
74 static struct crypto_alg des_alg
= {
76 .cra_flags
= CRYPTO_ALG_TYPE_CIPHER
,
77 .cra_blocksize
= DES_BLOCK_SIZE
,
78 .cra_ctxsize
= sizeof(struct crypt_s390_des_ctx
),
79 .cra_module
= THIS_MODULE
,
80 .cra_list
= LIST_HEAD_INIT(des_alg
.cra_list
),
83 .cia_min_keysize
= DES_KEY_SIZE
,
84 .cia_max_keysize
= DES_KEY_SIZE
,
85 .cia_setkey
= des_setkey
,
86 .cia_encrypt
= des_encrypt
,
87 .cia_decrypt
= des_decrypt
95 * For DES-EDE3, there is no known need to reject weak or
96 * complementation keys. Any weakness is obviated by the use of
99 * However, if the two independent 64-bit keys are equal,
100 * then the DES3 operation is simply the same as DES.
101 * Implementers MUST reject keys that exhibit this property.
104 static int des3_128_setkey(void *ctx
, const u8
*key
, unsigned int keylen
,
108 struct crypt_s390_des3_128_ctx
*dctx
= ctx
;
109 const u8
* temp_key
= key
;
111 if (!(memcmp(key
, &key
[DES_KEY_SIZE
], DES_KEY_SIZE
))) {
112 *flags
|= CRYPTO_TFM_RES_BAD_KEY_SCHED
;
115 for (i
= 0; i
< 2; i
++, temp_key
+= DES_KEY_SIZE
) {
116 ret
= crypto_des_check_key(temp_key
, DES_KEY_SIZE
, flags
);
120 memcpy(dctx
->key
, key
, keylen
);
124 static void des3_128_encrypt(void *ctx
, u8
*dst
, const u8
*src
)
126 struct crypt_s390_des3_128_ctx
*dctx
= ctx
;
128 crypt_s390_km(KM_TDEA_128_ENCRYPT
, dctx
->key
, dst
, (void*)src
,
129 DES3_128_BLOCK_SIZE
);
132 static void des3_128_decrypt(void *ctx
, u8
*dst
, const u8
*src
)
134 struct crypt_s390_des3_128_ctx
*dctx
= ctx
;
136 crypt_s390_km(KM_TDEA_128_DECRYPT
, dctx
->key
, dst
, (void*)src
,
137 DES3_128_BLOCK_SIZE
);
140 static struct crypto_alg des3_128_alg
= {
141 .cra_name
= "des3_ede128",
142 .cra_flags
= CRYPTO_ALG_TYPE_CIPHER
,
143 .cra_blocksize
= DES3_128_BLOCK_SIZE
,
144 .cra_ctxsize
= sizeof(struct crypt_s390_des3_128_ctx
),
145 .cra_module
= THIS_MODULE
,
146 .cra_list
= LIST_HEAD_INIT(des3_128_alg
.cra_list
),
149 .cia_min_keysize
= DES3_128_KEY_SIZE
,
150 .cia_max_keysize
= DES3_128_KEY_SIZE
,
151 .cia_setkey
= des3_128_setkey
,
152 .cia_encrypt
= des3_128_encrypt
,
153 .cia_decrypt
= des3_128_decrypt
161 * For DES-EDE3, there is no known need to reject weak or
162 * complementation keys. Any weakness is obviated by the use of
165 * However, if the first two or last two independent 64-bit keys are
166 * equal (k1 == k2 or k2 == k3), then the DES3 operation is simply the
167 * same as DES. Implementers MUST reject keys that exhibit this
171 static int des3_192_setkey(void *ctx
, const u8
*key
, unsigned int keylen
,
175 struct crypt_s390_des3_192_ctx
*dctx
= ctx
;
176 const u8
* temp_key
= key
;
178 if (!(memcmp(key
, &key
[DES_KEY_SIZE
], DES_KEY_SIZE
) &&
179 memcmp(&key
[DES_KEY_SIZE
], &key
[DES_KEY_SIZE
* 2],
182 *flags
|= CRYPTO_TFM_RES_BAD_KEY_SCHED
;
185 for (i
= 0; i
< 3; i
++, temp_key
+= DES_KEY_SIZE
) {
186 ret
= crypto_des_check_key(temp_key
, DES_KEY_SIZE
, flags
);
190 memcpy(dctx
->key
, key
, keylen
);
194 static void des3_192_encrypt(void *ctx
, u8
*dst
, const u8
*src
)
196 struct crypt_s390_des3_192_ctx
*dctx
= ctx
;
198 crypt_s390_km(KM_TDEA_192_ENCRYPT
, dctx
->key
, dst
, (void*)src
,
199 DES3_192_BLOCK_SIZE
);
202 static void des3_192_decrypt(void *ctx
, u8
*dst
, const u8
*src
)
204 struct crypt_s390_des3_192_ctx
*dctx
= ctx
;
206 crypt_s390_km(KM_TDEA_192_DECRYPT
, dctx
->key
, dst
, (void*)src
,
207 DES3_192_BLOCK_SIZE
);
210 static struct crypto_alg des3_192_alg
= {
211 .cra_name
= "des3_ede",
212 .cra_flags
= CRYPTO_ALG_TYPE_CIPHER
,
213 .cra_blocksize
= DES3_192_BLOCK_SIZE
,
214 .cra_ctxsize
= sizeof(struct crypt_s390_des3_192_ctx
),
215 .cra_module
= THIS_MODULE
,
216 .cra_list
= LIST_HEAD_INIT(des3_192_alg
.cra_list
),
219 .cia_min_keysize
= DES3_192_KEY_SIZE
,
220 .cia_max_keysize
= DES3_192_KEY_SIZE
,
221 .cia_setkey
= des3_192_setkey
,
222 .cia_encrypt
= des3_192_encrypt
,
223 .cia_decrypt
= des3_192_decrypt
228 static int init(void)
232 if (!crypt_s390_func_available(KM_DEA_ENCRYPT
) ||
233 !crypt_s390_func_available(KM_TDEA_128_ENCRYPT
) ||
234 !crypt_s390_func_available(KM_TDEA_192_ENCRYPT
))
237 ret
|= (crypto_register_alg(&des_alg
) == 0) ? 0:1;
238 ret
|= (crypto_register_alg(&des3_128_alg
) == 0) ? 0:2;
239 ret
|= (crypto_register_alg(&des3_192_alg
) == 0) ? 0:4;
241 crypto_unregister_alg(&des3_192_alg
);
242 crypto_unregister_alg(&des3_128_alg
);
243 crypto_unregister_alg(&des_alg
);
249 static void __exit
fini(void)
251 crypto_unregister_alg(&des3_192_alg
);
252 crypto_unregister_alg(&des3_128_alg
);
253 crypto_unregister_alg(&des_alg
);
260 MODULE_ALIAS("des3_ede");
262 MODULE_LICENSE("GPL");
263 MODULE_DESCRIPTION("DES & Triple DES EDE Cipher Algorithms");