1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 /* 3 * Symmetric key ciphers. 4 * 5 * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au> 6 */ 7 8 #ifndef _CRYPTO_INTERNAL_SKCIPHER_H 9 #define _CRYPTO_INTERNAL_SKCIPHER_H 10 11 #include <crypto/algapi.h> 12 #include <crypto/internal/cipher.h> 13 #include <crypto/skcipher.h> 14 #include <linux/types.h> 15 16 /* 17 * Set this if your algorithm is sync but needs a reqsize larger 18 * than MAX_SYNC_SKCIPHER_REQSIZE. 19 * 20 * Reuse bit that is specific to hash algorithms. 21 */ 22 #define CRYPTO_ALG_SKCIPHER_REQSIZE_LARGE CRYPTO_ALG_OPTIONAL_KEY 23 24 struct aead_request; 25 struct rtattr; 26 27 struct skcipher_instance { 28 void (*free)(struct skcipher_instance *inst); 29 union { 30 struct { 31 char head[offsetof(struct skcipher_alg, base)]; 32 struct crypto_instance base; 33 } s; 34 struct skcipher_alg alg; 35 }; 36 }; 37 38 struct lskcipher_instance { 39 void (*free)(struct lskcipher_instance *inst); 40 union { 41 struct { 42 char head[offsetof(struct lskcipher_alg, co.base)]; 43 struct crypto_instance base; 44 } s; 45 struct lskcipher_alg alg; 46 }; 47 }; 48 49 struct crypto_skcipher_spawn { 50 struct crypto_spawn base; 51 }; 52 53 struct crypto_lskcipher_spawn { 54 struct crypto_spawn base; 55 }; 56 57 struct skcipher_walk { 58 union { 59 /* Virtual address of the source. */ 60 struct { 61 struct { 62 const void *const addr; 63 } virt; 64 } src; 65 66 /* Private field for the API, do not use. */ 67 struct scatter_walk in; 68 }; 69 70 unsigned int nbytes; 71 72 union { 73 /* Virtual address of the destination. */ 74 struct { 75 struct { 76 void *const addr; 77 } virt; 78 } dst; 79 80 /* Private field for the API, do not use. */ 81 struct scatter_walk out; 82 }; 83 84 unsigned int total; 85 86 u8 *page; 87 u8 *buffer; 88 u8 *oiv; 89 void *iv; 90 91 unsigned int ivsize; 92 93 int flags; 94 unsigned int blocksize; 95 unsigned int stride; 96 unsigned int alignmask; 97 }; 98 99 static inline struct crypto_instance *skcipher_crypto_instance( 100 struct skcipher_instance *inst) 101 { 102 return &inst->s.base; 103 } 104 105 static inline struct crypto_instance *lskcipher_crypto_instance( 106 struct lskcipher_instance *inst) 107 { 108 return &inst->s.base; 109 } 110 111 static inline struct skcipher_instance *skcipher_alg_instance( 112 struct crypto_skcipher *skcipher) 113 { 114 return container_of(crypto_skcipher_alg(skcipher), 115 struct skcipher_instance, alg); 116 } 117 118 static inline struct lskcipher_instance *lskcipher_alg_instance( 119 struct crypto_lskcipher *lskcipher) 120 { 121 return container_of(crypto_lskcipher_alg(lskcipher), 122 struct lskcipher_instance, alg); 123 } 124 125 static inline void *skcipher_instance_ctx(struct skcipher_instance *inst) 126 { 127 return crypto_instance_ctx(skcipher_crypto_instance(inst)); 128 } 129 130 static inline void *lskcipher_instance_ctx(struct lskcipher_instance *inst) 131 { 132 return crypto_instance_ctx(lskcipher_crypto_instance(inst)); 133 } 134 135 static inline void skcipher_request_complete(struct skcipher_request *req, int err) 136 { 137 crypto_request_complete(&req->base, err); 138 } 139 140 int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn, 141 struct crypto_instance *inst, 142 const char *name, u32 type, u32 mask); 143 144 int crypto_grab_lskcipher(struct crypto_lskcipher_spawn *spawn, 145 struct crypto_instance *inst, 146 const char *name, u32 type, u32 mask); 147 148 static inline void crypto_drop_skcipher(struct crypto_skcipher_spawn *spawn) 149 { 150 crypto_drop_spawn(&spawn->base); 151 } 152 153 static inline void crypto_drop_lskcipher(struct crypto_lskcipher_spawn *spawn) 154 { 155 crypto_drop_spawn(&spawn->base); 156 } 157 158 static inline struct lskcipher_alg *crypto_lskcipher_spawn_alg( 159 struct crypto_lskcipher_spawn *spawn) 160 { 161 return container_of(spawn->base.alg, struct lskcipher_alg, co.base); 162 } 163 164 static inline struct skcipher_alg_common *crypto_spawn_skcipher_alg_common( 165 struct crypto_skcipher_spawn *spawn) 166 { 167 return container_of(spawn->base.alg, struct skcipher_alg_common, base); 168 } 169 170 static inline struct lskcipher_alg *crypto_spawn_lskcipher_alg( 171 struct crypto_lskcipher_spawn *spawn) 172 { 173 return crypto_lskcipher_spawn_alg(spawn); 174 } 175 176 static inline struct crypto_skcipher *crypto_spawn_skcipher( 177 struct crypto_skcipher_spawn *spawn) 178 { 179 return crypto_spawn_tfm2(&spawn->base); 180 } 181 182 static inline struct crypto_lskcipher *crypto_spawn_lskcipher( 183 struct crypto_lskcipher_spawn *spawn) 184 { 185 return crypto_spawn_tfm2(&spawn->base); 186 } 187 188 static inline void crypto_skcipher_set_reqsize( 189 struct crypto_skcipher *skcipher, unsigned int reqsize) 190 { 191 skcipher->reqsize = reqsize; 192 } 193 194 static inline void crypto_skcipher_set_reqsize_dma( 195 struct crypto_skcipher *skcipher, unsigned int reqsize) 196 { 197 reqsize += crypto_dma_align() & ~(crypto_tfm_ctx_alignment() - 1); 198 skcipher->reqsize = reqsize; 199 } 200 201 int crypto_register_skcipher(struct skcipher_alg *alg); 202 void crypto_unregister_skcipher(struct skcipher_alg *alg); 203 int crypto_register_skciphers(struct skcipher_alg *algs, int count); 204 void crypto_unregister_skciphers(struct skcipher_alg *algs, int count); 205 int skcipher_register_instance(struct crypto_template *tmpl, 206 struct skcipher_instance *inst); 207 208 int crypto_register_lskcipher(struct lskcipher_alg *alg); 209 void crypto_unregister_lskcipher(struct lskcipher_alg *alg); 210 int crypto_register_lskciphers(struct lskcipher_alg *algs, int count); 211 void crypto_unregister_lskciphers(struct lskcipher_alg *algs, int count); 212 int lskcipher_register_instance(struct crypto_template *tmpl, 213 struct lskcipher_instance *inst); 214 215 int skcipher_walk_done(struct skcipher_walk *walk, int res); 216 int skcipher_walk_virt(struct skcipher_walk *__restrict walk, 217 struct skcipher_request *__restrict req, 218 bool atomic); 219 int skcipher_walk_aead_encrypt(struct skcipher_walk *__restrict walk, 220 struct aead_request *__restrict req, 221 bool atomic); 222 int skcipher_walk_aead_decrypt(struct skcipher_walk *__restrict walk, 223 struct aead_request *__restrict req, 224 bool atomic); 225 226 static inline void skcipher_walk_abort(struct skcipher_walk *walk) 227 { 228 skcipher_walk_done(walk, -ECANCELED); 229 } 230 231 static inline void *crypto_skcipher_ctx(struct crypto_skcipher *tfm) 232 { 233 return crypto_tfm_ctx(&tfm->base); 234 } 235 236 static inline void *crypto_lskcipher_ctx(struct crypto_lskcipher *tfm) 237 { 238 return crypto_tfm_ctx(&tfm->base); 239 } 240 241 static inline void *crypto_skcipher_ctx_dma(struct crypto_skcipher *tfm) 242 { 243 return crypto_tfm_ctx_dma(&tfm->base); 244 } 245 246 static inline void *skcipher_request_ctx(struct skcipher_request *req) 247 { 248 return req->__ctx; 249 } 250 251 static inline void *skcipher_request_ctx_dma(struct skcipher_request *req) 252 { 253 unsigned int align = crypto_dma_align(); 254 255 if (align <= crypto_tfm_ctx_alignment()) 256 align = 1; 257 258 return PTR_ALIGN(skcipher_request_ctx(req), align); 259 } 260 261 static inline u32 skcipher_request_flags(struct skcipher_request *req) 262 { 263 return req->base.flags; 264 } 265 266 /* Helpers for simple block cipher modes of operation */ 267 struct skcipher_ctx_simple { 268 struct crypto_cipher *cipher; /* underlying block cipher */ 269 }; 270 static inline struct crypto_cipher * 271 skcipher_cipher_simple(struct crypto_skcipher *tfm) 272 { 273 struct skcipher_ctx_simple *ctx = crypto_skcipher_ctx(tfm); 274 275 return ctx->cipher; 276 } 277 278 struct skcipher_instance *skcipher_alloc_instance_simple( 279 struct crypto_template *tmpl, struct rtattr **tb); 280 281 static inline struct crypto_alg *skcipher_ialg_simple( 282 struct skcipher_instance *inst) 283 { 284 struct crypto_cipher_spawn *spawn = skcipher_instance_ctx(inst); 285 286 return crypto_spawn_cipher_alg(spawn); 287 } 288 289 static inline struct crypto_lskcipher *lskcipher_cipher_simple( 290 struct crypto_lskcipher *tfm) 291 { 292 struct crypto_lskcipher **ctx = crypto_lskcipher_ctx(tfm); 293 294 return *ctx; 295 } 296 297 struct lskcipher_instance *lskcipher_alloc_instance_simple( 298 struct crypto_template *tmpl, struct rtattr **tb); 299 300 static inline struct lskcipher_alg *lskcipher_ialg_simple( 301 struct lskcipher_instance *inst) 302 { 303 struct crypto_lskcipher_spawn *spawn = lskcipher_instance_ctx(inst); 304 305 return crypto_lskcipher_spawn_alg(spawn); 306 } 307 308 #endif /* _CRYPTO_INTERNAL_SKCIPHER_H */ 309 310