Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Cryptographic API. |
| 4 | * |
| 5 | * Copyright (c) 2002 James Morris <jmorris@intercode.com.au> |
Herbert Xu | 5cb1454b | 2005-11-05 16:58:14 +1100 | [diff] [blame] | 6 | * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | */ |
| 8 | #ifndef _CRYPTO_INTERNAL_H |
| 9 | #define _CRYPTO_INTERNAL_H |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 10 | |
| 11 | #include <crypto/algapi.h> |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 12 | #include <linux/completion.h> |
Herbert Xu | 5cb1454b | 2005-11-05 16:58:14 +1100 | [diff] [blame] | 13 | #include <linux/list.h> |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 14 | #include <linux/module.h> |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 15 | #include <linux/notifier.h> |
Herbert Xu | 1dbb920 | 2020-07-30 22:39:13 +1000 | [diff] [blame] | 16 | #include <linux/numa.h> |
| 17 | #include <linux/refcount.h> |
Herbert Xu | 5cb1454b | 2005-11-05 16:58:14 +1100 | [diff] [blame] | 18 | #include <linux/rwsem.h> |
Herbert Xu | 1dbb920 | 2020-07-30 22:39:13 +1000 | [diff] [blame] | 19 | #include <linux/sched.h> |
| 20 | #include <linux/types.h> |
Neil Horman | ccb778e | 2008-08-05 14:13:08 +0800 | [diff] [blame] | 21 | |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 22 | struct crypto_instance; |
| 23 | struct crypto_template; |
| 24 | |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 25 | struct crypto_larval { |
| 26 | struct crypto_alg alg; |
| 27 | struct crypto_alg *adult; |
| 28 | struct completion completion; |
Herbert Xu | 492e2b6 | 2006-09-21 11:35:17 +1000 | [diff] [blame] | 29 | u32 mask; |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 30 | }; |
| 31 | |
Herbert Xu | 5cb1454b | 2005-11-05 16:58:14 +1100 | [diff] [blame] | 32 | extern struct list_head crypto_alg_list; |
| 33 | extern struct rw_semaphore crypto_alg_sem; |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 34 | extern struct blocking_notifier_head crypto_chain; |
Herbert Xu | 5cb1454b | 2005-11-05 16:58:14 +1100 | [diff] [blame] | 35 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #ifdef CONFIG_PROC_FS |
| 37 | void __init crypto_init_proc(void); |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 38 | void __exit crypto_exit_proc(void); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #else |
| 40 | static inline void crypto_init_proc(void) |
| 41 | { } |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 42 | static inline void crypto_exit_proc(void) |
| 43 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #endif |
| 45 | |
Herbert Xu | f1ddcaf | 2007-01-27 10:05:15 +1100 | [diff] [blame] | 46 | static inline unsigned int crypto_cipher_ctxsize(struct crypto_alg *alg) |
Herbert Xu | fbdae9f | 2005-07-06 13:53:29 -0700 | [diff] [blame] | 47 | { |
Herbert Xu | f1ddcaf | 2007-01-27 10:05:15 +1100 | [diff] [blame] | 48 | return alg->cra_ctxsize; |
Herbert Xu | fbdae9f | 2005-07-06 13:53:29 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Herbert Xu | f1ddcaf | 2007-01-27 10:05:15 +1100 | [diff] [blame] | 51 | static inline unsigned int crypto_compress_ctxsize(struct crypto_alg *alg) |
Herbert Xu | fbdae9f | 2005-07-06 13:53:29 -0700 | [diff] [blame] | 52 | { |
| 53 | return alg->cra_ctxsize; |
| 54 | } |
| 55 | |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 56 | struct crypto_alg *crypto_mod_get(struct crypto_alg *alg); |
Herbert Xu | 492e2b6 | 2006-09-21 11:35:17 +1000 | [diff] [blame] | 57 | struct crypto_alg *crypto_alg_mod_lookup(const char *name, u32 type, u32 mask); |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 58 | |
Herbert Xu | 73d3864 | 2008-08-03 21:15:23 +0800 | [diff] [blame] | 59 | struct crypto_larval *crypto_larval_alloc(const char *name, u32 type, u32 mask); |
Herbert Xu | b9c55aa | 2007-12-04 12:46:48 +1100 | [diff] [blame] | 60 | void crypto_larval_kill(struct crypto_alg *alg); |
Herbert Xu | 73d3864 | 2008-08-03 21:15:23 +0800 | [diff] [blame] | 61 | void crypto_alg_tested(const char *name, int err); |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 62 | |
Steffen Klassert | 89b596b | 2011-09-27 07:22:08 +0200 | [diff] [blame] | 63 | void crypto_remove_spawns(struct crypto_alg *alg, struct list_head *list, |
| 64 | struct crypto_alg *nalg); |
Steffen Klassert | 22e5b20 | 2011-09-27 07:23:07 +0200 | [diff] [blame] | 65 | void crypto_remove_final(struct list_head *list); |
Herbert Xu | 6603523 | 2020-04-10 16:09:42 +1000 | [diff] [blame] | 66 | void crypto_shoot_alg(struct crypto_alg *alg); |
Herbert Xu | 27d2a33 | 2007-01-24 20:50:26 +1100 | [diff] [blame] | 67 | struct crypto_tfm *__crypto_alloc_tfm(struct crypto_alg *alg, u32 type, |
| 68 | u32 mask); |
Barry Song | 7bc13b5 | 2020-07-05 21:18:58 +1200 | [diff] [blame] | 69 | void *crypto_create_tfm_node(struct crypto_alg *alg, |
| 70 | const struct crypto_type *frontend, int node); |
| 71 | |
| 72 | static 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 Xu | d06854f | 2009-07-08 17:53:16 +0800 | [diff] [blame] | 78 | struct crypto_alg *crypto_find_alg(const char *alg_name, |
| 79 | const struct crypto_type *frontend, |
| 80 | u32 type, u32 mask); |
Barry Song | 7bc13b5 | 2020-07-05 21:18:58 +1200 | [diff] [blame] | 81 | |
| 82 | void *crypto_alloc_tfm_node(const char *alg_name, |
| 83 | const struct crypto_type *frontend, u32 type, u32 mask, |
| 84 | int node); |
| 85 | |
| 86 | static 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 Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 91 | |
Herbert Xu | 73d3864 | 2008-08-03 21:15:23 +0800 | [diff] [blame] | 92 | int crypto_probing_notify(unsigned long val, void *v); |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 93 | |
Herbert Xu | 38d2143 | 2015-04-20 13:39:00 +0800 | [diff] [blame] | 94 | unsigned int crypto_alg_extsize(struct crypto_alg *alg); |
| 95 | |
Herbert Xu | f2aefda | 2016-01-23 13:51:01 +0800 | [diff] [blame] | 96 | int crypto_type_has_alg(const char *name, const struct crypto_type *frontend, |
| 97 | u32 type, u32 mask); |
| 98 | |
Herbert Xu | 939e177 | 2013-06-25 19:15:17 +0800 | [diff] [blame] | 99 | static inline struct crypto_alg *crypto_alg_get(struct crypto_alg *alg) |
| 100 | { |
Eric Biggers | ce8614a | 2017-12-29 10:00:46 -0600 | [diff] [blame] | 101 | refcount_inc(&alg->cra_refcnt); |
Herbert Xu | 939e177 | 2013-06-25 19:15:17 +0800 | [diff] [blame] | 102 | return alg; |
| 103 | } |
| 104 | |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 105 | static inline void crypto_alg_put(struct crypto_alg *alg) |
| 106 | { |
Eric Biggers | ce8614a | 2017-12-29 10:00:46 -0600 | [diff] [blame] | 107 | if (refcount_dec_and_test(&alg->cra_refcnt) && alg->cra_destroy) |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 108 | alg->cra_destroy(alg); |
| 109 | } |
| 110 | |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 111 | static inline int crypto_tmpl_get(struct crypto_template *tmpl) |
| 112 | { |
| 113 | return try_module_get(tmpl->module); |
| 114 | } |
| 115 | |
| 116 | static inline void crypto_tmpl_put(struct crypto_template *tmpl) |
| 117 | { |
| 118 | module_put(tmpl->module); |
| 119 | } |
| 120 | |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 121 | static inline int crypto_is_larval(struct crypto_alg *alg) |
| 122 | { |
| 123 | return alg->cra_flags & CRYPTO_ALG_LARVAL; |
| 124 | } |
| 125 | |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 126 | static inline int crypto_is_dead(struct crypto_alg *alg) |
| 127 | { |
| 128 | return alg->cra_flags & CRYPTO_ALG_DEAD; |
| 129 | } |
| 130 | |
| 131 | static inline int crypto_is_moribund(struct crypto_alg *alg) |
| 132 | { |
| 133 | return alg->cra_flags & (CRYPTO_ALG_DEAD | CRYPTO_ALG_DYING); |
| 134 | } |
| 135 | |
Herbert Xu | 73d3864 | 2008-08-03 21:15:23 +0800 | [diff] [blame] | 136 | static inline void crypto_notify(unsigned long val, void *v) |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 137 | { |
Herbert Xu | 73d3864 | 2008-08-03 21:15:23 +0800 | [diff] [blame] | 138 | blocking_notifier_call_chain(&crypto_chain, val, v); |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 139 | } |
| 140 | |
Herbert Xu | 1dbb920 | 2020-07-30 22:39:13 +1000 | [diff] [blame] | 141 | static inline void crypto_yield(u32 flags) |
| 142 | { |
| 143 | if (flags & CRYPTO_TFM_REQ_MAY_SLEEP) |
| 144 | cond_resched(); |
| 145 | } |
| 146 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | #endif /* _CRYPTO_INTERNAL_H */ |
| 148 | |