xref: /openbmc/linux/crypto/internal.h (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
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>
211dbb920eSHerbert Xu #include <linux/scatterlist.h>
221dbb920eSHerbert Xu #include <linux/sched.h>
23ccb778e1SNeil Horman #include <linux/types.h>
244cc7720cSHerbert Xu 
254cc7720cSHerbert Xu struct akcipher_request;
264cc7720cSHerbert Xu struct crypto_akcipher;
272825982dSHerbert Xu struct crypto_instance;
282825982dSHerbert Xu struct crypto_template;
292825982dSHerbert Xu 
302825982dSHerbert Xu struct crypto_larval {
31492e2b63SHerbert Xu 	struct crypto_alg alg;
32adad556eSHerbert Xu 	struct crypto_alg *adult;
332825982dSHerbert Xu 	struct completion completion;
342825982dSHerbert Xu 	u32 mask;
355163ab50SHerbert Xu 	bool test_started;
365163ab50SHerbert Xu };
375163ab50SHerbert Xu 
385163ab50SHerbert Xu struct crypto_akcipher_sync_data {
395163ab50SHerbert Xu 	struct crypto_akcipher *tfm;
405163ab50SHerbert Xu 	const void *src;
415163ab50SHerbert Xu 	void *dst;
425163ab50SHerbert Xu 	unsigned int slen;
435163ab50SHerbert Xu 	unsigned int dlen;
445163ab50SHerbert Xu 
455163ab50SHerbert Xu 	struct akcipher_request *req;
465163ab50SHerbert Xu 	struct crypto_wait cwait;
475cb1454bSHerbert Xu 	struct scatterlist sg;
485cb1454bSHerbert Xu 	u8 *buf;
492825982dSHerbert Xu };
505cb1454bSHerbert Xu 
5101f727cdSHerbert Xu enum {
5201f727cdSHerbert Xu 	CRYPTOA_UNSPEC,
5306bd9c96SEric Biggers 	CRYPTOA_ALG,
5406bd9c96SEric Biggers 	CRYPTOA_TYPE,
5506bd9c96SEric Biggers 	__CRYPTOA_MAX,
5606bd9c96SEric Biggers };
5706bd9c96SEric Biggers 
5806bd9c96SEric Biggers #define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
5906bd9c96SEric Biggers 
6006bd9c96SEric Biggers /* Maximum number of (rtattr) parameters for each template. */
6106bd9c96SEric Biggers #define CRYPTO_MAX_ATTRS 32
6206bd9c96SEric Biggers 
6306bd9c96SEric Biggers extern struct list_head crypto_alg_list;
6406bd9c96SEric Biggers extern struct rw_semaphore crypto_alg_sem;
6506bd9c96SEric Biggers extern struct blocking_notifier_head crypto_chain;
6606bd9c96SEric Biggers 
6706bd9c96SEric Biggers int alg_test(const char *driver, const char *alg, u32 type, u32 mask);
6806bd9c96SEric Biggers 
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 {
72adad556eSHerbert Xu 	return true;
731da177e4SLinus Torvalds }
set_crypto_boot_test_finished(void)741da177e4SLinus Torvalds static inline void set_crypto_boot_test_finished(void)
75cce9e06dSHerbert Xu {
761da177e4SLinus Torvalds }
771da177e4SLinus Torvalds #else
781da177e4SLinus Torvalds DECLARE_STATIC_KEY_FALSE(__crypto_boot_test_finished);
crypto_boot_test_finished(void)79cce9e06dSHerbert Xu static inline bool crypto_boot_test_finished(void)
80cce9e06dSHerbert Xu {
811da177e4SLinus Torvalds 	return static_branch_likely(&__crypto_boot_test_finished);
821da177e4SLinus Torvalds }
set_crypto_boot_test_finished(void)83f1ddcaf3SHerbert Xu static inline void set_crypto_boot_test_finished(void)
84fbdae9f3SHerbert Xu {
85f1ddcaf3SHerbert Xu 	static_branch_enable(&__crypto_boot_test_finished);
86fbdae9f3SHerbert Xu }
87fbdae9f3SHerbert Xu #endif /* !CONFIG_CRYPTO_MANAGER_DISABLE_TESTS */
88f1ddcaf3SHerbert Xu 
89fbdae9f3SHerbert Xu #ifdef CONFIG_PROC_FS
90fbdae9f3SHerbert Xu void __init crypto_init_proc(void);
91fbdae9f3SHerbert Xu void __exit crypto_exit_proc(void);
92fbdae9f3SHerbert Xu #else
crypto_init_proc(void)932825982dSHerbert Xu static inline void crypto_init_proc(void)
94492e2b63SHerbert Xu { }
crypto_exit_proc(void)952825982dSHerbert Xu static inline void crypto_exit_proc(void)
9673d3864aSHerbert Xu { }
97b9c55aa4SHerbert Xu #endif
98adad556eSHerbert Xu 
crypto_cipher_ctxsize(struct crypto_alg * alg)9973d3864aSHerbert Xu static inline unsigned int crypto_cipher_ctxsize(struct crypto_alg *alg)
1002825982dSHerbert Xu {
10189b596baSSteffen Klassert 	return alg->cra_ctxsize;
10289b596baSSteffen Klassert }
10322e5b20bSSteffen Klassert 
crypto_compress_ctxsize(struct crypto_alg * alg)1046603523bSHerbert Xu static inline unsigned int crypto_compress_ctxsize(struct crypto_alg *alg)
10527d2a330SHerbert Xu {
10627d2a330SHerbert Xu 	return alg->cra_ctxsize;
1077bc13b5bSBarry Song }
1087bc13b5bSBarry Song 
109*3c3a24cbSHerbert Xu struct crypto_alg *crypto_mod_get(struct crypto_alg *alg);
110*3c3a24cbSHerbert Xu struct crypto_alg *crypto_alg_mod_lookup(const char *name, u32 type, u32 mask);
1117bc13b5bSBarry Song 
1127bc13b5bSBarry Song struct crypto_larval *crypto_larval_alloc(const char *name, u32 type, u32 mask);
1137bc13b5bSBarry Song void crypto_larval_kill(struct crypto_alg *alg);
1147bc13b5bSBarry Song void crypto_wait_for_test(struct crypto_larval *larval);
1157bc13b5bSBarry Song void crypto_alg_tested(const char *name, int err);
1167bc13b5bSBarry Song 
1177bc13b5bSBarry Song void crypto_remove_spawns(struct crypto_alg *alg, struct list_head *list,
118d06854f0SHerbert Xu 			  struct crypto_alg *nalg);
119d06854f0SHerbert Xu void crypto_remove_final(struct list_head *list);
120d06854f0SHerbert Xu void crypto_shoot_alg(struct crypto_alg *alg);
1217bc13b5bSBarry Song struct crypto_tfm *__crypto_alloc_tfmgfp(struct crypto_alg *alg, u32 type,
1227bc13b5bSBarry Song 					 u32 mask, gfp_t gfp);
1237bc13b5bSBarry Song struct crypto_tfm *__crypto_alloc_tfm(struct crypto_alg *alg, u32 type,
1247bc13b5bSBarry Song 				      u32 mask);
1257bc13b5bSBarry Song void *crypto_create_tfm_node(struct crypto_alg *alg,
1267bc13b5bSBarry Song 			const struct crypto_type *frontend, int node);
1277bc13b5bSBarry Song void *crypto_clone_tfm(const struct crypto_type *frontend,
1287bc13b5bSBarry Song 		       struct crypto_tfm *otfm);
1297bc13b5bSBarry Song 
1307bc13b5bSBarry Song int crypto_akcipher_sync_prep(struct crypto_akcipher_sync_data *data);
1316bfd4809SHerbert Xu int crypto_akcipher_sync_post(struct crypto_akcipher_sync_data *data, int err);
13273d3864aSHerbert Xu int crypto_init_akcipher_ops_sig(struct crypto_tfm *tfm);
1332825982dSHerbert Xu 
crypto_create_tfm(struct crypto_alg * alg,const struct crypto_type * frontend)13438d21433SHerbert Xu static inline void *crypto_create_tfm(struct crypto_alg *alg,
13538d21433SHerbert Xu 			const struct crypto_type *frontend)
136f2aefdabSHerbert Xu {
137f2aefdabSHerbert Xu 	return crypto_create_tfm_node(alg, frontend, NUMA_NO_NODE);
138f2aefdabSHerbert Xu }
139939e1779SHerbert Xu 
140939e1779SHerbert Xu struct crypto_alg *crypto_find_alg(const char *alg_name,
141ce8614a3SEric Biggers 				   const struct crypto_type *frontend,
142939e1779SHerbert Xu 				   u32 type, u32 mask);
143939e1779SHerbert Xu 
144939e1779SHerbert Xu void *crypto_alloc_tfm_node(const char *alg_name,
1456bfd4809SHerbert Xu 		       const struct crypto_type *frontend, u32 type, u32 mask,
1466bfd4809SHerbert Xu 		       int node);
147ce8614a3SEric Biggers 
crypto_alloc_tfm(const char * alg_name,const struct crypto_type * frontend,u32 type,u32 mask)1486bfd4809SHerbert Xu static inline void *crypto_alloc_tfm(const char *alg_name,
1496bfd4809SHerbert Xu 		       const struct crypto_type *frontend, u32 type, u32 mask)
1506bfd4809SHerbert Xu {
1514cc7720cSHerbert Xu 	return crypto_alloc_tfm_node(alg_name, frontend, type, mask, NUMA_NO_NODE);
1524cc7720cSHerbert Xu }
1534cc7720cSHerbert Xu 
1544cc7720cSHerbert Xu int crypto_probing_notify(unsigned long val, void *v);
1554cc7720cSHerbert Xu 
1564cc7720cSHerbert Xu unsigned int crypto_alg_extsize(struct crypto_alg *alg);
1574cc7720cSHerbert Xu 
1584cc7720cSHerbert Xu int crypto_type_has_alg(const char *name, const struct crypto_type *frontend,
1594cc7720cSHerbert Xu 			u32 type, u32 mask);
1604cc7720cSHerbert Xu 
crypto_alg_get(struct crypto_alg * alg)1612825982dSHerbert Xu static inline struct crypto_alg *crypto_alg_get(struct crypto_alg *alg)
1622825982dSHerbert Xu {
1632825982dSHerbert Xu 	refcount_inc(&alg->cra_refcnt);
1642825982dSHerbert Xu 	return alg;
1652825982dSHerbert Xu }
1666bfd4809SHerbert Xu 
crypto_alg_put(struct crypto_alg * alg)1676bfd4809SHerbert Xu static inline void crypto_alg_put(struct crypto_alg *alg)
1686bfd4809SHerbert Xu {
1696bfd4809SHerbert Xu 	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)1736bfd4809SHerbert Xu static inline int crypto_tmpl_get(struct crypto_template *tmpl)
1746bfd4809SHerbert Xu {
1756bfd4809SHerbert Xu 	return try_module_get(tmpl->module);
17673d3864aSHerbert Xu }
1772825982dSHerbert Xu 
crypto_tmpl_put(struct crypto_template * tmpl)17873d3864aSHerbert Xu static inline void crypto_tmpl_put(struct crypto_template *tmpl)
1792825982dSHerbert Xu {
1802825982dSHerbert Xu 	module_put(tmpl->module);
1811dbb920eSHerbert Xu }
1821dbb920eSHerbert Xu 
crypto_is_larval(struct crypto_alg * alg)1831dbb920eSHerbert Xu static inline int crypto_is_larval(struct crypto_alg *alg)
1841dbb920eSHerbert Xu {
1851dbb920eSHerbert Xu 	return alg->cra_flags & CRYPTO_ALG_LARVAL;
1861dbb920eSHerbert Xu }
187adad556eSHerbert Xu 
crypto_is_dead(struct crypto_alg * alg)188adad556eSHerbert Xu static inline int crypto_is_dead(struct crypto_alg *alg)
189adad556eSHerbert Xu {
190adad556eSHerbert Xu 	return alg->cra_flags & CRYPTO_ALG_DEAD;
191adad556eSHerbert Xu }
192ae131f49SHerbert Xu 
crypto_is_moribund(struct crypto_alg * alg)193ae131f49SHerbert Xu static inline int crypto_is_moribund(struct crypto_alg *alg)
194ae131f49SHerbert Xu {
195ae131f49SHerbert Xu 	return alg->cra_flags & (CRYPTO_ALG_DEAD | CRYPTO_ALG_DYING);
196ae131f49SHerbert Xu }
1971da177e4SLinus Torvalds 
crypto_notify(unsigned long val,void * v)1981da177e4SLinus Torvalds static inline void crypto_notify(unsigned long val, void *v)
199 {
200 	blocking_notifier_call_chain(&crypto_chain, val, v);
201 }
202 
crypto_yield(u32 flags)203 static inline void crypto_yield(u32 flags)
204 {
205 	if (flags & CRYPTO_TFM_REQ_MAY_SLEEP)
206 		cond_resched();
207 }
208 
crypto_is_test_larval(struct crypto_larval * larval)209 static inline int crypto_is_test_larval(struct crypto_larval *larval)
210 {
211 	return larval->alg.cra_driver_name[0];
212 }
213 
crypto_tfm_get(struct crypto_tfm * tfm)214 static inline struct crypto_tfm *crypto_tfm_get(struct crypto_tfm *tfm)
215 {
216 	return refcount_inc_not_zero(&tfm->refcnt) ? tfm : ERR_PTR(-EOVERFLOW);
217 }
218 
219 #endif	/* _CRYPTO_INTERNAL_H */
220 
221