[PATCH] s390: des crypto code cleanup
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / s390 / crypto / des_s390.c
blobf090c3c2745bf17ddac07652a97b2e02b2336265
1 /*
2 * Cryptographic API.
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];
34 u8 key[DES_KEY_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,
48 u32 *flags)
50 struct crypt_s390_des_ctx *dctx = ctx;
51 int ret;
53 /* test if key is valid (not a weak key) */
54 ret = crypto_des_check_key(key, keylen, flags);
55 if (ret == 0)
56 memcpy(dctx->key, key, keylen);
57 return ret;
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 = {
75 .cra_name = "des",
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),
81 .cra_u = {
82 .cipher = {
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
93 * RFC2451:
95 * For DES-EDE3, there is no known need to reject weak or
96 * complementation keys. Any weakness is obviated by the use of
97 * multiple keys.
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,
105 u32 *flags)
107 int i, ret;
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;
113 return -EINVAL;
115 for (i = 0; i < 2; i++, temp_key += DES_KEY_SIZE) {
116 ret = crypto_des_check_key(temp_key, DES_KEY_SIZE, flags);
117 if (ret < 0)
118 return ret;
120 memcpy(dctx->key, key, keylen);
121 return 0;
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),
147 .cra_u = {
148 .cipher = {
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
159 * RFC2451:
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.
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
168 * property.
171 static int des3_192_setkey(void *ctx, const u8 *key, unsigned int keylen,
172 u32 *flags)
174 int i, ret;
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],
180 DES_KEY_SIZE))) {
182 *flags |= CRYPTO_TFM_RES_BAD_KEY_SCHED;
183 return -EINVAL;
185 for (i = 0; i < 3; i++, temp_key += DES_KEY_SIZE) {
186 ret = crypto_des_check_key(temp_key, DES_KEY_SIZE, flags);
187 if (ret < 0)
188 return ret;
190 memcpy(dctx->key, key, keylen);
191 return 0;
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),
217 .cra_u = {
218 .cipher = {
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)
230 int ret = 0;
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))
235 return -ENOSYS;
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;
240 if (ret) {
241 crypto_unregister_alg(&des3_192_alg);
242 crypto_unregister_alg(&des3_128_alg);
243 crypto_unregister_alg(&des_alg);
244 return -EEXIST;
246 return 0;
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);
256 module_init(init);
257 module_exit(fini);
259 MODULE_ALIAS("des");
260 MODULE_ALIAS("des3_ede");
262 MODULE_LICENSE("GPL");
263 MODULE_DESCRIPTION("DES & Triple DES EDE Cipher Algorithms");