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/internal/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 skcipher_request *req = skcipher_request_cast(async_req); 24 struct ccp_ctx *ctx = crypto_tfm_ctx(req->base.tfm); 25 struct ccp_des3_req_ctx *rctx = skcipher_request_ctx(req); 26 27 if (ret) 28 return ret; 29 30 if (ctx->u.des3.mode != CCP_DES3_MODE_ECB) 31 memcpy(req->iv, rctx->iv, DES3_EDE_BLOCK_SIZE); 32 33 return 0; 34 } 35 36 static int ccp_des3_setkey(struct crypto_skcipher *tfm, const u8 *key, 37 unsigned int key_len) 38 { 39 struct ccp_crypto_skcipher_alg *alg = ccp_crypto_skcipher_alg(tfm); 40 struct ccp_ctx *ctx = crypto_skcipher_ctx(tfm); 41 int err; 42 43 err = verify_skcipher_des3_key(tfm, key); 44 if (err) 45 return err; 46 47 /* It's not clear that there is any support for a keysize of 112. 48 * If needed, the caller should make K1 == K3 49 */ 50 ctx->u.des3.type = CCP_DES3_TYPE_168; 51 ctx->u.des3.mode = alg->mode; 52 ctx->u.des3.key_len = key_len; 53 54 memcpy(ctx->u.des3.key, key, key_len); 55 sg_init_one(&ctx->u.des3.key_sg, ctx->u.des3.key, key_len); 56 57 return 0; 58 } 59 60 static int ccp_des3_crypt(struct skcipher_request *req, bool encrypt) 61 { 62 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); 63 struct ccp_ctx *ctx = crypto_skcipher_ctx(tfm); 64 struct ccp_des3_req_ctx *rctx = skcipher_request_ctx(req); 65 struct scatterlist *iv_sg = NULL; 66 unsigned int iv_len = 0; 67 68 if (!ctx->u.des3.key_len) 69 return -EINVAL; 70 71 if (((ctx->u.des3.mode == CCP_DES3_MODE_ECB) || 72 (ctx->u.des3.mode == CCP_DES3_MODE_CBC)) && 73 (req->cryptlen & (DES3_EDE_BLOCK_SIZE - 1))) 74 return -EINVAL; 75 76 if (ctx->u.des3.mode != CCP_DES3_MODE_ECB) { 77 if (!req->iv) 78 return -EINVAL; 79 80 memcpy(rctx->iv, req->iv, DES3_EDE_BLOCK_SIZE); 81 iv_sg = &rctx->iv_sg; 82 iv_len = DES3_EDE_BLOCK_SIZE; 83 sg_init_one(iv_sg, rctx->iv, iv_len); 84 } 85 86 memset(&rctx->cmd, 0, sizeof(rctx->cmd)); 87 INIT_LIST_HEAD(&rctx->cmd.entry); 88 rctx->cmd.engine = CCP_ENGINE_DES3; 89 rctx->cmd.u.des3.type = ctx->u.des3.type; 90 rctx->cmd.u.des3.mode = ctx->u.des3.mode; 91 rctx->cmd.u.des3.action = (encrypt) 92 ? CCP_DES3_ACTION_ENCRYPT 93 : CCP_DES3_ACTION_DECRYPT; 94 rctx->cmd.u.des3.key = &ctx->u.des3.key_sg; 95 rctx->cmd.u.des3.key_len = ctx->u.des3.key_len; 96 rctx->cmd.u.des3.iv = iv_sg; 97 rctx->cmd.u.des3.iv_len = iv_len; 98 rctx->cmd.u.des3.src = req->src; 99 rctx->cmd.u.des3.src_len = req->cryptlen; 100 rctx->cmd.u.des3.dst = req->dst; 101 102 return ccp_crypto_enqueue_request(&req->base, &rctx->cmd); 103 } 104 105 static int ccp_des3_encrypt(struct skcipher_request *req) 106 { 107 return ccp_des3_crypt(req, true); 108 } 109 110 static int ccp_des3_decrypt(struct skcipher_request *req) 111 { 112 return ccp_des3_crypt(req, false); 113 } 114 115 static int ccp_des3_init_tfm(struct crypto_skcipher *tfm) 116 { 117 struct ccp_ctx *ctx = crypto_skcipher_ctx(tfm); 118 119 ctx->complete = ccp_des3_complete; 120 ctx->u.des3.key_len = 0; 121 122 crypto_skcipher_set_reqsize(tfm, sizeof(struct ccp_des3_req_ctx)); 123 124 return 0; 125 } 126 127 static const struct skcipher_alg ccp_des3_defaults = { 128 .setkey = ccp_des3_setkey, 129 .encrypt = ccp_des3_encrypt, 130 .decrypt = ccp_des3_decrypt, 131 .min_keysize = DES3_EDE_KEY_SIZE, 132 .max_keysize = DES3_EDE_KEY_SIZE, 133 .init = ccp_des3_init_tfm, 134 135 .base.cra_flags = CRYPTO_ALG_ASYNC | 136 CRYPTO_ALG_ALLOCATES_MEMORY | 137 CRYPTO_ALG_KERN_DRIVER_ONLY | 138 CRYPTO_ALG_NEED_FALLBACK, 139 .base.cra_blocksize = DES3_EDE_BLOCK_SIZE, 140 .base.cra_ctxsize = sizeof(struct ccp_ctx), 141 .base.cra_priority = CCP_CRA_PRIORITY, 142 .base.cra_module = THIS_MODULE, 143 }; 144 145 struct ccp_des3_def { 146 enum ccp_des3_mode mode; 147 unsigned int version; 148 const char *name; 149 const char *driver_name; 150 unsigned int blocksize; 151 unsigned int ivsize; 152 const struct skcipher_alg *alg_defaults; 153 }; 154 155 static const struct ccp_des3_def des3_algs[] = { 156 { 157 .mode = CCP_DES3_MODE_ECB, 158 .version = CCP_VERSION(5, 0), 159 .name = "ecb(des3_ede)", 160 .driver_name = "ecb-des3-ccp", 161 .blocksize = DES3_EDE_BLOCK_SIZE, 162 .ivsize = 0, 163 .alg_defaults = &ccp_des3_defaults, 164 }, 165 { 166 .mode = CCP_DES3_MODE_CBC, 167 .version = CCP_VERSION(5, 0), 168 .name = "cbc(des3_ede)", 169 .driver_name = "cbc-des3-ccp", 170 .blocksize = DES3_EDE_BLOCK_SIZE, 171 .ivsize = DES3_EDE_BLOCK_SIZE, 172 .alg_defaults = &ccp_des3_defaults, 173 }, 174 }; 175 176 static int ccp_register_des3_alg(struct list_head *head, 177 const struct ccp_des3_def *def) 178 { 179 struct ccp_crypto_skcipher_alg *ccp_alg; 180 struct skcipher_alg *alg; 181 int ret; 182 183 ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL); 184 if (!ccp_alg) 185 return -ENOMEM; 186 187 INIT_LIST_HEAD(&ccp_alg->entry); 188 189 ccp_alg->mode = def->mode; 190 191 /* Copy the defaults and override as necessary */ 192 alg = &ccp_alg->alg; 193 *alg = *def->alg_defaults; 194 snprintf(alg->base.cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name); 195 snprintf(alg->base.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s", 196 def->driver_name); 197 alg->base.cra_blocksize = def->blocksize; 198 alg->ivsize = def->ivsize; 199 200 ret = crypto_register_skcipher(alg); 201 if (ret) { 202 pr_err("%s skcipher algorithm registration error (%d)\n", 203 alg->base.cra_name, ret); 204 kfree(ccp_alg); 205 return ret; 206 } 207 208 list_add(&ccp_alg->entry, head); 209 210 return 0; 211 } 212 213 int ccp_register_des3_algs(struct list_head *head) 214 { 215 int i, ret; 216 unsigned int ccpversion = ccp_version(); 217 218 for (i = 0; i < ARRAY_SIZE(des3_algs); i++) { 219 if (des3_algs[i].version > ccpversion) 220 continue; 221 ret = ccp_register_des3_alg(head, &des3_algs[i]); 222 if (ret) 223 return ret; 224 } 225 226 return 0; 227 } 228