0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/module.h>
0011 #include <linux/sched.h>
0012 #include <linux/delay.h>
0013 #include <linux/scatterlist.h>
0014 #include <linux/crypto.h>
0015 #include <crypto/algapi.h>
0016 #include <crypto/scatterwalk.h>
0017 #include <crypto/internal/des.h>
0018
0019 #include "ccp-crypto.h"
0020
0021 static int ccp_des3_complete(struct crypto_async_request *async_req, int ret)
0022 {
0023 struct skcipher_request *req = skcipher_request_cast(async_req);
0024 struct ccp_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
0025 struct ccp_des3_req_ctx *rctx = skcipher_request_ctx(req);
0026
0027 if (ret)
0028 return ret;
0029
0030 if (ctx->u.des3.mode != CCP_DES3_MODE_ECB)
0031 memcpy(req->iv, rctx->iv, DES3_EDE_BLOCK_SIZE);
0032
0033 return 0;
0034 }
0035
0036 static int ccp_des3_setkey(struct crypto_skcipher *tfm, const u8 *key,
0037 unsigned int key_len)
0038 {
0039 struct ccp_crypto_skcipher_alg *alg = ccp_crypto_skcipher_alg(tfm);
0040 struct ccp_ctx *ctx = crypto_skcipher_ctx(tfm);
0041 int err;
0042
0043 err = verify_skcipher_des3_key(tfm, key);
0044 if (err)
0045 return err;
0046
0047
0048
0049
0050 ctx->u.des3.type = CCP_DES3_TYPE_168;
0051 ctx->u.des3.mode = alg->mode;
0052 ctx->u.des3.key_len = key_len;
0053
0054 memcpy(ctx->u.des3.key, key, key_len);
0055 sg_init_one(&ctx->u.des3.key_sg, ctx->u.des3.key, key_len);
0056
0057 return 0;
0058 }
0059
0060 static int ccp_des3_crypt(struct skcipher_request *req, bool encrypt)
0061 {
0062 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
0063 struct ccp_ctx *ctx = crypto_skcipher_ctx(tfm);
0064 struct ccp_des3_req_ctx *rctx = skcipher_request_ctx(req);
0065 struct scatterlist *iv_sg = NULL;
0066 unsigned int iv_len = 0;
0067 int ret;
0068
0069 if (!ctx->u.des3.key_len)
0070 return -EINVAL;
0071
0072 if (((ctx->u.des3.mode == CCP_DES3_MODE_ECB) ||
0073 (ctx->u.des3.mode == CCP_DES3_MODE_CBC)) &&
0074 (req->cryptlen & (DES3_EDE_BLOCK_SIZE - 1)))
0075 return -EINVAL;
0076
0077 if (ctx->u.des3.mode != CCP_DES3_MODE_ECB) {
0078 if (!req->iv)
0079 return -EINVAL;
0080
0081 memcpy(rctx->iv, req->iv, DES3_EDE_BLOCK_SIZE);
0082 iv_sg = &rctx->iv_sg;
0083 iv_len = DES3_EDE_BLOCK_SIZE;
0084 sg_init_one(iv_sg, rctx->iv, iv_len);
0085 }
0086
0087 memset(&rctx->cmd, 0, sizeof(rctx->cmd));
0088 INIT_LIST_HEAD(&rctx->cmd.entry);
0089 rctx->cmd.engine = CCP_ENGINE_DES3;
0090 rctx->cmd.u.des3.type = ctx->u.des3.type;
0091 rctx->cmd.u.des3.mode = ctx->u.des3.mode;
0092 rctx->cmd.u.des3.action = (encrypt)
0093 ? CCP_DES3_ACTION_ENCRYPT
0094 : CCP_DES3_ACTION_DECRYPT;
0095 rctx->cmd.u.des3.key = &ctx->u.des3.key_sg;
0096 rctx->cmd.u.des3.key_len = ctx->u.des3.key_len;
0097 rctx->cmd.u.des3.iv = iv_sg;
0098 rctx->cmd.u.des3.iv_len = iv_len;
0099 rctx->cmd.u.des3.src = req->src;
0100 rctx->cmd.u.des3.src_len = req->cryptlen;
0101 rctx->cmd.u.des3.dst = req->dst;
0102
0103 ret = ccp_crypto_enqueue_request(&req->base, &rctx->cmd);
0104
0105 return ret;
0106 }
0107
0108 static int ccp_des3_encrypt(struct skcipher_request *req)
0109 {
0110 return ccp_des3_crypt(req, true);
0111 }
0112
0113 static int ccp_des3_decrypt(struct skcipher_request *req)
0114 {
0115 return ccp_des3_crypt(req, false);
0116 }
0117
0118 static int ccp_des3_init_tfm(struct crypto_skcipher *tfm)
0119 {
0120 struct ccp_ctx *ctx = crypto_skcipher_ctx(tfm);
0121
0122 ctx->complete = ccp_des3_complete;
0123 ctx->u.des3.key_len = 0;
0124
0125 crypto_skcipher_set_reqsize(tfm, sizeof(struct ccp_des3_req_ctx));
0126
0127 return 0;
0128 }
0129
0130 static const struct skcipher_alg ccp_des3_defaults = {
0131 .setkey = ccp_des3_setkey,
0132 .encrypt = ccp_des3_encrypt,
0133 .decrypt = ccp_des3_decrypt,
0134 .min_keysize = DES3_EDE_KEY_SIZE,
0135 .max_keysize = DES3_EDE_KEY_SIZE,
0136 .init = ccp_des3_init_tfm,
0137
0138 .base.cra_flags = CRYPTO_ALG_ASYNC |
0139 CRYPTO_ALG_ALLOCATES_MEMORY |
0140 CRYPTO_ALG_KERN_DRIVER_ONLY |
0141 CRYPTO_ALG_NEED_FALLBACK,
0142 .base.cra_blocksize = DES3_EDE_BLOCK_SIZE,
0143 .base.cra_ctxsize = sizeof(struct ccp_ctx),
0144 .base.cra_priority = CCP_CRA_PRIORITY,
0145 .base.cra_module = THIS_MODULE,
0146 };
0147
0148 struct ccp_des3_def {
0149 enum ccp_des3_mode mode;
0150 unsigned int version;
0151 const char *name;
0152 const char *driver_name;
0153 unsigned int blocksize;
0154 unsigned int ivsize;
0155 const struct skcipher_alg *alg_defaults;
0156 };
0157
0158 static const struct ccp_des3_def des3_algs[] = {
0159 {
0160 .mode = CCP_DES3_MODE_ECB,
0161 .version = CCP_VERSION(5, 0),
0162 .name = "ecb(des3_ede)",
0163 .driver_name = "ecb-des3-ccp",
0164 .blocksize = DES3_EDE_BLOCK_SIZE,
0165 .ivsize = 0,
0166 .alg_defaults = &ccp_des3_defaults,
0167 },
0168 {
0169 .mode = CCP_DES3_MODE_CBC,
0170 .version = CCP_VERSION(5, 0),
0171 .name = "cbc(des3_ede)",
0172 .driver_name = "cbc-des3-ccp",
0173 .blocksize = DES3_EDE_BLOCK_SIZE,
0174 .ivsize = DES3_EDE_BLOCK_SIZE,
0175 .alg_defaults = &ccp_des3_defaults,
0176 },
0177 };
0178
0179 static int ccp_register_des3_alg(struct list_head *head,
0180 const struct ccp_des3_def *def)
0181 {
0182 struct ccp_crypto_skcipher_alg *ccp_alg;
0183 struct skcipher_alg *alg;
0184 int ret;
0185
0186 ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL);
0187 if (!ccp_alg)
0188 return -ENOMEM;
0189
0190 INIT_LIST_HEAD(&ccp_alg->entry);
0191
0192 ccp_alg->mode = def->mode;
0193
0194
0195 alg = &ccp_alg->alg;
0196 *alg = *def->alg_defaults;
0197 snprintf(alg->base.cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
0198 snprintf(alg->base.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
0199 def->driver_name);
0200 alg->base.cra_blocksize = def->blocksize;
0201 alg->ivsize = def->ivsize;
0202
0203 ret = crypto_register_skcipher(alg);
0204 if (ret) {
0205 pr_err("%s skcipher algorithm registration error (%d)\n",
0206 alg->base.cra_name, ret);
0207 kfree(ccp_alg);
0208 return ret;
0209 }
0210
0211 list_add(&ccp_alg->entry, head);
0212
0213 return 0;
0214 }
0215
0216 int ccp_register_des3_algs(struct list_head *head)
0217 {
0218 int i, ret;
0219 unsigned int ccpversion = ccp_version();
0220
0221 for (i = 0; i < ARRAY_SIZE(des3_algs); i++) {
0222 if (des3_algs[i].version > ccpversion)
0223 continue;
0224 ret = ccp_register_des3_alg(head, &des3_algs[i]);
0225 if (ret)
0226 return ret;
0227 }
0228
0229 return 0;
0230 }