Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
Herbert Xu | 378f4f5 | 2007-12-17 20:07:31 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Symmetric key ciphers. |
| 4 | * |
| 5 | * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au> |
Herbert Xu | 378f4f5 | 2007-12-17 20:07:31 +0800 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #ifndef _CRYPTO_INTERNAL_SKCIPHER_H |
| 9 | #define _CRYPTO_INTERNAL_SKCIPHER_H |
| 10 | |
| 11 | #include <crypto/algapi.h> |
Herbert Xu | 61da88e | 2007-12-17 21:51:27 +0800 | [diff] [blame] | 12 | #include <crypto/skcipher.h> |
Herbert Xu | b286d8b | 2016-11-22 20:08:12 +0800 | [diff] [blame] | 13 | #include <linux/list.h> |
Herbert Xu | ecfc432 | 2007-12-05 21:08:36 +1100 | [diff] [blame] | 14 | #include <linux/types.h> |
| 15 | |
Herbert Xu | b286d8b | 2016-11-22 20:08:12 +0800 | [diff] [blame] | 16 | struct aead_request; |
Herbert Xu | ecfc432 | 2007-12-05 21:08:36 +1100 | [diff] [blame] | 17 | struct rtattr; |
Herbert Xu | 378f4f5 | 2007-12-17 20:07:31 +0800 | [diff] [blame] | 18 | |
Herbert Xu | 4e6c3df | 2016-07-12 13:17:31 +0800 | [diff] [blame] | 19 | struct skcipher_instance { |
| 20 | void (*free)(struct skcipher_instance *inst); |
| 21 | union { |
| 22 | struct { |
| 23 | char head[offsetof(struct skcipher_alg, base)]; |
| 24 | struct crypto_instance base; |
| 25 | } s; |
| 26 | struct skcipher_alg alg; |
| 27 | }; |
| 28 | }; |
| 29 | |
Herbert Xu | 378f4f5 | 2007-12-17 20:07:31 +0800 | [diff] [blame] | 30 | struct crypto_skcipher_spawn { |
| 31 | struct crypto_spawn base; |
| 32 | }; |
| 33 | |
Herbert Xu | b286d8b | 2016-11-22 20:08:12 +0800 | [diff] [blame] | 34 | struct skcipher_walk { |
| 35 | union { |
| 36 | struct { |
| 37 | struct page *page; |
| 38 | unsigned long offset; |
| 39 | } phys; |
| 40 | |
| 41 | struct { |
| 42 | u8 *page; |
| 43 | void *addr; |
| 44 | } virt; |
| 45 | } src, dst; |
| 46 | |
| 47 | struct scatter_walk in; |
| 48 | unsigned int nbytes; |
| 49 | |
| 50 | struct scatter_walk out; |
| 51 | unsigned int total; |
| 52 | |
| 53 | struct list_head buffers; |
| 54 | |
| 55 | u8 *page; |
| 56 | u8 *buffer; |
| 57 | u8 *oiv; |
| 58 | void *iv; |
| 59 | |
| 60 | unsigned int ivsize; |
| 61 | |
| 62 | int flags; |
| 63 | unsigned int blocksize; |
Ard Biesheuvel | c821f6a | 2016-12-29 14:09:08 +0000 | [diff] [blame] | 64 | unsigned int stride; |
Herbert Xu | b286d8b | 2016-11-22 20:08:12 +0800 | [diff] [blame] | 65 | unsigned int alignmask; |
| 66 | }; |
| 67 | |
Herbert Xu | 4e6c3df | 2016-07-12 13:17:31 +0800 | [diff] [blame] | 68 | static inline struct crypto_instance *skcipher_crypto_instance( |
| 69 | struct skcipher_instance *inst) |
| 70 | { |
| 71 | return &inst->s.base; |
| 72 | } |
| 73 | |
| 74 | static inline struct skcipher_instance *skcipher_alg_instance( |
| 75 | struct crypto_skcipher *skcipher) |
| 76 | { |
| 77 | return container_of(crypto_skcipher_alg(skcipher), |
| 78 | struct skcipher_instance, alg); |
| 79 | } |
| 80 | |
| 81 | static inline void *skcipher_instance_ctx(struct skcipher_instance *inst) |
| 82 | { |
| 83 | return crypto_instance_ctx(skcipher_crypto_instance(inst)); |
| 84 | } |
| 85 | |
| 86 | static inline void skcipher_request_complete(struct skcipher_request *req, int err) |
| 87 | { |
| 88 | req->base.complete(&req->base, err); |
| 89 | } |
| 90 | |
Eric Biggers | b9f76dd | 2020-01-02 19:58:45 -0800 | [diff] [blame] | 91 | int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn, |
| 92 | struct crypto_instance *inst, |
| 93 | const char *name, u32 type, u32 mask); |
Herbert Xu | 3a01d0e | 2016-07-12 13:17:50 +0800 | [diff] [blame] | 94 | |
Herbert Xu | 378f4f5 | 2007-12-17 20:07:31 +0800 | [diff] [blame] | 95 | static inline void crypto_drop_skcipher(struct crypto_skcipher_spawn *spawn) |
| 96 | { |
| 97 | crypto_drop_spawn(&spawn->base); |
| 98 | } |
| 99 | |
Herbert Xu | 3a01d0e | 2016-07-12 13:17:50 +0800 | [diff] [blame] | 100 | static inline struct skcipher_alg *crypto_skcipher_spawn_alg( |
Herbert Xu | 4e6c3df | 2016-07-12 13:17:31 +0800 | [diff] [blame] | 101 | struct crypto_skcipher_spawn *spawn) |
| 102 | { |
| 103 | return container_of(spawn->base.alg, struct skcipher_alg, base); |
| 104 | } |
| 105 | |
Herbert Xu | 3a01d0e | 2016-07-12 13:17:50 +0800 | [diff] [blame] | 106 | static inline struct skcipher_alg *crypto_spawn_skcipher_alg( |
Herbert Xu | 378f4f5 | 2007-12-17 20:07:31 +0800 | [diff] [blame] | 107 | struct crypto_skcipher_spawn *spawn) |
| 108 | { |
Herbert Xu | 3a01d0e | 2016-07-12 13:17:50 +0800 | [diff] [blame] | 109 | return crypto_skcipher_spawn_alg(spawn); |
| 110 | } |
| 111 | |
| 112 | static inline struct crypto_skcipher *crypto_spawn_skcipher( |
| 113 | struct crypto_skcipher_spawn *spawn) |
| 114 | { |
| 115 | return crypto_spawn_tfm2(&spawn->base); |
Herbert Xu | 378f4f5 | 2007-12-17 20:07:31 +0800 | [diff] [blame] | 116 | } |
| 117 | |
Herbert Xu | 4e6c3df | 2016-07-12 13:17:31 +0800 | [diff] [blame] | 118 | static inline void crypto_skcipher_set_reqsize( |
| 119 | struct crypto_skcipher *skcipher, unsigned int reqsize) |
| 120 | { |
| 121 | skcipher->reqsize = reqsize; |
| 122 | } |
| 123 | |
| 124 | int crypto_register_skcipher(struct skcipher_alg *alg); |
| 125 | void crypto_unregister_skcipher(struct skcipher_alg *alg); |
| 126 | int crypto_register_skciphers(struct skcipher_alg *algs, int count); |
| 127 | void crypto_unregister_skciphers(struct skcipher_alg *algs, int count); |
| 128 | int skcipher_register_instance(struct crypto_template *tmpl, |
| 129 | struct skcipher_instance *inst); |
| 130 | |
Herbert Xu | b286d8b | 2016-11-22 20:08:12 +0800 | [diff] [blame] | 131 | int skcipher_walk_done(struct skcipher_walk *walk, int err); |
| 132 | int skcipher_walk_virt(struct skcipher_walk *walk, |
| 133 | struct skcipher_request *req, |
| 134 | bool atomic); |
| 135 | void skcipher_walk_atomise(struct skcipher_walk *walk); |
| 136 | int skcipher_walk_async(struct skcipher_walk *walk, |
| 137 | struct skcipher_request *req); |
Herbert Xu | 34bc085 | 2016-11-30 21:14:07 +0800 | [diff] [blame] | 138 | int skcipher_walk_aead_encrypt(struct skcipher_walk *walk, |
| 139 | struct aead_request *req, bool atomic); |
| 140 | int skcipher_walk_aead_decrypt(struct skcipher_walk *walk, |
| 141 | struct aead_request *req, bool atomic); |
Herbert Xu | b286d8b | 2016-11-22 20:08:12 +0800 | [diff] [blame] | 142 | void skcipher_walk_complete(struct skcipher_walk *walk, int err); |
| 143 | |
Ard Biesheuvel | 6017826 | 2019-09-03 09:43:30 -0700 | [diff] [blame] | 144 | static inline void skcipher_walk_abort(struct skcipher_walk *walk) |
| 145 | { |
| 146 | skcipher_walk_done(walk, -ECANCELED); |
| 147 | } |
| 148 | |
Herbert Xu | 7a7ffe6 | 2015-08-20 15:21:45 +0800 | [diff] [blame] | 149 | static inline void *crypto_skcipher_ctx(struct crypto_skcipher *tfm) |
| 150 | { |
| 151 | return crypto_tfm_ctx(&tfm->base); |
| 152 | } |
| 153 | |
| 154 | static inline void *skcipher_request_ctx(struct skcipher_request *req) |
| 155 | { |
| 156 | return req->__ctx; |
| 157 | } |
| 158 | |
| 159 | static inline u32 skcipher_request_flags(struct skcipher_request *req) |
| 160 | { |
| 161 | return req->base.flags; |
| 162 | } |
| 163 | |
Herbert Xu | 4e6c3df | 2016-07-12 13:17:31 +0800 | [diff] [blame] | 164 | static inline unsigned int crypto_skcipher_alg_min_keysize( |
| 165 | struct skcipher_alg *alg) |
| 166 | { |
Herbert Xu | 4e6c3df | 2016-07-12 13:17:31 +0800 | [diff] [blame] | 167 | return alg->min_keysize; |
| 168 | } |
| 169 | |
| 170 | static inline unsigned int crypto_skcipher_alg_max_keysize( |
| 171 | struct skcipher_alg *alg) |
| 172 | { |
Herbert Xu | 4e6c3df | 2016-07-12 13:17:31 +0800 | [diff] [blame] | 173 | return alg->max_keysize; |
| 174 | } |
| 175 | |
Eric Biggers | 314d0f0 | 2019-06-02 22:46:11 -0700 | [diff] [blame] | 176 | static inline unsigned int crypto_skcipher_alg_walksize( |
| 177 | struct skcipher_alg *alg) |
| 178 | { |
Eric Biggers | 314d0f0 | 2019-06-02 22:46:11 -0700 | [diff] [blame] | 179 | return alg->walksize; |
| 180 | } |
| 181 | |
| 182 | /** |
Eric Biggers | 314d0f0 | 2019-06-02 22:46:11 -0700 | [diff] [blame] | 183 | * crypto_skcipher_walksize() - obtain walk size |
| 184 | * @tfm: cipher handle |
| 185 | * |
| 186 | * In some cases, algorithms can only perform optimally when operating on |
| 187 | * multiple blocks in parallel. This is reflected by the walksize, which |
| 188 | * must be a multiple of the chunksize (or equal if the concern does not |
| 189 | * apply) |
| 190 | * |
| 191 | * Return: walk size in bytes |
| 192 | */ |
| 193 | static inline unsigned int crypto_skcipher_walksize( |
| 194 | struct crypto_skcipher *tfm) |
| 195 | { |
| 196 | return crypto_skcipher_alg_walksize(crypto_skcipher_alg(tfm)); |
| 197 | } |
| 198 | |
Eric Biggers | 0872da1 | 2019-01-03 20:16:14 -0800 | [diff] [blame] | 199 | /* Helpers for simple block cipher modes of operation */ |
| 200 | struct skcipher_ctx_simple { |
| 201 | struct crypto_cipher *cipher; /* underlying block cipher */ |
| 202 | }; |
| 203 | static inline struct crypto_cipher * |
| 204 | skcipher_cipher_simple(struct crypto_skcipher *tfm) |
| 205 | { |
| 206 | struct skcipher_ctx_simple *ctx = crypto_skcipher_ctx(tfm); |
| 207 | |
| 208 | return ctx->cipher; |
| 209 | } |
Herbert Xu | b3c16bf | 2019-12-20 13:29:40 +0800 | [diff] [blame] | 210 | |
| 211 | struct skcipher_instance *skcipher_alloc_instance_simple( |
| 212 | struct crypto_template *tmpl, struct rtattr **tb); |
| 213 | |
| 214 | static inline struct crypto_alg *skcipher_ialg_simple( |
| 215 | struct skcipher_instance *inst) |
| 216 | { |
Eric Biggers | aacd5b4 | 2020-01-02 19:59:00 -0800 | [diff] [blame] | 217 | struct crypto_cipher_spawn *spawn = skcipher_instance_ctx(inst); |
Herbert Xu | b3c16bf | 2019-12-20 13:29:40 +0800 | [diff] [blame] | 218 | |
Eric Biggers | aacd5b4 | 2020-01-02 19:59:00 -0800 | [diff] [blame] | 219 | return crypto_spawn_cipher_alg(spawn); |
Herbert Xu | b3c16bf | 2019-12-20 13:29:40 +0800 | [diff] [blame] | 220 | } |
Eric Biggers | 0872da1 | 2019-01-03 20:16:14 -0800 | [diff] [blame] | 221 | |
Herbert Xu | 378f4f5 | 2007-12-17 20:07:31 +0800 | [diff] [blame] | 222 | #endif /* _CRYPTO_INTERNAL_SKCIPHER_H */ |
| 223 | |