diff options
author | Jan Glauber <jan.glauber@de.ibm.com> | 2006-01-14 16:20:53 -0500 |
---|---|---|
committer | Linus Torvalds <torvalds@g5.osdl.org> | 2006-01-14 21:27:08 -0500 |
commit | c1357833bf5d92b1bda41215ae88a6597664251b (patch) | |
tree | 986dafb9463c0f5764cd511defbd2650a66c3f68 /arch/s390 | |
parent | 7aa89746e89fca8fc722485aaf4454f2b636cf4d (diff) |
[PATCH] s390: des crypto code cleanup
Beautify the s390 in-kernel-crypto des code.
Signed-off-by: Jan Glauber <jan.glauber@de.ibm.com>
Signed-off-by: Martin Schwidefsky <schwidefsky@de.ibm.com>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Diffstat (limited to 'arch/s390')
-rw-r--r-- | arch/s390/crypto/des_s390.c | 155 |
1 files changed, 67 insertions, 88 deletions
diff --git a/arch/s390/crypto/des_s390.c b/arch/s390/crypto/des_s390.c index a38bb2a3eef6..f090c3c2745b 100644 --- a/arch/s390/crypto/des_s390.c +++ b/arch/s390/crypto/des_s390.c | |||
@@ -15,10 +15,8 @@ | |||
15 | */ | 15 | */ |
16 | #include <linux/init.h> | 16 | #include <linux/init.h> |
17 | #include <linux/module.h> | 17 | #include <linux/module.h> |
18 | #include <linux/mm.h> | ||
19 | #include <linux/errno.h> | ||
20 | #include <asm/scatterlist.h> | ||
21 | #include <linux/crypto.h> | 18 | #include <linux/crypto.h> |
19 | |||
22 | #include "crypt_s390.h" | 20 | #include "crypt_s390.h" |
23 | #include "crypto_des.h" | 21 | #include "crypto_des.h" |
24 | 22 | ||
@@ -46,37 +44,30 @@ struct crypt_s390_des3_192_ctx { | |||
46 | u8 key[DES3_192_KEY_SIZE]; | 44 | u8 key[DES3_192_KEY_SIZE]; |
47 | }; | 45 | }; |
48 | 46 | ||
49 | static int | 47 | static int des_setkey(void *ctx, const u8 *key, unsigned int keylen, |
50 | des_setkey(void *ctx, const u8 *key, unsigned int keylen, u32 *flags) | 48 | u32 *flags) |
51 | { | 49 | { |
52 | struct crypt_s390_des_ctx *dctx; | 50 | struct crypt_s390_des_ctx *dctx = ctx; |
53 | int ret; | 51 | int ret; |
54 | 52 | ||
55 | dctx = ctx; | 53 | /* test if key is valid (not a weak key) */ |
56 | //test if key is valid (not a weak key) | ||
57 | ret = crypto_des_check_key(key, keylen, flags); | 54 | ret = crypto_des_check_key(key, keylen, flags); |
58 | if (ret == 0){ | 55 | if (ret == 0) |
59 | memcpy(dctx->key, key, keylen); | 56 | memcpy(dctx->key, key, keylen); |
60 | } | ||
61 | return ret; | 57 | return ret; |
62 | } | 58 | } |
63 | 59 | ||
64 | 60 | static void des_encrypt(void *ctx, u8 *dst, const u8 *src) | |
65 | static void | ||
66 | des_encrypt(void *ctx, u8 *dst, const u8 *src) | ||
67 | { | 61 | { |
68 | struct crypt_s390_des_ctx *dctx; | 62 | struct crypt_s390_des_ctx *dctx = ctx; |
69 | 63 | ||
70 | dctx = ctx; | ||
71 | crypt_s390_km(KM_DEA_ENCRYPT, dctx->key, dst, src, DES_BLOCK_SIZE); | 64 | crypt_s390_km(KM_DEA_ENCRYPT, dctx->key, dst, src, DES_BLOCK_SIZE); |
72 | } | 65 | } |
73 | 66 | ||
74 | static void | 67 | static void des_decrypt(void *ctx, u8 *dst, const u8 *src) |
75 | des_decrypt(void *ctx, u8 *dst, const u8 *src) | ||
76 | { | 68 | { |
77 | struct crypt_s390_des_ctx *dctx; | 69 | struct crypt_s390_des_ctx *dctx = ctx; |
78 | 70 | ||
79 | dctx = ctx; | ||
80 | crypt_s390_km(KM_DEA_DECRYPT, dctx->key, dst, src, DES_BLOCK_SIZE); | 71 | crypt_s390_km(KM_DEA_DECRYPT, dctx->key, dst, src, DES_BLOCK_SIZE); |
81 | } | 72 | } |
82 | 73 | ||
@@ -87,12 +78,15 @@ static struct crypto_alg des_alg = { | |||
87 | .cra_ctxsize = sizeof(struct crypt_s390_des_ctx), | 78 | .cra_ctxsize = sizeof(struct crypt_s390_des_ctx), |
88 | .cra_module = THIS_MODULE, | 79 | .cra_module = THIS_MODULE, |
89 | .cra_list = LIST_HEAD_INIT(des_alg.cra_list), | 80 | .cra_list = LIST_HEAD_INIT(des_alg.cra_list), |
90 | .cra_u = { .cipher = { | 81 | .cra_u = { |
91 | .cia_min_keysize = DES_KEY_SIZE, | 82 | .cipher = { |
92 | .cia_max_keysize = DES_KEY_SIZE, | 83 | .cia_min_keysize = DES_KEY_SIZE, |
93 | .cia_setkey = des_setkey, | 84 | .cia_max_keysize = DES_KEY_SIZE, |
94 | .cia_encrypt = des_encrypt, | 85 | .cia_setkey = des_setkey, |
95 | .cia_decrypt = des_decrypt } } | 86 | .cia_encrypt = des_encrypt, |
87 | .cia_decrypt = des_decrypt | ||
88 | } | ||
89 | } | ||
96 | }; | 90 | }; |
97 | 91 | ||
98 | /* | 92 | /* |
@@ -107,20 +101,18 @@ static struct crypto_alg des_alg = { | |||
107 | * Implementers MUST reject keys that exhibit this property. | 101 | * Implementers MUST reject keys that exhibit this property. |
108 | * | 102 | * |
109 | */ | 103 | */ |
110 | static int | 104 | static int des3_128_setkey(void *ctx, const u8 *key, unsigned int keylen, |
111 | des3_128_setkey(void *ctx, const u8 *key, unsigned int keylen, u32 *flags) | 105 | u32 *flags) |
112 | { | 106 | { |
113 | int i, ret; | 107 | int i, ret; |
114 | struct crypt_s390_des3_128_ctx *dctx; | 108 | struct crypt_s390_des3_128_ctx *dctx = ctx; |
115 | const u8* temp_key = key; | 109 | const u8* temp_key = key; |
116 | 110 | ||
117 | dctx = ctx; | ||
118 | if (!(memcmp(key, &key[DES_KEY_SIZE], DES_KEY_SIZE))) { | 111 | if (!(memcmp(key, &key[DES_KEY_SIZE], DES_KEY_SIZE))) { |
119 | |||
120 | *flags |= CRYPTO_TFM_RES_BAD_KEY_SCHED; | 112 | *flags |= CRYPTO_TFM_RES_BAD_KEY_SCHED; |
121 | return -EINVAL; | 113 | return -EINVAL; |
122 | } | 114 | } |
123 | for (i = 0; i < 2; i++, temp_key += DES_KEY_SIZE) { | 115 | for (i = 0; i < 2; i++, temp_key += DES_KEY_SIZE) { |
124 | ret = crypto_des_check_key(temp_key, DES_KEY_SIZE, flags); | 116 | ret = crypto_des_check_key(temp_key, DES_KEY_SIZE, flags); |
125 | if (ret < 0) | 117 | if (ret < 0) |
126 | return ret; | 118 | return ret; |
@@ -129,24 +121,20 @@ des3_128_setkey(void *ctx, const u8 *key, unsigned int keylen, u32 *flags) | |||
129 | return 0; | 121 | return 0; |
130 | } | 122 | } |
131 | 123 | ||
132 | static void | 124 | static void des3_128_encrypt(void *ctx, u8 *dst, const u8 *src) |
133 | des3_128_encrypt(void *ctx, u8 *dst, const u8 *src) | ||
134 | { | 125 | { |
135 | struct crypt_s390_des3_128_ctx *dctx; | 126 | struct crypt_s390_des3_128_ctx *dctx = ctx; |
136 | 127 | ||
137 | dctx = ctx; | ||
138 | crypt_s390_km(KM_TDEA_128_ENCRYPT, dctx->key, dst, (void*)src, | 128 | crypt_s390_km(KM_TDEA_128_ENCRYPT, dctx->key, dst, (void*)src, |
139 | DES3_128_BLOCK_SIZE); | 129 | DES3_128_BLOCK_SIZE); |
140 | } | 130 | } |
141 | 131 | ||
142 | static void | 132 | static void des3_128_decrypt(void *ctx, u8 *dst, const u8 *src) |
143 | des3_128_decrypt(void *ctx, u8 *dst, const u8 *src) | ||
144 | { | 133 | { |
145 | struct crypt_s390_des3_128_ctx *dctx; | 134 | struct crypt_s390_des3_128_ctx *dctx = ctx; |
146 | 135 | ||
147 | dctx = ctx; | ||
148 | crypt_s390_km(KM_TDEA_128_DECRYPT, dctx->key, dst, (void*)src, | 136 | crypt_s390_km(KM_TDEA_128_DECRYPT, dctx->key, dst, (void*)src, |
149 | DES3_128_BLOCK_SIZE); | 137 | DES3_128_BLOCK_SIZE); |
150 | } | 138 | } |
151 | 139 | ||
152 | static struct crypto_alg des3_128_alg = { | 140 | static struct crypto_alg des3_128_alg = { |
@@ -156,12 +144,15 @@ static struct crypto_alg des3_128_alg = { | |||
156 | .cra_ctxsize = sizeof(struct crypt_s390_des3_128_ctx), | 144 | .cra_ctxsize = sizeof(struct crypt_s390_des3_128_ctx), |
157 | .cra_module = THIS_MODULE, | 145 | .cra_module = THIS_MODULE, |
158 | .cra_list = LIST_HEAD_INIT(des3_128_alg.cra_list), | 146 | .cra_list = LIST_HEAD_INIT(des3_128_alg.cra_list), |
159 | .cra_u = { .cipher = { | 147 | .cra_u = { |
160 | .cia_min_keysize = DES3_128_KEY_SIZE, | 148 | .cipher = { |
161 | .cia_max_keysize = DES3_128_KEY_SIZE, | 149 | .cia_min_keysize = DES3_128_KEY_SIZE, |
162 | .cia_setkey = des3_128_setkey, | 150 | .cia_max_keysize = DES3_128_KEY_SIZE, |
163 | .cia_encrypt = des3_128_encrypt, | 151 | .cia_setkey = des3_128_setkey, |
164 | .cia_decrypt = des3_128_decrypt } } | 152 | .cia_encrypt = des3_128_encrypt, |
153 | .cia_decrypt = des3_128_decrypt | ||
154 | } | ||
155 | } | ||
165 | }; | 156 | }; |
166 | 157 | ||
167 | /* | 158 | /* |
@@ -177,50 +168,43 @@ static struct crypto_alg des3_128_alg = { | |||
177 | * property. | 168 | * property. |
178 | * | 169 | * |
179 | */ | 170 | */ |
180 | static int | 171 | static int des3_192_setkey(void *ctx, const u8 *key, unsigned int keylen, |
181 | des3_192_setkey(void *ctx, const u8 *key, unsigned int keylen, u32 *flags) | 172 | u32 *flags) |
182 | { | 173 | { |
183 | int i, ret; | 174 | int i, ret; |
184 | struct crypt_s390_des3_192_ctx *dctx; | 175 | struct crypt_s390_des3_192_ctx *dctx = ctx; |
185 | const u8* temp_key; | 176 | const u8* temp_key = key; |
186 | 177 | ||
187 | dctx = ctx; | ||
188 | temp_key = key; | ||
189 | if (!(memcmp(key, &key[DES_KEY_SIZE], DES_KEY_SIZE) && | 178 | if (!(memcmp(key, &key[DES_KEY_SIZE], DES_KEY_SIZE) && |
190 | memcmp(&key[DES_KEY_SIZE], &key[DES_KEY_SIZE * 2], | 179 | memcmp(&key[DES_KEY_SIZE], &key[DES_KEY_SIZE * 2], |
191 | DES_KEY_SIZE))) { | 180 | DES_KEY_SIZE))) { |
192 | 181 | ||
193 | *flags |= CRYPTO_TFM_RES_BAD_KEY_SCHED; | 182 | *flags |= CRYPTO_TFM_RES_BAD_KEY_SCHED; |
194 | return -EINVAL; | 183 | return -EINVAL; |
195 | } | 184 | } |
196 | for (i = 0; i < 3; i++, temp_key += DES_KEY_SIZE) { | 185 | for (i = 0; i < 3; i++, temp_key += DES_KEY_SIZE) { |
197 | ret = crypto_des_check_key(temp_key, DES_KEY_SIZE, flags); | 186 | ret = crypto_des_check_key(temp_key, DES_KEY_SIZE, flags); |
198 | if (ret < 0){ | 187 | if (ret < 0) |
199 | return ret; | 188 | return ret; |
200 | } | ||
201 | } | 189 | } |
202 | memcpy(dctx->key, key, keylen); | 190 | memcpy(dctx->key, key, keylen); |
203 | return 0; | 191 | return 0; |
204 | } | 192 | } |
205 | 193 | ||
206 | static void | 194 | static void des3_192_encrypt(void *ctx, u8 *dst, const u8 *src) |
207 | des3_192_encrypt(void *ctx, u8 *dst, const u8 *src) | ||
208 | { | 195 | { |
209 | struct crypt_s390_des3_192_ctx *dctx; | 196 | struct crypt_s390_des3_192_ctx *dctx = ctx; |
210 | 197 | ||
211 | dctx = ctx; | ||
212 | crypt_s390_km(KM_TDEA_192_ENCRYPT, dctx->key, dst, (void*)src, | 198 | crypt_s390_km(KM_TDEA_192_ENCRYPT, dctx->key, dst, (void*)src, |
213 | DES3_192_BLOCK_SIZE); | 199 | DES3_192_BLOCK_SIZE); |
214 | } | 200 | } |
215 | 201 | ||
216 | static void | 202 | static void des3_192_decrypt(void *ctx, u8 *dst, const u8 *src) |
217 | des3_192_decrypt(void *ctx, u8 *dst, const u8 *src) | ||
218 | { | 203 | { |
219 | struct crypt_s390_des3_192_ctx *dctx; | 204 | struct crypt_s390_des3_192_ctx *dctx = ctx; |
220 | 205 | ||
221 | dctx = ctx; | ||
222 | crypt_s390_km(KM_TDEA_192_DECRYPT, dctx->key, dst, (void*)src, | 206 | crypt_s390_km(KM_TDEA_192_DECRYPT, dctx->key, dst, (void*)src, |
223 | DES3_192_BLOCK_SIZE); | 207 | DES3_192_BLOCK_SIZE); |
224 | } | 208 | } |
225 | 209 | ||
226 | static struct crypto_alg des3_192_alg = { | 210 | static struct crypto_alg des3_192_alg = { |
@@ -230,44 +214,39 @@ static struct crypto_alg des3_192_alg = { | |||
230 | .cra_ctxsize = sizeof(struct crypt_s390_des3_192_ctx), | 214 | .cra_ctxsize = sizeof(struct crypt_s390_des3_192_ctx), |
231 | .cra_module = THIS_MODULE, | 215 | .cra_module = THIS_MODULE, |
232 | .cra_list = LIST_HEAD_INIT(des3_192_alg.cra_list), | 216 | .cra_list = LIST_HEAD_INIT(des3_192_alg.cra_list), |
233 | .cra_u = { .cipher = { | 217 | .cra_u = { |
234 | .cia_min_keysize = DES3_192_KEY_SIZE, | 218 | .cipher = { |
235 | .cia_max_keysize = DES3_192_KEY_SIZE, | 219 | .cia_min_keysize = DES3_192_KEY_SIZE, |
236 | .cia_setkey = des3_192_setkey, | 220 | .cia_max_keysize = DES3_192_KEY_SIZE, |
237 | .cia_encrypt = des3_192_encrypt, | 221 | .cia_setkey = des3_192_setkey, |
238 | .cia_decrypt = des3_192_decrypt } } | 222 | .cia_encrypt = des3_192_encrypt, |
223 | .cia_decrypt = des3_192_decrypt | ||
224 | } | ||
225 | } | ||
239 | }; | 226 | }; |
240 | 227 | ||
241 | 228 | static int init(void) | |
242 | |||
243 | static int | ||
244 | init(void) | ||
245 | { | 229 | { |
246 | int ret; | 230 | int ret = 0; |
247 | 231 | ||
248 | if (!crypt_s390_func_available(KM_DEA_ENCRYPT) || | 232 | if (!crypt_s390_func_available(KM_DEA_ENCRYPT) || |
249 | !crypt_s390_func_available(KM_TDEA_128_ENCRYPT) || | 233 | !crypt_s390_func_available(KM_TDEA_128_ENCRYPT) || |
250 | !crypt_s390_func_available(KM_TDEA_192_ENCRYPT)){ | 234 | !crypt_s390_func_available(KM_TDEA_192_ENCRYPT)) |
251 | return -ENOSYS; | 235 | return -ENOSYS; |
252 | } | ||
253 | 236 | ||
254 | ret = 0; | 237 | ret |= (crypto_register_alg(&des_alg) == 0) ? 0:1; |
255 | ret |= (crypto_register_alg(&des_alg) == 0)? 0:1; | 238 | ret |= (crypto_register_alg(&des3_128_alg) == 0) ? 0:2; |
256 | ret |= (crypto_register_alg(&des3_128_alg) == 0)? 0:2; | 239 | ret |= (crypto_register_alg(&des3_192_alg) == 0) ? 0:4; |
257 | ret |= (crypto_register_alg(&des3_192_alg) == 0)? 0:4; | 240 | if (ret) { |
258 | if (ret){ | ||
259 | crypto_unregister_alg(&des3_192_alg); | 241 | crypto_unregister_alg(&des3_192_alg); |
260 | crypto_unregister_alg(&des3_128_alg); | 242 | crypto_unregister_alg(&des3_128_alg); |
261 | crypto_unregister_alg(&des_alg); | 243 | crypto_unregister_alg(&des_alg); |
262 | return -EEXIST; | 244 | return -EEXIST; |
263 | } | 245 | } |
264 | |||
265 | printk(KERN_INFO "crypt_s390: des_s390 loaded.\n"); | ||
266 | return 0; | 246 | return 0; |
267 | } | 247 | } |
268 | 248 | ||
269 | static void __exit | 249 | static void __exit fini(void) |
270 | fini(void) | ||
271 | { | 250 | { |
272 | crypto_unregister_alg(&des3_192_alg); | 251 | crypto_unregister_alg(&des3_192_alg); |
273 | crypto_unregister_alg(&des3_128_alg); | 252 | crypto_unregister_alg(&des3_128_alg); |