at master 7.8 kB view raw
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 24struct aead_request; 25struct rtattr; 26 27struct 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 38struct 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 49struct crypto_skcipher_spawn { 50 struct crypto_spawn base; 51}; 52 53struct crypto_lskcipher_spawn { 54 struct crypto_spawn base; 55}; 56 57struct 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 union { 71 /* Virtual address of the destination. */ 72 struct { 73 struct { 74 void *const addr; 75 } virt; 76 } dst; 77 78 /* Private field for the API, do not use. */ 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 98static inline struct crypto_instance *skcipher_crypto_instance( 99 struct skcipher_instance *inst) 100{ 101 return &inst->s.base; 102} 103 104static inline struct crypto_instance *lskcipher_crypto_instance( 105 struct lskcipher_instance *inst) 106{ 107 return &inst->s.base; 108} 109 110static 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 117static 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 124static inline void *skcipher_instance_ctx(struct skcipher_instance *inst) 125{ 126 return crypto_instance_ctx(skcipher_crypto_instance(inst)); 127} 128 129static inline void *lskcipher_instance_ctx(struct lskcipher_instance *inst) 130{ 131 return crypto_instance_ctx(lskcipher_crypto_instance(inst)); 132} 133 134static inline void skcipher_request_complete(struct skcipher_request *req, int err) 135{ 136 crypto_request_complete(&req->base, err); 137} 138 139int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn, 140 struct crypto_instance *inst, 141 const char *name, u32 type, u32 mask); 142 143int crypto_grab_lskcipher(struct crypto_lskcipher_spawn *spawn, 144 struct crypto_instance *inst, 145 const char *name, u32 type, u32 mask); 146 147static inline void crypto_drop_skcipher(struct crypto_skcipher_spawn *spawn) 148{ 149 crypto_drop_spawn(&spawn->base); 150} 151 152static inline void crypto_drop_lskcipher(struct crypto_lskcipher_spawn *spawn) 153{ 154 crypto_drop_spawn(&spawn->base); 155} 156 157static 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 163static 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 169static inline struct lskcipher_alg *crypto_spawn_lskcipher_alg( 170 struct crypto_lskcipher_spawn *spawn) 171{ 172 return crypto_lskcipher_spawn_alg(spawn); 173} 174 175static inline struct crypto_skcipher *crypto_spawn_skcipher( 176 struct crypto_skcipher_spawn *spawn) 177{ 178 return crypto_spawn_tfm2(&spawn->base); 179} 180 181static inline struct crypto_lskcipher *crypto_spawn_lskcipher( 182 struct crypto_lskcipher_spawn *spawn) 183{ 184 return crypto_spawn_tfm2(&spawn->base); 185} 186 187static inline void crypto_skcipher_set_reqsize( 188 struct crypto_skcipher *skcipher, unsigned int reqsize) 189{ 190 skcipher->reqsize = reqsize; 191} 192 193static 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 200int crypto_register_skcipher(struct skcipher_alg *alg); 201void crypto_unregister_skcipher(struct skcipher_alg *alg); 202int crypto_register_skciphers(struct skcipher_alg *algs, int count); 203void crypto_unregister_skciphers(struct skcipher_alg *algs, int count); 204int skcipher_register_instance(struct crypto_template *tmpl, 205 struct skcipher_instance *inst); 206 207int crypto_register_lskcipher(struct lskcipher_alg *alg); 208void crypto_unregister_lskcipher(struct lskcipher_alg *alg); 209int crypto_register_lskciphers(struct lskcipher_alg *algs, int count); 210void crypto_unregister_lskciphers(struct lskcipher_alg *algs, int count); 211int lskcipher_register_instance(struct crypto_template *tmpl, 212 struct lskcipher_instance *inst); 213 214int skcipher_walk_done(struct skcipher_walk *walk, int res); 215int skcipher_walk_virt(struct skcipher_walk *__restrict walk, 216 struct skcipher_request *__restrict req, 217 bool atomic); 218int skcipher_walk_aead_encrypt(struct skcipher_walk *__restrict walk, 219 struct aead_request *__restrict req, 220 bool atomic); 221int skcipher_walk_aead_decrypt(struct skcipher_walk *__restrict walk, 222 struct aead_request *__restrict req, 223 bool atomic); 224 225static inline void skcipher_walk_abort(struct skcipher_walk *walk) 226{ 227 skcipher_walk_done(walk, -ECANCELED); 228} 229 230static inline void *crypto_skcipher_ctx(struct crypto_skcipher *tfm) 231{ 232 return crypto_tfm_ctx(&tfm->base); 233} 234 235static inline void *crypto_lskcipher_ctx(struct crypto_lskcipher *tfm) 236{ 237 return crypto_tfm_ctx(&tfm->base); 238} 239 240static inline void *crypto_skcipher_ctx_dma(struct crypto_skcipher *tfm) 241{ 242 return crypto_tfm_ctx_dma(&tfm->base); 243} 244 245static inline void *skcipher_request_ctx(struct skcipher_request *req) 246{ 247 return req->__ctx; 248} 249 250static inline void *skcipher_request_ctx_dma(struct skcipher_request *req) 251{ 252 unsigned int align = crypto_dma_align(); 253 254 if (align <= crypto_tfm_ctx_alignment()) 255 align = 1; 256 257 return PTR_ALIGN(skcipher_request_ctx(req), align); 258} 259 260static inline u32 skcipher_request_flags(struct skcipher_request *req) 261{ 262 return req->base.flags; 263} 264 265/* Helpers for simple block cipher modes of operation */ 266struct skcipher_ctx_simple { 267 struct crypto_cipher *cipher; /* underlying block cipher */ 268}; 269static inline struct crypto_cipher * 270skcipher_cipher_simple(struct crypto_skcipher *tfm) 271{ 272 struct skcipher_ctx_simple *ctx = crypto_skcipher_ctx(tfm); 273 274 return ctx->cipher; 275} 276 277struct skcipher_instance *skcipher_alloc_instance_simple( 278 struct crypto_template *tmpl, struct rtattr **tb); 279 280static inline struct crypto_alg *skcipher_ialg_simple( 281 struct skcipher_instance *inst) 282{ 283 struct crypto_cipher_spawn *spawn = skcipher_instance_ctx(inst); 284 285 return crypto_spawn_cipher_alg(spawn); 286} 287 288static inline struct crypto_lskcipher *lskcipher_cipher_simple( 289 struct crypto_lskcipher *tfm) 290{ 291 struct crypto_lskcipher **ctx = crypto_lskcipher_ctx(tfm); 292 293 return *ctx; 294} 295 296struct lskcipher_instance *lskcipher_alloc_instance_simple( 297 struct crypto_template *tmpl, struct rtattr **tb); 298 299static inline struct lskcipher_alg *lskcipher_ialg_simple( 300 struct lskcipher_instance *inst) 301{ 302 struct crypto_lskcipher_spawn *spawn = lskcipher_instance_ctx(inst); 303 304 return crypto_lskcipher_spawn_alg(spawn); 305} 306 307#endif /* _CRYPTO_INTERNAL_SKCIPHER_H */ 308