4 * z990 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>
19 #include <linux/errno.h>
20 #include <asm/scatterlist.h>
21 #include <linux/crypto.h>
22 #include "crypt_z990.h"
23 #include "crypto_des.h"
25 #define DES_BLOCK_SIZE 8
26 #define DES_KEY_SIZE 8
28 #define DES3_128_KEY_SIZE (2 * DES_KEY_SIZE)
29 #define DES3_128_BLOCK_SIZE DES_BLOCK_SIZE
31 #define DES3_192_KEY_SIZE (3 * DES_KEY_SIZE)
32 #define DES3_192_BLOCK_SIZE DES_BLOCK_SIZE
34 struct crypt_z990_des_ctx {
35 u8 iv[DES_BLOCK_SIZE];
39 struct crypt_z990_des3_128_ctx {
40 u8 iv[DES_BLOCK_SIZE];
41 u8 key[DES3_128_KEY_SIZE];
44 struct crypt_z990_des3_192_ctx {
45 u8 iv[DES_BLOCK_SIZE];
46 u8 key[DES3_192_KEY_SIZE];
50 des_setkey(void *ctx, const u8 *key, unsigned int keylen, u32 *flags)
52 struct crypt_z990_des_ctx *dctx;
56 //test if key is valid (not a weak key)
57 ret = crypto_des_check_key(key, keylen, flags);
59 memcpy(dctx->key, key, keylen);
66 des_encrypt(void *ctx, u8 *dst, const u8 *src)
68 struct crypt_z990_des_ctx *dctx;
71 crypt_z990_km(KM_DEA_ENCRYPT, dctx->key, dst, src, DES_BLOCK_SIZE);
75 des_decrypt(void *ctx, u8 *dst, const u8 *src)
77 struct crypt_z990_des_ctx *dctx;
80 crypt_z990_km(KM_DEA_DECRYPT, dctx->key, dst, src, DES_BLOCK_SIZE);
83 static struct crypto_alg des_alg = {
85 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
86 .cra_blocksize = DES_BLOCK_SIZE,
87 .cra_ctxsize = sizeof(struct crypt_z990_des_ctx),
88 .cra_module = THIS_MODULE,
89 .cra_list = LIST_HEAD_INIT(des_alg.cra_list),
90 .cra_u = { .cipher = {
91 .cia_min_keysize = DES_KEY_SIZE,
92 .cia_max_keysize = DES_KEY_SIZE,
93 .cia_setkey = des_setkey,
94 .cia_encrypt = des_encrypt,
95 .cia_decrypt = des_decrypt } }
101 * For DES-EDE3, there is no known need to reject weak or
102 * complementation keys. Any weakness is obviated by the use of
105 * However, if the two independent 64-bit keys are equal,
106 * then the DES3 operation is simply the same as DES.
107 * Implementers MUST reject keys that exhibit this property.
111 des3_128_setkey(void *ctx, const u8 *key, unsigned int keylen, u32 *flags)
114 struct crypt_z990_des3_128_ctx *dctx;
115 const u8* temp_key = key;
118 if (!(memcmp(key, &key[DES_KEY_SIZE], DES_KEY_SIZE))) {
120 *flags |= CRYPTO_TFM_RES_BAD_KEY_SCHED;
123 for (i = 0; i < 2; i++, temp_key += DES_KEY_SIZE) {
124 ret = crypto_des_check_key(temp_key, DES_KEY_SIZE, flags);
128 memcpy(dctx->key, key, keylen);
133 des3_128_encrypt(void *ctx, u8 *dst, const u8 *src)
135 struct crypt_z990_des3_128_ctx *dctx;
138 crypt_z990_km(KM_TDEA_128_ENCRYPT, dctx->key, dst, (void*)src,
139 DES3_128_BLOCK_SIZE);
143 des3_128_decrypt(void *ctx, u8 *dst, const u8 *src)
145 struct crypt_z990_des3_128_ctx *dctx;
148 crypt_z990_km(KM_TDEA_128_DECRYPT, dctx->key, dst, (void*)src,
149 DES3_128_BLOCK_SIZE);
152 static struct crypto_alg des3_128_alg = {
153 .cra_name = "des3_ede128",
154 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
155 .cra_blocksize = DES3_128_BLOCK_SIZE,
156 .cra_ctxsize = sizeof(struct crypt_z990_des3_128_ctx),
157 .cra_module = THIS_MODULE,
158 .cra_list = LIST_HEAD_INIT(des3_128_alg.cra_list),
159 .cra_u = { .cipher = {
160 .cia_min_keysize = DES3_128_KEY_SIZE,
161 .cia_max_keysize = DES3_128_KEY_SIZE,
162 .cia_setkey = des3_128_setkey,
163 .cia_encrypt = des3_128_encrypt,
164 .cia_decrypt = des3_128_decrypt } }
170 * For DES-EDE3, there is no known need to reject weak or
171 * complementation keys. Any weakness is obviated by the use of
174 * However, if the first two or last two independent 64-bit keys are
175 * equal (k1 == k2 or k2 == k3), then the DES3 operation is simply the
176 * same as DES. Implementers MUST reject keys that exhibit this
181 des3_192_setkey(void *ctx, const u8 *key, unsigned int keylen, u32 *flags)
184 struct crypt_z990_des3_192_ctx *dctx;
189 if (!(memcmp(key, &key[DES_KEY_SIZE], DES_KEY_SIZE) &&
190 memcmp(&key[DES_KEY_SIZE], &key[DES_KEY_SIZE * 2],
193 *flags |= CRYPTO_TFM_RES_BAD_KEY_SCHED;
196 for (i = 0; i < 3; i++, temp_key += DES_KEY_SIZE) {
197 ret = crypto_des_check_key(temp_key, DES_KEY_SIZE, flags);
202 memcpy(dctx->key, key, keylen);
207 des3_192_encrypt(void *ctx, u8 *dst, const u8 *src)
209 struct crypt_z990_des3_192_ctx *dctx;
212 crypt_z990_km(KM_TDEA_192_ENCRYPT, dctx->key, dst, (void*)src,
213 DES3_192_BLOCK_SIZE);
217 des3_192_decrypt(void *ctx, u8 *dst, const u8 *src)
219 struct crypt_z990_des3_192_ctx *dctx;
222 crypt_z990_km(KM_TDEA_192_DECRYPT, dctx->key, dst, (void*)src,
223 DES3_192_BLOCK_SIZE);
226 static struct crypto_alg des3_192_alg = {
227 .cra_name = "des3_ede",
228 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
229 .cra_blocksize = DES3_192_BLOCK_SIZE,
230 .cra_ctxsize = sizeof(struct crypt_z990_des3_192_ctx),
231 .cra_module = THIS_MODULE,
232 .cra_list = LIST_HEAD_INIT(des3_192_alg.cra_list),
233 .cra_u = { .cipher = {
234 .cia_min_keysize = DES3_192_KEY_SIZE,
235 .cia_max_keysize = DES3_192_KEY_SIZE,
236 .cia_setkey = des3_192_setkey,
237 .cia_encrypt = des3_192_encrypt,
238 .cia_decrypt = des3_192_decrypt } }
248 if (!crypt_z990_func_available(KM_DEA_ENCRYPT) ||
249 !crypt_z990_func_available(KM_TDEA_128_ENCRYPT) ||
250 !crypt_z990_func_available(KM_TDEA_192_ENCRYPT)){
255 ret |= (crypto_register_alg(&des_alg) == 0)? 0:1;
256 ret |= (crypto_register_alg(&des3_128_alg) == 0)? 0:2;
257 ret |= (crypto_register_alg(&des3_192_alg) == 0)? 0:4;
259 crypto_unregister_alg(&des3_192_alg);
260 crypto_unregister_alg(&des3_128_alg);
261 crypto_unregister_alg(&des_alg);
265 printk(KERN_INFO "crypt_z990: des_z990 loaded.\n");
272 crypto_unregister_alg(&des3_192_alg);
273 crypto_unregister_alg(&des3_128_alg);
274 crypto_unregister_alg(&des_alg);
281 MODULE_ALIAS("des3_ede");
283 MODULE_LICENSE("GPL");
284 MODULE_DESCRIPTION("DES & Triple DES EDE Cipher Algorithms");