^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: GPL-2.0-or-later */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Symmetric key ciphers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #ifndef _CRYPTO_INTERNAL_SKCIPHER_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #define _CRYPTO_INTERNAL_SKCIPHER_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <crypto/algapi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <crypto/internal/cipher.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <crypto/skcipher.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) struct aead_request;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) struct rtattr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) struct skcipher_instance {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) void (*free)(struct skcipher_instance *inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) char head[offsetof(struct skcipher_alg, base)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) struct crypto_instance base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) } s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) struct skcipher_alg alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) struct crypto_skcipher_spawn {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) struct crypto_spawn base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) struct skcipher_walk {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) unsigned long offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) } phys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) u8 *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) void *addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) } virt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) } src, dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct scatter_walk in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) unsigned int nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) struct scatter_walk out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) unsigned int total;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) struct list_head buffers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) u8 *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) u8 *buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) u8 *oiv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) void *iv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) unsigned int ivsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) int flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) unsigned int blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) unsigned int stride;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) unsigned int alignmask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) static inline struct crypto_instance *skcipher_crypto_instance(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) struct skcipher_instance *inst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) return &inst->s.base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) static inline struct skcipher_instance *skcipher_alg_instance(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) struct crypto_skcipher *skcipher)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) return container_of(crypto_skcipher_alg(skcipher),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) struct skcipher_instance, alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) static inline void *skcipher_instance_ctx(struct skcipher_instance *inst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) return crypto_instance_ctx(skcipher_crypto_instance(inst));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) static inline void skcipher_request_complete(struct skcipher_request *req, int err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) req->base.complete(&req->base, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) struct crypto_instance *inst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) const char *name, u32 type, u32 mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) static inline void crypto_drop_skcipher(struct crypto_skcipher_spawn *spawn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) crypto_drop_spawn(&spawn->base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) static inline struct skcipher_alg *crypto_skcipher_spawn_alg(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) struct crypto_skcipher_spawn *spawn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) return container_of(spawn->base.alg, struct skcipher_alg, base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) static inline struct skcipher_alg *crypto_spawn_skcipher_alg(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) struct crypto_skcipher_spawn *spawn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) return crypto_skcipher_spawn_alg(spawn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) static inline struct crypto_skcipher *crypto_spawn_skcipher(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) struct crypto_skcipher_spawn *spawn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) return crypto_spawn_tfm2(&spawn->base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) static inline void crypto_skcipher_set_reqsize(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) struct crypto_skcipher *skcipher, unsigned int reqsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) skcipher->reqsize = reqsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) int crypto_register_skcipher(struct skcipher_alg *alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) void crypto_unregister_skcipher(struct skcipher_alg *alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) int crypto_register_skciphers(struct skcipher_alg *algs, int count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) void crypto_unregister_skciphers(struct skcipher_alg *algs, int count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) int skcipher_register_instance(struct crypto_template *tmpl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) struct skcipher_instance *inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) int skcipher_walk_done(struct skcipher_walk *walk, int err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) int skcipher_walk_virt(struct skcipher_walk *walk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) struct skcipher_request *req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) bool atomic);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) void skcipher_walk_atomise(struct skcipher_walk *walk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) int skcipher_walk_async(struct skcipher_walk *walk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) struct skcipher_request *req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) int skcipher_walk_aead_encrypt(struct skcipher_walk *walk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) struct aead_request *req, bool atomic);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) int skcipher_walk_aead_decrypt(struct skcipher_walk *walk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) struct aead_request *req, bool atomic);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) void skcipher_walk_complete(struct skcipher_walk *walk, int err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) static inline void skcipher_walk_abort(struct skcipher_walk *walk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) skcipher_walk_done(walk, -ECANCELED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) static inline void *crypto_skcipher_ctx(struct crypto_skcipher *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) return crypto_tfm_ctx(&tfm->base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) static inline void *skcipher_request_ctx(struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) return req->__ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) static inline u32 skcipher_request_flags(struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) return req->base.flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) static inline unsigned int crypto_skcipher_alg_min_keysize(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) struct skcipher_alg *alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) return alg->min_keysize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) static inline unsigned int crypto_skcipher_alg_max_keysize(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) struct skcipher_alg *alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) return alg->max_keysize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) static inline unsigned int crypto_skcipher_alg_walksize(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) struct skcipher_alg *alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) return alg->walksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) * crypto_skcipher_walksize() - obtain walk size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) * @tfm: cipher handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) * In some cases, algorithms can only perform optimally when operating on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) * multiple blocks in parallel. This is reflected by the walksize, which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) * must be a multiple of the chunksize (or equal if the concern does not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) * apply)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) * Return: walk size in bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) static inline unsigned int crypto_skcipher_walksize(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) struct crypto_skcipher *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) return crypto_skcipher_alg_walksize(crypto_skcipher_alg(tfm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) /* Helpers for simple block cipher modes of operation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) struct skcipher_ctx_simple {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) struct crypto_cipher *cipher; /* underlying block cipher */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) static inline struct crypto_cipher *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) skcipher_cipher_simple(struct crypto_skcipher *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) struct skcipher_ctx_simple *ctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) return ctx->cipher;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) struct skcipher_instance *skcipher_alloc_instance_simple(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) struct crypto_template *tmpl, struct rtattr **tb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) static inline struct crypto_alg *skcipher_ialg_simple(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) struct skcipher_instance *inst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) struct crypto_cipher_spawn *spawn = skcipher_instance_ctx(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) return crypto_spawn_cipher_alg(spawn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) #endif /* _CRYPTO_INTERNAL_SKCIPHER_H */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)