Commit c1357833bf5d92b1bda41215ae88a6597664251b

Authored by Jan Glauber
Committed by Linus Torvalds
1 parent 7aa89746e8

[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>

Showing 1 changed file with 67 additions and 88 deletions Side-by-side Diff

arch/s390/crypto/des_s390.c
... ... @@ -15,10 +15,8 @@
15 15 */
16 16 #include <linux/init.h>
17 17 #include <linux/module.h>
18   -#include <linux/mm.h>
19   -#include <linux/errno.h>
20   -#include <asm/scatterlist.h>
21 18 #include <linux/crypto.h>
  19 +
22 20 #include "crypt_s390.h"
23 21 #include "crypto_des.h"
24 22  
25 23  
26 24  
27 25  
28 26  
29 27  
30 28  
31 29  
32 30  
33 31  
34 32  
... ... @@ -46,37 +44,30 @@
46 44 u8 key[DES3_192_KEY_SIZE];
47 45 };
48 46  
49   -static int
50   -des_setkey(void *ctx, const u8 *key, unsigned int keylen, u32 *flags)
  47 +static int des_setkey(void *ctx, const u8 *key, unsigned int keylen,
  48 + u32 *flags)
51 49 {
52   - struct crypt_s390_des_ctx *dctx;
  50 + struct crypt_s390_des_ctx *dctx = ctx;
53 51 int ret;
54 52  
55   - dctx = ctx;
56   - //test if key is valid (not a weak key)
  53 + /* test if key is valid (not a weak key) */
57 54 ret = crypto_des_check_key(key, keylen, flags);
58   - if (ret == 0){
  55 + if (ret == 0)
59 56 memcpy(dctx->key, key, keylen);
60   - }
61 57 return ret;
62 58 }
63 59  
64   -
65   -static void
66   -des_encrypt(void *ctx, u8 *dst, const u8 *src)
  60 +static void 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 64 crypt_s390_km(KM_DEA_ENCRYPT, dctx->key, dst, src, DES_BLOCK_SIZE);
72 65 }
73 66  
74   -static void
75   -des_decrypt(void *ctx, u8 *dst, const u8 *src)
  67 +static void 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 71 crypt_s390_km(KM_DEA_DECRYPT, dctx->key, dst, src, DES_BLOCK_SIZE);
81 72 }
82 73  
... ... @@ -87,12 +78,15 @@
87 78 .cra_ctxsize = sizeof(struct crypt_s390_des_ctx),
88 79 .cra_module = THIS_MODULE,
89 80 .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 } }
  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
  88 + }
  89 + }
96 90 };
97 91  
98 92 /*
99 93  
100 94  
101 95  
102 96  
... ... @@ -107,20 +101,18 @@
107 101 * Implementers MUST reject keys that exhibit this property.
108 102 *
109 103 */
110   -static int
111   -des3_128_setkey(void *ctx, const u8 *key, unsigned int keylen, u32 *flags)
  104 +static int des3_128_setkey(void *ctx, const u8 *key, unsigned int keylen,
  105 + u32 *flags)
112 106 {
113 107 int i, ret;
114   - struct crypt_s390_des3_128_ctx *dctx;
  108 + struct crypt_s390_des3_128_ctx *dctx = ctx;
115 109 const u8* temp_key = key;
116 110  
117   - dctx = ctx;
118 111 if (!(memcmp(key, &key[DES_KEY_SIZE], DES_KEY_SIZE))) {
119   -
120 112 *flags |= CRYPTO_TFM_RES_BAD_KEY_SCHED;
121 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 116 ret = crypto_des_check_key(temp_key, DES_KEY_SIZE, flags);
125 117 if (ret < 0)
126 118 return ret;
127 119  
128 120  
129 121  
130 122  
131 123  
132 124  
133 125  
... ... @@ -129,24 +121,20 @@
129 121 return 0;
130 122 }
131 123  
132   -static void
133   -des3_128_encrypt(void *ctx, u8 *dst, const u8 *src)
  124 +static void 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 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
143   -des3_128_decrypt(void *ctx, u8 *dst, const u8 *src)
  132 +static void 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 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 140 static struct crypto_alg des3_128_alg = {
... ... @@ -156,12 +144,15 @@
156 144 .cra_ctxsize = sizeof(struct crypt_s390_des3_128_ctx),
157 145 .cra_module = THIS_MODULE,
158 146 .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 } }
  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
  154 + }
  155 + }
165 156 };
166 157  
167 158 /*
168 159  
169 160  
170 161  
171 162  
172 163  
173 164  
174 165  
175 166  
176 167  
177 168  
178 169  
179 170  
180 171  
... ... @@ -177,50 +168,43 @@
177 168 * property.
178 169 *
179 170 */
180   -static int
181   -des3_192_setkey(void *ctx, const u8 *key, unsigned int keylen, u32 *flags)
  171 +static int des3_192_setkey(void *ctx, const u8 *key, unsigned int keylen,
  172 + u32 *flags)
182 173 {
183 174 int i, ret;
184   - struct crypt_s390_des3_192_ctx *dctx;
185   - const u8* temp_key;
  175 + struct crypt_s390_des3_192_ctx *dctx = ctx;
  176 + const u8* temp_key = key;
186 177  
187   - dctx = ctx;
188   - temp_key = key;
189 178 if (!(memcmp(key, &key[DES_KEY_SIZE], DES_KEY_SIZE) &&
190 179 memcmp(&key[DES_KEY_SIZE], &key[DES_KEY_SIZE * 2],
191   - DES_KEY_SIZE))) {
  180 + DES_KEY_SIZE))) {
192 181  
193 182 *flags |= CRYPTO_TFM_RES_BAD_KEY_SCHED;
194 183 return -EINVAL;
195 184 }
196 185 for (i = 0; i < 3; i++, temp_key += DES_KEY_SIZE) {
197 186 ret = crypto_des_check_key(temp_key, DES_KEY_SIZE, flags);
198   - if (ret < 0){
  187 + if (ret < 0)
199 188 return ret;
200   - }
201 189 }
202 190 memcpy(dctx->key, key, keylen);
203 191 return 0;
204 192 }
205 193  
206   -static void
207   -des3_192_encrypt(void *ctx, u8 *dst, const u8 *src)
  194 +static void 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 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
217   -des3_192_decrypt(void *ctx, u8 *dst, const u8 *src)
  202 +static void 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 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 210 static struct crypto_alg des3_192_alg = {
227 211  
228 212  
229 213  
230 214  
231 215  
232 216  
233 217  
... ... @@ -230,44 +214,39 @@
230 214 .cra_ctxsize = sizeof(struct crypt_s390_des3_192_ctx),
231 215 .cra_module = THIS_MODULE,
232 216 .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 } }
  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
  224 + }
  225 + }
239 226 };
240 227  
241   -
242   -
243   -static int
244   -init(void)
  228 +static int init(void)
245 229 {
246   - int ret;
  230 + int ret = 0;
247 231  
248 232 if (!crypt_s390_func_available(KM_DEA_ENCRYPT) ||
249 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 235 return -ENOSYS;
252   - }
253 236  
254   - ret = 0;
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;
258   - if (ret){
  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) {
259 241 crypto_unregister_alg(&des3_192_alg);
260 242 crypto_unregister_alg(&des3_128_alg);
261 243 crypto_unregister_alg(&des_alg);
262 244 return -EEXIST;
263 245 }
264   -
265   - printk(KERN_INFO "crypt_s390: des_s390 loaded.\n");
266 246 return 0;
267 247 }
268 248  
269   -static void __exit
270   -fini(void)
  249 +static void __exit fini(void)
271 250 {
272 251 crypto_unregister_alg(&des3_192_alg);
273 252 crypto_unregister_alg(&des3_128_alg);