12874c5fdSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */ 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * Cryptographic API. 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Copyright (c) 2002 James Morris <jmorris@intercode.com.au> 65cb1454bSHerbert Xu * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au> 71da177e4SLinus Torvalds */ 81da177e4SLinus Torvalds #ifndef _CRYPTO_INTERNAL_H 91da177e4SLinus Torvalds #define _CRYPTO_INTERNAL_H 10cce9e06dSHerbert Xu 11cce9e06dSHerbert Xu #include <crypto/algapi.h> 122825982dSHerbert Xu #include <linux/completion.h> 135cb1454bSHerbert Xu #include <linux/list.h> 144cc7720cSHerbert Xu #include <linux/module.h> 152825982dSHerbert Xu #include <linux/notifier.h> 161dbb920eSHerbert Xu #include <linux/numa.h> 171dbb920eSHerbert Xu #include <linux/refcount.h> 185cb1454bSHerbert Xu #include <linux/rwsem.h> 191dbb920eSHerbert Xu #include <linux/sched.h> 201dbb920eSHerbert Xu #include <linux/types.h> 21ccb778e1SNeil Horman 224cc7720cSHerbert Xu struct crypto_instance; 234cc7720cSHerbert Xu struct crypto_template; 244cc7720cSHerbert Xu 252825982dSHerbert Xu struct crypto_larval { 262825982dSHerbert Xu struct crypto_alg alg; 272825982dSHerbert Xu struct crypto_alg *adult; 282825982dSHerbert Xu struct completion completion; 29492e2b63SHerbert Xu u32 mask; 302825982dSHerbert Xu }; 312825982dSHerbert Xu 325cb1454bSHerbert Xu extern struct list_head crypto_alg_list; 335cb1454bSHerbert Xu extern struct rw_semaphore crypto_alg_sem; 342825982dSHerbert Xu extern struct blocking_notifier_head crypto_chain; 355cb1454bSHerbert Xu 361da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 371da177e4SLinus Torvalds void __init crypto_init_proc(void); 38cce9e06dSHerbert Xu void __exit crypto_exit_proc(void); 391da177e4SLinus Torvalds #else 401da177e4SLinus Torvalds static inline void crypto_init_proc(void) 411da177e4SLinus Torvalds { } 42cce9e06dSHerbert Xu static inline void crypto_exit_proc(void) 43cce9e06dSHerbert Xu { } 441da177e4SLinus Torvalds #endif 451da177e4SLinus Torvalds 46f1ddcaf3SHerbert Xu static inline unsigned int crypto_cipher_ctxsize(struct crypto_alg *alg) 47fbdae9f3SHerbert Xu { 48f1ddcaf3SHerbert Xu return alg->cra_ctxsize; 49fbdae9f3SHerbert Xu } 50fbdae9f3SHerbert Xu 51f1ddcaf3SHerbert Xu static inline unsigned int crypto_compress_ctxsize(struct crypto_alg *alg) 52fbdae9f3SHerbert Xu { 53fbdae9f3SHerbert Xu return alg->cra_ctxsize; 54fbdae9f3SHerbert Xu } 55fbdae9f3SHerbert Xu 562825982dSHerbert Xu struct crypto_alg *crypto_mod_get(struct crypto_alg *alg); 57492e2b63SHerbert Xu struct crypto_alg *crypto_alg_mod_lookup(const char *name, u32 type, u32 mask); 582825982dSHerbert Xu 5973d3864aSHerbert Xu struct crypto_larval *crypto_larval_alloc(const char *name, u32 type, u32 mask); 60b9c55aa4SHerbert Xu void crypto_larval_kill(struct crypto_alg *alg); 6173d3864aSHerbert Xu void crypto_alg_tested(const char *name, int err); 622825982dSHerbert Xu 6389b596baSSteffen Klassert void crypto_remove_spawns(struct crypto_alg *alg, struct list_head *list, 6489b596baSSteffen Klassert struct crypto_alg *nalg); 6522e5b20bSSteffen Klassert void crypto_remove_final(struct list_head *list); 666603523bSHerbert Xu void crypto_shoot_alg(struct crypto_alg *alg); 6727d2a330SHerbert Xu struct crypto_tfm *__crypto_alloc_tfm(struct crypto_alg *alg, u32 type, 6827d2a330SHerbert Xu u32 mask); 697bc13b5bSBarry Song void *crypto_create_tfm_node(struct crypto_alg *alg, 707bc13b5bSBarry Song const struct crypto_type *frontend, int node); 717bc13b5bSBarry Song 727bc13b5bSBarry Song static inline void *crypto_create_tfm(struct crypto_alg *alg, 737bc13b5bSBarry Song const struct crypto_type *frontend) 747bc13b5bSBarry Song { 757bc13b5bSBarry Song return crypto_create_tfm_node(alg, frontend, NUMA_NO_NODE); 767bc13b5bSBarry Song } 777bc13b5bSBarry Song 78d06854f0SHerbert Xu struct crypto_alg *crypto_find_alg(const char *alg_name, 79d06854f0SHerbert Xu const struct crypto_type *frontend, 80d06854f0SHerbert Xu u32 type, u32 mask); 817bc13b5bSBarry Song 827bc13b5bSBarry Song void *crypto_alloc_tfm_node(const char *alg_name, 837bc13b5bSBarry Song const struct crypto_type *frontend, u32 type, u32 mask, 847bc13b5bSBarry Song int node); 857bc13b5bSBarry Song 867bc13b5bSBarry Song static inline void *crypto_alloc_tfm(const char *alg_name, 877bc13b5bSBarry Song const struct crypto_type *frontend, u32 type, u32 mask) 887bc13b5bSBarry Song { 897bc13b5bSBarry Song return crypto_alloc_tfm_node(alg_name, frontend, type, mask, NUMA_NO_NODE); 907bc13b5bSBarry Song } 916bfd4809SHerbert Xu 9273d3864aSHerbert Xu int crypto_probing_notify(unsigned long val, void *v); 932825982dSHerbert Xu 9438d21433SHerbert Xu unsigned int crypto_alg_extsize(struct crypto_alg *alg); 9538d21433SHerbert Xu 96f2aefdabSHerbert Xu int crypto_type_has_alg(const char *name, const struct crypto_type *frontend, 97f2aefdabSHerbert Xu u32 type, u32 mask); 98f2aefdabSHerbert Xu 99939e1779SHerbert Xu static inline struct crypto_alg *crypto_alg_get(struct crypto_alg *alg) 100939e1779SHerbert Xu { 101ce8614a3SEric Biggers refcount_inc(&alg->cra_refcnt); 102939e1779SHerbert Xu return alg; 103939e1779SHerbert Xu } 104939e1779SHerbert Xu 1056bfd4809SHerbert Xu static inline void crypto_alg_put(struct crypto_alg *alg) 1066bfd4809SHerbert Xu { 107ce8614a3SEric Biggers if (refcount_dec_and_test(&alg->cra_refcnt) && alg->cra_destroy) 1086bfd4809SHerbert Xu alg->cra_destroy(alg); 1096bfd4809SHerbert Xu } 1106bfd4809SHerbert Xu 1114cc7720cSHerbert Xu static inline int crypto_tmpl_get(struct crypto_template *tmpl) 1124cc7720cSHerbert Xu { 1134cc7720cSHerbert Xu return try_module_get(tmpl->module); 1144cc7720cSHerbert Xu } 1154cc7720cSHerbert Xu 1164cc7720cSHerbert Xu static inline void crypto_tmpl_put(struct crypto_template *tmpl) 1174cc7720cSHerbert Xu { 1184cc7720cSHerbert Xu module_put(tmpl->module); 1194cc7720cSHerbert Xu } 1204cc7720cSHerbert Xu 1212825982dSHerbert Xu static inline int crypto_is_larval(struct crypto_alg *alg) 1222825982dSHerbert Xu { 1232825982dSHerbert Xu return alg->cra_flags & CRYPTO_ALG_LARVAL; 1242825982dSHerbert Xu } 1252825982dSHerbert Xu 1266bfd4809SHerbert Xu static inline int crypto_is_dead(struct crypto_alg *alg) 1276bfd4809SHerbert Xu { 1286bfd4809SHerbert Xu return alg->cra_flags & CRYPTO_ALG_DEAD; 1296bfd4809SHerbert Xu } 1306bfd4809SHerbert Xu 1316bfd4809SHerbert Xu static inline int crypto_is_moribund(struct crypto_alg *alg) 1326bfd4809SHerbert Xu { 1336bfd4809SHerbert Xu return alg->cra_flags & (CRYPTO_ALG_DEAD | CRYPTO_ALG_DYING); 1346bfd4809SHerbert Xu } 1356bfd4809SHerbert Xu 13673d3864aSHerbert Xu static inline void crypto_notify(unsigned long val, void *v) 1372825982dSHerbert Xu { 13873d3864aSHerbert Xu blocking_notifier_call_chain(&crypto_chain, val, v); 1392825982dSHerbert Xu } 1402825982dSHerbert Xu 1411dbb920eSHerbert Xu static inline void crypto_yield(u32 flags) 1421dbb920eSHerbert Xu { 1431dbb920eSHerbert Xu if (flags & CRYPTO_TFM_REQ_MAY_SLEEP) 1441dbb920eSHerbert Xu cond_resched(); 1451dbb920eSHerbert Xu } 1461dbb920eSHerbert Xu 1471da177e4SLinus Torvalds #endif /* _CRYPTO_INTERNAL_H */ 1481da177e4SLinus Torvalds 149