0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/errno.h>
0010 #include <linux/kernel.h>
0011 #include <linux/module.h>
0012 #include <linux/seq_file.h>
0013 #include <linux/slab.h>
0014 #include <linux/string.h>
0015 #include <linux/crypto.h>
0016 #include <linux/compiler.h>
0017 #include <linux/vmalloc.h>
0018 #include <crypto/algapi.h>
0019 #include <linux/cryptouser.h>
0020 #include <net/netlink.h>
0021 #include <linux/scatterlist.h>
0022 #include <crypto/scatterwalk.h>
0023 #include <crypto/internal/acompress.h>
0024 #include <crypto/internal/scompress.h>
0025 #include "internal.h"
0026
0027 struct scomp_scratch {
0028 spinlock_t lock;
0029 void *src;
0030 void *dst;
0031 };
0032
0033 static DEFINE_PER_CPU(struct scomp_scratch, scomp_scratch) = {
0034 .lock = __SPIN_LOCK_UNLOCKED(scomp_scratch.lock),
0035 };
0036
0037 static const struct crypto_type crypto_scomp_type;
0038 static int scomp_scratch_users;
0039 static DEFINE_MUTEX(scomp_lock);
0040
0041 #ifdef CONFIG_NET
0042 static int crypto_scomp_report(struct sk_buff *skb, struct crypto_alg *alg)
0043 {
0044 struct crypto_report_comp rscomp;
0045
0046 memset(&rscomp, 0, sizeof(rscomp));
0047
0048 strscpy(rscomp.type, "scomp", sizeof(rscomp.type));
0049
0050 return nla_put(skb, CRYPTOCFGA_REPORT_COMPRESS,
0051 sizeof(rscomp), &rscomp);
0052 }
0053 #else
0054 static int crypto_scomp_report(struct sk_buff *skb, struct crypto_alg *alg)
0055 {
0056 return -ENOSYS;
0057 }
0058 #endif
0059
0060 static void crypto_scomp_show(struct seq_file *m, struct crypto_alg *alg)
0061 __maybe_unused;
0062
0063 static void crypto_scomp_show(struct seq_file *m, struct crypto_alg *alg)
0064 {
0065 seq_puts(m, "type : scomp\n");
0066 }
0067
0068 static void crypto_scomp_free_scratches(void)
0069 {
0070 struct scomp_scratch *scratch;
0071 int i;
0072
0073 for_each_possible_cpu(i) {
0074 scratch = per_cpu_ptr(&scomp_scratch, i);
0075
0076 vfree(scratch->src);
0077 vfree(scratch->dst);
0078 scratch->src = NULL;
0079 scratch->dst = NULL;
0080 }
0081 }
0082
0083 static int crypto_scomp_alloc_scratches(void)
0084 {
0085 struct scomp_scratch *scratch;
0086 int i;
0087
0088 for_each_possible_cpu(i) {
0089 void *mem;
0090
0091 scratch = per_cpu_ptr(&scomp_scratch, i);
0092
0093 mem = vmalloc_node(SCOMP_SCRATCH_SIZE, cpu_to_node(i));
0094 if (!mem)
0095 goto error;
0096 scratch->src = mem;
0097 mem = vmalloc_node(SCOMP_SCRATCH_SIZE, cpu_to_node(i));
0098 if (!mem)
0099 goto error;
0100 scratch->dst = mem;
0101 }
0102 return 0;
0103 error:
0104 crypto_scomp_free_scratches();
0105 return -ENOMEM;
0106 }
0107
0108 static int crypto_scomp_init_tfm(struct crypto_tfm *tfm)
0109 {
0110 int ret = 0;
0111
0112 mutex_lock(&scomp_lock);
0113 if (!scomp_scratch_users++)
0114 ret = crypto_scomp_alloc_scratches();
0115 mutex_unlock(&scomp_lock);
0116
0117 return ret;
0118 }
0119
0120 static int scomp_acomp_comp_decomp(struct acomp_req *req, int dir)
0121 {
0122 struct crypto_acomp *tfm = crypto_acomp_reqtfm(req);
0123 void **tfm_ctx = acomp_tfm_ctx(tfm);
0124 struct crypto_scomp *scomp = *tfm_ctx;
0125 void **ctx = acomp_request_ctx(req);
0126 struct scomp_scratch *scratch;
0127 int ret;
0128
0129 if (!req->src || !req->slen || req->slen > SCOMP_SCRATCH_SIZE)
0130 return -EINVAL;
0131
0132 if (req->dst && !req->dlen)
0133 return -EINVAL;
0134
0135 if (!req->dlen || req->dlen > SCOMP_SCRATCH_SIZE)
0136 req->dlen = SCOMP_SCRATCH_SIZE;
0137
0138 scratch = raw_cpu_ptr(&scomp_scratch);
0139 spin_lock(&scratch->lock);
0140
0141 scatterwalk_map_and_copy(scratch->src, req->src, 0, req->slen, 0);
0142 if (dir)
0143 ret = crypto_scomp_compress(scomp, scratch->src, req->slen,
0144 scratch->dst, &req->dlen, *ctx);
0145 else
0146 ret = crypto_scomp_decompress(scomp, scratch->src, req->slen,
0147 scratch->dst, &req->dlen, *ctx);
0148 if (!ret) {
0149 if (!req->dst) {
0150 req->dst = sgl_alloc(req->dlen, GFP_ATOMIC, NULL);
0151 if (!req->dst) {
0152 ret = -ENOMEM;
0153 goto out;
0154 }
0155 }
0156 scatterwalk_map_and_copy(scratch->dst, req->dst, 0, req->dlen,
0157 1);
0158 }
0159 out:
0160 spin_unlock(&scratch->lock);
0161 return ret;
0162 }
0163
0164 static int scomp_acomp_compress(struct acomp_req *req)
0165 {
0166 return scomp_acomp_comp_decomp(req, 1);
0167 }
0168
0169 static int scomp_acomp_decompress(struct acomp_req *req)
0170 {
0171 return scomp_acomp_comp_decomp(req, 0);
0172 }
0173
0174 static void crypto_exit_scomp_ops_async(struct crypto_tfm *tfm)
0175 {
0176 struct crypto_scomp **ctx = crypto_tfm_ctx(tfm);
0177
0178 crypto_free_scomp(*ctx);
0179
0180 mutex_lock(&scomp_lock);
0181 if (!--scomp_scratch_users)
0182 crypto_scomp_free_scratches();
0183 mutex_unlock(&scomp_lock);
0184 }
0185
0186 int crypto_init_scomp_ops_async(struct crypto_tfm *tfm)
0187 {
0188 struct crypto_alg *calg = tfm->__crt_alg;
0189 struct crypto_acomp *crt = __crypto_acomp_tfm(tfm);
0190 struct crypto_scomp **ctx = crypto_tfm_ctx(tfm);
0191 struct crypto_scomp *scomp;
0192
0193 if (!crypto_mod_get(calg))
0194 return -EAGAIN;
0195
0196 scomp = crypto_create_tfm(calg, &crypto_scomp_type);
0197 if (IS_ERR(scomp)) {
0198 crypto_mod_put(calg);
0199 return PTR_ERR(scomp);
0200 }
0201
0202 *ctx = scomp;
0203 tfm->exit = crypto_exit_scomp_ops_async;
0204
0205 crt->compress = scomp_acomp_compress;
0206 crt->decompress = scomp_acomp_decompress;
0207 crt->dst_free = sgl_free;
0208 crt->reqsize = sizeof(void *);
0209
0210 return 0;
0211 }
0212
0213 struct acomp_req *crypto_acomp_scomp_alloc_ctx(struct acomp_req *req)
0214 {
0215 struct crypto_acomp *acomp = crypto_acomp_reqtfm(req);
0216 struct crypto_tfm *tfm = crypto_acomp_tfm(acomp);
0217 struct crypto_scomp **tfm_ctx = crypto_tfm_ctx(tfm);
0218 struct crypto_scomp *scomp = *tfm_ctx;
0219 void *ctx;
0220
0221 ctx = crypto_scomp_alloc_ctx(scomp);
0222 if (IS_ERR(ctx)) {
0223 kfree(req);
0224 return NULL;
0225 }
0226
0227 *req->__ctx = ctx;
0228
0229 return req;
0230 }
0231
0232 void crypto_acomp_scomp_free_ctx(struct acomp_req *req)
0233 {
0234 struct crypto_acomp *acomp = crypto_acomp_reqtfm(req);
0235 struct crypto_tfm *tfm = crypto_acomp_tfm(acomp);
0236 struct crypto_scomp **tfm_ctx = crypto_tfm_ctx(tfm);
0237 struct crypto_scomp *scomp = *tfm_ctx;
0238 void *ctx = *req->__ctx;
0239
0240 if (ctx)
0241 crypto_scomp_free_ctx(scomp, ctx);
0242 }
0243
0244 static const struct crypto_type crypto_scomp_type = {
0245 .extsize = crypto_alg_extsize,
0246 .init_tfm = crypto_scomp_init_tfm,
0247 #ifdef CONFIG_PROC_FS
0248 .show = crypto_scomp_show,
0249 #endif
0250 .report = crypto_scomp_report,
0251 .maskclear = ~CRYPTO_ALG_TYPE_MASK,
0252 .maskset = CRYPTO_ALG_TYPE_MASK,
0253 .type = CRYPTO_ALG_TYPE_SCOMPRESS,
0254 .tfmsize = offsetof(struct crypto_scomp, base),
0255 };
0256
0257 int crypto_register_scomp(struct scomp_alg *alg)
0258 {
0259 struct crypto_alg *base = &alg->base;
0260
0261 base->cra_type = &crypto_scomp_type;
0262 base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
0263 base->cra_flags |= CRYPTO_ALG_TYPE_SCOMPRESS;
0264
0265 return crypto_register_alg(base);
0266 }
0267 EXPORT_SYMBOL_GPL(crypto_register_scomp);
0268
0269 void crypto_unregister_scomp(struct scomp_alg *alg)
0270 {
0271 crypto_unregister_alg(&alg->base);
0272 }
0273 EXPORT_SYMBOL_GPL(crypto_unregister_scomp);
0274
0275 int crypto_register_scomps(struct scomp_alg *algs, int count)
0276 {
0277 int i, ret;
0278
0279 for (i = 0; i < count; i++) {
0280 ret = crypto_register_scomp(&algs[i]);
0281 if (ret)
0282 goto err;
0283 }
0284
0285 return 0;
0286
0287 err:
0288 for (--i; i >= 0; --i)
0289 crypto_unregister_scomp(&algs[i]);
0290
0291 return ret;
0292 }
0293 EXPORT_SYMBOL_GPL(crypto_register_scomps);
0294
0295 void crypto_unregister_scomps(struct scomp_alg *algs, int count)
0296 {
0297 int i;
0298
0299 for (i = count - 1; i >= 0; --i)
0300 crypto_unregister_scomp(&algs[i]);
0301 }
0302 EXPORT_SYMBOL_GPL(crypto_unregister_scomps);
0303
0304 MODULE_LICENSE("GPL");
0305 MODULE_DESCRIPTION("Synchronous compression type");