^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * pcrypt - Parallel crypto wrapper.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2009 secunet Security Networks AG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (C) 2009 Steffen Klassert <steffen.klassert@secunet.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <crypto/algapi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <crypto/internal/aead.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/atomic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/kobject.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/cpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <crypto/pcrypt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) static struct padata_instance *pencrypt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) static struct padata_instance *pdecrypt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) static struct kset *pcrypt_kset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct pcrypt_instance_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) struct crypto_aead_spawn spawn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) struct padata_shell *psenc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) struct padata_shell *psdec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) atomic_t tfm_count;
^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 pcrypt_aead_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) struct crypto_aead *child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) unsigned int cb_cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) static inline struct pcrypt_instance_ctx *pcrypt_tfm_ictx(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) struct crypto_aead *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) return aead_instance_ctx(aead_alg_instance(tfm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) static int pcrypt_aead_setkey(struct crypto_aead *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) const u8 *key, unsigned int keylen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) struct pcrypt_aead_ctx *ctx = crypto_aead_ctx(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) return crypto_aead_setkey(ctx->child, key, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) static int pcrypt_aead_setauthsize(struct crypto_aead *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) unsigned int authsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) struct pcrypt_aead_ctx *ctx = crypto_aead_ctx(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) return crypto_aead_setauthsize(ctx->child, authsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) static void pcrypt_aead_serial(struct padata_priv *padata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) struct pcrypt_request *preq = pcrypt_padata_request(padata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) struct aead_request *req = pcrypt_request_ctx(preq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) aead_request_complete(req->base.data, padata->info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) static void pcrypt_aead_done(struct crypto_async_request *areq, int err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) struct aead_request *req = areq->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) struct pcrypt_request *preq = aead_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) struct padata_priv *padata = pcrypt_request_padata(preq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) padata->info = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) padata_do_serial(padata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) static void pcrypt_aead_enc(struct padata_priv *padata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) struct pcrypt_request *preq = pcrypt_padata_request(padata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) struct aead_request *req = pcrypt_request_ctx(preq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) ret = crypto_aead_encrypt(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if (ret == -EINPROGRESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) padata->info = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) padata_do_serial(padata);
^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 int pcrypt_aead_encrypt(struct aead_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) struct pcrypt_request *preq = aead_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) struct aead_request *creq = pcrypt_request_ctx(preq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) struct padata_priv *padata = pcrypt_request_padata(preq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) struct crypto_aead *aead = crypto_aead_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) struct pcrypt_aead_ctx *ctx = crypto_aead_ctx(aead);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) u32 flags = aead_request_flags(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) struct pcrypt_instance_ctx *ictx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) ictx = pcrypt_tfm_ictx(aead);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) memset(padata, 0, sizeof(struct padata_priv));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) padata->parallel = pcrypt_aead_enc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) padata->serial = pcrypt_aead_serial;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) aead_request_set_tfm(creq, ctx->child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) aead_request_set_callback(creq, flags & ~CRYPTO_TFM_REQ_MAY_SLEEP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) pcrypt_aead_done, req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) aead_request_set_crypt(creq, req->src, req->dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) req->cryptlen, req->iv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) aead_request_set_ad(creq, req->assoclen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) err = padata_do_parallel(ictx->psenc, padata, &ctx->cb_cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) return -EINPROGRESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) static void pcrypt_aead_dec(struct padata_priv *padata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) struct pcrypt_request *preq = pcrypt_padata_request(padata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) struct aead_request *req = pcrypt_request_ctx(preq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) ret = crypto_aead_decrypt(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) if (ret == -EINPROGRESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) padata->info = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) padata_do_serial(padata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) static int pcrypt_aead_decrypt(struct aead_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) struct pcrypt_request *preq = aead_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) struct aead_request *creq = pcrypt_request_ctx(preq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) struct padata_priv *padata = pcrypt_request_padata(preq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) struct crypto_aead *aead = crypto_aead_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) struct pcrypt_aead_ctx *ctx = crypto_aead_ctx(aead);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) u32 flags = aead_request_flags(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) struct pcrypt_instance_ctx *ictx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) ictx = pcrypt_tfm_ictx(aead);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) memset(padata, 0, sizeof(struct padata_priv));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) padata->parallel = pcrypt_aead_dec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) padata->serial = pcrypt_aead_serial;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) aead_request_set_tfm(creq, ctx->child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) aead_request_set_callback(creq, flags & ~CRYPTO_TFM_REQ_MAY_SLEEP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) pcrypt_aead_done, req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) aead_request_set_crypt(creq, req->src, req->dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) req->cryptlen, req->iv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) aead_request_set_ad(creq, req->assoclen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) err = padata_do_parallel(ictx->psdec, padata, &ctx->cb_cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) return -EINPROGRESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) return err;
^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 int pcrypt_aead_init_tfm(struct crypto_aead *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) int cpu, cpu_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) struct aead_instance *inst = aead_alg_instance(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) struct pcrypt_instance_ctx *ictx = aead_instance_ctx(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) struct pcrypt_aead_ctx *ctx = crypto_aead_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) struct crypto_aead *cipher;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) cpu_index = (unsigned int)atomic_inc_return(&ictx->tfm_count) %
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) cpumask_weight(cpu_online_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) ctx->cb_cpu = cpumask_first(cpu_online_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) for (cpu = 0; cpu < cpu_index; cpu++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) ctx->cb_cpu = cpumask_next(ctx->cb_cpu, cpu_online_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) cipher = crypto_spawn_aead(&ictx->spawn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) if (IS_ERR(cipher))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) return PTR_ERR(cipher);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) ctx->child = cipher;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) crypto_aead_set_reqsize(tfm, sizeof(struct pcrypt_request) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) sizeof(struct aead_request) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) crypto_aead_reqsize(cipher));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) static void pcrypt_aead_exit_tfm(struct crypto_aead *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) struct pcrypt_aead_ctx *ctx = crypto_aead_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) crypto_free_aead(ctx->child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) static void pcrypt_free(struct aead_instance *inst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) struct pcrypt_instance_ctx *ctx = aead_instance_ctx(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) crypto_drop_aead(&ctx->spawn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) padata_free_shell(ctx->psdec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) padata_free_shell(ctx->psenc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) kfree(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) static int pcrypt_init_instance(struct crypto_instance *inst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) struct crypto_alg *alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) "pcrypt(%s)", alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) return -ENAMETOOLONG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) memcpy(inst->alg.cra_name, alg->cra_name, CRYPTO_MAX_ALG_NAME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) inst->alg.cra_priority = alg->cra_priority + 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) inst->alg.cra_blocksize = alg->cra_blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) inst->alg.cra_alignmask = alg->cra_alignmask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) static int pcrypt_create_aead(struct crypto_template *tmpl, struct rtattr **tb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) struct crypto_attr_type *algt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) struct pcrypt_instance_ctx *ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) struct aead_instance *inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) struct aead_alg *alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) u32 mask = crypto_algt_inherited_mask(algt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) if (!inst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) ctx = aead_instance_ctx(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) ctx->psenc = padata_alloc_shell(pencrypt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) if (!ctx->psenc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) ctx->psdec = padata_alloc_shell(pdecrypt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) if (!ctx->psdec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) err = crypto_grab_aead(&ctx->spawn, aead_crypto_instance(inst),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) crypto_attr_alg_name(tb[1]), 0, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) alg = crypto_spawn_aead_alg(&ctx->spawn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) err = pcrypt_init_instance(aead_crypto_instance(inst), &alg->base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) inst->alg.base.cra_flags |= CRYPTO_ALG_ASYNC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) inst->alg.ivsize = crypto_aead_alg_ivsize(alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) inst->alg.maxauthsize = crypto_aead_alg_maxauthsize(alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) inst->alg.base.cra_ctxsize = sizeof(struct pcrypt_aead_ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) inst->alg.init = pcrypt_aead_init_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) inst->alg.exit = pcrypt_aead_exit_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) inst->alg.setkey = pcrypt_aead_setkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) inst->alg.setauthsize = pcrypt_aead_setauthsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) inst->alg.encrypt = pcrypt_aead_encrypt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) inst->alg.decrypt = pcrypt_aead_decrypt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) inst->free = pcrypt_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) err = aead_register_instance(tmpl, inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) err_free_inst:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) pcrypt_free(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) static int pcrypt_create(struct crypto_template *tmpl, struct rtattr **tb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) struct crypto_attr_type *algt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) algt = crypto_get_attr_type(tb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) if (IS_ERR(algt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) return PTR_ERR(algt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) switch (algt->type & algt->mask & CRYPTO_ALG_TYPE_MASK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) case CRYPTO_ALG_TYPE_AEAD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) return pcrypt_create_aead(tmpl, tb, algt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) static int pcrypt_sysfs_add(struct padata_instance *pinst, const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) pinst->kobj.kset = pcrypt_kset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) ret = kobject_add(&pinst->kobj, NULL, "%s", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) kobject_uevent(&pinst->kobj, KOBJ_ADD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) static int pcrypt_init_padata(struct padata_instance **pinst, const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) int ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) *pinst = padata_alloc(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) if (!*pinst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) ret = pcrypt_sysfs_add(*pinst, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) padata_free(*pinst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) static struct crypto_template pcrypt_tmpl = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) .name = "pcrypt",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) .create = pcrypt_create,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) .module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) static int __init pcrypt_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) int err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) pcrypt_kset = kset_create_and_add("pcrypt", NULL, kernel_kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) if (!pcrypt_kset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) err = pcrypt_init_padata(&pencrypt, "pencrypt");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) goto err_unreg_kset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) err = pcrypt_init_padata(&pdecrypt, "pdecrypt");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) goto err_deinit_pencrypt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) return crypto_register_template(&pcrypt_tmpl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) err_deinit_pencrypt:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) padata_free(pencrypt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) err_unreg_kset:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) kset_unregister(pcrypt_kset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) static void __exit pcrypt_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) crypto_unregister_template(&pcrypt_tmpl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) padata_free(pencrypt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) padata_free(pdecrypt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) kset_unregister(pcrypt_kset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) subsys_initcall(pcrypt_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) module_exit(pcrypt_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) MODULE_AUTHOR("Steffen Klassert <steffen.klassert@secunet.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) MODULE_DESCRIPTION("Parallel crypto wrapper");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) MODULE_ALIAS_CRYPTO("pcrypt");