^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) * Asynchronous Compression operations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) 2016, Intel Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Authors: Weigang Li <weigang.li@intel.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Giovanni Cabiddu <giovanni.cabiddu@intel.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/seq_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/crypto.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <crypto/algapi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/cryptouser.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/compiler.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <net/netlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <crypto/internal/acompress.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <crypto/internal/scompress.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include "internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) static const struct crypto_type crypto_acomp_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #ifdef CONFIG_NET
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) static int crypto_acomp_report(struct sk_buff *skb, struct crypto_alg *alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) struct crypto_report_acomp racomp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) memset(&racomp, 0, sizeof(racomp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) strscpy(racomp.type, "acomp", sizeof(racomp.type));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) return nla_put(skb, CRYPTOCFGA_REPORT_ACOMP, sizeof(racomp), &racomp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static int crypto_acomp_report(struct sk_buff *skb, struct crypto_alg *alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) return -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) static void crypto_acomp_show(struct seq_file *m, struct crypto_alg *alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) __maybe_unused;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) static void crypto_acomp_show(struct seq_file *m, struct crypto_alg *alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) seq_puts(m, "type : acomp\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) static void crypto_acomp_exit_tfm(struct crypto_tfm *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) struct crypto_acomp *acomp = __crypto_acomp_tfm(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) struct acomp_alg *alg = crypto_acomp_alg(acomp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) alg->exit(acomp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) static int crypto_acomp_init_tfm(struct crypto_tfm *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) struct crypto_acomp *acomp = __crypto_acomp_tfm(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) struct acomp_alg *alg = crypto_acomp_alg(acomp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if (tfm->__crt_alg->cra_type != &crypto_acomp_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) return crypto_init_scomp_ops_async(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) acomp->compress = alg->compress;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) acomp->decompress = alg->decompress;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) acomp->dst_free = alg->dst_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) acomp->reqsize = alg->reqsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if (alg->exit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) acomp->base.exit = crypto_acomp_exit_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) if (alg->init)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) return alg->init(acomp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) return 0;
^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 unsigned int crypto_acomp_extsize(struct crypto_alg *alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) int extsize = crypto_alg_extsize(alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if (alg->cra_type != &crypto_acomp_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) extsize += sizeof(struct crypto_scomp *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) return extsize;
^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) static const struct crypto_type crypto_acomp_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) .extsize = crypto_acomp_extsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) .init_tfm = crypto_acomp_init_tfm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) #ifdef CONFIG_PROC_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) .show = crypto_acomp_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) .report = crypto_acomp_report,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) .maskclear = ~CRYPTO_ALG_TYPE_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) .maskset = CRYPTO_ALG_TYPE_ACOMPRESS_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) .type = CRYPTO_ALG_TYPE_ACOMPRESS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) .tfmsize = offsetof(struct crypto_acomp, base),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) struct crypto_acomp *crypto_alloc_acomp(const char *alg_name, u32 type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) u32 mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) return crypto_alloc_tfm(alg_name, &crypto_acomp_type, type, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) EXPORT_SYMBOL_GPL(crypto_alloc_acomp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) struct crypto_acomp *crypto_alloc_acomp_node(const char *alg_name, u32 type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) u32 mask, int node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) return crypto_alloc_tfm_node(alg_name, &crypto_acomp_type, type, mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) EXPORT_SYMBOL_GPL(crypto_alloc_acomp_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) struct acomp_req *acomp_request_alloc(struct crypto_acomp *acomp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) struct crypto_tfm *tfm = crypto_acomp_tfm(acomp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) struct acomp_req *req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) req = __acomp_request_alloc(acomp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) if (req && (tfm->__crt_alg->cra_type != &crypto_acomp_type))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) return crypto_acomp_scomp_alloc_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) return req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) EXPORT_SYMBOL_GPL(acomp_request_alloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) void acomp_request_free(struct acomp_req *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) struct crypto_acomp *acomp = crypto_acomp_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) struct crypto_tfm *tfm = crypto_acomp_tfm(acomp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) if (tfm->__crt_alg->cra_type != &crypto_acomp_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) crypto_acomp_scomp_free_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) if (req->flags & CRYPTO_ACOMP_ALLOC_OUTPUT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) acomp->dst_free(req->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) req->dst = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) __acomp_request_free(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) EXPORT_SYMBOL_GPL(acomp_request_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) int crypto_register_acomp(struct acomp_alg *alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) struct crypto_alg *base = &alg->base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) base->cra_type = &crypto_acomp_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) base->cra_flags |= CRYPTO_ALG_TYPE_ACOMPRESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) return crypto_register_alg(base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) EXPORT_SYMBOL_GPL(crypto_register_acomp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) void crypto_unregister_acomp(struct acomp_alg *alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) crypto_unregister_alg(&alg->base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) EXPORT_SYMBOL_GPL(crypto_unregister_acomp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) int crypto_register_acomps(struct acomp_alg *algs, int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) int i, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) for (i = 0; i < count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) ret = crypto_register_acomp(&algs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) for (--i; i >= 0; --i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) crypto_unregister_acomp(&algs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) EXPORT_SYMBOL_GPL(crypto_register_acomps);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) void crypto_unregister_acomps(struct acomp_alg *algs, int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) for (i = count - 1; i >= 0; --i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) crypto_unregister_acomp(&algs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) EXPORT_SYMBOL_GPL(crypto_unregister_acomps);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) MODULE_DESCRIPTION("Asynchronous compression type");