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