123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579 |
- #ifndef _CRYPTO_SKCIPHER_H
- #define _CRYPTO_SKCIPHER_H
- #include <linux/crypto.h>
- #include <linux/kernel.h>
- #include <linux/slab.h>
- struct skcipher_request {
- unsigned int cryptlen;
- u8 *iv;
- struct scatterlist *src;
- struct scatterlist *dst;
- struct crypto_async_request base;
- void *__ctx[] CRYPTO_MINALIGN_ATTR;
- };
- struct skcipher_givcrypt_request {
- u64 seq;
- u8 *giv;
- struct ablkcipher_request creq;
- };
- struct crypto_skcipher {
- int (*setkey)(struct crypto_skcipher *tfm, const u8 *key,
- unsigned int keylen);
- int (*encrypt)(struct skcipher_request *req);
- int (*decrypt)(struct skcipher_request *req);
- unsigned int ivsize;
- unsigned int reqsize;
- unsigned int keysize;
- struct crypto_tfm base;
- };
- struct skcipher_alg {
- int (*setkey)(struct crypto_skcipher *tfm, const u8 *key,
- unsigned int keylen);
- int (*encrypt)(struct skcipher_request *req);
- int (*decrypt)(struct skcipher_request *req);
- int (*init)(struct crypto_skcipher *tfm);
- void (*exit)(struct crypto_skcipher *tfm);
- unsigned int min_keysize;
- unsigned int max_keysize;
- unsigned int ivsize;
- unsigned int chunksize;
- struct crypto_alg base;
- };
- #define SKCIPHER_REQUEST_ON_STACK(name, tfm) \
- char __##name##_desc[sizeof(struct skcipher_request) + \
- crypto_skcipher_reqsize(tfm)] CRYPTO_MINALIGN_ATTR; \
- struct skcipher_request *name = (void *)__##name##_desc
- static inline struct crypto_skcipher *__crypto_skcipher_cast(
- struct crypto_tfm *tfm)
- {
- return container_of(tfm, struct crypto_skcipher, base);
- }
- struct crypto_skcipher *crypto_alloc_skcipher(const char *alg_name,
- u32 type, u32 mask);
- static inline struct crypto_tfm *crypto_skcipher_tfm(
- struct crypto_skcipher *tfm)
- {
- return &tfm->base;
- }
- static inline void crypto_free_skcipher(struct crypto_skcipher *tfm)
- {
- crypto_destroy_tfm(tfm, crypto_skcipher_tfm(tfm));
- }
- static inline int crypto_has_skcipher(const char *alg_name, u32 type,
- u32 mask)
- {
- return crypto_has_alg(alg_name, crypto_skcipher_type(type),
- crypto_skcipher_mask(mask));
- }
- int crypto_has_skcipher2(const char *alg_name, u32 type, u32 mask);
- static inline const char *crypto_skcipher_driver_name(
- struct crypto_skcipher *tfm)
- {
- return crypto_tfm_alg_driver_name(crypto_skcipher_tfm(tfm));
- }
- static inline struct skcipher_alg *crypto_skcipher_alg(
- struct crypto_skcipher *tfm)
- {
- return container_of(crypto_skcipher_tfm(tfm)->__crt_alg,
- struct skcipher_alg, base);
- }
- static inline unsigned int crypto_skcipher_alg_ivsize(struct skcipher_alg *alg)
- {
- if ((alg->base.cra_flags & CRYPTO_ALG_TYPE_MASK) ==
- CRYPTO_ALG_TYPE_BLKCIPHER)
- return alg->base.cra_blkcipher.ivsize;
- if (alg->base.cra_ablkcipher.encrypt)
- return alg->base.cra_ablkcipher.ivsize;
- return alg->ivsize;
- }
- static inline unsigned int crypto_skcipher_ivsize(struct crypto_skcipher *tfm)
- {
- return tfm->ivsize;
- }
- static inline unsigned int crypto_skcipher_alg_chunksize(
- struct skcipher_alg *alg)
- {
- if ((alg->base.cra_flags & CRYPTO_ALG_TYPE_MASK) ==
- CRYPTO_ALG_TYPE_BLKCIPHER)
- return alg->base.cra_blocksize;
- if (alg->base.cra_ablkcipher.encrypt)
- return alg->base.cra_blocksize;
- return alg->chunksize;
- }
- static inline unsigned int crypto_skcipher_chunksize(
- struct crypto_skcipher *tfm)
- {
- return crypto_skcipher_alg_chunksize(crypto_skcipher_alg(tfm));
- }
- static inline unsigned int crypto_skcipher_blocksize(
- struct crypto_skcipher *tfm)
- {
- return crypto_tfm_alg_blocksize(crypto_skcipher_tfm(tfm));
- }
- static inline unsigned int crypto_skcipher_alignmask(
- struct crypto_skcipher *tfm)
- {
- return crypto_tfm_alg_alignmask(crypto_skcipher_tfm(tfm));
- }
- static inline u32 crypto_skcipher_get_flags(struct crypto_skcipher *tfm)
- {
- return crypto_tfm_get_flags(crypto_skcipher_tfm(tfm));
- }
- static inline void crypto_skcipher_set_flags(struct crypto_skcipher *tfm,
- u32 flags)
- {
- crypto_tfm_set_flags(crypto_skcipher_tfm(tfm), flags);
- }
- static inline void crypto_skcipher_clear_flags(struct crypto_skcipher *tfm,
- u32 flags)
- {
- crypto_tfm_clear_flags(crypto_skcipher_tfm(tfm), flags);
- }
- static inline int crypto_skcipher_setkey(struct crypto_skcipher *tfm,
- const u8 *key, unsigned int keylen)
- {
- return tfm->setkey(tfm, key, keylen);
- }
- static inline bool crypto_skcipher_has_setkey(struct crypto_skcipher *tfm)
- {
- return tfm->keysize;
- }
- static inline unsigned int crypto_skcipher_default_keysize(
- struct crypto_skcipher *tfm)
- {
- return tfm->keysize;
- }
- static inline struct crypto_skcipher *crypto_skcipher_reqtfm(
- struct skcipher_request *req)
- {
- return __crypto_skcipher_cast(req->base.tfm);
- }
- static inline int crypto_skcipher_encrypt(struct skcipher_request *req)
- {
- struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
- return tfm->encrypt(req);
- }
- static inline int crypto_skcipher_decrypt(struct skcipher_request *req)
- {
- struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
- return tfm->decrypt(req);
- }
- static inline unsigned int crypto_skcipher_reqsize(struct crypto_skcipher *tfm)
- {
- return tfm->reqsize;
- }
- static inline void skcipher_request_set_tfm(struct skcipher_request *req,
- struct crypto_skcipher *tfm)
- {
- req->base.tfm = crypto_skcipher_tfm(tfm);
- }
- static inline struct skcipher_request *skcipher_request_cast(
- struct crypto_async_request *req)
- {
- return container_of(req, struct skcipher_request, base);
- }
- static inline struct skcipher_request *skcipher_request_alloc(
- struct crypto_skcipher *tfm, gfp_t gfp)
- {
- struct skcipher_request *req;
- req = kmalloc(sizeof(struct skcipher_request) +
- crypto_skcipher_reqsize(tfm), gfp);
- if (likely(req))
- skcipher_request_set_tfm(req, tfm);
- return req;
- }
- static inline void skcipher_request_free(struct skcipher_request *req)
- {
- kzfree(req);
- }
- static inline void skcipher_request_zero(struct skcipher_request *req)
- {
- struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
- memzero_explicit(req, sizeof(*req) + crypto_skcipher_reqsize(tfm));
- }
- static inline void skcipher_request_set_callback(struct skcipher_request *req,
- u32 flags,
- crypto_completion_t compl,
- void *data)
- {
- req->base.complete = compl;
- req->base.data = data;
- req->base.flags = flags;
- }
- static inline void skcipher_request_set_crypt(
- struct skcipher_request *req,
- struct scatterlist *src, struct scatterlist *dst,
- unsigned int cryptlen, void *iv)
- {
- req->src = src;
- req->dst = dst;
- req->cryptlen = cryptlen;
- req->iv = iv;
- }
- #endif
|