1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * AMD Cryptographic Coprocessor (CCP) DES3 crypto API support 4 * 5 * Copyright (C) 2016,2017 Advanced Micro Devices, Inc. 6 * 7 * Author: Gary R Hook <ghook@amd.com> 8 */ 9 10 #include <linux/module.h> 11 #include <linux/sched.h> 12 #include <linux/delay.h> 13 #include <linux/scatterlist.h> 14 #include <linux/crypto.h> 15 #include <crypto/algapi.h> 16 #include <crypto/scatterwalk.h> 17 #include <crypto/des.h> 18 19 #include "ccp-crypto.h" 20 21 static int ccp_des3_complete(struct crypto_async_request *async_req, int ret) 22 { 23 struct ablkcipher_request *req = ablkcipher_request_cast(async_req); 24 struct ccp_ctx *ctx = crypto_tfm_ctx(req->base.tfm); 25 struct ccp_des3_req_ctx *rctx = ablkcipher_request_ctx(req); 26 27 if (ret) 28 return ret; 29 30 if (ctx->u.des3.mode != CCP_DES3_MODE_ECB) 31 memcpy(req->info, rctx->iv, DES3_EDE_BLOCK_SIZE); 32 33 return 0; 34 } 35 36 static int ccp_des3_setkey(struct crypto_ablkcipher *tfm, const u8 *key, 37 unsigned int key_len) 38 { 39 struct ccp_ctx *ctx = crypto_tfm_ctx(crypto_ablkcipher_tfm(tfm)); 40 struct ccp_crypto_ablkcipher_alg *alg = 41 ccp_crypto_ablkcipher_alg(crypto_ablkcipher_tfm(tfm)); 42 u32 *flags = &tfm->base.crt_flags; 43 int err; 44 45 err = __des3_verify_key(flags, key); 46 if (unlikely(err)) 47 return err; 48 49 /* It's not clear that there is any support for a keysize of 112. 50 * If needed, the caller should make K1 == K3 51 */ 52 ctx->u.des3.type = CCP_DES3_TYPE_168; 53 ctx->u.des3.mode = alg->mode; 54 ctx->u.des3.key_len = key_len; 55 56 memcpy(ctx->u.des3.key, key, key_len); 57 sg_init_one(&ctx->u.des3.key_sg, ctx->u.des3.key, key_len); 58 59 return 0; 60 } 61 62 static int ccp_des3_crypt(struct ablkcipher_request *req, bool encrypt) 63 { 64 struct ccp_ctx *ctx = crypto_tfm_ctx(req->base.tfm); 65 struct ccp_des3_req_ctx *rctx = ablkcipher_request_ctx(req); 66 struct scatterlist *iv_sg = NULL; 67 unsigned int iv_len = 0; 68 int ret; 69 70 if (!ctx->u.des3.key_len) 71 return -EINVAL; 72 73 if (((ctx->u.des3.mode == CCP_DES3_MODE_ECB) || 74 (ctx->u.des3.mode == CCP_DES3_MODE_CBC)) && 75 (req->nbytes & (DES3_EDE_BLOCK_SIZE - 1))) 76 return -EINVAL; 77 78 if (ctx->u.des3.mode != CCP_DES3_MODE_ECB) { 79 if (!req->info) 80 return -EINVAL; 81 82 memcpy(rctx->iv, req->info, DES3_EDE_BLOCK_SIZE); 83 iv_sg = &rctx->iv_sg; 84 iv_len = DES3_EDE_BLOCK_SIZE; 85 sg_init_one(iv_sg, rctx->iv, iv_len); 86 } 87 88 memset(&rctx->cmd, 0, sizeof(rctx->cmd)); 89 INIT_LIST_HEAD(&rctx->cmd.entry); 90 rctx->cmd.engine = CCP_ENGINE_DES3; 91 rctx->cmd.u.des3.type = ctx->u.des3.type; 92 rctx->cmd.u.des3.mode = ctx->u.des3.mode; 93 rctx->cmd.u.des3.action = (encrypt) 94 ? CCP_DES3_ACTION_ENCRYPT 95 : CCP_DES3_ACTION_DECRYPT; 96 rctx->cmd.u.des3.key = &ctx->u.des3.key_sg; 97 rctx->cmd.u.des3.key_len = ctx->u.des3.key_len; 98 rctx->cmd.u.des3.iv = iv_sg; 99 rctx->cmd.u.des3.iv_len = iv_len; 100 rctx->cmd.u.des3.src = req->src; 101 rctx->cmd.u.des3.src_len = req->nbytes; 102 rctx->cmd.u.des3.dst = req->dst; 103 104 ret = ccp_crypto_enqueue_request(&req->base, &rctx->cmd); 105 106 return ret; 107 } 108 109 static int ccp_des3_encrypt(struct ablkcipher_request *req) 110 { 111 return ccp_des3_crypt(req, true); 112 } 113 114 static int ccp_des3_decrypt(struct ablkcipher_request *req) 115 { 116 return ccp_des3_crypt(req, false); 117 } 118 119 static int ccp_des3_cra_init(struct crypto_tfm *tfm) 120 { 121 struct ccp_ctx *ctx = crypto_tfm_ctx(tfm); 122 123 ctx->complete = ccp_des3_complete; 124 ctx->u.des3.key_len = 0; 125 126 tfm->crt_ablkcipher.reqsize = sizeof(struct ccp_des3_req_ctx); 127 128 return 0; 129 } 130 131 static void ccp_des3_cra_exit(struct crypto_tfm *tfm) 132 { 133 } 134 135 static struct crypto_alg ccp_des3_defaults = { 136 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | 137 CRYPTO_ALG_ASYNC | 138 CRYPTO_ALG_KERN_DRIVER_ONLY | 139 CRYPTO_ALG_NEED_FALLBACK, 140 .cra_blocksize = DES3_EDE_BLOCK_SIZE, 141 .cra_ctxsize = sizeof(struct ccp_ctx), 142 .cra_priority = CCP_CRA_PRIORITY, 143 .cra_type = &crypto_ablkcipher_type, 144 .cra_init = ccp_des3_cra_init, 145 .cra_exit = ccp_des3_cra_exit, 146 .cra_module = THIS_MODULE, 147 .cra_ablkcipher = { 148 .setkey = ccp_des3_setkey, 149 .encrypt = ccp_des3_encrypt, 150 .decrypt = ccp_des3_decrypt, 151 .min_keysize = DES3_EDE_KEY_SIZE, 152 .max_keysize = DES3_EDE_KEY_SIZE, 153 }, 154 }; 155 156 struct ccp_des3_def { 157 enum ccp_des3_mode mode; 158 unsigned int version; 159 const char *name; 160 const char *driver_name; 161 unsigned int blocksize; 162 unsigned int ivsize; 163 struct crypto_alg *alg_defaults; 164 }; 165 166 static struct ccp_des3_def des3_algs[] = { 167 { 168 .mode = CCP_DES3_MODE_ECB, 169 .version = CCP_VERSION(5, 0), 170 .name = "ecb(des3_ede)", 171 .driver_name = "ecb-des3-ccp", 172 .blocksize = DES3_EDE_BLOCK_SIZE, 173 .ivsize = 0, 174 .alg_defaults = &ccp_des3_defaults, 175 }, 176 { 177 .mode = CCP_DES3_MODE_CBC, 178 .version = CCP_VERSION(5, 0), 179 .name = "cbc(des3_ede)", 180 .driver_name = "cbc-des3-ccp", 181 .blocksize = DES3_EDE_BLOCK_SIZE, 182 .ivsize = DES3_EDE_BLOCK_SIZE, 183 .alg_defaults = &ccp_des3_defaults, 184 }, 185 }; 186 187 static int ccp_register_des3_alg(struct list_head *head, 188 const struct ccp_des3_def *def) 189 { 190 struct ccp_crypto_ablkcipher_alg *ccp_alg; 191 struct crypto_alg *alg; 192 int ret; 193 194 ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL); 195 if (!ccp_alg) 196 return -ENOMEM; 197 198 INIT_LIST_HEAD(&ccp_alg->entry); 199 200 ccp_alg->mode = def->mode; 201 202 /* Copy the defaults and override as necessary */ 203 alg = &ccp_alg->alg; 204 *alg = *def->alg_defaults; 205 snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name); 206 snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s", 207 def->driver_name); 208 alg->cra_blocksize = def->blocksize; 209 alg->cra_ablkcipher.ivsize = def->ivsize; 210 211 ret = crypto_register_alg(alg); 212 if (ret) { 213 pr_err("%s ablkcipher algorithm registration error (%d)\n", 214 alg->cra_name, ret); 215 kfree(ccp_alg); 216 return ret; 217 } 218 219 list_add(&ccp_alg->entry, head); 220 221 return 0; 222 } 223 224 int ccp_register_des3_algs(struct list_head *head) 225 { 226 int i, ret; 227 unsigned int ccpversion = ccp_version(); 228 229 for (i = 0; i < ARRAY_SIZE(des3_algs); i++) { 230 if (des3_algs[i].version > ccpversion) 231 continue; 232 ret = ccp_register_des3_alg(head, &des3_algs[i]); 233 if (ret) 234 return ret; 235 } 236 237 return 0; 238 } 239