blob: 976ec9dfc76db1e3278d45596bb6ad1769e9df8c [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001/* SPDX-License-Identifier: GPL-2.0-or-later */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Cryptographic API.
4 *
5 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
Herbert Xu5cb1454b2005-11-05 16:58:14 +11006 * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
8#ifndef _CRYPTO_INTERNAL_H
9#define _CRYPTO_INTERNAL_H
Herbert Xucce9e062006-08-21 21:08:13 +100010
11#include <crypto/algapi.h>
Herbert Xu28259822006-08-06 21:23:26 +100012#include <linux/completion.h>
Herbert Xu5cb1454b2005-11-05 16:58:14 +110013#include <linux/list.h>
Herbert Xu4cc77202006-08-06 21:16:34 +100014#include <linux/module.h>
Herbert Xu28259822006-08-06 21:23:26 +100015#include <linux/notifier.h>
Herbert Xu1dbb9202020-07-30 22:39:13 +100016#include <linux/numa.h>
17#include <linux/refcount.h>
Herbert Xu5cb1454b2005-11-05 16:58:14 +110018#include <linux/rwsem.h>
Herbert Xu1dbb9202020-07-30 22:39:13 +100019#include <linux/sched.h>
20#include <linux/types.h>
Neil Hormanccb778e2008-08-05 14:13:08 +080021
Herbert Xu4cc77202006-08-06 21:16:34 +100022struct crypto_instance;
23struct crypto_template;
24
Herbert Xu28259822006-08-06 21:23:26 +100025struct crypto_larval {
26 struct crypto_alg alg;
27 struct crypto_alg *adult;
28 struct completion completion;
Herbert Xu492e2b62006-09-21 11:35:17 +100029 u32 mask;
Herbert Xu28259822006-08-06 21:23:26 +100030};
31
Herbert Xu5cb1454b2005-11-05 16:58:14 +110032extern struct list_head crypto_alg_list;
33extern struct rw_semaphore crypto_alg_sem;
Herbert Xu28259822006-08-06 21:23:26 +100034extern struct blocking_notifier_head crypto_chain;
Herbert Xu5cb1454b2005-11-05 16:58:14 +110035
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#ifdef CONFIG_PROC_FS
37void __init crypto_init_proc(void);
Herbert Xucce9e062006-08-21 21:08:13 +100038void __exit crypto_exit_proc(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#else
40static inline void crypto_init_proc(void)
41{ }
Herbert Xucce9e062006-08-21 21:08:13 +100042static inline void crypto_exit_proc(void)
43{ }
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#endif
45
Herbert Xuf1ddcaf2007-01-27 10:05:15 +110046static inline unsigned int crypto_cipher_ctxsize(struct crypto_alg *alg)
Herbert Xufbdae9f2005-07-06 13:53:29 -070047{
Herbert Xuf1ddcaf2007-01-27 10:05:15 +110048 return alg->cra_ctxsize;
Herbert Xufbdae9f2005-07-06 13:53:29 -070049}
50
Herbert Xuf1ddcaf2007-01-27 10:05:15 +110051static inline unsigned int crypto_compress_ctxsize(struct crypto_alg *alg)
Herbert Xufbdae9f2005-07-06 13:53:29 -070052{
53 return alg->cra_ctxsize;
54}
55
Herbert Xu28259822006-08-06 21:23:26 +100056struct crypto_alg *crypto_mod_get(struct crypto_alg *alg);
Herbert Xu492e2b62006-09-21 11:35:17 +100057struct crypto_alg *crypto_alg_mod_lookup(const char *name, u32 type, u32 mask);
Herbert Xu28259822006-08-06 21:23:26 +100058
Herbert Xu73d38642008-08-03 21:15:23 +080059struct crypto_larval *crypto_larval_alloc(const char *name, u32 type, u32 mask);
Herbert Xub9c55aa2007-12-04 12:46:48 +110060void crypto_larval_kill(struct crypto_alg *alg);
Herbert Xu73d38642008-08-03 21:15:23 +080061void crypto_alg_tested(const char *name, int err);
Herbert Xu28259822006-08-06 21:23:26 +100062
Steffen Klassert89b596b2011-09-27 07:22:08 +020063void crypto_remove_spawns(struct crypto_alg *alg, struct list_head *list,
64 struct crypto_alg *nalg);
Steffen Klassert22e5b202011-09-27 07:23:07 +020065void crypto_remove_final(struct list_head *list);
Herbert Xu66035232020-04-10 16:09:42 +100066void crypto_shoot_alg(struct crypto_alg *alg);
Herbert Xu27d2a332007-01-24 20:50:26 +110067struct crypto_tfm *__crypto_alloc_tfm(struct crypto_alg *alg, u32 type,
68 u32 mask);
Barry Song7bc13b52020-07-05 21:18:58 +120069void *crypto_create_tfm_node(struct crypto_alg *alg,
70 const struct crypto_type *frontend, int node);
71
72static inline void *crypto_create_tfm(struct crypto_alg *alg,
73 const struct crypto_type *frontend)
74{
75 return crypto_create_tfm_node(alg, frontend, NUMA_NO_NODE);
76}
77
Herbert Xud06854f2009-07-08 17:53:16 +080078struct crypto_alg *crypto_find_alg(const char *alg_name,
79 const struct crypto_type *frontend,
80 u32 type, u32 mask);
Barry Song7bc13b52020-07-05 21:18:58 +120081
82void *crypto_alloc_tfm_node(const char *alg_name,
83 const struct crypto_type *frontend, u32 type, u32 mask,
84 int node);
85
86static inline void *crypto_alloc_tfm(const char *alg_name,
87 const struct crypto_type *frontend, u32 type, u32 mask)
88{
89 return crypto_alloc_tfm_node(alg_name, frontend, type, mask, NUMA_NO_NODE);
90}
Herbert Xu6bfd4802006-09-21 11:39:29 +100091
Herbert Xu73d38642008-08-03 21:15:23 +080092int crypto_probing_notify(unsigned long val, void *v);
Herbert Xu28259822006-08-06 21:23:26 +100093
Herbert Xu38d21432015-04-20 13:39:00 +080094unsigned int crypto_alg_extsize(struct crypto_alg *alg);
95
Herbert Xuf2aefda2016-01-23 13:51:01 +080096int crypto_type_has_alg(const char *name, const struct crypto_type *frontend,
97 u32 type, u32 mask);
98
Herbert Xu939e1772013-06-25 19:15:17 +080099static inline struct crypto_alg *crypto_alg_get(struct crypto_alg *alg)
100{
Eric Biggersce8614a2017-12-29 10:00:46 -0600101 refcount_inc(&alg->cra_refcnt);
Herbert Xu939e1772013-06-25 19:15:17 +0800102 return alg;
103}
104
Herbert Xu6bfd4802006-09-21 11:39:29 +1000105static inline void crypto_alg_put(struct crypto_alg *alg)
106{
Eric Biggersce8614a2017-12-29 10:00:46 -0600107 if (refcount_dec_and_test(&alg->cra_refcnt) && alg->cra_destroy)
Herbert Xu6bfd4802006-09-21 11:39:29 +1000108 alg->cra_destroy(alg);
109}
110
Herbert Xu4cc77202006-08-06 21:16:34 +1000111static inline int crypto_tmpl_get(struct crypto_template *tmpl)
112{
113 return try_module_get(tmpl->module);
114}
115
116static inline void crypto_tmpl_put(struct crypto_template *tmpl)
117{
118 module_put(tmpl->module);
119}
120
Herbert Xu28259822006-08-06 21:23:26 +1000121static inline int crypto_is_larval(struct crypto_alg *alg)
122{
123 return alg->cra_flags & CRYPTO_ALG_LARVAL;
124}
125
Herbert Xu6bfd4802006-09-21 11:39:29 +1000126static inline int crypto_is_dead(struct crypto_alg *alg)
127{
128 return alg->cra_flags & CRYPTO_ALG_DEAD;
129}
130
131static inline int crypto_is_moribund(struct crypto_alg *alg)
132{
133 return alg->cra_flags & (CRYPTO_ALG_DEAD | CRYPTO_ALG_DYING);
134}
135
Herbert Xu73d38642008-08-03 21:15:23 +0800136static inline void crypto_notify(unsigned long val, void *v)
Herbert Xu28259822006-08-06 21:23:26 +1000137{
Herbert Xu73d38642008-08-03 21:15:23 +0800138 blocking_notifier_call_chain(&crypto_chain, val, v);
Herbert Xu28259822006-08-06 21:23:26 +1000139}
140
Herbert Xu1dbb9202020-07-30 22:39:13 +1000141static inline void crypto_yield(u32 flags)
142{
143 if (flags & CRYPTO_TFM_REQ_MAY_SLEEP)
144 cond_resched();
145}
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147#endif /* _CRYPTO_INTERNAL_H */
148