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