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