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

authored by Jan Glauber and committed by Linus Torvalds c1357833 7aa89746

+67 -88
+67 -88
arch/s390/crypto/des_s390.c
··· 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 ··· 44 46 u8 key[DES3_192_KEY_SIZE]; 45 47 }; 46 48 47 - static int 48 - des_setkey(void *ctx, const u8 *key, unsigned int keylen, u32 *flags) 49 + static int des_setkey(void *ctx, const u8 *key, unsigned int keylen, 50 + u32 *flags) 49 51 { 50 - struct crypt_s390_des_ctx *dctx; 52 + struct crypt_s390_des_ctx *dctx = ctx; 51 53 int ret; 52 54 53 - dctx = ctx; 54 - //test if key is valid (not a weak key) 55 + /* test if key is valid (not a weak key) */ 55 56 ret = crypto_des_check_key(key, keylen, flags); 56 - if (ret == 0){ 57 + if (ret == 0) 57 58 memcpy(dctx->key, key, keylen); 58 - } 59 59 return ret; 60 60 } 61 61 62 - 63 - static void 64 - des_encrypt(void *ctx, u8 *dst, const u8 *src) 62 + static void des_encrypt(void *ctx, u8 *dst, const u8 *src) 65 63 { 66 - struct crypt_s390_des_ctx *dctx; 64 + struct crypt_s390_des_ctx *dctx = ctx; 67 65 68 - dctx = ctx; 69 66 crypt_s390_km(KM_DEA_ENCRYPT, dctx->key, dst, src, DES_BLOCK_SIZE); 70 67 } 71 68 72 - static void 73 - des_decrypt(void *ctx, u8 *dst, const u8 *src) 69 + static void des_decrypt(void *ctx, u8 *dst, const u8 *src) 74 70 { 75 - struct crypt_s390_des_ctx *dctx; 71 + struct crypt_s390_des_ctx *dctx = ctx; 76 72 77 - dctx = ctx; 78 73 crypt_s390_km(KM_DEA_DECRYPT, dctx->key, dst, src, DES_BLOCK_SIZE); 79 74 } 80 75 ··· 78 87 .cra_ctxsize = sizeof(struct crypt_s390_des_ctx), 79 88 .cra_module = THIS_MODULE, 80 89 .cra_list = LIST_HEAD_INIT(des_alg.cra_list), 81 - .cra_u = { .cipher = { 82 - .cia_min_keysize = DES_KEY_SIZE, 83 - .cia_max_keysize = DES_KEY_SIZE, 84 - .cia_setkey = des_setkey, 85 - .cia_encrypt = des_encrypt, 86 - .cia_decrypt = des_decrypt } } 90 + .cra_u = { 91 + .cipher = { 92 + .cia_min_keysize = DES_KEY_SIZE, 93 + .cia_max_keysize = DES_KEY_SIZE, 94 + .cia_setkey = des_setkey, 95 + .cia_encrypt = des_encrypt, 96 + .cia_decrypt = des_decrypt 97 + } 98 + } 87 99 }; 88 100 89 101 /* ··· 101 107 * Implementers MUST reject keys that exhibit this property. 102 108 * 103 109 */ 104 - static int 105 - des3_128_setkey(void *ctx, const u8 *key, unsigned int keylen, u32 *flags) 110 + static int des3_128_setkey(void *ctx, const u8 *key, unsigned int keylen, 111 + u32 *flags) 106 112 { 107 113 int i, ret; 108 - struct crypt_s390_des3_128_ctx *dctx; 114 + struct crypt_s390_des3_128_ctx *dctx = ctx; 109 115 const u8* temp_key = key; 110 116 111 - dctx = ctx; 112 117 if (!(memcmp(key, &key[DES_KEY_SIZE], DES_KEY_SIZE))) { 113 - 114 118 *flags |= CRYPTO_TFM_RES_BAD_KEY_SCHED; 115 119 return -EINVAL; 116 120 } 117 - for (i = 0; i < 2; i++, temp_key += DES_KEY_SIZE) { 121 + for (i = 0; i < 2; i++, temp_key += DES_KEY_SIZE) { 118 122 ret = crypto_des_check_key(temp_key, DES_KEY_SIZE, flags); 119 123 if (ret < 0) 120 124 return ret; ··· 121 129 return 0; 122 130 } 123 131 124 - static void 125 - des3_128_encrypt(void *ctx, u8 *dst, const u8 *src) 132 + static void des3_128_encrypt(void *ctx, u8 *dst, const u8 *src) 126 133 { 127 - struct crypt_s390_des3_128_ctx *dctx; 134 + struct crypt_s390_des3_128_ctx *dctx = ctx; 128 135 129 - dctx = ctx; 130 136 crypt_s390_km(KM_TDEA_128_ENCRYPT, dctx->key, dst, (void*)src, 131 - DES3_128_BLOCK_SIZE); 137 + DES3_128_BLOCK_SIZE); 132 138 } 133 139 134 - static void 135 - des3_128_decrypt(void *ctx, u8 *dst, const u8 *src) 140 + static void des3_128_decrypt(void *ctx, u8 *dst, const u8 *src) 136 141 { 137 - struct crypt_s390_des3_128_ctx *dctx; 142 + struct crypt_s390_des3_128_ctx *dctx = ctx; 138 143 139 - dctx = ctx; 140 144 crypt_s390_km(KM_TDEA_128_DECRYPT, dctx->key, dst, (void*)src, 141 - DES3_128_BLOCK_SIZE); 145 + DES3_128_BLOCK_SIZE); 142 146 } 143 147 144 148 static struct crypto_alg des3_128_alg = { ··· 144 156 .cra_ctxsize = sizeof(struct crypt_s390_des3_128_ctx), 145 157 .cra_module = THIS_MODULE, 146 158 .cra_list = LIST_HEAD_INIT(des3_128_alg.cra_list), 147 - .cra_u = { .cipher = { 148 - .cia_min_keysize = DES3_128_KEY_SIZE, 149 - .cia_max_keysize = DES3_128_KEY_SIZE, 150 - .cia_setkey = des3_128_setkey, 151 - .cia_encrypt = des3_128_encrypt, 152 - .cia_decrypt = des3_128_decrypt } } 159 + .cra_u = { 160 + .cipher = { 161 + .cia_min_keysize = DES3_128_KEY_SIZE, 162 + .cia_max_keysize = DES3_128_KEY_SIZE, 163 + .cia_setkey = des3_128_setkey, 164 + .cia_encrypt = des3_128_encrypt, 165 + .cia_decrypt = des3_128_decrypt 166 + } 167 + } 153 168 }; 154 169 155 170 /* ··· 168 177 * property. 169 178 * 170 179 */ 171 - static int 172 - des3_192_setkey(void *ctx, const u8 *key, unsigned int keylen, u32 *flags) 180 + static int des3_192_setkey(void *ctx, const u8 *key, unsigned int keylen, 181 + u32 *flags) 173 182 { 174 183 int i, ret; 175 - struct crypt_s390_des3_192_ctx *dctx; 176 - const u8* temp_key; 184 + struct crypt_s390_des3_192_ctx *dctx = ctx; 185 + const u8* temp_key = key; 177 186 178 - dctx = ctx; 179 - temp_key = key; 180 187 if (!(memcmp(key, &key[DES_KEY_SIZE], DES_KEY_SIZE) && 181 188 memcmp(&key[DES_KEY_SIZE], &key[DES_KEY_SIZE * 2], 182 - DES_KEY_SIZE))) { 189 + DES_KEY_SIZE))) { 183 190 184 191 *flags |= CRYPTO_TFM_RES_BAD_KEY_SCHED; 185 192 return -EINVAL; 186 193 } 187 194 for (i = 0; i < 3; i++, temp_key += DES_KEY_SIZE) { 188 195 ret = crypto_des_check_key(temp_key, DES_KEY_SIZE, flags); 189 - if (ret < 0){ 196 + if (ret < 0) 190 197 return ret; 191 - } 192 198 } 193 199 memcpy(dctx->key, key, keylen); 194 200 return 0; 195 201 } 196 202 197 - static void 198 - des3_192_encrypt(void *ctx, u8 *dst, const u8 *src) 203 + static void des3_192_encrypt(void *ctx, u8 *dst, const u8 *src) 199 204 { 200 - struct crypt_s390_des3_192_ctx *dctx; 205 + struct crypt_s390_des3_192_ctx *dctx = ctx; 201 206 202 - dctx = ctx; 203 207 crypt_s390_km(KM_TDEA_192_ENCRYPT, dctx->key, dst, (void*)src, 204 - DES3_192_BLOCK_SIZE); 208 + DES3_192_BLOCK_SIZE); 205 209 } 206 210 207 - static void 208 - des3_192_decrypt(void *ctx, u8 *dst, const u8 *src) 211 + static void des3_192_decrypt(void *ctx, u8 *dst, const u8 *src) 209 212 { 210 - struct crypt_s390_des3_192_ctx *dctx; 213 + struct crypt_s390_des3_192_ctx *dctx = ctx; 211 214 212 - dctx = ctx; 213 215 crypt_s390_km(KM_TDEA_192_DECRYPT, dctx->key, dst, (void*)src, 214 - DES3_192_BLOCK_SIZE); 216 + DES3_192_BLOCK_SIZE); 215 217 } 216 218 217 219 static struct crypto_alg des3_192_alg = { ··· 214 230 .cra_ctxsize = sizeof(struct crypt_s390_des3_192_ctx), 215 231 .cra_module = THIS_MODULE, 216 232 .cra_list = LIST_HEAD_INIT(des3_192_alg.cra_list), 217 - .cra_u = { .cipher = { 218 - .cia_min_keysize = DES3_192_KEY_SIZE, 219 - .cia_max_keysize = DES3_192_KEY_SIZE, 220 - .cia_setkey = des3_192_setkey, 221 - .cia_encrypt = des3_192_encrypt, 222 - .cia_decrypt = des3_192_decrypt } } 233 + .cra_u = { 234 + .cipher = { 235 + .cia_min_keysize = DES3_192_KEY_SIZE, 236 + .cia_max_keysize = DES3_192_KEY_SIZE, 237 + .cia_setkey = des3_192_setkey, 238 + .cia_encrypt = des3_192_encrypt, 239 + .cia_decrypt = des3_192_decrypt 240 + } 241 + } 223 242 }; 224 243 225 - 226 - 227 - static int 228 - init(void) 244 + static int init(void) 229 245 { 230 - int ret; 246 + int ret = 0; 231 247 232 248 if (!crypt_s390_func_available(KM_DEA_ENCRYPT) || 233 249 !crypt_s390_func_available(KM_TDEA_128_ENCRYPT) || 234 - !crypt_s390_func_available(KM_TDEA_192_ENCRYPT)){ 250 + !crypt_s390_func_available(KM_TDEA_192_ENCRYPT)) 235 251 return -ENOSYS; 236 - } 237 252 238 - ret = 0; 239 - ret |= (crypto_register_alg(&des_alg) == 0)? 0:1; 240 - ret |= (crypto_register_alg(&des3_128_alg) == 0)? 0:2; 241 - ret |= (crypto_register_alg(&des3_192_alg) == 0)? 0:4; 242 - if (ret){ 253 + ret |= (crypto_register_alg(&des_alg) == 0) ? 0:1; 254 + ret |= (crypto_register_alg(&des3_128_alg) == 0) ? 0:2; 255 + ret |= (crypto_register_alg(&des3_192_alg) == 0) ? 0:4; 256 + if (ret) { 243 257 crypto_unregister_alg(&des3_192_alg); 244 258 crypto_unregister_alg(&des3_128_alg); 245 259 crypto_unregister_alg(&des_alg); 246 260 return -EEXIST; 247 261 } 248 - 249 - printk(KERN_INFO "crypt_s390: des_s390 loaded.\n"); 250 262 return 0; 251 263 } 252 264 253 - static void __exit 254 - fini(void) 265 + static void __exit fini(void) 255 266 { 256 267 crypto_unregister_alg(&des3_192_alg); 257 268 crypto_unregister_alg(&des3_128_alg);