Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

crypto: skcipher - use crypto_grab_cipher() and simplify error paths

Make skcipher_alloc_instance_simple() use the new function
crypto_grab_cipher() to initialize its cipher spawn.

This is needed to make all spawns be initialized in a consistent way.

Also simplify the error handling by taking advantage of crypto_drop_*()
now accepting (as a no-op) spawns that haven't been initialized yet, and
by taking advantage of crypto_grab_*() now handling ERR_PTR() names.

Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

authored by

Eric Biggers and committed by
Herbert Xu
aacd5b4c c282586f

+17 -26
+15 -24
crypto/skcipher.c
··· 908 908 909 909 static void skcipher_free_instance_simple(struct skcipher_instance *inst) 910 910 { 911 - crypto_drop_spawn(skcipher_instance_ctx(inst)); 911 + crypto_drop_cipher(skcipher_instance_ctx(inst)); 912 912 kfree(inst); 913 913 } 914 914 ··· 932 932 struct crypto_template *tmpl, struct rtattr **tb) 933 933 { 934 934 struct crypto_attr_type *algt; 935 - struct crypto_alg *cipher_alg; 936 - struct skcipher_instance *inst; 937 - struct crypto_spawn *spawn; 938 935 u32 mask; 936 + struct skcipher_instance *inst; 937 + struct crypto_cipher_spawn *spawn; 938 + struct crypto_alg *cipher_alg; 939 939 int err; 940 940 941 941 algt = crypto_get_attr_type(tb); ··· 945 945 if ((algt->type ^ CRYPTO_ALG_TYPE_SKCIPHER) & algt->mask) 946 946 return ERR_PTR(-EINVAL); 947 947 948 - mask = CRYPTO_ALG_TYPE_MASK | 949 - crypto_requires_off(algt->type, algt->mask, 950 - CRYPTO_ALG_NEED_FALLBACK); 951 - 952 - cipher_alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER, mask); 953 - if (IS_ERR(cipher_alg)) 954 - return ERR_CAST(cipher_alg); 948 + mask = crypto_requires_off(algt->type, algt->mask, 949 + CRYPTO_ALG_NEED_FALLBACK); 955 950 956 951 inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL); 957 - if (!inst) { 958 - err = -ENOMEM; 959 - goto err_put_cipher_alg; 960 - } 952 + if (!inst) 953 + return ERR_PTR(-ENOMEM); 961 954 spawn = skcipher_instance_ctx(inst); 955 + 956 + err = crypto_grab_cipher(spawn, skcipher_crypto_instance(inst), 957 + crypto_attr_alg_name(tb[1]), 0, mask); 958 + if (err) 959 + goto err_free_inst; 960 + cipher_alg = crypto_spawn_cipher_alg(spawn); 962 961 963 962 err = crypto_inst_setname(skcipher_crypto_instance(inst), tmpl->name, 964 963 cipher_alg); 965 964 if (err) 966 965 goto err_free_inst; 967 966 968 - spawn->dropref = true; 969 - err = crypto_init_spawn(spawn, cipher_alg, 970 - skcipher_crypto_instance(inst), 971 - CRYPTO_ALG_TYPE_MASK); 972 - if (err) 973 - goto err_free_inst; 974 967 inst->free = skcipher_free_instance_simple; 975 968 976 969 /* Default algorithm properties, can be overridden */ ··· 983 990 return inst; 984 991 985 992 err_free_inst: 986 - kfree(inst); 987 - err_put_cipher_alg: 988 - crypto_mod_put(cipher_alg); 993 + skcipher_free_instance_simple(inst); 989 994 return ERR_PTR(err); 990 995 } 991 996 EXPORT_SYMBOL_GPL(skcipher_alloc_instance_simple);
+2 -2
include/crypto/internal/skcipher.h
··· 214 214 static inline struct crypto_alg *skcipher_ialg_simple( 215 215 struct skcipher_instance *inst) 216 216 { 217 - struct crypto_spawn *spawn = skcipher_instance_ctx(inst); 217 + struct crypto_cipher_spawn *spawn = skcipher_instance_ctx(inst); 218 218 219 - return spawn->alg; 219 + return crypto_spawn_cipher_alg(spawn); 220 220 } 221 221 222 222 #endif /* _CRYPTO_INTERNAL_SKCIPHER_H */