Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/drivers/crypto/ccp/ccp-crypto-des3.c
26282 views
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 <[email protected]>
8
*/
9
10
#include <crypto/internal/des.h>
11
#include <crypto/internal/skcipher.h>
12
#include <linux/err.h>
13
#include <linux/kernel.h>
14
#include <linux/list.h>
15
#include <linux/module.h>
16
#include <linux/scatterlist.h>
17
#include <linux/slab.h>
18
#include <linux/string.h>
19
20
#include "ccp-crypto.h"
21
22
static int ccp_des3_complete(struct crypto_async_request *async_req, int ret)
23
{
24
struct skcipher_request *req = skcipher_request_cast(async_req);
25
struct ccp_ctx *ctx = crypto_skcipher_ctx_dma(
26
crypto_skcipher_reqtfm(req));
27
struct ccp_des3_req_ctx *rctx = skcipher_request_ctx_dma(req);
28
29
if (ret)
30
return ret;
31
32
if (ctx->u.des3.mode != CCP_DES3_MODE_ECB)
33
memcpy(req->iv, rctx->iv, DES3_EDE_BLOCK_SIZE);
34
35
return 0;
36
}
37
38
static int ccp_des3_setkey(struct crypto_skcipher *tfm, const u8 *key,
39
unsigned int key_len)
40
{
41
struct ccp_crypto_skcipher_alg *alg = ccp_crypto_skcipher_alg(tfm);
42
struct ccp_ctx *ctx = crypto_skcipher_ctx_dma(tfm);
43
int err;
44
45
err = verify_skcipher_des3_key(tfm, key);
46
if (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 skcipher_request *req, bool encrypt)
63
{
64
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
65
struct ccp_ctx *ctx = crypto_skcipher_ctx_dma(tfm);
66
struct ccp_des3_req_ctx *rctx = skcipher_request_ctx_dma(req);
67
struct scatterlist *iv_sg = NULL;
68
unsigned int iv_len = 0;
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->cryptlen & (DES3_EDE_BLOCK_SIZE - 1)))
76
return -EINVAL;
77
78
if (ctx->u.des3.mode != CCP_DES3_MODE_ECB) {
79
if (!req->iv)
80
return -EINVAL;
81
82
memcpy(rctx->iv, req->iv, 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->cryptlen;
102
rctx->cmd.u.des3.dst = req->dst;
103
104
return ccp_crypto_enqueue_request(&req->base, &rctx->cmd);
105
}
106
107
static int ccp_des3_encrypt(struct skcipher_request *req)
108
{
109
return ccp_des3_crypt(req, true);
110
}
111
112
static int ccp_des3_decrypt(struct skcipher_request *req)
113
{
114
return ccp_des3_crypt(req, false);
115
}
116
117
static int ccp_des3_init_tfm(struct crypto_skcipher *tfm)
118
{
119
struct ccp_ctx *ctx = crypto_skcipher_ctx_dma(tfm);
120
121
ctx->complete = ccp_des3_complete;
122
ctx->u.des3.key_len = 0;
123
124
crypto_skcipher_set_reqsize_dma(tfm, sizeof(struct ccp_des3_req_ctx));
125
126
return 0;
127
}
128
129
static const struct skcipher_alg ccp_des3_defaults = {
130
.setkey = ccp_des3_setkey,
131
.encrypt = ccp_des3_encrypt,
132
.decrypt = ccp_des3_decrypt,
133
.min_keysize = DES3_EDE_KEY_SIZE,
134
.max_keysize = DES3_EDE_KEY_SIZE,
135
.init = ccp_des3_init_tfm,
136
137
.base.cra_flags = CRYPTO_ALG_ASYNC |
138
CRYPTO_ALG_ALLOCATES_MEMORY |
139
CRYPTO_ALG_KERN_DRIVER_ONLY |
140
CRYPTO_ALG_NEED_FALLBACK,
141
.base.cra_blocksize = DES3_EDE_BLOCK_SIZE,
142
.base.cra_ctxsize = sizeof(struct ccp_ctx) + CRYPTO_DMA_PADDING,
143
.base.cra_priority = CCP_CRA_PRIORITY,
144
.base.cra_module = THIS_MODULE,
145
};
146
147
struct ccp_des3_def {
148
enum ccp_des3_mode mode;
149
unsigned int version;
150
const char *name;
151
const char *driver_name;
152
unsigned int blocksize;
153
unsigned int ivsize;
154
const struct skcipher_alg *alg_defaults;
155
};
156
157
static const struct ccp_des3_def des3_algs[] = {
158
{
159
.mode = CCP_DES3_MODE_ECB,
160
.version = CCP_VERSION(5, 0),
161
.name = "ecb(des3_ede)",
162
.driver_name = "ecb-des3-ccp",
163
.blocksize = DES3_EDE_BLOCK_SIZE,
164
.ivsize = 0,
165
.alg_defaults = &ccp_des3_defaults,
166
},
167
{
168
.mode = CCP_DES3_MODE_CBC,
169
.version = CCP_VERSION(5, 0),
170
.name = "cbc(des3_ede)",
171
.driver_name = "cbc-des3-ccp",
172
.blocksize = DES3_EDE_BLOCK_SIZE,
173
.ivsize = DES3_EDE_BLOCK_SIZE,
174
.alg_defaults = &ccp_des3_defaults,
175
},
176
};
177
178
static int ccp_register_des3_alg(struct list_head *head,
179
const struct ccp_des3_def *def)
180
{
181
struct ccp_crypto_skcipher_alg *ccp_alg;
182
struct skcipher_alg *alg;
183
int ret;
184
185
ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL);
186
if (!ccp_alg)
187
return -ENOMEM;
188
189
INIT_LIST_HEAD(&ccp_alg->entry);
190
191
ccp_alg->mode = def->mode;
192
193
/* Copy the defaults and override as necessary */
194
alg = &ccp_alg->alg;
195
*alg = *def->alg_defaults;
196
snprintf(alg->base.cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
197
snprintf(alg->base.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
198
def->driver_name);
199
alg->base.cra_blocksize = def->blocksize;
200
alg->ivsize = def->ivsize;
201
202
ret = crypto_register_skcipher(alg);
203
if (ret) {
204
pr_err("%s skcipher algorithm registration error (%d)\n",
205
alg->base.cra_name, ret);
206
kfree(ccp_alg);
207
return ret;
208
}
209
210
list_add(&ccp_alg->entry, head);
211
212
return 0;
213
}
214
215
int ccp_register_des3_algs(struct list_head *head)
216
{
217
int i, ret;
218
unsigned int ccpversion = ccp_version();
219
220
for (i = 0; i < ARRAY_SIZE(des3_algs); i++) {
221
if (des3_algs[i].version > ccpversion)
222
continue;
223
ret = ccp_register_des3_alg(head, &des3_algs[i]);
224
if (ret)
225
return ret;
226
}
227
228
return 0;
229
}
230
231