Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Gary R Hook | 990672d | 2017-03-15 13:20:52 -0500 | [diff] [blame] | 2 | /* |
| 3 | * AMD Cryptographic Coprocessor (CCP) DES3 crypto API support |
| 4 | * |
Gary R Hook | 68cc652 | 2017-07-17 15:00:49 -0500 | [diff] [blame] | 5 | * Copyright (C) 2016,2017 Advanced Micro Devices, Inc. |
Gary R Hook | 990672d | 2017-03-15 13:20:52 -0500 | [diff] [blame] | 6 | * |
| 7 | * Author: Gary R Hook <ghook@amd.com> |
Gary R Hook | 990672d | 2017-03-15 13:20:52 -0500 | [diff] [blame] | 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> |
Ard Biesheuvel | b525041 | 2019-08-15 12:00:51 +0300 | [diff] [blame] | 17 | #include <crypto/internal/des.h> |
Gary R Hook | 990672d | 2017-03-15 13:20:52 -0500 | [diff] [blame] | 18 | |
| 19 | #include "ccp-crypto.h" |
| 20 | |
| 21 | static int ccp_des3_complete(struct crypto_async_request *async_req, int ret) |
| 22 | { |
Ard Biesheuvel | be9fe62 | 2019-11-09 18:09:29 +0100 | [diff] [blame] | 23 | struct skcipher_request *req = skcipher_request_cast(async_req); |
Gary R Hook | 990672d | 2017-03-15 13:20:52 -0500 | [diff] [blame] | 24 | struct ccp_ctx *ctx = crypto_tfm_ctx(req->base.tfm); |
Ard Biesheuvel | be9fe62 | 2019-11-09 18:09:29 +0100 | [diff] [blame] | 25 | struct ccp_des3_req_ctx *rctx = skcipher_request_ctx(req); |
Gary R Hook | 990672d | 2017-03-15 13:20:52 -0500 | [diff] [blame] | 26 | |
| 27 | if (ret) |
| 28 | return ret; |
| 29 | |
| 30 | if (ctx->u.des3.mode != CCP_DES3_MODE_ECB) |
Ard Biesheuvel | be9fe62 | 2019-11-09 18:09:29 +0100 | [diff] [blame] | 31 | memcpy(req->iv, rctx->iv, DES3_EDE_BLOCK_SIZE); |
Gary R Hook | 990672d | 2017-03-15 13:20:52 -0500 | [diff] [blame] | 32 | |
| 33 | return 0; |
| 34 | } |
| 35 | |
Ard Biesheuvel | be9fe62 | 2019-11-09 18:09:29 +0100 | [diff] [blame] | 36 | static int ccp_des3_setkey(struct crypto_skcipher *tfm, const u8 *key, |
Gary R Hook | 990672d | 2017-03-15 13:20:52 -0500 | [diff] [blame] | 37 | unsigned int key_len) |
| 38 | { |
Ard Biesheuvel | be9fe62 | 2019-11-09 18:09:29 +0100 | [diff] [blame] | 39 | struct ccp_crypto_skcipher_alg *alg = ccp_crypto_skcipher_alg(tfm); |
| 40 | struct ccp_ctx *ctx = crypto_skcipher_ctx(tfm); |
Herbert Xu | 76a329c | 2019-04-11 16:51:05 +0800 | [diff] [blame] | 41 | int err; |
Gary R Hook | 990672d | 2017-03-15 13:20:52 -0500 | [diff] [blame] | 42 | |
Ard Biesheuvel | be9fe62 | 2019-11-09 18:09:29 +0100 | [diff] [blame] | 43 | err = verify_skcipher_des3_key(tfm, key); |
Ard Biesheuvel | b525041 | 2019-08-15 12:00:51 +0300 | [diff] [blame] | 44 | if (err) |
Herbert Xu | 76a329c | 2019-04-11 16:51:05 +0800 | [diff] [blame] | 45 | return err; |
Gary R Hook | 990672d | 2017-03-15 13:20:52 -0500 | [diff] [blame] | 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 | |
Ard Biesheuvel | be9fe62 | 2019-11-09 18:09:29 +0100 | [diff] [blame] | 60 | static int ccp_des3_crypt(struct skcipher_request *req, bool encrypt) |
Gary R Hook | 990672d | 2017-03-15 13:20:52 -0500 | [diff] [blame] | 61 | { |
Ard Biesheuvel | be9fe62 | 2019-11-09 18:09:29 +0100 | [diff] [blame] | 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); |
Gary R Hook | 990672d | 2017-03-15 13:20:52 -0500 | [diff] [blame] | 65 | struct scatterlist *iv_sg = NULL; |
| 66 | unsigned int iv_len = 0; |
| 67 | int ret; |
| 68 | |
| 69 | if (!ctx->u.des3.key_len) |
| 70 | return -EINVAL; |
| 71 | |
| 72 | if (((ctx->u.des3.mode == CCP_DES3_MODE_ECB) || |
| 73 | (ctx->u.des3.mode == CCP_DES3_MODE_CBC)) && |
Ard Biesheuvel | be9fe62 | 2019-11-09 18:09:29 +0100 | [diff] [blame] | 74 | (req->cryptlen & (DES3_EDE_BLOCK_SIZE - 1))) |
Gary R Hook | 990672d | 2017-03-15 13:20:52 -0500 | [diff] [blame] | 75 | return -EINVAL; |
| 76 | |
| 77 | if (ctx->u.des3.mode != CCP_DES3_MODE_ECB) { |
Ard Biesheuvel | be9fe62 | 2019-11-09 18:09:29 +0100 | [diff] [blame] | 78 | if (!req->iv) |
Gary R Hook | 990672d | 2017-03-15 13:20:52 -0500 | [diff] [blame] | 79 | return -EINVAL; |
| 80 | |
Ard Biesheuvel | be9fe62 | 2019-11-09 18:09:29 +0100 | [diff] [blame] | 81 | memcpy(rctx->iv, req->iv, DES3_EDE_BLOCK_SIZE); |
Gary R Hook | 990672d | 2017-03-15 13:20:52 -0500 | [diff] [blame] | 82 | iv_sg = &rctx->iv_sg; |
| 83 | iv_len = DES3_EDE_BLOCK_SIZE; |
| 84 | sg_init_one(iv_sg, rctx->iv, iv_len); |
| 85 | } |
| 86 | |
| 87 | memset(&rctx->cmd, 0, sizeof(rctx->cmd)); |
| 88 | INIT_LIST_HEAD(&rctx->cmd.entry); |
| 89 | rctx->cmd.engine = CCP_ENGINE_DES3; |
| 90 | rctx->cmd.u.des3.type = ctx->u.des3.type; |
| 91 | rctx->cmd.u.des3.mode = ctx->u.des3.mode; |
| 92 | rctx->cmd.u.des3.action = (encrypt) |
| 93 | ? CCP_DES3_ACTION_ENCRYPT |
| 94 | : CCP_DES3_ACTION_DECRYPT; |
| 95 | rctx->cmd.u.des3.key = &ctx->u.des3.key_sg; |
| 96 | rctx->cmd.u.des3.key_len = ctx->u.des3.key_len; |
| 97 | rctx->cmd.u.des3.iv = iv_sg; |
| 98 | rctx->cmd.u.des3.iv_len = iv_len; |
| 99 | rctx->cmd.u.des3.src = req->src; |
Ard Biesheuvel | be9fe62 | 2019-11-09 18:09:29 +0100 | [diff] [blame] | 100 | rctx->cmd.u.des3.src_len = req->cryptlen; |
Gary R Hook | 990672d | 2017-03-15 13:20:52 -0500 | [diff] [blame] | 101 | rctx->cmd.u.des3.dst = req->dst; |
| 102 | |
| 103 | ret = ccp_crypto_enqueue_request(&req->base, &rctx->cmd); |
| 104 | |
| 105 | return ret; |
| 106 | } |
| 107 | |
Ard Biesheuvel | be9fe62 | 2019-11-09 18:09:29 +0100 | [diff] [blame] | 108 | static int ccp_des3_encrypt(struct skcipher_request *req) |
Gary R Hook | 990672d | 2017-03-15 13:20:52 -0500 | [diff] [blame] | 109 | { |
| 110 | return ccp_des3_crypt(req, true); |
| 111 | } |
| 112 | |
Ard Biesheuvel | be9fe62 | 2019-11-09 18:09:29 +0100 | [diff] [blame] | 113 | static int ccp_des3_decrypt(struct skcipher_request *req) |
Gary R Hook | 990672d | 2017-03-15 13:20:52 -0500 | [diff] [blame] | 114 | { |
| 115 | return ccp_des3_crypt(req, false); |
| 116 | } |
| 117 | |
Ard Biesheuvel | be9fe62 | 2019-11-09 18:09:29 +0100 | [diff] [blame] | 118 | static int ccp_des3_init_tfm(struct crypto_skcipher *tfm) |
Gary R Hook | 990672d | 2017-03-15 13:20:52 -0500 | [diff] [blame] | 119 | { |
Ard Biesheuvel | be9fe62 | 2019-11-09 18:09:29 +0100 | [diff] [blame] | 120 | struct ccp_ctx *ctx = crypto_skcipher_ctx(tfm); |
Gary R Hook | 990672d | 2017-03-15 13:20:52 -0500 | [diff] [blame] | 121 | |
| 122 | ctx->complete = ccp_des3_complete; |
| 123 | ctx->u.des3.key_len = 0; |
| 124 | |
Ard Biesheuvel | be9fe62 | 2019-11-09 18:09:29 +0100 | [diff] [blame] | 125 | crypto_skcipher_set_reqsize(tfm, sizeof(struct ccp_des3_req_ctx)); |
Gary R Hook | 990672d | 2017-03-15 13:20:52 -0500 | [diff] [blame] | 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
Ard Biesheuvel | be9fe62 | 2019-11-09 18:09:29 +0100 | [diff] [blame] | 130 | static const struct skcipher_alg ccp_des3_defaults = { |
| 131 | .setkey = ccp_des3_setkey, |
| 132 | .encrypt = ccp_des3_encrypt, |
| 133 | .decrypt = ccp_des3_decrypt, |
| 134 | .min_keysize = DES3_EDE_KEY_SIZE, |
| 135 | .max_keysize = DES3_EDE_KEY_SIZE, |
| 136 | .init = ccp_des3_init_tfm, |
Gary R Hook | 990672d | 2017-03-15 13:20:52 -0500 | [diff] [blame] | 137 | |
Ard Biesheuvel | be9fe62 | 2019-11-09 18:09:29 +0100 | [diff] [blame] | 138 | .base.cra_flags = CRYPTO_ALG_ASYNC | |
Mikulas Patocka | b8aa7dc | 2020-07-09 23:20:41 -0700 | [diff] [blame] | 139 | CRYPTO_ALG_ALLOCATES_MEMORY | |
Ard Biesheuvel | be9fe62 | 2019-11-09 18:09:29 +0100 | [diff] [blame] | 140 | CRYPTO_ALG_KERN_DRIVER_ONLY | |
| 141 | CRYPTO_ALG_NEED_FALLBACK, |
| 142 | .base.cra_blocksize = DES3_EDE_BLOCK_SIZE, |
| 143 | .base.cra_ctxsize = sizeof(struct ccp_ctx), |
| 144 | .base.cra_priority = CCP_CRA_PRIORITY, |
| 145 | .base.cra_module = THIS_MODULE, |
Gary R Hook | 990672d | 2017-03-15 13:20:52 -0500 | [diff] [blame] | 146 | }; |
| 147 | |
| 148 | struct ccp_des3_def { |
| 149 | enum ccp_des3_mode mode; |
| 150 | unsigned int version; |
| 151 | const char *name; |
| 152 | const char *driver_name; |
| 153 | unsigned int blocksize; |
| 154 | unsigned int ivsize; |
Ard Biesheuvel | be9fe62 | 2019-11-09 18:09:29 +0100 | [diff] [blame] | 155 | const struct skcipher_alg *alg_defaults; |
Gary R Hook | 990672d | 2017-03-15 13:20:52 -0500 | [diff] [blame] | 156 | }; |
| 157 | |
Ard Biesheuvel | be9fe62 | 2019-11-09 18:09:29 +0100 | [diff] [blame] | 158 | static const struct ccp_des3_def des3_algs[] = { |
Gary R Hook | 990672d | 2017-03-15 13:20:52 -0500 | [diff] [blame] | 159 | { |
| 160 | .mode = CCP_DES3_MODE_ECB, |
| 161 | .version = CCP_VERSION(5, 0), |
| 162 | .name = "ecb(des3_ede)", |
| 163 | .driver_name = "ecb-des3-ccp", |
| 164 | .blocksize = DES3_EDE_BLOCK_SIZE, |
| 165 | .ivsize = 0, |
| 166 | .alg_defaults = &ccp_des3_defaults, |
| 167 | }, |
| 168 | { |
| 169 | .mode = CCP_DES3_MODE_CBC, |
| 170 | .version = CCP_VERSION(5, 0), |
| 171 | .name = "cbc(des3_ede)", |
| 172 | .driver_name = "cbc-des3-ccp", |
| 173 | .blocksize = DES3_EDE_BLOCK_SIZE, |
| 174 | .ivsize = DES3_EDE_BLOCK_SIZE, |
| 175 | .alg_defaults = &ccp_des3_defaults, |
| 176 | }, |
| 177 | }; |
| 178 | |
| 179 | static int ccp_register_des3_alg(struct list_head *head, |
| 180 | const struct ccp_des3_def *def) |
| 181 | { |
Ard Biesheuvel | be9fe62 | 2019-11-09 18:09:29 +0100 | [diff] [blame] | 182 | struct ccp_crypto_skcipher_alg *ccp_alg; |
| 183 | struct skcipher_alg *alg; |
Gary R Hook | 990672d | 2017-03-15 13:20:52 -0500 | [diff] [blame] | 184 | int ret; |
| 185 | |
| 186 | ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL); |
| 187 | if (!ccp_alg) |
| 188 | return -ENOMEM; |
| 189 | |
| 190 | INIT_LIST_HEAD(&ccp_alg->entry); |
| 191 | |
| 192 | ccp_alg->mode = def->mode; |
| 193 | |
| 194 | /* Copy the defaults and override as necessary */ |
| 195 | alg = &ccp_alg->alg; |
| 196 | *alg = *def->alg_defaults; |
Ard Biesheuvel | be9fe62 | 2019-11-09 18:09:29 +0100 | [diff] [blame] | 197 | snprintf(alg->base.cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name); |
| 198 | snprintf(alg->base.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s", |
Gary R Hook | 990672d | 2017-03-15 13:20:52 -0500 | [diff] [blame] | 199 | def->driver_name); |
Ard Biesheuvel | be9fe62 | 2019-11-09 18:09:29 +0100 | [diff] [blame] | 200 | alg->base.cra_blocksize = def->blocksize; |
| 201 | alg->ivsize = def->ivsize; |
Gary R Hook | 990672d | 2017-03-15 13:20:52 -0500 | [diff] [blame] | 202 | |
Ard Biesheuvel | be9fe62 | 2019-11-09 18:09:29 +0100 | [diff] [blame] | 203 | ret = crypto_register_skcipher(alg); |
Gary R Hook | 990672d | 2017-03-15 13:20:52 -0500 | [diff] [blame] | 204 | if (ret) { |
Ard Biesheuvel | be9fe62 | 2019-11-09 18:09:29 +0100 | [diff] [blame] | 205 | pr_err("%s skcipher algorithm registration error (%d)\n", |
| 206 | alg->base.cra_name, ret); |
Gary R Hook | 990672d | 2017-03-15 13:20:52 -0500 | [diff] [blame] | 207 | kfree(ccp_alg); |
| 208 | return ret; |
| 209 | } |
| 210 | |
| 211 | list_add(&ccp_alg->entry, head); |
| 212 | |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | int ccp_register_des3_algs(struct list_head *head) |
| 217 | { |
| 218 | int i, ret; |
| 219 | unsigned int ccpversion = ccp_version(); |
| 220 | |
| 221 | for (i = 0; i < ARRAY_SIZE(des3_algs); i++) { |
| 222 | if (des3_algs[i].version > ccpversion) |
| 223 | continue; |
| 224 | ret = ccp_register_des3_alg(head, &des3_algs[i]); |
| 225 | if (ret) |
| 226 | return ret; |
| 227 | } |
| 228 | |
| 229 | return 0; |
| 230 | } |