xref: /openbmc/linux/crypto/internal.h (revision 891ebfdf)
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>
13ae131f49SHerbert Xu #include <linux/err.h>
14adad556eSHerbert Xu #include <linux/jump_label.h>
155cb1454bSHerbert Xu #include <linux/list.h>
164cc7720cSHerbert Xu #include <linux/module.h>
172825982dSHerbert Xu #include <linux/notifier.h>
181dbb920eSHerbert Xu #include <linux/numa.h>
191dbb920eSHerbert Xu #include <linux/refcount.h>
205cb1454bSHerbert Xu #include <linux/rwsem.h>
216cb8815fSHerbert Xu #include <linux/scatterlist.h>
221dbb920eSHerbert Xu #include <linux/sched.h>
231dbb920eSHerbert Xu #include <linux/types.h>
24ccb778e1SNeil Horman 
256cb8815fSHerbert Xu struct akcipher_request;
266cb8815fSHerbert Xu struct crypto_akcipher;
274cc7720cSHerbert Xu struct crypto_instance;
284cc7720cSHerbert Xu struct crypto_template;
294cc7720cSHerbert Xu 
302825982dSHerbert Xu struct crypto_larval {
312825982dSHerbert Xu 	struct crypto_alg alg;
322825982dSHerbert Xu 	struct crypto_alg *adult;
332825982dSHerbert Xu 	struct completion completion;
34492e2b63SHerbert Xu 	u32 mask;
35adad556eSHerbert Xu 	bool test_started;
362825982dSHerbert Xu };
372825982dSHerbert Xu 
386cb8815fSHerbert Xu struct crypto_akcipher_sync_data {
396cb8815fSHerbert Xu 	struct crypto_akcipher *tfm;
406cb8815fSHerbert Xu 	const void *src;
416cb8815fSHerbert Xu 	void *dst;
426cb8815fSHerbert Xu 	unsigned int slen;
436cb8815fSHerbert Xu 	unsigned int dlen;
446cb8815fSHerbert Xu 
456cb8815fSHerbert Xu 	struct akcipher_request *req;
466cb8815fSHerbert Xu 	struct crypto_wait cwait;
47*891ebfdfSHerbert Xu 	struct scatterlist sg;
486cb8815fSHerbert Xu 	u8 *buf;
496cb8815fSHerbert Xu };
506cb8815fSHerbert Xu 
515163ab50SHerbert Xu enum {
525163ab50SHerbert Xu 	CRYPTOA_UNSPEC,
535163ab50SHerbert Xu 	CRYPTOA_ALG,
545163ab50SHerbert Xu 	CRYPTOA_TYPE,
555163ab50SHerbert Xu 	__CRYPTOA_MAX,
565163ab50SHerbert Xu };
575163ab50SHerbert Xu 
585163ab50SHerbert Xu #define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
595163ab50SHerbert Xu 
605163ab50SHerbert Xu /* Maximum number of (rtattr) parameters for each template. */
615163ab50SHerbert Xu #define CRYPTO_MAX_ATTRS 32
625163ab50SHerbert Xu 
635cb1454bSHerbert Xu extern struct list_head crypto_alg_list;
645cb1454bSHerbert Xu extern struct rw_semaphore crypto_alg_sem;
652825982dSHerbert Xu extern struct blocking_notifier_head crypto_chain;
665cb1454bSHerbert Xu 
6701f727cdSHerbert Xu int alg_test(const char *driver, const char *alg, u32 type, u32 mask);
6801f727cdSHerbert Xu 
6906bd9c96SEric Biggers #ifdef CONFIG_CRYPTO_MANAGER_DISABLE_TESTS
crypto_boot_test_finished(void)7006bd9c96SEric Biggers static inline bool crypto_boot_test_finished(void)
7106bd9c96SEric Biggers {
7206bd9c96SEric Biggers 	return true;
7306bd9c96SEric Biggers }
set_crypto_boot_test_finished(void)7406bd9c96SEric Biggers static inline void set_crypto_boot_test_finished(void)
7506bd9c96SEric Biggers {
7606bd9c96SEric Biggers }
7706bd9c96SEric Biggers #else
7806bd9c96SEric Biggers DECLARE_STATIC_KEY_FALSE(__crypto_boot_test_finished);
crypto_boot_test_finished(void)7906bd9c96SEric Biggers static inline bool crypto_boot_test_finished(void)
8006bd9c96SEric Biggers {
8106bd9c96SEric Biggers 	return static_branch_likely(&__crypto_boot_test_finished);
8206bd9c96SEric Biggers }
set_crypto_boot_test_finished(void)8306bd9c96SEric Biggers static inline void set_crypto_boot_test_finished(void)
8406bd9c96SEric Biggers {
8506bd9c96SEric Biggers 	static_branch_enable(&__crypto_boot_test_finished);
8606bd9c96SEric Biggers }
8706bd9c96SEric Biggers #endif /* !CONFIG_CRYPTO_MANAGER_DISABLE_TESTS */
88adad556eSHerbert Xu 
891da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
901da177e4SLinus Torvalds void __init crypto_init_proc(void);
91cce9e06dSHerbert Xu void __exit crypto_exit_proc(void);
921da177e4SLinus Torvalds #else
crypto_init_proc(void)931da177e4SLinus Torvalds static inline void crypto_init_proc(void)
941da177e4SLinus Torvalds { }
crypto_exit_proc(void)95cce9e06dSHerbert Xu static inline void crypto_exit_proc(void)
96cce9e06dSHerbert Xu { }
971da177e4SLinus Torvalds #endif
981da177e4SLinus Torvalds 
crypto_cipher_ctxsize(struct crypto_alg * alg)99f1ddcaf3SHerbert Xu static inline unsigned int crypto_cipher_ctxsize(struct crypto_alg *alg)
100fbdae9f3SHerbert Xu {
101f1ddcaf3SHerbert Xu 	return alg->cra_ctxsize;
102fbdae9f3SHerbert Xu }
103fbdae9f3SHerbert Xu 
crypto_compress_ctxsize(struct crypto_alg * alg)104f1ddcaf3SHerbert Xu static inline unsigned int crypto_compress_ctxsize(struct crypto_alg *alg)
105fbdae9f3SHerbert Xu {
106fbdae9f3SHerbert Xu 	return alg->cra_ctxsize;
107fbdae9f3SHerbert Xu }
108fbdae9f3SHerbert Xu 
1092825982dSHerbert Xu struct crypto_alg *crypto_mod_get(struct crypto_alg *alg);
110492e2b63SHerbert Xu struct crypto_alg *crypto_alg_mod_lookup(const char *name, u32 type, u32 mask);
1112825982dSHerbert Xu 
11273d3864aSHerbert Xu struct crypto_larval *crypto_larval_alloc(const char *name, u32 type, u32 mask);
113b9c55aa4SHerbert Xu void crypto_larval_kill(struct crypto_alg *alg);
114adad556eSHerbert Xu void crypto_wait_for_test(struct crypto_larval *larval);
11573d3864aSHerbert Xu void crypto_alg_tested(const char *name, int err);
1162825982dSHerbert Xu 
11789b596baSSteffen Klassert void crypto_remove_spawns(struct crypto_alg *alg, struct list_head *list,
11889b596baSSteffen Klassert 			  struct crypto_alg *nalg);
11922e5b20bSSteffen Klassert void crypto_remove_final(struct list_head *list);
1206603523bSHerbert Xu void crypto_shoot_alg(struct crypto_alg *alg);
121fa3b3565SHerbert Xu struct crypto_tfm *__crypto_alloc_tfmgfp(struct crypto_alg *alg, u32 type,
122fa3b3565SHerbert Xu 					 u32 mask, gfp_t gfp);
12327d2a330SHerbert Xu struct crypto_tfm *__crypto_alloc_tfm(struct crypto_alg *alg, u32 type,
12427d2a330SHerbert Xu 				      u32 mask);
1257bc13b5bSBarry Song void *crypto_create_tfm_node(struct crypto_alg *alg,
1267bc13b5bSBarry Song 			const struct crypto_type *frontend, int node);
1273c3a24cbSHerbert Xu void *crypto_clone_tfm(const struct crypto_type *frontend,
1283c3a24cbSHerbert Xu 		       struct crypto_tfm *otfm);
1297bc13b5bSBarry Song 
1306cb8815fSHerbert Xu int crypto_akcipher_sync_prep(struct crypto_akcipher_sync_data *data);
1316cb8815fSHerbert Xu int crypto_akcipher_sync_post(struct crypto_akcipher_sync_data *data, int err);
1326cb8815fSHerbert Xu int crypto_init_akcipher_ops_sig(struct crypto_tfm *tfm);
1336cb8815fSHerbert Xu 
crypto_create_tfm(struct crypto_alg * alg,const struct crypto_type * frontend)1347bc13b5bSBarry Song static inline void *crypto_create_tfm(struct crypto_alg *alg,
1357bc13b5bSBarry Song 			const struct crypto_type *frontend)
1367bc13b5bSBarry Song {
1377bc13b5bSBarry Song 	return crypto_create_tfm_node(alg, frontend, NUMA_NO_NODE);
1387bc13b5bSBarry Song }
1397bc13b5bSBarry Song 
140d06854f0SHerbert Xu struct crypto_alg *crypto_find_alg(const char *alg_name,
141d06854f0SHerbert Xu 				   const struct crypto_type *frontend,
142d06854f0SHerbert Xu 				   u32 type, u32 mask);
1437bc13b5bSBarry Song 
1447bc13b5bSBarry Song void *crypto_alloc_tfm_node(const char *alg_name,
1457bc13b5bSBarry Song 		       const struct crypto_type *frontend, u32 type, u32 mask,
1467bc13b5bSBarry Song 		       int node);
1477bc13b5bSBarry Song 
crypto_alloc_tfm(const char * alg_name,const struct crypto_type * frontend,u32 type,u32 mask)1487bc13b5bSBarry Song static inline void *crypto_alloc_tfm(const char *alg_name,
1497bc13b5bSBarry Song 		       const struct crypto_type *frontend, u32 type, u32 mask)
1507bc13b5bSBarry Song {
1517bc13b5bSBarry Song 	return crypto_alloc_tfm_node(alg_name, frontend, type, mask, NUMA_NO_NODE);
1527bc13b5bSBarry Song }
1536bfd4809SHerbert Xu 
15473d3864aSHerbert Xu int crypto_probing_notify(unsigned long val, void *v);
1552825982dSHerbert Xu 
15638d21433SHerbert Xu unsigned int crypto_alg_extsize(struct crypto_alg *alg);
15738d21433SHerbert Xu 
158f2aefdabSHerbert Xu int crypto_type_has_alg(const char *name, const struct crypto_type *frontend,
159f2aefdabSHerbert Xu 			u32 type, u32 mask);
160f2aefdabSHerbert Xu 
crypto_alg_get(struct crypto_alg * alg)161939e1779SHerbert Xu static inline struct crypto_alg *crypto_alg_get(struct crypto_alg *alg)
162939e1779SHerbert Xu {
163ce8614a3SEric Biggers 	refcount_inc(&alg->cra_refcnt);
164939e1779SHerbert Xu 	return alg;
165939e1779SHerbert Xu }
166939e1779SHerbert Xu 
crypto_alg_put(struct crypto_alg * alg)1676bfd4809SHerbert Xu static inline void crypto_alg_put(struct crypto_alg *alg)
1686bfd4809SHerbert Xu {
169ce8614a3SEric Biggers 	if (refcount_dec_and_test(&alg->cra_refcnt) && alg->cra_destroy)
1706bfd4809SHerbert Xu 		alg->cra_destroy(alg);
1716bfd4809SHerbert Xu }
1726bfd4809SHerbert Xu 
crypto_tmpl_get(struct crypto_template * tmpl)1734cc7720cSHerbert Xu static inline int crypto_tmpl_get(struct crypto_template *tmpl)
1744cc7720cSHerbert Xu {
1754cc7720cSHerbert Xu 	return try_module_get(tmpl->module);
1764cc7720cSHerbert Xu }
1774cc7720cSHerbert Xu 
crypto_tmpl_put(struct crypto_template * tmpl)1784cc7720cSHerbert Xu static inline void crypto_tmpl_put(struct crypto_template *tmpl)
1794cc7720cSHerbert Xu {
1804cc7720cSHerbert Xu 	module_put(tmpl->module);
1814cc7720cSHerbert Xu }
1824cc7720cSHerbert Xu 
crypto_is_larval(struct crypto_alg * alg)1832825982dSHerbert Xu static inline int crypto_is_larval(struct crypto_alg *alg)
1842825982dSHerbert Xu {
1852825982dSHerbert Xu 	return alg->cra_flags & CRYPTO_ALG_LARVAL;
1862825982dSHerbert Xu }
1872825982dSHerbert Xu 
crypto_is_dead(struct crypto_alg * alg)1886bfd4809SHerbert Xu static inline int crypto_is_dead(struct crypto_alg *alg)
1896bfd4809SHerbert Xu {
1906bfd4809SHerbert Xu 	return alg->cra_flags & CRYPTO_ALG_DEAD;
1916bfd4809SHerbert Xu }
1926bfd4809SHerbert Xu 
crypto_is_moribund(struct crypto_alg * alg)1936bfd4809SHerbert Xu static inline int crypto_is_moribund(struct crypto_alg *alg)
1946bfd4809SHerbert Xu {
1956bfd4809SHerbert Xu 	return alg->cra_flags & (CRYPTO_ALG_DEAD | CRYPTO_ALG_DYING);
1966bfd4809SHerbert Xu }
1976bfd4809SHerbert Xu 
crypto_notify(unsigned long val,void * v)19873d3864aSHerbert Xu static inline void crypto_notify(unsigned long val, void *v)
1992825982dSHerbert Xu {
20073d3864aSHerbert Xu 	blocking_notifier_call_chain(&crypto_chain, val, v);
2012825982dSHerbert Xu }
2022825982dSHerbert Xu 
crypto_yield(u32 flags)2031dbb920eSHerbert Xu static inline void crypto_yield(u32 flags)
2041dbb920eSHerbert Xu {
2051dbb920eSHerbert Xu 	if (flags & CRYPTO_TFM_REQ_MAY_SLEEP)
2061dbb920eSHerbert Xu 		cond_resched();
2071dbb920eSHerbert Xu }
2081dbb920eSHerbert Xu 
crypto_is_test_larval(struct crypto_larval * larval)209adad556eSHerbert Xu static inline int crypto_is_test_larval(struct crypto_larval *larval)
210adad556eSHerbert Xu {
211adad556eSHerbert Xu 	return larval->alg.cra_driver_name[0];
212adad556eSHerbert Xu }
213adad556eSHerbert Xu 
crypto_tfm_get(struct crypto_tfm * tfm)214ae131f49SHerbert Xu static inline struct crypto_tfm *crypto_tfm_get(struct crypto_tfm *tfm)
215ae131f49SHerbert Xu {
216ae131f49SHerbert Xu 	return refcount_inc_not_zero(&tfm->refcnt) ? tfm : ERR_PTR(-EOVERFLOW);
217ae131f49SHerbert Xu }
218ae131f49SHerbert Xu 
2191da177e4SLinus Torvalds #endif	/* _CRYPTO_INTERNAL_H */
2201da177e4SLinus Torvalds 
221