.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
---|
1 | 2 | /* |
---|
2 | 3 | * AMD Cryptographic Coprocessor (CCP) DES3 crypto API support |
---|
3 | 4 | * |
---|
4 | 5 | * Copyright (C) 2016,2017 Advanced Micro Devices, Inc. |
---|
5 | 6 | * |
---|
6 | 7 | * 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 | 8 | */ |
---|
12 | 9 | |
---|
13 | 10 | #include <linux/module.h> |
---|
.. | .. |
---|
17 | 14 | #include <linux/crypto.h> |
---|
18 | 15 | #include <crypto/algapi.h> |
---|
19 | 16 | #include <crypto/scatterwalk.h> |
---|
20 | | -#include <crypto/des.h> |
---|
| 17 | +#include <crypto/internal/des.h> |
---|
21 | 18 | |
---|
22 | 19 | #include "ccp-crypto.h" |
---|
23 | 20 | |
---|
24 | 21 | static int ccp_des3_complete(struct crypto_async_request *async_req, int ret) |
---|
25 | 22 | { |
---|
26 | | - struct ablkcipher_request *req = ablkcipher_request_cast(async_req); |
---|
| 23 | + struct skcipher_request *req = skcipher_request_cast(async_req); |
---|
27 | 24 | struct ccp_ctx *ctx = crypto_tfm_ctx(req->base.tfm); |
---|
28 | | - struct ccp_des3_req_ctx *rctx = ablkcipher_request_ctx(req); |
---|
| 25 | + struct ccp_des3_req_ctx *rctx = skcipher_request_ctx(req); |
---|
29 | 26 | |
---|
30 | 27 | if (ret) |
---|
31 | 28 | return ret; |
---|
32 | 29 | |
---|
33 | 30 | if (ctx->u.des3.mode != CCP_DES3_MODE_ECB) |
---|
34 | | - memcpy(req->info, rctx->iv, DES3_EDE_BLOCK_SIZE); |
---|
| 31 | + memcpy(req->iv, rctx->iv, DES3_EDE_BLOCK_SIZE); |
---|
35 | 32 | |
---|
36 | 33 | return 0; |
---|
37 | 34 | } |
---|
38 | 35 | |
---|
39 | | -static int ccp_des3_setkey(struct crypto_ablkcipher *tfm, const u8 *key, |
---|
| 36 | +static int ccp_des3_setkey(struct crypto_skcipher *tfm, const u8 *key, |
---|
40 | 37 | unsigned int key_len) |
---|
41 | 38 | { |
---|
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; |
---|
| 39 | + struct ccp_crypto_skcipher_alg *alg = ccp_crypto_skcipher_alg(tfm); |
---|
| 40 | + struct ccp_ctx *ctx = crypto_skcipher_ctx(tfm); |
---|
| 41 | + int err; |
---|
46 | 42 | |
---|
47 | | - |
---|
48 | | - /* From des_generic.c: |
---|
49 | | - * |
---|
50 | | - * RFC2451: |
---|
51 | | - * If the first two or last two independent 64-bit keys are |
---|
52 | | - * equal (k1 == k2 or k2 == k3), then the DES3 operation is simply the |
---|
53 | | - * same as DES. Implementers MUST reject keys that exhibit this |
---|
54 | | - * property. |
---|
55 | | - */ |
---|
56 | | - const u32 *K = (const u32 *)key; |
---|
57 | | - |
---|
58 | | - if (unlikely(!((K[0] ^ K[2]) | (K[1] ^ K[3])) || |
---|
59 | | - !((K[2] ^ K[4]) | (K[3] ^ K[5]))) && |
---|
60 | | - (*flags & CRYPTO_TFM_REQ_WEAK_KEY)) { |
---|
61 | | - *flags |= CRYPTO_TFM_RES_WEAK_KEY; |
---|
62 | | - return -EINVAL; |
---|
63 | | - } |
---|
| 43 | + err = verify_skcipher_des3_key(tfm, key); |
---|
| 44 | + if (err) |
---|
| 45 | + return err; |
---|
64 | 46 | |
---|
65 | 47 | /* It's not clear that there is any support for a keysize of 112. |
---|
66 | 48 | * If needed, the caller should make K1 == K3 |
---|
.. | .. |
---|
75 | 57 | return 0; |
---|
76 | 58 | } |
---|
77 | 59 | |
---|
78 | | -static int ccp_des3_crypt(struct ablkcipher_request *req, bool encrypt) |
---|
| 60 | +static int ccp_des3_crypt(struct skcipher_request *req, bool encrypt) |
---|
79 | 61 | { |
---|
80 | | - struct ccp_ctx *ctx = crypto_tfm_ctx(req->base.tfm); |
---|
81 | | - struct ccp_des3_req_ctx *rctx = ablkcipher_request_ctx(req); |
---|
| 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); |
---|
82 | 65 | struct scatterlist *iv_sg = NULL; |
---|
83 | 66 | unsigned int iv_len = 0; |
---|
84 | 67 | int ret; |
---|
.. | .. |
---|
88 | 71 | |
---|
89 | 72 | if (((ctx->u.des3.mode == CCP_DES3_MODE_ECB) || |
---|
90 | 73 | (ctx->u.des3.mode == CCP_DES3_MODE_CBC)) && |
---|
91 | | - (req->nbytes & (DES3_EDE_BLOCK_SIZE - 1))) |
---|
| 74 | + (req->cryptlen & (DES3_EDE_BLOCK_SIZE - 1))) |
---|
92 | 75 | return -EINVAL; |
---|
93 | 76 | |
---|
94 | 77 | if (ctx->u.des3.mode != CCP_DES3_MODE_ECB) { |
---|
95 | | - if (!req->info) |
---|
| 78 | + if (!req->iv) |
---|
96 | 79 | return -EINVAL; |
---|
97 | 80 | |
---|
98 | | - memcpy(rctx->iv, req->info, DES3_EDE_BLOCK_SIZE); |
---|
| 81 | + memcpy(rctx->iv, req->iv, DES3_EDE_BLOCK_SIZE); |
---|
99 | 82 | iv_sg = &rctx->iv_sg; |
---|
100 | 83 | iv_len = DES3_EDE_BLOCK_SIZE; |
---|
101 | 84 | sg_init_one(iv_sg, rctx->iv, iv_len); |
---|
.. | .. |
---|
114 | 97 | rctx->cmd.u.des3.iv = iv_sg; |
---|
115 | 98 | rctx->cmd.u.des3.iv_len = iv_len; |
---|
116 | 99 | rctx->cmd.u.des3.src = req->src; |
---|
117 | | - rctx->cmd.u.des3.src_len = req->nbytes; |
---|
| 100 | + rctx->cmd.u.des3.src_len = req->cryptlen; |
---|
118 | 101 | rctx->cmd.u.des3.dst = req->dst; |
---|
119 | 102 | |
---|
120 | 103 | ret = ccp_crypto_enqueue_request(&req->base, &rctx->cmd); |
---|
.. | .. |
---|
122 | 105 | return ret; |
---|
123 | 106 | } |
---|
124 | 107 | |
---|
125 | | -static int ccp_des3_encrypt(struct ablkcipher_request *req) |
---|
| 108 | +static int ccp_des3_encrypt(struct skcipher_request *req) |
---|
126 | 109 | { |
---|
127 | 110 | return ccp_des3_crypt(req, true); |
---|
128 | 111 | } |
---|
129 | 112 | |
---|
130 | | -static int ccp_des3_decrypt(struct ablkcipher_request *req) |
---|
| 113 | +static int ccp_des3_decrypt(struct skcipher_request *req) |
---|
131 | 114 | { |
---|
132 | 115 | return ccp_des3_crypt(req, false); |
---|
133 | 116 | } |
---|
134 | 117 | |
---|
135 | | -static int ccp_des3_cra_init(struct crypto_tfm *tfm) |
---|
| 118 | +static int ccp_des3_init_tfm(struct crypto_skcipher *tfm) |
---|
136 | 119 | { |
---|
137 | | - struct ccp_ctx *ctx = crypto_tfm_ctx(tfm); |
---|
| 120 | + struct ccp_ctx *ctx = crypto_skcipher_ctx(tfm); |
---|
138 | 121 | |
---|
139 | 122 | ctx->complete = ccp_des3_complete; |
---|
140 | 123 | ctx->u.des3.key_len = 0; |
---|
141 | 124 | |
---|
142 | | - tfm->crt_ablkcipher.reqsize = sizeof(struct ccp_des3_req_ctx); |
---|
| 125 | + crypto_skcipher_set_reqsize(tfm, sizeof(struct ccp_des3_req_ctx)); |
---|
143 | 126 | |
---|
144 | 127 | return 0; |
---|
145 | 128 | } |
---|
146 | 129 | |
---|
147 | | -static void ccp_des3_cra_exit(struct crypto_tfm *tfm) |
---|
148 | | -{ |
---|
149 | | -} |
---|
| 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, |
---|
150 | 137 | |
---|
151 | | -static struct crypto_alg ccp_des3_defaults = { |
---|
152 | | - .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | |
---|
153 | | - CRYPTO_ALG_ASYNC | |
---|
154 | | - CRYPTO_ALG_KERN_DRIVER_ONLY | |
---|
155 | | - CRYPTO_ALG_NEED_FALLBACK, |
---|
156 | | - .cra_blocksize = DES3_EDE_BLOCK_SIZE, |
---|
157 | | - .cra_ctxsize = sizeof(struct ccp_ctx), |
---|
158 | | - .cra_priority = CCP_CRA_PRIORITY, |
---|
159 | | - .cra_type = &crypto_ablkcipher_type, |
---|
160 | | - .cra_init = ccp_des3_cra_init, |
---|
161 | | - .cra_exit = ccp_des3_cra_exit, |
---|
162 | | - .cra_module = THIS_MODULE, |
---|
163 | | - .cra_ablkcipher = { |
---|
164 | | - .setkey = ccp_des3_setkey, |
---|
165 | | - .encrypt = ccp_des3_encrypt, |
---|
166 | | - .decrypt = ccp_des3_decrypt, |
---|
167 | | - .min_keysize = DES3_EDE_KEY_SIZE, |
---|
168 | | - .max_keysize = DES3_EDE_KEY_SIZE, |
---|
169 | | - }, |
---|
| 138 | + .base.cra_flags = CRYPTO_ALG_ASYNC | |
---|
| 139 | + CRYPTO_ALG_ALLOCATES_MEMORY | |
---|
| 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, |
---|
170 | 146 | }; |
---|
171 | 147 | |
---|
172 | 148 | struct ccp_des3_def { |
---|
.. | .. |
---|
176 | 152 | const char *driver_name; |
---|
177 | 153 | unsigned int blocksize; |
---|
178 | 154 | unsigned int ivsize; |
---|
179 | | - struct crypto_alg *alg_defaults; |
---|
| 155 | + const struct skcipher_alg *alg_defaults; |
---|
180 | 156 | }; |
---|
181 | 157 | |
---|
182 | | -static struct ccp_des3_def des3_algs[] = { |
---|
| 158 | +static const struct ccp_des3_def des3_algs[] = { |
---|
183 | 159 | { |
---|
184 | 160 | .mode = CCP_DES3_MODE_ECB, |
---|
185 | 161 | .version = CCP_VERSION(5, 0), |
---|
.. | .. |
---|
203 | 179 | static int ccp_register_des3_alg(struct list_head *head, |
---|
204 | 180 | const struct ccp_des3_def *def) |
---|
205 | 181 | { |
---|
206 | | - struct ccp_crypto_ablkcipher_alg *ccp_alg; |
---|
207 | | - struct crypto_alg *alg; |
---|
| 182 | + struct ccp_crypto_skcipher_alg *ccp_alg; |
---|
| 183 | + struct skcipher_alg *alg; |
---|
208 | 184 | int ret; |
---|
209 | 185 | |
---|
210 | 186 | ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL); |
---|
.. | .. |
---|
218 | 194 | /* Copy the defaults and override as necessary */ |
---|
219 | 195 | alg = &ccp_alg->alg; |
---|
220 | 196 | *alg = *def->alg_defaults; |
---|
221 | | - snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name); |
---|
222 | | - snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s", |
---|
| 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", |
---|
223 | 199 | def->driver_name); |
---|
224 | | - alg->cra_blocksize = def->blocksize; |
---|
225 | | - alg->cra_ablkcipher.ivsize = def->ivsize; |
---|
| 200 | + alg->base.cra_blocksize = def->blocksize; |
---|
| 201 | + alg->ivsize = def->ivsize; |
---|
226 | 202 | |
---|
227 | | - ret = crypto_register_alg(alg); |
---|
| 203 | + ret = crypto_register_skcipher(alg); |
---|
228 | 204 | if (ret) { |
---|
229 | | - pr_err("%s ablkcipher algorithm registration error (%d)\n", |
---|
230 | | - alg->cra_name, ret); |
---|
| 205 | + pr_err("%s skcipher algorithm registration error (%d)\n", |
---|
| 206 | + alg->base.cra_name, ret); |
---|
231 | 207 | kfree(ccp_alg); |
---|
232 | 208 | return ret; |
---|
233 | 209 | } |
---|