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 struct { 60 void *addr; 61 } virt; 62 } src, dst; 63 64 struct scatter_walk in; 65 unsigned int nbytes; 66 67 struct scatter_walk out; 68 unsigned int total; 69 70 u8 *page; 71 u8 *buffer; 72 u8 *oiv; 73 void *iv; 74 75 unsigned int ivsize; 76 77 int flags; 78 unsigned int blocksize; 79 unsigned int stride; 80 unsigned int alignmask; 81 }; 82 83 static inline struct crypto_instance *skcipher_crypto_instance( 84 struct skcipher_instance *inst) 85 { 86 return &inst->s.base; 87 } 88 89 static inline struct crypto_instance *lskcipher_crypto_instance( 90 struct lskcipher_instance *inst) 91 { 92 return &inst->s.base; 93 } 94 95 static inline struct skcipher_instance *skcipher_alg_instance( 96 struct crypto_skcipher *skcipher) 97 { 98 return container_of(crypto_skcipher_alg(skcipher), 99 struct skcipher_instance, alg); 100 } 101 102 static inline struct lskcipher_instance *lskcipher_alg_instance( 103 struct crypto_lskcipher *lskcipher) 104 { 105 return container_of(crypto_lskcipher_alg(lskcipher), 106 struct lskcipher_instance, alg); 107 } 108 109 static inline void *skcipher_instance_ctx(struct skcipher_instance *inst) 110 { 111 return crypto_instance_ctx(skcipher_crypto_instance(inst)); 112 } 113 114 static inline void *lskcipher_instance_ctx(struct lskcipher_instance *inst) 115 { 116 return crypto_instance_ctx(lskcipher_crypto_instance(inst)); 117 } 118 119 static inline void skcipher_request_complete(struct skcipher_request *req, int err) 120 { 121 crypto_request_complete(&req->base, err); 122 } 123 124 int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn, 125 struct crypto_instance *inst, 126 const char *name, u32 type, u32 mask); 127 128 int crypto_grab_lskcipher(struct crypto_lskcipher_spawn *spawn, 129 struct crypto_instance *inst, 130 const char *name, u32 type, u32 mask); 131 132 static inline void crypto_drop_skcipher(struct crypto_skcipher_spawn *spawn) 133 { 134 crypto_drop_spawn(&spawn->base); 135 } 136 137 static inline void crypto_drop_lskcipher(struct crypto_lskcipher_spawn *spawn) 138 { 139 crypto_drop_spawn(&spawn->base); 140 } 141 142 static inline struct lskcipher_alg *crypto_lskcipher_spawn_alg( 143 struct crypto_lskcipher_spawn *spawn) 144 { 145 return container_of(spawn->base.alg, struct lskcipher_alg, co.base); 146 } 147 148 static inline struct skcipher_alg_common *crypto_spawn_skcipher_alg_common( 149 struct crypto_skcipher_spawn *spawn) 150 { 151 return container_of(spawn->base.alg, struct skcipher_alg_common, base); 152 } 153 154 static inline struct lskcipher_alg *crypto_spawn_lskcipher_alg( 155 struct crypto_lskcipher_spawn *spawn) 156 { 157 return crypto_lskcipher_spawn_alg(spawn); 158 } 159 160 static inline struct crypto_skcipher *crypto_spawn_skcipher( 161 struct crypto_skcipher_spawn *spawn) 162 { 163 return crypto_spawn_tfm2(&spawn->base); 164 } 165 166 static inline struct crypto_lskcipher *crypto_spawn_lskcipher( 167 struct crypto_lskcipher_spawn *spawn) 168 { 169 return crypto_spawn_tfm2(&spawn->base); 170 } 171 172 static inline void crypto_skcipher_set_reqsize( 173 struct crypto_skcipher *skcipher, unsigned int reqsize) 174 { 175 skcipher->reqsize = reqsize; 176 } 177 178 static inline void crypto_skcipher_set_reqsize_dma( 179 struct crypto_skcipher *skcipher, unsigned int reqsize) 180 { 181 reqsize += crypto_dma_align() & ~(crypto_tfm_ctx_alignment() - 1); 182 skcipher->reqsize = reqsize; 183 } 184 185 int crypto_register_skcipher(struct skcipher_alg *alg); 186 void crypto_unregister_skcipher(struct skcipher_alg *alg); 187 int crypto_register_skciphers(struct skcipher_alg *algs, int count); 188 void crypto_unregister_skciphers(struct skcipher_alg *algs, int count); 189 int skcipher_register_instance(struct crypto_template *tmpl, 190 struct skcipher_instance *inst); 191 192 int crypto_register_lskcipher(struct lskcipher_alg *alg); 193 void crypto_unregister_lskcipher(struct lskcipher_alg *alg); 194 int crypto_register_lskciphers(struct lskcipher_alg *algs, int count); 195 void crypto_unregister_lskciphers(struct lskcipher_alg *algs, int count); 196 int lskcipher_register_instance(struct crypto_template *tmpl, 197 struct lskcipher_instance *inst); 198 199 int skcipher_walk_done(struct skcipher_walk *walk, int res); 200 int skcipher_walk_virt(struct skcipher_walk *__restrict walk, 201 struct skcipher_request *__restrict req, 202 bool atomic); 203 int skcipher_walk_aead_encrypt(struct skcipher_walk *__restrict walk, 204 struct aead_request *__restrict req, 205 bool atomic); 206 int skcipher_walk_aead_decrypt(struct skcipher_walk *__restrict walk, 207 struct aead_request *__restrict req, 208 bool atomic); 209 210 static inline void skcipher_walk_abort(struct skcipher_walk *walk) 211 { 212 skcipher_walk_done(walk, -ECANCELED); 213 } 214 215 static inline void *crypto_skcipher_ctx(struct crypto_skcipher *tfm) 216 { 217 return crypto_tfm_ctx(&tfm->base); 218 } 219 220 static inline void *crypto_lskcipher_ctx(struct crypto_lskcipher *tfm) 221 { 222 return crypto_tfm_ctx(&tfm->base); 223 } 224 225 static inline void *crypto_skcipher_ctx_dma(struct crypto_skcipher *tfm) 226 { 227 return crypto_tfm_ctx_dma(&tfm->base); 228 } 229 230 static inline void *skcipher_request_ctx(struct skcipher_request *req) 231 { 232 return req->__ctx; 233 } 234 235 static inline void *skcipher_request_ctx_dma(struct skcipher_request *req) 236 { 237 unsigned int align = crypto_dma_align(); 238 239 if (align <= crypto_tfm_ctx_alignment()) 240 align = 1; 241 242 return PTR_ALIGN(skcipher_request_ctx(req), align); 243 } 244 245 static inline u32 skcipher_request_flags(struct skcipher_request *req) 246 { 247 return req->base.flags; 248 } 249 250 /* Helpers for simple block cipher modes of operation */ 251 struct skcipher_ctx_simple { 252 struct crypto_cipher *cipher; /* underlying block cipher */ 253 }; 254 static inline struct crypto_cipher * 255 skcipher_cipher_simple(struct crypto_skcipher *tfm) 256 { 257 struct skcipher_ctx_simple *ctx = crypto_skcipher_ctx(tfm); 258 259 return ctx->cipher; 260 } 261 262 struct skcipher_instance *skcipher_alloc_instance_simple( 263 struct crypto_template *tmpl, struct rtattr **tb); 264 265 static inline struct crypto_alg *skcipher_ialg_simple( 266 struct skcipher_instance *inst) 267 { 268 struct crypto_cipher_spawn *spawn = skcipher_instance_ctx(inst); 269 270 return crypto_spawn_cipher_alg(spawn); 271 } 272 273 static inline struct crypto_lskcipher *lskcipher_cipher_simple( 274 struct crypto_lskcipher *tfm) 275 { 276 struct crypto_lskcipher **ctx = crypto_lskcipher_ctx(tfm); 277 278 return *ctx; 279 } 280 281 struct lskcipher_instance *lskcipher_alloc_instance_simple( 282 struct crypto_template *tmpl, struct rtattr **tb); 283 284 static inline struct lskcipher_alg *lskcipher_ialg_simple( 285 struct lskcipher_instance *inst) 286 { 287 struct crypto_lskcipher_spawn *spawn = lskcipher_instance_ctx(inst); 288 289 return crypto_lskcipher_spawn_alg(spawn); 290 } 291 292 #endif /* _CRYPTO_INTERNAL_SKCIPHER_H */ 293 294